xref: /openbsd-src/sys/dev/pci/drm/amd/amdgpu/amdgpu_kms.c (revision 7350f337b9e3eb4461d99580e625c7ef148d107c)
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 #include <drm/drmP.h>
29 #include "amdgpu.h"
30 #include <drm/amdgpu_drm.h>
31 #include "amdgpu_sched.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/pm_runtime.h>
39 #include "amdgpu_amdkfd.h"
40 
41 #include "vga.h"
42 
43 #if NVGA > 0
44 #include <dev/ic/mc6845reg.h>
45 #include <dev/ic/pcdisplayvar.h>
46 #include <dev/ic/vgareg.h>
47 #include <dev/ic/vgavar.h>
48 
49 extern int vga_console_attached;
50 #endif
51 
52 #ifdef __amd64__
53 #include "efifb.h"
54 #include <machine/biosvar.h>
55 #endif
56 
57 #if NEFIFB > 0
58 #include <machine/efifbvar.h>
59 #endif
60 
61 int     amdgpu_probe(struct device *, void *, void *);
62 void    amdgpu_attach(struct device *, struct device *, void *);
63 int     amdgpu_detach(struct device *, int);
64 int     amdgpu_activate(struct device *, int);
65 void    amdgpu_attachhook(struct device *);
66 int     amdgpu_forcedetach(struct amdgpu_device *);
67 
68 bool	amdgpu_msi_ok(struct amdgpu_device *);
69 
70 extern const struct drm_pcidev amdgpu_pciidlist[];
71 extern struct drm_driver amdgpu_kms_driver;
72 extern int amdgpu_exp_hw_support;
73 
74 /*
75  * set if the mountroot hook has a fatal error
76  * such as not being able to find the firmware
77  */
78 int amdgpu_fatal_error;
79 
80 struct cfattach amdgpu_ca = {
81         sizeof (struct amdgpu_device), amdgpu_probe, amdgpu_attach,
82         amdgpu_detach, amdgpu_activate
83 };
84 
85 struct cfdriver amdgpu_cd = {
86         NULL, "amdgpu", DV_DULL
87 };
88 
89 #ifdef __linux__
90 /**
91  * amdgpu_driver_unload_kms - Main unload function for KMS.
92  *
93  * @dev: drm dev pointer
94  *
95  * This is the main unload function for KMS (all asics).
96  * Returns 0 on success.
97  */
98 void amdgpu_driver_unload_kms(struct drm_device *dev)
99 {
100 	struct amdgpu_device *adev = dev->dev_private;
101 
102 	if (adev == NULL)
103 		return;
104 
105 	if (adev->rmmio == NULL)
106 		goto done_free;
107 
108 	if (amdgpu_sriov_vf(adev))
109 		amdgpu_virt_request_full_gpu(adev, false);
110 
111 	if (amdgpu_device_is_px(dev)) {
112 		pm_runtime_get_sync(dev->dev);
113 		pm_runtime_forbid(dev->dev);
114 	}
115 
116 	amdgpu_acpi_fini(adev);
117 
118 	amdgpu_device_fini(adev);
119 
120 done_free:
121 	kfree(adev);
122 	dev->dev_private = NULL;
123 }
124 
125 /**
126  * amdgpu_driver_load_kms - Main load function for KMS.
127  *
128  * @dev: drm dev pointer
129  * @flags: device flags
130  *
131  * This is the main load function for KMS (all asics).
132  * Returns 0 on success, error on failure.
133  */
134 int amdgpu_driver_load_kms(struct drm_device *dev, unsigned long flags)
135 {
136 	struct amdgpu_device *adev;
137 	int r, acpi_status;
138 
139 #ifdef CONFIG_DRM_AMDGPU_SI
140 	if (!amdgpu_si_support) {
141 		switch (flags & AMD_ASIC_MASK) {
142 		case CHIP_TAHITI:
143 		case CHIP_PITCAIRN:
144 		case CHIP_VERDE:
145 		case CHIP_OLAND:
146 		case CHIP_HAINAN:
147 			dev_info(dev->dev,
148 				 "SI support provided by radeon.\n");
149 			dev_info(dev->dev,
150 				 "Use radeon.si_support=0 amdgpu.si_support=1 to override.\n"
151 				);
152 			return -ENODEV;
153 		}
154 	}
155 #endif
156 #ifdef CONFIG_DRM_AMDGPU_CIK
157 	if (!amdgpu_cik_support) {
158 		switch (flags & AMD_ASIC_MASK) {
159 		case CHIP_KAVERI:
160 		case CHIP_BONAIRE:
161 		case CHIP_HAWAII:
162 		case CHIP_KABINI:
163 		case CHIP_MULLINS:
164 			dev_info(dev->dev,
165 				 "CIK support provided by radeon.\n");
166 			dev_info(dev->dev,
167 				 "Use radeon.cik_support=0 amdgpu.cik_support=1 to override.\n"
168 				);
169 			return -ENODEV;
170 		}
171 	}
172 #endif
173 
174 	adev = kzalloc(sizeof(struct amdgpu_device), GFP_KERNEL);
175 	if (adev == NULL) {
176 		return -ENOMEM;
177 	}
178 	dev->dev_private = (void *)adev;
179 
180 	if ((amdgpu_runtime_pm != 0) &&
181 	    amdgpu_has_atpx() &&
182 	    (amdgpu_is_atpx_hybrid() ||
183 	     amdgpu_has_atpx_dgpu_power_cntl()) &&
184 	    ((flags & AMD_IS_APU) == 0) &&
185 	    !pci_is_thunderbolt_attached(dev->pdev))
186 		flags |= AMD_IS_PX;
187 
188 	/* amdgpu_device_init should report only fatal error
189 	 * like memory allocation failure or iomapping failure,
190 	 * or memory manager initialization failure, it must
191 	 * properly initialize the GPU MC controller and permit
192 	 * VRAM allocation
193 	 */
194 	r = amdgpu_device_init(adev, dev, dev->pdev, flags);
195 	if (r) {
196 		dev_err(&dev->pdev->dev, "Fatal error during GPU init\n");
197 		goto out;
198 	}
199 
200 	/* Call ACPI methods: require modeset init
201 	 * but failure is not fatal
202 	 */
203 	if (!r) {
204 		acpi_status = amdgpu_acpi_init(adev);
205 		if (acpi_status)
206 		dev_dbg(&dev->pdev->dev,
207 				"Error during ACPI methods call\n");
208 	}
209 
210 	if (amdgpu_device_is_px(dev)) {
211 		dev_pm_set_driver_flags(dev->dev, DPM_FLAG_NEVER_SKIP);
212 		pm_runtime_use_autosuspend(dev->dev);
213 		pm_runtime_set_autosuspend_delay(dev->dev, 5000);
214 		pm_runtime_set_active(dev->dev);
215 		pm_runtime_allow(dev->dev);
216 		pm_runtime_mark_last_busy(dev->dev);
217 		pm_runtime_put_autosuspend(dev->dev);
218 	}
219 
220 out:
221 	if (r) {
222 		/* balance pm_runtime_get_sync in amdgpu_driver_unload_kms */
223 		if (adev->rmmio && amdgpu_device_is_px(dev))
224 			pm_runtime_put_noidle(dev->dev);
225 		amdgpu_driver_unload_kms(dev);
226 	}
227 
228 	return r;
229 }
230 #endif /* __linux__ */
231 
232 static int amdgpu_firmware_info(struct drm_amdgpu_info_firmware *fw_info,
233 				struct drm_amdgpu_query_fw *query_fw,
234 				struct amdgpu_device *adev)
235 {
236 	switch (query_fw->fw_type) {
237 	case AMDGPU_INFO_FW_VCE:
238 		fw_info->ver = adev->vce.fw_version;
239 		fw_info->feature = adev->vce.fb_version;
240 		break;
241 	case AMDGPU_INFO_FW_UVD:
242 		fw_info->ver = adev->uvd.fw_version;
243 		fw_info->feature = 0;
244 		break;
245 	case AMDGPU_INFO_FW_VCN:
246 		fw_info->ver = adev->vcn.fw_version;
247 		fw_info->feature = 0;
248 		break;
249 	case AMDGPU_INFO_FW_GMC:
250 		fw_info->ver = adev->gmc.fw_version;
251 		fw_info->feature = 0;
252 		break;
253 	case AMDGPU_INFO_FW_GFX_ME:
254 		fw_info->ver = adev->gfx.me_fw_version;
255 		fw_info->feature = adev->gfx.me_feature_version;
256 		break;
257 	case AMDGPU_INFO_FW_GFX_PFP:
258 		fw_info->ver = adev->gfx.pfp_fw_version;
259 		fw_info->feature = adev->gfx.pfp_feature_version;
260 		break;
261 	case AMDGPU_INFO_FW_GFX_CE:
262 		fw_info->ver = adev->gfx.ce_fw_version;
263 		fw_info->feature = adev->gfx.ce_feature_version;
264 		break;
265 	case AMDGPU_INFO_FW_GFX_RLC:
266 		fw_info->ver = adev->gfx.rlc_fw_version;
267 		fw_info->feature = adev->gfx.rlc_feature_version;
268 		break;
269 	case AMDGPU_INFO_FW_GFX_RLC_RESTORE_LIST_CNTL:
270 		fw_info->ver = adev->gfx.rlc_srlc_fw_version;
271 		fw_info->feature = adev->gfx.rlc_srlc_feature_version;
272 		break;
273 	case AMDGPU_INFO_FW_GFX_RLC_RESTORE_LIST_GPM_MEM:
274 		fw_info->ver = adev->gfx.rlc_srlg_fw_version;
275 		fw_info->feature = adev->gfx.rlc_srlg_feature_version;
276 		break;
277 	case AMDGPU_INFO_FW_GFX_RLC_RESTORE_LIST_SRM_MEM:
278 		fw_info->ver = adev->gfx.rlc_srls_fw_version;
279 		fw_info->feature = adev->gfx.rlc_srls_feature_version;
280 		break;
281 	case AMDGPU_INFO_FW_GFX_MEC:
282 		if (query_fw->index == 0) {
283 			fw_info->ver = adev->gfx.mec_fw_version;
284 			fw_info->feature = adev->gfx.mec_feature_version;
285 		} else if (query_fw->index == 1) {
286 			fw_info->ver = adev->gfx.mec2_fw_version;
287 			fw_info->feature = adev->gfx.mec2_feature_version;
288 		} else
289 			return -EINVAL;
290 		break;
291 	case AMDGPU_INFO_FW_SMC:
292 		fw_info->ver = adev->pm.fw_version;
293 		fw_info->feature = 0;
294 		break;
295 	case AMDGPU_INFO_FW_SDMA:
296 		if (query_fw->index >= adev->sdma.num_instances)
297 			return -EINVAL;
298 		fw_info->ver = adev->sdma.instance[query_fw->index].fw_version;
299 		fw_info->feature = adev->sdma.instance[query_fw->index].feature_version;
300 		break;
301 	case AMDGPU_INFO_FW_SOS:
302 		fw_info->ver = adev->psp.sos_fw_version;
303 		fw_info->feature = adev->psp.sos_feature_version;
304 		break;
305 	case AMDGPU_INFO_FW_ASD:
306 		fw_info->ver = adev->psp.asd_fw_version;
307 		fw_info->feature = adev->psp.asd_feature_version;
308 		break;
309 	default:
310 		return -EINVAL;
311 	}
312 	return 0;
313 }
314 
315 /*
316  * Userspace get information ioctl
317  */
318 /**
319  * amdgpu_info_ioctl - answer a device specific request.
320  *
321  * @adev: amdgpu device pointer
322  * @data: request object
323  * @filp: drm filp
324  *
325  * This function is used to pass device specific parameters to the userspace
326  * drivers.  Examples include: pci device id, pipeline parms, tiling params,
327  * etc. (all asics).
328  * Returns 0 on success, -EINVAL on failure.
329  */
330 static int amdgpu_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
331 {
332 	struct amdgpu_device *adev = dev->dev_private;
333 	struct drm_amdgpu_info *info = data;
334 	struct amdgpu_mode_info *minfo = &adev->mode_info;
335 	void __user *out = (void __user *)(uintptr_t)info->return_pointer;
336 	uint32_t size = info->return_size;
337 	struct drm_crtc *crtc;
338 	uint32_t ui32 = 0;
339 	uint64_t ui64 = 0;
340 	int i, j, found;
341 	int ui32_size = sizeof(ui32);
342 
343 	if (!info->return_size || !info->return_pointer)
344 		return -EINVAL;
345 
346 	switch (info->query) {
347 	case AMDGPU_INFO_ACCEL_WORKING:
348 		ui32 = adev->accel_working;
349 		return copy_to_user(out, &ui32, min(size, 4u)) ? -EFAULT : 0;
350 	case AMDGPU_INFO_CRTC_FROM_ID:
351 		for (i = 0, found = 0; i < adev->mode_info.num_crtc; i++) {
352 			crtc = (struct drm_crtc *)minfo->crtcs[i];
353 			if (crtc && crtc->base.id == info->mode_crtc.id) {
354 				struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
355 				ui32 = amdgpu_crtc->crtc_id;
356 				found = 1;
357 				break;
358 			}
359 		}
360 		if (!found) {
361 			DRM_DEBUG_KMS("unknown crtc id %d\n", info->mode_crtc.id);
362 			return -EINVAL;
363 		}
364 		return copy_to_user(out, &ui32, min(size, 4u)) ? -EFAULT : 0;
365 	case AMDGPU_INFO_HW_IP_INFO: {
366 		struct drm_amdgpu_info_hw_ip ip = {};
367 		enum amd_ip_block_type type;
368 		uint32_t ring_mask = 0;
369 		uint32_t ib_start_alignment = 0;
370 		uint32_t ib_size_alignment = 0;
371 
372 		if (info->query_hw_ip.ip_instance >= AMDGPU_HW_IP_INSTANCE_MAX_COUNT)
373 			return -EINVAL;
374 
375 		switch (info->query_hw_ip.type) {
376 		case AMDGPU_HW_IP_GFX:
377 			type = AMD_IP_BLOCK_TYPE_GFX;
378 			for (i = 0; i < adev->gfx.num_gfx_rings; i++)
379 				ring_mask |= adev->gfx.gfx_ring[i].ready << i;
380 			ib_start_alignment = 32;
381 			ib_size_alignment = 32;
382 			break;
383 		case AMDGPU_HW_IP_COMPUTE:
384 			type = AMD_IP_BLOCK_TYPE_GFX;
385 			for (i = 0; i < adev->gfx.num_compute_rings; i++)
386 				ring_mask |= adev->gfx.compute_ring[i].ready << i;
387 			ib_start_alignment = 32;
388 			ib_size_alignment = 32;
389 			break;
390 		case AMDGPU_HW_IP_DMA:
391 			type = AMD_IP_BLOCK_TYPE_SDMA;
392 			for (i = 0; i < adev->sdma.num_instances; i++)
393 				ring_mask |= adev->sdma.instance[i].ring.ready << i;
394 			ib_start_alignment = 256;
395 			ib_size_alignment = 4;
396 			break;
397 		case AMDGPU_HW_IP_UVD:
398 			type = AMD_IP_BLOCK_TYPE_UVD;
399 			for (i = 0; i < adev->uvd.num_uvd_inst; i++) {
400 				if (adev->uvd.harvest_config & (1 << i))
401 					continue;
402 				ring_mask |= adev->uvd.inst[i].ring.ready;
403 			}
404 			ib_start_alignment = 64;
405 			ib_size_alignment = 64;
406 			break;
407 		case AMDGPU_HW_IP_VCE:
408 			type = AMD_IP_BLOCK_TYPE_VCE;
409 			for (i = 0; i < adev->vce.num_rings; i++)
410 				ring_mask |= adev->vce.ring[i].ready << i;
411 			ib_start_alignment = 4;
412 			ib_size_alignment = 1;
413 			break;
414 		case AMDGPU_HW_IP_UVD_ENC:
415 			type = AMD_IP_BLOCK_TYPE_UVD;
416 			for (i = 0; i < adev->uvd.num_uvd_inst; i++) {
417 				if (adev->uvd.harvest_config & (1 << i))
418 					continue;
419 				for (j = 0; j < adev->uvd.num_enc_rings; j++)
420 					ring_mask |= adev->uvd.inst[i].ring_enc[j].ready << j;
421 			}
422 			ib_start_alignment = 64;
423 			ib_size_alignment = 64;
424 			break;
425 		case AMDGPU_HW_IP_VCN_DEC:
426 			type = AMD_IP_BLOCK_TYPE_VCN;
427 			ring_mask = adev->vcn.ring_dec.ready;
428 			ib_start_alignment = 16;
429 			ib_size_alignment = 16;
430 			break;
431 		case AMDGPU_HW_IP_VCN_ENC:
432 			type = AMD_IP_BLOCK_TYPE_VCN;
433 			for (i = 0; i < adev->vcn.num_enc_rings; i++)
434 				ring_mask |= adev->vcn.ring_enc[i].ready << i;
435 			ib_start_alignment = 64;
436 			ib_size_alignment = 1;
437 			break;
438 		case AMDGPU_HW_IP_VCN_JPEG:
439 			type = AMD_IP_BLOCK_TYPE_VCN;
440 			ring_mask = adev->vcn.ring_jpeg.ready;
441 			ib_start_alignment = 16;
442 			ib_size_alignment = 16;
443 			break;
444 		default:
445 			return -EINVAL;
446 		}
447 
448 		for (i = 0; i < adev->num_ip_blocks; i++) {
449 			if (adev->ip_blocks[i].version->type == type &&
450 			    adev->ip_blocks[i].status.valid) {
451 				ip.hw_ip_version_major = adev->ip_blocks[i].version->major;
452 				ip.hw_ip_version_minor = adev->ip_blocks[i].version->minor;
453 				ip.capabilities_flags = 0;
454 				ip.available_rings = ring_mask;
455 				ip.ib_start_alignment = ib_start_alignment;
456 				ip.ib_size_alignment = ib_size_alignment;
457 				break;
458 			}
459 		}
460 		return copy_to_user(out, &ip,
461 				    min((size_t)size, sizeof(ip))) ? -EFAULT : 0;
462 	}
463 	case AMDGPU_INFO_HW_IP_COUNT: {
464 		enum amd_ip_block_type type;
465 		uint32_t count = 0;
466 
467 		switch (info->query_hw_ip.type) {
468 		case AMDGPU_HW_IP_GFX:
469 			type = AMD_IP_BLOCK_TYPE_GFX;
470 			break;
471 		case AMDGPU_HW_IP_COMPUTE:
472 			type = AMD_IP_BLOCK_TYPE_GFX;
473 			break;
474 		case AMDGPU_HW_IP_DMA:
475 			type = AMD_IP_BLOCK_TYPE_SDMA;
476 			break;
477 		case AMDGPU_HW_IP_UVD:
478 			type = AMD_IP_BLOCK_TYPE_UVD;
479 			break;
480 		case AMDGPU_HW_IP_VCE:
481 			type = AMD_IP_BLOCK_TYPE_VCE;
482 			break;
483 		case AMDGPU_HW_IP_UVD_ENC:
484 			type = AMD_IP_BLOCK_TYPE_UVD;
485 			break;
486 		case AMDGPU_HW_IP_VCN_DEC:
487 		case AMDGPU_HW_IP_VCN_ENC:
488 		case AMDGPU_HW_IP_VCN_JPEG:
489 			type = AMD_IP_BLOCK_TYPE_VCN;
490 			break;
491 		default:
492 			return -EINVAL;
493 		}
494 
495 		for (i = 0; i < adev->num_ip_blocks; i++)
496 			if (adev->ip_blocks[i].version->type == type &&
497 			    adev->ip_blocks[i].status.valid &&
498 			    count < AMDGPU_HW_IP_INSTANCE_MAX_COUNT)
499 				count++;
500 
501 		return copy_to_user(out, &count, min(size, 4u)) ? -EFAULT : 0;
502 	}
503 	case AMDGPU_INFO_TIMESTAMP:
504 		ui64 = amdgpu_gfx_get_gpu_clock_counter(adev);
505 		return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
506 	case AMDGPU_INFO_FW_VERSION: {
507 		struct drm_amdgpu_info_firmware fw_info;
508 		int ret;
509 
510 		/* We only support one instance of each IP block right now. */
511 		if (info->query_fw.ip_instance != 0)
512 			return -EINVAL;
513 
514 		ret = amdgpu_firmware_info(&fw_info, &info->query_fw, adev);
515 		if (ret)
516 			return ret;
517 
518 		return copy_to_user(out, &fw_info,
519 				    min((size_t)size, sizeof(fw_info))) ? -EFAULT : 0;
520 	}
521 	case AMDGPU_INFO_NUM_BYTES_MOVED:
522 		ui64 = atomic64_read(&adev->num_bytes_moved);
523 		return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
524 	case AMDGPU_INFO_NUM_EVICTIONS:
525 		ui64 = atomic64_read(&adev->num_evictions);
526 		return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
527 	case AMDGPU_INFO_NUM_VRAM_CPU_PAGE_FAULTS:
528 		ui64 = atomic64_read(&adev->num_vram_cpu_page_faults);
529 		return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
530 	case AMDGPU_INFO_VRAM_USAGE:
531 		ui64 = amdgpu_vram_mgr_usage(&adev->mman.bdev.man[TTM_PL_VRAM]);
532 		return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
533 	case AMDGPU_INFO_VIS_VRAM_USAGE:
534 		ui64 = amdgpu_vram_mgr_vis_usage(&adev->mman.bdev.man[TTM_PL_VRAM]);
535 		return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
536 	case AMDGPU_INFO_GTT_USAGE:
537 		ui64 = amdgpu_gtt_mgr_usage(&adev->mman.bdev.man[TTM_PL_TT]);
538 		return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
539 	case AMDGPU_INFO_GDS_CONFIG: {
540 		struct drm_amdgpu_info_gds gds_info;
541 
542 		memset(&gds_info, 0, sizeof(gds_info));
543 		gds_info.gds_gfx_partition_size = adev->gds.mem.gfx_partition_size >> AMDGPU_GDS_SHIFT;
544 		gds_info.compute_partition_size = adev->gds.mem.cs_partition_size >> AMDGPU_GDS_SHIFT;
545 		gds_info.gds_total_size = adev->gds.mem.total_size >> AMDGPU_GDS_SHIFT;
546 		gds_info.gws_per_gfx_partition = adev->gds.gws.gfx_partition_size >> AMDGPU_GWS_SHIFT;
547 		gds_info.gws_per_compute_partition = adev->gds.gws.cs_partition_size >> AMDGPU_GWS_SHIFT;
548 		gds_info.oa_per_gfx_partition = adev->gds.oa.gfx_partition_size >> AMDGPU_OA_SHIFT;
549 		gds_info.oa_per_compute_partition = adev->gds.oa.cs_partition_size >> AMDGPU_OA_SHIFT;
550 		return copy_to_user(out, &gds_info,
551 				    min((size_t)size, sizeof(gds_info))) ? -EFAULT : 0;
552 	}
553 	case AMDGPU_INFO_VRAM_GTT: {
554 		struct drm_amdgpu_info_vram_gtt vram_gtt;
555 
556 		vram_gtt.vram_size = adev->gmc.real_vram_size -
557 			atomic64_read(&adev->vram_pin_size);
558 		vram_gtt.vram_cpu_accessible_size = adev->gmc.visible_vram_size -
559 			atomic64_read(&adev->visible_pin_size);
560 		vram_gtt.gtt_size = adev->mman.bdev.man[TTM_PL_TT].size;
561 		vram_gtt.gtt_size *= PAGE_SIZE;
562 		vram_gtt.gtt_size -= atomic64_read(&adev->gart_pin_size);
563 		return copy_to_user(out, &vram_gtt,
564 				    min((size_t)size, sizeof(vram_gtt))) ? -EFAULT : 0;
565 	}
566 	case AMDGPU_INFO_MEMORY: {
567 		struct drm_amdgpu_memory_info mem;
568 
569 		memset(&mem, 0, sizeof(mem));
570 		mem.vram.total_heap_size = adev->gmc.real_vram_size;
571 		mem.vram.usable_heap_size = adev->gmc.real_vram_size -
572 			atomic64_read(&adev->vram_pin_size);
573 		mem.vram.heap_usage =
574 			amdgpu_vram_mgr_usage(&adev->mman.bdev.man[TTM_PL_VRAM]);
575 		mem.vram.max_allocation = mem.vram.usable_heap_size * 3 / 4;
576 
577 		mem.cpu_accessible_vram.total_heap_size =
578 			adev->gmc.visible_vram_size;
579 		mem.cpu_accessible_vram.usable_heap_size = adev->gmc.visible_vram_size -
580 			atomic64_read(&adev->visible_pin_size);
581 		mem.cpu_accessible_vram.heap_usage =
582 			amdgpu_vram_mgr_vis_usage(&adev->mman.bdev.man[TTM_PL_VRAM]);
583 		mem.cpu_accessible_vram.max_allocation =
584 			mem.cpu_accessible_vram.usable_heap_size * 3 / 4;
585 
586 		mem.gtt.total_heap_size = adev->mman.bdev.man[TTM_PL_TT].size;
587 		mem.gtt.total_heap_size *= PAGE_SIZE;
588 		mem.gtt.usable_heap_size = mem.gtt.total_heap_size -
589 			atomic64_read(&adev->gart_pin_size);
590 		mem.gtt.heap_usage =
591 			amdgpu_gtt_mgr_usage(&adev->mman.bdev.man[TTM_PL_TT]);
592 		mem.gtt.max_allocation = mem.gtt.usable_heap_size * 3 / 4;
593 
594 		return copy_to_user(out, &mem,
595 				    min((size_t)size, sizeof(mem)))
596 				    ? -EFAULT : 0;
597 	}
598 	case AMDGPU_INFO_READ_MMR_REG: {
599 		unsigned n, alloc_size;
600 		uint32_t *regs;
601 		unsigned se_num = (info->read_mmr_reg.instance >>
602 				   AMDGPU_INFO_MMR_SE_INDEX_SHIFT) &
603 				  AMDGPU_INFO_MMR_SE_INDEX_MASK;
604 		unsigned sh_num = (info->read_mmr_reg.instance >>
605 				   AMDGPU_INFO_MMR_SH_INDEX_SHIFT) &
606 				  AMDGPU_INFO_MMR_SH_INDEX_MASK;
607 
608 		/* set full masks if the userspace set all bits
609 		 * in the bitfields */
610 		if (se_num == AMDGPU_INFO_MMR_SE_INDEX_MASK)
611 			se_num = 0xffffffff;
612 		if (sh_num == AMDGPU_INFO_MMR_SH_INDEX_MASK)
613 			sh_num = 0xffffffff;
614 
615 		regs = kmalloc_array(info->read_mmr_reg.count, sizeof(*regs), GFP_KERNEL);
616 		if (!regs)
617 			return -ENOMEM;
618 		alloc_size = info->read_mmr_reg.count * sizeof(*regs);
619 
620 		for (i = 0; i < info->read_mmr_reg.count; i++)
621 			if (amdgpu_asic_read_register(adev, se_num, sh_num,
622 						      info->read_mmr_reg.dword_offset + i,
623 						      &regs[i])) {
624 				DRM_DEBUG_KMS("unallowed offset %#x\n",
625 					      info->read_mmr_reg.dword_offset + i);
626 				kfree(regs);
627 				return -EFAULT;
628 			}
629 		n = copy_to_user(out, regs, min(size, alloc_size));
630 		kfree(regs);
631 		return n ? -EFAULT : 0;
632 	}
633 	case AMDGPU_INFO_DEV_INFO: {
634 		struct drm_amdgpu_info_device dev_info = {};
635 		uint64_t vm_size;
636 
637 		dev_info.device_id = dev->pdev->device;
638 		dev_info.chip_rev = adev->rev_id;
639 		dev_info.external_rev = adev->external_rev_id;
640 		dev_info.pci_rev = dev->pdev->revision;
641 		dev_info.family = adev->family;
642 		dev_info.num_shader_engines = adev->gfx.config.max_shader_engines;
643 		dev_info.num_shader_arrays_per_engine = adev->gfx.config.max_sh_per_se;
644 		/* return all clocks in KHz */
645 		dev_info.gpu_counter_freq = amdgpu_asic_get_xclk(adev) * 10;
646 		if (adev->pm.dpm_enabled) {
647 			dev_info.max_engine_clock = amdgpu_dpm_get_sclk(adev, false) * 10;
648 			dev_info.max_memory_clock = amdgpu_dpm_get_mclk(adev, false) * 10;
649 		} else {
650 			dev_info.max_engine_clock = adev->clock.default_sclk * 10;
651 			dev_info.max_memory_clock = adev->clock.default_mclk * 10;
652 		}
653 		dev_info.enabled_rb_pipes_mask = adev->gfx.config.backend_enable_mask;
654 		dev_info.num_rb_pipes = adev->gfx.config.max_backends_per_se *
655 			adev->gfx.config.max_shader_engines;
656 		dev_info.num_hw_gfx_contexts = adev->gfx.config.max_hw_contexts;
657 		dev_info._pad = 0;
658 		dev_info.ids_flags = 0;
659 		if (adev->flags & AMD_IS_APU)
660 			dev_info.ids_flags |= AMDGPU_IDS_FLAGS_FUSION;
661 		if (amdgpu_sriov_vf(adev))
662 			dev_info.ids_flags |= AMDGPU_IDS_FLAGS_PREEMPTION;
663 
664 		vm_size = adev->vm_manager.max_pfn * AMDGPU_GPU_PAGE_SIZE;
665 		vm_size -= AMDGPU_VA_RESERVED_SIZE;
666 
667 		/* Older VCE FW versions are buggy and can handle only 40bits */
668 		if (adev->vce.fw_version < AMDGPU_VCE_FW_53_45)
669 			vm_size = min(vm_size, 1ULL << 40);
670 
671 		dev_info.virtual_address_offset = AMDGPU_VA_RESERVED_SIZE;
672 		dev_info.virtual_address_max =
673 			min(vm_size, AMDGPU_VA_HOLE_START);
674 
675 		if (vm_size > AMDGPU_VA_HOLE_START) {
676 			dev_info.high_va_offset = AMDGPU_VA_HOLE_END;
677 			dev_info.high_va_max = AMDGPU_VA_HOLE_END | vm_size;
678 		}
679 		dev_info.virtual_address_alignment = max((int)PAGE_SIZE, AMDGPU_GPU_PAGE_SIZE);
680 		dev_info.pte_fragment_size = (1 << adev->vm_manager.fragment_size) * AMDGPU_GPU_PAGE_SIZE;
681 		dev_info.gart_page_size = AMDGPU_GPU_PAGE_SIZE;
682 		dev_info.cu_active_number = adev->gfx.cu_info.number;
683 		dev_info.cu_ao_mask = adev->gfx.cu_info.ao_cu_mask;
684 		dev_info.ce_ram_size = adev->gfx.ce_ram_size;
685 		memcpy(&dev_info.cu_ao_bitmap[0], &adev->gfx.cu_info.ao_cu_bitmap[0],
686 		       sizeof(adev->gfx.cu_info.ao_cu_bitmap));
687 		memcpy(&dev_info.cu_bitmap[0], &adev->gfx.cu_info.bitmap[0],
688 		       sizeof(adev->gfx.cu_info.bitmap));
689 		dev_info.vram_type = adev->gmc.vram_type;
690 		dev_info.vram_bit_width = adev->gmc.vram_width;
691 		dev_info.vce_harvest_config = adev->vce.harvest_config;
692 		dev_info.gc_double_offchip_lds_buf =
693 			adev->gfx.config.double_offchip_lds_buf;
694 
695 		if (amdgpu_ngg) {
696 			dev_info.prim_buf_gpu_addr = adev->gfx.ngg.buf[NGG_PRIM].gpu_addr;
697 			dev_info.prim_buf_size = adev->gfx.ngg.buf[NGG_PRIM].size;
698 			dev_info.pos_buf_gpu_addr = adev->gfx.ngg.buf[NGG_POS].gpu_addr;
699 			dev_info.pos_buf_size = adev->gfx.ngg.buf[NGG_POS].size;
700 			dev_info.cntl_sb_buf_gpu_addr = adev->gfx.ngg.buf[NGG_CNTL].gpu_addr;
701 			dev_info.cntl_sb_buf_size = adev->gfx.ngg.buf[NGG_CNTL].size;
702 			dev_info.param_buf_gpu_addr = adev->gfx.ngg.buf[NGG_PARAM].gpu_addr;
703 			dev_info.param_buf_size = adev->gfx.ngg.buf[NGG_PARAM].size;
704 		}
705 		dev_info.wave_front_size = adev->gfx.cu_info.wave_front_size;
706 		dev_info.num_shader_visible_vgprs = adev->gfx.config.max_gprs;
707 		dev_info.num_cu_per_sh = adev->gfx.config.max_cu_per_sh;
708 		dev_info.num_tcc_blocks = adev->gfx.config.max_texture_channel_caches;
709 		dev_info.gs_vgt_table_depth = adev->gfx.config.gs_vgt_table_depth;
710 		dev_info.gs_prim_buffer_depth = adev->gfx.config.gs_prim_buffer_depth;
711 		dev_info.max_gs_waves_per_vgt = adev->gfx.config.max_gs_threads;
712 
713 		return copy_to_user(out, &dev_info,
714 				    min((size_t)size, sizeof(dev_info))) ? -EFAULT : 0;
715 	}
716 	case AMDGPU_INFO_VCE_CLOCK_TABLE: {
717 		unsigned i;
718 		struct drm_amdgpu_info_vce_clock_table vce_clk_table = {};
719 		struct amd_vce_state *vce_state;
720 
721 		for (i = 0; i < AMDGPU_VCE_CLOCK_TABLE_ENTRIES; i++) {
722 			vce_state = amdgpu_dpm_get_vce_clock_state(adev, i);
723 			if (vce_state) {
724 				vce_clk_table.entries[i].sclk = vce_state->sclk;
725 				vce_clk_table.entries[i].mclk = vce_state->mclk;
726 				vce_clk_table.entries[i].eclk = vce_state->evclk;
727 				vce_clk_table.num_valid_entries++;
728 			}
729 		}
730 
731 		return copy_to_user(out, &vce_clk_table,
732 				    min((size_t)size, sizeof(vce_clk_table))) ? -EFAULT : 0;
733 	}
734 	case AMDGPU_INFO_VBIOS: {
735 		uint32_t bios_size = adev->bios_size;
736 
737 		switch (info->vbios_info.type) {
738 		case AMDGPU_INFO_VBIOS_SIZE:
739 			return copy_to_user(out, &bios_size,
740 					min((size_t)size, sizeof(bios_size)))
741 					? -EFAULT : 0;
742 		case AMDGPU_INFO_VBIOS_IMAGE: {
743 			uint8_t *bios;
744 			uint32_t bios_offset = info->vbios_info.offset;
745 
746 			if (bios_offset >= bios_size)
747 				return -EINVAL;
748 
749 			bios = adev->bios + bios_offset;
750 			return copy_to_user(out, bios,
751 					    min((size_t)size, (size_t)(bios_size - bios_offset)))
752 					? -EFAULT : 0;
753 		}
754 		default:
755 			DRM_DEBUG_KMS("Invalid request %d\n",
756 					info->vbios_info.type);
757 			return -EINVAL;
758 		}
759 	}
760 	case AMDGPU_INFO_NUM_HANDLES: {
761 		struct drm_amdgpu_info_num_handles handle;
762 
763 		switch (info->query_hw_ip.type) {
764 		case AMDGPU_HW_IP_UVD:
765 			/* Starting Polaris, we support unlimited UVD handles */
766 			if (adev->asic_type < CHIP_POLARIS10) {
767 				handle.uvd_max_handles = adev->uvd.max_handles;
768 				handle.uvd_used_handles = amdgpu_uvd_used_handles(adev);
769 
770 				return copy_to_user(out, &handle,
771 					min((size_t)size, sizeof(handle))) ? -EFAULT : 0;
772 			} else {
773 				return -ENODATA;
774 			}
775 
776 			break;
777 		default:
778 			return -EINVAL;
779 		}
780 	}
781 	case AMDGPU_INFO_SENSOR: {
782 		if (!adev->pm.dpm_enabled)
783 			return -ENOENT;
784 
785 		switch (info->sensor_info.type) {
786 		case AMDGPU_INFO_SENSOR_GFX_SCLK:
787 			/* get sclk in Mhz */
788 			if (amdgpu_dpm_read_sensor(adev,
789 						   AMDGPU_PP_SENSOR_GFX_SCLK,
790 						   (void *)&ui32, &ui32_size)) {
791 				return -EINVAL;
792 			}
793 			ui32 /= 100;
794 			break;
795 		case AMDGPU_INFO_SENSOR_GFX_MCLK:
796 			/* get mclk in Mhz */
797 			if (amdgpu_dpm_read_sensor(adev,
798 						   AMDGPU_PP_SENSOR_GFX_MCLK,
799 						   (void *)&ui32, &ui32_size)) {
800 				return -EINVAL;
801 			}
802 			ui32 /= 100;
803 			break;
804 		case AMDGPU_INFO_SENSOR_GPU_TEMP:
805 			/* get temperature in millidegrees C */
806 			if (amdgpu_dpm_read_sensor(adev,
807 						   AMDGPU_PP_SENSOR_GPU_TEMP,
808 						   (void *)&ui32, &ui32_size)) {
809 				return -EINVAL;
810 			}
811 			break;
812 		case AMDGPU_INFO_SENSOR_GPU_LOAD:
813 			/* get GPU load */
814 			if (amdgpu_dpm_read_sensor(adev,
815 						   AMDGPU_PP_SENSOR_GPU_LOAD,
816 						   (void *)&ui32, &ui32_size)) {
817 				return -EINVAL;
818 			}
819 			break;
820 		case AMDGPU_INFO_SENSOR_GPU_AVG_POWER:
821 			/* get average GPU power */
822 			if (amdgpu_dpm_read_sensor(adev,
823 						   AMDGPU_PP_SENSOR_GPU_POWER,
824 						   (void *)&ui32, &ui32_size)) {
825 				return -EINVAL;
826 			}
827 			ui32 >>= 8;
828 			break;
829 		case AMDGPU_INFO_SENSOR_VDDNB:
830 			/* get VDDNB in millivolts */
831 			if (amdgpu_dpm_read_sensor(adev,
832 						   AMDGPU_PP_SENSOR_VDDNB,
833 						   (void *)&ui32, &ui32_size)) {
834 				return -EINVAL;
835 			}
836 			break;
837 		case AMDGPU_INFO_SENSOR_VDDGFX:
838 			/* get VDDGFX in millivolts */
839 			if (amdgpu_dpm_read_sensor(adev,
840 						   AMDGPU_PP_SENSOR_VDDGFX,
841 						   (void *)&ui32, &ui32_size)) {
842 				return -EINVAL;
843 			}
844 			break;
845 		case AMDGPU_INFO_SENSOR_STABLE_PSTATE_GFX_SCLK:
846 			/* get stable pstate sclk in Mhz */
847 			if (amdgpu_dpm_read_sensor(adev,
848 						   AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK,
849 						   (void *)&ui32, &ui32_size)) {
850 				return -EINVAL;
851 			}
852 			ui32 /= 100;
853 			break;
854 		case AMDGPU_INFO_SENSOR_STABLE_PSTATE_GFX_MCLK:
855 			/* get stable pstate mclk in Mhz */
856 			if (amdgpu_dpm_read_sensor(adev,
857 						   AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK,
858 						   (void *)&ui32, &ui32_size)) {
859 				return -EINVAL;
860 			}
861 			ui32 /= 100;
862 			break;
863 		default:
864 			DRM_DEBUG_KMS("Invalid request %d\n",
865 				      info->sensor_info.type);
866 			return -EINVAL;
867 		}
868 		return copy_to_user(out, &ui32, min(size, 4u)) ? -EFAULT : 0;
869 	}
870 	case AMDGPU_INFO_VRAM_LOST_COUNTER:
871 		ui32 = atomic_read(&adev->vram_lost_counter);
872 		return copy_to_user(out, &ui32, min(size, 4u)) ? -EFAULT : 0;
873 	default:
874 		DRM_DEBUG_KMS("Invalid request %d\n", info->query);
875 		return -EINVAL;
876 	}
877 	return 0;
878 }
879 
880 
881 /*
882  * Outdated mess for old drm with Xorg being in charge (void function now).
883  */
884 /**
885  * amdgpu_driver_lastclose_kms - drm callback for last close
886  *
887  * @dev: drm dev pointer
888  *
889  * Switch vga_switcheroo state after last close (all asics).
890  */
891 void amdgpu_driver_lastclose_kms(struct drm_device *dev)
892 {
893 	drm_fb_helper_lastclose(dev);
894 	vga_switcheroo_process_delayed_switch();
895 }
896 
897 /**
898  * amdgpu_driver_open_kms - drm callback for open
899  *
900  * @dev: drm dev pointer
901  * @file_priv: drm file
902  *
903  * On device open, init vm on cayman+ (all asics).
904  * Returns 0 on success, error on failure.
905  */
906 int amdgpu_driver_open_kms(struct drm_device *dev, struct drm_file *file_priv)
907 {
908 	struct amdgpu_device *adev = dev->dev_private;
909 	struct amdgpu_fpriv *fpriv;
910 	int r, pasid;
911 
912 	/* Ensure IB tests are run on ring */
913 	flush_delayed_work(&adev->late_init_work);
914 
915 	file_priv->driver_priv = NULL;
916 
917 	r = pm_runtime_get_sync(dev->dev);
918 	if (r < 0)
919 		return r;
920 
921 	fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL);
922 	if (unlikely(!fpriv)) {
923 		r = -ENOMEM;
924 		goto out_suspend;
925 	}
926 
927 	pasid = amdgpu_pasid_alloc(16);
928 	if (pasid < 0) {
929 		dev_warn(adev->dev, "No more PASIDs available!");
930 		pasid = 0;
931 	}
932 	r = amdgpu_vm_init(adev, &fpriv->vm, AMDGPU_VM_CONTEXT_GFX, pasid);
933 	if (r)
934 		goto error_pasid;
935 
936 	fpriv->prt_va = amdgpu_vm_bo_add(adev, &fpriv->vm, NULL);
937 	if (!fpriv->prt_va) {
938 		r = -ENOMEM;
939 		goto error_vm;
940 	}
941 
942 	if (amdgpu_sriov_vf(adev)) {
943 		r = amdgpu_map_static_csa(adev, &fpriv->vm, &fpriv->csa_va);
944 		if (r)
945 			goto error_vm;
946 	}
947 
948 	rw_init(&fpriv->bo_list_lock, "agbo");
949 	idr_init(&fpriv->bo_list_handles);
950 
951 	amdgpu_ctx_mgr_init(&fpriv->ctx_mgr);
952 
953 	file_priv->driver_priv = fpriv;
954 	goto out_suspend;
955 
956 error_vm:
957 	amdgpu_vm_fini(adev, &fpriv->vm);
958 
959 error_pasid:
960 	if (pasid)
961 		amdgpu_pasid_free(pasid);
962 
963 	kfree(fpriv);
964 
965 out_suspend:
966 	pm_runtime_mark_last_busy(dev->dev);
967 	pm_runtime_put_autosuspend(dev->dev);
968 
969 	return r;
970 }
971 
972 /**
973  * amdgpu_driver_postclose_kms - drm callback for post close
974  *
975  * @dev: drm dev pointer
976  * @file_priv: drm file
977  *
978  * On device post close, tear down vm on cayman+ (all asics).
979  */
980 void amdgpu_driver_postclose_kms(struct drm_device *dev,
981 				 struct drm_file *file_priv)
982 {
983 	struct amdgpu_device *adev = dev->dev_private;
984 	struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
985 	struct amdgpu_bo_list *list;
986 	struct amdgpu_bo *pd;
987 	unsigned int pasid;
988 	int handle;
989 
990 	if (!fpriv)
991 		return;
992 
993 	pm_runtime_get_sync(dev->dev);
994 
995 	if (adev->asic_type != CHIP_RAVEN) {
996 		amdgpu_uvd_free_handles(adev, file_priv);
997 		amdgpu_vce_free_handles(adev, file_priv);
998 	}
999 
1000 	amdgpu_vm_bo_rmv(adev, fpriv->prt_va);
1001 
1002 	if (amdgpu_sriov_vf(adev)) {
1003 		/* TODO: how to handle reserve failure */
1004 		BUG_ON(amdgpu_bo_reserve(adev->virt.csa_obj, true));
1005 		amdgpu_vm_bo_rmv(adev, fpriv->csa_va);
1006 		fpriv->csa_va = NULL;
1007 		amdgpu_bo_unreserve(adev->virt.csa_obj);
1008 	}
1009 
1010 	pasid = fpriv->vm.pasid;
1011 	pd = amdgpu_bo_ref(fpriv->vm.root.base.bo);
1012 
1013 	amdgpu_vm_fini(adev, &fpriv->vm);
1014 	amdgpu_ctx_mgr_fini(&fpriv->ctx_mgr);
1015 
1016 	if (pasid)
1017 		amdgpu_pasid_free_delayed(pd->tbo.resv, pasid);
1018 	amdgpu_bo_unref(&pd);
1019 
1020 	idr_for_each_entry(&fpriv->bo_list_handles, list, handle)
1021 		amdgpu_bo_list_put(list);
1022 
1023 	idr_destroy(&fpriv->bo_list_handles);
1024 	mutex_destroy(&fpriv->bo_list_lock);
1025 
1026 	kfree(fpriv);
1027 	file_priv->driver_priv = NULL;
1028 
1029 	pm_runtime_mark_last_busy(dev->dev);
1030 	pm_runtime_put_autosuspend(dev->dev);
1031 }
1032 
1033 /*
1034  * VBlank related functions.
1035  */
1036 /**
1037  * amdgpu_get_vblank_counter_kms - get frame count
1038  *
1039  * @dev: drm dev pointer
1040  * @pipe: crtc to get the frame count from
1041  *
1042  * Gets the frame count on the requested crtc (all asics).
1043  * Returns frame count on success, -EINVAL on failure.
1044  */
1045 u32 amdgpu_get_vblank_counter_kms(struct drm_device *dev, unsigned int pipe)
1046 {
1047 	struct amdgpu_device *adev = dev->dev_private;
1048 	int vpos, hpos, stat;
1049 	u32 count;
1050 
1051 	if (pipe >= adev->mode_info.num_crtc) {
1052 		DRM_ERROR("Invalid crtc %u\n", pipe);
1053 		return -EINVAL;
1054 	}
1055 
1056 	/* The hw increments its frame counter at start of vsync, not at start
1057 	 * of vblank, as is required by DRM core vblank counter handling.
1058 	 * Cook the hw count here to make it appear to the caller as if it
1059 	 * incremented at start of vblank. We measure distance to start of
1060 	 * vblank in vpos. vpos therefore will be >= 0 between start of vblank
1061 	 * and start of vsync, so vpos >= 0 means to bump the hw frame counter
1062 	 * result by 1 to give the proper appearance to caller.
1063 	 */
1064 	if (adev->mode_info.crtcs[pipe]) {
1065 		/* Repeat readout if needed to provide stable result if
1066 		 * we cross start of vsync during the queries.
1067 		 */
1068 		do {
1069 			count = amdgpu_display_vblank_get_counter(adev, pipe);
1070 			/* Ask amdgpu_display_get_crtc_scanoutpos to return
1071 			 * vpos as distance to start of vblank, instead of
1072 			 * regular vertical scanout pos.
1073 			 */
1074 			stat = amdgpu_display_get_crtc_scanoutpos(
1075 				dev, pipe, GET_DISTANCE_TO_VBLANKSTART,
1076 				&vpos, &hpos, NULL, NULL,
1077 				&adev->mode_info.crtcs[pipe]->base.hwmode);
1078 		} while (count != amdgpu_display_vblank_get_counter(adev, pipe));
1079 
1080 		if (((stat & (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE)) !=
1081 		    (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE))) {
1082 			DRM_DEBUG_VBL("Query failed! stat %d\n", stat);
1083 		} else {
1084 			DRM_DEBUG_VBL("crtc %d: dist from vblank start %d\n",
1085 				      pipe, vpos);
1086 
1087 			/* Bump counter if we are at >= leading edge of vblank,
1088 			 * but before vsync where vpos would turn negative and
1089 			 * the hw counter really increments.
1090 			 */
1091 			if (vpos >= 0)
1092 				count++;
1093 		}
1094 	} else {
1095 		/* Fallback to use value as is. */
1096 		count = amdgpu_display_vblank_get_counter(adev, pipe);
1097 		DRM_DEBUG_VBL("NULL mode info! Returned count may be wrong.\n");
1098 	}
1099 
1100 	return count;
1101 }
1102 
1103 /**
1104  * amdgpu_enable_vblank_kms - enable vblank interrupt
1105  *
1106  * @dev: drm dev pointer
1107  * @pipe: crtc to enable vblank interrupt for
1108  *
1109  * Enable the interrupt on the requested crtc (all asics).
1110  * Returns 0 on success, -EINVAL on failure.
1111  */
1112 int amdgpu_enable_vblank_kms(struct drm_device *dev, unsigned int pipe)
1113 {
1114 	struct amdgpu_device *adev = dev->dev_private;
1115 	int idx = amdgpu_display_crtc_idx_to_irq_type(adev, pipe);
1116 
1117 	return amdgpu_irq_get(adev, &adev->crtc_irq, idx);
1118 }
1119 
1120 /**
1121  * amdgpu_disable_vblank_kms - disable vblank interrupt
1122  *
1123  * @dev: drm dev pointer
1124  * @pipe: crtc to disable vblank interrupt for
1125  *
1126  * Disable the interrupt on the requested crtc (all asics).
1127  */
1128 void amdgpu_disable_vblank_kms(struct drm_device *dev, unsigned int pipe)
1129 {
1130 	struct amdgpu_device *adev = dev->dev_private;
1131 	int idx = amdgpu_display_crtc_idx_to_irq_type(adev, pipe);
1132 
1133 	amdgpu_irq_put(adev, &adev->crtc_irq, idx);
1134 }
1135 
1136 const struct drm_ioctl_desc amdgpu_ioctls_kms[] = {
1137 	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_CREATE, amdgpu_gem_create_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
1138 	DRM_IOCTL_DEF_DRV(AMDGPU_CTX, amdgpu_ctx_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
1139 	DRM_IOCTL_DEF_DRV(AMDGPU_VM, amdgpu_vm_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
1140 	DRM_IOCTL_DEF_DRV(AMDGPU_SCHED, amdgpu_sched_ioctl, DRM_MASTER),
1141 	DRM_IOCTL_DEF_DRV(AMDGPU_BO_LIST, amdgpu_bo_list_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
1142 	DRM_IOCTL_DEF_DRV(AMDGPU_FENCE_TO_HANDLE, amdgpu_cs_fence_to_handle_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
1143 	/* KMS */
1144 	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_MMAP, amdgpu_gem_mmap_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
1145 	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_WAIT_IDLE, amdgpu_gem_wait_idle_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
1146 	DRM_IOCTL_DEF_DRV(AMDGPU_CS, amdgpu_cs_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
1147 	DRM_IOCTL_DEF_DRV(AMDGPU_INFO, amdgpu_info_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
1148 	DRM_IOCTL_DEF_DRV(AMDGPU_WAIT_CS, amdgpu_cs_wait_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
1149 	DRM_IOCTL_DEF_DRV(AMDGPU_WAIT_FENCES, amdgpu_cs_wait_fences_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
1150 	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_METADATA, amdgpu_gem_metadata_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
1151 	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_VA, amdgpu_gem_va_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
1152 	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_OP, amdgpu_gem_op_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
1153 	DRM_IOCTL_DEF_DRV(AMDGPU_GEM_USERPTR, amdgpu_gem_userptr_ioctl, DRM_AUTH|DRM_RENDER_ALLOW)
1154 };
1155 const int amdgpu_max_kms_ioctl = ARRAY_SIZE(amdgpu_ioctls_kms);
1156 
1157 /*
1158  * Debugfs info
1159  */
1160 #if defined(CONFIG_DEBUG_FS)
1161 
1162 static int amdgpu_debugfs_firmware_info(struct seq_file *m, void *data)
1163 {
1164 	struct drm_info_node *node = (struct drm_info_node *) m->private;
1165 	struct drm_device *dev = node->minor->dev;
1166 	struct amdgpu_device *adev = dev->dev_private;
1167 	struct drm_amdgpu_info_firmware fw_info;
1168 	struct drm_amdgpu_query_fw query_fw;
1169 	struct atom_context *ctx = adev->mode_info.atom_context;
1170 	int ret, i;
1171 
1172 	/* VCE */
1173 	query_fw.fw_type = AMDGPU_INFO_FW_VCE;
1174 	ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1175 	if (ret)
1176 		return ret;
1177 	seq_printf(m, "VCE feature version: %u, firmware version: 0x%08x\n",
1178 		   fw_info.feature, fw_info.ver);
1179 
1180 	/* UVD */
1181 	query_fw.fw_type = AMDGPU_INFO_FW_UVD;
1182 	ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1183 	if (ret)
1184 		return ret;
1185 	seq_printf(m, "UVD feature version: %u, firmware version: 0x%08x\n",
1186 		   fw_info.feature, fw_info.ver);
1187 
1188 	/* GMC */
1189 	query_fw.fw_type = AMDGPU_INFO_FW_GMC;
1190 	ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1191 	if (ret)
1192 		return ret;
1193 	seq_printf(m, "MC feature version: %u, firmware version: 0x%08x\n",
1194 		   fw_info.feature, fw_info.ver);
1195 
1196 	/* ME */
1197 	query_fw.fw_type = AMDGPU_INFO_FW_GFX_ME;
1198 	ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1199 	if (ret)
1200 		return ret;
1201 	seq_printf(m, "ME feature version: %u, firmware version: 0x%08x\n",
1202 		   fw_info.feature, fw_info.ver);
1203 
1204 	/* PFP */
1205 	query_fw.fw_type = AMDGPU_INFO_FW_GFX_PFP;
1206 	ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1207 	if (ret)
1208 		return ret;
1209 	seq_printf(m, "PFP feature version: %u, firmware version: 0x%08x\n",
1210 		   fw_info.feature, fw_info.ver);
1211 
1212 	/* CE */
1213 	query_fw.fw_type = AMDGPU_INFO_FW_GFX_CE;
1214 	ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1215 	if (ret)
1216 		return ret;
1217 	seq_printf(m, "CE feature version: %u, firmware version: 0x%08x\n",
1218 		   fw_info.feature, fw_info.ver);
1219 
1220 	/* RLC */
1221 	query_fw.fw_type = AMDGPU_INFO_FW_GFX_RLC;
1222 	ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1223 	if (ret)
1224 		return ret;
1225 	seq_printf(m, "RLC feature version: %u, firmware version: 0x%08x\n",
1226 		   fw_info.feature, fw_info.ver);
1227 
1228 	/* RLC SAVE RESTORE LIST CNTL */
1229 	query_fw.fw_type = AMDGPU_INFO_FW_GFX_RLC_RESTORE_LIST_CNTL;
1230 	ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1231 	if (ret)
1232 		return ret;
1233 	seq_printf(m, "RLC SRLC feature version: %u, firmware version: 0x%08x\n",
1234 		   fw_info.feature, fw_info.ver);
1235 
1236 	/* RLC SAVE RESTORE LIST GPM MEM */
1237 	query_fw.fw_type = AMDGPU_INFO_FW_GFX_RLC_RESTORE_LIST_GPM_MEM;
1238 	ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1239 	if (ret)
1240 		return ret;
1241 	seq_printf(m, "RLC SRLG feature version: %u, firmware version: 0x%08x\n",
1242 		   fw_info.feature, fw_info.ver);
1243 
1244 	/* RLC SAVE RESTORE LIST SRM MEM */
1245 	query_fw.fw_type = AMDGPU_INFO_FW_GFX_RLC_RESTORE_LIST_SRM_MEM;
1246 	ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1247 	if (ret)
1248 		return ret;
1249 	seq_printf(m, "RLC SRLS feature version: %u, firmware version: 0x%08x\n",
1250 		   fw_info.feature, fw_info.ver);
1251 
1252 	/* MEC */
1253 	query_fw.fw_type = AMDGPU_INFO_FW_GFX_MEC;
1254 	query_fw.index = 0;
1255 	ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1256 	if (ret)
1257 		return ret;
1258 	seq_printf(m, "MEC feature version: %u, firmware version: 0x%08x\n",
1259 		   fw_info.feature, fw_info.ver);
1260 
1261 	/* MEC2 */
1262 	if (adev->asic_type == CHIP_KAVERI ||
1263 	    (adev->asic_type > CHIP_TOPAZ && adev->asic_type != CHIP_STONEY)) {
1264 		query_fw.index = 1;
1265 		ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1266 		if (ret)
1267 			return ret;
1268 		seq_printf(m, "MEC2 feature version: %u, firmware version: 0x%08x\n",
1269 			   fw_info.feature, fw_info.ver);
1270 	}
1271 
1272 	/* PSP SOS */
1273 	query_fw.fw_type = AMDGPU_INFO_FW_SOS;
1274 	ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1275 	if (ret)
1276 		return ret;
1277 	seq_printf(m, "SOS feature version: %u, firmware version: 0x%08x\n",
1278 		   fw_info.feature, fw_info.ver);
1279 
1280 
1281 	/* PSP ASD */
1282 	query_fw.fw_type = AMDGPU_INFO_FW_ASD;
1283 	ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1284 	if (ret)
1285 		return ret;
1286 	seq_printf(m, "ASD feature version: %u, firmware version: 0x%08x\n",
1287 		   fw_info.feature, fw_info.ver);
1288 
1289 	/* SMC */
1290 	query_fw.fw_type = AMDGPU_INFO_FW_SMC;
1291 	ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1292 	if (ret)
1293 		return ret;
1294 	seq_printf(m, "SMC feature version: %u, firmware version: 0x%08x\n",
1295 		   fw_info.feature, fw_info.ver);
1296 
1297 	/* SDMA */
1298 	query_fw.fw_type = AMDGPU_INFO_FW_SDMA;
1299 	for (i = 0; i < adev->sdma.num_instances; i++) {
1300 		query_fw.index = i;
1301 		ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1302 		if (ret)
1303 			return ret;
1304 		seq_printf(m, "SDMA%d feature version: %u, firmware version: 0x%08x\n",
1305 			   i, fw_info.feature, fw_info.ver);
1306 	}
1307 
1308 	/* VCN */
1309 	query_fw.fw_type = AMDGPU_INFO_FW_VCN;
1310 	ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1311 	if (ret)
1312 		return ret;
1313 	seq_printf(m, "VCN feature version: %u, firmware version: 0x%08x\n",
1314 		   fw_info.feature, fw_info.ver);
1315 
1316 
1317 	seq_printf(m, "VBIOS version: %s\n", ctx->vbios_version);
1318 
1319 	return 0;
1320 }
1321 
1322 static const struct drm_info_list amdgpu_firmware_info_list[] = {
1323 	{"amdgpu_firmware_info", amdgpu_debugfs_firmware_info, 0, NULL},
1324 };
1325 #endif
1326 
1327 int amdgpu_debugfs_firmware_init(struct amdgpu_device *adev)
1328 {
1329 #if defined(CONFIG_DEBUG_FS)
1330 	return amdgpu_debugfs_add_files(adev, amdgpu_firmware_info_list,
1331 					ARRAY_SIZE(amdgpu_firmware_info_list));
1332 #else
1333 	return 0;
1334 #endif
1335 }
1336 
1337 int
1338 amdgpu_probe(struct device *parent, void *match, void *aux)
1339 {
1340 	if (amdgpu_fatal_error)
1341 		return 0;
1342 	if (drm_pciprobe(aux, amdgpu_pciidlist))
1343 		return 20;
1344 	return 0;
1345 }
1346 
1347 /*
1348  * some functions are only called once on init regardless of how many times
1349  * amdgpu attaches in linux this is handled via module_init()/module_exit()
1350  */
1351 int amdgpu_refcnt;
1352 
1353 int __init drm_sched_fence_slab_init(void);
1354 void __exit drm_sched_fence_slab_fini(void);
1355 
1356 void
1357 amdgpu_attach(struct device *parent, struct device *self, void *aux)
1358 {
1359 	struct amdgpu_device	*adev = (struct amdgpu_device *)self;
1360 	struct drm_device	*dev;
1361 	struct pci_attach_args	*pa = aux;
1362 	const struct drm_pcidev	*id_entry;
1363 	pcireg_t		 type;
1364 	int			 i;
1365 	uint8_t			 rmmio_bar;
1366 	paddr_t			 fb_aper;
1367 	pcireg_t		 addr, mask;
1368 	int			 s;
1369 
1370 	id_entry = drm_find_description(PCI_VENDOR(pa->pa_id),
1371 	    PCI_PRODUCT(pa->pa_id), amdgpu_pciidlist);
1372 	adev->flags = id_entry->driver_data;
1373 	adev->family = adev->flags & AMD_ASIC_MASK;
1374 	adev->pc = pa->pa_pc;
1375 	adev->pa_tag = pa->pa_tag;
1376 	adev->iot = pa->pa_iot;
1377 	adev->memt = pa->pa_memt;
1378 	adev->dmat = pa->pa_dmat;
1379 
1380 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_DISPLAY &&
1381 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_DISPLAY_VGA &&
1382 	    (pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG)
1383 	    & (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE))
1384 	    == (PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE)) {
1385 		adev->primary = 1;
1386 #if NVGA > 0
1387 		adev->console = vga_is_console(pa->pa_iot, -1);
1388 		vga_console_attached = 1;
1389 #endif
1390 	}
1391 #if NEFIFB > 0
1392 	if (efifb_is_primary(pa)) {
1393 		adev->primary = 1;
1394 		adev->console = efifb_is_console(pa);
1395 		efifb_detach();
1396 	}
1397 #endif
1398 
1399 #define AMDGPU_PCI_MEM		0x10
1400 
1401 	type = pci_mapreg_type(pa->pa_pc, pa->pa_tag, AMDGPU_PCI_MEM);
1402 	if (PCI_MAPREG_TYPE(type) != PCI_MAPREG_TYPE_MEM ||
1403 	    pci_mapreg_info(pa->pa_pc, pa->pa_tag, AMDGPU_PCI_MEM,
1404 	    type, &adev->fb_aper_offset, &adev->fb_aper_size, NULL)) {
1405 		printf(": can't get frambuffer info\n");
1406 		return;
1407 	}
1408 
1409 	if (adev->fb_aper_offset == 0) {
1410 		bus_size_t start, end, pci_mem_end;
1411 		bus_addr_t base;
1412 
1413 		start = max(PCI_MEM_START, pa->pa_memex->ex_start);
1414 		if (PCI_MAPREG_MEM_TYPE(type) == PCI_MAPREG_MEM_TYPE_64BIT)
1415 			pci_mem_end = PCI_MEM64_END;
1416 		else
1417 			pci_mem_end = PCI_MEM_END;
1418 		end = min(pci_mem_end, pa->pa_memex->ex_end);
1419 		if (pa->pa_memex == NULL ||
1420 		    extent_alloc_subregion(pa->pa_memex, start, end,
1421 		    adev->fb_aper_size, adev->fb_aper_size, 0, 0, 0, &base)) {
1422 			printf(": can't reserve framebuffer space\n");
1423 			return;
1424 		}
1425 		pci_conf_write(pa->pa_pc, pa->pa_tag, AMDGPU_PCI_MEM, base);
1426 		if (PCI_MAPREG_MEM_TYPE(type) == PCI_MAPREG_MEM_TYPE_64BIT)
1427 			pci_conf_write(pa->pa_pc, pa->pa_tag,
1428 			    AMDGPU_PCI_MEM + 4, (uint64_t)base >> 32);
1429 		adev->fb_aper_offset = base;
1430 	}
1431 
1432 	for (i = PCI_MAPREG_START; i < PCI_MAPREG_END ;) {
1433 		type = pci_mapreg_type(pa->pa_pc, pa->pa_tag, i);
1434 		if (PCI_MAPREG_TYPE(type) != PCI_MAPREG_TYPE_IO) {
1435 			if (type & PCI_MAPREG_MEM_TYPE_64BIT)
1436 				i += 8;
1437 			else
1438 				i += 4;
1439 			continue;
1440 		}
1441 		if (pci_mapreg_map(pa, i, type, 0,
1442 		    &adev->rio_mem_bst, &adev->rio_mem_bsh, NULL,
1443 		    &adev->rio_mem_size, 0)) {
1444 			printf(": can't map rio space\n");
1445 			return;
1446 		}
1447 		break;
1448 	}
1449 
1450 	if (adev->family >= CHIP_BONAIRE) {
1451 		type = pci_mapreg_type(pa->pa_pc, pa->pa_tag, 0x18);
1452 		if (PCI_MAPREG_TYPE(type) != PCI_MAPREG_TYPE_MEM ||
1453 		    pci_mapreg_map(pa, 0x18, type, 0,
1454 		    &adev->doorbell.bst, &adev->doorbell.bsh,
1455 		    &adev->doorbell.base, &adev->doorbell.size, 0)) {
1456 			printf(": can't map doorbell space\n");
1457 			return;
1458 		}
1459 	}
1460 
1461 	if (adev->family >= CHIP_BONAIRE)
1462 		rmmio_bar = 0x24;
1463 	else
1464 		rmmio_bar = 0x18;
1465 
1466 	type = pci_mapreg_type(pa->pa_pc, pa->pa_tag, rmmio_bar);
1467 	if (PCI_MAPREG_TYPE(type) != PCI_MAPREG_TYPE_MEM ||
1468 	    pci_mapreg_map(pa, rmmio_bar, type, 0,
1469 	    &adev->rmmio_bst, &adev->rmmio_bsh, &adev->rmmio_base,
1470 	    &adev->rmmio_size, 0)) {
1471 		printf(": can't map rmmio space\n");
1472 		return;
1473 	}
1474 
1475 	/*
1476 	 * Make sure we have a base address for the ROM such that we
1477 	 * can map it later.
1478 	 */
1479 	s = splhigh();
1480 	addr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_ROM_REG);
1481 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_ROM_REG, ~PCI_ROM_ENABLE);
1482 	mask = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_ROM_REG);
1483 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_ROM_REG, addr);
1484 	splx(s);
1485 
1486 	if (addr == 0 && PCI_ROM_SIZE(mask) != 0 && pa->pa_memex) {
1487 		bus_size_t size, start, end;
1488 		bus_addr_t base;
1489 
1490 		size = PCI_ROM_SIZE(mask);
1491 		start = max(PCI_MEM_START, pa->pa_memex->ex_start);
1492 		end = min(PCI_MEM_END, pa->pa_memex->ex_end);
1493 		if (extent_alloc_subregion(pa->pa_memex, start, end, size,
1494 		    size, 0, 0, 0, &base) == 0)
1495 			pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_ROM_REG, base);
1496 	}
1497 
1498 	printf("\n");
1499 
1500 	/* from amdgpu_init() */
1501 	if (amdgpu_refcnt == 0) {
1502 		drm_sched_fence_slab_init();
1503 
1504 		if (amdgpu_sync_init()) {
1505 			printf(": amdgpu_sync_init failed\n");
1506 			return;
1507 		}
1508 
1509 		if (amdgpu_fence_slab_init()) {
1510 			amdgpu_sync_fini();
1511 			printf(": amdgpu_fence_slab_init failed\n");
1512 			return;
1513 		}
1514 
1515 		amdgpu_kms_driver.num_ioctls = amdgpu_max_kms_ioctl;
1516 		amdgpu_register_atpx_handler();
1517 	}
1518 	amdgpu_refcnt++;
1519 
1520 	/* from amdgpu_pci_probe() */
1521 {
1522 	int ret;
1523 	bool supports_atomic = false;
1524 
1525 	if (!amdgpu_virtual_display &&
1526 	     amdgpu_device_asic_has_dc_support(adev->family))
1527 		supports_atomic = true;
1528 
1529 	if ((adev->flags & AMD_EXP_HW_SUPPORT) && !amdgpu_exp_hw_support) {
1530 		DRM_INFO("This hardware requires experimental hardware support.\n");
1531 	}
1532 
1533 	/*
1534 	 * Initialize amdkfd before starting radeon.
1535 	 */
1536 	amdgpu_amdkfd_init();
1537 
1538 	/* warn the user if they mix atomic and non-atomic capable GPUs */
1539 	if ((amdgpu_kms_driver.driver_features & DRIVER_ATOMIC) && !supports_atomic)
1540 		DRM_ERROR("Mixing atomic and non-atomic capable GPUs!\n");
1541 	/* support atomic early so the atomic debugfs stuff gets created */
1542 	if (supports_atomic)
1543 		amdgpu_kms_driver.driver_features |= DRIVER_ATOMIC;
1544 }
1545 
1546 	dev = drm_attach_pci(&amdgpu_kms_driver, pa, 0, adev->primary,
1547 	    self, NULL);
1548 	adev->ddev = dev;
1549 	adev->pdev = dev->pdev;
1550 
1551 	if (!amdgpu_msi_ok(adev))
1552 		pa->pa_flags &= ~PCI_FLAGS_MSI_ENABLED;
1553 
1554 	adev->irq.msi_enabled = false;
1555 	if (pci_intr_map_msi(pa, &adev->intrh) == 0)
1556 		adev->irq.msi_enabled = true;
1557 	else if (pci_intr_map(pa, &adev->intrh) != 0) {
1558 		printf(": couldn't map interrupt\n");
1559 		return;
1560 	}
1561 	printf("%s: %s\n", adev->self.dv_xname,
1562 	    pci_intr_string(pa->pa_pc, adev->intrh));
1563 
1564 	adev->irqh = pci_intr_establish(pa->pa_pc, adev->intrh, IPL_TTY,
1565 	    amdgpu_irq_handler, adev->ddev, adev->self.dv_xname);
1566 	if (adev->irqh == NULL) {
1567 		printf("%s: couldn't establish interrupt\n",
1568 		    adev->self.dv_xname);
1569 		return;
1570 	}
1571 	adev->pdev->irq = -1;
1572 
1573 	fb_aper = bus_space_mmap(adev->memt, adev->fb_aper_offset, 0, 0, 0);
1574 	if (fb_aper != -1)
1575 		rasops_claim_framebuffer(fb_aper, adev->fb_aper_size, self);
1576 
1577 
1578 	adev->shutdown = true;
1579 	config_mountroot(self, amdgpu_attachhook);
1580 }
1581 
1582 int
1583 amdgpu_forcedetach(struct amdgpu_device *adev)
1584 {
1585 	struct pci_softc	*sc = (struct pci_softc *)adev->self.dv_parent;
1586 	pcitag_t		 tag = adev->pa_tag;
1587 
1588 #if NVGA > 0
1589 	if (adev->primary)
1590 		vga_console_attached = 0;
1591 #endif
1592 
1593 	/* reprobe pci device for non efi systems */
1594 #if NEFIFB > 0
1595 	if (bios_efiinfo == NULL && !efifb_cb_found()) {
1596 #endif
1597 		config_detach(&adev->self, 0);
1598 		return pci_probe_device(sc, tag, NULL, NULL);
1599 #if NEFIFB > 0
1600 	} else if (adev->primary) {
1601 		efifb_reattach();
1602 	}
1603 #endif
1604 
1605 	return 0;
1606 }
1607 
1608 void amdgpu_burner(void *, u_int, u_int);
1609 int amdgpu_wsioctl(void *, u_long, caddr_t, int, struct proc *);
1610 paddr_t amdgpu_wsmmap(void *, off_t, int);
1611 int amdgpu_alloc_screen(void *, const struct wsscreen_descr *,
1612     void **, int *, int *, long *);
1613 void amdgpu_free_screen(void *, void *);
1614 int amdgpu_show_screen(void *, void *, int,
1615     void (*)(void *, int, int), void *);
1616 void amdgpu_doswitch(void *);
1617 void amdgpu_enter_ddb(void *, void *);
1618 
1619 struct wsscreen_descr amdgpu_stdscreen = {
1620 	"std",
1621 	0, 0,
1622 	0,
1623 	0, 0,
1624 	WSSCREEN_UNDERLINE | WSSCREEN_HILIT |
1625 	WSSCREEN_REVERSE | WSSCREEN_WSCOLORS
1626 };
1627 
1628 const struct wsscreen_descr *amdgpu_scrlist[] = {
1629 	&amdgpu_stdscreen,
1630 };
1631 
1632 struct wsscreen_list amdgpu_screenlist = {
1633 	nitems(amdgpu_scrlist), amdgpu_scrlist
1634 };
1635 
1636 struct wsdisplay_accessops amdgpu_accessops = {
1637 	.ioctl = amdgpu_wsioctl,
1638 	.mmap = amdgpu_wsmmap,
1639 	.alloc_screen = amdgpu_alloc_screen,
1640 	.free_screen = amdgpu_free_screen,
1641 	.show_screen = amdgpu_show_screen,
1642 	.enter_ddb = amdgpu_enter_ddb,
1643 	.getchar = rasops_getchar,
1644 	.load_font = rasops_load_font,
1645 	.list_font = rasops_list_font,
1646 	.scrollback = rasops_scrollback,
1647 	.burn_screen = amdgpu_burner
1648 };
1649 
1650 int
1651 amdgpu_wsioctl(void *v, u_long cmd, caddr_t data, int flag, struct proc *p)
1652 {
1653 	struct rasops_info *ri = v;
1654 	struct wsdisplay_fbinfo *wdf;
1655 
1656 	switch (cmd) {
1657 	case WSDISPLAYIO_GTYPE:
1658 		*(int *)data = WSDISPLAY_TYPE_RADEONDRM;
1659 		return 0;
1660 	case WSDISPLAYIO_GINFO:
1661 		wdf = (struct wsdisplay_fbinfo *)data;
1662 		wdf->width = ri->ri_width;
1663 		wdf->height = ri->ri_height;
1664 		wdf->depth = ri->ri_depth;
1665 		wdf->cmsize = 0;
1666 		return 0;
1667 	default:
1668 		return -1;
1669 	}
1670 }
1671 
1672 paddr_t
1673 amdgpu_wsmmap(void *v, off_t off, int prot)
1674 {
1675 	return (-1);
1676 }
1677 
1678 int
1679 amdgpu_alloc_screen(void *v, const struct wsscreen_descr *type,
1680     void **cookiep, int *curxp, int *curyp, long *attrp)
1681 {
1682 	return rasops_alloc_screen(v, cookiep, curxp, curyp, attrp);
1683 }
1684 
1685 void
1686 amdgpu_free_screen(void *v, void *cookie)
1687 {
1688 	return rasops_free_screen(v, cookie);
1689 }
1690 
1691 int
1692 amdgpu_show_screen(void *v, void *cookie, int waitok,
1693     void (*cb)(void *, int, int), void *cbarg)
1694 {
1695 	struct rasops_info *ri = v;
1696 	struct amdgpu_device *adev = ri->ri_hw;
1697 
1698 	if (cookie == ri->ri_active)
1699 		return (0);
1700 
1701 	adev->switchcb = cb;
1702 	adev->switchcbarg = cbarg;
1703 	adev->switchcookie = cookie;
1704 	if (cb) {
1705 		task_add(systq, &adev->switchtask);
1706 		return (EAGAIN);
1707 	}
1708 
1709 	amdgpu_doswitch(v);
1710 
1711 	return (0);
1712 }
1713 
1714 void
1715 amdgpu_doswitch(void *v)
1716 {
1717 	struct rasops_info *ri = v;
1718 	struct amdgpu_device *adev = ri->ri_hw;
1719 	struct amdgpu_crtc *amdgpu_crtc;
1720 	int i, crtc;
1721 
1722 	rasops_show_screen(ri, adev->switchcookie, 0, NULL, NULL);
1723 	drm_fb_helper_restore_fbdev_mode_unlocked((void *)adev->mode_info.rfbdev);
1724 
1725 	if (adev->switchcb)
1726 		(adev->switchcb)(adev->switchcbarg, 0, 0);
1727 }
1728 
1729 void
1730 amdgpu_enter_ddb(void *v, void *cookie)
1731 {
1732 	struct rasops_info *ri = v;
1733 	struct amdgpu_device *adev = ri->ri_hw;
1734 	struct drm_fb_helper *fb_helper = (void *)adev->mode_info.rfbdev;
1735 
1736 	if (cookie == ri->ri_active)
1737 		return;
1738 
1739 	rasops_show_screen(ri, cookie, 0, NULL, NULL);
1740 	drm_fb_helper_debug_enter(fb_helper->fbdev);
1741 }
1742 
1743 
1744 void
1745 amdgpu_attachhook(struct device *self)
1746 {
1747 	struct amdgpu_device	*adev = (struct amdgpu_device *)self;
1748 	struct drm_device	*dev = adev->ddev;
1749 	int r, acpi_status;
1750 
1751 	if ((amdgpu_runtime_pm != 0) &&
1752 	    amdgpu_has_atpx() &&
1753 	    (amdgpu_is_atpx_hybrid() ||
1754 	     amdgpu_has_atpx_dgpu_power_cntl()) &&
1755 	    ((adev->flags & AMD_IS_APU) == 0) &&
1756 	    !pci_is_thunderbolt_attached(dev->pdev))
1757 		adev->flags |= AMD_IS_PX;
1758 
1759 	/* amdgpu_device_init should report only fatal error
1760 	 * like memory allocation failure or iomapping failure,
1761 	 * or memory manager initialization failure, it must
1762 	 * properly initialize the GPU MC controller and permit
1763 	 * VRAM allocation
1764 	 */
1765 	r = amdgpu_device_init(adev, dev, dev->pdev, adev->flags);
1766 	if (r) {
1767 		dev_err(&dev->pdev->dev, "Fatal error during GPU init\n");
1768 		goto out;
1769 	}
1770 
1771 	/* Call ACPI methods: require modeset init
1772 	 * but failure is not fatal
1773 	 */
1774 	if (!r) {
1775 		acpi_status = amdgpu_acpi_init(adev);
1776 		if (acpi_status)
1777 		dev_dbg(&dev->pdev->dev,
1778 				"Error during ACPI methods call\n");
1779 	}
1780 
1781 	if (amdgpu_device_is_px(dev)) {
1782 		pm_runtime_use_autosuspend(dev->dev);
1783 		pm_runtime_set_autosuspend_delay(dev->dev, 5000);
1784 		pm_runtime_set_active(dev->dev);
1785 		pm_runtime_allow(dev->dev);
1786 		pm_runtime_mark_last_busy(dev->dev);
1787 		pm_runtime_put_autosuspend(dev->dev);
1788 	}
1789 
1790 {
1791 	struct drm_fb_helper *fb_helper = (void *)adev->mode_info.rfbdev;
1792 	struct wsemuldisplaydev_attach_args aa;
1793 	struct rasops_info *ri = &adev->ro;
1794 
1795 	task_set(&adev->switchtask, amdgpu_doswitch, ri);
1796 
1797 	if (ri->ri_bits == NULL)
1798 		return;
1799 
1800 	drm_fb_helper_restore_fbdev_mode_unlocked(fb_helper);
1801 
1802 	ri->ri_flg = RI_CENTER | RI_VCONS | RI_WRONLY;
1803 	rasops_init(ri, 160, 160);
1804 
1805 	ri->ri_hw = adev;
1806 
1807 	amdgpu_stdscreen.capabilities = ri->ri_caps;
1808 	amdgpu_stdscreen.nrows = ri->ri_rows;
1809 	amdgpu_stdscreen.ncols = ri->ri_cols;
1810 	amdgpu_stdscreen.textops = &ri->ri_ops;
1811 	amdgpu_stdscreen.fontwidth = ri->ri_font->fontwidth;
1812 	amdgpu_stdscreen.fontheight = ri->ri_font->fontheight;
1813 
1814 	aa.console = adev->console;
1815 	aa.primary = adev->primary;
1816 	aa.scrdata = &amdgpu_screenlist;
1817 	aa.accessops = &amdgpu_accessops;
1818 	aa.accesscookie = ri;
1819 	aa.defaultscreens = 0;
1820 
1821 	if (adev->console) {
1822 		long defattr;
1823 
1824 		ri->ri_ops.alloc_attr(ri->ri_active, 0, 0, 0, &defattr);
1825 		wsdisplay_cnattach(&amdgpu_stdscreen, ri->ri_active,
1826 		    ri->ri_ccol, ri->ri_crow, defattr);
1827 	}
1828 
1829 	/*
1830 	 * Now that we've taken over the console, disable decoding of
1831 	 * VGA legacy addresses, and opt out of arbitration.
1832 	 */
1833 	amdgpu_asic_set_vga_state(adev, false);
1834 	pci_disable_legacy_vga(&adev->self);
1835 
1836 	printf("%s: %dx%d, %dbpp\n", adev->self.dv_xname,
1837 	    ri->ri_width, ri->ri_height, ri->ri_depth);
1838 
1839 	config_found_sm(&adev->self, &aa, wsemuldisplaydevprint,
1840 	    wsemuldisplaydevsubmatch);
1841 
1842 	/*
1843 	 * in linux via amdgpu_pci_probe -> drm_dev_register
1844 	 */
1845 	drm_dev_register(dev, adev->flags);
1846 }
1847 
1848 out:
1849 	if (r) {
1850 		/* balance pm_runtime_get_sync in amdgpu_driver_unload_kms */
1851 		if (amdgpu_device_is_px(dev))
1852 			pm_runtime_put_noidle(dev->dev);
1853 		amdgpu_fatal_error = 1;
1854 		amdgpu_forcedetach(adev);
1855 	}
1856 }
1857 
1858 /* from amdgpu_exit amdgpu_driver_unload_kms */
1859 int
1860 amdgpu_detach(struct device *self, int flags)
1861 {
1862 	struct amdgpu_device *adev = (struct amdgpu_device *)self;
1863 	struct drm_device *dev = adev->ddev;
1864 
1865 	if (adev == NULL)
1866 		return 0;
1867 
1868 	amdgpu_refcnt--;
1869 
1870 	if (amdgpu_refcnt == 0)
1871 		amdgpu_amdkfd_fini();
1872 
1873 	pci_intr_disestablish(adev->pc, adev->irqh);
1874 
1875 	if (amdgpu_sriov_vf(adev))
1876 		amdgpu_virt_request_full_gpu(adev, false);
1877 
1878 	if (amdgpu_device_is_px(dev)) {
1879 		pm_runtime_get_sync(dev->dev);
1880 		pm_runtime_forbid(dev->dev);
1881 	}
1882 
1883 	amdgpu_acpi_fini(adev);
1884 
1885 	amdgpu_device_fini(adev);
1886 
1887 	if (amdgpu_refcnt == 0) {
1888 		amdgpu_unregister_atpx_handler();
1889 		amdgpu_sync_fini();
1890 		amdgpu_fence_slab_fini();
1891 
1892 		drm_sched_fence_slab_fini();
1893 	}
1894 
1895 	if (adev->ddev != NULL) {
1896 		config_detach(adev->ddev->dev, flags);
1897 		adev->ddev = NULL;
1898 	}
1899 
1900 	return 0;
1901 }
1902 
1903 int
1904 amdgpu_activate(struct device *self, int act)
1905 {
1906 	struct amdgpu_device *adev = (struct amdgpu_device *)self;
1907 	int rv = 0;
1908 
1909 	if (adev->ddev == NULL)
1910 		return (0);
1911 
1912 	switch (act) {
1913 	case DVACT_QUIESCE:
1914 		rv = config_activate_children(self, act);
1915 		amdgpu_device_suspend(adev->ddev, true, true);
1916 		break;
1917 	case DVACT_SUSPEND:
1918 		break;
1919 	case DVACT_RESUME:
1920 		break;
1921 	case DVACT_WAKEUP:
1922 		amdgpu_device_resume(adev->ddev, true, true);
1923 		rv = config_activate_children(self, act);
1924 		break;
1925 	}
1926 
1927 	return (rv);
1928 }
1929