2 * Copyright 2016 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
24 #include <linux/module.h>
27 #include <asm/hypervisor.h>
30 #include <drm/drm_drv.h>
34 #include "amdgpu_ras.h"
35 #include "amdgpu_reset.h"
40 #define POPULATE_UCODE_INFO(vf2pf_info, ucode, ver) \
42 vf2pf_info->ucode_info[ucode].id = ucode; \
43 vf2pf_info->ucode_info[ucode].version = ver; \
46 bool amdgpu_virt_mmio_blocked(struct amdgpu_device *adev)
48 /* By now all MMIO pages except mailbox are blocked */
49 /* if blocking is enabled in hypervisor. Choose the */
50 /* SCRATCH_REG0 to test. */
51 return RREG32_NO_KIQ(0xc040) == 0xffffffff;
54 void amdgpu_virt_init_setting(struct amdgpu_device *adev)
56 struct drm_device *ddev = adev_to_drm(adev);
58 /* enable virtual display */
59 if (adev->asic_type != CHIP_ALDEBARAN &&
60 adev->asic_type != CHIP_ARCTURUS &&
61 ((adev->pdev->class >> 8) != PCI_CLASS_ACCELERATOR_PROCESSING)) {
62 if (adev->mode_info.num_crtc == 0)
63 adev->mode_info.num_crtc = 1;
64 adev->enable_virtual_display = true;
66 ddev->driver_features &= ~DRIVER_ATOMIC;
70 /* Reduce kcq number to 2 to reduce latency */
71 if (amdgpu_num_kcq == -1)
76 * amdgpu_virt_request_full_gpu() - request full gpu access
77 * @adev: amdgpu device.
78 * @init: is driver init time.
79 * When start to init/fini driver, first need to request full gpu access.
80 * Return: Zero if request success, otherwise will return error.
82 int amdgpu_virt_request_full_gpu(struct amdgpu_device *adev, bool init)
84 struct amdgpu_virt *virt = &adev->virt;
87 if (virt->ops && virt->ops->req_full_gpu) {
88 r = virt->ops->req_full_gpu(adev, init);
90 adev->no_hw_access = true;
94 adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME;
101 * amdgpu_virt_release_full_gpu() - release full gpu access
102 * @adev: amdgpu device.
103 * @init: is driver init time.
104 * When finishing driver init/fini, need to release full gpu access.
105 * Return: Zero if release success, otherwise will returen error.
107 int amdgpu_virt_release_full_gpu(struct amdgpu_device *adev, bool init)
109 struct amdgpu_virt *virt = &adev->virt;
112 if (virt->ops && virt->ops->rel_full_gpu) {
113 r = virt->ops->rel_full_gpu(adev, init);
117 adev->virt.caps |= AMDGPU_SRIOV_CAPS_RUNTIME;
123 * amdgpu_virt_reset_gpu() - reset gpu
124 * @adev: amdgpu device.
125 * Send reset command to GPU hypervisor to reset GPU that VM is using
126 * Return: Zero if reset success, otherwise will return error.
128 int amdgpu_virt_reset_gpu(struct amdgpu_device *adev)
130 struct amdgpu_virt *virt = &adev->virt;
133 if (virt->ops && virt->ops->reset_gpu) {
134 r = virt->ops->reset_gpu(adev);
138 adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME;
144 void amdgpu_virt_request_init_data(struct amdgpu_device *adev)
146 struct amdgpu_virt *virt = &adev->virt;
148 if (virt->ops && virt->ops->req_init_data)
149 virt->ops->req_init_data(adev);
151 if (adev->virt.req_init_data_ver > 0)
152 DRM_INFO("host supports REQ_INIT_DATA handshake\n");
154 DRM_WARN("host doesn't support REQ_INIT_DATA handshake\n");
158 * amdgpu_virt_ready_to_reset() - send ready to reset to host
159 * @adev: amdgpu device.
160 * Send ready to reset message to GPU hypervisor to signal we have stopped GPU
161 * activity and is ready for host FLR
163 void amdgpu_virt_ready_to_reset(struct amdgpu_device *adev)
165 struct amdgpu_virt *virt = &adev->virt;
167 if (virt->ops && virt->ops->reset_gpu)
168 virt->ops->ready_to_reset(adev);
172 * amdgpu_virt_wait_reset() - wait for reset gpu completed
173 * @adev: amdgpu device.
174 * Wait for GPU reset completed.
175 * Return: Zero if reset success, otherwise will return error.
177 int amdgpu_virt_wait_reset(struct amdgpu_device *adev)
179 struct amdgpu_virt *virt = &adev->virt;
181 if (!virt->ops || !virt->ops->wait_reset)
184 return virt->ops->wait_reset(adev);
188 * amdgpu_virt_alloc_mm_table() - alloc memory for mm table
189 * @adev: amdgpu device.
190 * MM table is used by UVD and VCE for its initialization
191 * Return: Zero if allocate success.
193 int amdgpu_virt_alloc_mm_table(struct amdgpu_device *adev)
197 if (!amdgpu_sriov_vf(adev) || adev->virt.mm_table.gpu_addr)
200 r = amdgpu_bo_create_kernel(adev, PAGE_SIZE, PAGE_SIZE,
201 AMDGPU_GEM_DOMAIN_VRAM |
202 AMDGPU_GEM_DOMAIN_GTT,
203 &adev->virt.mm_table.bo,
204 &adev->virt.mm_table.gpu_addr,
205 (void *)&adev->virt.mm_table.cpu_addr);
207 DRM_ERROR("failed to alloc mm table and error = %d.\n", r);
211 memset((void *)adev->virt.mm_table.cpu_addr, 0, PAGE_SIZE);
212 DRM_INFO("MM table gpu addr = 0x%llx, cpu addr = %p.\n",
213 adev->virt.mm_table.gpu_addr,
214 adev->virt.mm_table.cpu_addr);
219 * amdgpu_virt_free_mm_table() - free mm table memory
220 * @adev: amdgpu device.
221 * Free MM table memory
223 void amdgpu_virt_free_mm_table(struct amdgpu_device *adev)
225 if (!amdgpu_sriov_vf(adev) || !adev->virt.mm_table.gpu_addr)
228 amdgpu_bo_free_kernel(&adev->virt.mm_table.bo,
229 &adev->virt.mm_table.gpu_addr,
230 (void *)&adev->virt.mm_table.cpu_addr);
231 adev->virt.mm_table.gpu_addr = 0;
235 * amdgpu_virt_rcvd_ras_interrupt() - receive ras interrupt
236 * @adev: amdgpu device.
237 * Check whether host sent RAS error message
238 * Return: true if found, otherwise false
240 bool amdgpu_virt_rcvd_ras_interrupt(struct amdgpu_device *adev)
242 struct amdgpu_virt *virt = &adev->virt;
244 if (!virt->ops || !virt->ops->rcvd_ras_intr)
247 return virt->ops->rcvd_ras_intr(adev);
251 unsigned int amd_sriov_msg_checksum(void *obj,
252 unsigned long obj_size,
254 unsigned int checksum)
256 unsigned int ret = key;
261 /* calculate checksum */
262 for (i = 0; i < obj_size; ++i)
264 /* minus the checksum itself */
265 pos = (char *)&checksum;
266 for (i = 0; i < sizeof(checksum); ++i)
271 static int amdgpu_virt_init_ras_err_handler_data(struct amdgpu_device *adev)
273 struct amdgpu_virt *virt = &adev->virt;
274 struct amdgpu_virt_ras_err_handler_data **data = &virt->virt_eh_data;
275 /* GPU will be marked bad on host if bp count more then 10,
276 * so alloc 512 is enough.
278 unsigned int align_space = 512;
280 struct amdgpu_bo **bps_bo = NULL;
282 *data = kmalloc(sizeof(struct amdgpu_virt_ras_err_handler_data), GFP_KERNEL);
286 bps = kmalloc_array(align_space, sizeof(*(*data)->bps), GFP_KERNEL);
290 bps_bo = kmalloc_array(align_space, sizeof(*(*data)->bps_bo), GFP_KERNEL);
295 (*data)->bps_bo = bps_bo;
297 (*data)->last_reserved = 0;
299 virt->ras_init_done = true;
311 static void amdgpu_virt_ras_release_bp(struct amdgpu_device *adev)
313 struct amdgpu_virt *virt = &adev->virt;
314 struct amdgpu_virt_ras_err_handler_data *data = virt->virt_eh_data;
315 struct amdgpu_bo *bo;
321 for (i = data->last_reserved - 1; i >= 0; i--) {
322 bo = data->bps_bo[i];
324 amdgpu_bo_free_kernel(&bo, NULL, NULL);
325 data->bps_bo[i] = bo;
327 data->last_reserved = i;
331 void amdgpu_virt_release_ras_err_handler_data(struct amdgpu_device *adev)
333 struct amdgpu_virt *virt = &adev->virt;
334 struct amdgpu_virt_ras_err_handler_data *data = virt->virt_eh_data;
336 virt->ras_init_done = false;
341 amdgpu_virt_ras_release_bp(adev);
346 virt->virt_eh_data = NULL;
349 static void amdgpu_virt_ras_add_bps(struct amdgpu_device *adev,
350 struct eeprom_table_record *bps, int pages)
352 struct amdgpu_virt *virt = &adev->virt;
353 struct amdgpu_virt_ras_err_handler_data *data = virt->virt_eh_data;
358 memcpy(&data->bps[data->count], bps, pages * sizeof(*data->bps));
359 data->count += pages;
362 static void amdgpu_virt_ras_reserve_bps(struct amdgpu_device *adev)
364 struct amdgpu_virt *virt = &adev->virt;
365 struct amdgpu_virt_ras_err_handler_data *data = virt->virt_eh_data;
366 struct amdgpu_vram_mgr *mgr = &adev->mman.vram_mgr;
367 struct ttm_resource_manager *man = &mgr->manager;
368 struct amdgpu_bo *bo = NULL;
375 for (i = data->last_reserved; i < data->count; i++) {
376 bp = data->bps[i].retired_page;
378 /* There are two cases of reserve error should be ignored:
379 * 1) a ras bad page has been allocated (used by someone);
380 * 2) a ras bad page has been reserved (duplicate error injection
383 if (ttm_resource_manager_used(man)) {
384 amdgpu_vram_mgr_reserve_range(&adev->mman.vram_mgr,
385 bp << AMDGPU_GPU_PAGE_SHIFT,
386 AMDGPU_GPU_PAGE_SIZE);
387 data->bps_bo[i] = NULL;
389 if (amdgpu_bo_create_kernel_at(adev, bp << AMDGPU_GPU_PAGE_SHIFT,
390 AMDGPU_GPU_PAGE_SIZE,
392 DRM_DEBUG("RAS WARN: reserve vram for retired page %llx fail\n", bp);
393 data->bps_bo[i] = bo;
395 data->last_reserved = i + 1;
400 static bool amdgpu_virt_ras_check_bad_page(struct amdgpu_device *adev,
401 uint64_t retired_page)
403 struct amdgpu_virt *virt = &adev->virt;
404 struct amdgpu_virt_ras_err_handler_data *data = virt->virt_eh_data;
410 for (i = 0; i < data->count; i++)
411 if (retired_page == data->bps[i].retired_page)
417 static void amdgpu_virt_add_bad_page(struct amdgpu_device *adev,
418 uint64_t bp_block_offset, uint32_t bp_block_size)
420 struct eeprom_table_record bp;
421 uint64_t retired_page;
422 uint32_t bp_idx, bp_cnt;
423 void *vram_usage_va = NULL;
425 if (adev->mman.fw_vram_usage_va)
426 vram_usage_va = adev->mman.fw_vram_usage_va;
428 vram_usage_va = adev->mman.drv_vram_usage_va;
430 memset(&bp, 0, sizeof(bp));
433 bp_cnt = bp_block_size / sizeof(uint64_t);
434 for (bp_idx = 0; bp_idx < bp_cnt; bp_idx++) {
435 retired_page = *(uint64_t *)(vram_usage_va +
436 bp_block_offset + bp_idx * sizeof(uint64_t));
437 bp.retired_page = retired_page;
439 if (amdgpu_virt_ras_check_bad_page(adev, retired_page))
442 amdgpu_virt_ras_add_bps(adev, &bp, 1);
444 amdgpu_virt_ras_reserve_bps(adev);
449 static int amdgpu_virt_read_pf2vf_data(struct amdgpu_device *adev)
451 struct amd_sriov_msg_pf2vf_info_header *pf2vf_info = adev->virt.fw_reserve.p_pf2vf;
458 if (adev->virt.fw_reserve.p_pf2vf == NULL)
461 if (pf2vf_info->size > 1024) {
462 dev_err(adev->dev, "invalid pf2vf message size: 0x%x\n", pf2vf_info->size);
466 switch (pf2vf_info->version) {
468 checksum = ((struct amdgim_pf2vf_info_v1 *)pf2vf_info)->checksum;
469 checkval = amd_sriov_msg_checksum(
470 adev->virt.fw_reserve.p_pf2vf, pf2vf_info->size,
471 adev->virt.fw_reserve.checksum_key, checksum);
472 if (checksum != checkval) {
474 "invalid pf2vf message: header checksum=0x%x calculated checksum=0x%x\n",
479 adev->virt.gim_feature =
480 ((struct amdgim_pf2vf_info_v1 *)pf2vf_info)->feature_flags;
483 /* TODO: missing key, need to add it later */
484 checksum = ((struct amd_sriov_msg_pf2vf_info *)pf2vf_info)->checksum;
485 checkval = amd_sriov_msg_checksum(
486 adev->virt.fw_reserve.p_pf2vf, pf2vf_info->size,
488 if (checksum != checkval) {
490 "invalid pf2vf message: header checksum=0x%x calculated checksum=0x%x\n",
495 adev->virt.vf2pf_update_interval_ms =
496 ((struct amd_sriov_msg_pf2vf_info *)pf2vf_info)->vf2pf_update_interval_ms;
497 adev->virt.gim_feature =
498 ((struct amd_sriov_msg_pf2vf_info *)pf2vf_info)->feature_flags.all;
499 adev->virt.reg_access =
500 ((struct amd_sriov_msg_pf2vf_info *)pf2vf_info)->reg_access_flags.all;
502 adev->virt.decode_max_dimension_pixels = 0;
503 adev->virt.decode_max_frame_pixels = 0;
504 adev->virt.encode_max_dimension_pixels = 0;
505 adev->virt.encode_max_frame_pixels = 0;
506 adev->virt.is_mm_bw_enabled = false;
507 for (i = 0; i < AMD_SRIOV_MSG_RESERVE_VCN_INST; i++) {
508 tmp = ((struct amd_sriov_msg_pf2vf_info *)pf2vf_info)->mm_bw_management[i].decode_max_dimension_pixels;
509 adev->virt.decode_max_dimension_pixels = max(tmp, adev->virt.decode_max_dimension_pixels);
511 tmp = ((struct amd_sriov_msg_pf2vf_info *)pf2vf_info)->mm_bw_management[i].decode_max_frame_pixels;
512 adev->virt.decode_max_frame_pixels = max(tmp, adev->virt.decode_max_frame_pixels);
514 tmp = ((struct amd_sriov_msg_pf2vf_info *)pf2vf_info)->mm_bw_management[i].encode_max_dimension_pixels;
515 adev->virt.encode_max_dimension_pixels = max(tmp, adev->virt.encode_max_dimension_pixels);
517 tmp = ((struct amd_sriov_msg_pf2vf_info *)pf2vf_info)->mm_bw_management[i].encode_max_frame_pixels;
518 adev->virt.encode_max_frame_pixels = max(tmp, adev->virt.encode_max_frame_pixels);
520 if ((adev->virt.decode_max_dimension_pixels > 0) || (adev->virt.encode_max_dimension_pixels > 0))
521 adev->virt.is_mm_bw_enabled = true;
524 ((struct amd_sriov_msg_pf2vf_info *)pf2vf_info)->uuid;
527 dev_err(adev->dev, "invalid pf2vf version: 0x%x\n", pf2vf_info->version);
531 /* correct too large or too little interval value */
532 if (adev->virt.vf2pf_update_interval_ms < 200 || adev->virt.vf2pf_update_interval_ms > 10000)
533 adev->virt.vf2pf_update_interval_ms = 2000;
538 static void amdgpu_virt_populate_vf2pf_ucode_info(struct amdgpu_device *adev)
540 struct amd_sriov_msg_vf2pf_info *vf2pf_info;
541 vf2pf_info = (struct amd_sriov_msg_vf2pf_info *) adev->virt.fw_reserve.p_vf2pf;
543 if (adev->virt.fw_reserve.p_vf2pf == NULL)
546 POPULATE_UCODE_INFO(vf2pf_info, AMD_SRIOV_UCODE_ID_VCE, adev->vce.fw_version);
547 POPULATE_UCODE_INFO(vf2pf_info, AMD_SRIOV_UCODE_ID_UVD, adev->uvd.fw_version);
548 POPULATE_UCODE_INFO(vf2pf_info, AMD_SRIOV_UCODE_ID_MC, adev->gmc.fw_version);
549 POPULATE_UCODE_INFO(vf2pf_info, AMD_SRIOV_UCODE_ID_ME, adev->gfx.me_fw_version);
550 POPULATE_UCODE_INFO(vf2pf_info, AMD_SRIOV_UCODE_ID_PFP, adev->gfx.pfp_fw_version);
551 POPULATE_UCODE_INFO(vf2pf_info, AMD_SRIOV_UCODE_ID_CE, adev->gfx.ce_fw_version);
552 POPULATE_UCODE_INFO(vf2pf_info, AMD_SRIOV_UCODE_ID_RLC, adev->gfx.rlc_fw_version);
553 POPULATE_UCODE_INFO(vf2pf_info, AMD_SRIOV_UCODE_ID_RLC_SRLC, adev->gfx.rlc_srlc_fw_version);
554 POPULATE_UCODE_INFO(vf2pf_info, AMD_SRIOV_UCODE_ID_RLC_SRLG, adev->gfx.rlc_srlg_fw_version);
555 POPULATE_UCODE_INFO(vf2pf_info, AMD_SRIOV_UCODE_ID_RLC_SRLS, adev->gfx.rlc_srls_fw_version);
556 POPULATE_UCODE_INFO(vf2pf_info, AMD_SRIOV_UCODE_ID_MEC, adev->gfx.mec_fw_version);
557 POPULATE_UCODE_INFO(vf2pf_info, AMD_SRIOV_UCODE_ID_MEC2, adev->gfx.mec2_fw_version);
558 POPULATE_UCODE_INFO(vf2pf_info, AMD_SRIOV_UCODE_ID_SOS, adev->psp.sos.fw_version);
559 POPULATE_UCODE_INFO(vf2pf_info, AMD_SRIOV_UCODE_ID_ASD,
560 adev->psp.asd_context.bin_desc.fw_version);
561 POPULATE_UCODE_INFO(vf2pf_info, AMD_SRIOV_UCODE_ID_TA_RAS,
562 adev->psp.ras_context.context.bin_desc.fw_version);
563 POPULATE_UCODE_INFO(vf2pf_info, AMD_SRIOV_UCODE_ID_TA_XGMI,
564 adev->psp.xgmi_context.context.bin_desc.fw_version);
565 POPULATE_UCODE_INFO(vf2pf_info, AMD_SRIOV_UCODE_ID_SMC, adev->pm.fw_version);
566 POPULATE_UCODE_INFO(vf2pf_info, AMD_SRIOV_UCODE_ID_SDMA, adev->sdma.instance[0].fw_version);
567 POPULATE_UCODE_INFO(vf2pf_info, AMD_SRIOV_UCODE_ID_SDMA2, adev->sdma.instance[1].fw_version);
568 POPULATE_UCODE_INFO(vf2pf_info, AMD_SRIOV_UCODE_ID_VCN, adev->vcn.fw_version);
569 POPULATE_UCODE_INFO(vf2pf_info, AMD_SRIOV_UCODE_ID_DMCU, adev->dm.dmcu_fw_version);
572 static int amdgpu_virt_write_vf2pf_data(struct amdgpu_device *adev)
574 struct amd_sriov_msg_vf2pf_info *vf2pf_info;
576 vf2pf_info = (struct amd_sriov_msg_vf2pf_info *) adev->virt.fw_reserve.p_vf2pf;
578 if (adev->virt.fw_reserve.p_vf2pf == NULL)
581 memset(vf2pf_info, 0, sizeof(struct amd_sriov_msg_vf2pf_info));
583 vf2pf_info->header.size = sizeof(struct amd_sriov_msg_vf2pf_info);
584 vf2pf_info->header.version = AMD_SRIOV_MSG_FW_VRAM_VF2PF_VER;
587 if (THIS_MODULE->version != NULL)
588 strcpy(vf2pf_info->driver_version, THIS_MODULE->version);
591 strcpy(vf2pf_info->driver_version, "N/A");
593 vf2pf_info->pf2vf_version_required = 0; // no requirement, guest understands all
594 vf2pf_info->driver_cert = 0;
595 vf2pf_info->os_info.all = 0;
597 vf2pf_info->fb_usage =
598 ttm_resource_manager_usage(&adev->mman.vram_mgr.manager) >> 20;
599 vf2pf_info->fb_vis_usage =
600 amdgpu_vram_mgr_vis_usage(&adev->mman.vram_mgr) >> 20;
601 vf2pf_info->fb_size = adev->gmc.real_vram_size >> 20;
602 vf2pf_info->fb_vis_size = adev->gmc.visible_vram_size >> 20;
604 amdgpu_virt_populate_vf2pf_ucode_info(adev);
606 /* TODO: read dynamic info */
607 vf2pf_info->gfx_usage = 0;
608 vf2pf_info->compute_usage = 0;
609 vf2pf_info->encode_usage = 0;
610 vf2pf_info->decode_usage = 0;
612 vf2pf_info->dummy_page_addr = (uint64_t)adev->dummy_page_addr;
613 vf2pf_info->mes_info_addr = (uint64_t)adev->mes.resource_1_gpu_addr;
615 if (adev->mes.resource_1) {
616 vf2pf_info->mes_info_size = adev->mes.resource_1->tbo.base.size;
618 vf2pf_info->checksum =
619 amd_sriov_msg_checksum(
620 vf2pf_info, sizeof(*vf2pf_info), 0, 0);
625 static void amdgpu_virt_update_vf2pf_work_item(struct work_struct *work)
627 struct amdgpu_device *adev = container_of(work, struct amdgpu_device, virt.vf2pf_work.work);
630 ret = amdgpu_virt_read_pf2vf_data(adev);
632 adev->virt.vf2pf_update_retry_cnt++;
634 if ((amdgpu_virt_rcvd_ras_interrupt(adev) ||
635 adev->virt.vf2pf_update_retry_cnt >= AMDGPU_VF2PF_UPDATE_MAX_RETRY_LIMIT) &&
636 amdgpu_sriov_runtime(adev)) {
638 amdgpu_ras_set_fed(adev, true);
639 if (amdgpu_reset_domain_schedule(adev->reset_domain,
640 &adev->kfd.reset_work))
643 dev_err(adev->dev, "Failed to queue work! at %s", __func__);
649 adev->virt.vf2pf_update_retry_cnt = 0;
650 amdgpu_virt_write_vf2pf_data(adev);
653 schedule_delayed_work(&(adev->virt.vf2pf_work), adev->virt.vf2pf_update_interval_ms);
656 void amdgpu_virt_fini_data_exchange(struct amdgpu_device *adev)
658 if (adev->virt.vf2pf_update_interval_ms != 0) {
659 DRM_INFO("clean up the vf2pf work item\n");
660 cancel_delayed_work_sync(&adev->virt.vf2pf_work);
661 adev->virt.vf2pf_update_interval_ms = 0;
665 void amdgpu_virt_init_data_exchange(struct amdgpu_device *adev)
667 adev->virt.fw_reserve.p_pf2vf = NULL;
668 adev->virt.fw_reserve.p_vf2pf = NULL;
669 adev->virt.vf2pf_update_interval_ms = 0;
670 adev->virt.vf2pf_update_retry_cnt = 0;
672 if (adev->mman.fw_vram_usage_va && adev->mman.drv_vram_usage_va) {
673 DRM_WARN("Currently fw_vram and drv_vram should not have values at the same time!");
674 } else if (adev->mman.fw_vram_usage_va || adev->mman.drv_vram_usage_va) {
675 /* go through this logic in ip_init and reset to init workqueue*/
676 amdgpu_virt_exchange_data(adev);
678 INIT_DELAYED_WORK(&adev->virt.vf2pf_work, amdgpu_virt_update_vf2pf_work_item);
679 schedule_delayed_work(&(adev->virt.vf2pf_work), msecs_to_jiffies(adev->virt.vf2pf_update_interval_ms));
680 } else if (adev->bios != NULL) {
681 /* got through this logic in early init stage to get necessary flags, e.g. rlcg_acc related*/
682 adev->virt.fw_reserve.p_pf2vf =
683 (struct amd_sriov_msg_pf2vf_info_header *)
684 (adev->bios + (AMD_SRIOV_MSG_PF2VF_OFFSET_KB << 10));
686 amdgpu_virt_read_pf2vf_data(adev);
691 void amdgpu_virt_exchange_data(struct amdgpu_device *adev)
693 uint64_t bp_block_offset = 0;
694 uint32_t bp_block_size = 0;
695 struct amd_sriov_msg_pf2vf_info *pf2vf_v2 = NULL;
697 if (adev->mman.fw_vram_usage_va || adev->mman.drv_vram_usage_va) {
698 if (adev->mman.fw_vram_usage_va) {
699 adev->virt.fw_reserve.p_pf2vf =
700 (struct amd_sriov_msg_pf2vf_info_header *)
701 (adev->mman.fw_vram_usage_va + (AMD_SRIOV_MSG_PF2VF_OFFSET_KB << 10));
702 adev->virt.fw_reserve.p_vf2pf =
703 (struct amd_sriov_msg_vf2pf_info_header *)
704 (adev->mman.fw_vram_usage_va + (AMD_SRIOV_MSG_VF2PF_OFFSET_KB << 10));
705 } else if (adev->mman.drv_vram_usage_va) {
706 adev->virt.fw_reserve.p_pf2vf =
707 (struct amd_sriov_msg_pf2vf_info_header *)
708 (adev->mman.drv_vram_usage_va + (AMD_SRIOV_MSG_PF2VF_OFFSET_KB << 10));
709 adev->virt.fw_reserve.p_vf2pf =
710 (struct amd_sriov_msg_vf2pf_info_header *)
711 (adev->mman.drv_vram_usage_va + (AMD_SRIOV_MSG_VF2PF_OFFSET_KB << 10));
714 amdgpu_virt_read_pf2vf_data(adev);
715 amdgpu_virt_write_vf2pf_data(adev);
717 /* bad page handling for version 2 */
718 if (adev->virt.fw_reserve.p_pf2vf->version == 2) {
719 pf2vf_v2 = (struct amd_sriov_msg_pf2vf_info *)adev->virt.fw_reserve.p_pf2vf;
721 bp_block_offset = ((uint64_t)pf2vf_v2->bp_block_offset_low & 0xFFFFFFFF) |
722 ((((uint64_t)pf2vf_v2->bp_block_offset_high) << 32) & 0xFFFFFFFF00000000);
723 bp_block_size = pf2vf_v2->bp_block_size;
725 if (bp_block_size && !adev->virt.ras_init_done)
726 amdgpu_virt_init_ras_err_handler_data(adev);
728 if (adev->virt.ras_init_done)
729 amdgpu_virt_add_bad_page(adev, bp_block_offset, bp_block_size);
734 void amdgpu_detect_virtualization(struct amdgpu_device *adev)
738 switch (adev->asic_type) {
741 reg = RREG32(mmBIF_IOV_FUNC_IDENTIFIER);
747 case CHIP_SIENNA_CICHLID:
750 case CHIP_IP_DISCOVERY:
751 reg = RREG32(mmRCC_IOV_FUNC_IDENTIFIER);
753 default: /* other chip doesn't support SRIOV */
759 adev->virt.caps |= AMDGPU_SRIOV_CAPS_IS_VF;
761 if (reg & 0x80000000)
762 adev->virt.caps |= AMDGPU_SRIOV_CAPS_ENABLE_IOV;
765 /* passthrough mode exclus sriov mod */
766 if (is_virtual_machine() && !xen_initial_domain())
767 adev->virt.caps |= AMDGPU_PASSTHROUGH_MODE;
770 /* we have the ability to check now */
771 if (amdgpu_sriov_vf(adev)) {
772 switch (adev->asic_type) {
775 vi_set_virt_ops(adev);
778 soc15_set_virt_ops(adev);
780 /* not send GPU_INIT_DATA with MS_HYPERV*/
781 if (!hypervisor_is_type(X86_HYPER_MS_HYPERV))
783 /* send a dummy GPU_INIT_DATA request to host on vega10 */
784 amdgpu_virt_request_init_data(adev);
789 soc15_set_virt_ops(adev);
793 case CHIP_SIENNA_CICHLID:
794 case CHIP_IP_DISCOVERY:
795 nv_set_virt_ops(adev);
796 /* try send GPU_INIT_DATA request to host */
797 amdgpu_virt_request_init_data(adev);
799 default: /* other chip doesn't support SRIOV */
800 DRM_ERROR("Unknown asic type: %d!\n", adev->asic_type);
806 static bool amdgpu_virt_access_debugfs_is_mmio(struct amdgpu_device *adev)
808 return amdgpu_sriov_is_debug(adev) ? true : false;
811 static bool amdgpu_virt_access_debugfs_is_kiq(struct amdgpu_device *adev)
813 return amdgpu_sriov_is_normal(adev) ? true : false;
816 int amdgpu_virt_enable_access_debugfs(struct amdgpu_device *adev)
818 if (!amdgpu_sriov_vf(adev) ||
819 amdgpu_virt_access_debugfs_is_kiq(adev))
822 if (amdgpu_virt_access_debugfs_is_mmio(adev))
823 adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME;
830 void amdgpu_virt_disable_access_debugfs(struct amdgpu_device *adev)
832 if (amdgpu_sriov_vf(adev))
833 adev->virt.caps |= AMDGPU_SRIOV_CAPS_RUNTIME;
836 enum amdgpu_sriov_vf_mode amdgpu_virt_get_sriov_vf_mode(struct amdgpu_device *adev)
838 enum amdgpu_sriov_vf_mode mode;
840 if (amdgpu_sriov_vf(adev)) {
841 if (amdgpu_sriov_is_pp_one_vf(adev))
842 mode = SRIOV_VF_MODE_ONE_VF;
844 mode = SRIOV_VF_MODE_MULTI_VF;
846 mode = SRIOV_VF_MODE_BARE_METAL;
852 void amdgpu_virt_post_reset(struct amdgpu_device *adev)
854 if (amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(11, 0, 3)) {
855 /* force set to GFXOFF state after reset,
856 * to avoid some invalid operation before GC enable
858 adev->gfx.is_poweron = false;
861 adev->mes.ring.sched.ready = false;
864 bool amdgpu_virt_fw_load_skip_check(struct amdgpu_device *adev, uint32_t ucode_id)
866 switch (amdgpu_ip_version(adev, MP0_HWIP, 0)) {
867 case IP_VERSION(13, 0, 0):
868 /* no vf autoload, white list */
869 if (ucode_id == AMDGPU_UCODE_ID_VCN1 ||
870 ucode_id == AMDGPU_UCODE_ID_VCN)
874 case IP_VERSION(11, 0, 9):
875 case IP_VERSION(11, 0, 7):
876 /* black list for CHIP_NAVI12 and CHIP_SIENNA_CICHLID */
877 if (ucode_id == AMDGPU_UCODE_ID_RLC_G
878 || ucode_id == AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL
879 || ucode_id == AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM
880 || ucode_id == AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM
881 || ucode_id == AMDGPU_UCODE_ID_SMC)
885 case IP_VERSION(13, 0, 10):
887 if (ucode_id == AMDGPU_UCODE_ID_CAP
888 || ucode_id == AMDGPU_UCODE_ID_CP_RS64_PFP
889 || ucode_id == AMDGPU_UCODE_ID_CP_RS64_ME
890 || ucode_id == AMDGPU_UCODE_ID_CP_RS64_MEC
891 || ucode_id == AMDGPU_UCODE_ID_CP_RS64_PFP_P0_STACK
892 || ucode_id == AMDGPU_UCODE_ID_CP_RS64_PFP_P1_STACK
893 || ucode_id == AMDGPU_UCODE_ID_CP_RS64_ME_P0_STACK
894 || ucode_id == AMDGPU_UCODE_ID_CP_RS64_ME_P1_STACK
895 || ucode_id == AMDGPU_UCODE_ID_CP_RS64_MEC_P0_STACK
896 || ucode_id == AMDGPU_UCODE_ID_CP_RS64_MEC_P1_STACK
897 || ucode_id == AMDGPU_UCODE_ID_CP_RS64_MEC_P2_STACK
898 || ucode_id == AMDGPU_UCODE_ID_CP_RS64_MEC_P3_STACK
899 || ucode_id == AMDGPU_UCODE_ID_CP_MES
900 || ucode_id == AMDGPU_UCODE_ID_CP_MES_DATA
901 || ucode_id == AMDGPU_UCODE_ID_CP_MES1
902 || ucode_id == AMDGPU_UCODE_ID_CP_MES1_DATA
903 || ucode_id == AMDGPU_UCODE_ID_VCN1
904 || ucode_id == AMDGPU_UCODE_ID_VCN)
909 /* lagacy black list */
910 if (ucode_id == AMDGPU_UCODE_ID_SDMA0
911 || ucode_id == AMDGPU_UCODE_ID_SDMA1
912 || ucode_id == AMDGPU_UCODE_ID_SDMA2
913 || ucode_id == AMDGPU_UCODE_ID_SDMA3
914 || ucode_id == AMDGPU_UCODE_ID_SDMA4
915 || ucode_id == AMDGPU_UCODE_ID_SDMA5
916 || ucode_id == AMDGPU_UCODE_ID_SDMA6
917 || ucode_id == AMDGPU_UCODE_ID_SDMA7
918 || ucode_id == AMDGPU_UCODE_ID_RLC_G
919 || ucode_id == AMDGPU_UCODE_ID_RLC_RESTORE_LIST_CNTL
920 || ucode_id == AMDGPU_UCODE_ID_RLC_RESTORE_LIST_GPM_MEM
921 || ucode_id == AMDGPU_UCODE_ID_RLC_RESTORE_LIST_SRM_MEM
922 || ucode_id == AMDGPU_UCODE_ID_SMC)
929 void amdgpu_virt_update_sriov_video_codec(struct amdgpu_device *adev,
930 struct amdgpu_video_codec_info *encode, uint32_t encode_array_size,
931 struct amdgpu_video_codec_info *decode, uint32_t decode_array_size)
935 if (!adev->virt.is_mm_bw_enabled)
939 for (i = 0; i < encode_array_size; i++) {
940 encode[i].max_width = adev->virt.encode_max_dimension_pixels;
941 encode[i].max_pixels_per_frame = adev->virt.encode_max_frame_pixels;
942 if (encode[i].max_width > 0)
943 encode[i].max_height = encode[i].max_pixels_per_frame / encode[i].max_width;
945 encode[i].max_height = 0;
950 for (i = 0; i < decode_array_size; i++) {
951 decode[i].max_width = adev->virt.decode_max_dimension_pixels;
952 decode[i].max_pixels_per_frame = adev->virt.decode_max_frame_pixels;
953 if (decode[i].max_width > 0)
954 decode[i].max_height = decode[i].max_pixels_per_frame / decode[i].max_width;
956 decode[i].max_height = 0;
961 bool amdgpu_virt_get_rlcg_reg_access_flag(struct amdgpu_device *adev,
962 u32 acc_flags, u32 hwip,
963 bool write, u32 *rlcg_flag)
969 if (amdgpu_sriov_reg_indirect_gc(adev)) {
971 write ? AMDGPU_RLCG_GC_WRITE : AMDGPU_RLCG_GC_READ;
973 /* only in new version, AMDGPU_REGS_NO_KIQ and
974 * AMDGPU_REGS_RLC are enabled simultaneously */
975 } else if ((acc_flags & AMDGPU_REGS_RLC) &&
976 !(acc_flags & AMDGPU_REGS_NO_KIQ) && write) {
977 *rlcg_flag = AMDGPU_RLCG_GC_WRITE_LEGACY;
982 if (amdgpu_sriov_reg_indirect_mmhub(adev) &&
983 (acc_flags & AMDGPU_REGS_RLC) && write) {
984 *rlcg_flag = AMDGPU_RLCG_MMHUB_WRITE;
994 u32 amdgpu_virt_rlcg_reg_rw(struct amdgpu_device *adev, u32 offset, u32 v, u32 flag, u32 xcc_id)
996 struct amdgpu_rlcg_reg_access_ctrl *reg_access_ctrl;
997 uint32_t timeout = 50000;
1006 if (!adev->gfx.rlc.rlcg_reg_access_supported) {
1008 "indirect registers access through rlcg is not available\n");
1012 if (adev->gfx.xcc_mask && (((1 << xcc_id) & adev->gfx.xcc_mask) == 0)) {
1013 dev_err(adev->dev, "invalid xcc\n");
1017 if (amdgpu_device_skip_hw_access(adev))
1020 reg_access_ctrl = &adev->gfx.rlc.reg_access_ctrl[xcc_id];
1021 scratch_reg0 = (void __iomem *)adev->rmmio + 4 * reg_access_ctrl->scratch_reg0;
1022 scratch_reg1 = (void __iomem *)adev->rmmio + 4 * reg_access_ctrl->scratch_reg1;
1023 scratch_reg2 = (void __iomem *)adev->rmmio + 4 * reg_access_ctrl->scratch_reg2;
1024 scratch_reg3 = (void __iomem *)adev->rmmio + 4 * reg_access_ctrl->scratch_reg3;
1026 mutex_lock(&adev->virt.rlcg_reg_lock);
1028 if (reg_access_ctrl->spare_int)
1029 spare_int = (void __iomem *)adev->rmmio + 4 * reg_access_ctrl->spare_int;
1031 if (offset == reg_access_ctrl->grbm_cntl) {
1032 /* if the target reg offset is grbm_cntl, write to scratch_reg2 */
1033 writel(v, scratch_reg2);
1034 if (flag == AMDGPU_RLCG_GC_WRITE_LEGACY)
1035 writel(v, ((void __iomem *)adev->rmmio) + (offset * 4));
1036 } else if (offset == reg_access_ctrl->grbm_idx) {
1037 /* if the target reg offset is grbm_idx, write to scratch_reg3 */
1038 writel(v, scratch_reg3);
1039 if (flag == AMDGPU_RLCG_GC_WRITE_LEGACY)
1040 writel(v, ((void __iomem *)adev->rmmio) + (offset * 4));
1043 * SCRATCH_REG0 = read/write value
1044 * SCRATCH_REG1[30:28] = command
1045 * SCRATCH_REG1[19:0] = address in dword
1046 * SCRATCH_REG1[27:24] = Error reporting
1048 writel(v, scratch_reg0);
1049 writel((offset | flag), scratch_reg1);
1050 if (reg_access_ctrl->spare_int)
1051 writel(1, spare_int);
1053 for (i = 0; i < timeout; i++) {
1054 tmp = readl(scratch_reg1);
1055 if (!(tmp & AMDGPU_RLCG_SCRATCH1_ADDRESS_MASK))
1060 tmp = readl(scratch_reg1);
1061 if (i >= timeout || (tmp & AMDGPU_RLCG_SCRATCH1_ERROR_MASK) != 0) {
1062 if (amdgpu_sriov_rlcg_error_report_enabled(adev)) {
1063 if (tmp & AMDGPU_RLCG_VFGATE_DISABLED) {
1065 "vfgate is disabled, rlcg failed to program reg: 0x%05x\n", offset);
1066 } else if (tmp & AMDGPU_RLCG_WRONG_OPERATION_TYPE) {
1068 "wrong operation type, rlcg failed to program reg: 0x%05x\n", offset);
1069 } else if (tmp & AMDGPU_RLCG_REG_NOT_IN_RANGE) {
1071 "register is not in range, rlcg failed to program reg: 0x%05x\n", offset);
1074 "unknown error type, rlcg failed to program reg: 0x%05x\n", offset);
1078 "timeout: rlcg faled to program reg: 0x%05x\n", offset);
1083 ret = readl(scratch_reg0);
1085 mutex_unlock(&adev->virt.rlcg_reg_lock);
1090 void amdgpu_sriov_wreg(struct amdgpu_device *adev,
1091 u32 offset, u32 value,
1092 u32 acc_flags, u32 hwip, u32 xcc_id)
1096 if (amdgpu_device_skip_hw_access(adev))
1099 if (!amdgpu_sriov_runtime(adev) &&
1100 amdgpu_virt_get_rlcg_reg_access_flag(adev, acc_flags, hwip, true, &rlcg_flag)) {
1101 amdgpu_virt_rlcg_reg_rw(adev, offset, value, rlcg_flag, xcc_id);
1105 if (acc_flags & AMDGPU_REGS_NO_KIQ)
1106 WREG32_NO_KIQ(offset, value);
1108 WREG32(offset, value);
1111 u32 amdgpu_sriov_rreg(struct amdgpu_device *adev,
1112 u32 offset, u32 acc_flags, u32 hwip, u32 xcc_id)
1116 if (amdgpu_device_skip_hw_access(adev))
1119 if (!amdgpu_sriov_runtime(adev) &&
1120 amdgpu_virt_get_rlcg_reg_access_flag(adev, acc_flags, hwip, false, &rlcg_flag))
1121 return amdgpu_virt_rlcg_reg_rw(adev, offset, 0, rlcg_flag, xcc_id);
1123 if (acc_flags & AMDGPU_REGS_NO_KIQ)
1124 return RREG32_NO_KIQ(offset);
1126 return RREG32(offset);
1129 bool amdgpu_sriov_xnack_support(struct amdgpu_device *adev)
1131 bool xnack_mode = true;
1133 if (amdgpu_sriov_vf(adev) &&
1134 amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(9, 4, 2))