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/firmware.h>
25 #include <linux/pci.h>
27 #include <drm/drm_cache.h>
31 #include "amdgpu_atomfirmware.h"
32 #include "amdgpu_gem.h"
34 #include "gc/gc_9_0_sh_mask.h"
35 #include "dce/dce_12_0_offset.h"
36 #include "dce/dce_12_0_sh_mask.h"
37 #include "vega10_enum.h"
38 #include "mmhub/mmhub_1_0_offset.h"
39 #include "athub/athub_1_0_sh_mask.h"
40 #include "athub/athub_1_0_offset.h"
41 #include "oss/osssys_4_0_offset.h"
45 #include "soc15_common.h"
46 #include "umc/umc_6_0_sh_mask.h"
48 #include "gfxhub_v1_0.h"
49 #include "mmhub_v1_0.h"
50 #include "athub_v1_0.h"
51 #include "gfxhub_v1_1.h"
52 #include "gfxhub_v1_2.h"
53 #include "mmhub_v9_4.h"
54 #include "mmhub_v1_7.h"
55 #include "mmhub_v1_8.h"
62 #include "ivsrcid/vmc/irqsrcs_vmc_1_0.h"
64 #include "amdgpu_ras.h"
65 #include "amdgpu_xgmi.h"
67 #include "amdgpu_reset.h"
69 /* add these here since we already include dce12 headers and these are for DCN */
70 #define mmHUBP0_DCSURF_PRI_VIEWPORT_DIMENSION 0x055d
71 #define mmHUBP0_DCSURF_PRI_VIEWPORT_DIMENSION_BASE_IDX 2
72 #define HUBP0_DCSURF_PRI_VIEWPORT_DIMENSION__PRI_VIEWPORT_WIDTH__SHIFT 0x0
73 #define HUBP0_DCSURF_PRI_VIEWPORT_DIMENSION__PRI_VIEWPORT_HEIGHT__SHIFT 0x10
74 #define HUBP0_DCSURF_PRI_VIEWPORT_DIMENSION__PRI_VIEWPORT_WIDTH_MASK 0x00003FFFL
75 #define HUBP0_DCSURF_PRI_VIEWPORT_DIMENSION__PRI_VIEWPORT_HEIGHT_MASK 0x3FFF0000L
76 #define mmDCHUBBUB_SDPIF_MMIO_CNTRL_0 0x049d
77 #define mmDCHUBBUB_SDPIF_MMIO_CNTRL_0_BASE_IDX 2
79 #define mmHUBP0_DCSURF_PRI_VIEWPORT_DIMENSION_DCN2 0x05ea
80 #define mmHUBP0_DCSURF_PRI_VIEWPORT_DIMENSION_DCN2_BASE_IDX 2
83 static const char *gfxhub_client_ids[] = {
99 static const char *mmhub_client_ids_raven[][2] = {
124 static const char *mmhub_client_ids_renoir[][2] = {
152 static const char *mmhub_client_ids_vega10[][2] = {
165 [32+14][0] = "SDMA0",
178 [32+4][1] = "DCEDWB",
181 [32+14][1] = "SDMA1",
184 static const char *mmhub_client_ids_vega12[][2] = {
197 [32+15][0] = "SDMA0",
207 [32+1][1] = "DCEDWB",
213 [32+15][1] = "SDMA1",
216 static const char *mmhub_client_ids_vega20[][2] = {
230 [32+12][0] = "UTCL2",
231 [32+14][0] = "SDMA1",
249 [32+14][1] = "SDMA1",
252 static const char *mmhub_client_ids_arcturus[][2] = {
293 static const char *mmhub_client_ids_aldebaran[][2] = {
296 [32+1][0] = "DBGU_IO0",
297 [32+2][0] = "DBGU_IO2",
299 [96+11][0] = "JPEG0",
301 [96+13][0] = "VCNU0",
302 [128+11][0] = "JPEG1",
303 [128+12][0] = "VCN1",
304 [128+13][0] = "VCNU1",
307 [256+0][0] = "SDMA0",
308 [256+1][0] = "SDMA1",
309 [256+2][0] = "SDMA2",
310 [256+3][0] = "SDMA3",
311 [256+4][0] = "SDMA4",
315 [32+1][1] = "DBGU_IO0",
316 [32+2][1] = "DBGU_IO2",
318 [96+11][1] = "JPEG0",
320 [96+13][1] = "VCNU0",
321 [128+11][1] = "JPEG1",
322 [128+12][1] = "VCN1",
323 [128+13][1] = "VCNU1",
326 [256+0][1] = "SDMA0",
327 [256+1][1] = "SDMA1",
328 [256+2][1] = "SDMA2",
329 [256+3][1] = "SDMA3",
330 [256+4][1] = "SDMA4",
334 static const struct soc15_reg_golden golden_settings_mmhub_1_0_0[] =
336 SOC15_REG_GOLDEN_VALUE(MMHUB, 0, mmDAGB1_WRCLI2, 0x00000007, 0xfe5fe0fa),
337 SOC15_REG_GOLDEN_VALUE(MMHUB, 0, mmMMEA1_DRAM_WR_CLI2GRP_MAP0, 0x00000030, 0x55555565)
340 static const struct soc15_reg_golden golden_settings_athub_1_0_0[] =
342 SOC15_REG_GOLDEN_VALUE(ATHUB, 0, mmRPB_ARB_CNTL, 0x0000ff00, 0x00000800),
343 SOC15_REG_GOLDEN_VALUE(ATHUB, 0, mmRPB_ARB_CNTL2, 0x00ff00ff, 0x00080008)
346 static const uint32_t ecc_umc_mcumc_ctrl_addrs[] = {
347 (0x000143c0 + 0x00000000),
348 (0x000143c0 + 0x00000800),
349 (0x000143c0 + 0x00001000),
350 (0x000143c0 + 0x00001800),
351 (0x000543c0 + 0x00000000),
352 (0x000543c0 + 0x00000800),
353 (0x000543c0 + 0x00001000),
354 (0x000543c0 + 0x00001800),
355 (0x000943c0 + 0x00000000),
356 (0x000943c0 + 0x00000800),
357 (0x000943c0 + 0x00001000),
358 (0x000943c0 + 0x00001800),
359 (0x000d43c0 + 0x00000000),
360 (0x000d43c0 + 0x00000800),
361 (0x000d43c0 + 0x00001000),
362 (0x000d43c0 + 0x00001800),
363 (0x001143c0 + 0x00000000),
364 (0x001143c0 + 0x00000800),
365 (0x001143c0 + 0x00001000),
366 (0x001143c0 + 0x00001800),
367 (0x001543c0 + 0x00000000),
368 (0x001543c0 + 0x00000800),
369 (0x001543c0 + 0x00001000),
370 (0x001543c0 + 0x00001800),
371 (0x001943c0 + 0x00000000),
372 (0x001943c0 + 0x00000800),
373 (0x001943c0 + 0x00001000),
374 (0x001943c0 + 0x00001800),
375 (0x001d43c0 + 0x00000000),
376 (0x001d43c0 + 0x00000800),
377 (0x001d43c0 + 0x00001000),
378 (0x001d43c0 + 0x00001800),
381 static const uint32_t ecc_umc_mcumc_ctrl_mask_addrs[] = {
382 (0x000143e0 + 0x00000000),
383 (0x000143e0 + 0x00000800),
384 (0x000143e0 + 0x00001000),
385 (0x000143e0 + 0x00001800),
386 (0x000543e0 + 0x00000000),
387 (0x000543e0 + 0x00000800),
388 (0x000543e0 + 0x00001000),
389 (0x000543e0 + 0x00001800),
390 (0x000943e0 + 0x00000000),
391 (0x000943e0 + 0x00000800),
392 (0x000943e0 + 0x00001000),
393 (0x000943e0 + 0x00001800),
394 (0x000d43e0 + 0x00000000),
395 (0x000d43e0 + 0x00000800),
396 (0x000d43e0 + 0x00001000),
397 (0x000d43e0 + 0x00001800),
398 (0x001143e0 + 0x00000000),
399 (0x001143e0 + 0x00000800),
400 (0x001143e0 + 0x00001000),
401 (0x001143e0 + 0x00001800),
402 (0x001543e0 + 0x00000000),
403 (0x001543e0 + 0x00000800),
404 (0x001543e0 + 0x00001000),
405 (0x001543e0 + 0x00001800),
406 (0x001943e0 + 0x00000000),
407 (0x001943e0 + 0x00000800),
408 (0x001943e0 + 0x00001000),
409 (0x001943e0 + 0x00001800),
410 (0x001d43e0 + 0x00000000),
411 (0x001d43e0 + 0x00000800),
412 (0x001d43e0 + 0x00001000),
413 (0x001d43e0 + 0x00001800),
416 static int gmc_v9_0_ecc_interrupt_state(struct amdgpu_device *adev,
417 struct amdgpu_irq_src *src,
419 enum amdgpu_interrupt_state state)
421 u32 bits, i, tmp, reg;
423 /* Devices newer then VEGA10/12 shall have these programming
424 sequences performed by PSP BL */
425 if (adev->asic_type >= CHIP_VEGA20)
431 case AMDGPU_IRQ_STATE_DISABLE:
432 for (i = 0; i < ARRAY_SIZE(ecc_umc_mcumc_ctrl_addrs); i++) {
433 reg = ecc_umc_mcumc_ctrl_addrs[i];
438 for (i = 0; i < ARRAY_SIZE(ecc_umc_mcumc_ctrl_mask_addrs); i++) {
439 reg = ecc_umc_mcumc_ctrl_mask_addrs[i];
445 case AMDGPU_IRQ_STATE_ENABLE:
446 for (i = 0; i < ARRAY_SIZE(ecc_umc_mcumc_ctrl_addrs); i++) {
447 reg = ecc_umc_mcumc_ctrl_addrs[i];
452 for (i = 0; i < ARRAY_SIZE(ecc_umc_mcumc_ctrl_mask_addrs); i++) {
453 reg = ecc_umc_mcumc_ctrl_mask_addrs[i];
466 static int gmc_v9_0_vm_fault_interrupt_state(struct amdgpu_device *adev,
467 struct amdgpu_irq_src *src,
469 enum amdgpu_interrupt_state state)
471 struct amdgpu_vmhub *hub;
472 u32 tmp, reg, bits, i, j;
474 bits = VM_CONTEXT1_CNTL__RANGE_PROTECTION_FAULT_ENABLE_INTERRUPT_MASK |
475 VM_CONTEXT1_CNTL__DUMMY_PAGE_PROTECTION_FAULT_ENABLE_INTERRUPT_MASK |
476 VM_CONTEXT1_CNTL__PDE0_PROTECTION_FAULT_ENABLE_INTERRUPT_MASK |
477 VM_CONTEXT1_CNTL__VALID_PROTECTION_FAULT_ENABLE_INTERRUPT_MASK |
478 VM_CONTEXT1_CNTL__READ_PROTECTION_FAULT_ENABLE_INTERRUPT_MASK |
479 VM_CONTEXT1_CNTL__WRITE_PROTECTION_FAULT_ENABLE_INTERRUPT_MASK |
480 VM_CONTEXT1_CNTL__EXECUTE_PROTECTION_FAULT_ENABLE_INTERRUPT_MASK;
483 case AMDGPU_IRQ_STATE_DISABLE:
484 for (j = 0; j < adev->num_vmhubs; j++) {
485 hub = &adev->vmhub[j];
486 for (i = 0; i < 16; i++) {
487 reg = hub->vm_context0_cntl + i;
489 /* This works because this interrupt is only
490 * enabled at init/resume and disabled in
491 * fini/suspend, so the overall state doesn't
492 * change over the course of suspend/resume.
494 if (adev->in_s0ix && (j == AMDGPU_GFXHUB_0))
497 if (j == AMDGPU_GFXHUB_0)
498 tmp = RREG32_SOC15_IP(GC, reg);
500 tmp = RREG32_SOC15_IP(MMHUB, reg);
504 if (j == AMDGPU_GFXHUB_0)
505 WREG32_SOC15_IP(GC, reg, tmp);
507 WREG32_SOC15_IP(MMHUB, reg, tmp);
511 case AMDGPU_IRQ_STATE_ENABLE:
512 for (j = 0; j < adev->num_vmhubs; j++) {
513 hub = &adev->vmhub[j];
514 for (i = 0; i < 16; i++) {
515 reg = hub->vm_context0_cntl + i;
517 /* This works because this interrupt is only
518 * enabled at init/resume and disabled in
519 * fini/suspend, so the overall state doesn't
520 * change over the course of suspend/resume.
522 if (adev->in_s0ix && (j == AMDGPU_GFXHUB_0))
525 if (j == AMDGPU_GFXHUB_0)
526 tmp = RREG32_SOC15_IP(GC, reg);
528 tmp = RREG32_SOC15_IP(MMHUB, reg);
532 if (j == AMDGPU_GFXHUB_0)
533 WREG32_SOC15_IP(GC, reg, tmp);
535 WREG32_SOC15_IP(MMHUB, reg, tmp);
546 static int gmc_v9_0_process_interrupt(struct amdgpu_device *adev,
547 struct amdgpu_irq_src *source,
548 struct amdgpu_iv_entry *entry)
550 bool retry_fault = !!(entry->src_data[1] & 0x80);
551 bool write_fault = !!(entry->src_data[1] & 0x20);
552 uint32_t status = 0, cid = 0, rw = 0;
553 struct amdgpu_task_info task_info;
554 struct amdgpu_vmhub *hub;
555 const char *mmhub_cid;
556 const char *hub_name;
558 uint32_t cam_index = 0;
561 addr = (u64)entry->src_data[0] << 12;
562 addr |= ((u64)entry->src_data[1] & 0xf) << 44;
565 if (adev->irq.retry_cam_enabled) {
566 /* Delegate it to a different ring if the hardware hasn't
569 if (entry->ih == &adev->irq.ih) {
570 amdgpu_irq_delegate(adev, entry, 8);
574 cam_index = entry->src_data[2] & 0x3ff;
576 ret = amdgpu_vm_handle_fault(adev, entry->pasid, addr, write_fault);
577 WDOORBELL32(adev->irq.retry_cam_doorbell_index, cam_index);
581 /* Process it onyl if it's the first fault for this address */
582 if (entry->ih != &adev->irq.ih_soft &&
583 amdgpu_gmc_filter_faults(adev, entry->ih, addr, entry->pasid,
587 /* Delegate it to a different ring if the hardware hasn't
590 if (entry->ih == &adev->irq.ih) {
591 amdgpu_irq_delegate(adev, entry, 8);
595 /* Try to handle the recoverable page faults by filling page
598 if (amdgpu_vm_handle_fault(adev, entry->pasid, addr, write_fault))
603 if (!printk_ratelimit())
606 if (entry->client_id == SOC15_IH_CLIENTID_VMC) {
608 hub = &adev->vmhub[AMDGPU_MMHUB_0];
609 } else if (entry->client_id == SOC15_IH_CLIENTID_VMC1) {
611 hub = &adev->vmhub[AMDGPU_MMHUB_1];
613 hub_name = "gfxhub0";
614 hub = &adev->vmhub[AMDGPU_GFXHUB_0];
617 memset(&task_info, 0, sizeof(struct amdgpu_task_info));
618 amdgpu_vm_get_task_info(adev, entry->pasid, &task_info);
621 "[%s] %s page fault (src_id:%u ring:%u vmid:%u "
622 "pasid:%u, for process %s pid %d thread %s pid %d)\n",
623 hub_name, retry_fault ? "retry" : "no-retry",
624 entry->src_id, entry->ring_id, entry->vmid,
625 entry->pasid, task_info.process_name, task_info.tgid,
626 task_info.task_name, task_info.pid);
627 dev_err(adev->dev, " in page starting at address 0x%016llx from IH client 0x%x (%s)\n",
628 addr, entry->client_id,
629 soc15_ih_clientid_name[entry->client_id]);
631 if (amdgpu_sriov_vf(adev))
635 * Issue a dummy read to wait for the status register to
636 * be updated to avoid reading an incorrect value due to
637 * the new fast GRBM interface.
639 if ((entry->vmid_src == AMDGPU_GFXHUB_0) &&
640 (adev->ip_versions[GC_HWIP][0] < IP_VERSION(9, 4, 2)))
641 RREG32(hub->vm_l2_pro_fault_status);
643 status = RREG32(hub->vm_l2_pro_fault_status);
644 cid = REG_GET_FIELD(status, VM_L2_PROTECTION_FAULT_STATUS, CID);
645 rw = REG_GET_FIELD(status, VM_L2_PROTECTION_FAULT_STATUS, RW);
646 WREG32_P(hub->vm_l2_pro_fault_cntl, 1, ~1);
650 "VM_L2_PROTECTION_FAULT_STATUS:0x%08X\n",
652 if (hub == &adev->vmhub[AMDGPU_GFXHUB_0]) {
653 dev_err(adev->dev, "\t Faulty UTCL2 client ID: %s (0x%x)\n",
654 cid >= ARRAY_SIZE(gfxhub_client_ids) ? "unknown" :
655 gfxhub_client_ids[cid],
658 switch (adev->ip_versions[MMHUB_HWIP][0]) {
659 case IP_VERSION(9, 0, 0):
660 mmhub_cid = mmhub_client_ids_vega10[cid][rw];
662 case IP_VERSION(9, 3, 0):
663 mmhub_cid = mmhub_client_ids_vega12[cid][rw];
665 case IP_VERSION(9, 4, 0):
666 mmhub_cid = mmhub_client_ids_vega20[cid][rw];
668 case IP_VERSION(9, 4, 1):
669 mmhub_cid = mmhub_client_ids_arcturus[cid][rw];
671 case IP_VERSION(9, 1, 0):
672 case IP_VERSION(9, 2, 0):
673 mmhub_cid = mmhub_client_ids_raven[cid][rw];
675 case IP_VERSION(1, 5, 0):
676 case IP_VERSION(2, 4, 0):
677 mmhub_cid = mmhub_client_ids_renoir[cid][rw];
679 case IP_VERSION(1, 8, 0):
680 case IP_VERSION(9, 4, 2):
681 mmhub_cid = mmhub_client_ids_aldebaran[cid][rw];
687 dev_err(adev->dev, "\t Faulty UTCL2 client ID: %s (0x%x)\n",
688 mmhub_cid ? mmhub_cid : "unknown", cid);
690 dev_err(adev->dev, "\t MORE_FAULTS: 0x%lx\n",
691 REG_GET_FIELD(status,
692 VM_L2_PROTECTION_FAULT_STATUS, MORE_FAULTS));
693 dev_err(adev->dev, "\t WALKER_ERROR: 0x%lx\n",
694 REG_GET_FIELD(status,
695 VM_L2_PROTECTION_FAULT_STATUS, WALKER_ERROR));
696 dev_err(adev->dev, "\t PERMISSION_FAULTS: 0x%lx\n",
697 REG_GET_FIELD(status,
698 VM_L2_PROTECTION_FAULT_STATUS, PERMISSION_FAULTS));
699 dev_err(adev->dev, "\t MAPPING_ERROR: 0x%lx\n",
700 REG_GET_FIELD(status,
701 VM_L2_PROTECTION_FAULT_STATUS, MAPPING_ERROR));
702 dev_err(adev->dev, "\t RW: 0x%x\n", rw);
706 static const struct amdgpu_irq_src_funcs gmc_v9_0_irq_funcs = {
707 .set = gmc_v9_0_vm_fault_interrupt_state,
708 .process = gmc_v9_0_process_interrupt,
712 static const struct amdgpu_irq_src_funcs gmc_v9_0_ecc_funcs = {
713 .set = gmc_v9_0_ecc_interrupt_state,
714 .process = amdgpu_umc_process_ecc_irq,
717 static void gmc_v9_0_set_irq_funcs(struct amdgpu_device *adev)
719 adev->gmc.vm_fault.num_types = 1;
720 adev->gmc.vm_fault.funcs = &gmc_v9_0_irq_funcs;
722 if (!amdgpu_sriov_vf(adev) &&
723 !adev->gmc.xgmi.connected_to_cpu) {
724 adev->gmc.ecc_irq.num_types = 1;
725 adev->gmc.ecc_irq.funcs = &gmc_v9_0_ecc_funcs;
729 static uint32_t gmc_v9_0_get_invalidate_req(unsigned int vmid,
734 req = REG_SET_FIELD(req, VM_INVALIDATE_ENG0_REQ,
735 PER_VMID_INVALIDATE_REQ, 1 << vmid);
736 req = REG_SET_FIELD(req, VM_INVALIDATE_ENG0_REQ, FLUSH_TYPE, flush_type);
737 req = REG_SET_FIELD(req, VM_INVALIDATE_ENG0_REQ, INVALIDATE_L2_PTES, 1);
738 req = REG_SET_FIELD(req, VM_INVALIDATE_ENG0_REQ, INVALIDATE_L2_PDE0, 1);
739 req = REG_SET_FIELD(req, VM_INVALIDATE_ENG0_REQ, INVALIDATE_L2_PDE1, 1);
740 req = REG_SET_FIELD(req, VM_INVALIDATE_ENG0_REQ, INVALIDATE_L2_PDE2, 1);
741 req = REG_SET_FIELD(req, VM_INVALIDATE_ENG0_REQ, INVALIDATE_L1_PTES, 1);
742 req = REG_SET_FIELD(req, VM_INVALIDATE_ENG0_REQ,
743 CLEAR_PROTECTION_FAULT_STATUS_ADDR, 0);
749 * gmc_v9_0_use_invalidate_semaphore - judge whether to use semaphore
751 * @adev: amdgpu_device pointer
755 static bool gmc_v9_0_use_invalidate_semaphore(struct amdgpu_device *adev,
758 if (adev->ip_versions[GC_HWIP][0] == IP_VERSION(9, 4, 2) ||
759 adev->ip_versions[GC_HWIP][0] == IP_VERSION(9, 4, 3))
762 return ((vmhub == AMDGPU_MMHUB_0 ||
763 vmhub == AMDGPU_MMHUB_1) &&
764 (!amdgpu_sriov_vf(adev)) &&
765 (!(!(adev->apu_flags & AMD_APU_IS_RAVEN2) &&
766 (adev->apu_flags & AMD_APU_IS_PICASSO))));
769 static bool gmc_v9_0_get_atc_vmid_pasid_mapping_info(struct amdgpu_device *adev,
770 uint8_t vmid, uint16_t *p_pasid)
774 value = RREG32(SOC15_REG_OFFSET(ATHUB, 0, mmATC_VMID0_PASID_MAPPING)
776 *p_pasid = value & ATC_VMID0_PASID_MAPPING__PASID_MASK;
778 return !!(value & ATC_VMID0_PASID_MAPPING__VALID_MASK);
783 * VMID 0 is the physical GPU addresses as used by the kernel.
784 * VMIDs 1-15 are used for userspace clients and are handled
785 * by the amdgpu vm/hsa code.
789 * gmc_v9_0_flush_gpu_tlb - tlb flush with certain type
791 * @adev: amdgpu_device pointer
792 * @vmid: vm instance to flush
793 * @vmhub: which hub to flush
794 * @flush_type: the flush type
796 * Flush the TLB for the requested page table using certain type.
798 static void gmc_v9_0_flush_gpu_tlb(struct amdgpu_device *adev, uint32_t vmid,
799 uint32_t vmhub, uint32_t flush_type)
801 bool use_semaphore = gmc_v9_0_use_invalidate_semaphore(adev, vmhub);
802 const unsigned eng = 17;
803 u32 j, inv_req, inv_req2, tmp;
804 struct amdgpu_vmhub *hub;
806 BUG_ON(vmhub >= adev->num_vmhubs);
808 hub = &adev->vmhub[vmhub];
809 if (adev->gmc.xgmi.num_physical_nodes &&
810 adev->ip_versions[GC_HWIP][0] == IP_VERSION(9, 4, 0)) {
811 /* Vega20+XGMI caches PTEs in TC and TLB. Add a
812 * heavy-weight TLB flush (type 2), which flushes
813 * both. Due to a race condition with concurrent
814 * memory accesses using the same TLB cache line, we
815 * still need a second TLB flush after this.
817 inv_req = gmc_v9_0_get_invalidate_req(vmid, 2);
818 inv_req2 = gmc_v9_0_get_invalidate_req(vmid, flush_type);
820 inv_req = gmc_v9_0_get_invalidate_req(vmid, flush_type);
824 /* This is necessary for a HW workaround under SRIOV as well
825 * as GFXOFF under bare metal
827 if (adev->gfx.kiq.ring.sched.ready &&
828 (amdgpu_sriov_runtime(adev) || !amdgpu_sriov_vf(adev)) &&
829 down_read_trylock(&adev->reset_domain->sem)) {
830 uint32_t req = hub->vm_inv_eng0_req + hub->eng_distance * eng;
831 uint32_t ack = hub->vm_inv_eng0_ack + hub->eng_distance * eng;
833 amdgpu_virt_kiq_reg_write_reg_wait(adev, req, ack, inv_req,
835 up_read(&adev->reset_domain->sem);
839 spin_lock(&adev->gmc.invalidate_lock);
842 * It may lose gpuvm invalidate acknowldege state across power-gating
843 * off cycle, add semaphore acquire before invalidation and semaphore
844 * release after invalidation to avoid entering power gated state
848 /* TODO: It needs to continue working on debugging with semaphore for GFXHUB as well. */
850 for (j = 0; j < adev->usec_timeout; j++) {
851 /* a read return value of 1 means semaphore acquire */
852 if (vmhub == AMDGPU_GFXHUB_0)
853 tmp = RREG32_SOC15_IP_NO_KIQ(GC, hub->vm_inv_eng0_sem + hub->eng_distance * eng);
855 tmp = RREG32_SOC15_IP_NO_KIQ(MMHUB, hub->vm_inv_eng0_sem + hub->eng_distance * eng);
862 if (j >= adev->usec_timeout)
863 DRM_ERROR("Timeout waiting for sem acquire in VM flush!\n");
867 if (vmhub == AMDGPU_GFXHUB_0)
868 WREG32_SOC15_IP_NO_KIQ(GC, hub->vm_inv_eng0_req + hub->eng_distance * eng, inv_req);
870 WREG32_SOC15_IP_NO_KIQ(MMHUB, hub->vm_inv_eng0_req + hub->eng_distance * eng, inv_req);
873 * Issue a dummy read to wait for the ACK register to
874 * be cleared to avoid a false ACK due to the new fast
877 if ((vmhub == AMDGPU_GFXHUB_0) &&
878 (adev->ip_versions[GC_HWIP][0] < IP_VERSION(9, 4, 2)))
879 RREG32_NO_KIQ(hub->vm_inv_eng0_req +
880 hub->eng_distance * eng);
882 for (j = 0; j < adev->usec_timeout; j++) {
883 if (vmhub == AMDGPU_GFXHUB_0)
884 tmp = RREG32_SOC15_IP_NO_KIQ(GC, hub->vm_inv_eng0_ack + hub->eng_distance * eng);
886 tmp = RREG32_SOC15_IP_NO_KIQ(MMHUB, hub->vm_inv_eng0_ack + hub->eng_distance * eng);
888 if (tmp & (1 << vmid))
897 /* TODO: It needs to continue working on debugging with semaphore for GFXHUB as well. */
900 * add semaphore release after invalidation,
901 * write with 0 means semaphore release
903 if (vmhub == AMDGPU_GFXHUB_0)
904 WREG32_SOC15_IP_NO_KIQ(GC, hub->vm_inv_eng0_sem + hub->eng_distance * eng, 0);
906 WREG32_SOC15_IP_NO_KIQ(MMHUB, hub->vm_inv_eng0_sem + hub->eng_distance * eng, 0);
909 spin_unlock(&adev->gmc.invalidate_lock);
911 if (j < adev->usec_timeout)
914 DRM_ERROR("Timeout waiting for VM flush ACK!\n");
918 * gmc_v9_0_flush_gpu_tlb_pasid - tlb flush via pasid
920 * @adev: amdgpu_device pointer
921 * @pasid: pasid to be flush
922 * @flush_type: the flush type
923 * @all_hub: flush all hubs
925 * Flush the TLB for the requested pasid.
927 static int gmc_v9_0_flush_gpu_tlb_pasid(struct amdgpu_device *adev,
928 uint16_t pasid, uint32_t flush_type,
934 uint16_t queried_pasid;
936 u32 usec_timeout = amdgpu_sriov_vf(adev) ? SRIOV_USEC_TIMEOUT : adev->usec_timeout;
937 struct amdgpu_ring *ring = &adev->gfx.kiq.ring;
938 struct amdgpu_kiq *kiq = &adev->gfx.kiq;
940 if (amdgpu_in_reset(adev))
943 if (ring->sched.ready && down_read_trylock(&adev->reset_domain->sem)) {
944 /* Vega20+XGMI caches PTEs in TC and TLB. Add a
945 * heavy-weight TLB flush (type 2), which flushes
946 * both. Due to a race condition with concurrent
947 * memory accesses using the same TLB cache line, we
948 * still need a second TLB flush after this.
950 bool vega20_xgmi_wa = (adev->gmc.xgmi.num_physical_nodes &&
951 adev->ip_versions[GC_HWIP][0] == IP_VERSION(9, 4, 0));
952 /* 2 dwords flush + 8 dwords fence */
953 unsigned int ndw = kiq->pmf->invalidate_tlbs_size + 8;
956 ndw += kiq->pmf->invalidate_tlbs_size;
958 spin_lock(&adev->gfx.kiq.ring_lock);
959 /* 2 dwords flush + 8 dwords fence */
960 amdgpu_ring_alloc(ring, ndw);
962 kiq->pmf->kiq_invalidate_tlbs(ring,
964 kiq->pmf->kiq_invalidate_tlbs(ring,
965 pasid, flush_type, all_hub);
966 r = amdgpu_fence_emit_polling(ring, &seq, MAX_KIQ_REG_WAIT);
968 amdgpu_ring_undo(ring);
969 spin_unlock(&adev->gfx.kiq.ring_lock);
970 up_read(&adev->reset_domain->sem);
974 amdgpu_ring_commit(ring);
975 spin_unlock(&adev->gfx.kiq.ring_lock);
976 r = amdgpu_fence_wait_polling(ring, seq, usec_timeout);
978 dev_err(adev->dev, "wait for kiq fence error: %ld.\n", r);
979 up_read(&adev->reset_domain->sem);
982 up_read(&adev->reset_domain->sem);
986 for (vmid = 1; vmid < 16; vmid++) {
988 ret = gmc_v9_0_get_atc_vmid_pasid_mapping_info(adev, vmid,
990 if (ret && queried_pasid == pasid) {
992 for (i = 0; i < adev->num_vmhubs; i++)
993 gmc_v9_0_flush_gpu_tlb(adev, vmid,
996 gmc_v9_0_flush_gpu_tlb(adev, vmid,
997 AMDGPU_GFXHUB_0, flush_type);
1007 static uint64_t gmc_v9_0_emit_flush_gpu_tlb(struct amdgpu_ring *ring,
1008 unsigned vmid, uint64_t pd_addr)
1010 bool use_semaphore = gmc_v9_0_use_invalidate_semaphore(ring->adev, ring->vm_hub);
1011 struct amdgpu_device *adev = ring->adev;
1012 struct amdgpu_vmhub *hub = &adev->vmhub[ring->vm_hub];
1013 uint32_t req = gmc_v9_0_get_invalidate_req(vmid, 0);
1014 unsigned eng = ring->vm_inv_eng;
1017 * It may lose gpuvm invalidate acknowldege state across power-gating
1018 * off cycle, add semaphore acquire before invalidation and semaphore
1019 * release after invalidation to avoid entering power gated state
1023 /* TODO: It needs to continue working on debugging with semaphore for GFXHUB as well. */
1025 /* a read return value of 1 means semaphore acuqire */
1026 amdgpu_ring_emit_reg_wait(ring,
1027 hub->vm_inv_eng0_sem +
1028 hub->eng_distance * eng, 0x1, 0x1);
1030 amdgpu_ring_emit_wreg(ring, hub->ctx0_ptb_addr_lo32 +
1031 (hub->ctx_addr_distance * vmid),
1032 lower_32_bits(pd_addr));
1034 amdgpu_ring_emit_wreg(ring, hub->ctx0_ptb_addr_hi32 +
1035 (hub->ctx_addr_distance * vmid),
1036 upper_32_bits(pd_addr));
1038 amdgpu_ring_emit_reg_write_reg_wait(ring, hub->vm_inv_eng0_req +
1039 hub->eng_distance * eng,
1040 hub->vm_inv_eng0_ack +
1041 hub->eng_distance * eng,
1044 /* TODO: It needs to continue working on debugging with semaphore for GFXHUB as well. */
1047 * add semaphore release after invalidation,
1048 * write with 0 means semaphore release
1050 amdgpu_ring_emit_wreg(ring, hub->vm_inv_eng0_sem +
1051 hub->eng_distance * eng, 0);
1056 static void gmc_v9_0_emit_pasid_mapping(struct amdgpu_ring *ring, unsigned vmid,
1059 struct amdgpu_device *adev = ring->adev;
1062 /* Do nothing because there's no lut register for mmhub1. */
1063 if (ring->vm_hub == AMDGPU_MMHUB_1)
1066 if (ring->vm_hub == AMDGPU_GFXHUB_0)
1067 reg = SOC15_REG_OFFSET(OSSSYS, 0, mmIH_VMID_0_LUT) + vmid;
1069 reg = SOC15_REG_OFFSET(OSSSYS, 0, mmIH_VMID_0_LUT_MM) + vmid;
1071 amdgpu_ring_emit_wreg(ring, reg, pasid);
1075 * PTE format on VEGA 10:
1084 * 47:12 4k physical page base address
1094 * PDE format on VEGA 10:
1095 * 63:59 block fragment size
1099 * 47:6 physical base address of PD or PTE
1106 static uint64_t gmc_v9_0_map_mtype(struct amdgpu_device *adev, uint32_t flags)
1110 case AMDGPU_VM_MTYPE_DEFAULT:
1111 return AMDGPU_PTE_MTYPE_VG10(MTYPE_NC);
1112 case AMDGPU_VM_MTYPE_NC:
1113 return AMDGPU_PTE_MTYPE_VG10(MTYPE_NC);
1114 case AMDGPU_VM_MTYPE_WC:
1115 return AMDGPU_PTE_MTYPE_VG10(MTYPE_WC);
1116 case AMDGPU_VM_MTYPE_RW:
1117 return AMDGPU_PTE_MTYPE_VG10(MTYPE_RW);
1118 case AMDGPU_VM_MTYPE_CC:
1119 return AMDGPU_PTE_MTYPE_VG10(MTYPE_CC);
1120 case AMDGPU_VM_MTYPE_UC:
1121 return AMDGPU_PTE_MTYPE_VG10(MTYPE_UC);
1123 return AMDGPU_PTE_MTYPE_VG10(MTYPE_NC);
1127 static void gmc_v9_0_get_vm_pde(struct amdgpu_device *adev, int level,
1128 uint64_t *addr, uint64_t *flags)
1130 if (!(*flags & AMDGPU_PDE_PTE) && !(*flags & AMDGPU_PTE_SYSTEM))
1131 *addr = amdgpu_gmc_vram_mc2pa(adev, *addr);
1132 BUG_ON(*addr & 0xFFFF00000000003FULL);
1134 if (!adev->gmc.translate_further)
1137 if (level == AMDGPU_VM_PDB1) {
1138 /* Set the block fragment size */
1139 if (!(*flags & AMDGPU_PDE_PTE))
1140 *flags |= AMDGPU_PDE_BFS(0x9);
1142 } else if (level == AMDGPU_VM_PDB0) {
1143 if (*flags & AMDGPU_PDE_PTE) {
1144 *flags &= ~AMDGPU_PDE_PTE;
1145 if (!(*flags & AMDGPU_PTE_VALID))
1146 *addr |= 1 << PAGE_SHIFT;
1148 *flags |= AMDGPU_PTE_TF;
1153 static void gmc_v9_0_get_coherence_flags(struct amdgpu_device *adev,
1154 struct amdgpu_bo *bo,
1155 struct amdgpu_bo_va_mapping *mapping,
1158 struct amdgpu_device *bo_adev = amdgpu_ttm_adev(bo->tbo.bdev);
1159 bool is_vram = bo->tbo.resource->mem_type == TTM_PL_VRAM;
1160 bool coherent = bo->flags & AMDGPU_GEM_CREATE_COHERENT;
1161 bool uncached = bo->flags & AMDGPU_GEM_CREATE_UNCACHED;
1165 switch (adev->ip_versions[GC_HWIP][0]) {
1166 case IP_VERSION(9, 4, 1):
1167 case IP_VERSION(9, 4, 2):
1168 case IP_VERSION(9, 4, 3):
1170 if (bo_adev == adev) {
1177 /* FIXME: is this still needed? Or does
1178 * amdgpu_ttm_tt_pde_flags already handle this?
1180 if ((adev->ip_versions[GC_HWIP][0] == IP_VERSION(9, 4, 2) ||
1181 adev->ip_versions[GC_HWIP][0] == IP_VERSION(9, 4, 3)) &&
1182 adev->gmc.xgmi.connected_to_cpu)
1185 if (uncached || coherent)
1189 if (mapping->bo_va->is_xgmi)
1193 if (uncached || coherent)
1197 /* FIXME: is this still needed? Or does
1198 * amdgpu_ttm_tt_pde_flags already handle this?
1204 if (uncached || coherent)
1209 /* FIXME: is this still needed? Or does
1210 * amdgpu_ttm_tt_pde_flags already handle this?
1216 if (mtype != MTYPE_NC)
1217 *flags = (*flags & ~AMDGPU_PTE_MTYPE_VG10_MASK) |
1218 AMDGPU_PTE_MTYPE_VG10(mtype);
1219 *flags |= snoop ? AMDGPU_PTE_SNOOPED : 0;
1222 static void gmc_v9_0_get_vm_pte(struct amdgpu_device *adev,
1223 struct amdgpu_bo_va_mapping *mapping,
1226 struct amdgpu_bo *bo = mapping->bo_va->base.bo;
1228 *flags &= ~AMDGPU_PTE_EXECUTABLE;
1229 *flags |= mapping->flags & AMDGPU_PTE_EXECUTABLE;
1231 *flags &= ~AMDGPU_PTE_MTYPE_VG10_MASK;
1232 *flags |= mapping->flags & AMDGPU_PTE_MTYPE_VG10_MASK;
1234 if (mapping->flags & AMDGPU_PTE_PRT) {
1235 *flags |= AMDGPU_PTE_PRT;
1236 *flags &= ~AMDGPU_PTE_VALID;
1239 if (bo && bo->tbo.resource)
1240 gmc_v9_0_get_coherence_flags(adev, mapping->bo_va->base.bo,
1244 static unsigned gmc_v9_0_get_vbios_fb_size(struct amdgpu_device *adev)
1246 u32 d1vga_control = RREG32_SOC15(DCE, 0, mmD1VGA_CONTROL);
1249 /* TODO move to DC so GMC doesn't need to hard-code DCN registers */
1251 if (REG_GET_FIELD(d1vga_control, D1VGA_CONTROL, D1VGA_MODE_ENABLE)) {
1252 size = AMDGPU_VBIOS_VGA_ALLOCATION;
1256 switch (adev->ip_versions[DCE_HWIP][0]) {
1257 case IP_VERSION(1, 0, 0):
1258 case IP_VERSION(1, 0, 1):
1259 viewport = RREG32_SOC15(DCE, 0, mmHUBP0_DCSURF_PRI_VIEWPORT_DIMENSION);
1260 size = (REG_GET_FIELD(viewport,
1261 HUBP0_DCSURF_PRI_VIEWPORT_DIMENSION, PRI_VIEWPORT_HEIGHT) *
1262 REG_GET_FIELD(viewport,
1263 HUBP0_DCSURF_PRI_VIEWPORT_DIMENSION, PRI_VIEWPORT_WIDTH) *
1266 case IP_VERSION(2, 1, 0):
1267 viewport = RREG32_SOC15(DCE, 0, mmHUBP0_DCSURF_PRI_VIEWPORT_DIMENSION_DCN2);
1268 size = (REG_GET_FIELD(viewport,
1269 HUBP0_DCSURF_PRI_VIEWPORT_DIMENSION, PRI_VIEWPORT_HEIGHT) *
1270 REG_GET_FIELD(viewport,
1271 HUBP0_DCSURF_PRI_VIEWPORT_DIMENSION, PRI_VIEWPORT_WIDTH) *
1275 viewport = RREG32_SOC15(DCE, 0, mmSCL0_VIEWPORT_SIZE);
1276 size = (REG_GET_FIELD(viewport, SCL0_VIEWPORT_SIZE, VIEWPORT_HEIGHT) *
1277 REG_GET_FIELD(viewport, SCL0_VIEWPORT_SIZE, VIEWPORT_WIDTH) *
1286 static const struct amdgpu_gmc_funcs gmc_v9_0_gmc_funcs = {
1287 .flush_gpu_tlb = gmc_v9_0_flush_gpu_tlb,
1288 .flush_gpu_tlb_pasid = gmc_v9_0_flush_gpu_tlb_pasid,
1289 .emit_flush_gpu_tlb = gmc_v9_0_emit_flush_gpu_tlb,
1290 .emit_pasid_mapping = gmc_v9_0_emit_pasid_mapping,
1291 .map_mtype = gmc_v9_0_map_mtype,
1292 .get_vm_pde = gmc_v9_0_get_vm_pde,
1293 .get_vm_pte = gmc_v9_0_get_vm_pte,
1294 .get_vbios_fb_size = gmc_v9_0_get_vbios_fb_size,
1297 static void gmc_v9_0_set_gmc_funcs(struct amdgpu_device *adev)
1299 adev->gmc.gmc_funcs = &gmc_v9_0_gmc_funcs;
1302 static void gmc_v9_0_set_umc_funcs(struct amdgpu_device *adev)
1304 switch (adev->ip_versions[UMC_HWIP][0]) {
1305 case IP_VERSION(6, 0, 0):
1306 adev->umc.funcs = &umc_v6_0_funcs;
1308 case IP_VERSION(6, 1, 1):
1309 adev->umc.max_ras_err_cnt_per_query = UMC_V6_1_TOTAL_CHANNEL_NUM;
1310 adev->umc.channel_inst_num = UMC_V6_1_CHANNEL_INSTANCE_NUM;
1311 adev->umc.umc_inst_num = UMC_V6_1_UMC_INSTANCE_NUM;
1312 adev->umc.channel_offs = UMC_V6_1_PER_CHANNEL_OFFSET_VG20;
1313 adev->umc.retire_unit = 1;
1314 adev->umc.channel_idx_tbl = &umc_v6_1_channel_idx_tbl[0][0];
1315 adev->umc.ras = &umc_v6_1_ras;
1317 case IP_VERSION(6, 1, 2):
1318 adev->umc.max_ras_err_cnt_per_query = UMC_V6_1_TOTAL_CHANNEL_NUM;
1319 adev->umc.channel_inst_num = UMC_V6_1_CHANNEL_INSTANCE_NUM;
1320 adev->umc.umc_inst_num = UMC_V6_1_UMC_INSTANCE_NUM;
1321 adev->umc.channel_offs = UMC_V6_1_PER_CHANNEL_OFFSET_ARCT;
1322 adev->umc.retire_unit = 1;
1323 adev->umc.channel_idx_tbl = &umc_v6_1_channel_idx_tbl[0][0];
1324 adev->umc.ras = &umc_v6_1_ras;
1326 case IP_VERSION(6, 7, 0):
1327 adev->umc.max_ras_err_cnt_per_query =
1328 UMC_V6_7_TOTAL_CHANNEL_NUM * UMC_V6_7_BAD_PAGE_NUM_PER_CHANNEL;
1329 adev->umc.channel_inst_num = UMC_V6_7_CHANNEL_INSTANCE_NUM;
1330 adev->umc.umc_inst_num = UMC_V6_7_UMC_INSTANCE_NUM;
1331 adev->umc.channel_offs = UMC_V6_7_PER_CHANNEL_OFFSET;
1332 adev->umc.retire_unit = (UMC_V6_7_NA_MAP_PA_NUM * 2);
1333 if (!adev->gmc.xgmi.connected_to_cpu)
1334 adev->umc.ras = &umc_v6_7_ras;
1335 if (1 & adev->smuio.funcs->get_die_id(adev))
1336 adev->umc.channel_idx_tbl = &umc_v6_7_channel_idx_tbl_first[0][0];
1338 adev->umc.channel_idx_tbl = &umc_v6_7_channel_idx_tbl_second[0][0];
1345 static void gmc_v9_0_set_mmhub_funcs(struct amdgpu_device *adev)
1347 switch (adev->ip_versions[MMHUB_HWIP][0]) {
1348 case IP_VERSION(9, 4, 1):
1349 adev->mmhub.funcs = &mmhub_v9_4_funcs;
1351 case IP_VERSION(9, 4, 2):
1352 adev->mmhub.funcs = &mmhub_v1_7_funcs;
1354 case IP_VERSION(1, 8, 0):
1355 adev->mmhub.funcs = &mmhub_v1_8_funcs;
1358 adev->mmhub.funcs = &mmhub_v1_0_funcs;
1363 static void gmc_v9_0_set_mmhub_ras_funcs(struct amdgpu_device *adev)
1365 switch (adev->ip_versions[MMHUB_HWIP][0]) {
1366 case IP_VERSION(9, 4, 0):
1367 adev->mmhub.ras = &mmhub_v1_0_ras;
1369 case IP_VERSION(9, 4, 1):
1370 adev->mmhub.ras = &mmhub_v9_4_ras;
1372 case IP_VERSION(9, 4, 2):
1373 adev->mmhub.ras = &mmhub_v1_7_ras;
1376 /* mmhub ras is not available */
1381 static void gmc_v9_0_set_gfxhub_funcs(struct amdgpu_device *adev)
1383 if (adev->ip_versions[GC_HWIP][0] == IP_VERSION(9, 4, 3))
1384 adev->gfxhub.funcs = &gfxhub_v1_2_funcs;
1386 adev->gfxhub.funcs = &gfxhub_v1_0_funcs;
1389 static void gmc_v9_0_set_hdp_ras_funcs(struct amdgpu_device *adev)
1391 adev->hdp.ras = &hdp_v4_0_ras;
1394 static void gmc_v9_0_set_mca_ras_funcs(struct amdgpu_device *adev)
1396 struct amdgpu_mca *mca = &adev->mca;
1398 /* is UMC the right IP to check for MCA? Maybe DF? */
1399 switch (adev->ip_versions[UMC_HWIP][0]) {
1400 case IP_VERSION(6, 7, 0):
1401 if (!adev->gmc.xgmi.connected_to_cpu) {
1402 mca->mp0.ras = &mca_v3_0_mp0_ras;
1403 mca->mp1.ras = &mca_v3_0_mp1_ras;
1404 mca->mpio.ras = &mca_v3_0_mpio_ras;
1412 static void gmc_v9_0_set_xgmi_ras_funcs(struct amdgpu_device *adev)
1414 if (!adev->gmc.xgmi.connected_to_cpu)
1415 adev->gmc.xgmi.ras = &xgmi_ras;
1418 static int gmc_v9_0_early_init(void *handle)
1420 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1422 /* ARCT and VEGA20 don't have XGMI defined in their IP discovery tables */
1423 if (adev->asic_type == CHIP_VEGA20 ||
1424 adev->asic_type == CHIP_ARCTURUS)
1425 adev->gmc.xgmi.supported = true;
1427 if (adev->ip_versions[XGMI_HWIP][0] == IP_VERSION(6, 1, 0)) {
1428 adev->gmc.xgmi.supported = true;
1429 adev->gmc.xgmi.connected_to_cpu =
1430 adev->smuio.funcs->is_host_gpu_xgmi_supported(adev);
1433 gmc_v9_0_set_gmc_funcs(adev);
1434 gmc_v9_0_set_irq_funcs(adev);
1435 gmc_v9_0_set_umc_funcs(adev);
1436 gmc_v9_0_set_mmhub_funcs(adev);
1437 gmc_v9_0_set_mmhub_ras_funcs(adev);
1438 gmc_v9_0_set_gfxhub_funcs(adev);
1439 gmc_v9_0_set_hdp_ras_funcs(adev);
1440 gmc_v9_0_set_mca_ras_funcs(adev);
1441 gmc_v9_0_set_xgmi_ras_funcs(adev);
1443 adev->gmc.shared_aperture_start = 0x2000000000000000ULL;
1444 adev->gmc.shared_aperture_end =
1445 adev->gmc.shared_aperture_start + (4ULL << 30) - 1;
1446 adev->gmc.private_aperture_start = 0x1000000000000000ULL;
1447 adev->gmc.private_aperture_end =
1448 adev->gmc.private_aperture_start + (4ULL << 30) - 1;
1453 static int gmc_v9_0_late_init(void *handle)
1455 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1458 r = amdgpu_gmc_allocate_vm_inv_eng(adev);
1463 * Workaround performance drop issue with VBIOS enables partial
1464 * writes, while disables HBM ECC for vega10.
1466 if (!amdgpu_sriov_vf(adev) &&
1467 (adev->ip_versions[UMC_HWIP][0] == IP_VERSION(6, 0, 0))) {
1468 if (!(adev->ras_enabled & (1 << AMDGPU_RAS_BLOCK__UMC))) {
1469 if (adev->df.funcs &&
1470 adev->df.funcs->enable_ecc_force_par_wr_rmw)
1471 adev->df.funcs->enable_ecc_force_par_wr_rmw(adev, false);
1475 if (!amdgpu_persistent_edc_harvesting_supported(adev)) {
1476 if (adev->mmhub.ras && adev->mmhub.ras->ras_block.hw_ops &&
1477 adev->mmhub.ras->ras_block.hw_ops->reset_ras_error_count)
1478 adev->mmhub.ras->ras_block.hw_ops->reset_ras_error_count(adev);
1480 if (adev->hdp.ras && adev->hdp.ras->ras_block.hw_ops &&
1481 adev->hdp.ras->ras_block.hw_ops->reset_ras_error_count)
1482 adev->hdp.ras->ras_block.hw_ops->reset_ras_error_count(adev);
1485 r = amdgpu_gmc_ras_late_init(adev);
1489 return amdgpu_irq_get(adev, &adev->gmc.vm_fault, 0);
1492 static void gmc_v9_0_vram_gtt_location(struct amdgpu_device *adev,
1493 struct amdgpu_gmc *mc)
1495 u64 base = adev->mmhub.funcs->get_fb_location(adev);
1497 /* add the xgmi offset of the physical node */
1498 base += adev->gmc.xgmi.physical_node_id * adev->gmc.xgmi.node_segment_size;
1499 if (adev->gmc.xgmi.connected_to_cpu) {
1500 amdgpu_gmc_sysvm_location(adev, mc);
1502 amdgpu_gmc_vram_location(adev, mc, base);
1503 amdgpu_gmc_gart_location(adev, mc);
1504 amdgpu_gmc_agp_location(adev, mc);
1506 /* base offset of vram pages */
1507 adev->vm_manager.vram_base_offset = adev->gfxhub.funcs->get_mc_fb_offset(adev);
1509 /* XXX: add the xgmi offset of the physical node? */
1510 adev->vm_manager.vram_base_offset +=
1511 adev->gmc.xgmi.physical_node_id * adev->gmc.xgmi.node_segment_size;
1515 * gmc_v9_0_mc_init - initialize the memory controller driver params
1517 * @adev: amdgpu_device pointer
1519 * Look up the amount of vram, vram width, and decide how to place
1520 * vram and gart within the GPU's physical address space.
1521 * Returns 0 for success.
1523 static int gmc_v9_0_mc_init(struct amdgpu_device *adev)
1527 /* size in MB on si */
1528 adev->gmc.mc_vram_size =
1529 adev->nbio.funcs->get_memsize(adev) * 1024ULL * 1024ULL;
1530 adev->gmc.real_vram_size = adev->gmc.mc_vram_size;
1532 if (!(adev->flags & AMD_IS_APU) &&
1533 !adev->gmc.xgmi.connected_to_cpu) {
1534 r = amdgpu_device_resize_fb_bar(adev);
1538 adev->gmc.aper_base = pci_resource_start(adev->pdev, 0);
1539 adev->gmc.aper_size = pci_resource_len(adev->pdev, 0);
1541 #ifdef CONFIG_X86_64
1543 * AMD Accelerated Processing Platform (APP) supporting GPU-HOST xgmi
1544 * interface can use VRAM through here as it appears system reserved
1545 * memory in host address space.
1547 * For APUs, VRAM is just the stolen system memory and can be accessed
1550 * Otherwise, use the legacy Host Data Path (HDP) through PCIe BAR.
1553 /* check whether both host-gpu and gpu-gpu xgmi links exist */
1554 if (((adev->flags & AMD_IS_APU) && !amdgpu_passthrough(adev)) ||
1555 (adev->gmc.xgmi.supported &&
1556 adev->gmc.xgmi.connected_to_cpu)) {
1557 adev->gmc.aper_base =
1558 adev->gfxhub.funcs->get_mc_fb_offset(adev) +
1559 adev->gmc.xgmi.physical_node_id *
1560 adev->gmc.xgmi.node_segment_size;
1561 adev->gmc.aper_size = adev->gmc.real_vram_size;
1565 adev->gmc.visible_vram_size = adev->gmc.aper_size;
1567 /* set the gart size */
1568 if (amdgpu_gart_size == -1) {
1569 switch (adev->ip_versions[GC_HWIP][0]) {
1570 case IP_VERSION(9, 0, 1): /* all engines support GPUVM */
1571 case IP_VERSION(9, 2, 1): /* all engines support GPUVM */
1572 case IP_VERSION(9, 4, 0):
1573 case IP_VERSION(9, 4, 1):
1574 case IP_VERSION(9, 4, 2):
1575 case IP_VERSION(9, 4, 3):
1577 adev->gmc.gart_size = 512ULL << 20;
1579 case IP_VERSION(9, 1, 0): /* DCE SG support */
1580 case IP_VERSION(9, 2, 2): /* DCE SG support */
1581 case IP_VERSION(9, 3, 0):
1582 adev->gmc.gart_size = 1024ULL << 20;
1586 adev->gmc.gart_size = (u64)amdgpu_gart_size << 20;
1589 adev->gmc.gart_size += adev->pm.smu_prv_buffer_size;
1591 gmc_v9_0_vram_gtt_location(adev, &adev->gmc);
1596 static int gmc_v9_0_gart_init(struct amdgpu_device *adev)
1600 if (adev->gart.bo) {
1601 WARN(1, "VEGA10 PCIE GART already initialized\n");
1605 if (adev->gmc.xgmi.connected_to_cpu) {
1606 adev->gmc.vmid0_page_table_depth = 1;
1607 adev->gmc.vmid0_page_table_block_size = 12;
1609 adev->gmc.vmid0_page_table_depth = 0;
1610 adev->gmc.vmid0_page_table_block_size = 0;
1613 /* Initialize common gart structure */
1614 r = amdgpu_gart_init(adev);
1617 adev->gart.table_size = adev->gart.num_gpu_pages * 8;
1618 adev->gart.gart_pte_flags = AMDGPU_PTE_MTYPE_VG10(MTYPE_UC) |
1619 AMDGPU_PTE_EXECUTABLE;
1621 r = amdgpu_gart_table_vram_alloc(adev);
1625 if (adev->gmc.xgmi.connected_to_cpu) {
1626 r = amdgpu_gmc_pdb0_alloc(adev);
1633 * gmc_v9_0_save_registers - saves regs
1635 * @adev: amdgpu_device pointer
1637 * This saves potential register values that should be
1638 * restored upon resume
1640 static void gmc_v9_0_save_registers(struct amdgpu_device *adev)
1642 if ((adev->ip_versions[DCE_HWIP][0] == IP_VERSION(1, 0, 0)) ||
1643 (adev->ip_versions[DCE_HWIP][0] == IP_VERSION(1, 0, 1)))
1644 adev->gmc.sdpif_register = RREG32_SOC15(DCE, 0, mmDCHUBBUB_SDPIF_MMIO_CNTRL_0);
1647 static int gmc_v9_0_sw_init(void *handle)
1649 int r, vram_width = 0, vram_type = 0, vram_vendor = 0, dma_addr_bits;
1650 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1652 adev->gfxhub.funcs->init(adev);
1654 adev->mmhub.funcs->init(adev);
1656 spin_lock_init(&adev->gmc.invalidate_lock);
1658 r = amdgpu_atomfirmware_get_vram_info(adev,
1659 &vram_width, &vram_type, &vram_vendor);
1660 if (amdgpu_sriov_vf(adev))
1661 /* For Vega10 SR-IOV, vram_width can't be read from ATOM as RAVEN,
1662 * and DF related registers is not readable, seems hardcord is the
1663 * only way to set the correct vram_width
1665 adev->gmc.vram_width = 2048;
1666 else if (amdgpu_emu_mode != 1)
1667 adev->gmc.vram_width = vram_width;
1669 if (!adev->gmc.vram_width) {
1670 int chansize, numchan;
1672 /* hbm memory channel size */
1673 if (adev->flags & AMD_IS_APU)
1677 if (adev->df.funcs &&
1678 adev->df.funcs->get_hbm_channel_number) {
1679 numchan = adev->df.funcs->get_hbm_channel_number(adev);
1680 adev->gmc.vram_width = numchan * chansize;
1684 adev->gmc.vram_type = vram_type;
1685 adev->gmc.vram_vendor = vram_vendor;
1686 switch (adev->ip_versions[GC_HWIP][0]) {
1687 case IP_VERSION(9, 1, 0):
1688 case IP_VERSION(9, 2, 2):
1689 adev->num_vmhubs = 2;
1691 if (adev->rev_id == 0x0 || adev->rev_id == 0x1) {
1692 amdgpu_vm_adjust_size(adev, 256 * 1024, 9, 3, 48);
1694 /* vm_size is 128TB + 512GB for legacy 3-level page support */
1695 amdgpu_vm_adjust_size(adev, 128 * 1024 + 512, 9, 2, 48);
1696 adev->gmc.translate_further =
1697 adev->vm_manager.num_level > 1;
1700 case IP_VERSION(9, 0, 1):
1701 case IP_VERSION(9, 2, 1):
1702 case IP_VERSION(9, 4, 0):
1703 case IP_VERSION(9, 3, 0):
1704 case IP_VERSION(9, 4, 2):
1705 case IP_VERSION(9, 4, 3):
1706 adev->num_vmhubs = 2;
1710 * To fulfill 4-level page support,
1711 * vm size is 256TB (48bit), maximum size of Vega10,
1712 * block size 512 (9bit)
1714 /* sriov restrict max_pfn below AMDGPU_GMC_HOLE */
1715 if (amdgpu_sriov_vf(adev))
1716 amdgpu_vm_adjust_size(adev, 256 * 1024, 9, 3, 47);
1718 amdgpu_vm_adjust_size(adev, 256 * 1024, 9, 3, 48);
1719 if (adev->ip_versions[GC_HWIP][0] == IP_VERSION(9, 4, 2))
1720 adev->gmc.translate_further = adev->vm_manager.num_level > 1;
1722 case IP_VERSION(9, 4, 1):
1723 adev->num_vmhubs = 3;
1725 /* Keep the vm size same with Vega20 */
1726 amdgpu_vm_adjust_size(adev, 256 * 1024, 9, 3, 48);
1727 adev->gmc.translate_further = adev->vm_manager.num_level > 1;
1733 /* This interrupt is VMC page fault.*/
1734 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_VMC, VMC_1_0__SRCID__VM_FAULT,
1735 &adev->gmc.vm_fault);
1739 if (adev->ip_versions[GC_HWIP][0] == IP_VERSION(9, 4, 1)) {
1740 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_VMC1, VMC_1_0__SRCID__VM_FAULT,
1741 &adev->gmc.vm_fault);
1746 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_UTCL2, UTCL2_1_0__SRCID__FAULT,
1747 &adev->gmc.vm_fault);
1752 if (!amdgpu_sriov_vf(adev) &&
1753 !adev->gmc.xgmi.connected_to_cpu) {
1754 /* interrupt sent to DF. */
1755 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DF, 0,
1756 &adev->gmc.ecc_irq);
1761 /* Set the internal MC address mask
1762 * This is the max address of the GPU's
1763 * internal address space.
1765 adev->gmc.mc_mask = 0xffffffffffffULL; /* 48 bit MC */
1767 dma_addr_bits = adev->ip_versions[GC_HWIP][0] == IP_VERSION(9, 4, 2) ? 48:44;
1768 r = dma_set_mask_and_coherent(adev->dev, DMA_BIT_MASK(dma_addr_bits));
1770 printk(KERN_WARNING "amdgpu: No suitable DMA available.\n");
1773 adev->need_swiotlb = drm_need_swiotlb(dma_addr_bits);
1775 r = gmc_v9_0_mc_init(adev);
1779 amdgpu_gmc_get_vbios_allocations(adev);
1781 /* Memory manager */
1782 r = amdgpu_bo_init(adev);
1786 r = gmc_v9_0_gart_init(adev);
1792 * VMID 0 is reserved for System
1793 * amdgpu graphics/compute will use VMIDs 1..n-1
1794 * amdkfd will use VMIDs n..15
1796 * The first KFD VMID is 8 for GPUs with graphics, 3 for
1797 * compute-only GPUs. On compute-only GPUs that leaves 2 VMIDs
1798 * for video processing.
1800 adev->vm_manager.first_kfd_vmid =
1801 (adev->ip_versions[GC_HWIP][0] == IP_VERSION(9, 4, 1) ||
1802 adev->ip_versions[GC_HWIP][0] == IP_VERSION(9, 4, 2) ||
1803 adev->ip_versions[GC_HWIP][0] == IP_VERSION(9, 4, 3)) ? 3 : 8;
1805 amdgpu_vm_manager_init(adev);
1807 gmc_v9_0_save_registers(adev);
1809 r = amdgpu_gmc_ras_sw_init(adev);
1816 static int gmc_v9_0_sw_fini(void *handle)
1818 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1820 amdgpu_gmc_ras_fini(adev);
1821 amdgpu_gem_force_release(adev);
1822 amdgpu_vm_manager_fini(adev);
1823 amdgpu_gart_table_vram_free(adev);
1824 amdgpu_bo_free_kernel(&adev->gmc.pdb0_bo, NULL, &adev->gmc.ptr_pdb0);
1825 amdgpu_bo_fini(adev);
1830 static void gmc_v9_0_init_golden_registers(struct amdgpu_device *adev)
1833 switch (adev->ip_versions[MMHUB_HWIP][0]) {
1834 case IP_VERSION(9, 0, 0):
1835 if (amdgpu_sriov_vf(adev))
1838 case IP_VERSION(9, 4, 0):
1839 soc15_program_register_sequence(adev,
1840 golden_settings_mmhub_1_0_0,
1841 ARRAY_SIZE(golden_settings_mmhub_1_0_0));
1842 soc15_program_register_sequence(adev,
1843 golden_settings_athub_1_0_0,
1844 ARRAY_SIZE(golden_settings_athub_1_0_0));
1846 case IP_VERSION(9, 1, 0):
1847 case IP_VERSION(9, 2, 0):
1848 /* TODO for renoir */
1849 soc15_program_register_sequence(adev,
1850 golden_settings_athub_1_0_0,
1851 ARRAY_SIZE(golden_settings_athub_1_0_0));
1859 * gmc_v9_0_restore_registers - restores regs
1861 * @adev: amdgpu_device pointer
1863 * This restores register values, saved at suspend.
1865 void gmc_v9_0_restore_registers(struct amdgpu_device *adev)
1867 if ((adev->ip_versions[DCE_HWIP][0] == IP_VERSION(1, 0, 0)) ||
1868 (adev->ip_versions[DCE_HWIP][0] == IP_VERSION(1, 0, 1))) {
1869 WREG32_SOC15(DCE, 0, mmDCHUBBUB_SDPIF_MMIO_CNTRL_0, adev->gmc.sdpif_register);
1870 WARN_ON(adev->gmc.sdpif_register !=
1871 RREG32_SOC15(DCE, 0, mmDCHUBBUB_SDPIF_MMIO_CNTRL_0));
1876 * gmc_v9_0_gart_enable - gart enable
1878 * @adev: amdgpu_device pointer
1880 static int gmc_v9_0_gart_enable(struct amdgpu_device *adev)
1884 if (adev->gmc.xgmi.connected_to_cpu)
1885 amdgpu_gmc_init_pdb0(adev);
1887 if (adev->gart.bo == NULL) {
1888 dev_err(adev->dev, "No VRAM object for PCIE GART.\n");
1892 amdgpu_gtt_mgr_recover(&adev->mman.gtt_mgr);
1894 if (!adev->in_s0ix) {
1895 r = adev->gfxhub.funcs->gart_enable(adev);
1900 r = adev->mmhub.funcs->gart_enable(adev);
1904 DRM_INFO("PCIE GART of %uM enabled.\n",
1905 (unsigned)(adev->gmc.gart_size >> 20));
1906 if (adev->gmc.pdb0_bo)
1907 DRM_INFO("PDB0 located at 0x%016llX\n",
1908 (unsigned long long)amdgpu_bo_gpu_offset(adev->gmc.pdb0_bo));
1909 DRM_INFO("PTB located at 0x%016llX\n",
1910 (unsigned long long)amdgpu_bo_gpu_offset(adev->gart.bo));
1915 static int gmc_v9_0_hw_init(void *handle)
1917 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1921 /* The sequence of these two function calls matters.*/
1922 gmc_v9_0_init_golden_registers(adev);
1924 if (adev->mode_info.num_crtc) {
1925 /* Lockout access through VGA aperture*/
1926 WREG32_FIELD15(DCE, 0, VGA_HDP_CONTROL, VGA_MEMORY_DISABLE, 1);
1927 /* disable VGA render */
1928 WREG32_FIELD15(DCE, 0, VGA_RENDER_CONTROL, VGA_VSTATUS_CNTL, 0);
1931 if (adev->mmhub.funcs->update_power_gating)
1932 adev->mmhub.funcs->update_power_gating(adev, true);
1934 adev->hdp.funcs->init_registers(adev);
1936 /* After HDP is initialized, flush HDP.*/
1937 adev->hdp.funcs->flush_hdp(adev, NULL);
1939 if (amdgpu_vm_fault_stop == AMDGPU_VM_FAULT_STOP_ALWAYS)
1944 if (!amdgpu_sriov_vf(adev)) {
1946 adev->gfxhub.funcs->set_fault_enable_default(adev, value);
1947 adev->mmhub.funcs->set_fault_enable_default(adev, value);
1949 for (i = 0; i < adev->num_vmhubs; ++i) {
1950 if (adev->in_s0ix && (i == AMDGPU_GFXHUB_0))
1952 gmc_v9_0_flush_gpu_tlb(adev, 0, i, 0);
1955 if (adev->umc.funcs && adev->umc.funcs->init_registers)
1956 adev->umc.funcs->init_registers(adev);
1958 r = gmc_v9_0_gart_enable(adev);
1962 if (amdgpu_emu_mode == 1)
1963 return amdgpu_gmc_vram_checking(adev);
1969 * gmc_v9_0_gart_disable - gart disable
1971 * @adev: amdgpu_device pointer
1973 * This disables all VM page table.
1975 static void gmc_v9_0_gart_disable(struct amdgpu_device *adev)
1978 adev->gfxhub.funcs->gart_disable(adev);
1979 adev->mmhub.funcs->gart_disable(adev);
1982 static int gmc_v9_0_hw_fini(void *handle)
1984 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1986 gmc_v9_0_gart_disable(adev);
1988 if (amdgpu_sriov_vf(adev)) {
1989 /* full access mode, so don't touch any GMC register */
1990 DRM_DEBUG("For SRIOV client, shouldn't do anything.\n");
1995 * Pair the operations did in gmc_v9_0_hw_init and thus maintain
1996 * a correct cached state for GMC. Otherwise, the "gate" again
1997 * operation on S3 resuming will fail due to wrong cached state.
1999 if (adev->mmhub.funcs->update_power_gating)
2000 adev->mmhub.funcs->update_power_gating(adev, false);
2002 amdgpu_irq_put(adev, &adev->gmc.ecc_irq, 0);
2003 amdgpu_irq_put(adev, &adev->gmc.vm_fault, 0);
2008 static int gmc_v9_0_suspend(void *handle)
2010 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2012 return gmc_v9_0_hw_fini(adev);
2015 static int gmc_v9_0_resume(void *handle)
2018 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2020 r = gmc_v9_0_hw_init(adev);
2024 amdgpu_vmid_reset_all(adev);
2029 static bool gmc_v9_0_is_idle(void *handle)
2031 /* MC is always ready in GMC v9.*/
2035 static int gmc_v9_0_wait_for_idle(void *handle)
2037 /* There is no need to wait for MC idle in GMC v9.*/
2041 static int gmc_v9_0_soft_reset(void *handle)
2043 /* XXX for emulation.*/
2047 static int gmc_v9_0_set_clockgating_state(void *handle,
2048 enum amd_clockgating_state state)
2050 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2052 adev->mmhub.funcs->set_clockgating(adev, state);
2054 athub_v1_0_set_clockgating(adev, state);
2059 static void gmc_v9_0_get_clockgating_state(void *handle, u64 *flags)
2061 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
2063 adev->mmhub.funcs->get_clockgating(adev, flags);
2065 athub_v1_0_get_clockgating(adev, flags);
2068 static int gmc_v9_0_set_powergating_state(void *handle,
2069 enum amd_powergating_state state)
2074 const struct amd_ip_funcs gmc_v9_0_ip_funcs = {
2076 .early_init = gmc_v9_0_early_init,
2077 .late_init = gmc_v9_0_late_init,
2078 .sw_init = gmc_v9_0_sw_init,
2079 .sw_fini = gmc_v9_0_sw_fini,
2080 .hw_init = gmc_v9_0_hw_init,
2081 .hw_fini = gmc_v9_0_hw_fini,
2082 .suspend = gmc_v9_0_suspend,
2083 .resume = gmc_v9_0_resume,
2084 .is_idle = gmc_v9_0_is_idle,
2085 .wait_for_idle = gmc_v9_0_wait_for_idle,
2086 .soft_reset = gmc_v9_0_soft_reset,
2087 .set_clockgating_state = gmc_v9_0_set_clockgating_state,
2088 .set_powergating_state = gmc_v9_0_set_powergating_state,
2089 .get_clockgating_state = gmc_v9_0_get_clockgating_state,
2092 const struct amdgpu_ip_block_version gmc_v9_0_ip_block =
2094 .type = AMD_IP_BLOCK_TYPE_GMC,
2098 .funcs = &gmc_v9_0_ip_funcs,