2 * Copyright 2008 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 * Copyright 2009 Jerome Glisse.
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 * Authors: Dave Airlie
30 #include <drm/amdgpu_drm.h>
31 #include <drm/drm_drv.h>
32 #include "amdgpu_uvd.h"
33 #include "amdgpu_vce.h"
36 #include <linux/vga_switcheroo.h>
37 #include <linux/slab.h>
38 #include <linux/uaccess.h>
39 #include <linux/pci.h>
40 #include <linux/pm_runtime.h>
41 #include "amdgpu_amdkfd.h"
42 #include "amdgpu_gem.h"
43 #include "amdgpu_display.h"
44 #include "amdgpu_ras.h"
46 void amdgpu_unregister_gpu_instance(struct amdgpu_device *adev)
48 struct amdgpu_gpu_instance *gpu_instance;
51 mutex_lock(&mgpu_info.mutex);
53 for (i = 0; i < mgpu_info.num_gpu; i++) {
54 gpu_instance = &(mgpu_info.gpu_ins[i]);
55 if (gpu_instance->adev == adev) {
56 mgpu_info.gpu_ins[i] =
57 mgpu_info.gpu_ins[mgpu_info.num_gpu - 1];
59 if (adev->flags & AMD_IS_APU)
67 mutex_unlock(&mgpu_info.mutex);
71 * amdgpu_driver_unload_kms - Main unload function for KMS.
73 * @dev: drm dev pointer
75 * This is the main unload function for KMS (all asics).
76 * Returns 0 on success.
78 void amdgpu_driver_unload_kms(struct drm_device *dev)
80 struct amdgpu_device *adev = drm_to_adev(dev);
85 amdgpu_unregister_gpu_instance(adev);
87 if (adev->rmmio == NULL)
91 pm_runtime_get_sync(dev->dev);
92 pm_runtime_forbid(dev->dev);
95 if (amdgpu_acpi_smart_shift_update(dev, AMDGPU_SS_DRV_UNLOAD))
96 DRM_WARN("smart shift update failed\n");
98 amdgpu_acpi_fini(adev);
99 amdgpu_device_fini_hw(adev);
102 void amdgpu_register_gpu_instance(struct amdgpu_device *adev)
104 struct amdgpu_gpu_instance *gpu_instance;
106 mutex_lock(&mgpu_info.mutex);
108 if (mgpu_info.num_gpu >= MAX_GPU_INSTANCE) {
109 DRM_ERROR("Cannot register more gpu instance\n");
110 mutex_unlock(&mgpu_info.mutex);
114 gpu_instance = &(mgpu_info.gpu_ins[mgpu_info.num_gpu]);
115 gpu_instance->adev = adev;
116 gpu_instance->mgpu_fan_enabled = 0;
119 if (adev->flags & AMD_IS_APU)
122 mgpu_info.num_dgpu++;
124 mutex_unlock(&mgpu_info.mutex);
127 static void amdgpu_get_audio_func(struct amdgpu_device *adev)
129 struct pci_dev *p = NULL;
131 p = pci_get_domain_bus_and_slot(pci_domain_nr(adev->pdev->bus),
132 adev->pdev->bus->number, 1);
134 pm_runtime_get_sync(&p->dev);
136 pm_runtime_mark_last_busy(&p->dev);
137 pm_runtime_put_autosuspend(&p->dev);
144 * amdgpu_driver_load_kms - Main load function for KMS.
146 * @adev: pointer to struct amdgpu_device
147 * @flags: device flags
149 * This is the main load function for KMS (all asics).
150 * Returns 0 on success, error on failure.
152 int amdgpu_driver_load_kms(struct amdgpu_device *adev, unsigned long flags)
154 struct drm_device *dev;
155 struct pci_dev *parent;
158 dev = adev_to_drm(adev);
160 if (amdgpu_has_atpx() &&
161 (amdgpu_is_atpx_hybrid() ||
162 amdgpu_has_atpx_dgpu_power_cntl()) &&
163 ((flags & AMD_IS_APU) == 0) &&
164 !pci_is_thunderbolt_attached(to_pci_dev(dev->dev)))
167 parent = pci_upstream_bridge(adev->pdev);
168 adev->has_pr3 = parent ? pci_pr3_present(parent) : false;
170 /* amdgpu_device_init should report only fatal error
171 * like memory allocation failure or iomapping failure,
172 * or memory manager initialization failure, it must
173 * properly initialize the GPU MC controller and permit
176 r = amdgpu_device_init(adev, flags);
178 dev_err(dev->dev, "Fatal error during GPU init\n");
182 if (amdgpu_device_supports_px(dev) &&
183 (amdgpu_runtime_pm != 0)) { /* enable runpm by default for atpx */
185 dev_info(adev->dev, "Using ATPX for runtime pm\n");
186 } else if (amdgpu_device_supports_boco(dev) &&
187 (amdgpu_runtime_pm != 0)) { /* enable runpm by default for boco */
189 dev_info(adev->dev, "Using BOCO for runtime pm\n");
190 } else if (amdgpu_device_supports_baco(dev) &&
191 (amdgpu_runtime_pm != 0)) {
192 switch (adev->asic_type) {
195 /* enable runpm if runpm=1 */
196 if (amdgpu_runtime_pm > 0)
200 /* turn runpm on if noretry=0 */
201 if (!adev->gmc.noretry)
205 /* enable runpm on CI+ */
209 /* XXX: disable runtime pm if we are the primary adapter
210 * to avoid displays being re-enabled after DPMS.
211 * This needs to be sorted out and fixed properly.
216 dev_info(adev->dev, "Using BACO for runtime pm\n");
219 /* Call ACPI methods: require modeset init
220 * but failure is not fatal
223 acpi_status = amdgpu_acpi_init(adev);
225 dev_dbg(dev->dev, "Error during ACPI methods call\n");
228 /* only need to skip on ATPX */
229 if (amdgpu_device_supports_px(dev))
230 dev_pm_set_driver_flags(dev->dev, DPM_FLAG_NO_DIRECT_COMPLETE);
231 /* we want direct complete for BOCO */
232 if (amdgpu_device_supports_boco(dev))
233 dev_pm_set_driver_flags(dev->dev, DPM_FLAG_SMART_PREPARE |
234 DPM_FLAG_SMART_SUSPEND |
235 DPM_FLAG_MAY_SKIP_RESUME);
236 pm_runtime_use_autosuspend(dev->dev);
237 pm_runtime_set_autosuspend_delay(dev->dev, 5000);
239 pm_runtime_allow(dev->dev);
241 pm_runtime_mark_last_busy(dev->dev);
242 pm_runtime_put_autosuspend(dev->dev);
245 * For runpm implemented via BACO, PMFW will handle the
246 * timing for BACO in and out:
247 * - put ASIC into BACO state only when both video and
248 * audio functions are in D3 state.
249 * - pull ASIC out of BACO state when either video or
250 * audio function is in D0 state.
251 * Also, at startup, PMFW assumes both functions are in
254 * So if snd driver was loaded prior to amdgpu driver
255 * and audio function was put into D3 state, there will
256 * be no PMFW-aware D-state transition(D0->D3) on runpm
257 * suspend. Thus the BACO will be not correctly kicked in.
259 * Via amdgpu_get_audio_func(), the audio dev is put
260 * into D0 state. Then there will be a PMFW-aware D-state
261 * transition(D0->D3) on runpm suspend.
263 if (amdgpu_device_supports_baco(dev) &&
264 !(adev->flags & AMD_IS_APU) &&
265 (adev->asic_type >= CHIP_NAVI10))
266 amdgpu_get_audio_func(adev);
269 if (amdgpu_acpi_smart_shift_update(dev, AMDGPU_SS_DRV_LOAD))
270 DRM_WARN("smart shift update failed\n");
274 /* balance pm_runtime_get_sync in amdgpu_driver_unload_kms */
275 if (adev->rmmio && adev->runpm)
276 pm_runtime_put_noidle(dev->dev);
277 amdgpu_driver_unload_kms(dev);
283 static int amdgpu_firmware_info(struct drm_amdgpu_info_firmware *fw_info,
284 struct drm_amdgpu_query_fw *query_fw,
285 struct amdgpu_device *adev)
287 switch (query_fw->fw_type) {
288 case AMDGPU_INFO_FW_VCE:
289 fw_info->ver = adev->vce.fw_version;
290 fw_info->feature = adev->vce.fb_version;
292 case AMDGPU_INFO_FW_UVD:
293 fw_info->ver = adev->uvd.fw_version;
294 fw_info->feature = 0;
296 case AMDGPU_INFO_FW_VCN:
297 fw_info->ver = adev->vcn.fw_version;
298 fw_info->feature = 0;
300 case AMDGPU_INFO_FW_GMC:
301 fw_info->ver = adev->gmc.fw_version;
302 fw_info->feature = 0;
304 case AMDGPU_INFO_FW_GFX_ME:
305 fw_info->ver = adev->gfx.me_fw_version;
306 fw_info->feature = adev->gfx.me_feature_version;
308 case AMDGPU_INFO_FW_GFX_PFP:
309 fw_info->ver = adev->gfx.pfp_fw_version;
310 fw_info->feature = adev->gfx.pfp_feature_version;
312 case AMDGPU_INFO_FW_GFX_CE:
313 fw_info->ver = adev->gfx.ce_fw_version;
314 fw_info->feature = adev->gfx.ce_feature_version;
316 case AMDGPU_INFO_FW_GFX_RLC:
317 fw_info->ver = adev->gfx.rlc_fw_version;
318 fw_info->feature = adev->gfx.rlc_feature_version;
320 case AMDGPU_INFO_FW_GFX_RLC_RESTORE_LIST_CNTL:
321 fw_info->ver = adev->gfx.rlc_srlc_fw_version;
322 fw_info->feature = adev->gfx.rlc_srlc_feature_version;
324 case AMDGPU_INFO_FW_GFX_RLC_RESTORE_LIST_GPM_MEM:
325 fw_info->ver = adev->gfx.rlc_srlg_fw_version;
326 fw_info->feature = adev->gfx.rlc_srlg_feature_version;
328 case AMDGPU_INFO_FW_GFX_RLC_RESTORE_LIST_SRM_MEM:
329 fw_info->ver = adev->gfx.rlc_srls_fw_version;
330 fw_info->feature = adev->gfx.rlc_srls_feature_version;
332 case AMDGPU_INFO_FW_GFX_MEC:
333 if (query_fw->index == 0) {
334 fw_info->ver = adev->gfx.mec_fw_version;
335 fw_info->feature = adev->gfx.mec_feature_version;
336 } else if (query_fw->index == 1) {
337 fw_info->ver = adev->gfx.mec2_fw_version;
338 fw_info->feature = adev->gfx.mec2_feature_version;
342 case AMDGPU_INFO_FW_SMC:
343 fw_info->ver = adev->pm.fw_version;
344 fw_info->feature = 0;
346 case AMDGPU_INFO_FW_TA:
347 switch (query_fw->index) {
348 case TA_FW_TYPE_PSP_XGMI:
349 fw_info->ver = adev->psp.xgmi_context.context.bin_desc.fw_version;
350 fw_info->feature = adev->psp.xgmi_context.context
351 .bin_desc.feature_version;
353 case TA_FW_TYPE_PSP_RAS:
354 fw_info->ver = adev->psp.ras_context.context.bin_desc.fw_version;
355 fw_info->feature = adev->psp.ras_context.context
356 .bin_desc.feature_version;
358 case TA_FW_TYPE_PSP_HDCP:
359 fw_info->ver = adev->psp.hdcp_context.context.bin_desc.fw_version;
360 fw_info->feature = adev->psp.hdcp_context.context
361 .bin_desc.feature_version;
363 case TA_FW_TYPE_PSP_DTM:
364 fw_info->ver = adev->psp.dtm_context.context.bin_desc.fw_version;
365 fw_info->feature = adev->psp.dtm_context.context
366 .bin_desc.feature_version;
368 case TA_FW_TYPE_PSP_RAP:
369 fw_info->ver = adev->psp.rap_context.context.bin_desc.fw_version;
370 fw_info->feature = adev->psp.rap_context.context
371 .bin_desc.feature_version;
373 case TA_FW_TYPE_PSP_SECUREDISPLAY:
374 fw_info->ver = adev->psp.securedisplay_context.context.bin_desc.fw_version;
376 adev->psp.securedisplay_context.context.bin_desc
383 case AMDGPU_INFO_FW_SDMA:
384 if (query_fw->index >= adev->sdma.num_instances)
386 fw_info->ver = adev->sdma.instance[query_fw->index].fw_version;
387 fw_info->feature = adev->sdma.instance[query_fw->index].feature_version;
389 case AMDGPU_INFO_FW_SOS:
390 fw_info->ver = adev->psp.sos.fw_version;
391 fw_info->feature = adev->psp.sos.feature_version;
393 case AMDGPU_INFO_FW_ASD:
394 fw_info->ver = adev->psp.asd_context.bin_desc.fw_version;
395 fw_info->feature = adev->psp.asd_context.bin_desc.feature_version;
397 case AMDGPU_INFO_FW_DMCU:
398 fw_info->ver = adev->dm.dmcu_fw_version;
399 fw_info->feature = 0;
401 case AMDGPU_INFO_FW_DMCUB:
402 fw_info->ver = adev->dm.dmcub_fw_version;
403 fw_info->feature = 0;
405 case AMDGPU_INFO_FW_TOC:
406 fw_info->ver = adev->psp.toc.fw_version;
407 fw_info->feature = adev->psp.toc.feature_version;
415 static int amdgpu_hw_ip_info(struct amdgpu_device *adev,
416 struct drm_amdgpu_info *info,
417 struct drm_amdgpu_info_hw_ip *result)
419 uint32_t ib_start_alignment = 0;
420 uint32_t ib_size_alignment = 0;
421 enum amd_ip_block_type type;
422 unsigned int num_rings = 0;
425 if (info->query_hw_ip.ip_instance >= AMDGPU_HW_IP_INSTANCE_MAX_COUNT)
428 switch (info->query_hw_ip.type) {
429 case AMDGPU_HW_IP_GFX:
430 type = AMD_IP_BLOCK_TYPE_GFX;
431 for (i = 0; i < adev->gfx.num_gfx_rings; i++)
432 if (adev->gfx.gfx_ring[i].sched.ready)
434 ib_start_alignment = 32;
435 ib_size_alignment = 32;
437 case AMDGPU_HW_IP_COMPUTE:
438 type = AMD_IP_BLOCK_TYPE_GFX;
439 for (i = 0; i < adev->gfx.num_compute_rings; i++)
440 if (adev->gfx.compute_ring[i].sched.ready)
442 ib_start_alignment = 32;
443 ib_size_alignment = 32;
445 case AMDGPU_HW_IP_DMA:
446 type = AMD_IP_BLOCK_TYPE_SDMA;
447 for (i = 0; i < adev->sdma.num_instances; i++)
448 if (adev->sdma.instance[i].ring.sched.ready)
450 ib_start_alignment = 256;
451 ib_size_alignment = 4;
453 case AMDGPU_HW_IP_UVD:
454 type = AMD_IP_BLOCK_TYPE_UVD;
455 for (i = 0; i < adev->uvd.num_uvd_inst; i++) {
456 if (adev->uvd.harvest_config & (1 << i))
459 if (adev->uvd.inst[i].ring.sched.ready)
462 ib_start_alignment = 64;
463 ib_size_alignment = 64;
465 case AMDGPU_HW_IP_VCE:
466 type = AMD_IP_BLOCK_TYPE_VCE;
467 for (i = 0; i < adev->vce.num_rings; i++)
468 if (adev->vce.ring[i].sched.ready)
470 ib_start_alignment = 4;
471 ib_size_alignment = 1;
473 case AMDGPU_HW_IP_UVD_ENC:
474 type = AMD_IP_BLOCK_TYPE_UVD;
475 for (i = 0; i < adev->uvd.num_uvd_inst; i++) {
476 if (adev->uvd.harvest_config & (1 << i))
479 for (j = 0; j < adev->uvd.num_enc_rings; j++)
480 if (adev->uvd.inst[i].ring_enc[j].sched.ready)
483 ib_start_alignment = 64;
484 ib_size_alignment = 64;
486 case AMDGPU_HW_IP_VCN_DEC:
487 type = AMD_IP_BLOCK_TYPE_VCN;
488 for (i = 0; i < adev->vcn.num_vcn_inst; i++) {
489 if (adev->uvd.harvest_config & (1 << i))
492 if (adev->vcn.inst[i].ring_dec.sched.ready)
495 ib_start_alignment = 16;
496 ib_size_alignment = 16;
498 case AMDGPU_HW_IP_VCN_ENC:
499 type = AMD_IP_BLOCK_TYPE_VCN;
500 for (i = 0; i < adev->vcn.num_vcn_inst; i++) {
501 if (adev->uvd.harvest_config & (1 << i))
504 for (j = 0; j < adev->vcn.num_enc_rings; j++)
505 if (adev->vcn.inst[i].ring_enc[j].sched.ready)
508 ib_start_alignment = 64;
509 ib_size_alignment = 1;
511 case AMDGPU_HW_IP_VCN_JPEG:
512 type = (amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_JPEG)) ?
513 AMD_IP_BLOCK_TYPE_JPEG : AMD_IP_BLOCK_TYPE_VCN;
515 for (i = 0; i < adev->jpeg.num_jpeg_inst; i++) {
516 if (adev->jpeg.harvest_config & (1 << i))
519 if (adev->jpeg.inst[i].ring_dec.sched.ready)
522 ib_start_alignment = 16;
523 ib_size_alignment = 16;
529 for (i = 0; i < adev->num_ip_blocks; i++)
530 if (adev->ip_blocks[i].version->type == type &&
531 adev->ip_blocks[i].status.valid)
534 if (i == adev->num_ip_blocks)
537 num_rings = min(amdgpu_ctx_num_entities[info->query_hw_ip.type],
540 result->hw_ip_version_major = adev->ip_blocks[i].version->major;
541 result->hw_ip_version_minor = adev->ip_blocks[i].version->minor;
542 result->capabilities_flags = 0;
543 result->available_rings = (1 << num_rings) - 1;
544 result->ib_start_alignment = ib_start_alignment;
545 result->ib_size_alignment = ib_size_alignment;
550 * Userspace get information ioctl
553 * amdgpu_info_ioctl - answer a device specific request.
555 * @dev: drm device pointer
556 * @data: request object
559 * This function is used to pass device specific parameters to the userspace
560 * drivers. Examples include: pci device id, pipeline parms, tiling params,
562 * Returns 0 on success, -EINVAL on failure.
564 int amdgpu_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
566 struct amdgpu_device *adev = drm_to_adev(dev);
567 struct drm_amdgpu_info *info = data;
568 struct amdgpu_mode_info *minfo = &adev->mode_info;
569 void __user *out = (void __user *)(uintptr_t)info->return_pointer;
570 uint32_t size = info->return_size;
571 struct drm_crtc *crtc;
575 int ui32_size = sizeof(ui32);
577 if (!info->return_size || !info->return_pointer)
580 switch (info->query) {
581 case AMDGPU_INFO_ACCEL_WORKING:
582 ui32 = adev->accel_working;
583 return copy_to_user(out, &ui32, min(size, 4u)) ? -EFAULT : 0;
584 case AMDGPU_INFO_CRTC_FROM_ID:
585 for (i = 0, found = 0; i < adev->mode_info.num_crtc; i++) {
586 crtc = (struct drm_crtc *)minfo->crtcs[i];
587 if (crtc && crtc->base.id == info->mode_crtc.id) {
588 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
589 ui32 = amdgpu_crtc->crtc_id;
595 DRM_DEBUG_KMS("unknown crtc id %d\n", info->mode_crtc.id);
598 return copy_to_user(out, &ui32, min(size, 4u)) ? -EFAULT : 0;
599 case AMDGPU_INFO_HW_IP_INFO: {
600 struct drm_amdgpu_info_hw_ip ip = {};
603 ret = amdgpu_hw_ip_info(adev, info, &ip);
607 ret = copy_to_user(out, &ip, min((size_t)size, sizeof(ip)));
608 return ret ? -EFAULT : 0;
610 case AMDGPU_INFO_HW_IP_COUNT: {
611 enum amd_ip_block_type type;
614 switch (info->query_hw_ip.type) {
615 case AMDGPU_HW_IP_GFX:
616 type = AMD_IP_BLOCK_TYPE_GFX;
618 case AMDGPU_HW_IP_COMPUTE:
619 type = AMD_IP_BLOCK_TYPE_GFX;
621 case AMDGPU_HW_IP_DMA:
622 type = AMD_IP_BLOCK_TYPE_SDMA;
624 case AMDGPU_HW_IP_UVD:
625 type = AMD_IP_BLOCK_TYPE_UVD;
627 case AMDGPU_HW_IP_VCE:
628 type = AMD_IP_BLOCK_TYPE_VCE;
630 case AMDGPU_HW_IP_UVD_ENC:
631 type = AMD_IP_BLOCK_TYPE_UVD;
633 case AMDGPU_HW_IP_VCN_DEC:
634 case AMDGPU_HW_IP_VCN_ENC:
635 type = AMD_IP_BLOCK_TYPE_VCN;
637 case AMDGPU_HW_IP_VCN_JPEG:
638 type = (amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_JPEG)) ?
639 AMD_IP_BLOCK_TYPE_JPEG : AMD_IP_BLOCK_TYPE_VCN;
645 for (i = 0; i < adev->num_ip_blocks; i++)
646 if (adev->ip_blocks[i].version->type == type &&
647 adev->ip_blocks[i].status.valid &&
648 count < AMDGPU_HW_IP_INSTANCE_MAX_COUNT)
651 return copy_to_user(out, &count, min(size, 4u)) ? -EFAULT : 0;
653 case AMDGPU_INFO_TIMESTAMP:
654 ui64 = amdgpu_gfx_get_gpu_clock_counter(adev);
655 return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
656 case AMDGPU_INFO_FW_VERSION: {
657 struct drm_amdgpu_info_firmware fw_info;
660 /* We only support one instance of each IP block right now. */
661 if (info->query_fw.ip_instance != 0)
664 ret = amdgpu_firmware_info(&fw_info, &info->query_fw, adev);
668 return copy_to_user(out, &fw_info,
669 min((size_t)size, sizeof(fw_info))) ? -EFAULT : 0;
671 case AMDGPU_INFO_NUM_BYTES_MOVED:
672 ui64 = atomic64_read(&adev->num_bytes_moved);
673 return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
674 case AMDGPU_INFO_NUM_EVICTIONS:
675 ui64 = atomic64_read(&adev->num_evictions);
676 return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
677 case AMDGPU_INFO_NUM_VRAM_CPU_PAGE_FAULTS:
678 ui64 = atomic64_read(&adev->num_vram_cpu_page_faults);
679 return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
680 case AMDGPU_INFO_VRAM_USAGE:
681 ui64 = amdgpu_vram_mgr_usage(&adev->mman.vram_mgr);
682 return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
683 case AMDGPU_INFO_VIS_VRAM_USAGE:
684 ui64 = amdgpu_vram_mgr_vis_usage(&adev->mman.vram_mgr);
685 return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
686 case AMDGPU_INFO_GTT_USAGE:
687 ui64 = amdgpu_gtt_mgr_usage(&adev->mman.gtt_mgr);
688 return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
689 case AMDGPU_INFO_GDS_CONFIG: {
690 struct drm_amdgpu_info_gds gds_info;
692 memset(&gds_info, 0, sizeof(gds_info));
693 gds_info.compute_partition_size = adev->gds.gds_size;
694 gds_info.gds_total_size = adev->gds.gds_size;
695 gds_info.gws_per_compute_partition = adev->gds.gws_size;
696 gds_info.oa_per_compute_partition = adev->gds.oa_size;
697 return copy_to_user(out, &gds_info,
698 min((size_t)size, sizeof(gds_info))) ? -EFAULT : 0;
700 case AMDGPU_INFO_VRAM_GTT: {
701 struct drm_amdgpu_info_vram_gtt vram_gtt;
703 vram_gtt.vram_size = adev->gmc.real_vram_size -
704 atomic64_read(&adev->vram_pin_size) -
705 AMDGPU_VM_RESERVED_VRAM;
706 vram_gtt.vram_cpu_accessible_size =
707 min(adev->gmc.visible_vram_size -
708 atomic64_read(&adev->visible_pin_size),
710 vram_gtt.gtt_size = ttm_manager_type(&adev->mman.bdev, TTM_PL_TT)->size;
711 vram_gtt.gtt_size *= PAGE_SIZE;
712 vram_gtt.gtt_size -= atomic64_read(&adev->gart_pin_size);
713 return copy_to_user(out, &vram_gtt,
714 min((size_t)size, sizeof(vram_gtt))) ? -EFAULT : 0;
716 case AMDGPU_INFO_MEMORY: {
717 struct drm_amdgpu_memory_info mem;
718 struct ttm_resource_manager *gtt_man =
719 ttm_manager_type(&adev->mman.bdev, TTM_PL_TT);
720 memset(&mem, 0, sizeof(mem));
721 mem.vram.total_heap_size = adev->gmc.real_vram_size;
722 mem.vram.usable_heap_size = adev->gmc.real_vram_size -
723 atomic64_read(&adev->vram_pin_size) -
724 AMDGPU_VM_RESERVED_VRAM;
725 mem.vram.heap_usage =
726 amdgpu_vram_mgr_usage(&adev->mman.vram_mgr);
727 mem.vram.max_allocation = mem.vram.usable_heap_size * 3 / 4;
729 mem.cpu_accessible_vram.total_heap_size =
730 adev->gmc.visible_vram_size;
731 mem.cpu_accessible_vram.usable_heap_size =
732 min(adev->gmc.visible_vram_size -
733 atomic64_read(&adev->visible_pin_size),
734 mem.vram.usable_heap_size);
735 mem.cpu_accessible_vram.heap_usage =
736 amdgpu_vram_mgr_vis_usage(&adev->mman.vram_mgr);
737 mem.cpu_accessible_vram.max_allocation =
738 mem.cpu_accessible_vram.usable_heap_size * 3 / 4;
740 mem.gtt.total_heap_size = gtt_man->size;
741 mem.gtt.total_heap_size *= PAGE_SIZE;
742 mem.gtt.usable_heap_size = mem.gtt.total_heap_size -
743 atomic64_read(&adev->gart_pin_size);
745 amdgpu_gtt_mgr_usage(&adev->mman.gtt_mgr);
746 mem.gtt.max_allocation = mem.gtt.usable_heap_size * 3 / 4;
748 return copy_to_user(out, &mem,
749 min((size_t)size, sizeof(mem)))
752 case AMDGPU_INFO_READ_MMR_REG: {
753 unsigned n, alloc_size;
755 unsigned se_num = (info->read_mmr_reg.instance >>
756 AMDGPU_INFO_MMR_SE_INDEX_SHIFT) &
757 AMDGPU_INFO_MMR_SE_INDEX_MASK;
758 unsigned sh_num = (info->read_mmr_reg.instance >>
759 AMDGPU_INFO_MMR_SH_INDEX_SHIFT) &
760 AMDGPU_INFO_MMR_SH_INDEX_MASK;
762 /* set full masks if the userspace set all bits
763 * in the bitfields */
764 if (se_num == AMDGPU_INFO_MMR_SE_INDEX_MASK)
766 else if (se_num >= AMDGPU_GFX_MAX_SE)
768 if (sh_num == AMDGPU_INFO_MMR_SH_INDEX_MASK)
770 else if (sh_num >= AMDGPU_GFX_MAX_SH_PER_SE)
773 if (info->read_mmr_reg.count > 128)
776 regs = kmalloc_array(info->read_mmr_reg.count, sizeof(*regs), GFP_KERNEL);
779 alloc_size = info->read_mmr_reg.count * sizeof(*regs);
781 amdgpu_gfx_off_ctrl(adev, false);
782 for (i = 0; i < info->read_mmr_reg.count; i++) {
783 if (amdgpu_asic_read_register(adev, se_num, sh_num,
784 info->read_mmr_reg.dword_offset + i,
786 DRM_DEBUG_KMS("unallowed offset %#x\n",
787 info->read_mmr_reg.dword_offset + i);
789 amdgpu_gfx_off_ctrl(adev, true);
793 amdgpu_gfx_off_ctrl(adev, true);
794 n = copy_to_user(out, regs, min(size, alloc_size));
796 return n ? -EFAULT : 0;
798 case AMDGPU_INFO_DEV_INFO: {
799 struct drm_amdgpu_info_device *dev_info;
803 dev_info = kzalloc(sizeof(*dev_info), GFP_KERNEL);
807 dev_info->device_id = adev->pdev->device;
808 dev_info->chip_rev = adev->rev_id;
809 dev_info->external_rev = adev->external_rev_id;
810 dev_info->pci_rev = adev->pdev->revision;
811 dev_info->family = adev->family;
812 dev_info->num_shader_engines = adev->gfx.config.max_shader_engines;
813 dev_info->num_shader_arrays_per_engine = adev->gfx.config.max_sh_per_se;
814 /* return all clocks in KHz */
815 dev_info->gpu_counter_freq = amdgpu_asic_get_xclk(adev) * 10;
816 if (adev->pm.dpm_enabled) {
817 dev_info->max_engine_clock = amdgpu_dpm_get_sclk(adev, false) * 10;
818 dev_info->max_memory_clock = amdgpu_dpm_get_mclk(adev, false) * 10;
820 dev_info->max_engine_clock = adev->clock.default_sclk * 10;
821 dev_info->max_memory_clock = adev->clock.default_mclk * 10;
823 dev_info->enabled_rb_pipes_mask = adev->gfx.config.backend_enable_mask;
824 dev_info->num_rb_pipes = adev->gfx.config.max_backends_per_se *
825 adev->gfx.config.max_shader_engines;
826 dev_info->num_hw_gfx_contexts = adev->gfx.config.max_hw_contexts;
828 dev_info->ids_flags = 0;
829 if (adev->flags & AMD_IS_APU)
830 dev_info->ids_flags |= AMDGPU_IDS_FLAGS_FUSION;
831 if (amdgpu_mcbp || amdgpu_sriov_vf(adev))
832 dev_info->ids_flags |= AMDGPU_IDS_FLAGS_PREEMPTION;
833 if (amdgpu_is_tmz(adev))
834 dev_info->ids_flags |= AMDGPU_IDS_FLAGS_TMZ;
836 vm_size = adev->vm_manager.max_pfn * AMDGPU_GPU_PAGE_SIZE;
837 vm_size -= AMDGPU_VA_RESERVED_SIZE;
839 /* Older VCE FW versions are buggy and can handle only 40bits */
840 if (adev->vce.fw_version &&
841 adev->vce.fw_version < AMDGPU_VCE_FW_53_45)
842 vm_size = min(vm_size, 1ULL << 40);
844 dev_info->virtual_address_offset = AMDGPU_VA_RESERVED_SIZE;
845 dev_info->virtual_address_max =
846 min(vm_size, AMDGPU_GMC_HOLE_START);
848 if (vm_size > AMDGPU_GMC_HOLE_START) {
849 dev_info->high_va_offset = AMDGPU_GMC_HOLE_END;
850 dev_info->high_va_max = AMDGPU_GMC_HOLE_END | vm_size;
852 dev_info->virtual_address_alignment = max_t(u32, PAGE_SIZE, AMDGPU_GPU_PAGE_SIZE);
853 dev_info->pte_fragment_size = (1 << adev->vm_manager.fragment_size) * AMDGPU_GPU_PAGE_SIZE;
854 dev_info->gart_page_size = max_t(u32, PAGE_SIZE, AMDGPU_GPU_PAGE_SIZE);
855 dev_info->cu_active_number = adev->gfx.cu_info.number;
856 dev_info->cu_ao_mask = adev->gfx.cu_info.ao_cu_mask;
857 dev_info->ce_ram_size = adev->gfx.ce_ram_size;
858 memcpy(&dev_info->cu_ao_bitmap[0], &adev->gfx.cu_info.ao_cu_bitmap[0],
859 sizeof(adev->gfx.cu_info.ao_cu_bitmap));
860 memcpy(&dev_info->cu_bitmap[0], &adev->gfx.cu_info.bitmap[0],
861 sizeof(adev->gfx.cu_info.bitmap));
862 dev_info->vram_type = adev->gmc.vram_type;
863 dev_info->vram_bit_width = adev->gmc.vram_width;
864 dev_info->vce_harvest_config = adev->vce.harvest_config;
865 dev_info->gc_double_offchip_lds_buf =
866 adev->gfx.config.double_offchip_lds_buf;
867 dev_info->wave_front_size = adev->gfx.cu_info.wave_front_size;
868 dev_info->num_shader_visible_vgprs = adev->gfx.config.max_gprs;
869 dev_info->num_cu_per_sh = adev->gfx.config.max_cu_per_sh;
870 dev_info->num_tcc_blocks = adev->gfx.config.max_texture_channel_caches;
871 dev_info->gs_vgt_table_depth = adev->gfx.config.gs_vgt_table_depth;
872 dev_info->gs_prim_buffer_depth = adev->gfx.config.gs_prim_buffer_depth;
873 dev_info->max_gs_waves_per_vgt = adev->gfx.config.max_gs_threads;
875 if (adev->family >= AMDGPU_FAMILY_NV)
876 dev_info->pa_sc_tile_steering_override =
877 adev->gfx.config.pa_sc_tile_steering_override;
879 dev_info->tcc_disabled_mask = adev->gfx.config.tcc_disabled_mask;
881 ret = copy_to_user(out, dev_info,
882 min((size_t)size, sizeof(*dev_info))) ? -EFAULT : 0;
886 case AMDGPU_INFO_VCE_CLOCK_TABLE: {
888 struct drm_amdgpu_info_vce_clock_table vce_clk_table = {};
889 struct amd_vce_state *vce_state;
891 for (i = 0; i < AMDGPU_VCE_CLOCK_TABLE_ENTRIES; i++) {
892 vce_state = amdgpu_dpm_get_vce_clock_state(adev, i);
894 vce_clk_table.entries[i].sclk = vce_state->sclk;
895 vce_clk_table.entries[i].mclk = vce_state->mclk;
896 vce_clk_table.entries[i].eclk = vce_state->evclk;
897 vce_clk_table.num_valid_entries++;
901 return copy_to_user(out, &vce_clk_table,
902 min((size_t)size, sizeof(vce_clk_table))) ? -EFAULT : 0;
904 case AMDGPU_INFO_VBIOS: {
905 uint32_t bios_size = adev->bios_size;
907 switch (info->vbios_info.type) {
908 case AMDGPU_INFO_VBIOS_SIZE:
909 return copy_to_user(out, &bios_size,
910 min((size_t)size, sizeof(bios_size)))
912 case AMDGPU_INFO_VBIOS_IMAGE: {
914 uint32_t bios_offset = info->vbios_info.offset;
916 if (bios_offset >= bios_size)
919 bios = adev->bios + bios_offset;
920 return copy_to_user(out, bios,
921 min((size_t)size, (size_t)(bios_size - bios_offset)))
924 case AMDGPU_INFO_VBIOS_INFO: {
925 struct drm_amdgpu_info_vbios vbios_info = {};
926 struct atom_context *atom_context;
928 atom_context = adev->mode_info.atom_context;
929 memcpy(vbios_info.name, atom_context->name, sizeof(atom_context->name));
930 memcpy(vbios_info.vbios_pn, atom_context->vbios_pn, sizeof(atom_context->vbios_pn));
931 vbios_info.version = atom_context->version;
932 memcpy(vbios_info.vbios_ver_str, atom_context->vbios_ver_str,
933 sizeof(atom_context->vbios_ver_str));
934 memcpy(vbios_info.date, atom_context->date, sizeof(atom_context->date));
936 return copy_to_user(out, &vbios_info,
937 min((size_t)size, sizeof(vbios_info))) ? -EFAULT : 0;
940 DRM_DEBUG_KMS("Invalid request %d\n",
941 info->vbios_info.type);
945 case AMDGPU_INFO_NUM_HANDLES: {
946 struct drm_amdgpu_info_num_handles handle;
948 switch (info->query_hw_ip.type) {
949 case AMDGPU_HW_IP_UVD:
950 /* Starting Polaris, we support unlimited UVD handles */
951 if (adev->asic_type < CHIP_POLARIS10) {
952 handle.uvd_max_handles = adev->uvd.max_handles;
953 handle.uvd_used_handles = amdgpu_uvd_used_handles(adev);
955 return copy_to_user(out, &handle,
956 min((size_t)size, sizeof(handle))) ? -EFAULT : 0;
966 case AMDGPU_INFO_SENSOR: {
967 if (!adev->pm.dpm_enabled)
970 switch (info->sensor_info.type) {
971 case AMDGPU_INFO_SENSOR_GFX_SCLK:
972 /* get sclk in Mhz */
973 if (amdgpu_dpm_read_sensor(adev,
974 AMDGPU_PP_SENSOR_GFX_SCLK,
975 (void *)&ui32, &ui32_size)) {
980 case AMDGPU_INFO_SENSOR_GFX_MCLK:
981 /* get mclk in Mhz */
982 if (amdgpu_dpm_read_sensor(adev,
983 AMDGPU_PP_SENSOR_GFX_MCLK,
984 (void *)&ui32, &ui32_size)) {
989 case AMDGPU_INFO_SENSOR_GPU_TEMP:
990 /* get temperature in millidegrees C */
991 if (amdgpu_dpm_read_sensor(adev,
992 AMDGPU_PP_SENSOR_GPU_TEMP,
993 (void *)&ui32, &ui32_size)) {
997 case AMDGPU_INFO_SENSOR_GPU_LOAD:
999 if (amdgpu_dpm_read_sensor(adev,
1000 AMDGPU_PP_SENSOR_GPU_LOAD,
1001 (void *)&ui32, &ui32_size)) {
1005 case AMDGPU_INFO_SENSOR_GPU_AVG_POWER:
1006 /* get average GPU power */
1007 if (amdgpu_dpm_read_sensor(adev,
1008 AMDGPU_PP_SENSOR_GPU_POWER,
1009 (void *)&ui32, &ui32_size)) {
1014 case AMDGPU_INFO_SENSOR_VDDNB:
1015 /* get VDDNB in millivolts */
1016 if (amdgpu_dpm_read_sensor(adev,
1017 AMDGPU_PP_SENSOR_VDDNB,
1018 (void *)&ui32, &ui32_size)) {
1022 case AMDGPU_INFO_SENSOR_VDDGFX:
1023 /* get VDDGFX in millivolts */
1024 if (amdgpu_dpm_read_sensor(adev,
1025 AMDGPU_PP_SENSOR_VDDGFX,
1026 (void *)&ui32, &ui32_size)) {
1030 case AMDGPU_INFO_SENSOR_STABLE_PSTATE_GFX_SCLK:
1031 /* get stable pstate sclk in Mhz */
1032 if (amdgpu_dpm_read_sensor(adev,
1033 AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK,
1034 (void *)&ui32, &ui32_size)) {
1039 case AMDGPU_INFO_SENSOR_STABLE_PSTATE_GFX_MCLK:
1040 /* get stable pstate mclk in Mhz */
1041 if (amdgpu_dpm_read_sensor(adev,
1042 AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK,
1043 (void *)&ui32, &ui32_size)) {
1049 DRM_DEBUG_KMS("Invalid request %d\n",
1050 info->sensor_info.type);
1053 return copy_to_user(out, &ui32, min(size, 4u)) ? -EFAULT : 0;
1055 case AMDGPU_INFO_VRAM_LOST_COUNTER:
1056 ui32 = atomic_read(&adev->vram_lost_counter);
1057 return copy_to_user(out, &ui32, min(size, 4u)) ? -EFAULT : 0;
1058 case AMDGPU_INFO_RAS_ENABLED_FEATURES: {
1059 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
1064 ras_mask = (uint64_t)adev->ras_enabled << 32 | ras->features;
1066 return copy_to_user(out, &ras_mask,
1067 min_t(u64, size, sizeof(ras_mask))) ?
1070 case AMDGPU_INFO_VIDEO_CAPS: {
1071 const struct amdgpu_video_codecs *codecs;
1072 struct drm_amdgpu_info_video_caps *caps;
1075 switch (info->video_cap.type) {
1076 case AMDGPU_INFO_VIDEO_CAPS_DECODE:
1077 r = amdgpu_asic_query_video_codecs(adev, false, &codecs);
1081 case AMDGPU_INFO_VIDEO_CAPS_ENCODE:
1082 r = amdgpu_asic_query_video_codecs(adev, true, &codecs);
1087 DRM_DEBUG_KMS("Invalid request %d\n",
1088 info->video_cap.type);
1092 caps = kzalloc(sizeof(*caps), GFP_KERNEL);
1096 for (i = 0; i < codecs->codec_count; i++) {
1097 int idx = codecs->codec_array[i].codec_type;
1100 case AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_MPEG2:
1101 case AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_MPEG4:
1102 case AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_VC1:
1103 case AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_MPEG4_AVC:
1104 case AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_HEVC:
1105 case AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_JPEG:
1106 case AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_VP9:
1107 case AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_AV1:
1108 caps->codec_info[idx].valid = 1;
1109 caps->codec_info[idx].max_width =
1110 codecs->codec_array[i].max_width;
1111 caps->codec_info[idx].max_height =
1112 codecs->codec_array[i].max_height;
1113 caps->codec_info[idx].max_pixels_per_frame =
1114 codecs->codec_array[i].max_pixels_per_frame;
1115 caps->codec_info[idx].max_level =
1116 codecs->codec_array[i].max_level;
1122 r = copy_to_user(out, caps,
1123 min((size_t)size, sizeof(*caps))) ? -EFAULT : 0;
1128 DRM_DEBUG_KMS("Invalid request %d\n", info->query);
1136 * Outdated mess for old drm with Xorg being in charge (void function now).
1139 * amdgpu_driver_lastclose_kms - drm callback for last close
1141 * @dev: drm dev pointer
1143 * Switch vga_switcheroo state after last close (all asics).
1145 void amdgpu_driver_lastclose_kms(struct drm_device *dev)
1147 drm_fb_helper_lastclose(dev);
1148 vga_switcheroo_process_delayed_switch();
1152 * amdgpu_driver_open_kms - drm callback for open
1154 * @dev: drm dev pointer
1155 * @file_priv: drm file
1157 * On device open, init vm on cayman+ (all asics).
1158 * Returns 0 on success, error on failure.
1160 int amdgpu_driver_open_kms(struct drm_device *dev, struct drm_file *file_priv)
1162 struct amdgpu_device *adev = drm_to_adev(dev);
1163 struct amdgpu_fpriv *fpriv;
1166 /* Ensure IB tests are run on ring */
1167 flush_delayed_work(&adev->delayed_init_work);
1170 if (amdgpu_ras_intr_triggered()) {
1171 DRM_ERROR("RAS Intr triggered, device disabled!!");
1175 file_priv->driver_priv = NULL;
1177 r = pm_runtime_get_sync(dev->dev);
1181 fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL);
1182 if (unlikely(!fpriv)) {
1187 pasid = amdgpu_pasid_alloc(16);
1189 dev_warn(adev->dev, "No more PASIDs available!");
1193 r = amdgpu_vm_init(adev, &fpriv->vm);
1197 r = amdgpu_vm_set_pasid(adev, &fpriv->vm, pasid);
1201 fpriv->prt_va = amdgpu_vm_bo_add(adev, &fpriv->vm, NULL);
1202 if (!fpriv->prt_va) {
1207 if (amdgpu_mcbp || amdgpu_sriov_vf(adev)) {
1208 uint64_t csa_addr = amdgpu_csa_vaddr(adev) & AMDGPU_GMC_HOLE_MASK;
1210 r = amdgpu_map_static_csa(adev, &fpriv->vm, adev->virt.csa_obj,
1211 &fpriv->csa_va, csa_addr, AMDGPU_CSA_SIZE);
1216 mutex_init(&fpriv->bo_list_lock);
1217 idr_init(&fpriv->bo_list_handles);
1219 amdgpu_ctx_mgr_init(&fpriv->ctx_mgr);
1221 file_priv->driver_priv = fpriv;
1225 amdgpu_vm_fini(adev, &fpriv->vm);
1229 amdgpu_pasid_free(pasid);
1230 amdgpu_vm_set_pasid(adev, &fpriv->vm, 0);
1236 pm_runtime_mark_last_busy(dev->dev);
1238 pm_runtime_put_autosuspend(dev->dev);
1244 * amdgpu_driver_postclose_kms - drm callback for post close
1246 * @dev: drm dev pointer
1247 * @file_priv: drm file
1249 * On device post close, tear down vm on cayman+ (all asics).
1251 void amdgpu_driver_postclose_kms(struct drm_device *dev,
1252 struct drm_file *file_priv)
1254 struct amdgpu_device *adev = drm_to_adev(dev);
1255 struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
1256 struct amdgpu_bo_list *list;
1257 struct amdgpu_bo *pd;
1264 pm_runtime_get_sync(dev->dev);
1266 if (amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_UVD) != NULL)
1267 amdgpu_uvd_free_handles(adev, file_priv);
1268 if (amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_VCE) != NULL)
1269 amdgpu_vce_free_handles(adev, file_priv);
1271 amdgpu_vm_bo_rmv(adev, fpriv->prt_va);
1273 if (amdgpu_mcbp || amdgpu_sriov_vf(adev)) {
1274 /* TODO: how to handle reserve failure */
1275 BUG_ON(amdgpu_bo_reserve(adev->virt.csa_obj, true));
1276 amdgpu_vm_bo_rmv(adev, fpriv->csa_va);
1277 fpriv->csa_va = NULL;
1278 amdgpu_bo_unreserve(adev->virt.csa_obj);
1281 pasid = fpriv->vm.pasid;
1282 pd = amdgpu_bo_ref(fpriv->vm.root.bo);
1284 amdgpu_ctx_mgr_fini(&fpriv->ctx_mgr);
1285 amdgpu_vm_fini(adev, &fpriv->vm);
1288 amdgpu_pasid_free_delayed(pd->tbo.base.resv, pasid);
1289 amdgpu_bo_unref(&pd);
1291 idr_for_each_entry(&fpriv->bo_list_handles, list, handle)
1292 amdgpu_bo_list_put(list);
1294 idr_destroy(&fpriv->bo_list_handles);
1295 mutex_destroy(&fpriv->bo_list_lock);
1298 file_priv->driver_priv = NULL;
1300 pm_runtime_mark_last_busy(dev->dev);
1301 pm_runtime_put_autosuspend(dev->dev);
1305 void amdgpu_driver_release_kms(struct drm_device *dev)
1307 struct amdgpu_device *adev = drm_to_adev(dev);
1309 amdgpu_device_fini_sw(adev);
1310 pci_set_drvdata(adev->pdev, NULL);
1314 * VBlank related functions.
1317 * amdgpu_get_vblank_counter_kms - get frame count
1319 * @crtc: crtc to get the frame count from
1321 * Gets the frame count on the requested crtc (all asics).
1322 * Returns frame count on success, -EINVAL on failure.
1324 u32 amdgpu_get_vblank_counter_kms(struct drm_crtc *crtc)
1326 struct drm_device *dev = crtc->dev;
1327 unsigned int pipe = crtc->index;
1328 struct amdgpu_device *adev = drm_to_adev(dev);
1329 int vpos, hpos, stat;
1332 if (pipe >= adev->mode_info.num_crtc) {
1333 DRM_ERROR("Invalid crtc %u\n", pipe);
1337 /* The hw increments its frame counter at start of vsync, not at start
1338 * of vblank, as is required by DRM core vblank counter handling.
1339 * Cook the hw count here to make it appear to the caller as if it
1340 * incremented at start of vblank. We measure distance to start of
1341 * vblank in vpos. vpos therefore will be >= 0 between start of vblank
1342 * and start of vsync, so vpos >= 0 means to bump the hw frame counter
1343 * result by 1 to give the proper appearance to caller.
1345 if (adev->mode_info.crtcs[pipe]) {
1346 /* Repeat readout if needed to provide stable result if
1347 * we cross start of vsync during the queries.
1350 count = amdgpu_display_vblank_get_counter(adev, pipe);
1351 /* Ask amdgpu_display_get_crtc_scanoutpos to return
1352 * vpos as distance to start of vblank, instead of
1353 * regular vertical scanout pos.
1355 stat = amdgpu_display_get_crtc_scanoutpos(
1356 dev, pipe, GET_DISTANCE_TO_VBLANKSTART,
1357 &vpos, &hpos, NULL, NULL,
1358 &adev->mode_info.crtcs[pipe]->base.hwmode);
1359 } while (count != amdgpu_display_vblank_get_counter(adev, pipe));
1361 if (((stat & (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE)) !=
1362 (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE))) {
1363 DRM_DEBUG_VBL("Query failed! stat %d\n", stat);
1365 DRM_DEBUG_VBL("crtc %d: dist from vblank start %d\n",
1368 /* Bump counter if we are at >= leading edge of vblank,
1369 * but before vsync where vpos would turn negative and
1370 * the hw counter really increments.
1376 /* Fallback to use value as is. */
1377 count = amdgpu_display_vblank_get_counter(adev, pipe);
1378 DRM_DEBUG_VBL("NULL mode info! Returned count may be wrong.\n");
1385 * amdgpu_enable_vblank_kms - enable vblank interrupt
1387 * @crtc: crtc to enable vblank interrupt for
1389 * Enable the interrupt on the requested crtc (all asics).
1390 * Returns 0 on success, -EINVAL on failure.
1392 int amdgpu_enable_vblank_kms(struct drm_crtc *crtc)
1394 struct drm_device *dev = crtc->dev;
1395 unsigned int pipe = crtc->index;
1396 struct amdgpu_device *adev = drm_to_adev(dev);
1397 int idx = amdgpu_display_crtc_idx_to_irq_type(adev, pipe);
1399 return amdgpu_irq_get(adev, &adev->crtc_irq, idx);
1403 * amdgpu_disable_vblank_kms - disable vblank interrupt
1405 * @crtc: crtc to disable vblank interrupt for
1407 * Disable the interrupt on the requested crtc (all asics).
1409 void amdgpu_disable_vblank_kms(struct drm_crtc *crtc)
1411 struct drm_device *dev = crtc->dev;
1412 unsigned int pipe = crtc->index;
1413 struct amdgpu_device *adev = drm_to_adev(dev);
1414 int idx = amdgpu_display_crtc_idx_to_irq_type(adev, pipe);
1416 amdgpu_irq_put(adev, &adev->crtc_irq, idx);
1422 #if defined(CONFIG_DEBUG_FS)
1424 static int amdgpu_debugfs_firmware_info_show(struct seq_file *m, void *unused)
1426 struct amdgpu_device *adev = (struct amdgpu_device *)m->private;
1427 struct drm_amdgpu_info_firmware fw_info;
1428 struct drm_amdgpu_query_fw query_fw;
1429 struct atom_context *ctx = adev->mode_info.atom_context;
1430 uint8_t smu_minor, smu_debug;
1434 static const char *ta_fw_name[TA_FW_TYPE_MAX_INDEX] = {
1435 #define TA_FW_NAME(type) [TA_FW_TYPE_PSP_##type] = #type
1441 TA_FW_NAME(SECUREDISPLAY),
1446 query_fw.fw_type = AMDGPU_INFO_FW_VCE;
1447 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1450 seq_printf(m, "VCE feature version: %u, firmware version: 0x%08x\n",
1451 fw_info.feature, fw_info.ver);
1454 query_fw.fw_type = AMDGPU_INFO_FW_UVD;
1455 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1458 seq_printf(m, "UVD feature version: %u, firmware version: 0x%08x\n",
1459 fw_info.feature, fw_info.ver);
1462 query_fw.fw_type = AMDGPU_INFO_FW_GMC;
1463 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1466 seq_printf(m, "MC feature version: %u, firmware version: 0x%08x\n",
1467 fw_info.feature, fw_info.ver);
1470 query_fw.fw_type = AMDGPU_INFO_FW_GFX_ME;
1471 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1474 seq_printf(m, "ME feature version: %u, firmware version: 0x%08x\n",
1475 fw_info.feature, fw_info.ver);
1478 query_fw.fw_type = AMDGPU_INFO_FW_GFX_PFP;
1479 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1482 seq_printf(m, "PFP feature version: %u, firmware version: 0x%08x\n",
1483 fw_info.feature, fw_info.ver);
1486 query_fw.fw_type = AMDGPU_INFO_FW_GFX_CE;
1487 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1490 seq_printf(m, "CE feature version: %u, firmware version: 0x%08x\n",
1491 fw_info.feature, fw_info.ver);
1494 query_fw.fw_type = AMDGPU_INFO_FW_GFX_RLC;
1495 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1498 seq_printf(m, "RLC feature version: %u, firmware version: 0x%08x\n",
1499 fw_info.feature, fw_info.ver);
1501 /* RLC SAVE RESTORE LIST CNTL */
1502 query_fw.fw_type = AMDGPU_INFO_FW_GFX_RLC_RESTORE_LIST_CNTL;
1503 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1506 seq_printf(m, "RLC SRLC feature version: %u, firmware version: 0x%08x\n",
1507 fw_info.feature, fw_info.ver);
1509 /* RLC SAVE RESTORE LIST GPM MEM */
1510 query_fw.fw_type = AMDGPU_INFO_FW_GFX_RLC_RESTORE_LIST_GPM_MEM;
1511 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1514 seq_printf(m, "RLC SRLG feature version: %u, firmware version: 0x%08x\n",
1515 fw_info.feature, fw_info.ver);
1517 /* RLC SAVE RESTORE LIST SRM MEM */
1518 query_fw.fw_type = AMDGPU_INFO_FW_GFX_RLC_RESTORE_LIST_SRM_MEM;
1519 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1522 seq_printf(m, "RLC SRLS feature version: %u, firmware version: 0x%08x\n",
1523 fw_info.feature, fw_info.ver);
1526 query_fw.fw_type = AMDGPU_INFO_FW_GFX_MEC;
1528 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1531 seq_printf(m, "MEC feature version: %u, firmware version: 0x%08x\n",
1532 fw_info.feature, fw_info.ver);
1535 if (adev->gfx.mec2_fw) {
1537 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1540 seq_printf(m, "MEC2 feature version: %u, firmware version: 0x%08x\n",
1541 fw_info.feature, fw_info.ver);
1545 query_fw.fw_type = AMDGPU_INFO_FW_SOS;
1546 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1549 seq_printf(m, "SOS feature version: %u, firmware version: 0x%08x\n",
1550 fw_info.feature, fw_info.ver);
1554 query_fw.fw_type = AMDGPU_INFO_FW_ASD;
1555 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1558 seq_printf(m, "ASD feature version: %u, firmware version: 0x%08x\n",
1559 fw_info.feature, fw_info.ver);
1561 query_fw.fw_type = AMDGPU_INFO_FW_TA;
1562 for (i = TA_FW_TYPE_PSP_XGMI; i < TA_FW_TYPE_MAX_INDEX; i++) {
1564 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1568 seq_printf(m, "TA %s feature version: 0x%08x, firmware version: 0x%08x\n",
1569 ta_fw_name[i], fw_info.feature, fw_info.ver);
1573 query_fw.fw_type = AMDGPU_INFO_FW_SMC;
1574 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1577 smu_major = (fw_info.ver >> 16) & 0xffff;
1578 smu_minor = (fw_info.ver >> 8) & 0xff;
1579 smu_debug = (fw_info.ver >> 0) & 0xff;
1580 seq_printf(m, "SMC feature version: %u, firmware version: 0x%08x (%d.%d.%d)\n",
1581 fw_info.feature, fw_info.ver, smu_major, smu_minor, smu_debug);
1584 query_fw.fw_type = AMDGPU_INFO_FW_SDMA;
1585 for (i = 0; i < adev->sdma.num_instances; i++) {
1587 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1590 seq_printf(m, "SDMA%d feature version: %u, firmware version: 0x%08x\n",
1591 i, fw_info.feature, fw_info.ver);
1595 query_fw.fw_type = AMDGPU_INFO_FW_VCN;
1596 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1599 seq_printf(m, "VCN feature version: %u, firmware version: 0x%08x\n",
1600 fw_info.feature, fw_info.ver);
1603 query_fw.fw_type = AMDGPU_INFO_FW_DMCU;
1604 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1607 seq_printf(m, "DMCU feature version: %u, firmware version: 0x%08x\n",
1608 fw_info.feature, fw_info.ver);
1611 query_fw.fw_type = AMDGPU_INFO_FW_DMCUB;
1612 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1615 seq_printf(m, "DMCUB feature version: %u, firmware version: 0x%08x\n",
1616 fw_info.feature, fw_info.ver);
1619 query_fw.fw_type = AMDGPU_INFO_FW_TOC;
1620 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1623 seq_printf(m, "TOC feature version: %u, firmware version: 0x%08x\n",
1624 fw_info.feature, fw_info.ver);
1626 seq_printf(m, "VBIOS version: %s\n", ctx->vbios_version);
1631 DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_firmware_info);
1635 void amdgpu_debugfs_firmware_init(struct amdgpu_device *adev)
1637 #if defined(CONFIG_DEBUG_FS)
1638 struct drm_minor *minor = adev_to_drm(adev)->primary;
1639 struct dentry *root = minor->debugfs_root;
1641 debugfs_create_file("amdgpu_firmware_info", 0444, root,
1642 adev, &amdgpu_debugfs_firmware_info_fops);