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 "mmhub_v9_4.h"
53 #include "mmhub_v1_7.h"
60 #include "ivsrcid/vmc/irqsrcs_vmc_1_0.h"
62 #include "amdgpu_ras.h"
63 #include "amdgpu_xgmi.h"
65 /* add these here since we already include dce12 headers and these are for DCN */
66 #define mmHUBP0_DCSURF_PRI_VIEWPORT_DIMENSION 0x055d
67 #define mmHUBP0_DCSURF_PRI_VIEWPORT_DIMENSION_BASE_IDX 2
68 #define HUBP0_DCSURF_PRI_VIEWPORT_DIMENSION__PRI_VIEWPORT_WIDTH__SHIFT 0x0
69 #define HUBP0_DCSURF_PRI_VIEWPORT_DIMENSION__PRI_VIEWPORT_HEIGHT__SHIFT 0x10
70 #define HUBP0_DCSURF_PRI_VIEWPORT_DIMENSION__PRI_VIEWPORT_WIDTH_MASK 0x00003FFFL
71 #define HUBP0_DCSURF_PRI_VIEWPORT_DIMENSION__PRI_VIEWPORT_HEIGHT_MASK 0x3FFF0000L
72 #define mmDCHUBBUB_SDPIF_MMIO_CNTRL_0 0x049d
73 #define mmDCHUBBUB_SDPIF_MMIO_CNTRL_0_BASE_IDX 2
75 #define mmHUBP0_DCSURF_PRI_VIEWPORT_DIMENSION_DCN2 0x05ea
76 #define mmHUBP0_DCSURF_PRI_VIEWPORT_DIMENSION_DCN2_BASE_IDX 2
79 static const char *gfxhub_client_ids[] = {
95 static const char *mmhub_client_ids_raven[][2] = {
120 static const char *mmhub_client_ids_renoir[][2] = {
148 static const char *mmhub_client_ids_vega10[][2] = {
161 [32+14][0] = "SDMA0",
174 [32+4][1] = "DCEDWB",
177 [32+14][1] = "SDMA1",
180 static const char *mmhub_client_ids_vega12[][2] = {
193 [32+15][0] = "SDMA0",
203 [32+1][1] = "DCEDWB",
209 [32+15][1] = "SDMA1",
212 static const char *mmhub_client_ids_vega20[][2] = {
226 [32+12][0] = "UTCL2",
227 [32+14][0] = "SDMA1",
245 [32+14][1] = "SDMA1",
248 static const char *mmhub_client_ids_arcturus[][2] = {
289 static const char *mmhub_client_ids_aldebaran[][2] = {
292 [32+1][0] = "DBGU_IO0",
293 [32+2][0] = "DBGU_IO2",
295 [96+11][0] = "JPEG0",
297 [96+13][0] = "VCNU0",
298 [128+11][0] = "JPEG1",
299 [128+12][0] = "VCN1",
300 [128+13][0] = "VCNU1",
303 [256+0][0] = "SDMA0",
304 [256+1][0] = "SDMA1",
305 [256+2][0] = "SDMA2",
306 [256+3][0] = "SDMA3",
307 [256+4][0] = "SDMA4",
311 [32+1][1] = "DBGU_IO0",
312 [32+2][1] = "DBGU_IO2",
314 [96+11][1] = "JPEG0",
316 [96+13][1] = "VCNU0",
317 [128+11][1] = "JPEG1",
318 [128+12][1] = "VCN1",
319 [128+13][1] = "VCNU1",
322 [256+0][1] = "SDMA0",
323 [256+1][1] = "SDMA1",
324 [256+2][1] = "SDMA2",
325 [256+3][1] = "SDMA3",
326 [256+4][1] = "SDMA4",
330 static const struct soc15_reg_golden golden_settings_mmhub_1_0_0[] =
332 SOC15_REG_GOLDEN_VALUE(MMHUB, 0, mmDAGB1_WRCLI2, 0x00000007, 0xfe5fe0fa),
333 SOC15_REG_GOLDEN_VALUE(MMHUB, 0, mmMMEA1_DRAM_WR_CLI2GRP_MAP0, 0x00000030, 0x55555565)
336 static const struct soc15_reg_golden golden_settings_athub_1_0_0[] =
338 SOC15_REG_GOLDEN_VALUE(ATHUB, 0, mmRPB_ARB_CNTL, 0x0000ff00, 0x00000800),
339 SOC15_REG_GOLDEN_VALUE(ATHUB, 0, mmRPB_ARB_CNTL2, 0x00ff00ff, 0x00080008)
342 static const uint32_t ecc_umc_mcumc_ctrl_addrs[] = {
343 (0x000143c0 + 0x00000000),
344 (0x000143c0 + 0x00000800),
345 (0x000143c0 + 0x00001000),
346 (0x000143c0 + 0x00001800),
347 (0x000543c0 + 0x00000000),
348 (0x000543c0 + 0x00000800),
349 (0x000543c0 + 0x00001000),
350 (0x000543c0 + 0x00001800),
351 (0x000943c0 + 0x00000000),
352 (0x000943c0 + 0x00000800),
353 (0x000943c0 + 0x00001000),
354 (0x000943c0 + 0x00001800),
355 (0x000d43c0 + 0x00000000),
356 (0x000d43c0 + 0x00000800),
357 (0x000d43c0 + 0x00001000),
358 (0x000d43c0 + 0x00001800),
359 (0x001143c0 + 0x00000000),
360 (0x001143c0 + 0x00000800),
361 (0x001143c0 + 0x00001000),
362 (0x001143c0 + 0x00001800),
363 (0x001543c0 + 0x00000000),
364 (0x001543c0 + 0x00000800),
365 (0x001543c0 + 0x00001000),
366 (0x001543c0 + 0x00001800),
367 (0x001943c0 + 0x00000000),
368 (0x001943c0 + 0x00000800),
369 (0x001943c0 + 0x00001000),
370 (0x001943c0 + 0x00001800),
371 (0x001d43c0 + 0x00000000),
372 (0x001d43c0 + 0x00000800),
373 (0x001d43c0 + 0x00001000),
374 (0x001d43c0 + 0x00001800),
377 static const uint32_t ecc_umc_mcumc_ctrl_mask_addrs[] = {
378 (0x000143e0 + 0x00000000),
379 (0x000143e0 + 0x00000800),
380 (0x000143e0 + 0x00001000),
381 (0x000143e0 + 0x00001800),
382 (0x000543e0 + 0x00000000),
383 (0x000543e0 + 0x00000800),
384 (0x000543e0 + 0x00001000),
385 (0x000543e0 + 0x00001800),
386 (0x000943e0 + 0x00000000),
387 (0x000943e0 + 0x00000800),
388 (0x000943e0 + 0x00001000),
389 (0x000943e0 + 0x00001800),
390 (0x000d43e0 + 0x00000000),
391 (0x000d43e0 + 0x00000800),
392 (0x000d43e0 + 0x00001000),
393 (0x000d43e0 + 0x00001800),
394 (0x001143e0 + 0x00000000),
395 (0x001143e0 + 0x00000800),
396 (0x001143e0 + 0x00001000),
397 (0x001143e0 + 0x00001800),
398 (0x001543e0 + 0x00000000),
399 (0x001543e0 + 0x00000800),
400 (0x001543e0 + 0x00001000),
401 (0x001543e0 + 0x00001800),
402 (0x001943e0 + 0x00000000),
403 (0x001943e0 + 0x00000800),
404 (0x001943e0 + 0x00001000),
405 (0x001943e0 + 0x00001800),
406 (0x001d43e0 + 0x00000000),
407 (0x001d43e0 + 0x00000800),
408 (0x001d43e0 + 0x00001000),
409 (0x001d43e0 + 0x00001800),
412 static int gmc_v9_0_ecc_interrupt_state(struct amdgpu_device *adev,
413 struct amdgpu_irq_src *src,
415 enum amdgpu_interrupt_state state)
417 u32 bits, i, tmp, reg;
419 /* Devices newer then VEGA10/12 shall have these programming
420 sequences performed by PSP BL */
421 if (adev->asic_type >= CHIP_VEGA20)
427 case AMDGPU_IRQ_STATE_DISABLE:
428 for (i = 0; i < ARRAY_SIZE(ecc_umc_mcumc_ctrl_addrs); i++) {
429 reg = ecc_umc_mcumc_ctrl_addrs[i];
434 for (i = 0; i < ARRAY_SIZE(ecc_umc_mcumc_ctrl_mask_addrs); i++) {
435 reg = ecc_umc_mcumc_ctrl_mask_addrs[i];
441 case AMDGPU_IRQ_STATE_ENABLE:
442 for (i = 0; i < ARRAY_SIZE(ecc_umc_mcumc_ctrl_addrs); i++) {
443 reg = ecc_umc_mcumc_ctrl_addrs[i];
448 for (i = 0; i < ARRAY_SIZE(ecc_umc_mcumc_ctrl_mask_addrs); i++) {
449 reg = ecc_umc_mcumc_ctrl_mask_addrs[i];
462 static int gmc_v9_0_vm_fault_interrupt_state(struct amdgpu_device *adev,
463 struct amdgpu_irq_src *src,
465 enum amdgpu_interrupt_state state)
467 struct amdgpu_vmhub *hub;
468 u32 tmp, reg, bits, i, j;
470 bits = VM_CONTEXT1_CNTL__RANGE_PROTECTION_FAULT_ENABLE_INTERRUPT_MASK |
471 VM_CONTEXT1_CNTL__DUMMY_PAGE_PROTECTION_FAULT_ENABLE_INTERRUPT_MASK |
472 VM_CONTEXT1_CNTL__PDE0_PROTECTION_FAULT_ENABLE_INTERRUPT_MASK |
473 VM_CONTEXT1_CNTL__VALID_PROTECTION_FAULT_ENABLE_INTERRUPT_MASK |
474 VM_CONTEXT1_CNTL__READ_PROTECTION_FAULT_ENABLE_INTERRUPT_MASK |
475 VM_CONTEXT1_CNTL__WRITE_PROTECTION_FAULT_ENABLE_INTERRUPT_MASK |
476 VM_CONTEXT1_CNTL__EXECUTE_PROTECTION_FAULT_ENABLE_INTERRUPT_MASK;
479 case AMDGPU_IRQ_STATE_DISABLE:
480 for (j = 0; j < adev->num_vmhubs; j++) {
481 hub = &adev->vmhub[j];
482 for (i = 0; i < 16; i++) {
483 reg = hub->vm_context0_cntl + i;
485 if (j == AMDGPU_GFXHUB_0)
486 tmp = RREG32_SOC15_IP(GC, reg);
488 tmp = RREG32_SOC15_IP(MMHUB, reg);
492 if (j == AMDGPU_GFXHUB_0)
493 WREG32_SOC15_IP(GC, reg, tmp);
495 WREG32_SOC15_IP(MMHUB, reg, tmp);
499 case AMDGPU_IRQ_STATE_ENABLE:
500 for (j = 0; j < adev->num_vmhubs; j++) {
501 hub = &adev->vmhub[j];
502 for (i = 0; i < 16; i++) {
503 reg = hub->vm_context0_cntl + i;
505 if (j == AMDGPU_GFXHUB_0)
506 tmp = RREG32_SOC15_IP(GC, reg);
508 tmp = RREG32_SOC15_IP(MMHUB, reg);
512 if (j == AMDGPU_GFXHUB_0)
513 WREG32_SOC15_IP(GC, reg, tmp);
515 WREG32_SOC15_IP(MMHUB, reg, tmp);
526 static int gmc_v9_0_process_interrupt(struct amdgpu_device *adev,
527 struct amdgpu_irq_src *source,
528 struct amdgpu_iv_entry *entry)
530 bool retry_fault = !!(entry->src_data[1] & 0x80);
531 bool write_fault = !!(entry->src_data[1] & 0x20);
532 uint32_t status = 0, cid = 0, rw = 0;
533 struct amdgpu_task_info task_info;
534 struct amdgpu_vmhub *hub;
535 const char *mmhub_cid;
536 const char *hub_name;
539 addr = (u64)entry->src_data[0] << 12;
540 addr |= ((u64)entry->src_data[1] & 0xf) << 44;
543 /* Returning 1 here also prevents sending the IV to the KFD */
545 /* Process it onyl if it's the first fault for this address */
546 if (entry->ih != &adev->irq.ih_soft &&
547 amdgpu_gmc_filter_faults(adev, entry->ih, addr, entry->pasid,
551 /* Delegate it to a different ring if the hardware hasn't
554 if (entry->ih == &adev->irq.ih) {
555 amdgpu_irq_delegate(adev, entry, 8);
559 /* Try to handle the recoverable page faults by filling page
562 if (amdgpu_vm_handle_fault(adev, entry->pasid, addr, write_fault))
566 if (!printk_ratelimit())
569 if (entry->client_id == SOC15_IH_CLIENTID_VMC) {
571 hub = &adev->vmhub[AMDGPU_MMHUB_0];
572 } else if (entry->client_id == SOC15_IH_CLIENTID_VMC1) {
574 hub = &adev->vmhub[AMDGPU_MMHUB_1];
576 hub_name = "gfxhub0";
577 hub = &adev->vmhub[AMDGPU_GFXHUB_0];
580 memset(&task_info, 0, sizeof(struct amdgpu_task_info));
581 amdgpu_vm_get_task_info(adev, entry->pasid, &task_info);
584 "[%s] %s page fault (src_id:%u ring:%u vmid:%u "
585 "pasid:%u, for process %s pid %d thread %s pid %d)\n",
586 hub_name, retry_fault ? "retry" : "no-retry",
587 entry->src_id, entry->ring_id, entry->vmid,
588 entry->pasid, task_info.process_name, task_info.tgid,
589 task_info.task_name, task_info.pid);
590 dev_err(adev->dev, " in page starting at address 0x%016llx from IH client 0x%x (%s)\n",
591 addr, entry->client_id,
592 soc15_ih_clientid_name[entry->client_id]);
594 if (amdgpu_sriov_vf(adev))
598 * Issue a dummy read to wait for the status register to
599 * be updated to avoid reading an incorrect value due to
600 * the new fast GRBM interface.
602 if ((entry->vmid_src == AMDGPU_GFXHUB_0) &&
603 (adev->ip_versions[GC_HWIP][0] < IP_VERSION(9, 4, 2)))
604 RREG32(hub->vm_l2_pro_fault_status);
606 status = RREG32(hub->vm_l2_pro_fault_status);
607 cid = REG_GET_FIELD(status, VM_L2_PROTECTION_FAULT_STATUS, CID);
608 rw = REG_GET_FIELD(status, VM_L2_PROTECTION_FAULT_STATUS, RW);
609 WREG32_P(hub->vm_l2_pro_fault_cntl, 1, ~1);
613 "VM_L2_PROTECTION_FAULT_STATUS:0x%08X\n",
615 if (hub == &adev->vmhub[AMDGPU_GFXHUB_0]) {
616 dev_err(adev->dev, "\t Faulty UTCL2 client ID: %s (0x%x)\n",
617 cid >= ARRAY_SIZE(gfxhub_client_ids) ? "unknown" :
618 gfxhub_client_ids[cid],
621 switch (adev->ip_versions[MMHUB_HWIP][0]) {
622 case IP_VERSION(9, 0, 0):
623 mmhub_cid = mmhub_client_ids_vega10[cid][rw];
625 case IP_VERSION(9, 3, 0):
626 mmhub_cid = mmhub_client_ids_vega12[cid][rw];
628 case IP_VERSION(9, 4, 0):
629 mmhub_cid = mmhub_client_ids_vega20[cid][rw];
631 case IP_VERSION(9, 4, 1):
632 mmhub_cid = mmhub_client_ids_arcturus[cid][rw];
634 case IP_VERSION(9, 1, 0):
635 case IP_VERSION(9, 2, 0):
636 mmhub_cid = mmhub_client_ids_raven[cid][rw];
638 case IP_VERSION(1, 5, 0):
639 case IP_VERSION(2, 4, 0):
640 mmhub_cid = mmhub_client_ids_renoir[cid][rw];
642 case IP_VERSION(9, 4, 2):
643 mmhub_cid = mmhub_client_ids_aldebaran[cid][rw];
649 dev_err(adev->dev, "\t Faulty UTCL2 client ID: %s (0x%x)\n",
650 mmhub_cid ? mmhub_cid : "unknown", cid);
652 dev_err(adev->dev, "\t MORE_FAULTS: 0x%lx\n",
653 REG_GET_FIELD(status,
654 VM_L2_PROTECTION_FAULT_STATUS, MORE_FAULTS));
655 dev_err(adev->dev, "\t WALKER_ERROR: 0x%lx\n",
656 REG_GET_FIELD(status,
657 VM_L2_PROTECTION_FAULT_STATUS, WALKER_ERROR));
658 dev_err(adev->dev, "\t PERMISSION_FAULTS: 0x%lx\n",
659 REG_GET_FIELD(status,
660 VM_L2_PROTECTION_FAULT_STATUS, PERMISSION_FAULTS));
661 dev_err(adev->dev, "\t MAPPING_ERROR: 0x%lx\n",
662 REG_GET_FIELD(status,
663 VM_L2_PROTECTION_FAULT_STATUS, MAPPING_ERROR));
664 dev_err(adev->dev, "\t RW: 0x%x\n", rw);
668 static const struct amdgpu_irq_src_funcs gmc_v9_0_irq_funcs = {
669 .set = gmc_v9_0_vm_fault_interrupt_state,
670 .process = gmc_v9_0_process_interrupt,
674 static const struct amdgpu_irq_src_funcs gmc_v9_0_ecc_funcs = {
675 .set = gmc_v9_0_ecc_interrupt_state,
676 .process = amdgpu_umc_process_ecc_irq,
679 static void gmc_v9_0_set_irq_funcs(struct amdgpu_device *adev)
681 adev->gmc.vm_fault.num_types = 1;
682 adev->gmc.vm_fault.funcs = &gmc_v9_0_irq_funcs;
684 if (!amdgpu_sriov_vf(adev) &&
685 !adev->gmc.xgmi.connected_to_cpu) {
686 adev->gmc.ecc_irq.num_types = 1;
687 adev->gmc.ecc_irq.funcs = &gmc_v9_0_ecc_funcs;
691 static uint32_t gmc_v9_0_get_invalidate_req(unsigned int vmid,
696 req = REG_SET_FIELD(req, VM_INVALIDATE_ENG0_REQ,
697 PER_VMID_INVALIDATE_REQ, 1 << vmid);
698 req = REG_SET_FIELD(req, VM_INVALIDATE_ENG0_REQ, FLUSH_TYPE, flush_type);
699 req = REG_SET_FIELD(req, VM_INVALIDATE_ENG0_REQ, INVALIDATE_L2_PTES, 1);
700 req = REG_SET_FIELD(req, VM_INVALIDATE_ENG0_REQ, INVALIDATE_L2_PDE0, 1);
701 req = REG_SET_FIELD(req, VM_INVALIDATE_ENG0_REQ, INVALIDATE_L2_PDE1, 1);
702 req = REG_SET_FIELD(req, VM_INVALIDATE_ENG0_REQ, INVALIDATE_L2_PDE2, 1);
703 req = REG_SET_FIELD(req, VM_INVALIDATE_ENG0_REQ, INVALIDATE_L1_PTES, 1);
704 req = REG_SET_FIELD(req, VM_INVALIDATE_ENG0_REQ,
705 CLEAR_PROTECTION_FAULT_STATUS_ADDR, 0);
711 * gmc_v9_0_use_invalidate_semaphore - judge whether to use semaphore
713 * @adev: amdgpu_device pointer
717 static bool gmc_v9_0_use_invalidate_semaphore(struct amdgpu_device *adev,
720 if (adev->ip_versions[GC_HWIP][0] == IP_VERSION(9, 4, 2))
723 return ((vmhub == AMDGPU_MMHUB_0 ||
724 vmhub == AMDGPU_MMHUB_1) &&
725 (!amdgpu_sriov_vf(adev)) &&
726 (!(!(adev->apu_flags & AMD_APU_IS_RAVEN2) &&
727 (adev->apu_flags & AMD_APU_IS_PICASSO))));
730 static bool gmc_v9_0_get_atc_vmid_pasid_mapping_info(struct amdgpu_device *adev,
731 uint8_t vmid, uint16_t *p_pasid)
735 value = RREG32(SOC15_REG_OFFSET(ATHUB, 0, mmATC_VMID0_PASID_MAPPING)
737 *p_pasid = value & ATC_VMID0_PASID_MAPPING__PASID_MASK;
739 return !!(value & ATC_VMID0_PASID_MAPPING__VALID_MASK);
744 * VMID 0 is the physical GPU addresses as used by the kernel.
745 * VMIDs 1-15 are used for userspace clients and are handled
746 * by the amdgpu vm/hsa code.
750 * gmc_v9_0_flush_gpu_tlb - tlb flush with certain type
752 * @adev: amdgpu_device pointer
753 * @vmid: vm instance to flush
754 * @vmhub: which hub to flush
755 * @flush_type: the flush type
757 * Flush the TLB for the requested page table using certain type.
759 static void gmc_v9_0_flush_gpu_tlb(struct amdgpu_device *adev, uint32_t vmid,
760 uint32_t vmhub, uint32_t flush_type)
762 bool use_semaphore = gmc_v9_0_use_invalidate_semaphore(adev, vmhub);
763 const unsigned eng = 17;
764 u32 j, inv_req, inv_req2, tmp;
765 struct amdgpu_vmhub *hub;
767 BUG_ON(vmhub >= adev->num_vmhubs);
769 hub = &adev->vmhub[vmhub];
770 if (adev->gmc.xgmi.num_physical_nodes &&
771 adev->ip_versions[GC_HWIP][0] == IP_VERSION(9, 4, 0)) {
772 /* Vega20+XGMI caches PTEs in TC and TLB. Add a
773 * heavy-weight TLB flush (type 2), which flushes
774 * both. Due to a race condition with concurrent
775 * memory accesses using the same TLB cache line, we
776 * still need a second TLB flush after this.
778 inv_req = gmc_v9_0_get_invalidate_req(vmid, 2);
779 inv_req2 = gmc_v9_0_get_invalidate_req(vmid, flush_type);
781 inv_req = gmc_v9_0_get_invalidate_req(vmid, flush_type);
785 /* This is necessary for a HW workaround under SRIOV as well
786 * as GFXOFF under bare metal
788 if (adev->gfx.kiq.ring.sched.ready &&
789 (amdgpu_sriov_runtime(adev) || !amdgpu_sriov_vf(adev)) &&
790 down_read_trylock(&adev->reset_sem)) {
791 uint32_t req = hub->vm_inv_eng0_req + hub->eng_distance * eng;
792 uint32_t ack = hub->vm_inv_eng0_ack + hub->eng_distance * eng;
794 amdgpu_virt_kiq_reg_write_reg_wait(adev, req, ack, inv_req,
796 up_read(&adev->reset_sem);
800 spin_lock(&adev->gmc.invalidate_lock);
803 * It may lose gpuvm invalidate acknowldege state across power-gating
804 * off cycle, add semaphore acquire before invalidation and semaphore
805 * release after invalidation to avoid entering power gated state
809 /* TODO: It needs to continue working on debugging with semaphore for GFXHUB as well. */
811 for (j = 0; j < adev->usec_timeout; j++) {
812 /* a read return value of 1 means semaphore acquire */
813 if (vmhub == AMDGPU_GFXHUB_0)
814 tmp = RREG32_SOC15_IP_NO_KIQ(GC, hub->vm_inv_eng0_sem + hub->eng_distance * eng);
816 tmp = RREG32_SOC15_IP_NO_KIQ(MMHUB, hub->vm_inv_eng0_sem + hub->eng_distance * eng);
823 if (j >= adev->usec_timeout)
824 DRM_ERROR("Timeout waiting for sem acquire in VM flush!\n");
828 if (vmhub == AMDGPU_GFXHUB_0)
829 WREG32_SOC15_IP_NO_KIQ(GC, hub->vm_inv_eng0_req + hub->eng_distance * eng, inv_req);
831 WREG32_SOC15_IP_NO_KIQ(MMHUB, hub->vm_inv_eng0_req + hub->eng_distance * eng, inv_req);
834 * Issue a dummy read to wait for the ACK register to
835 * be cleared to avoid a false ACK due to the new fast
838 if ((vmhub == AMDGPU_GFXHUB_0) &&
839 (adev->ip_versions[GC_HWIP][0] < IP_VERSION(9, 4, 2)))
840 RREG32_NO_KIQ(hub->vm_inv_eng0_req +
841 hub->eng_distance * eng);
843 for (j = 0; j < adev->usec_timeout; j++) {
844 if (vmhub == AMDGPU_GFXHUB_0)
845 tmp = RREG32_SOC15_IP_NO_KIQ(GC, hub->vm_inv_eng0_ack + hub->eng_distance * eng);
847 tmp = RREG32_SOC15_IP_NO_KIQ(MMHUB, hub->vm_inv_eng0_ack + hub->eng_distance * eng);
849 if (tmp & (1 << vmid))
858 /* TODO: It needs to continue working on debugging with semaphore for GFXHUB as well. */
861 * add semaphore release after invalidation,
862 * write with 0 means semaphore release
864 if (vmhub == AMDGPU_GFXHUB_0)
865 WREG32_SOC15_IP_NO_KIQ(GC, hub->vm_inv_eng0_sem + hub->eng_distance * eng, 0);
867 WREG32_SOC15_IP_NO_KIQ(MMHUB, hub->vm_inv_eng0_sem + hub->eng_distance * eng, 0);
870 spin_unlock(&adev->gmc.invalidate_lock);
872 if (j < adev->usec_timeout)
875 DRM_ERROR("Timeout waiting for VM flush ACK!\n");
879 * gmc_v9_0_flush_gpu_tlb_pasid - tlb flush via pasid
881 * @adev: amdgpu_device pointer
882 * @pasid: pasid to be flush
883 * @flush_type: the flush type
884 * @all_hub: flush all hubs
886 * Flush the TLB for the requested pasid.
888 static int gmc_v9_0_flush_gpu_tlb_pasid(struct amdgpu_device *adev,
889 uint16_t pasid, uint32_t flush_type,
895 uint16_t queried_pasid;
897 struct amdgpu_ring *ring = &adev->gfx.kiq.ring;
898 struct amdgpu_kiq *kiq = &adev->gfx.kiq;
900 if (amdgpu_in_reset(adev))
903 if (ring->sched.ready && down_read_trylock(&adev->reset_sem)) {
904 /* Vega20+XGMI caches PTEs in TC and TLB. Add a
905 * heavy-weight TLB flush (type 2), which flushes
906 * both. Due to a race condition with concurrent
907 * memory accesses using the same TLB cache line, we
908 * still need a second TLB flush after this.
910 bool vega20_xgmi_wa = (adev->gmc.xgmi.num_physical_nodes &&
911 adev->ip_versions[GC_HWIP][0] == IP_VERSION(9, 4, 0));
912 /* 2 dwords flush + 8 dwords fence */
913 unsigned int ndw = kiq->pmf->invalidate_tlbs_size + 8;
916 ndw += kiq->pmf->invalidate_tlbs_size;
918 spin_lock(&adev->gfx.kiq.ring_lock);
919 /* 2 dwords flush + 8 dwords fence */
920 amdgpu_ring_alloc(ring, ndw);
922 kiq->pmf->kiq_invalidate_tlbs(ring,
924 kiq->pmf->kiq_invalidate_tlbs(ring,
925 pasid, flush_type, all_hub);
926 r = amdgpu_fence_emit_polling(ring, &seq, MAX_KIQ_REG_WAIT);
928 amdgpu_ring_undo(ring);
929 spin_unlock(&adev->gfx.kiq.ring_lock);
930 up_read(&adev->reset_sem);
934 amdgpu_ring_commit(ring);
935 spin_unlock(&adev->gfx.kiq.ring_lock);
936 r = amdgpu_fence_wait_polling(ring, seq, adev->usec_timeout);
938 dev_err(adev->dev, "wait for kiq fence error: %ld.\n", r);
939 up_read(&adev->reset_sem);
942 up_read(&adev->reset_sem);
946 for (vmid = 1; vmid < 16; vmid++) {
948 ret = gmc_v9_0_get_atc_vmid_pasid_mapping_info(adev, vmid,
950 if (ret && queried_pasid == pasid) {
952 for (i = 0; i < adev->num_vmhubs; i++)
953 gmc_v9_0_flush_gpu_tlb(adev, vmid,
956 gmc_v9_0_flush_gpu_tlb(adev, vmid,
957 AMDGPU_GFXHUB_0, flush_type);
967 static uint64_t gmc_v9_0_emit_flush_gpu_tlb(struct amdgpu_ring *ring,
968 unsigned vmid, uint64_t pd_addr)
970 bool use_semaphore = gmc_v9_0_use_invalidate_semaphore(ring->adev, ring->funcs->vmhub);
971 struct amdgpu_device *adev = ring->adev;
972 struct amdgpu_vmhub *hub = &adev->vmhub[ring->funcs->vmhub];
973 uint32_t req = gmc_v9_0_get_invalidate_req(vmid, 0);
974 unsigned eng = ring->vm_inv_eng;
977 * It may lose gpuvm invalidate acknowldege state across power-gating
978 * off cycle, add semaphore acquire before invalidation and semaphore
979 * release after invalidation to avoid entering power gated state
983 /* TODO: It needs to continue working on debugging with semaphore for GFXHUB as well. */
985 /* a read return value of 1 means semaphore acuqire */
986 amdgpu_ring_emit_reg_wait(ring,
987 hub->vm_inv_eng0_sem +
988 hub->eng_distance * eng, 0x1, 0x1);
990 amdgpu_ring_emit_wreg(ring, hub->ctx0_ptb_addr_lo32 +
991 (hub->ctx_addr_distance * vmid),
992 lower_32_bits(pd_addr));
994 amdgpu_ring_emit_wreg(ring, hub->ctx0_ptb_addr_hi32 +
995 (hub->ctx_addr_distance * vmid),
996 upper_32_bits(pd_addr));
998 amdgpu_ring_emit_reg_write_reg_wait(ring, hub->vm_inv_eng0_req +
999 hub->eng_distance * eng,
1000 hub->vm_inv_eng0_ack +
1001 hub->eng_distance * eng,
1004 /* TODO: It needs to continue working on debugging with semaphore for GFXHUB as well. */
1007 * add semaphore release after invalidation,
1008 * write with 0 means semaphore release
1010 amdgpu_ring_emit_wreg(ring, hub->vm_inv_eng0_sem +
1011 hub->eng_distance * eng, 0);
1016 static void gmc_v9_0_emit_pasid_mapping(struct amdgpu_ring *ring, unsigned vmid,
1019 struct amdgpu_device *adev = ring->adev;
1022 /* Do nothing because there's no lut register for mmhub1. */
1023 if (ring->funcs->vmhub == AMDGPU_MMHUB_1)
1026 if (ring->funcs->vmhub == AMDGPU_GFXHUB_0)
1027 reg = SOC15_REG_OFFSET(OSSSYS, 0, mmIH_VMID_0_LUT) + vmid;
1029 reg = SOC15_REG_OFFSET(OSSSYS, 0, mmIH_VMID_0_LUT_MM) + vmid;
1031 amdgpu_ring_emit_wreg(ring, reg, pasid);
1035 * PTE format on VEGA 10:
1044 * 47:12 4k physical page base address
1054 * PDE format on VEGA 10:
1055 * 63:59 block fragment size
1059 * 47:6 physical base address of PD or PTE
1066 static uint64_t gmc_v9_0_map_mtype(struct amdgpu_device *adev, uint32_t flags)
1070 case AMDGPU_VM_MTYPE_DEFAULT:
1071 return AMDGPU_PTE_MTYPE_VG10(MTYPE_NC);
1072 case AMDGPU_VM_MTYPE_NC:
1073 return AMDGPU_PTE_MTYPE_VG10(MTYPE_NC);
1074 case AMDGPU_VM_MTYPE_WC:
1075 return AMDGPU_PTE_MTYPE_VG10(MTYPE_WC);
1076 case AMDGPU_VM_MTYPE_RW:
1077 return AMDGPU_PTE_MTYPE_VG10(MTYPE_RW);
1078 case AMDGPU_VM_MTYPE_CC:
1079 return AMDGPU_PTE_MTYPE_VG10(MTYPE_CC);
1080 case AMDGPU_VM_MTYPE_UC:
1081 return AMDGPU_PTE_MTYPE_VG10(MTYPE_UC);
1083 return AMDGPU_PTE_MTYPE_VG10(MTYPE_NC);
1087 static void gmc_v9_0_get_vm_pde(struct amdgpu_device *adev, int level,
1088 uint64_t *addr, uint64_t *flags)
1090 if (!(*flags & AMDGPU_PDE_PTE) && !(*flags & AMDGPU_PTE_SYSTEM))
1091 *addr = amdgpu_gmc_vram_mc2pa(adev, *addr);
1092 BUG_ON(*addr & 0xFFFF00000000003FULL);
1094 if (!adev->gmc.translate_further)
1097 if (level == AMDGPU_VM_PDB1) {
1098 /* Set the block fragment size */
1099 if (!(*flags & AMDGPU_PDE_PTE))
1100 *flags |= AMDGPU_PDE_BFS(0x9);
1102 } else if (level == AMDGPU_VM_PDB0) {
1103 if (*flags & AMDGPU_PDE_PTE)
1104 *flags &= ~AMDGPU_PDE_PTE;
1106 *flags |= AMDGPU_PTE_TF;
1110 static void gmc_v9_0_get_vm_pte(struct amdgpu_device *adev,
1111 struct amdgpu_bo_va_mapping *mapping,
1114 *flags &= ~AMDGPU_PTE_EXECUTABLE;
1115 *flags |= mapping->flags & AMDGPU_PTE_EXECUTABLE;
1117 *flags &= ~AMDGPU_PTE_MTYPE_VG10_MASK;
1118 *flags |= mapping->flags & AMDGPU_PTE_MTYPE_VG10_MASK;
1120 if (mapping->flags & AMDGPU_PTE_PRT) {
1121 *flags |= AMDGPU_PTE_PRT;
1122 *flags &= ~AMDGPU_PTE_VALID;
1125 if ((adev->ip_versions[GC_HWIP][0] == IP_VERSION(9, 4, 1) ||
1126 adev->ip_versions[GC_HWIP][0] == IP_VERSION(9, 4, 2)) &&
1127 !(*flags & AMDGPU_PTE_SYSTEM) &&
1128 mapping->bo_va->is_xgmi)
1129 *flags |= AMDGPU_PTE_SNOOPED;
1131 if (adev->ip_versions[GC_HWIP][0] == IP_VERSION(9, 4, 2))
1132 *flags |= mapping->flags & AMDGPU_PTE_SNOOPED;
1135 static unsigned gmc_v9_0_get_vbios_fb_size(struct amdgpu_device *adev)
1137 u32 d1vga_control = RREG32_SOC15(DCE, 0, mmD1VGA_CONTROL);
1140 /* TODO move to DC so GMC doesn't need to hard-code DCN registers */
1142 if (REG_GET_FIELD(d1vga_control, D1VGA_CONTROL, D1VGA_MODE_ENABLE)) {
1143 size = AMDGPU_VBIOS_VGA_ALLOCATION;
1147 switch (adev->ip_versions[DCE_HWIP][0]) {
1148 case IP_VERSION(1, 0, 0):
1149 case IP_VERSION(1, 0, 1):
1150 viewport = RREG32_SOC15(DCE, 0, mmHUBP0_DCSURF_PRI_VIEWPORT_DIMENSION);
1151 size = (REG_GET_FIELD(viewport,
1152 HUBP0_DCSURF_PRI_VIEWPORT_DIMENSION, PRI_VIEWPORT_HEIGHT) *
1153 REG_GET_FIELD(viewport,
1154 HUBP0_DCSURF_PRI_VIEWPORT_DIMENSION, PRI_VIEWPORT_WIDTH) *
1157 case IP_VERSION(2, 1, 0):
1158 viewport = RREG32_SOC15(DCE, 0, mmHUBP0_DCSURF_PRI_VIEWPORT_DIMENSION_DCN2);
1159 size = (REG_GET_FIELD(viewport,
1160 HUBP0_DCSURF_PRI_VIEWPORT_DIMENSION, PRI_VIEWPORT_HEIGHT) *
1161 REG_GET_FIELD(viewport,
1162 HUBP0_DCSURF_PRI_VIEWPORT_DIMENSION, PRI_VIEWPORT_WIDTH) *
1166 viewport = RREG32_SOC15(DCE, 0, mmSCL0_VIEWPORT_SIZE);
1167 size = (REG_GET_FIELD(viewport, SCL0_VIEWPORT_SIZE, VIEWPORT_HEIGHT) *
1168 REG_GET_FIELD(viewport, SCL0_VIEWPORT_SIZE, VIEWPORT_WIDTH) *
1177 static const struct amdgpu_gmc_funcs gmc_v9_0_gmc_funcs = {
1178 .flush_gpu_tlb = gmc_v9_0_flush_gpu_tlb,
1179 .flush_gpu_tlb_pasid = gmc_v9_0_flush_gpu_tlb_pasid,
1180 .emit_flush_gpu_tlb = gmc_v9_0_emit_flush_gpu_tlb,
1181 .emit_pasid_mapping = gmc_v9_0_emit_pasid_mapping,
1182 .map_mtype = gmc_v9_0_map_mtype,
1183 .get_vm_pde = gmc_v9_0_get_vm_pde,
1184 .get_vm_pte = gmc_v9_0_get_vm_pte,
1185 .get_vbios_fb_size = gmc_v9_0_get_vbios_fb_size,
1188 static void gmc_v9_0_set_gmc_funcs(struct amdgpu_device *adev)
1190 adev->gmc.gmc_funcs = &gmc_v9_0_gmc_funcs;
1193 static void gmc_v9_0_set_umc_funcs(struct amdgpu_device *adev)
1195 switch (adev->ip_versions[UMC_HWIP][0]) {
1196 case IP_VERSION(6, 0, 0):
1197 adev->umc.funcs = &umc_v6_0_funcs;
1199 case IP_VERSION(6, 1, 1):
1200 adev->umc.max_ras_err_cnt_per_query = UMC_V6_1_TOTAL_CHANNEL_NUM;
1201 adev->umc.channel_inst_num = UMC_V6_1_CHANNEL_INSTANCE_NUM;
1202 adev->umc.umc_inst_num = UMC_V6_1_UMC_INSTANCE_NUM;
1203 adev->umc.channel_offs = UMC_V6_1_PER_CHANNEL_OFFSET_VG20;
1204 adev->umc.channel_idx_tbl = &umc_v6_1_channel_idx_tbl[0][0];
1205 adev->umc.ras_funcs = &umc_v6_1_ras_funcs;
1207 case IP_VERSION(6, 1, 2):
1208 adev->umc.max_ras_err_cnt_per_query = UMC_V6_1_TOTAL_CHANNEL_NUM;
1209 adev->umc.channel_inst_num = UMC_V6_1_CHANNEL_INSTANCE_NUM;
1210 adev->umc.umc_inst_num = UMC_V6_1_UMC_INSTANCE_NUM;
1211 adev->umc.channel_offs = UMC_V6_1_PER_CHANNEL_OFFSET_ARCT;
1212 adev->umc.channel_idx_tbl = &umc_v6_1_channel_idx_tbl[0][0];
1213 adev->umc.ras_funcs = &umc_v6_1_ras_funcs;
1215 case IP_VERSION(6, 7, 0):
1216 adev->umc.max_ras_err_cnt_per_query = UMC_V6_7_TOTAL_CHANNEL_NUM;
1217 adev->umc.channel_inst_num = UMC_V6_7_CHANNEL_INSTANCE_NUM;
1218 adev->umc.umc_inst_num = UMC_V6_7_UMC_INSTANCE_NUM;
1219 adev->umc.channel_offs = UMC_V6_7_PER_CHANNEL_OFFSET;
1220 if (!adev->gmc.xgmi.connected_to_cpu)
1221 adev->umc.ras_funcs = &umc_v6_7_ras_funcs;
1222 if (1 & adev->smuio.funcs->get_die_id(adev))
1223 adev->umc.channel_idx_tbl = &umc_v6_7_channel_idx_tbl_first[0][0];
1225 adev->umc.channel_idx_tbl = &umc_v6_7_channel_idx_tbl_second[0][0];
1232 static void gmc_v9_0_set_mmhub_funcs(struct amdgpu_device *adev)
1234 switch (adev->ip_versions[MMHUB_HWIP][0]) {
1235 case IP_VERSION(9, 4, 1):
1236 adev->mmhub.funcs = &mmhub_v9_4_funcs;
1238 case IP_VERSION(9, 4, 2):
1239 adev->mmhub.funcs = &mmhub_v1_7_funcs;
1242 adev->mmhub.funcs = &mmhub_v1_0_funcs;
1247 static void gmc_v9_0_set_mmhub_ras_funcs(struct amdgpu_device *adev)
1249 switch (adev->ip_versions[MMHUB_HWIP][0]) {
1250 case IP_VERSION(9, 4, 0):
1251 adev->mmhub.ras_funcs = &mmhub_v1_0_ras_funcs;
1253 case IP_VERSION(9, 4, 1):
1254 adev->mmhub.ras_funcs = &mmhub_v9_4_ras_funcs;
1256 case IP_VERSION(9, 4, 2):
1257 adev->mmhub.ras_funcs = &mmhub_v1_7_ras_funcs;
1260 /* mmhub ras is not available */
1265 static void gmc_v9_0_set_gfxhub_funcs(struct amdgpu_device *adev)
1267 adev->gfxhub.funcs = &gfxhub_v1_0_funcs;
1270 static void gmc_v9_0_set_hdp_ras_funcs(struct amdgpu_device *adev)
1272 adev->hdp.ras_funcs = &hdp_v4_0_ras_funcs;
1275 static void gmc_v9_0_set_mca_funcs(struct amdgpu_device *adev)
1277 /* is UMC the right IP to check for MCA? Maybe DF? */
1278 switch (adev->ip_versions[UMC_HWIP][0]) {
1279 case IP_VERSION(6, 7, 0):
1280 if (!adev->gmc.xgmi.connected_to_cpu)
1281 adev->mca.funcs = &mca_v3_0_funcs;
1288 static int gmc_v9_0_early_init(void *handle)
1290 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1292 /* ARCT and VEGA20 don't have XGMI defined in their IP discovery tables */
1293 if (adev->asic_type == CHIP_VEGA20 ||
1294 adev->asic_type == CHIP_ARCTURUS)
1295 adev->gmc.xgmi.supported = true;
1297 if (adev->ip_versions[XGMI_HWIP][0] == IP_VERSION(6, 1, 0)) {
1298 adev->gmc.xgmi.supported = true;
1299 adev->gmc.xgmi.connected_to_cpu =
1300 adev->smuio.funcs->is_host_gpu_xgmi_supported(adev);
1303 gmc_v9_0_set_gmc_funcs(adev);
1304 gmc_v9_0_set_irq_funcs(adev);
1305 gmc_v9_0_set_umc_funcs(adev);
1306 gmc_v9_0_set_mmhub_funcs(adev);
1307 gmc_v9_0_set_mmhub_ras_funcs(adev);
1308 gmc_v9_0_set_gfxhub_funcs(adev);
1309 gmc_v9_0_set_hdp_ras_funcs(adev);
1310 gmc_v9_0_set_mca_funcs(adev);
1312 adev->gmc.shared_aperture_start = 0x2000000000000000ULL;
1313 adev->gmc.shared_aperture_end =
1314 adev->gmc.shared_aperture_start + (4ULL << 30) - 1;
1315 adev->gmc.private_aperture_start = 0x1000000000000000ULL;
1316 adev->gmc.private_aperture_end =
1317 adev->gmc.private_aperture_start + (4ULL << 30) - 1;
1322 static int gmc_v9_0_late_init(void *handle)
1324 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1327 r = amdgpu_gmc_allocate_vm_inv_eng(adev);
1332 * Workaround performance drop issue with VBIOS enables partial
1333 * writes, while disables HBM ECC for vega10.
1335 if (!amdgpu_sriov_vf(adev) &&
1336 (adev->ip_versions[UMC_HWIP][0] == IP_VERSION(6, 0, 0))) {
1337 if (!(adev->ras_enabled & (1 << AMDGPU_RAS_BLOCK__UMC))) {
1338 if (adev->df.funcs &&
1339 adev->df.funcs->enable_ecc_force_par_wr_rmw)
1340 adev->df.funcs->enable_ecc_force_par_wr_rmw(adev, false);
1344 if (!amdgpu_persistent_edc_harvesting_supported(adev)) {
1345 if (adev->mmhub.ras_funcs &&
1346 adev->mmhub.ras_funcs->reset_ras_error_count)
1347 adev->mmhub.ras_funcs->reset_ras_error_count(adev);
1349 if (adev->hdp.ras_funcs &&
1350 adev->hdp.ras_funcs->reset_ras_error_count)
1351 adev->hdp.ras_funcs->reset_ras_error_count(adev);
1354 r = amdgpu_gmc_ras_late_init(adev);
1358 return amdgpu_irq_get(adev, &adev->gmc.vm_fault, 0);
1361 static void gmc_v9_0_vram_gtt_location(struct amdgpu_device *adev,
1362 struct amdgpu_gmc *mc)
1364 u64 base = adev->mmhub.funcs->get_fb_location(adev);
1366 /* add the xgmi offset of the physical node */
1367 base += adev->gmc.xgmi.physical_node_id * adev->gmc.xgmi.node_segment_size;
1368 if (adev->gmc.xgmi.connected_to_cpu) {
1369 amdgpu_gmc_sysvm_location(adev, mc);
1371 amdgpu_gmc_vram_location(adev, mc, base);
1372 amdgpu_gmc_gart_location(adev, mc);
1373 amdgpu_gmc_agp_location(adev, mc);
1375 /* base offset of vram pages */
1376 adev->vm_manager.vram_base_offset = adev->gfxhub.funcs->get_mc_fb_offset(adev);
1378 /* XXX: add the xgmi offset of the physical node? */
1379 adev->vm_manager.vram_base_offset +=
1380 adev->gmc.xgmi.physical_node_id * adev->gmc.xgmi.node_segment_size;
1384 * gmc_v9_0_mc_init - initialize the memory controller driver params
1386 * @adev: amdgpu_device pointer
1388 * Look up the amount of vram, vram width, and decide how to place
1389 * vram and gart within the GPU's physical address space.
1390 * Returns 0 for success.
1392 static int gmc_v9_0_mc_init(struct amdgpu_device *adev)
1396 /* size in MB on si */
1397 adev->gmc.mc_vram_size =
1398 adev->nbio.funcs->get_memsize(adev) * 1024ULL * 1024ULL;
1399 adev->gmc.real_vram_size = adev->gmc.mc_vram_size;
1401 if (!(adev->flags & AMD_IS_APU) &&
1402 !adev->gmc.xgmi.connected_to_cpu) {
1403 r = amdgpu_device_resize_fb_bar(adev);
1407 adev->gmc.aper_base = pci_resource_start(adev->pdev, 0);
1408 adev->gmc.aper_size = pci_resource_len(adev->pdev, 0);
1410 #ifdef CONFIG_X86_64
1412 * AMD Accelerated Processing Platform (APP) supporting GPU-HOST xgmi
1413 * interface can use VRAM through here as it appears system reserved
1414 * memory in host address space.
1416 * For APUs, VRAM is just the stolen system memory and can be accessed
1419 * Otherwise, use the legacy Host Data Path (HDP) through PCIe BAR.
1422 /* check whether both host-gpu and gpu-gpu xgmi links exist */
1423 if ((adev->flags & AMD_IS_APU) ||
1424 (adev->gmc.xgmi.supported &&
1425 adev->gmc.xgmi.connected_to_cpu)) {
1426 adev->gmc.aper_base =
1427 adev->gfxhub.funcs->get_mc_fb_offset(adev) +
1428 adev->gmc.xgmi.physical_node_id *
1429 adev->gmc.xgmi.node_segment_size;
1430 adev->gmc.aper_size = adev->gmc.real_vram_size;
1434 /* In case the PCI BAR is larger than the actual amount of vram */
1435 adev->gmc.visible_vram_size = adev->gmc.aper_size;
1436 if (adev->gmc.visible_vram_size > adev->gmc.real_vram_size)
1437 adev->gmc.visible_vram_size = adev->gmc.real_vram_size;
1439 /* set the gart size */
1440 if (amdgpu_gart_size == -1) {
1441 switch (adev->ip_versions[GC_HWIP][0]) {
1442 case IP_VERSION(9, 0, 1): /* all engines support GPUVM */
1443 case IP_VERSION(9, 2, 1): /* all engines support GPUVM */
1444 case IP_VERSION(9, 4, 0):
1445 case IP_VERSION(9, 4, 1):
1446 case IP_VERSION(9, 4, 2):
1448 adev->gmc.gart_size = 512ULL << 20;
1450 case IP_VERSION(9, 1, 0): /* DCE SG support */
1451 case IP_VERSION(9, 2, 2): /* DCE SG support */
1452 case IP_VERSION(9, 3, 0):
1453 adev->gmc.gart_size = 1024ULL << 20;
1457 adev->gmc.gart_size = (u64)amdgpu_gart_size << 20;
1460 adev->gmc.gart_size += adev->pm.smu_prv_buffer_size;
1462 gmc_v9_0_vram_gtt_location(adev, &adev->gmc);
1467 static int gmc_v9_0_gart_init(struct amdgpu_device *adev)
1471 if (adev->gart.bo) {
1472 WARN(1, "VEGA10 PCIE GART already initialized\n");
1476 if (adev->gmc.xgmi.connected_to_cpu) {
1477 adev->gmc.vmid0_page_table_depth = 1;
1478 adev->gmc.vmid0_page_table_block_size = 12;
1480 adev->gmc.vmid0_page_table_depth = 0;
1481 adev->gmc.vmid0_page_table_block_size = 0;
1484 /* Initialize common gart structure */
1485 r = amdgpu_gart_init(adev);
1488 adev->gart.table_size = adev->gart.num_gpu_pages * 8;
1489 adev->gart.gart_pte_flags = AMDGPU_PTE_MTYPE_VG10(MTYPE_UC) |
1490 AMDGPU_PTE_EXECUTABLE;
1492 r = amdgpu_gart_table_vram_alloc(adev);
1496 if (adev->gmc.xgmi.connected_to_cpu) {
1497 r = amdgpu_gmc_pdb0_alloc(adev);
1504 * gmc_v9_0_save_registers - saves regs
1506 * @adev: amdgpu_device pointer
1508 * This saves potential register values that should be
1509 * restored upon resume
1511 static void gmc_v9_0_save_registers(struct amdgpu_device *adev)
1513 if ((adev->ip_versions[DCE_HWIP][0] == IP_VERSION(1, 0, 0)) ||
1514 (adev->ip_versions[DCE_HWIP][0] == IP_VERSION(1, 0, 1)))
1515 adev->gmc.sdpif_register = RREG32_SOC15(DCE, 0, mmDCHUBBUB_SDPIF_MMIO_CNTRL_0);
1518 static int gmc_v9_0_sw_init(void *handle)
1520 int r, vram_width = 0, vram_type = 0, vram_vendor = 0;
1521 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1523 adev->gfxhub.funcs->init(adev);
1525 adev->mmhub.funcs->init(adev);
1526 if (adev->mca.funcs)
1527 adev->mca.funcs->init(adev);
1529 spin_lock_init(&adev->gmc.invalidate_lock);
1531 r = amdgpu_atomfirmware_get_vram_info(adev,
1532 &vram_width, &vram_type, &vram_vendor);
1533 if (amdgpu_sriov_vf(adev))
1534 /* For Vega10 SR-IOV, vram_width can't be read from ATOM as RAVEN,
1535 * and DF related registers is not readable, seems hardcord is the
1536 * only way to set the correct vram_width
1538 adev->gmc.vram_width = 2048;
1539 else if (amdgpu_emu_mode != 1)
1540 adev->gmc.vram_width = vram_width;
1542 if (!adev->gmc.vram_width) {
1543 int chansize, numchan;
1545 /* hbm memory channel size */
1546 if (adev->flags & AMD_IS_APU)
1550 if (adev->df.funcs &&
1551 adev->df.funcs->get_hbm_channel_number) {
1552 numchan = adev->df.funcs->get_hbm_channel_number(adev);
1553 adev->gmc.vram_width = numchan * chansize;
1557 adev->gmc.vram_type = vram_type;
1558 adev->gmc.vram_vendor = vram_vendor;
1559 switch (adev->ip_versions[GC_HWIP][0]) {
1560 case IP_VERSION(9, 1, 0):
1561 case IP_VERSION(9, 2, 2):
1562 adev->num_vmhubs = 2;
1564 if (adev->rev_id == 0x0 || adev->rev_id == 0x1) {
1565 amdgpu_vm_adjust_size(adev, 256 * 1024, 9, 3, 48);
1567 /* vm_size is 128TB + 512GB for legacy 3-level page support */
1568 amdgpu_vm_adjust_size(adev, 128 * 1024 + 512, 9, 2, 48);
1569 adev->gmc.translate_further =
1570 adev->vm_manager.num_level > 1;
1573 case IP_VERSION(9, 0, 1):
1574 case IP_VERSION(9, 2, 1):
1575 case IP_VERSION(9, 4, 0):
1576 case IP_VERSION(9, 3, 0):
1577 case IP_VERSION(9, 4, 2):
1578 adev->num_vmhubs = 2;
1582 * To fulfill 4-level page support,
1583 * vm size is 256TB (48bit), maximum size of Vega10,
1584 * block size 512 (9bit)
1586 /* sriov restrict max_pfn below AMDGPU_GMC_HOLE */
1587 if (amdgpu_sriov_vf(adev))
1588 amdgpu_vm_adjust_size(adev, 256 * 1024, 9, 3, 47);
1590 amdgpu_vm_adjust_size(adev, 256 * 1024, 9, 3, 48);
1592 case IP_VERSION(9, 4, 1):
1593 adev->num_vmhubs = 3;
1595 /* Keep the vm size same with Vega20 */
1596 amdgpu_vm_adjust_size(adev, 256 * 1024, 9, 3, 48);
1602 /* This interrupt is VMC page fault.*/
1603 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_VMC, VMC_1_0__SRCID__VM_FAULT,
1604 &adev->gmc.vm_fault);
1608 if (adev->ip_versions[GC_HWIP][0] == IP_VERSION(9, 4, 1)) {
1609 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_VMC1, VMC_1_0__SRCID__VM_FAULT,
1610 &adev->gmc.vm_fault);
1615 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_UTCL2, UTCL2_1_0__SRCID__FAULT,
1616 &adev->gmc.vm_fault);
1621 if (!amdgpu_sriov_vf(adev) &&
1622 !adev->gmc.xgmi.connected_to_cpu) {
1623 /* interrupt sent to DF. */
1624 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DF, 0,
1625 &adev->gmc.ecc_irq);
1630 /* Set the internal MC address mask
1631 * This is the max address of the GPU's
1632 * internal address space.
1634 adev->gmc.mc_mask = 0xffffffffffffULL; /* 48 bit MC */
1636 r = dma_set_mask_and_coherent(adev->dev, DMA_BIT_MASK(44));
1638 printk(KERN_WARNING "amdgpu: No suitable DMA available.\n");
1641 adev->need_swiotlb = drm_need_swiotlb(44);
1643 r = gmc_v9_0_mc_init(adev);
1647 amdgpu_gmc_get_vbios_allocations(adev);
1649 /* Memory manager */
1650 r = amdgpu_bo_init(adev);
1654 r = gmc_v9_0_gart_init(adev);
1660 * VMID 0 is reserved for System
1661 * amdgpu graphics/compute will use VMIDs 1..n-1
1662 * amdkfd will use VMIDs n..15
1664 * The first KFD VMID is 8 for GPUs with graphics, 3 for
1665 * compute-only GPUs. On compute-only GPUs that leaves 2 VMIDs
1666 * for video processing.
1668 adev->vm_manager.first_kfd_vmid =
1669 (adev->ip_versions[GC_HWIP][0] == IP_VERSION(9, 4, 1) ||
1670 adev->ip_versions[GC_HWIP][0] == IP_VERSION(9, 4, 2)) ? 3 : 8;
1672 amdgpu_vm_manager_init(adev);
1674 gmc_v9_0_save_registers(adev);
1679 static int gmc_v9_0_sw_fini(void *handle)
1681 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1683 amdgpu_gmc_ras_fini(adev);
1684 amdgpu_gem_force_release(adev);
1685 amdgpu_vm_manager_fini(adev);
1686 amdgpu_gart_table_vram_free(adev);
1687 amdgpu_bo_unref(&adev->gmc.pdb0_bo);
1688 amdgpu_bo_fini(adev);
1693 static void gmc_v9_0_init_golden_registers(struct amdgpu_device *adev)
1696 switch (adev->ip_versions[MMHUB_HWIP][0]) {
1697 case IP_VERSION(9, 0, 0):
1698 if (amdgpu_sriov_vf(adev))
1701 case IP_VERSION(9, 4, 0):
1702 soc15_program_register_sequence(adev,
1703 golden_settings_mmhub_1_0_0,
1704 ARRAY_SIZE(golden_settings_mmhub_1_0_0));
1705 soc15_program_register_sequence(adev,
1706 golden_settings_athub_1_0_0,
1707 ARRAY_SIZE(golden_settings_athub_1_0_0));
1709 case IP_VERSION(9, 1, 0):
1710 case IP_VERSION(9, 2, 0):
1711 /* TODO for renoir */
1712 soc15_program_register_sequence(adev,
1713 golden_settings_athub_1_0_0,
1714 ARRAY_SIZE(golden_settings_athub_1_0_0));
1722 * gmc_v9_0_restore_registers - restores regs
1724 * @adev: amdgpu_device pointer
1726 * This restores register values, saved at suspend.
1728 void gmc_v9_0_restore_registers(struct amdgpu_device *adev)
1730 if ((adev->ip_versions[DCE_HWIP][0] == IP_VERSION(1, 0, 0)) ||
1731 (adev->ip_versions[DCE_HWIP][0] == IP_VERSION(1, 0, 1))) {
1732 WREG32_SOC15(DCE, 0, mmDCHUBBUB_SDPIF_MMIO_CNTRL_0, adev->gmc.sdpif_register);
1733 WARN_ON(adev->gmc.sdpif_register !=
1734 RREG32_SOC15(DCE, 0, mmDCHUBBUB_SDPIF_MMIO_CNTRL_0));
1739 * gmc_v9_0_gart_enable - gart enable
1741 * @adev: amdgpu_device pointer
1743 static int gmc_v9_0_gart_enable(struct amdgpu_device *adev)
1747 if (adev->gmc.xgmi.connected_to_cpu)
1748 amdgpu_gmc_init_pdb0(adev);
1750 if (adev->gart.bo == NULL) {
1751 dev_err(adev->dev, "No VRAM object for PCIE GART.\n");
1755 if (amdgpu_sriov_vf(adev) && amdgpu_in_reset(adev))
1758 r = amdgpu_gtt_mgr_recover(&adev->mman.gtt_mgr);
1763 r = adev->gfxhub.funcs->gart_enable(adev);
1767 r = adev->mmhub.funcs->gart_enable(adev);
1771 DRM_INFO("PCIE GART of %uM enabled.\n",
1772 (unsigned)(adev->gmc.gart_size >> 20));
1773 if (adev->gmc.pdb0_bo)
1774 DRM_INFO("PDB0 located at 0x%016llX\n",
1775 (unsigned long long)amdgpu_bo_gpu_offset(adev->gmc.pdb0_bo));
1776 DRM_INFO("PTB located at 0x%016llX\n",
1777 (unsigned long long)amdgpu_bo_gpu_offset(adev->gart.bo));
1779 adev->gart.ready = true;
1783 static int gmc_v9_0_hw_init(void *handle)
1785 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1789 /* The sequence of these two function calls matters.*/
1790 gmc_v9_0_init_golden_registers(adev);
1792 if (adev->mode_info.num_crtc) {
1793 /* Lockout access through VGA aperture*/
1794 WREG32_FIELD15(DCE, 0, VGA_HDP_CONTROL, VGA_MEMORY_DISABLE, 1);
1795 /* disable VGA render */
1796 WREG32_FIELD15(DCE, 0, VGA_RENDER_CONTROL, VGA_VSTATUS_CNTL, 0);
1799 if (adev->mmhub.funcs->update_power_gating)
1800 adev->mmhub.funcs->update_power_gating(adev, true);
1802 adev->hdp.funcs->init_registers(adev);
1804 /* After HDP is initialized, flush HDP.*/
1805 adev->hdp.funcs->flush_hdp(adev, NULL);
1807 if (amdgpu_vm_fault_stop == AMDGPU_VM_FAULT_STOP_ALWAYS)
1812 if (!amdgpu_sriov_vf(adev)) {
1813 adev->gfxhub.funcs->set_fault_enable_default(adev, value);
1814 adev->mmhub.funcs->set_fault_enable_default(adev, value);
1816 for (i = 0; i < adev->num_vmhubs; ++i)
1817 gmc_v9_0_flush_gpu_tlb(adev, 0, i, 0);
1819 if (adev->umc.funcs && adev->umc.funcs->init_registers)
1820 adev->umc.funcs->init_registers(adev);
1822 return gmc_v9_0_gart_enable(adev);
1826 * gmc_v9_0_gart_disable - gart disable
1828 * @adev: amdgpu_device pointer
1830 * This disables all VM page table.
1832 static void gmc_v9_0_gart_disable(struct amdgpu_device *adev)
1834 adev->gfxhub.funcs->gart_disable(adev);
1835 adev->mmhub.funcs->gart_disable(adev);
1838 static int gmc_v9_0_hw_fini(void *handle)
1840 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1842 gmc_v9_0_gart_disable(adev);
1844 if (amdgpu_sriov_vf(adev)) {
1845 /* full access mode, so don't touch any GMC register */
1846 DRM_DEBUG("For SRIOV client, shouldn't do anything.\n");
1851 * Pair the operations did in gmc_v9_0_hw_init and thus maintain
1852 * a correct cached state for GMC. Otherwise, the "gate" again
1853 * operation on S3 resuming will fail due to wrong cached state.
1855 if (adev->mmhub.funcs->update_power_gating)
1856 adev->mmhub.funcs->update_power_gating(adev, false);
1858 amdgpu_irq_put(adev, &adev->gmc.ecc_irq, 0);
1859 amdgpu_irq_put(adev, &adev->gmc.vm_fault, 0);
1864 static int gmc_v9_0_suspend(void *handle)
1866 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1868 return gmc_v9_0_hw_fini(adev);
1871 static int gmc_v9_0_resume(void *handle)
1874 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1876 r = gmc_v9_0_hw_init(adev);
1880 amdgpu_vmid_reset_all(adev);
1885 static bool gmc_v9_0_is_idle(void *handle)
1887 /* MC is always ready in GMC v9.*/
1891 static int gmc_v9_0_wait_for_idle(void *handle)
1893 /* There is no need to wait for MC idle in GMC v9.*/
1897 static int gmc_v9_0_soft_reset(void *handle)
1899 /* XXX for emulation.*/
1903 static int gmc_v9_0_set_clockgating_state(void *handle,
1904 enum amd_clockgating_state state)
1906 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1908 adev->mmhub.funcs->set_clockgating(adev, state);
1910 athub_v1_0_set_clockgating(adev, state);
1915 static void gmc_v9_0_get_clockgating_state(void *handle, u32 *flags)
1917 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1919 adev->mmhub.funcs->get_clockgating(adev, flags);
1921 athub_v1_0_get_clockgating(adev, flags);
1924 static int gmc_v9_0_set_powergating_state(void *handle,
1925 enum amd_powergating_state state)
1930 const struct amd_ip_funcs gmc_v9_0_ip_funcs = {
1932 .early_init = gmc_v9_0_early_init,
1933 .late_init = gmc_v9_0_late_init,
1934 .sw_init = gmc_v9_0_sw_init,
1935 .sw_fini = gmc_v9_0_sw_fini,
1936 .hw_init = gmc_v9_0_hw_init,
1937 .hw_fini = gmc_v9_0_hw_fini,
1938 .suspend = gmc_v9_0_suspend,
1939 .resume = gmc_v9_0_resume,
1940 .is_idle = gmc_v9_0_is_idle,
1941 .wait_for_idle = gmc_v9_0_wait_for_idle,
1942 .soft_reset = gmc_v9_0_soft_reset,
1943 .set_clockgating_state = gmc_v9_0_set_clockgating_state,
1944 .set_powergating_state = gmc_v9_0_set_powergating_state,
1945 .get_clockgating_state = gmc_v9_0_get_clockgating_state,
1948 const struct amdgpu_ip_block_version gmc_v9_0_ip_block =
1950 .type = AMD_IP_BLOCK_TYPE_GMC,
1954 .funcs = &gmc_v9_0_ip_funcs,