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