xref: /openbsd-src/sys/dev/pci/drm/amd/amdgpu/amdgpu_kms.c (revision 25c4e8bd056e974b28f4a0ffd39d76c190a56013)
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/amdgpu_drm.h>
31 #include <drm/drm_drv.h>
32 #include "amdgpu_uvd.h"
33 #include "amdgpu_vce.h"
34 #include "atom.h"
35 
36 #include <linux/vga_switcheroo.h>
37 #include <linux/slab.h>
38 #include <linux/uaccess.h>
39 #include <linux/pci.h>
40 #include <linux/pm_runtime.h>
41 #include "amdgpu_amdkfd.h"
42 #include "amdgpu_gem.h"
43 #include "amdgpu_display.h"
44 #include "amdgpu_ras.h"
45 
46 void amdgpu_unregister_gpu_instance(struct amdgpu_device *adev)
47 {
48 	struct amdgpu_gpu_instance *gpu_instance;
49 	int i;
50 
51 	mutex_lock(&mgpu_info.mutex);
52 
53 	for (i = 0; i < mgpu_info.num_gpu; i++) {
54 		gpu_instance = &(mgpu_info.gpu_ins[i]);
55 		if (gpu_instance->adev == adev) {
56 			mgpu_info.gpu_ins[i] =
57 				mgpu_info.gpu_ins[mgpu_info.num_gpu - 1];
58 			mgpu_info.num_gpu--;
59 			if (adev->flags & AMD_IS_APU)
60 				mgpu_info.num_apu--;
61 			else
62 				mgpu_info.num_dgpu--;
63 			break;
64 		}
65 	}
66 
67 	mutex_unlock(&mgpu_info.mutex);
68 }
69 
70 #include <drm/drm_drv.h>
71 
72 #include "vga.h"
73 
74 #if NVGA > 0
75 #include <dev/ic/mc6845reg.h>
76 #include <dev/ic/pcdisplayvar.h>
77 #include <dev/ic/vgareg.h>
78 #include <dev/ic/vgavar.h>
79 
80 extern int vga_console_attached;
81 #endif
82 
83 #ifdef __amd64__
84 #include "efifb.h"
85 #include <machine/biosvar.h>
86 #endif
87 
88 #if NEFIFB > 0
89 #include <machine/efifbvar.h>
90 #endif
91 
92 int     amdgpu_probe(struct device *, void *, void *);
93 void    amdgpu_attach(struct device *, struct device *, void *);
94 int     amdgpu_detach(struct device *, int);
95 int     amdgpu_activate(struct device *, int);
96 void    amdgpu_attachhook(struct device *);
97 int     amdgpu_forcedetach(struct amdgpu_device *);
98 
99 bool	amdgpu_msi_ok(struct amdgpu_device *);
100 
101 extern const struct pci_device_id amdgpu_pciidlist[];
102 extern struct drm_driver amdgpu_kms_driver;
103 extern int amdgpu_exp_hw_support;
104 
105 /*
106  * set if the mountroot hook has a fatal error
107  * such as not being able to find the firmware
108  */
109 int amdgpu_fatal_error;
110 
111 const struct cfattach amdgpu_ca = {
112         sizeof (struct amdgpu_device), amdgpu_probe, amdgpu_attach,
113         amdgpu_detach, amdgpu_activate
114 };
115 
116 struct cfdriver amdgpu_cd = {
117         NULL, "amdgpu", DV_DULL
118 };
119 
120 #ifdef __linux__
121 /**
122  * amdgpu_driver_unload_kms - Main unload function for KMS.
123  *
124  * @dev: drm dev pointer
125  *
126  * This is the main unload function for KMS (all asics).
127  * Returns 0 on success.
128  */
129 void amdgpu_driver_unload_kms(struct drm_device *dev)
130 {
131 	struct amdgpu_device *adev = drm_to_adev(dev);
132 
133 	if (adev == NULL)
134 		return;
135 
136 	amdgpu_unregister_gpu_instance(adev);
137 
138 	if (adev->rmmio == NULL)
139 		return;
140 
141 	if (adev->runpm) {
142 		pm_runtime_get_sync(dev->dev);
143 		pm_runtime_forbid(dev->dev);
144 	}
145 
146 	if (amdgpu_acpi_smart_shift_update(dev, AMDGPU_SS_DRV_UNLOAD))
147 		DRM_WARN("smart shift update failed\n");
148 
149 	amdgpu_acpi_fini(adev);
150 	amdgpu_device_fini_hw(adev);
151 }
152 #endif /* __linux__ */
153 
154 void amdgpu_register_gpu_instance(struct amdgpu_device *adev)
155 {
156 	struct amdgpu_gpu_instance *gpu_instance;
157 
158 	mutex_lock(&mgpu_info.mutex);
159 
160 	if (mgpu_info.num_gpu >= MAX_GPU_INSTANCE) {
161 		DRM_ERROR("Cannot register more gpu instance\n");
162 		mutex_unlock(&mgpu_info.mutex);
163 		return;
164 	}
165 
166 	gpu_instance = &(mgpu_info.gpu_ins[mgpu_info.num_gpu]);
167 	gpu_instance->adev = adev;
168 	gpu_instance->mgpu_fan_enabled = 0;
169 
170 	mgpu_info.num_gpu++;
171 	if (adev->flags & AMD_IS_APU)
172 		mgpu_info.num_apu++;
173 	else
174 		mgpu_info.num_dgpu++;
175 
176 	mutex_unlock(&mgpu_info.mutex);
177 }
178 
179 static void amdgpu_get_audio_func(struct amdgpu_device *adev)
180 {
181 	STUB();
182 #ifdef notyet
183 	struct pci_dev *p = NULL;
184 
185 	p = pci_get_domain_bus_and_slot(pci_domain_nr(adev->pdev->bus),
186 			adev->pdev->bus->number, 1);
187 	if (p) {
188 		pm_runtime_get_sync(&p->dev);
189 
190 		pm_runtime_mark_last_busy(&p->dev);
191 		pm_runtime_put_autosuspend(&p->dev);
192 
193 		pci_dev_put(p);
194 	}
195 #endif
196 }
197 
198 #ifdef __linux__
199 /**
200  * amdgpu_driver_load_kms - Main load function for KMS.
201  *
202  * @adev: pointer to struct amdgpu_device
203  * @flags: device flags
204  *
205  * This is the main load function for KMS (all asics).
206  * Returns 0 on success, error on failure.
207  */
208 int amdgpu_driver_load_kms(struct amdgpu_device *adev, unsigned long flags)
209 {
210 	struct drm_device *dev;
211 	int r, acpi_status;
212 
213 	dev = adev_to_drm(adev);
214 
215 	/* amdgpu_device_init should report only fatal error
216 	 * like memory allocation failure or iomapping failure,
217 	 * or memory manager initialization failure, it must
218 	 * properly initialize the GPU MC controller and permit
219 	 * VRAM allocation
220 	 */
221 	r = amdgpu_device_init(adev, flags);
222 	if (r) {
223 		dev_err(dev->dev, "Fatal error during GPU init\n");
224 		goto out;
225 	}
226 
227 	if (amdgpu_device_supports_px(dev) &&
228 	    (amdgpu_runtime_pm != 0)) { /* enable runpm by default for atpx */
229 		adev->runpm = true;
230 		dev_info(adev->dev, "Using ATPX for runtime pm\n");
231 	} else if (amdgpu_device_supports_boco(dev) &&
232 		   (amdgpu_runtime_pm != 0)) { /* enable runpm by default for boco */
233 		adev->runpm = true;
234 		dev_info(adev->dev, "Using BOCO for runtime pm\n");
235 	} else if (amdgpu_device_supports_baco(dev) &&
236 		   (amdgpu_runtime_pm != 0)) {
237 		switch (adev->asic_type) {
238 		case CHIP_VEGA20:
239 		case CHIP_ARCTURUS:
240 			/* enable runpm if runpm=1 */
241 			if (amdgpu_runtime_pm > 0)
242 				adev->runpm = true;
243 			break;
244 		case CHIP_VEGA10:
245 			/* turn runpm on if noretry=0 */
246 			if (!adev->gmc.noretry)
247 				adev->runpm = true;
248 			break;
249 		default:
250 			/* enable runpm on CI+ */
251 			adev->runpm = true;
252 			break;
253 		}
254 		/* XXX: disable runtime pm if we are the primary adapter
255 		 * to avoid displays being re-enabled after DPMS.
256 		 * This needs to be sorted out and fixed properly.
257 		 */
258 		if (adev->is_fw_fb)
259 			adev->runpm = false;
260 		if (adev->runpm)
261 			dev_info(adev->dev, "Using BACO for runtime pm\n");
262 	}
263 
264 	/* Call ACPI methods: require modeset init
265 	 * but failure is not fatal
266 	 */
267 
268 	acpi_status = amdgpu_acpi_init(adev);
269 	if (acpi_status)
270 		dev_dbg(dev->dev, "Error during ACPI methods call\n");
271 
272 	if (adev->runpm) {
273 		/* only need to skip on ATPX */
274 		if (amdgpu_device_supports_px(dev))
275 			dev_pm_set_driver_flags(dev->dev, DPM_FLAG_NO_DIRECT_COMPLETE);
276 		/* we want direct complete for BOCO */
277 		if (amdgpu_device_supports_boco(dev))
278 			dev_pm_set_driver_flags(dev->dev, DPM_FLAG_SMART_PREPARE |
279 						DPM_FLAG_SMART_SUSPEND |
280 						DPM_FLAG_MAY_SKIP_RESUME);
281 		pm_runtime_use_autosuspend(dev->dev);
282 		pm_runtime_set_autosuspend_delay(dev->dev, 5000);
283 
284 		pm_runtime_allow(dev->dev);
285 
286 		pm_runtime_mark_last_busy(dev->dev);
287 		pm_runtime_put_autosuspend(dev->dev);
288 
289 		/*
290 		 * For runpm implemented via BACO, PMFW will handle the
291 		 * timing for BACO in and out:
292 		 *   - put ASIC into BACO state only when both video and
293 		 *     audio functions are in D3 state.
294 		 *   - pull ASIC out of BACO state when either video or
295 		 *     audio function is in D0 state.
296 		 * Also, at startup, PMFW assumes both functions are in
297 		 * D0 state.
298 		 *
299 		 * So if snd driver was loaded prior to amdgpu driver
300 		 * and audio function was put into D3 state, there will
301 		 * be no PMFW-aware D-state transition(D0->D3) on runpm
302 		 * suspend. Thus the BACO will be not correctly kicked in.
303 		 *
304 		 * Via amdgpu_get_audio_func(), the audio dev is put
305 		 * into D0 state. Then there will be a PMFW-aware D-state
306 		 * transition(D0->D3) on runpm suspend.
307 		 */
308 		if (amdgpu_device_supports_baco(dev) &&
309 		    !(adev->flags & AMD_IS_APU) &&
310 		    (adev->asic_type >= CHIP_NAVI10))
311 			amdgpu_get_audio_func(adev);
312 	}
313 
314 	if (amdgpu_acpi_smart_shift_update(dev, AMDGPU_SS_DRV_LOAD))
315 		DRM_WARN("smart shift update failed\n");
316 
317 out:
318 	if (r) {
319 		/* balance pm_runtime_get_sync in amdgpu_driver_unload_kms */
320 		if (adev->rmmio && adev->runpm)
321 			pm_runtime_put_noidle(dev->dev);
322 		amdgpu_driver_unload_kms(dev);
323 	}
324 
325 	return r;
326 }
327 #endif /* __linux__ */
328 
329 static int amdgpu_firmware_info(struct drm_amdgpu_info_firmware *fw_info,
330 				struct drm_amdgpu_query_fw *query_fw,
331 				struct amdgpu_device *adev)
332 {
333 	switch (query_fw->fw_type) {
334 	case AMDGPU_INFO_FW_VCE:
335 		fw_info->ver = adev->vce.fw_version;
336 		fw_info->feature = adev->vce.fb_version;
337 		break;
338 	case AMDGPU_INFO_FW_UVD:
339 		fw_info->ver = adev->uvd.fw_version;
340 		fw_info->feature = 0;
341 		break;
342 	case AMDGPU_INFO_FW_VCN:
343 		fw_info->ver = adev->vcn.fw_version;
344 		fw_info->feature = 0;
345 		break;
346 	case AMDGPU_INFO_FW_GMC:
347 		fw_info->ver = adev->gmc.fw_version;
348 		fw_info->feature = 0;
349 		break;
350 	case AMDGPU_INFO_FW_GFX_ME:
351 		fw_info->ver = adev->gfx.me_fw_version;
352 		fw_info->feature = adev->gfx.me_feature_version;
353 		break;
354 	case AMDGPU_INFO_FW_GFX_PFP:
355 		fw_info->ver = adev->gfx.pfp_fw_version;
356 		fw_info->feature = adev->gfx.pfp_feature_version;
357 		break;
358 	case AMDGPU_INFO_FW_GFX_CE:
359 		fw_info->ver = adev->gfx.ce_fw_version;
360 		fw_info->feature = adev->gfx.ce_feature_version;
361 		break;
362 	case AMDGPU_INFO_FW_GFX_RLC:
363 		fw_info->ver = adev->gfx.rlc_fw_version;
364 		fw_info->feature = adev->gfx.rlc_feature_version;
365 		break;
366 	case AMDGPU_INFO_FW_GFX_RLC_RESTORE_LIST_CNTL:
367 		fw_info->ver = adev->gfx.rlc_srlc_fw_version;
368 		fw_info->feature = adev->gfx.rlc_srlc_feature_version;
369 		break;
370 	case AMDGPU_INFO_FW_GFX_RLC_RESTORE_LIST_GPM_MEM:
371 		fw_info->ver = adev->gfx.rlc_srlg_fw_version;
372 		fw_info->feature = adev->gfx.rlc_srlg_feature_version;
373 		break;
374 	case AMDGPU_INFO_FW_GFX_RLC_RESTORE_LIST_SRM_MEM:
375 		fw_info->ver = adev->gfx.rlc_srls_fw_version;
376 		fw_info->feature = adev->gfx.rlc_srls_feature_version;
377 		break;
378 	case AMDGPU_INFO_FW_GFX_MEC:
379 		if (query_fw->index == 0) {
380 			fw_info->ver = adev->gfx.mec_fw_version;
381 			fw_info->feature = adev->gfx.mec_feature_version;
382 		} else if (query_fw->index == 1) {
383 			fw_info->ver = adev->gfx.mec2_fw_version;
384 			fw_info->feature = adev->gfx.mec2_feature_version;
385 		} else
386 			return -EINVAL;
387 		break;
388 	case AMDGPU_INFO_FW_SMC:
389 		fw_info->ver = adev->pm.fw_version;
390 		fw_info->feature = 0;
391 		break;
392 	case AMDGPU_INFO_FW_TA:
393 		switch (query_fw->index) {
394 		case TA_FW_TYPE_PSP_XGMI:
395 			fw_info->ver = adev->psp.ta_fw_version;
396 			fw_info->feature = adev->psp.xgmi.feature_version;
397 			break;
398 		case TA_FW_TYPE_PSP_RAS:
399 			fw_info->ver = adev->psp.ta_fw_version;
400 			fw_info->feature = adev->psp.ras.feature_version;
401 			break;
402 		case TA_FW_TYPE_PSP_HDCP:
403 			fw_info->ver = adev->psp.ta_fw_version;
404 			fw_info->feature = adev->psp.hdcp.feature_version;
405 			break;
406 		case TA_FW_TYPE_PSP_DTM:
407 			fw_info->ver = adev->psp.ta_fw_version;
408 			fw_info->feature = adev->psp.dtm.feature_version;
409 			break;
410 		case TA_FW_TYPE_PSP_RAP:
411 			fw_info->ver = adev->psp.ta_fw_version;
412 			fw_info->feature = adev->psp.rap.feature_version;
413 			break;
414 		case TA_FW_TYPE_PSP_SECUREDISPLAY:
415 			fw_info->ver = adev->psp.ta_fw_version;
416 			fw_info->feature = adev->psp.securedisplay.feature_version;
417 			break;
418 		default:
419 			return -EINVAL;
420 		}
421 		break;
422 	case AMDGPU_INFO_FW_SDMA:
423 		if (query_fw->index >= adev->sdma.num_instances)
424 			return -EINVAL;
425 		fw_info->ver = adev->sdma.instance[query_fw->index].fw_version;
426 		fw_info->feature = adev->sdma.instance[query_fw->index].feature_version;
427 		break;
428 	case AMDGPU_INFO_FW_SOS:
429 		fw_info->ver = adev->psp.sos.fw_version;
430 		fw_info->feature = adev->psp.sos.feature_version;
431 		break;
432 	case AMDGPU_INFO_FW_ASD:
433 		fw_info->ver = adev->psp.asd.fw_version;
434 		fw_info->feature = adev->psp.asd.feature_version;
435 		break;
436 	case AMDGPU_INFO_FW_DMCU:
437 		fw_info->ver = adev->dm.dmcu_fw_version;
438 		fw_info->feature = 0;
439 		break;
440 	case AMDGPU_INFO_FW_DMCUB:
441 		fw_info->ver = adev->dm.dmcub_fw_version;
442 		fw_info->feature = 0;
443 		break;
444 	case AMDGPU_INFO_FW_TOC:
445 		fw_info->ver = adev->psp.toc.fw_version;
446 		fw_info->feature = adev->psp.toc.feature_version;
447 		break;
448 	default:
449 		return -EINVAL;
450 	}
451 	return 0;
452 }
453 
454 static int amdgpu_hw_ip_info(struct amdgpu_device *adev,
455 			     struct drm_amdgpu_info *info,
456 			     struct drm_amdgpu_info_hw_ip *result)
457 {
458 	uint32_t ib_start_alignment = 0;
459 	uint32_t ib_size_alignment = 0;
460 	enum amd_ip_block_type type;
461 	unsigned int num_rings = 0;
462 	unsigned int i, j;
463 
464 	if (info->query_hw_ip.ip_instance >= AMDGPU_HW_IP_INSTANCE_MAX_COUNT)
465 		return -EINVAL;
466 
467 	switch (info->query_hw_ip.type) {
468 	case AMDGPU_HW_IP_GFX:
469 		type = AMD_IP_BLOCK_TYPE_GFX;
470 		for (i = 0; i < adev->gfx.num_gfx_rings; i++)
471 			if (adev->gfx.gfx_ring[i].sched.ready)
472 				++num_rings;
473 		ib_start_alignment = 32;
474 		ib_size_alignment = 32;
475 		break;
476 	case AMDGPU_HW_IP_COMPUTE:
477 		type = AMD_IP_BLOCK_TYPE_GFX;
478 		for (i = 0; i < adev->gfx.num_compute_rings; i++)
479 			if (adev->gfx.compute_ring[i].sched.ready)
480 				++num_rings;
481 		ib_start_alignment = 32;
482 		ib_size_alignment = 32;
483 		break;
484 	case AMDGPU_HW_IP_DMA:
485 		type = AMD_IP_BLOCK_TYPE_SDMA;
486 		for (i = 0; i < adev->sdma.num_instances; i++)
487 			if (adev->sdma.instance[i].ring.sched.ready)
488 				++num_rings;
489 		ib_start_alignment = 256;
490 		ib_size_alignment = 4;
491 		break;
492 	case AMDGPU_HW_IP_UVD:
493 		type = AMD_IP_BLOCK_TYPE_UVD;
494 		for (i = 0; i < adev->uvd.num_uvd_inst; i++) {
495 			if (adev->uvd.harvest_config & (1 << i))
496 				continue;
497 
498 			if (adev->uvd.inst[i].ring.sched.ready)
499 				++num_rings;
500 		}
501 		ib_start_alignment = 64;
502 		ib_size_alignment = 64;
503 		break;
504 	case AMDGPU_HW_IP_VCE:
505 		type = AMD_IP_BLOCK_TYPE_VCE;
506 		for (i = 0; i < adev->vce.num_rings; i++)
507 			if (adev->vce.ring[i].sched.ready)
508 				++num_rings;
509 		ib_start_alignment = 4;
510 		ib_size_alignment = 1;
511 		break;
512 	case AMDGPU_HW_IP_UVD_ENC:
513 		type = AMD_IP_BLOCK_TYPE_UVD;
514 		for (i = 0; i < adev->uvd.num_uvd_inst; i++) {
515 			if (adev->uvd.harvest_config & (1 << i))
516 				continue;
517 
518 			for (j = 0; j < adev->uvd.num_enc_rings; j++)
519 				if (adev->uvd.inst[i].ring_enc[j].sched.ready)
520 					++num_rings;
521 		}
522 		ib_start_alignment = 64;
523 		ib_size_alignment = 64;
524 		break;
525 	case AMDGPU_HW_IP_VCN_DEC:
526 		type = AMD_IP_BLOCK_TYPE_VCN;
527 		for (i = 0; i < adev->vcn.num_vcn_inst; i++) {
528 			if (adev->uvd.harvest_config & (1 << i))
529 				continue;
530 
531 			if (adev->vcn.inst[i].ring_dec.sched.ready)
532 				++num_rings;
533 		}
534 		ib_start_alignment = 16;
535 		ib_size_alignment = 16;
536 		break;
537 	case AMDGPU_HW_IP_VCN_ENC:
538 		type = AMD_IP_BLOCK_TYPE_VCN;
539 		for (i = 0; i < adev->vcn.num_vcn_inst; i++) {
540 			if (adev->uvd.harvest_config & (1 << i))
541 				continue;
542 
543 			for (j = 0; j < adev->vcn.num_enc_rings; j++)
544 				if (adev->vcn.inst[i].ring_enc[j].sched.ready)
545 					++num_rings;
546 		}
547 		ib_start_alignment = 64;
548 		ib_size_alignment = 1;
549 		break;
550 	case AMDGPU_HW_IP_VCN_JPEG:
551 		type = (amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_JPEG)) ?
552 			AMD_IP_BLOCK_TYPE_JPEG : AMD_IP_BLOCK_TYPE_VCN;
553 
554 		for (i = 0; i < adev->jpeg.num_jpeg_inst; i++) {
555 			if (adev->jpeg.harvest_config & (1 << i))
556 				continue;
557 
558 			if (adev->jpeg.inst[i].ring_dec.sched.ready)
559 				++num_rings;
560 		}
561 		ib_start_alignment = 16;
562 		ib_size_alignment = 16;
563 		break;
564 	default:
565 		return -EINVAL;
566 	}
567 
568 	for (i = 0; i < adev->num_ip_blocks; i++)
569 		if (adev->ip_blocks[i].version->type == type &&
570 		    adev->ip_blocks[i].status.valid)
571 			break;
572 
573 	if (i == adev->num_ip_blocks)
574 		return 0;
575 
576 	num_rings = min(amdgpu_ctx_num_entities[info->query_hw_ip.type],
577 			num_rings);
578 
579 	result->hw_ip_version_major = adev->ip_blocks[i].version->major;
580 	result->hw_ip_version_minor = adev->ip_blocks[i].version->minor;
581 	result->capabilities_flags = 0;
582 	result->available_rings = (1 << num_rings) - 1;
583 	result->ib_start_alignment = ib_start_alignment;
584 	result->ib_size_alignment = ib_size_alignment;
585 	return 0;
586 }
587 
588 /*
589  * Userspace get information ioctl
590  */
591 /**
592  * amdgpu_info_ioctl - answer a device specific request.
593  *
594  * @dev: drm device pointer
595  * @data: request object
596  * @filp: drm filp
597  *
598  * This function is used to pass device specific parameters to the userspace
599  * drivers.  Examples include: pci device id, pipeline parms, tiling params,
600  * etc. (all asics).
601  * Returns 0 on success, -EINVAL on failure.
602  */
603 int amdgpu_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
604 {
605 	struct amdgpu_device *adev = drm_to_adev(dev);
606 	struct drm_amdgpu_info *info = data;
607 	struct amdgpu_mode_info *minfo = &adev->mode_info;
608 	void __user *out = (void __user *)(uintptr_t)info->return_pointer;
609 	uint32_t size = info->return_size;
610 	struct drm_crtc *crtc;
611 	uint32_t ui32 = 0;
612 	uint64_t ui64 = 0;
613 	int i, found;
614 	int ui32_size = sizeof(ui32);
615 
616 	if (!info->return_size || !info->return_pointer)
617 		return -EINVAL;
618 
619 	switch (info->query) {
620 	case AMDGPU_INFO_ACCEL_WORKING:
621 		ui32 = adev->accel_working;
622 		return copy_to_user(out, &ui32, min(size, 4u)) ? -EFAULT : 0;
623 	case AMDGPU_INFO_CRTC_FROM_ID:
624 		for (i = 0, found = 0; i < adev->mode_info.num_crtc; i++) {
625 			crtc = (struct drm_crtc *)minfo->crtcs[i];
626 			if (crtc && crtc->base.id == info->mode_crtc.id) {
627 				struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
628 				ui32 = amdgpu_crtc->crtc_id;
629 				found = 1;
630 				break;
631 			}
632 		}
633 		if (!found) {
634 			DRM_DEBUG_KMS("unknown crtc id %d\n", info->mode_crtc.id);
635 			return -EINVAL;
636 		}
637 		return copy_to_user(out, &ui32, min(size, 4u)) ? -EFAULT : 0;
638 	case AMDGPU_INFO_HW_IP_INFO: {
639 		struct drm_amdgpu_info_hw_ip ip = {};
640 		int ret;
641 
642 		ret = amdgpu_hw_ip_info(adev, info, &ip);
643 		if (ret)
644 			return ret;
645 
646 		ret = copy_to_user(out, &ip, min((size_t)size, sizeof(ip)));
647 		return ret ? -EFAULT : 0;
648 	}
649 	case AMDGPU_INFO_HW_IP_COUNT: {
650 		enum amd_ip_block_type type;
651 		uint32_t count = 0;
652 
653 		switch (info->query_hw_ip.type) {
654 		case AMDGPU_HW_IP_GFX:
655 			type = AMD_IP_BLOCK_TYPE_GFX;
656 			break;
657 		case AMDGPU_HW_IP_COMPUTE:
658 			type = AMD_IP_BLOCK_TYPE_GFX;
659 			break;
660 		case AMDGPU_HW_IP_DMA:
661 			type = AMD_IP_BLOCK_TYPE_SDMA;
662 			break;
663 		case AMDGPU_HW_IP_UVD:
664 			type = AMD_IP_BLOCK_TYPE_UVD;
665 			break;
666 		case AMDGPU_HW_IP_VCE:
667 			type = AMD_IP_BLOCK_TYPE_VCE;
668 			break;
669 		case AMDGPU_HW_IP_UVD_ENC:
670 			type = AMD_IP_BLOCK_TYPE_UVD;
671 			break;
672 		case AMDGPU_HW_IP_VCN_DEC:
673 		case AMDGPU_HW_IP_VCN_ENC:
674 			type = AMD_IP_BLOCK_TYPE_VCN;
675 			break;
676 		case AMDGPU_HW_IP_VCN_JPEG:
677 			type = (amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_JPEG)) ?
678 				AMD_IP_BLOCK_TYPE_JPEG : AMD_IP_BLOCK_TYPE_VCN;
679 			break;
680 		default:
681 			return -EINVAL;
682 		}
683 
684 		for (i = 0; i < adev->num_ip_blocks; i++)
685 			if (adev->ip_blocks[i].version->type == type &&
686 			    adev->ip_blocks[i].status.valid &&
687 			    count < AMDGPU_HW_IP_INSTANCE_MAX_COUNT)
688 				count++;
689 
690 		return copy_to_user(out, &count, min(size, 4u)) ? -EFAULT : 0;
691 	}
692 	case AMDGPU_INFO_TIMESTAMP:
693 		ui64 = amdgpu_gfx_get_gpu_clock_counter(adev);
694 		return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
695 	case AMDGPU_INFO_FW_VERSION: {
696 		struct drm_amdgpu_info_firmware fw_info;
697 		int ret;
698 
699 		/* We only support one instance of each IP block right now. */
700 		if (info->query_fw.ip_instance != 0)
701 			return -EINVAL;
702 
703 		ret = amdgpu_firmware_info(&fw_info, &info->query_fw, adev);
704 		if (ret)
705 			return ret;
706 
707 		return copy_to_user(out, &fw_info,
708 				    min((size_t)size, sizeof(fw_info))) ? -EFAULT : 0;
709 	}
710 	case AMDGPU_INFO_NUM_BYTES_MOVED:
711 		ui64 = atomic64_read(&adev->num_bytes_moved);
712 		return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
713 	case AMDGPU_INFO_NUM_EVICTIONS:
714 		ui64 = atomic64_read(&adev->num_evictions);
715 		return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
716 	case AMDGPU_INFO_NUM_VRAM_CPU_PAGE_FAULTS:
717 		ui64 = atomic64_read(&adev->num_vram_cpu_page_faults);
718 		return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
719 	case AMDGPU_INFO_VRAM_USAGE:
720 		ui64 = amdgpu_vram_mgr_usage(ttm_manager_type(&adev->mman.bdev, TTM_PL_VRAM));
721 		return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
722 	case AMDGPU_INFO_VIS_VRAM_USAGE:
723 		ui64 = amdgpu_vram_mgr_vis_usage(ttm_manager_type(&adev->mman.bdev, TTM_PL_VRAM));
724 		return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
725 	case AMDGPU_INFO_GTT_USAGE:
726 		ui64 = amdgpu_gtt_mgr_usage(ttm_manager_type(&adev->mman.bdev, TTM_PL_TT));
727 		return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
728 	case AMDGPU_INFO_GDS_CONFIG: {
729 		struct drm_amdgpu_info_gds gds_info;
730 
731 		memset(&gds_info, 0, sizeof(gds_info));
732 		gds_info.compute_partition_size = adev->gds.gds_size;
733 		gds_info.gds_total_size = adev->gds.gds_size;
734 		gds_info.gws_per_compute_partition = adev->gds.gws_size;
735 		gds_info.oa_per_compute_partition = adev->gds.oa_size;
736 		return copy_to_user(out, &gds_info,
737 				    min((size_t)size, sizeof(gds_info))) ? -EFAULT : 0;
738 	}
739 	case AMDGPU_INFO_VRAM_GTT: {
740 		struct drm_amdgpu_info_vram_gtt vram_gtt;
741 
742 		vram_gtt.vram_size = adev->gmc.real_vram_size -
743 			atomic64_read(&adev->vram_pin_size) -
744 			AMDGPU_VM_RESERVED_VRAM;
745 		vram_gtt.vram_cpu_accessible_size =
746 			min(adev->gmc.visible_vram_size -
747 			    atomic64_read(&adev->visible_pin_size),
748 			    vram_gtt.vram_size);
749 		vram_gtt.gtt_size = ttm_manager_type(&adev->mman.bdev, TTM_PL_TT)->size;
750 		vram_gtt.gtt_size *= PAGE_SIZE;
751 		vram_gtt.gtt_size -= atomic64_read(&adev->gart_pin_size);
752 		return copy_to_user(out, &vram_gtt,
753 				    min((size_t)size, sizeof(vram_gtt))) ? -EFAULT : 0;
754 	}
755 	case AMDGPU_INFO_MEMORY: {
756 		struct drm_amdgpu_memory_info mem;
757 		struct ttm_resource_manager *vram_man =
758 			ttm_manager_type(&adev->mman.bdev, TTM_PL_VRAM);
759 		struct ttm_resource_manager *gtt_man =
760 			ttm_manager_type(&adev->mman.bdev, TTM_PL_TT);
761 		memset(&mem, 0, sizeof(mem));
762 		mem.vram.total_heap_size = adev->gmc.real_vram_size;
763 		mem.vram.usable_heap_size = adev->gmc.real_vram_size -
764 			atomic64_read(&adev->vram_pin_size) -
765 			AMDGPU_VM_RESERVED_VRAM;
766 		mem.vram.heap_usage =
767 			amdgpu_vram_mgr_usage(vram_man);
768 		mem.vram.max_allocation = mem.vram.usable_heap_size * 3 / 4;
769 
770 		mem.cpu_accessible_vram.total_heap_size =
771 			adev->gmc.visible_vram_size;
772 		mem.cpu_accessible_vram.usable_heap_size =
773 			min(adev->gmc.visible_vram_size -
774 			    atomic64_read(&adev->visible_pin_size),
775 			    mem.vram.usable_heap_size);
776 		mem.cpu_accessible_vram.heap_usage =
777 			amdgpu_vram_mgr_vis_usage(vram_man);
778 		mem.cpu_accessible_vram.max_allocation =
779 			mem.cpu_accessible_vram.usable_heap_size * 3 / 4;
780 
781 		mem.gtt.total_heap_size = gtt_man->size;
782 		mem.gtt.total_heap_size *= PAGE_SIZE;
783 		mem.gtt.usable_heap_size = mem.gtt.total_heap_size -
784 			atomic64_read(&adev->gart_pin_size);
785 		mem.gtt.heap_usage =
786 			amdgpu_gtt_mgr_usage(gtt_man);
787 		mem.gtt.max_allocation = mem.gtt.usable_heap_size * 3 / 4;
788 
789 		return copy_to_user(out, &mem,
790 				    min((size_t)size, sizeof(mem)))
791 				    ? -EFAULT : 0;
792 	}
793 	case AMDGPU_INFO_READ_MMR_REG: {
794 		unsigned n, alloc_size;
795 		uint32_t *regs;
796 		unsigned se_num = (info->read_mmr_reg.instance >>
797 				   AMDGPU_INFO_MMR_SE_INDEX_SHIFT) &
798 				  AMDGPU_INFO_MMR_SE_INDEX_MASK;
799 		unsigned sh_num = (info->read_mmr_reg.instance >>
800 				   AMDGPU_INFO_MMR_SH_INDEX_SHIFT) &
801 				  AMDGPU_INFO_MMR_SH_INDEX_MASK;
802 
803 		/* set full masks if the userspace set all bits
804 		 * in the bitfields */
805 		if (se_num == AMDGPU_INFO_MMR_SE_INDEX_MASK)
806 			se_num = 0xffffffff;
807 		else if (se_num >= AMDGPU_GFX_MAX_SE)
808 			return -EINVAL;
809 		if (sh_num == AMDGPU_INFO_MMR_SH_INDEX_MASK)
810 			sh_num = 0xffffffff;
811 		else if (sh_num >= AMDGPU_GFX_MAX_SH_PER_SE)
812 			return -EINVAL;
813 
814 		if (info->read_mmr_reg.count > 128)
815 			return -EINVAL;
816 
817 		regs = kmalloc_array(info->read_mmr_reg.count, sizeof(*regs), GFP_KERNEL);
818 		if (!regs)
819 			return -ENOMEM;
820 		alloc_size = info->read_mmr_reg.count * sizeof(*regs);
821 
822 		amdgpu_gfx_off_ctrl(adev, false);
823 		for (i = 0; i < info->read_mmr_reg.count; i++) {
824 			if (amdgpu_asic_read_register(adev, se_num, sh_num,
825 						      info->read_mmr_reg.dword_offset + i,
826 						      &regs[i])) {
827 				DRM_DEBUG_KMS("unallowed offset %#x\n",
828 					      info->read_mmr_reg.dword_offset + i);
829 				kfree(regs);
830 				amdgpu_gfx_off_ctrl(adev, true);
831 				return -EFAULT;
832 			}
833 		}
834 		amdgpu_gfx_off_ctrl(adev, true);
835 		n = copy_to_user(out, regs, min(size, alloc_size));
836 		kfree(regs);
837 		return n ? -EFAULT : 0;
838 	}
839 	case AMDGPU_INFO_DEV_INFO: {
840 		struct drm_amdgpu_info_device *dev_info;
841 		uint64_t vm_size;
842 		int ret;
843 
844 		dev_info = kzalloc(sizeof(*dev_info), GFP_KERNEL);
845 		if (!dev_info)
846 			return -ENOMEM;
847 
848 		dev_info->device_id = adev->pdev->device;
849 		dev_info->chip_rev = adev->rev_id;
850 		dev_info->external_rev = adev->external_rev_id;
851 		dev_info->pci_rev = adev->pdev->revision;
852 		dev_info->family = adev->family;
853 		dev_info->num_shader_engines = adev->gfx.config.max_shader_engines;
854 		dev_info->num_shader_arrays_per_engine = adev->gfx.config.max_sh_per_se;
855 		/* return all clocks in KHz */
856 		dev_info->gpu_counter_freq = amdgpu_asic_get_xclk(adev) * 10;
857 		if (adev->pm.dpm_enabled) {
858 			dev_info->max_engine_clock = amdgpu_dpm_get_sclk(adev, false) * 10;
859 			dev_info->max_memory_clock = amdgpu_dpm_get_mclk(adev, false) * 10;
860 		} else {
861 			dev_info->max_engine_clock = adev->clock.default_sclk * 10;
862 			dev_info->max_memory_clock = adev->clock.default_mclk * 10;
863 		}
864 		dev_info->enabled_rb_pipes_mask = adev->gfx.config.backend_enable_mask;
865 		dev_info->num_rb_pipes = adev->gfx.config.max_backends_per_se *
866 			adev->gfx.config.max_shader_engines;
867 		dev_info->num_hw_gfx_contexts = adev->gfx.config.max_hw_contexts;
868 		dev_info->_pad = 0;
869 		dev_info->ids_flags = 0;
870 		if (adev->flags & AMD_IS_APU)
871 			dev_info->ids_flags |= AMDGPU_IDS_FLAGS_FUSION;
872 		if (amdgpu_mcbp || amdgpu_sriov_vf(adev))
873 			dev_info->ids_flags |= AMDGPU_IDS_FLAGS_PREEMPTION;
874 		if (amdgpu_is_tmz(adev))
875 			dev_info->ids_flags |= AMDGPU_IDS_FLAGS_TMZ;
876 
877 		vm_size = adev->vm_manager.max_pfn * AMDGPU_GPU_PAGE_SIZE;
878 		vm_size -= AMDGPU_VA_RESERVED_SIZE;
879 
880 		/* Older VCE FW versions are buggy and can handle only 40bits */
881 		if (adev->vce.fw_version &&
882 		    adev->vce.fw_version < AMDGPU_VCE_FW_53_45)
883 			vm_size = min(vm_size, 1ULL << 40);
884 
885 		dev_info->virtual_address_offset = AMDGPU_VA_RESERVED_SIZE;
886 		dev_info->virtual_address_max =
887 			min(vm_size, AMDGPU_GMC_HOLE_START);
888 
889 		if (vm_size > AMDGPU_GMC_HOLE_START) {
890 			dev_info->high_va_offset = AMDGPU_GMC_HOLE_END;
891 			dev_info->high_va_max = AMDGPU_GMC_HOLE_END | vm_size;
892 		}
893 		dev_info->virtual_address_alignment = max_t(u32, PAGE_SIZE, AMDGPU_GPU_PAGE_SIZE);
894 		dev_info->pte_fragment_size = (1 << adev->vm_manager.fragment_size) * AMDGPU_GPU_PAGE_SIZE;
895 		dev_info->gart_page_size = max_t(u32, PAGE_SIZE, AMDGPU_GPU_PAGE_SIZE);
896 		dev_info->cu_active_number = adev->gfx.cu_info.number;
897 		dev_info->cu_ao_mask = adev->gfx.cu_info.ao_cu_mask;
898 		dev_info->ce_ram_size = adev->gfx.ce_ram_size;
899 		memcpy(&dev_info->cu_ao_bitmap[0], &adev->gfx.cu_info.ao_cu_bitmap[0],
900 		       sizeof(adev->gfx.cu_info.ao_cu_bitmap));
901 		memcpy(&dev_info->cu_bitmap[0], &adev->gfx.cu_info.bitmap[0],
902 		       sizeof(adev->gfx.cu_info.bitmap));
903 		dev_info->vram_type = adev->gmc.vram_type;
904 		dev_info->vram_bit_width = adev->gmc.vram_width;
905 		dev_info->vce_harvest_config = adev->vce.harvest_config;
906 		dev_info->gc_double_offchip_lds_buf =
907 			adev->gfx.config.double_offchip_lds_buf;
908 		dev_info->wave_front_size = adev->gfx.cu_info.wave_front_size;
909 		dev_info->num_shader_visible_vgprs = adev->gfx.config.max_gprs;
910 		dev_info->num_cu_per_sh = adev->gfx.config.max_cu_per_sh;
911 		dev_info->num_tcc_blocks = adev->gfx.config.max_texture_channel_caches;
912 		dev_info->gs_vgt_table_depth = adev->gfx.config.gs_vgt_table_depth;
913 		dev_info->gs_prim_buffer_depth = adev->gfx.config.gs_prim_buffer_depth;
914 		dev_info->max_gs_waves_per_vgt = adev->gfx.config.max_gs_threads;
915 
916 		if (adev->family >= AMDGPU_FAMILY_NV)
917 			dev_info->pa_sc_tile_steering_override =
918 				adev->gfx.config.pa_sc_tile_steering_override;
919 
920 		dev_info->tcc_disabled_mask = adev->gfx.config.tcc_disabled_mask;
921 
922 		ret = copy_to_user(out, dev_info,
923 				   min((size_t)size, sizeof(*dev_info))) ? -EFAULT : 0;
924 		kfree(dev_info);
925 		return ret;
926 	}
927 	case AMDGPU_INFO_VCE_CLOCK_TABLE: {
928 		unsigned i;
929 		struct drm_amdgpu_info_vce_clock_table vce_clk_table = {};
930 		struct amd_vce_state *vce_state;
931 
932 		for (i = 0; i < AMDGPU_VCE_CLOCK_TABLE_ENTRIES; i++) {
933 			vce_state = amdgpu_dpm_get_vce_clock_state(adev, i);
934 			if (vce_state) {
935 				vce_clk_table.entries[i].sclk = vce_state->sclk;
936 				vce_clk_table.entries[i].mclk = vce_state->mclk;
937 				vce_clk_table.entries[i].eclk = vce_state->evclk;
938 				vce_clk_table.num_valid_entries++;
939 			}
940 		}
941 
942 		return copy_to_user(out, &vce_clk_table,
943 				    min((size_t)size, sizeof(vce_clk_table))) ? -EFAULT : 0;
944 	}
945 	case AMDGPU_INFO_VBIOS: {
946 		uint32_t bios_size = adev->bios_size;
947 
948 		switch (info->vbios_info.type) {
949 		case AMDGPU_INFO_VBIOS_SIZE:
950 			return copy_to_user(out, &bios_size,
951 					min((size_t)size, sizeof(bios_size)))
952 					? -EFAULT : 0;
953 		case AMDGPU_INFO_VBIOS_IMAGE: {
954 			uint8_t *bios;
955 			uint32_t bios_offset = info->vbios_info.offset;
956 
957 			if (bios_offset >= bios_size)
958 				return -EINVAL;
959 
960 			bios = adev->bios + bios_offset;
961 			return copy_to_user(out, bios,
962 					    min((size_t)size, (size_t)(bios_size - bios_offset)))
963 					? -EFAULT : 0;
964 		}
965 		case AMDGPU_INFO_VBIOS_INFO: {
966 			struct drm_amdgpu_info_vbios vbios_info = {};
967 			struct atom_context *atom_context;
968 
969 			atom_context = adev->mode_info.atom_context;
970 			memcpy(vbios_info.name, atom_context->name, sizeof(atom_context->name));
971 			memcpy(vbios_info.vbios_pn, atom_context->vbios_pn, sizeof(atom_context->vbios_pn));
972 			vbios_info.version = atom_context->version;
973 			memcpy(vbios_info.vbios_ver_str, atom_context->vbios_ver_str,
974 						sizeof(atom_context->vbios_ver_str));
975 			memcpy(vbios_info.date, atom_context->date, sizeof(atom_context->date));
976 
977 			return copy_to_user(out, &vbios_info,
978 						min((size_t)size, sizeof(vbios_info))) ? -EFAULT : 0;
979 		}
980 		default:
981 			DRM_DEBUG_KMS("Invalid request %d\n",
982 					info->vbios_info.type);
983 			return -EINVAL;
984 		}
985 	}
986 	case AMDGPU_INFO_NUM_HANDLES: {
987 		struct drm_amdgpu_info_num_handles handle;
988 
989 		switch (info->query_hw_ip.type) {
990 		case AMDGPU_HW_IP_UVD:
991 			/* Starting Polaris, we support unlimited UVD handles */
992 			if (adev->asic_type < CHIP_POLARIS10) {
993 				handle.uvd_max_handles = adev->uvd.max_handles;
994 				handle.uvd_used_handles = amdgpu_uvd_used_handles(adev);
995 
996 				return copy_to_user(out, &handle,
997 					min((size_t)size, sizeof(handle))) ? -EFAULT : 0;
998 			} else {
999 				return -ENODATA;
1000 			}
1001 
1002 			break;
1003 		default:
1004 			return -EINVAL;
1005 		}
1006 	}
1007 	case AMDGPU_INFO_SENSOR: {
1008 		if (!adev->pm.dpm_enabled)
1009 			return -ENOENT;
1010 
1011 		switch (info->sensor_info.type) {
1012 		case AMDGPU_INFO_SENSOR_GFX_SCLK:
1013 			/* get sclk in Mhz */
1014 			if (amdgpu_dpm_read_sensor(adev,
1015 						   AMDGPU_PP_SENSOR_GFX_SCLK,
1016 						   (void *)&ui32, &ui32_size)) {
1017 				return -EINVAL;
1018 			}
1019 			ui32 /= 100;
1020 			break;
1021 		case AMDGPU_INFO_SENSOR_GFX_MCLK:
1022 			/* get mclk in Mhz */
1023 			if (amdgpu_dpm_read_sensor(adev,
1024 						   AMDGPU_PP_SENSOR_GFX_MCLK,
1025 						   (void *)&ui32, &ui32_size)) {
1026 				return -EINVAL;
1027 			}
1028 			ui32 /= 100;
1029 			break;
1030 		case AMDGPU_INFO_SENSOR_GPU_TEMP:
1031 			/* get temperature in millidegrees C */
1032 			if (amdgpu_dpm_read_sensor(adev,
1033 						   AMDGPU_PP_SENSOR_GPU_TEMP,
1034 						   (void *)&ui32, &ui32_size)) {
1035 				return -EINVAL;
1036 			}
1037 			break;
1038 		case AMDGPU_INFO_SENSOR_GPU_LOAD:
1039 			/* get GPU load */
1040 			if (amdgpu_dpm_read_sensor(adev,
1041 						   AMDGPU_PP_SENSOR_GPU_LOAD,
1042 						   (void *)&ui32, &ui32_size)) {
1043 				return -EINVAL;
1044 			}
1045 			break;
1046 		case AMDGPU_INFO_SENSOR_GPU_AVG_POWER:
1047 			/* get average GPU power */
1048 			if (amdgpu_dpm_read_sensor(adev,
1049 						   AMDGPU_PP_SENSOR_GPU_POWER,
1050 						   (void *)&ui32, &ui32_size)) {
1051 				return -EINVAL;
1052 			}
1053 			ui32 >>= 8;
1054 			break;
1055 		case AMDGPU_INFO_SENSOR_VDDNB:
1056 			/* get VDDNB in millivolts */
1057 			if (amdgpu_dpm_read_sensor(adev,
1058 						   AMDGPU_PP_SENSOR_VDDNB,
1059 						   (void *)&ui32, &ui32_size)) {
1060 				return -EINVAL;
1061 			}
1062 			break;
1063 		case AMDGPU_INFO_SENSOR_VDDGFX:
1064 			/* get VDDGFX in millivolts */
1065 			if (amdgpu_dpm_read_sensor(adev,
1066 						   AMDGPU_PP_SENSOR_VDDGFX,
1067 						   (void *)&ui32, &ui32_size)) {
1068 				return -EINVAL;
1069 			}
1070 			break;
1071 		case AMDGPU_INFO_SENSOR_STABLE_PSTATE_GFX_SCLK:
1072 			/* get stable pstate sclk in Mhz */
1073 			if (amdgpu_dpm_read_sensor(adev,
1074 						   AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK,
1075 						   (void *)&ui32, &ui32_size)) {
1076 				return -EINVAL;
1077 			}
1078 			ui32 /= 100;
1079 			break;
1080 		case AMDGPU_INFO_SENSOR_STABLE_PSTATE_GFX_MCLK:
1081 			/* get stable pstate mclk in Mhz */
1082 			if (amdgpu_dpm_read_sensor(adev,
1083 						   AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK,
1084 						   (void *)&ui32, &ui32_size)) {
1085 				return -EINVAL;
1086 			}
1087 			ui32 /= 100;
1088 			break;
1089 		default:
1090 			DRM_DEBUG_KMS("Invalid request %d\n",
1091 				      info->sensor_info.type);
1092 			return -EINVAL;
1093 		}
1094 		return copy_to_user(out, &ui32, min(size, 4u)) ? -EFAULT : 0;
1095 	}
1096 	case AMDGPU_INFO_VRAM_LOST_COUNTER:
1097 		ui32 = atomic_read(&adev->vram_lost_counter);
1098 		return copy_to_user(out, &ui32, min(size, 4u)) ? -EFAULT : 0;
1099 	case AMDGPU_INFO_RAS_ENABLED_FEATURES: {
1100 		struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
1101 		uint64_t ras_mask;
1102 
1103 		if (!ras)
1104 			return -EINVAL;
1105 		ras_mask = (uint64_t)adev->ras_enabled << 32 | ras->features;
1106 
1107 		return copy_to_user(out, &ras_mask,
1108 				min_t(u64, size, sizeof(ras_mask))) ?
1109 			-EFAULT : 0;
1110 	}
1111 	case AMDGPU_INFO_VIDEO_CAPS: {
1112 		const struct amdgpu_video_codecs *codecs;
1113 		struct drm_amdgpu_info_video_caps *caps;
1114 		int r;
1115 
1116 		switch (info->video_cap.type) {
1117 		case AMDGPU_INFO_VIDEO_CAPS_DECODE:
1118 			r = amdgpu_asic_query_video_codecs(adev, false, &codecs);
1119 			if (r)
1120 				return -EINVAL;
1121 			break;
1122 		case AMDGPU_INFO_VIDEO_CAPS_ENCODE:
1123 			r = amdgpu_asic_query_video_codecs(adev, true, &codecs);
1124 			if (r)
1125 				return -EINVAL;
1126 			break;
1127 		default:
1128 			DRM_DEBUG_KMS("Invalid request %d\n",
1129 				      info->video_cap.type);
1130 			return -EINVAL;
1131 		}
1132 
1133 		caps = kzalloc(sizeof(*caps), GFP_KERNEL);
1134 		if (!caps)
1135 			return -ENOMEM;
1136 
1137 		for (i = 0; i < codecs->codec_count; i++) {
1138 			int idx = codecs->codec_array[i].codec_type;
1139 
1140 			switch (idx) {
1141 			case AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_MPEG2:
1142 			case AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_MPEG4:
1143 			case AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_VC1:
1144 			case AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_MPEG4_AVC:
1145 			case AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_HEVC:
1146 			case AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_JPEG:
1147 			case AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_VP9:
1148 			case AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_AV1:
1149 				caps->codec_info[idx].valid = 1;
1150 				caps->codec_info[idx].max_width =
1151 					codecs->codec_array[i].max_width;
1152 				caps->codec_info[idx].max_height =
1153 					codecs->codec_array[i].max_height;
1154 				caps->codec_info[idx].max_pixels_per_frame =
1155 					codecs->codec_array[i].max_pixels_per_frame;
1156 				caps->codec_info[idx].max_level =
1157 					codecs->codec_array[i].max_level;
1158 				break;
1159 			default:
1160 				break;
1161 			}
1162 		}
1163 		r = copy_to_user(out, caps,
1164 				 min((size_t)size, sizeof(*caps))) ? -EFAULT : 0;
1165 		kfree(caps);
1166 		return r;
1167 	}
1168 	default:
1169 		DRM_DEBUG_KMS("Invalid request %d\n", info->query);
1170 		return -EINVAL;
1171 	}
1172 	return 0;
1173 }
1174 
1175 
1176 /*
1177  * Outdated mess for old drm with Xorg being in charge (void function now).
1178  */
1179 /**
1180  * amdgpu_driver_lastclose_kms - drm callback for last close
1181  *
1182  * @dev: drm dev pointer
1183  *
1184  * Switch vga_switcheroo state after last close (all asics).
1185  */
1186 void amdgpu_driver_lastclose_kms(struct drm_device *dev)
1187 {
1188 	drm_fb_helper_lastclose(dev);
1189 	vga_switcheroo_process_delayed_switch();
1190 }
1191 
1192 /**
1193  * amdgpu_driver_open_kms - drm callback for open
1194  *
1195  * @dev: drm dev pointer
1196  * @file_priv: drm file
1197  *
1198  * On device open, init vm on cayman+ (all asics).
1199  * Returns 0 on success, error on failure.
1200  */
1201 int amdgpu_driver_open_kms(struct drm_device *dev, struct drm_file *file_priv)
1202 {
1203 	struct amdgpu_device *adev = drm_to_adev(dev);
1204 	struct amdgpu_fpriv *fpriv;
1205 	int r, pasid;
1206 
1207 	/* Ensure IB tests are run on ring */
1208 	flush_delayed_work(&adev->delayed_init_work);
1209 
1210 
1211 	if (amdgpu_ras_intr_triggered()) {
1212 		DRM_ERROR("RAS Intr triggered, device disabled!!");
1213 		return -EHWPOISON;
1214 	}
1215 
1216 	file_priv->driver_priv = NULL;
1217 
1218 	r = pm_runtime_get_sync(dev->dev);
1219 	if (r < 0)
1220 		goto pm_put;
1221 
1222 	fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL);
1223 	if (unlikely(!fpriv)) {
1224 		r = -ENOMEM;
1225 		goto out_suspend;
1226 	}
1227 
1228 	pasid = amdgpu_pasid_alloc(16);
1229 	if (pasid < 0) {
1230 		dev_warn(adev->dev, "No more PASIDs available!");
1231 		pasid = 0;
1232 	}
1233 
1234 	r = amdgpu_vm_init(adev, &fpriv->vm);
1235 	if (r)
1236 		goto error_pasid;
1237 
1238 	r = amdgpu_vm_set_pasid(adev, &fpriv->vm, pasid);
1239 	if (r)
1240 		goto error_vm;
1241 
1242 	fpriv->prt_va = amdgpu_vm_bo_add(adev, &fpriv->vm, NULL);
1243 	if (!fpriv->prt_va) {
1244 		r = -ENOMEM;
1245 		goto error_vm;
1246 	}
1247 
1248 	if (amdgpu_mcbp || amdgpu_sriov_vf(adev)) {
1249 		uint64_t csa_addr = amdgpu_csa_vaddr(adev) & AMDGPU_GMC_HOLE_MASK;
1250 
1251 		r = amdgpu_map_static_csa(adev, &fpriv->vm, adev->virt.csa_obj,
1252 						&fpriv->csa_va, csa_addr, AMDGPU_CSA_SIZE);
1253 		if (r)
1254 			goto error_vm;
1255 	}
1256 
1257 	rw_init(&fpriv->bo_list_lock, "agbo");
1258 	idr_init(&fpriv->bo_list_handles);
1259 
1260 	amdgpu_ctx_mgr_init(&fpriv->ctx_mgr);
1261 
1262 	file_priv->driver_priv = fpriv;
1263 	goto out_suspend;
1264 
1265 error_vm:
1266 	amdgpu_vm_fini(adev, &fpriv->vm);
1267 
1268 error_pasid:
1269 	if (pasid) {
1270 		amdgpu_pasid_free(pasid);
1271 		amdgpu_vm_set_pasid(adev, &fpriv->vm, 0);
1272 	}
1273 
1274 	kfree(fpriv);
1275 
1276 out_suspend:
1277 	pm_runtime_mark_last_busy(dev->dev);
1278 pm_put:
1279 	pm_runtime_put_autosuspend(dev->dev);
1280 
1281 	return r;
1282 }
1283 
1284 /**
1285  * amdgpu_driver_postclose_kms - drm callback for post close
1286  *
1287  * @dev: drm dev pointer
1288  * @file_priv: drm file
1289  *
1290  * On device post close, tear down vm on cayman+ (all asics).
1291  */
1292 void amdgpu_driver_postclose_kms(struct drm_device *dev,
1293 				 struct drm_file *file_priv)
1294 {
1295 	struct amdgpu_device *adev = drm_to_adev(dev);
1296 	struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
1297 	struct amdgpu_bo_list *list;
1298 	struct amdgpu_bo *pd;
1299 	u32 pasid;
1300 	int handle;
1301 
1302 	if (!fpriv)
1303 		return;
1304 
1305 	pm_runtime_get_sync(dev->dev);
1306 
1307 	if (amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_UVD) != NULL)
1308 		amdgpu_uvd_free_handles(adev, file_priv);
1309 	if (amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_VCE) != NULL)
1310 		amdgpu_vce_free_handles(adev, file_priv);
1311 
1312 	amdgpu_vm_bo_rmv(adev, fpriv->prt_va);
1313 
1314 	if (amdgpu_mcbp || amdgpu_sriov_vf(adev)) {
1315 		/* TODO: how to handle reserve failure */
1316 		BUG_ON(amdgpu_bo_reserve(adev->virt.csa_obj, true));
1317 		amdgpu_vm_bo_rmv(adev, fpriv->csa_va);
1318 		fpriv->csa_va = NULL;
1319 		amdgpu_bo_unreserve(adev->virt.csa_obj);
1320 	}
1321 
1322 	pasid = fpriv->vm.pasid;
1323 	pd = amdgpu_bo_ref(fpriv->vm.root.bo);
1324 
1325 	amdgpu_ctx_mgr_fini(&fpriv->ctx_mgr);
1326 	amdgpu_vm_fini(adev, &fpriv->vm);
1327 
1328 	if (pasid)
1329 		amdgpu_pasid_free_delayed(pd->tbo.base.resv, pasid);
1330 	amdgpu_bo_unref(&pd);
1331 
1332 	idr_for_each_entry(&fpriv->bo_list_handles, list, handle)
1333 		amdgpu_bo_list_put(list);
1334 
1335 	idr_destroy(&fpriv->bo_list_handles);
1336 	mutex_destroy(&fpriv->bo_list_lock);
1337 
1338 	kfree(fpriv);
1339 	file_priv->driver_priv = NULL;
1340 
1341 	pm_runtime_mark_last_busy(dev->dev);
1342 	pm_runtime_put_autosuspend(dev->dev);
1343 }
1344 
1345 
1346 void amdgpu_driver_release_kms(struct drm_device *dev)
1347 {
1348 	struct amdgpu_device *adev = drm_to_adev(dev);
1349 
1350 	amdgpu_device_fini_sw(adev);
1351 	pci_set_drvdata(adev->pdev, NULL);
1352 }
1353 
1354 /*
1355  * VBlank related functions.
1356  */
1357 /**
1358  * amdgpu_get_vblank_counter_kms - get frame count
1359  *
1360  * @crtc: crtc to get the frame count from
1361  *
1362  * Gets the frame count on the requested crtc (all asics).
1363  * Returns frame count on success, -EINVAL on failure.
1364  */
1365 u32 amdgpu_get_vblank_counter_kms(struct drm_crtc *crtc)
1366 {
1367 	struct drm_device *dev = crtc->dev;
1368 	unsigned int pipe = crtc->index;
1369 	struct amdgpu_device *adev = drm_to_adev(dev);
1370 	int vpos, hpos, stat;
1371 	u32 count;
1372 
1373 	if (pipe >= adev->mode_info.num_crtc) {
1374 		DRM_ERROR("Invalid crtc %u\n", pipe);
1375 		return -EINVAL;
1376 	}
1377 
1378 	/* The hw increments its frame counter at start of vsync, not at start
1379 	 * of vblank, as is required by DRM core vblank counter handling.
1380 	 * Cook the hw count here to make it appear to the caller as if it
1381 	 * incremented at start of vblank. We measure distance to start of
1382 	 * vblank in vpos. vpos therefore will be >= 0 between start of vblank
1383 	 * and start of vsync, so vpos >= 0 means to bump the hw frame counter
1384 	 * result by 1 to give the proper appearance to caller.
1385 	 */
1386 	if (adev->mode_info.crtcs[pipe]) {
1387 		/* Repeat readout if needed to provide stable result if
1388 		 * we cross start of vsync during the queries.
1389 		 */
1390 		do {
1391 			count = amdgpu_display_vblank_get_counter(adev, pipe);
1392 			/* Ask amdgpu_display_get_crtc_scanoutpos to return
1393 			 * vpos as distance to start of vblank, instead of
1394 			 * regular vertical scanout pos.
1395 			 */
1396 			stat = amdgpu_display_get_crtc_scanoutpos(
1397 				dev, pipe, GET_DISTANCE_TO_VBLANKSTART,
1398 				&vpos, &hpos, NULL, NULL,
1399 				&adev->mode_info.crtcs[pipe]->base.hwmode);
1400 		} while (count != amdgpu_display_vblank_get_counter(adev, pipe));
1401 
1402 		if (((stat & (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE)) !=
1403 		    (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE))) {
1404 			DRM_DEBUG_VBL("Query failed! stat %d\n", stat);
1405 		} else {
1406 			DRM_DEBUG_VBL("crtc %d: dist from vblank start %d\n",
1407 				      pipe, vpos);
1408 
1409 			/* Bump counter if we are at >= leading edge of vblank,
1410 			 * but before vsync where vpos would turn negative and
1411 			 * the hw counter really increments.
1412 			 */
1413 			if (vpos >= 0)
1414 				count++;
1415 		}
1416 	} else {
1417 		/* Fallback to use value as is. */
1418 		count = amdgpu_display_vblank_get_counter(adev, pipe);
1419 		DRM_DEBUG_VBL("NULL mode info! Returned count may be wrong.\n");
1420 	}
1421 
1422 	return count;
1423 }
1424 
1425 /**
1426  * amdgpu_enable_vblank_kms - enable vblank interrupt
1427  *
1428  * @crtc: crtc to enable vblank interrupt for
1429  *
1430  * Enable the interrupt on the requested crtc (all asics).
1431  * Returns 0 on success, -EINVAL on failure.
1432  */
1433 int amdgpu_enable_vblank_kms(struct drm_crtc *crtc)
1434 {
1435 	struct drm_device *dev = crtc->dev;
1436 	unsigned int pipe = crtc->index;
1437 	struct amdgpu_device *adev = drm_to_adev(dev);
1438 	int idx = amdgpu_display_crtc_idx_to_irq_type(adev, pipe);
1439 
1440 	return amdgpu_irq_get(adev, &adev->crtc_irq, idx);
1441 }
1442 
1443 /**
1444  * amdgpu_disable_vblank_kms - disable vblank interrupt
1445  *
1446  * @crtc: crtc to disable vblank interrupt for
1447  *
1448  * Disable the interrupt on the requested crtc (all asics).
1449  */
1450 void amdgpu_disable_vblank_kms(struct drm_crtc *crtc)
1451 {
1452 	struct drm_device *dev = crtc->dev;
1453 	unsigned int pipe = crtc->index;
1454 	struct amdgpu_device *adev = drm_to_adev(dev);
1455 	int idx = amdgpu_display_crtc_idx_to_irq_type(adev, pipe);
1456 
1457 	amdgpu_irq_put(adev, &adev->crtc_irq, idx);
1458 }
1459 
1460 /*
1461  * Debugfs info
1462  */
1463 #if defined(CONFIG_DEBUG_FS)
1464 
1465 static int amdgpu_debugfs_firmware_info_show(struct seq_file *m, void *unused)
1466 {
1467 	struct amdgpu_device *adev = (struct amdgpu_device *)m->private;
1468 	struct drm_amdgpu_info_firmware fw_info;
1469 	struct drm_amdgpu_query_fw query_fw;
1470 	struct atom_context *ctx = adev->mode_info.atom_context;
1471 	int ret, i;
1472 
1473 	static const char *ta_fw_name[TA_FW_TYPE_MAX_INDEX] = {
1474 #define TA_FW_NAME(type) [TA_FW_TYPE_PSP_##type] = #type
1475 		TA_FW_NAME(XGMI),
1476 		TA_FW_NAME(RAS),
1477 		TA_FW_NAME(HDCP),
1478 		TA_FW_NAME(DTM),
1479 		TA_FW_NAME(RAP),
1480 		TA_FW_NAME(SECUREDISPLAY),
1481 #undef TA_FW_NAME
1482 	};
1483 
1484 	/* VCE */
1485 	query_fw.fw_type = AMDGPU_INFO_FW_VCE;
1486 	ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1487 	if (ret)
1488 		return ret;
1489 	seq_printf(m, "VCE feature version: %u, firmware version: 0x%08x\n",
1490 		   fw_info.feature, fw_info.ver);
1491 
1492 	/* UVD */
1493 	query_fw.fw_type = AMDGPU_INFO_FW_UVD;
1494 	ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1495 	if (ret)
1496 		return ret;
1497 	seq_printf(m, "UVD feature version: %u, firmware version: 0x%08x\n",
1498 		   fw_info.feature, fw_info.ver);
1499 
1500 	/* GMC */
1501 	query_fw.fw_type = AMDGPU_INFO_FW_GMC;
1502 	ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1503 	if (ret)
1504 		return ret;
1505 	seq_printf(m, "MC feature version: %u, firmware version: 0x%08x\n",
1506 		   fw_info.feature, fw_info.ver);
1507 
1508 	/* ME */
1509 	query_fw.fw_type = AMDGPU_INFO_FW_GFX_ME;
1510 	ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1511 	if (ret)
1512 		return ret;
1513 	seq_printf(m, "ME feature version: %u, firmware version: 0x%08x\n",
1514 		   fw_info.feature, fw_info.ver);
1515 
1516 	/* PFP */
1517 	query_fw.fw_type = AMDGPU_INFO_FW_GFX_PFP;
1518 	ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1519 	if (ret)
1520 		return ret;
1521 	seq_printf(m, "PFP feature version: %u, firmware version: 0x%08x\n",
1522 		   fw_info.feature, fw_info.ver);
1523 
1524 	/* CE */
1525 	query_fw.fw_type = AMDGPU_INFO_FW_GFX_CE;
1526 	ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1527 	if (ret)
1528 		return ret;
1529 	seq_printf(m, "CE feature version: %u, firmware version: 0x%08x\n",
1530 		   fw_info.feature, fw_info.ver);
1531 
1532 	/* RLC */
1533 	query_fw.fw_type = AMDGPU_INFO_FW_GFX_RLC;
1534 	ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1535 	if (ret)
1536 		return ret;
1537 	seq_printf(m, "RLC feature version: %u, firmware version: 0x%08x\n",
1538 		   fw_info.feature, fw_info.ver);
1539 
1540 	/* RLC SAVE RESTORE LIST CNTL */
1541 	query_fw.fw_type = AMDGPU_INFO_FW_GFX_RLC_RESTORE_LIST_CNTL;
1542 	ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1543 	if (ret)
1544 		return ret;
1545 	seq_printf(m, "RLC SRLC feature version: %u, firmware version: 0x%08x\n",
1546 		   fw_info.feature, fw_info.ver);
1547 
1548 	/* RLC SAVE RESTORE LIST GPM MEM */
1549 	query_fw.fw_type = AMDGPU_INFO_FW_GFX_RLC_RESTORE_LIST_GPM_MEM;
1550 	ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1551 	if (ret)
1552 		return ret;
1553 	seq_printf(m, "RLC SRLG feature version: %u, firmware version: 0x%08x\n",
1554 		   fw_info.feature, fw_info.ver);
1555 
1556 	/* RLC SAVE RESTORE LIST SRM MEM */
1557 	query_fw.fw_type = AMDGPU_INFO_FW_GFX_RLC_RESTORE_LIST_SRM_MEM;
1558 	ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1559 	if (ret)
1560 		return ret;
1561 	seq_printf(m, "RLC SRLS feature version: %u, firmware version: 0x%08x\n",
1562 		   fw_info.feature, fw_info.ver);
1563 
1564 	/* MEC */
1565 	query_fw.fw_type = AMDGPU_INFO_FW_GFX_MEC;
1566 	query_fw.index = 0;
1567 	ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1568 	if (ret)
1569 		return ret;
1570 	seq_printf(m, "MEC feature version: %u, firmware version: 0x%08x\n",
1571 		   fw_info.feature, fw_info.ver);
1572 
1573 	/* MEC2 */
1574 	if (adev->gfx.mec2_fw) {
1575 		query_fw.index = 1;
1576 		ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1577 		if (ret)
1578 			return ret;
1579 		seq_printf(m, "MEC2 feature version: %u, firmware version: 0x%08x\n",
1580 			   fw_info.feature, fw_info.ver);
1581 	}
1582 
1583 	/* PSP SOS */
1584 	query_fw.fw_type = AMDGPU_INFO_FW_SOS;
1585 	ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1586 	if (ret)
1587 		return ret;
1588 	seq_printf(m, "SOS feature version: %u, firmware version: 0x%08x\n",
1589 		   fw_info.feature, fw_info.ver);
1590 
1591 
1592 	/* PSP ASD */
1593 	query_fw.fw_type = AMDGPU_INFO_FW_ASD;
1594 	ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1595 	if (ret)
1596 		return ret;
1597 	seq_printf(m, "ASD feature version: %u, firmware version: 0x%08x\n",
1598 		   fw_info.feature, fw_info.ver);
1599 
1600 	query_fw.fw_type = AMDGPU_INFO_FW_TA;
1601 	for (i = TA_FW_TYPE_PSP_XGMI; i < TA_FW_TYPE_MAX_INDEX; i++) {
1602 		query_fw.index = i;
1603 		ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1604 		if (ret)
1605 			continue;
1606 
1607 		seq_printf(m, "TA %s feature version: 0x%08x, firmware version: 0x%08x\n",
1608 			   ta_fw_name[i], fw_info.feature, fw_info.ver);
1609 	}
1610 
1611 	/* SMC */
1612 	query_fw.fw_type = AMDGPU_INFO_FW_SMC;
1613 	ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1614 	if (ret)
1615 		return ret;
1616 	seq_printf(m, "SMC feature version: %u, firmware version: 0x%08x\n",
1617 		   fw_info.feature, fw_info.ver);
1618 
1619 	/* SDMA */
1620 	query_fw.fw_type = AMDGPU_INFO_FW_SDMA;
1621 	for (i = 0; i < adev->sdma.num_instances; i++) {
1622 		query_fw.index = i;
1623 		ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1624 		if (ret)
1625 			return ret;
1626 		seq_printf(m, "SDMA%d feature version: %u, firmware version: 0x%08x\n",
1627 			   i, fw_info.feature, fw_info.ver);
1628 	}
1629 
1630 	/* VCN */
1631 	query_fw.fw_type = AMDGPU_INFO_FW_VCN;
1632 	ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1633 	if (ret)
1634 		return ret;
1635 	seq_printf(m, "VCN feature version: %u, firmware version: 0x%08x\n",
1636 		   fw_info.feature, fw_info.ver);
1637 
1638 	/* DMCU */
1639 	query_fw.fw_type = AMDGPU_INFO_FW_DMCU;
1640 	ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1641 	if (ret)
1642 		return ret;
1643 	seq_printf(m, "DMCU feature version: %u, firmware version: 0x%08x\n",
1644 		   fw_info.feature, fw_info.ver);
1645 
1646 	/* DMCUB */
1647 	query_fw.fw_type = AMDGPU_INFO_FW_DMCUB;
1648 	ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1649 	if (ret)
1650 		return ret;
1651 	seq_printf(m, "DMCUB feature version: %u, firmware version: 0x%08x\n",
1652 		   fw_info.feature, fw_info.ver);
1653 
1654 	/* TOC */
1655 	query_fw.fw_type = AMDGPU_INFO_FW_TOC;
1656 	ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1657 	if (ret)
1658 		return ret;
1659 	seq_printf(m, "TOC feature version: %u, firmware version: 0x%08x\n",
1660 		   fw_info.feature, fw_info.ver);
1661 
1662 	seq_printf(m, "VBIOS version: %s\n", ctx->vbios_version);
1663 
1664 	return 0;
1665 }
1666 
1667 DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_firmware_info);
1668 
1669 #endif
1670 
1671 void amdgpu_debugfs_firmware_init(struct amdgpu_device *adev)
1672 {
1673 #if defined(CONFIG_DEBUG_FS)
1674 	struct drm_minor *minor = adev_to_drm(adev)->primary;
1675 	struct dentry *root = minor->debugfs_root;
1676 
1677 	debugfs_create_file("amdgpu_firmware_info", 0444, root,
1678 			    adev, &amdgpu_debugfs_firmware_info_fops);
1679 
1680 #endif
1681 }
1682 
1683 int
1684 amdgpu_probe(struct device *parent, void *match, void *aux)
1685 {
1686 	struct pci_attach_args *pa = aux;
1687 	const struct pci_device_id *id_entry;
1688 	unsigned long flags = 0;
1689 
1690 	if (amdgpu_fatal_error)
1691 		return 0;
1692 
1693 	id_entry = drm_find_description(PCI_VENDOR(pa->pa_id),
1694 	    PCI_PRODUCT(pa->pa_id), amdgpu_pciidlist);
1695 	if (id_entry != NULL) {
1696 		flags = id_entry->driver_data;
1697 		if (flags & AMD_EXP_HW_SUPPORT)
1698 			return 0;
1699 		else
1700 			return 20;
1701 	}
1702 
1703 	return 0;
1704 }
1705 
1706 /*
1707  * some functions are only called once on init regardless of how many times
1708  * amdgpu attaches in linux this is handled via module_init()/module_exit()
1709  */
1710 int amdgpu_refcnt;
1711 
1712 int __init drm_sched_fence_slab_init(void);
1713 void __exit drm_sched_fence_slab_fini(void);
1714 irqreturn_t amdgpu_irq_handler(void *);
1715 
1716 void
1717 amdgpu_attach(struct device *parent, struct device *self, void *aux)
1718 {
1719 	struct amdgpu_device	*adev = (struct amdgpu_device *)self;
1720 	struct drm_device	*dev;
1721 	struct pci_attach_args	*pa = aux;
1722 	const struct pci_device_id *id_entry;
1723 	pcireg_t		 type;
1724 	int			 i;
1725 	uint8_t			 rmmio_bar;
1726 	paddr_t			 fb_aper;
1727 	pcireg_t		 addr, mask;
1728 	int			 s;
1729 	bool			 supports_atomic = false;
1730 
1731 	id_entry = drm_find_description(PCI_VENDOR(pa->pa_id),
1732 	    PCI_PRODUCT(pa->pa_id), amdgpu_pciidlist);
1733 	adev->flags = id_entry->driver_data;
1734 	adev->family = adev->flags & AMD_ASIC_MASK;
1735 	adev->pc = pa->pa_pc;
1736 	adev->pa_tag = pa->pa_tag;
1737 	adev->iot = pa->pa_iot;
1738 	adev->memt = pa->pa_memt;
1739 	adev->dmat = pa->pa_dmat;
1740 
1741 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_DISPLAY &&
1742 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_DISPLAY_VGA &&
1743 	    (pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG)
1744 	    & (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE))
1745 	    == (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE)) {
1746 		adev->primary = 1;
1747 #if NVGA > 0
1748 		adev->console = vga_is_console(pa->pa_iot, -1);
1749 		vga_console_attached = 1;
1750 #endif
1751 	}
1752 #if NEFIFB > 0
1753 	if (efifb_is_primary(pa)) {
1754 		adev->primary = 1;
1755 		adev->console = efifb_is_console(pa);
1756 		efifb_detach();
1757 	}
1758 #endif
1759 
1760 #define AMDGPU_PCI_MEM		0x10
1761 
1762 	type = pci_mapreg_type(pa->pa_pc, pa->pa_tag, AMDGPU_PCI_MEM);
1763 	if (PCI_MAPREG_TYPE(type) != PCI_MAPREG_TYPE_MEM ||
1764 	    pci_mapreg_info(pa->pa_pc, pa->pa_tag, AMDGPU_PCI_MEM,
1765 	    type, &adev->fb_aper_offset, &adev->fb_aper_size, NULL)) {
1766 		printf(": can't get frambuffer info\n");
1767 		return;
1768 	}
1769 
1770 	if (adev->fb_aper_offset == 0) {
1771 		bus_size_t start, end, pci_mem_end;
1772 		bus_addr_t base;
1773 
1774 		KASSERT(pa->pa_memex != NULL);
1775 
1776 		start = max(PCI_MEM_START, pa->pa_memex->ex_start);
1777 		if (PCI_MAPREG_MEM_TYPE(type) == PCI_MAPREG_MEM_TYPE_64BIT)
1778 			pci_mem_end = PCI_MEM64_END;
1779 		else
1780 			pci_mem_end = PCI_MEM_END;
1781 		end = min(pci_mem_end, pa->pa_memex->ex_end);
1782 		if (extent_alloc_subregion(pa->pa_memex, start, end,
1783 		    adev->fb_aper_size, adev->fb_aper_size, 0, 0, 0, &base)) {
1784 			printf(": can't reserve framebuffer space\n");
1785 			return;
1786 		}
1787 		pci_conf_write(pa->pa_pc, pa->pa_tag, AMDGPU_PCI_MEM, base);
1788 		if (PCI_MAPREG_MEM_TYPE(type) == PCI_MAPREG_MEM_TYPE_64BIT)
1789 			pci_conf_write(pa->pa_pc, pa->pa_tag,
1790 			    AMDGPU_PCI_MEM + 4, (uint64_t)base >> 32);
1791 		adev->fb_aper_offset = base;
1792 	}
1793 
1794 	if (adev->family >= CHIP_BONAIRE) {
1795 		type = pci_mapreg_type(pa->pa_pc, pa->pa_tag, 0x18);
1796 		if (PCI_MAPREG_TYPE(type) != PCI_MAPREG_TYPE_MEM ||
1797 		    pci_mapreg_map(pa, 0x18, type, BUS_SPACE_MAP_LINEAR,
1798 		    &adev->doorbell.bst, &adev->doorbell.bsh,
1799 		    &adev->doorbell.base, &adev->doorbell.size, 0)) {
1800 			printf(": can't map doorbell space\n");
1801 			return;
1802 		}
1803 		adev->doorbell.ptr = bus_space_vaddr(adev->doorbell.bst,
1804 		    adev->doorbell.bsh);
1805 	}
1806 
1807 	if (adev->family >= CHIP_BONAIRE)
1808 		rmmio_bar = 0x24;
1809 	else
1810 		rmmio_bar = 0x18;
1811 
1812 	type = pci_mapreg_type(pa->pa_pc, pa->pa_tag, rmmio_bar);
1813 	if (PCI_MAPREG_TYPE(type) != PCI_MAPREG_TYPE_MEM ||
1814 	    pci_mapreg_map(pa, rmmio_bar, type, BUS_SPACE_MAP_LINEAR,
1815 	    &adev->rmmio_bst, &adev->rmmio_bsh, &adev->rmmio_base,
1816 	    &adev->rmmio_size, 0)) {
1817 		printf(": can't map rmmio space\n");
1818 		return;
1819 	}
1820 	adev->rmmio = bus_space_vaddr(adev->rmmio_bst, adev->rmmio_bsh);
1821 
1822 	/*
1823 	 * Make sure we have a base address for the ROM such that we
1824 	 * can map it later.
1825 	 */
1826 	s = splhigh();
1827 	addr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_ROM_REG);
1828 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_ROM_REG, ~PCI_ROM_ENABLE);
1829 	mask = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_ROM_REG);
1830 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_ROM_REG, addr);
1831 	splx(s);
1832 
1833 	if (addr == 0 && PCI_ROM_SIZE(mask) != 0 && pa->pa_memex) {
1834 		bus_size_t size, start, end;
1835 		bus_addr_t base;
1836 
1837 		size = PCI_ROM_SIZE(mask);
1838 		start = max(PCI_MEM_START, pa->pa_memex->ex_start);
1839 		end = min(PCI_MEM_END, pa->pa_memex->ex_end);
1840 		if (extent_alloc_subregion(pa->pa_memex, start, end, size,
1841 		    size, 0, 0, 0, &base) == 0)
1842 			pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_ROM_REG, base);
1843 	}
1844 
1845 	printf("\n");
1846 
1847 	/* from amdgpu_pci_probe(), aspm test done later */
1848 
1849 	if (!amdgpu_virtual_display &&
1850 	     amdgpu_device_asic_has_dc_support(adev->family))
1851 		supports_atomic = true;
1852 
1853 	if ((adev->flags & AMD_EXP_HW_SUPPORT) && !amdgpu_exp_hw_support) {
1854 		DRM_INFO("This hardware requires experimental hardware support.\n");
1855 		return;
1856 	}
1857 
1858 	/*
1859 	 * Initialize amdkfd before starting radeon.
1860 	 */
1861 	amdgpu_amdkfd_init();
1862 
1863 	dev = drm_attach_pci(&amdgpu_kms_driver, pa, 0, adev->primary,
1864 	    self, &adev->ddev);
1865 	if (dev == NULL) {
1866 		printf("%s: drm attach failed\n", adev->self.dv_xname);
1867 		return;
1868 	}
1869 	adev->pdev = dev->pdev;
1870 	adev->is_fw_fb = adev->primary;
1871 
1872 	/* from amdgpu_pci_probe() */
1873 	if (amdgpu_aspm == -1 && !pcie_aspm_enabled(adev->pdev))
1874 		amdgpu_aspm = 0;
1875 
1876 	if (!supports_atomic)
1877 		dev->driver_features &= ~DRIVER_ATOMIC;
1878 
1879 	if (!amdgpu_msi_ok(adev))
1880 		pa->pa_flags &= ~PCI_FLAGS_MSI_ENABLED;
1881 
1882 	/* from amdgpu_init() */
1883 	if (amdgpu_refcnt == 0) {
1884 		drm_sched_fence_slab_init();
1885 
1886 		if (amdgpu_sync_init()) {
1887 			printf("%s: amdgpu_sync_init failed\n",
1888 			    adev->self.dv_xname);
1889 			return;
1890 		}
1891 
1892 		if (amdgpu_fence_slab_init()) {
1893 			amdgpu_sync_fini();
1894 			printf("%s: amdgpu_fence_slab_init failed\n",
1895 			    adev->self.dv_xname);
1896 			return;
1897 		}
1898 
1899 		amdgpu_register_atpx_handler();
1900 		amdgpu_acpi_detect();
1901 	}
1902 	amdgpu_refcnt++;
1903 
1904 	adev->irq.msi_enabled = false;
1905 	if (pci_intr_map_msi(pa, &adev->intrh) == 0)
1906 		adev->irq.msi_enabled = true;
1907 	else if (pci_intr_map(pa, &adev->intrh) != 0) {
1908 		printf("%s: couldn't map interrupt\n", adev->self.dv_xname);
1909 		return;
1910 	}
1911 	printf("%s: %s\n", adev->self.dv_xname,
1912 	    pci_intr_string(pa->pa_pc, adev->intrh));
1913 
1914 	adev->irqh = pci_intr_establish(pa->pa_pc, adev->intrh, IPL_TTY,
1915 	    amdgpu_irq_handler, &adev->ddev, adev->self.dv_xname);
1916 	if (adev->irqh == NULL) {
1917 		printf("%s: couldn't establish interrupt\n",
1918 		    adev->self.dv_xname);
1919 		return;
1920 	}
1921 	adev->pdev->irq = 0;
1922 
1923 	fb_aper = bus_space_mmap(adev->memt, adev->fb_aper_offset, 0, 0, 0);
1924 	if (fb_aper != -1)
1925 		rasops_claim_framebuffer(fb_aper, adev->fb_aper_size, self);
1926 
1927 
1928 	adev->shutdown = true;
1929 	config_mountroot(self, amdgpu_attachhook);
1930 }
1931 
1932 int
1933 amdgpu_forcedetach(struct amdgpu_device *adev)
1934 {
1935 	struct pci_softc	*sc = (struct pci_softc *)adev->self.dv_parent;
1936 	pcitag_t		 tag = adev->pa_tag;
1937 
1938 #if NVGA > 0
1939 	if (adev->primary)
1940 		vga_console_attached = 0;
1941 #endif
1942 
1943 	/* reprobe pci device for non efi systems */
1944 #if NEFIFB > 0
1945 	if (bios_efiinfo == NULL && !efifb_cb_found()) {
1946 #endif
1947 		config_detach(&adev->self, 0);
1948 		return pci_probe_device(sc, tag, NULL, NULL);
1949 #if NEFIFB > 0
1950 	} else if (adev->primary) {
1951 		efifb_reattach();
1952 	}
1953 #endif
1954 
1955 	return 0;
1956 }
1957 
1958 void amdgpu_burner(void *, u_int, u_int);
1959 int amdgpu_wsioctl(void *, u_long, caddr_t, int, struct proc *);
1960 paddr_t amdgpu_wsmmap(void *, off_t, int);
1961 int amdgpu_alloc_screen(void *, const struct wsscreen_descr *,
1962     void **, int *, int *, uint32_t *);
1963 void amdgpu_free_screen(void *, void *);
1964 int amdgpu_show_screen(void *, void *, int,
1965     void (*)(void *, int, int), void *);
1966 void amdgpu_doswitch(void *);
1967 void amdgpu_enter_ddb(void *, void *);
1968 
1969 struct wsscreen_descr amdgpu_stdscreen = {
1970 	"std",
1971 	0, 0,
1972 	0,
1973 	0, 0,
1974 	WSSCREEN_UNDERLINE | WSSCREEN_HILIT |
1975 	WSSCREEN_REVERSE | WSSCREEN_WSCOLORS
1976 };
1977 
1978 const struct wsscreen_descr *amdgpu_scrlist[] = {
1979 	&amdgpu_stdscreen,
1980 };
1981 
1982 struct wsscreen_list amdgpu_screenlist = {
1983 	nitems(amdgpu_scrlist), amdgpu_scrlist
1984 };
1985 
1986 struct wsdisplay_accessops amdgpu_accessops = {
1987 	.ioctl = amdgpu_wsioctl,
1988 	.mmap = amdgpu_wsmmap,
1989 	.alloc_screen = amdgpu_alloc_screen,
1990 	.free_screen = amdgpu_free_screen,
1991 	.show_screen = amdgpu_show_screen,
1992 	.enter_ddb = amdgpu_enter_ddb,
1993 	.getchar = rasops_getchar,
1994 	.load_font = rasops_load_font,
1995 	.list_font = rasops_list_font,
1996 	.scrollback = rasops_scrollback,
1997 	.burn_screen = amdgpu_burner
1998 };
1999 
2000 int
2001 amdgpu_wsioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
2002 {
2003 	struct rasops_info *ri = v;
2004 	struct amdgpu_device *adev = ri->ri_hw;
2005 	struct backlight_device *bd = adev->dm.backlight_dev[0];
2006 	struct wsdisplay_param *dp = (struct wsdisplay_param *)data;
2007 	struct wsdisplay_fbinfo *wdf;
2008 
2009 	switch (cmd) {
2010 	case WSDISPLAYIO_GTYPE:
2011 		*(u_int *)data = WSDISPLAY_TYPE_RADEONDRM;
2012 		return 0;
2013 	case WSDISPLAYIO_GINFO:
2014 		wdf = (struct wsdisplay_fbinfo *)data;
2015 		wdf->width = ri->ri_width;
2016 		wdf->height = ri->ri_height;
2017 		wdf->depth = ri->ri_depth;
2018 		wdf->stride = ri->ri_stride;
2019 		wdf->offset = 0;
2020 		wdf->cmsize = 0;
2021 		return 0;
2022 	case WSDISPLAYIO_GETPARAM:
2023 		if (bd == NULL)
2024 			return -1;
2025 
2026 		switch (dp->param) {
2027 		case WSDISPLAYIO_PARAM_BRIGHTNESS:
2028 			dp->min = 0;
2029 			dp->max = bd->props.max_brightness;
2030 			dp->curval = bd->props.brightness;
2031 			return (dp->max > dp->min) ? 0 : -1;
2032 		}
2033 		break;
2034 	case WSDISPLAYIO_SETPARAM:
2035 		if (bd == NULL || dp->curval > bd->props.max_brightness)
2036 			return -1;
2037 
2038 		switch (dp->param) {
2039 		case WSDISPLAYIO_PARAM_BRIGHTNESS:
2040 			bd->props.brightness = dp->curval;
2041 			backlight_update_status(bd);
2042 			return 0;
2043 		}
2044 		break;
2045 	}
2046 
2047 	return (-1);
2048 }
2049 
2050 paddr_t
2051 amdgpu_wsmmap(void *v, off_t off, int prot)
2052 {
2053 	return (-1);
2054 }
2055 
2056 int
2057 amdgpu_alloc_screen(void *v, const struct wsscreen_descr *type,
2058     void **cookiep, int *curxp, int *curyp, uint32_t *attrp)
2059 {
2060 	return rasops_alloc_screen(v, cookiep, curxp, curyp, attrp);
2061 }
2062 
2063 void
2064 amdgpu_free_screen(void *v, void *cookie)
2065 {
2066 	return rasops_free_screen(v, cookie);
2067 }
2068 
2069 int
2070 amdgpu_show_screen(void *v, void *cookie, int waitok,
2071     void (*cb)(void *, int, int), void *cbarg)
2072 {
2073 	struct rasops_info *ri = v;
2074 	struct amdgpu_device *adev = ri->ri_hw;
2075 
2076 	if (cookie == ri->ri_active)
2077 		return (0);
2078 
2079 	adev->switchcb = cb;
2080 	adev->switchcbarg = cbarg;
2081 	adev->switchcookie = cookie;
2082 	if (cb) {
2083 		task_add(systq, &adev->switchtask);
2084 		return (EAGAIN);
2085 	}
2086 
2087 	amdgpu_doswitch(v);
2088 
2089 	return (0);
2090 }
2091 
2092 void
2093 amdgpu_doswitch(void *v)
2094 {
2095 	struct rasops_info *ri = v;
2096 	struct amdgpu_device *adev = ri->ri_hw;
2097 	struct amdgpu_crtc *amdgpu_crtc;
2098 	int i, crtc;
2099 
2100 	rasops_show_screen(ri, adev->switchcookie, 0, NULL, NULL);
2101 	drm_fb_helper_restore_fbdev_mode_unlocked((void *)adev->mode_info.rfbdev);
2102 
2103 	if (adev->switchcb)
2104 		(adev->switchcb)(adev->switchcbarg, 0, 0);
2105 }
2106 
2107 void
2108 amdgpu_enter_ddb(void *v, void *cookie)
2109 {
2110 	struct rasops_info *ri = v;
2111 	struct amdgpu_device *adev = ri->ri_hw;
2112 	struct drm_fb_helper *fb_helper = (void *)adev->mode_info.rfbdev;
2113 
2114 	if (cookie == ri->ri_active)
2115 		return;
2116 
2117 	rasops_show_screen(ri, cookie, 0, NULL, NULL);
2118 	drm_fb_helper_debug_enter(fb_helper->fbdev);
2119 }
2120 
2121 
2122 void
2123 amdgpu_attachhook(struct device *self)
2124 {
2125 	struct amdgpu_device	*adev = (struct amdgpu_device *)self;
2126 	struct drm_device	*dev = &adev->ddev;
2127 	int r, acpi_status;
2128 
2129 	/* amdgpu_device_init should report only fatal error
2130 	 * like memory allocation failure or iomapping failure,
2131 	 * or memory manager initialization failure, it must
2132 	 * properly initialize the GPU MC controller and permit
2133 	 * VRAM allocation
2134 	 */
2135 	r = amdgpu_device_init(adev, adev->flags);
2136 	if (r) {
2137 		dev_err(&dev->pdev->dev, "Fatal error during GPU init\n");
2138 		goto out;
2139 	}
2140 
2141 	if (amdgpu_device_supports_boco(dev) &&
2142 	    (amdgpu_runtime_pm != 0)) /* enable runpm by default for boco */
2143 		adev->runpm = true;
2144 	else if (amdgpu_device_supports_baco(dev) &&
2145 		 (amdgpu_runtime_pm != 0) &&
2146 		 (adev->asic_type >= CHIP_TOPAZ) &&
2147 		 (adev->asic_type != CHIP_VEGA10) &&
2148 		 (adev->asic_type != CHIP_VEGA20) &&
2149 		 (adev->asic_type != CHIP_ARCTURUS)) /* enable runpm on VI+ */
2150 		adev->runpm = true;
2151 	else if (amdgpu_device_supports_baco(dev) &&
2152 		 (amdgpu_runtime_pm > 0))  /* enable runpm if runpm=1 on CI */
2153 		adev->runpm = true;
2154 
2155 	/* Call ACPI methods: require modeset init
2156 	 * but failure is not fatal
2157 	 */
2158 	if (!r) {
2159 		acpi_status = amdgpu_acpi_init(adev);
2160 		if (acpi_status)
2161 			dev_dbg(&dev->pdev->dev,
2162 				"Error during ACPI methods call\n");
2163 	}
2164 
2165 	if (adev->runpm) {
2166 		dev_pm_set_driver_flags(dev->dev, DPM_FLAG_NEVER_SKIP);
2167 		pm_runtime_use_autosuspend(dev->dev);
2168 		pm_runtime_set_autosuspend_delay(dev->dev, 5000);
2169 		pm_runtime_set_active(dev->dev);
2170 		pm_runtime_allow(dev->dev);
2171 		pm_runtime_mark_last_busy(dev->dev);
2172 		pm_runtime_put_autosuspend(dev->dev);
2173 	}
2174 {
2175 	struct wsemuldisplaydev_attach_args aa;
2176 	struct rasops_info *ri = &adev->ro;
2177 
2178 	task_set(&adev->switchtask, amdgpu_doswitch, ri);
2179 
2180 	if (ri->ri_bits == NULL)
2181 		return;
2182 
2183 	ri->ri_flg = RI_CENTER | RI_VCONS | RI_WRONLY;
2184 	rasops_init(ri, 160, 160);
2185 
2186 	ri->ri_hw = adev;
2187 
2188 	amdgpu_stdscreen.capabilities = ri->ri_caps;
2189 	amdgpu_stdscreen.nrows = ri->ri_rows;
2190 	amdgpu_stdscreen.ncols = ri->ri_cols;
2191 	amdgpu_stdscreen.textops = &ri->ri_ops;
2192 	amdgpu_stdscreen.fontwidth = ri->ri_font->fontwidth;
2193 	amdgpu_stdscreen.fontheight = ri->ri_font->fontheight;
2194 
2195 	aa.console = adev->console;
2196 	aa.primary = adev->primary;
2197 	aa.scrdata = &amdgpu_screenlist;
2198 	aa.accessops = &amdgpu_accessops;
2199 	aa.accesscookie = ri;
2200 	aa.defaultscreens = 0;
2201 
2202 	if (adev->console) {
2203 		uint32_t defattr;
2204 
2205 		ri->ri_ops.pack_attr(ri->ri_active, 0, 0, 0, &defattr);
2206 		wsdisplay_cnattach(&amdgpu_stdscreen, ri->ri_active,
2207 		    ri->ri_ccol, ri->ri_crow, defattr);
2208 	}
2209 
2210 	/*
2211 	 * Now that we've taken over the console, disable decoding of
2212 	 * VGA legacy addresses, and opt out of arbitration.
2213 	 */
2214 	amdgpu_asic_set_vga_state(adev, false);
2215 	pci_disable_legacy_vga(&adev->self);
2216 
2217 	printf("%s: %dx%d, %dbpp\n", adev->self.dv_xname,
2218 	    ri->ri_width, ri->ri_height, ri->ri_depth);
2219 
2220 	config_found_sm(&adev->self, &aa, wsemuldisplaydevprint,
2221 	    wsemuldisplaydevsubmatch);
2222 
2223 	/*
2224 	 * in linux via amdgpu_pci_probe -> drm_dev_register
2225 	 */
2226 	drm_dev_register(dev, adev->flags);
2227 }
2228 
2229 out:
2230 	if (r) {
2231 		/* balance pm_runtime_get_sync in amdgpu_driver_unload_kms */
2232 		if (adev->runpm)
2233 			pm_runtime_put_noidle(dev->dev);
2234 		amdgpu_fatal_error = 1;
2235 		amdgpu_forcedetach(adev);
2236 	}
2237 }
2238 
2239 /* from amdgpu_exit amdgpu_driver_unload_kms */
2240 int
2241 amdgpu_detach(struct device *self, int flags)
2242 {
2243 	struct amdgpu_device *adev = (struct amdgpu_device *)self;
2244 	struct drm_device *dev = &adev->ddev;
2245 
2246 	if (adev == NULL)
2247 		return 0;
2248 
2249 	amdgpu_refcnt--;
2250 
2251 	if (amdgpu_refcnt == 0)
2252 		amdgpu_amdkfd_fini();
2253 
2254 	pci_intr_disestablish(adev->pc, adev->irqh);
2255 
2256 	amdgpu_unregister_gpu_instance(adev);
2257 
2258 	if (adev->runpm) {
2259 		pm_runtime_get_sync(dev->dev);
2260 		pm_runtime_forbid(dev->dev);
2261 	}
2262 
2263 	amdgpu_acpi_fini(adev);
2264 	amdgpu_device_fini_hw(adev);
2265 
2266 	if (amdgpu_refcnt == 0) {
2267 		amdgpu_unregister_atpx_handler();
2268 		amdgpu_sync_fini();
2269 		amdgpu_fence_slab_fini();
2270 
2271 		drm_sched_fence_slab_fini();
2272 	}
2273 
2274 	config_detach(adev->ddev.dev, flags);
2275 
2276 	return 0;
2277 }
2278 
2279 int
2280 amdgpu_activate(struct device *self, int act)
2281 {
2282 	struct amdgpu_device *adev = (struct amdgpu_device *)self;
2283 	struct drm_device *dev = &adev->ddev;
2284 	int rv = 0;
2285 
2286 	if (dev->dev == NULL)
2287 		return (0);
2288 
2289 	switch (act) {
2290 	case DVACT_QUIESCE:
2291 		rv = config_activate_children(self, act);
2292 		amdgpu_device_suspend(dev, true);
2293 		break;
2294 	case DVACT_SUSPEND:
2295 		break;
2296 	case DVACT_RESUME:
2297 		break;
2298 	case DVACT_WAKEUP:
2299 		amdgpu_device_resume(dev, true);
2300 		rv = config_activate_children(self, act);
2301 		break;
2302 	}
2303 
2304 	return (rv);
2305 }
2306