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+ */
210 dev_info(adev->dev, "Using BACO for runtime pm\n");
213 /* Call ACPI methods: require modeset init
214 * but failure is not fatal
217 acpi_status = amdgpu_acpi_init(adev);
219 dev_dbg(dev->dev, "Error during ACPI methods call\n");
222 /* only need to skip on ATPX */
223 if (amdgpu_device_supports_px(dev))
224 dev_pm_set_driver_flags(dev->dev, DPM_FLAG_NO_DIRECT_COMPLETE);
225 /* we want direct complete for BOCO */
226 if (amdgpu_device_supports_boco(dev))
227 dev_pm_set_driver_flags(dev->dev, DPM_FLAG_SMART_PREPARE |
228 DPM_FLAG_SMART_SUSPEND |
229 DPM_FLAG_MAY_SKIP_RESUME);
230 pm_runtime_use_autosuspend(dev->dev);
231 pm_runtime_set_autosuspend_delay(dev->dev, 5000);
233 pm_runtime_allow(dev->dev);
235 pm_runtime_mark_last_busy(dev->dev);
236 pm_runtime_put_autosuspend(dev->dev);
239 * For runpm implemented via BACO, PMFW will handle the
240 * timing for BACO in and out:
241 * - put ASIC into BACO state only when both video and
242 * audio functions are in D3 state.
243 * - pull ASIC out of BACO state when either video or
244 * audio function is in D0 state.
245 * Also, at startup, PMFW assumes both functions are in
248 * So if snd driver was loaded prior to amdgpu driver
249 * and audio function was put into D3 state, there will
250 * be no PMFW-aware D-state transition(D0->D3) on runpm
251 * suspend. Thus the BACO will be not correctly kicked in.
253 * Via amdgpu_get_audio_func(), the audio dev is put
254 * into D0 state. Then there will be a PMFW-aware D-state
255 * transition(D0->D3) on runpm suspend.
257 if (amdgpu_device_supports_baco(dev) &&
258 !(adev->flags & AMD_IS_APU) &&
259 (adev->asic_type >= CHIP_NAVI10))
260 amdgpu_get_audio_func(adev);
263 if (amdgpu_acpi_smart_shift_update(dev, AMDGPU_SS_DRV_LOAD))
264 DRM_WARN("smart shift update failed\n");
268 /* balance pm_runtime_get_sync in amdgpu_driver_unload_kms */
269 if (adev->rmmio && adev->runpm)
270 pm_runtime_put_noidle(dev->dev);
271 amdgpu_driver_unload_kms(dev);
277 static int amdgpu_firmware_info(struct drm_amdgpu_info_firmware *fw_info,
278 struct drm_amdgpu_query_fw *query_fw,
279 struct amdgpu_device *adev)
281 switch (query_fw->fw_type) {
282 case AMDGPU_INFO_FW_VCE:
283 fw_info->ver = adev->vce.fw_version;
284 fw_info->feature = adev->vce.fb_version;
286 case AMDGPU_INFO_FW_UVD:
287 fw_info->ver = adev->uvd.fw_version;
288 fw_info->feature = 0;
290 case AMDGPU_INFO_FW_VCN:
291 fw_info->ver = adev->vcn.fw_version;
292 fw_info->feature = 0;
294 case AMDGPU_INFO_FW_GMC:
295 fw_info->ver = adev->gmc.fw_version;
296 fw_info->feature = 0;
298 case AMDGPU_INFO_FW_GFX_ME:
299 fw_info->ver = adev->gfx.me_fw_version;
300 fw_info->feature = adev->gfx.me_feature_version;
302 case AMDGPU_INFO_FW_GFX_PFP:
303 fw_info->ver = adev->gfx.pfp_fw_version;
304 fw_info->feature = adev->gfx.pfp_feature_version;
306 case AMDGPU_INFO_FW_GFX_CE:
307 fw_info->ver = adev->gfx.ce_fw_version;
308 fw_info->feature = adev->gfx.ce_feature_version;
310 case AMDGPU_INFO_FW_GFX_RLC:
311 fw_info->ver = adev->gfx.rlc_fw_version;
312 fw_info->feature = adev->gfx.rlc_feature_version;
314 case AMDGPU_INFO_FW_GFX_RLC_RESTORE_LIST_CNTL:
315 fw_info->ver = adev->gfx.rlc_srlc_fw_version;
316 fw_info->feature = adev->gfx.rlc_srlc_feature_version;
318 case AMDGPU_INFO_FW_GFX_RLC_RESTORE_LIST_GPM_MEM:
319 fw_info->ver = adev->gfx.rlc_srlg_fw_version;
320 fw_info->feature = adev->gfx.rlc_srlg_feature_version;
322 case AMDGPU_INFO_FW_GFX_RLC_RESTORE_LIST_SRM_MEM:
323 fw_info->ver = adev->gfx.rlc_srls_fw_version;
324 fw_info->feature = adev->gfx.rlc_srls_feature_version;
326 case AMDGPU_INFO_FW_GFX_MEC:
327 if (query_fw->index == 0) {
328 fw_info->ver = adev->gfx.mec_fw_version;
329 fw_info->feature = adev->gfx.mec_feature_version;
330 } else if (query_fw->index == 1) {
331 fw_info->ver = adev->gfx.mec2_fw_version;
332 fw_info->feature = adev->gfx.mec2_feature_version;
336 case AMDGPU_INFO_FW_SMC:
337 fw_info->ver = adev->pm.fw_version;
338 fw_info->feature = 0;
340 case AMDGPU_INFO_FW_TA:
341 switch (query_fw->index) {
342 case TA_FW_TYPE_PSP_XGMI:
343 fw_info->ver = adev->psp.xgmi_context.context.bin_desc.fw_version;
344 fw_info->feature = adev->psp.xgmi_context.context
345 .bin_desc.feature_version;
347 case TA_FW_TYPE_PSP_RAS:
348 fw_info->ver = adev->psp.ras_context.context.bin_desc.fw_version;
349 fw_info->feature = adev->psp.ras_context.context
350 .bin_desc.feature_version;
352 case TA_FW_TYPE_PSP_HDCP:
353 fw_info->ver = adev->psp.hdcp_context.context.bin_desc.fw_version;
354 fw_info->feature = adev->psp.hdcp_context.context
355 .bin_desc.feature_version;
357 case TA_FW_TYPE_PSP_DTM:
358 fw_info->ver = adev->psp.dtm_context.context.bin_desc.fw_version;
359 fw_info->feature = adev->psp.dtm_context.context
360 .bin_desc.feature_version;
362 case TA_FW_TYPE_PSP_RAP:
363 fw_info->ver = adev->psp.rap_context.context.bin_desc.fw_version;
364 fw_info->feature = adev->psp.rap_context.context
365 .bin_desc.feature_version;
367 case TA_FW_TYPE_PSP_SECUREDISPLAY:
368 fw_info->ver = adev->psp.securedisplay_context.context.bin_desc.fw_version;
370 adev->psp.securedisplay_context.context.bin_desc
377 case AMDGPU_INFO_FW_SDMA:
378 if (query_fw->index >= adev->sdma.num_instances)
380 fw_info->ver = adev->sdma.instance[query_fw->index].fw_version;
381 fw_info->feature = adev->sdma.instance[query_fw->index].feature_version;
383 case AMDGPU_INFO_FW_SOS:
384 fw_info->ver = adev->psp.sos.fw_version;
385 fw_info->feature = adev->psp.sos.feature_version;
387 case AMDGPU_INFO_FW_ASD:
388 fw_info->ver = adev->psp.asd_context.bin_desc.fw_version;
389 fw_info->feature = adev->psp.asd_context.bin_desc.feature_version;
391 case AMDGPU_INFO_FW_DMCU:
392 fw_info->ver = adev->dm.dmcu_fw_version;
393 fw_info->feature = 0;
395 case AMDGPU_INFO_FW_DMCUB:
396 fw_info->ver = adev->dm.dmcub_fw_version;
397 fw_info->feature = 0;
399 case AMDGPU_INFO_FW_TOC:
400 fw_info->ver = adev->psp.toc.fw_version;
401 fw_info->feature = adev->psp.toc.feature_version;
409 static int amdgpu_hw_ip_info(struct amdgpu_device *adev,
410 struct drm_amdgpu_info *info,
411 struct drm_amdgpu_info_hw_ip *result)
413 uint32_t ib_start_alignment = 0;
414 uint32_t ib_size_alignment = 0;
415 enum amd_ip_block_type type;
416 unsigned int num_rings = 0;
419 if (info->query_hw_ip.ip_instance >= AMDGPU_HW_IP_INSTANCE_MAX_COUNT)
422 switch (info->query_hw_ip.type) {
423 case AMDGPU_HW_IP_GFX:
424 type = AMD_IP_BLOCK_TYPE_GFX;
425 for (i = 0; i < adev->gfx.num_gfx_rings; i++)
426 if (adev->gfx.gfx_ring[i].sched.ready)
428 ib_start_alignment = 32;
429 ib_size_alignment = 32;
431 case AMDGPU_HW_IP_COMPUTE:
432 type = AMD_IP_BLOCK_TYPE_GFX;
433 for (i = 0; i < adev->gfx.num_compute_rings; i++)
434 if (adev->gfx.compute_ring[i].sched.ready)
436 ib_start_alignment = 32;
437 ib_size_alignment = 32;
439 case AMDGPU_HW_IP_DMA:
440 type = AMD_IP_BLOCK_TYPE_SDMA;
441 for (i = 0; i < adev->sdma.num_instances; i++)
442 if (adev->sdma.instance[i].ring.sched.ready)
444 ib_start_alignment = 256;
445 ib_size_alignment = 4;
447 case AMDGPU_HW_IP_UVD:
448 type = AMD_IP_BLOCK_TYPE_UVD;
449 for (i = 0; i < adev->uvd.num_uvd_inst; i++) {
450 if (adev->uvd.harvest_config & (1 << i))
453 if (adev->uvd.inst[i].ring.sched.ready)
456 ib_start_alignment = 64;
457 ib_size_alignment = 64;
459 case AMDGPU_HW_IP_VCE:
460 type = AMD_IP_BLOCK_TYPE_VCE;
461 for (i = 0; i < adev->vce.num_rings; i++)
462 if (adev->vce.ring[i].sched.ready)
464 ib_start_alignment = 4;
465 ib_size_alignment = 1;
467 case AMDGPU_HW_IP_UVD_ENC:
468 type = AMD_IP_BLOCK_TYPE_UVD;
469 for (i = 0; i < adev->uvd.num_uvd_inst; i++) {
470 if (adev->uvd.harvest_config & (1 << i))
473 for (j = 0; j < adev->uvd.num_enc_rings; j++)
474 if (adev->uvd.inst[i].ring_enc[j].sched.ready)
477 ib_start_alignment = 64;
478 ib_size_alignment = 64;
480 case AMDGPU_HW_IP_VCN_DEC:
481 type = AMD_IP_BLOCK_TYPE_VCN;
482 for (i = 0; i < adev->vcn.num_vcn_inst; i++) {
483 if (adev->uvd.harvest_config & (1 << i))
486 if (adev->vcn.inst[i].ring_dec.sched.ready)
489 ib_start_alignment = 16;
490 ib_size_alignment = 16;
492 case AMDGPU_HW_IP_VCN_ENC:
493 type = AMD_IP_BLOCK_TYPE_VCN;
494 for (i = 0; i < adev->vcn.num_vcn_inst; i++) {
495 if (adev->uvd.harvest_config & (1 << i))
498 for (j = 0; j < adev->vcn.num_enc_rings; j++)
499 if (adev->vcn.inst[i].ring_enc[j].sched.ready)
502 ib_start_alignment = 64;
503 ib_size_alignment = 1;
505 case AMDGPU_HW_IP_VCN_JPEG:
506 type = (amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_JPEG)) ?
507 AMD_IP_BLOCK_TYPE_JPEG : AMD_IP_BLOCK_TYPE_VCN;
509 for (i = 0; i < adev->jpeg.num_jpeg_inst; i++) {
510 if (adev->jpeg.harvest_config & (1 << i))
513 if (adev->jpeg.inst[i].ring_dec.sched.ready)
516 ib_start_alignment = 16;
517 ib_size_alignment = 16;
523 for (i = 0; i < adev->num_ip_blocks; i++)
524 if (adev->ip_blocks[i].version->type == type &&
525 adev->ip_blocks[i].status.valid)
528 if (i == adev->num_ip_blocks)
531 num_rings = min(amdgpu_ctx_num_entities[info->query_hw_ip.type],
534 result->hw_ip_version_major = adev->ip_blocks[i].version->major;
535 result->hw_ip_version_minor = adev->ip_blocks[i].version->minor;
536 result->capabilities_flags = 0;
537 result->available_rings = (1 << num_rings) - 1;
538 result->ib_start_alignment = ib_start_alignment;
539 result->ib_size_alignment = ib_size_alignment;
544 * Userspace get information ioctl
547 * amdgpu_info_ioctl - answer a device specific request.
549 * @dev: drm device pointer
550 * @data: request object
553 * This function is used to pass device specific parameters to the userspace
554 * drivers. Examples include: pci device id, pipeline parms, tiling params,
556 * Returns 0 on success, -EINVAL on failure.
558 int amdgpu_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
560 struct amdgpu_device *adev = drm_to_adev(dev);
561 struct drm_amdgpu_info *info = data;
562 struct amdgpu_mode_info *minfo = &adev->mode_info;
563 void __user *out = (void __user *)(uintptr_t)info->return_pointer;
564 uint32_t size = info->return_size;
565 struct drm_crtc *crtc;
569 int ui32_size = sizeof(ui32);
571 if (!info->return_size || !info->return_pointer)
574 switch (info->query) {
575 case AMDGPU_INFO_ACCEL_WORKING:
576 ui32 = adev->accel_working;
577 return copy_to_user(out, &ui32, min(size, 4u)) ? -EFAULT : 0;
578 case AMDGPU_INFO_CRTC_FROM_ID:
579 for (i = 0, found = 0; i < adev->mode_info.num_crtc; i++) {
580 crtc = (struct drm_crtc *)minfo->crtcs[i];
581 if (crtc && crtc->base.id == info->mode_crtc.id) {
582 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
583 ui32 = amdgpu_crtc->crtc_id;
589 DRM_DEBUG_KMS("unknown crtc id %d\n", info->mode_crtc.id);
592 return copy_to_user(out, &ui32, min(size, 4u)) ? -EFAULT : 0;
593 case AMDGPU_INFO_HW_IP_INFO: {
594 struct drm_amdgpu_info_hw_ip ip = {};
597 ret = amdgpu_hw_ip_info(adev, info, &ip);
601 ret = copy_to_user(out, &ip, min((size_t)size, sizeof(ip)));
602 return ret ? -EFAULT : 0;
604 case AMDGPU_INFO_HW_IP_COUNT: {
605 enum amd_ip_block_type type;
608 switch (info->query_hw_ip.type) {
609 case AMDGPU_HW_IP_GFX:
610 type = AMD_IP_BLOCK_TYPE_GFX;
612 case AMDGPU_HW_IP_COMPUTE:
613 type = AMD_IP_BLOCK_TYPE_GFX;
615 case AMDGPU_HW_IP_DMA:
616 type = AMD_IP_BLOCK_TYPE_SDMA;
618 case AMDGPU_HW_IP_UVD:
619 type = AMD_IP_BLOCK_TYPE_UVD;
621 case AMDGPU_HW_IP_VCE:
622 type = AMD_IP_BLOCK_TYPE_VCE;
624 case AMDGPU_HW_IP_UVD_ENC:
625 type = AMD_IP_BLOCK_TYPE_UVD;
627 case AMDGPU_HW_IP_VCN_DEC:
628 case AMDGPU_HW_IP_VCN_ENC:
629 type = AMD_IP_BLOCK_TYPE_VCN;
631 case AMDGPU_HW_IP_VCN_JPEG:
632 type = (amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_JPEG)) ?
633 AMD_IP_BLOCK_TYPE_JPEG : AMD_IP_BLOCK_TYPE_VCN;
639 for (i = 0; i < adev->num_ip_blocks; i++)
640 if (adev->ip_blocks[i].version->type == type &&
641 adev->ip_blocks[i].status.valid &&
642 count < AMDGPU_HW_IP_INSTANCE_MAX_COUNT)
645 return copy_to_user(out, &count, min(size, 4u)) ? -EFAULT : 0;
647 case AMDGPU_INFO_TIMESTAMP:
648 ui64 = amdgpu_gfx_get_gpu_clock_counter(adev);
649 return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
650 case AMDGPU_INFO_FW_VERSION: {
651 struct drm_amdgpu_info_firmware fw_info;
654 /* We only support one instance of each IP block right now. */
655 if (info->query_fw.ip_instance != 0)
658 ret = amdgpu_firmware_info(&fw_info, &info->query_fw, adev);
662 return copy_to_user(out, &fw_info,
663 min((size_t)size, sizeof(fw_info))) ? -EFAULT : 0;
665 case AMDGPU_INFO_NUM_BYTES_MOVED:
666 ui64 = atomic64_read(&adev->num_bytes_moved);
667 return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
668 case AMDGPU_INFO_NUM_EVICTIONS:
669 ui64 = atomic64_read(&adev->num_evictions);
670 return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
671 case AMDGPU_INFO_NUM_VRAM_CPU_PAGE_FAULTS:
672 ui64 = atomic64_read(&adev->num_vram_cpu_page_faults);
673 return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
674 case AMDGPU_INFO_VRAM_USAGE:
675 ui64 = amdgpu_vram_mgr_usage(ttm_manager_type(&adev->mman.bdev, TTM_PL_VRAM));
676 return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
677 case AMDGPU_INFO_VIS_VRAM_USAGE:
678 ui64 = amdgpu_vram_mgr_vis_usage(ttm_manager_type(&adev->mman.bdev, TTM_PL_VRAM));
679 return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
680 case AMDGPU_INFO_GTT_USAGE:
681 ui64 = amdgpu_gtt_mgr_usage(ttm_manager_type(&adev->mman.bdev, TTM_PL_TT));
682 return copy_to_user(out, &ui64, min(size, 8u)) ? -EFAULT : 0;
683 case AMDGPU_INFO_GDS_CONFIG: {
684 struct drm_amdgpu_info_gds gds_info;
686 memset(&gds_info, 0, sizeof(gds_info));
687 gds_info.compute_partition_size = adev->gds.gds_size;
688 gds_info.gds_total_size = adev->gds.gds_size;
689 gds_info.gws_per_compute_partition = adev->gds.gws_size;
690 gds_info.oa_per_compute_partition = adev->gds.oa_size;
691 return copy_to_user(out, &gds_info,
692 min((size_t)size, sizeof(gds_info))) ? -EFAULT : 0;
694 case AMDGPU_INFO_VRAM_GTT: {
695 struct drm_amdgpu_info_vram_gtt vram_gtt;
697 vram_gtt.vram_size = adev->gmc.real_vram_size -
698 atomic64_read(&adev->vram_pin_size) -
699 AMDGPU_VM_RESERVED_VRAM;
700 vram_gtt.vram_cpu_accessible_size =
701 min(adev->gmc.visible_vram_size -
702 atomic64_read(&adev->visible_pin_size),
704 vram_gtt.gtt_size = ttm_manager_type(&adev->mman.bdev, TTM_PL_TT)->size;
705 vram_gtt.gtt_size *= PAGE_SIZE;
706 vram_gtt.gtt_size -= atomic64_read(&adev->gart_pin_size);
707 return copy_to_user(out, &vram_gtt,
708 min((size_t)size, sizeof(vram_gtt))) ? -EFAULT : 0;
710 case AMDGPU_INFO_MEMORY: {
711 struct drm_amdgpu_memory_info mem;
712 struct ttm_resource_manager *vram_man =
713 ttm_manager_type(&adev->mman.bdev, TTM_PL_VRAM);
714 struct ttm_resource_manager *gtt_man =
715 ttm_manager_type(&adev->mman.bdev, TTM_PL_TT);
716 memset(&mem, 0, sizeof(mem));
717 mem.vram.total_heap_size = adev->gmc.real_vram_size;
718 mem.vram.usable_heap_size = adev->gmc.real_vram_size -
719 atomic64_read(&adev->vram_pin_size) -
720 AMDGPU_VM_RESERVED_VRAM;
721 mem.vram.heap_usage =
722 amdgpu_vram_mgr_usage(vram_man);
723 mem.vram.max_allocation = mem.vram.usable_heap_size * 3 / 4;
725 mem.cpu_accessible_vram.total_heap_size =
726 adev->gmc.visible_vram_size;
727 mem.cpu_accessible_vram.usable_heap_size =
728 min(adev->gmc.visible_vram_size -
729 atomic64_read(&adev->visible_pin_size),
730 mem.vram.usable_heap_size);
731 mem.cpu_accessible_vram.heap_usage =
732 amdgpu_vram_mgr_vis_usage(vram_man);
733 mem.cpu_accessible_vram.max_allocation =
734 mem.cpu_accessible_vram.usable_heap_size * 3 / 4;
736 mem.gtt.total_heap_size = gtt_man->size;
737 mem.gtt.total_heap_size *= PAGE_SIZE;
738 mem.gtt.usable_heap_size = mem.gtt.total_heap_size -
739 atomic64_read(&adev->gart_pin_size);
741 amdgpu_gtt_mgr_usage(gtt_man);
742 mem.gtt.max_allocation = mem.gtt.usable_heap_size * 3 / 4;
744 return copy_to_user(out, &mem,
745 min((size_t)size, sizeof(mem)))
748 case AMDGPU_INFO_READ_MMR_REG: {
749 unsigned n, alloc_size;
751 unsigned se_num = (info->read_mmr_reg.instance >>
752 AMDGPU_INFO_MMR_SE_INDEX_SHIFT) &
753 AMDGPU_INFO_MMR_SE_INDEX_MASK;
754 unsigned sh_num = (info->read_mmr_reg.instance >>
755 AMDGPU_INFO_MMR_SH_INDEX_SHIFT) &
756 AMDGPU_INFO_MMR_SH_INDEX_MASK;
758 /* set full masks if the userspace set all bits
759 * in the bitfields */
760 if (se_num == AMDGPU_INFO_MMR_SE_INDEX_MASK)
762 else if (se_num >= AMDGPU_GFX_MAX_SE)
764 if (sh_num == AMDGPU_INFO_MMR_SH_INDEX_MASK)
766 else if (sh_num >= AMDGPU_GFX_MAX_SH_PER_SE)
769 if (info->read_mmr_reg.count > 128)
772 regs = kmalloc_array(info->read_mmr_reg.count, sizeof(*regs), GFP_KERNEL);
775 alloc_size = info->read_mmr_reg.count * sizeof(*regs);
777 amdgpu_gfx_off_ctrl(adev, false);
778 for (i = 0; i < info->read_mmr_reg.count; i++) {
779 if (amdgpu_asic_read_register(adev, se_num, sh_num,
780 info->read_mmr_reg.dword_offset + i,
782 DRM_DEBUG_KMS("unallowed offset %#x\n",
783 info->read_mmr_reg.dword_offset + i);
785 amdgpu_gfx_off_ctrl(adev, true);
789 amdgpu_gfx_off_ctrl(adev, true);
790 n = copy_to_user(out, regs, min(size, alloc_size));
792 return n ? -EFAULT : 0;
794 case AMDGPU_INFO_DEV_INFO: {
795 struct drm_amdgpu_info_device *dev_info;
799 dev_info = kzalloc(sizeof(*dev_info), GFP_KERNEL);
803 dev_info->device_id = adev->pdev->device;
804 dev_info->chip_rev = adev->rev_id;
805 dev_info->external_rev = adev->external_rev_id;
806 dev_info->pci_rev = adev->pdev->revision;
807 dev_info->family = adev->family;
808 dev_info->num_shader_engines = adev->gfx.config.max_shader_engines;
809 dev_info->num_shader_arrays_per_engine = adev->gfx.config.max_sh_per_se;
810 /* return all clocks in KHz */
811 dev_info->gpu_counter_freq = amdgpu_asic_get_xclk(adev) * 10;
812 if (adev->pm.dpm_enabled) {
813 dev_info->max_engine_clock = amdgpu_dpm_get_sclk(adev, false) * 10;
814 dev_info->max_memory_clock = amdgpu_dpm_get_mclk(adev, false) * 10;
816 dev_info->max_engine_clock = adev->clock.default_sclk * 10;
817 dev_info->max_memory_clock = adev->clock.default_mclk * 10;
819 dev_info->enabled_rb_pipes_mask = adev->gfx.config.backend_enable_mask;
820 dev_info->num_rb_pipes = adev->gfx.config.max_backends_per_se *
821 adev->gfx.config.max_shader_engines;
822 dev_info->num_hw_gfx_contexts = adev->gfx.config.max_hw_contexts;
824 dev_info->ids_flags = 0;
825 if (adev->flags & AMD_IS_APU)
826 dev_info->ids_flags |= AMDGPU_IDS_FLAGS_FUSION;
827 if (amdgpu_mcbp || amdgpu_sriov_vf(adev))
828 dev_info->ids_flags |= AMDGPU_IDS_FLAGS_PREEMPTION;
829 if (amdgpu_is_tmz(adev))
830 dev_info->ids_flags |= AMDGPU_IDS_FLAGS_TMZ;
832 vm_size = adev->vm_manager.max_pfn * AMDGPU_GPU_PAGE_SIZE;
833 vm_size -= AMDGPU_VA_RESERVED_SIZE;
835 /* Older VCE FW versions are buggy and can handle only 40bits */
836 if (adev->vce.fw_version &&
837 adev->vce.fw_version < AMDGPU_VCE_FW_53_45)
838 vm_size = min(vm_size, 1ULL << 40);
840 dev_info->virtual_address_offset = AMDGPU_VA_RESERVED_SIZE;
841 dev_info->virtual_address_max =
842 min(vm_size, AMDGPU_GMC_HOLE_START);
844 if (vm_size > AMDGPU_GMC_HOLE_START) {
845 dev_info->high_va_offset = AMDGPU_GMC_HOLE_END;
846 dev_info->high_va_max = AMDGPU_GMC_HOLE_END | vm_size;
848 dev_info->virtual_address_alignment = max_t(u32, PAGE_SIZE, AMDGPU_GPU_PAGE_SIZE);
849 dev_info->pte_fragment_size = (1 << adev->vm_manager.fragment_size) * AMDGPU_GPU_PAGE_SIZE;
850 dev_info->gart_page_size = max_t(u32, PAGE_SIZE, AMDGPU_GPU_PAGE_SIZE);
851 dev_info->cu_active_number = adev->gfx.cu_info.number;
852 dev_info->cu_ao_mask = adev->gfx.cu_info.ao_cu_mask;
853 dev_info->ce_ram_size = adev->gfx.ce_ram_size;
854 memcpy(&dev_info->cu_ao_bitmap[0], &adev->gfx.cu_info.ao_cu_bitmap[0],
855 sizeof(adev->gfx.cu_info.ao_cu_bitmap));
856 memcpy(&dev_info->cu_bitmap[0], &adev->gfx.cu_info.bitmap[0],
857 sizeof(adev->gfx.cu_info.bitmap));
858 dev_info->vram_type = adev->gmc.vram_type;
859 dev_info->vram_bit_width = adev->gmc.vram_width;
860 dev_info->vce_harvest_config = adev->vce.harvest_config;
861 dev_info->gc_double_offchip_lds_buf =
862 adev->gfx.config.double_offchip_lds_buf;
863 dev_info->wave_front_size = adev->gfx.cu_info.wave_front_size;
864 dev_info->num_shader_visible_vgprs = adev->gfx.config.max_gprs;
865 dev_info->num_cu_per_sh = adev->gfx.config.max_cu_per_sh;
866 dev_info->num_tcc_blocks = adev->gfx.config.max_texture_channel_caches;
867 dev_info->gs_vgt_table_depth = adev->gfx.config.gs_vgt_table_depth;
868 dev_info->gs_prim_buffer_depth = adev->gfx.config.gs_prim_buffer_depth;
869 dev_info->max_gs_waves_per_vgt = adev->gfx.config.max_gs_threads;
871 if (adev->family >= AMDGPU_FAMILY_NV)
872 dev_info->pa_sc_tile_steering_override =
873 adev->gfx.config.pa_sc_tile_steering_override;
875 dev_info->tcc_disabled_mask = adev->gfx.config.tcc_disabled_mask;
877 ret = copy_to_user(out, dev_info,
878 min((size_t)size, sizeof(*dev_info))) ? -EFAULT : 0;
882 case AMDGPU_INFO_VCE_CLOCK_TABLE: {
884 struct drm_amdgpu_info_vce_clock_table vce_clk_table = {};
885 struct amd_vce_state *vce_state;
887 for (i = 0; i < AMDGPU_VCE_CLOCK_TABLE_ENTRIES; i++) {
888 vce_state = amdgpu_dpm_get_vce_clock_state(adev, i);
890 vce_clk_table.entries[i].sclk = vce_state->sclk;
891 vce_clk_table.entries[i].mclk = vce_state->mclk;
892 vce_clk_table.entries[i].eclk = vce_state->evclk;
893 vce_clk_table.num_valid_entries++;
897 return copy_to_user(out, &vce_clk_table,
898 min((size_t)size, sizeof(vce_clk_table))) ? -EFAULT : 0;
900 case AMDGPU_INFO_VBIOS: {
901 uint32_t bios_size = adev->bios_size;
903 switch (info->vbios_info.type) {
904 case AMDGPU_INFO_VBIOS_SIZE:
905 return copy_to_user(out, &bios_size,
906 min((size_t)size, sizeof(bios_size)))
908 case AMDGPU_INFO_VBIOS_IMAGE: {
910 uint32_t bios_offset = info->vbios_info.offset;
912 if (bios_offset >= bios_size)
915 bios = adev->bios + bios_offset;
916 return copy_to_user(out, bios,
917 min((size_t)size, (size_t)(bios_size - bios_offset)))
920 case AMDGPU_INFO_VBIOS_INFO: {
921 struct drm_amdgpu_info_vbios vbios_info = {};
922 struct atom_context *atom_context;
924 atom_context = adev->mode_info.atom_context;
925 memcpy(vbios_info.name, atom_context->name, sizeof(atom_context->name));
926 memcpy(vbios_info.vbios_pn, atom_context->vbios_pn, sizeof(atom_context->vbios_pn));
927 vbios_info.version = atom_context->version;
928 memcpy(vbios_info.vbios_ver_str, atom_context->vbios_ver_str,
929 sizeof(atom_context->vbios_ver_str));
930 memcpy(vbios_info.date, atom_context->date, sizeof(atom_context->date));
932 return copy_to_user(out, &vbios_info,
933 min((size_t)size, sizeof(vbios_info))) ? -EFAULT : 0;
936 DRM_DEBUG_KMS("Invalid request %d\n",
937 info->vbios_info.type);
941 case AMDGPU_INFO_NUM_HANDLES: {
942 struct drm_amdgpu_info_num_handles handle;
944 switch (info->query_hw_ip.type) {
945 case AMDGPU_HW_IP_UVD:
946 /* Starting Polaris, we support unlimited UVD handles */
947 if (adev->asic_type < CHIP_POLARIS10) {
948 handle.uvd_max_handles = adev->uvd.max_handles;
949 handle.uvd_used_handles = amdgpu_uvd_used_handles(adev);
951 return copy_to_user(out, &handle,
952 min((size_t)size, sizeof(handle))) ? -EFAULT : 0;
962 case AMDGPU_INFO_SENSOR: {
963 if (!adev->pm.dpm_enabled)
966 switch (info->sensor_info.type) {
967 case AMDGPU_INFO_SENSOR_GFX_SCLK:
968 /* get sclk in Mhz */
969 if (amdgpu_dpm_read_sensor(adev,
970 AMDGPU_PP_SENSOR_GFX_SCLK,
971 (void *)&ui32, &ui32_size)) {
976 case AMDGPU_INFO_SENSOR_GFX_MCLK:
977 /* get mclk in Mhz */
978 if (amdgpu_dpm_read_sensor(adev,
979 AMDGPU_PP_SENSOR_GFX_MCLK,
980 (void *)&ui32, &ui32_size)) {
985 case AMDGPU_INFO_SENSOR_GPU_TEMP:
986 /* get temperature in millidegrees C */
987 if (amdgpu_dpm_read_sensor(adev,
988 AMDGPU_PP_SENSOR_GPU_TEMP,
989 (void *)&ui32, &ui32_size)) {
993 case AMDGPU_INFO_SENSOR_GPU_LOAD:
995 if (amdgpu_dpm_read_sensor(adev,
996 AMDGPU_PP_SENSOR_GPU_LOAD,
997 (void *)&ui32, &ui32_size)) {
1001 case AMDGPU_INFO_SENSOR_GPU_AVG_POWER:
1002 /* get average GPU power */
1003 if (amdgpu_dpm_read_sensor(adev,
1004 AMDGPU_PP_SENSOR_GPU_POWER,
1005 (void *)&ui32, &ui32_size)) {
1010 case AMDGPU_INFO_SENSOR_VDDNB:
1011 /* get VDDNB in millivolts */
1012 if (amdgpu_dpm_read_sensor(adev,
1013 AMDGPU_PP_SENSOR_VDDNB,
1014 (void *)&ui32, &ui32_size)) {
1018 case AMDGPU_INFO_SENSOR_VDDGFX:
1019 /* get VDDGFX in millivolts */
1020 if (amdgpu_dpm_read_sensor(adev,
1021 AMDGPU_PP_SENSOR_VDDGFX,
1022 (void *)&ui32, &ui32_size)) {
1026 case AMDGPU_INFO_SENSOR_STABLE_PSTATE_GFX_SCLK:
1027 /* get stable pstate sclk in Mhz */
1028 if (amdgpu_dpm_read_sensor(adev,
1029 AMDGPU_PP_SENSOR_STABLE_PSTATE_SCLK,
1030 (void *)&ui32, &ui32_size)) {
1035 case AMDGPU_INFO_SENSOR_STABLE_PSTATE_GFX_MCLK:
1036 /* get stable pstate mclk in Mhz */
1037 if (amdgpu_dpm_read_sensor(adev,
1038 AMDGPU_PP_SENSOR_STABLE_PSTATE_MCLK,
1039 (void *)&ui32, &ui32_size)) {
1045 DRM_DEBUG_KMS("Invalid request %d\n",
1046 info->sensor_info.type);
1049 return copy_to_user(out, &ui32, min(size, 4u)) ? -EFAULT : 0;
1051 case AMDGPU_INFO_VRAM_LOST_COUNTER:
1052 ui32 = atomic_read(&adev->vram_lost_counter);
1053 return copy_to_user(out, &ui32, min(size, 4u)) ? -EFAULT : 0;
1054 case AMDGPU_INFO_RAS_ENABLED_FEATURES: {
1055 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
1060 ras_mask = (uint64_t)adev->ras_enabled << 32 | ras->features;
1062 return copy_to_user(out, &ras_mask,
1063 min_t(u64, size, sizeof(ras_mask))) ?
1066 case AMDGPU_INFO_VIDEO_CAPS: {
1067 const struct amdgpu_video_codecs *codecs;
1068 struct drm_amdgpu_info_video_caps *caps;
1071 switch (info->video_cap.type) {
1072 case AMDGPU_INFO_VIDEO_CAPS_DECODE:
1073 r = amdgpu_asic_query_video_codecs(adev, false, &codecs);
1077 case AMDGPU_INFO_VIDEO_CAPS_ENCODE:
1078 r = amdgpu_asic_query_video_codecs(adev, true, &codecs);
1083 DRM_DEBUG_KMS("Invalid request %d\n",
1084 info->video_cap.type);
1088 caps = kzalloc(sizeof(*caps), GFP_KERNEL);
1092 for (i = 0; i < codecs->codec_count; i++) {
1093 int idx = codecs->codec_array[i].codec_type;
1096 case AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_MPEG2:
1097 case AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_MPEG4:
1098 case AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_VC1:
1099 case AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_MPEG4_AVC:
1100 case AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_HEVC:
1101 case AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_JPEG:
1102 case AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_VP9:
1103 case AMDGPU_INFO_VIDEO_CAPS_CODEC_IDX_AV1:
1104 caps->codec_info[idx].valid = 1;
1105 caps->codec_info[idx].max_width =
1106 codecs->codec_array[i].max_width;
1107 caps->codec_info[idx].max_height =
1108 codecs->codec_array[i].max_height;
1109 caps->codec_info[idx].max_pixels_per_frame =
1110 codecs->codec_array[i].max_pixels_per_frame;
1111 caps->codec_info[idx].max_level =
1112 codecs->codec_array[i].max_level;
1118 r = copy_to_user(out, caps,
1119 min((size_t)size, sizeof(*caps))) ? -EFAULT : 0;
1124 DRM_DEBUG_KMS("Invalid request %d\n", info->query);
1132 * Outdated mess for old drm with Xorg being in charge (void function now).
1135 * amdgpu_driver_lastclose_kms - drm callback for last close
1137 * @dev: drm dev pointer
1139 * Switch vga_switcheroo state after last close (all asics).
1141 void amdgpu_driver_lastclose_kms(struct drm_device *dev)
1143 drm_fb_helper_lastclose(dev);
1144 vga_switcheroo_process_delayed_switch();
1148 * amdgpu_driver_open_kms - drm callback for open
1150 * @dev: drm dev pointer
1151 * @file_priv: drm file
1153 * On device open, init vm on cayman+ (all asics).
1154 * Returns 0 on success, error on failure.
1156 int amdgpu_driver_open_kms(struct drm_device *dev, struct drm_file *file_priv)
1158 struct amdgpu_device *adev = drm_to_adev(dev);
1159 struct amdgpu_fpriv *fpriv;
1162 /* Ensure IB tests are run on ring */
1163 flush_delayed_work(&adev->delayed_init_work);
1166 if (amdgpu_ras_intr_triggered()) {
1167 DRM_ERROR("RAS Intr triggered, device disabled!!");
1171 file_priv->driver_priv = NULL;
1173 r = pm_runtime_get_sync(dev->dev);
1177 fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL);
1178 if (unlikely(!fpriv)) {
1183 pasid = amdgpu_pasid_alloc(16);
1185 dev_warn(adev->dev, "No more PASIDs available!");
1189 r = amdgpu_vm_init(adev, &fpriv->vm);
1193 r = amdgpu_vm_set_pasid(adev, &fpriv->vm, pasid);
1197 fpriv->prt_va = amdgpu_vm_bo_add(adev, &fpriv->vm, NULL);
1198 if (!fpriv->prt_va) {
1203 if (amdgpu_mcbp || amdgpu_sriov_vf(adev)) {
1204 uint64_t csa_addr = amdgpu_csa_vaddr(adev) & AMDGPU_GMC_HOLE_MASK;
1206 r = amdgpu_map_static_csa(adev, &fpriv->vm, adev->virt.csa_obj,
1207 &fpriv->csa_va, csa_addr, AMDGPU_CSA_SIZE);
1212 mutex_init(&fpriv->bo_list_lock);
1213 idr_init(&fpriv->bo_list_handles);
1215 amdgpu_ctx_mgr_init(&fpriv->ctx_mgr);
1217 file_priv->driver_priv = fpriv;
1221 amdgpu_vm_fini(adev, &fpriv->vm);
1225 amdgpu_pasid_free(pasid);
1226 amdgpu_vm_set_pasid(adev, &fpriv->vm, 0);
1232 pm_runtime_mark_last_busy(dev->dev);
1234 pm_runtime_put_autosuspend(dev->dev);
1240 * amdgpu_driver_postclose_kms - drm callback for post close
1242 * @dev: drm dev pointer
1243 * @file_priv: drm file
1245 * On device post close, tear down vm on cayman+ (all asics).
1247 void amdgpu_driver_postclose_kms(struct drm_device *dev,
1248 struct drm_file *file_priv)
1250 struct amdgpu_device *adev = drm_to_adev(dev);
1251 struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
1252 struct amdgpu_bo_list *list;
1253 struct amdgpu_bo *pd;
1260 pm_runtime_get_sync(dev->dev);
1262 if (amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_UVD) != NULL)
1263 amdgpu_uvd_free_handles(adev, file_priv);
1264 if (amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_VCE) != NULL)
1265 amdgpu_vce_free_handles(adev, file_priv);
1267 amdgpu_vm_bo_rmv(adev, fpriv->prt_va);
1269 if (amdgpu_mcbp || amdgpu_sriov_vf(adev)) {
1270 /* TODO: how to handle reserve failure */
1271 BUG_ON(amdgpu_bo_reserve(adev->virt.csa_obj, true));
1272 amdgpu_vm_bo_rmv(adev, fpriv->csa_va);
1273 fpriv->csa_va = NULL;
1274 amdgpu_bo_unreserve(adev->virt.csa_obj);
1277 pasid = fpriv->vm.pasid;
1278 pd = amdgpu_bo_ref(fpriv->vm.root.bo);
1280 amdgpu_ctx_mgr_fini(&fpriv->ctx_mgr);
1281 amdgpu_vm_fini(adev, &fpriv->vm);
1284 amdgpu_pasid_free_delayed(pd->tbo.base.resv, pasid);
1285 amdgpu_bo_unref(&pd);
1287 idr_for_each_entry(&fpriv->bo_list_handles, list, handle)
1288 amdgpu_bo_list_put(list);
1290 idr_destroy(&fpriv->bo_list_handles);
1291 mutex_destroy(&fpriv->bo_list_lock);
1294 file_priv->driver_priv = NULL;
1296 pm_runtime_mark_last_busy(dev->dev);
1297 pm_runtime_put_autosuspend(dev->dev);
1301 void amdgpu_driver_release_kms(struct drm_device *dev)
1303 struct amdgpu_device *adev = drm_to_adev(dev);
1305 amdgpu_device_fini_sw(adev);
1306 pci_set_drvdata(adev->pdev, NULL);
1310 * VBlank related functions.
1313 * amdgpu_get_vblank_counter_kms - get frame count
1315 * @crtc: crtc to get the frame count from
1317 * Gets the frame count on the requested crtc (all asics).
1318 * Returns frame count on success, -EINVAL on failure.
1320 u32 amdgpu_get_vblank_counter_kms(struct drm_crtc *crtc)
1322 struct drm_device *dev = crtc->dev;
1323 unsigned int pipe = crtc->index;
1324 struct amdgpu_device *adev = drm_to_adev(dev);
1325 int vpos, hpos, stat;
1328 if (pipe >= adev->mode_info.num_crtc) {
1329 DRM_ERROR("Invalid crtc %u\n", pipe);
1333 /* The hw increments its frame counter at start of vsync, not at start
1334 * of vblank, as is required by DRM core vblank counter handling.
1335 * Cook the hw count here to make it appear to the caller as if it
1336 * incremented at start of vblank. We measure distance to start of
1337 * vblank in vpos. vpos therefore will be >= 0 between start of vblank
1338 * and start of vsync, so vpos >= 0 means to bump the hw frame counter
1339 * result by 1 to give the proper appearance to caller.
1341 if (adev->mode_info.crtcs[pipe]) {
1342 /* Repeat readout if needed to provide stable result if
1343 * we cross start of vsync during the queries.
1346 count = amdgpu_display_vblank_get_counter(adev, pipe);
1347 /* Ask amdgpu_display_get_crtc_scanoutpos to return
1348 * vpos as distance to start of vblank, instead of
1349 * regular vertical scanout pos.
1351 stat = amdgpu_display_get_crtc_scanoutpos(
1352 dev, pipe, GET_DISTANCE_TO_VBLANKSTART,
1353 &vpos, &hpos, NULL, NULL,
1354 &adev->mode_info.crtcs[pipe]->base.hwmode);
1355 } while (count != amdgpu_display_vblank_get_counter(adev, pipe));
1357 if (((stat & (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE)) !=
1358 (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE))) {
1359 DRM_DEBUG_VBL("Query failed! stat %d\n", stat);
1361 DRM_DEBUG_VBL("crtc %d: dist from vblank start %d\n",
1364 /* Bump counter if we are at >= leading edge of vblank,
1365 * but before vsync where vpos would turn negative and
1366 * the hw counter really increments.
1372 /* Fallback to use value as is. */
1373 count = amdgpu_display_vblank_get_counter(adev, pipe);
1374 DRM_DEBUG_VBL("NULL mode info! Returned count may be wrong.\n");
1381 * amdgpu_enable_vblank_kms - enable vblank interrupt
1383 * @crtc: crtc to enable vblank interrupt for
1385 * Enable the interrupt on the requested crtc (all asics).
1386 * Returns 0 on success, -EINVAL on failure.
1388 int amdgpu_enable_vblank_kms(struct drm_crtc *crtc)
1390 struct drm_device *dev = crtc->dev;
1391 unsigned int pipe = crtc->index;
1392 struct amdgpu_device *adev = drm_to_adev(dev);
1393 int idx = amdgpu_display_crtc_idx_to_irq_type(adev, pipe);
1395 return amdgpu_irq_get(adev, &adev->crtc_irq, idx);
1399 * amdgpu_disable_vblank_kms - disable vblank interrupt
1401 * @crtc: crtc to disable vblank interrupt for
1403 * Disable the interrupt on the requested crtc (all asics).
1405 void amdgpu_disable_vblank_kms(struct drm_crtc *crtc)
1407 struct drm_device *dev = crtc->dev;
1408 unsigned int pipe = crtc->index;
1409 struct amdgpu_device *adev = drm_to_adev(dev);
1410 int idx = amdgpu_display_crtc_idx_to_irq_type(adev, pipe);
1412 amdgpu_irq_put(adev, &adev->crtc_irq, idx);
1418 #if defined(CONFIG_DEBUG_FS)
1420 static int amdgpu_debugfs_firmware_info_show(struct seq_file *m, void *unused)
1422 struct amdgpu_device *adev = (struct amdgpu_device *)m->private;
1423 struct drm_amdgpu_info_firmware fw_info;
1424 struct drm_amdgpu_query_fw query_fw;
1425 struct atom_context *ctx = adev->mode_info.atom_context;
1426 uint8_t smu_minor, smu_debug;
1430 static const char *ta_fw_name[TA_FW_TYPE_MAX_INDEX] = {
1431 #define TA_FW_NAME(type) [TA_FW_TYPE_PSP_##type] = #type
1437 TA_FW_NAME(SECUREDISPLAY),
1442 query_fw.fw_type = AMDGPU_INFO_FW_VCE;
1443 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1446 seq_printf(m, "VCE feature version: %u, firmware version: 0x%08x\n",
1447 fw_info.feature, fw_info.ver);
1450 query_fw.fw_type = AMDGPU_INFO_FW_UVD;
1451 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1454 seq_printf(m, "UVD feature version: %u, firmware version: 0x%08x\n",
1455 fw_info.feature, fw_info.ver);
1458 query_fw.fw_type = AMDGPU_INFO_FW_GMC;
1459 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1462 seq_printf(m, "MC feature version: %u, firmware version: 0x%08x\n",
1463 fw_info.feature, fw_info.ver);
1466 query_fw.fw_type = AMDGPU_INFO_FW_GFX_ME;
1467 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1470 seq_printf(m, "ME feature version: %u, firmware version: 0x%08x\n",
1471 fw_info.feature, fw_info.ver);
1474 query_fw.fw_type = AMDGPU_INFO_FW_GFX_PFP;
1475 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1478 seq_printf(m, "PFP feature version: %u, firmware version: 0x%08x\n",
1479 fw_info.feature, fw_info.ver);
1482 query_fw.fw_type = AMDGPU_INFO_FW_GFX_CE;
1483 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1486 seq_printf(m, "CE feature version: %u, firmware version: 0x%08x\n",
1487 fw_info.feature, fw_info.ver);
1490 query_fw.fw_type = AMDGPU_INFO_FW_GFX_RLC;
1491 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1494 seq_printf(m, "RLC feature version: %u, firmware version: 0x%08x\n",
1495 fw_info.feature, fw_info.ver);
1497 /* RLC SAVE RESTORE LIST CNTL */
1498 query_fw.fw_type = AMDGPU_INFO_FW_GFX_RLC_RESTORE_LIST_CNTL;
1499 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1502 seq_printf(m, "RLC SRLC feature version: %u, firmware version: 0x%08x\n",
1503 fw_info.feature, fw_info.ver);
1505 /* RLC SAVE RESTORE LIST GPM MEM */
1506 query_fw.fw_type = AMDGPU_INFO_FW_GFX_RLC_RESTORE_LIST_GPM_MEM;
1507 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1510 seq_printf(m, "RLC SRLG feature version: %u, firmware version: 0x%08x\n",
1511 fw_info.feature, fw_info.ver);
1513 /* RLC SAVE RESTORE LIST SRM MEM */
1514 query_fw.fw_type = AMDGPU_INFO_FW_GFX_RLC_RESTORE_LIST_SRM_MEM;
1515 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1518 seq_printf(m, "RLC SRLS feature version: %u, firmware version: 0x%08x\n",
1519 fw_info.feature, fw_info.ver);
1522 query_fw.fw_type = AMDGPU_INFO_FW_GFX_MEC;
1524 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1527 seq_printf(m, "MEC feature version: %u, firmware version: 0x%08x\n",
1528 fw_info.feature, fw_info.ver);
1531 if (adev->gfx.mec2_fw) {
1533 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1536 seq_printf(m, "MEC2 feature version: %u, firmware version: 0x%08x\n",
1537 fw_info.feature, fw_info.ver);
1541 query_fw.fw_type = AMDGPU_INFO_FW_SOS;
1542 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1545 seq_printf(m, "SOS feature version: %u, firmware version: 0x%08x\n",
1546 fw_info.feature, fw_info.ver);
1550 query_fw.fw_type = AMDGPU_INFO_FW_ASD;
1551 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1554 seq_printf(m, "ASD feature version: %u, firmware version: 0x%08x\n",
1555 fw_info.feature, fw_info.ver);
1557 query_fw.fw_type = AMDGPU_INFO_FW_TA;
1558 for (i = TA_FW_TYPE_PSP_XGMI; i < TA_FW_TYPE_MAX_INDEX; i++) {
1560 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1564 seq_printf(m, "TA %s feature version: 0x%08x, firmware version: 0x%08x\n",
1565 ta_fw_name[i], fw_info.feature, fw_info.ver);
1569 query_fw.fw_type = AMDGPU_INFO_FW_SMC;
1570 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1573 smu_major = (fw_info.ver >> 16) & 0xffff;
1574 smu_minor = (fw_info.ver >> 8) & 0xff;
1575 smu_debug = (fw_info.ver >> 0) & 0xff;
1576 seq_printf(m, "SMC feature version: %u, firmware version: 0x%08x (%d.%d.%d)\n",
1577 fw_info.feature, fw_info.ver, smu_major, smu_minor, smu_debug);
1580 query_fw.fw_type = AMDGPU_INFO_FW_SDMA;
1581 for (i = 0; i < adev->sdma.num_instances; i++) {
1583 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1586 seq_printf(m, "SDMA%d feature version: %u, firmware version: 0x%08x\n",
1587 i, fw_info.feature, fw_info.ver);
1591 query_fw.fw_type = AMDGPU_INFO_FW_VCN;
1592 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1595 seq_printf(m, "VCN feature version: %u, firmware version: 0x%08x\n",
1596 fw_info.feature, fw_info.ver);
1599 query_fw.fw_type = AMDGPU_INFO_FW_DMCU;
1600 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1603 seq_printf(m, "DMCU feature version: %u, firmware version: 0x%08x\n",
1604 fw_info.feature, fw_info.ver);
1607 query_fw.fw_type = AMDGPU_INFO_FW_DMCUB;
1608 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1611 seq_printf(m, "DMCUB feature version: %u, firmware version: 0x%08x\n",
1612 fw_info.feature, fw_info.ver);
1615 query_fw.fw_type = AMDGPU_INFO_FW_TOC;
1616 ret = amdgpu_firmware_info(&fw_info, &query_fw, adev);
1619 seq_printf(m, "TOC feature version: %u, firmware version: 0x%08x\n",
1620 fw_info.feature, fw_info.ver);
1622 seq_printf(m, "VBIOS version: %s\n", ctx->vbios_version);
1627 DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_firmware_info);
1631 void amdgpu_debugfs_firmware_init(struct amdgpu_device *adev)
1633 #if defined(CONFIG_DEBUG_FS)
1634 struct drm_minor *minor = adev_to_drm(adev)->primary;
1635 struct dentry *root = minor->debugfs_root;
1637 debugfs_create_file("amdgpu_firmware_info", 0444, root,
1638 adev, &amdgpu_debugfs_firmware_info_fops);