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