Loading...
1/*
2 * Copyright 2019 Advanced Micro Devices, Inc.
3 *
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:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
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.
21 *
22 */
23#include <linux/firmware.h>
24#include <linux/pci.h>
25
26#include <drm/drm_cache.h>
27
28#include "amdgpu.h"
29#include "amdgpu_atomfirmware.h"
30#include "gmc_v10_0.h"
31#include "umc_v8_7.h"
32
33#include "athub/athub_2_0_0_sh_mask.h"
34#include "athub/athub_2_0_0_offset.h"
35#include "dcn/dcn_2_0_0_offset.h"
36#include "dcn/dcn_2_0_0_sh_mask.h"
37#include "oss/osssys_5_0_0_offset.h"
38#include "ivsrcid/vmc/irqsrcs_vmc_1_0.h"
39#include "navi10_enum.h"
40
41#include "soc15.h"
42#include "soc15d.h"
43#include "soc15_common.h"
44
45#include "nbio_v2_3.h"
46
47#include "gfxhub_v2_0.h"
48#include "gfxhub_v2_1.h"
49#include "mmhub_v2_0.h"
50#include "mmhub_v2_3.h"
51#include "athub_v2_0.h"
52#include "athub_v2_1.h"
53
54#include "amdgpu_reset.h"
55
56#if 0
57static const struct soc15_reg_golden golden_settings_navi10_hdp[] =
58{
59 /* TODO add golden setting for hdp */
60};
61#endif
62
63static int gmc_v10_0_ecc_interrupt_state(struct amdgpu_device *adev,
64 struct amdgpu_irq_src *src,
65 unsigned type,
66 enum amdgpu_interrupt_state state)
67{
68 return 0;
69}
70
71static int
72gmc_v10_0_vm_fault_interrupt_state(struct amdgpu_device *adev,
73 struct amdgpu_irq_src *src, unsigned type,
74 enum amdgpu_interrupt_state state)
75{
76 switch (state) {
77 case AMDGPU_IRQ_STATE_DISABLE:
78 /* MM HUB */
79 amdgpu_gmc_set_vm_fault_masks(adev, AMDGPU_MMHUB_0, false);
80 /* GFX HUB */
81 amdgpu_gmc_set_vm_fault_masks(adev, AMDGPU_GFXHUB_0, false);
82 break;
83 case AMDGPU_IRQ_STATE_ENABLE:
84 /* MM HUB */
85 amdgpu_gmc_set_vm_fault_masks(adev, AMDGPU_MMHUB_0, true);
86 /* GFX HUB */
87 amdgpu_gmc_set_vm_fault_masks(adev, AMDGPU_GFXHUB_0, true);
88 break;
89 default:
90 break;
91 }
92
93 return 0;
94}
95
96static int gmc_v10_0_process_interrupt(struct amdgpu_device *adev,
97 struct amdgpu_irq_src *source,
98 struct amdgpu_iv_entry *entry)
99{
100 bool retry_fault = !!(entry->src_data[1] & 0x80);
101 bool write_fault = !!(entry->src_data[1] & 0x20);
102 struct amdgpu_vmhub *hub = &adev->vmhub[entry->vmid_src];
103 struct amdgpu_task_info task_info;
104 uint32_t status = 0;
105 u64 addr;
106
107 addr = (u64)entry->src_data[0] << 12;
108 addr |= ((u64)entry->src_data[1] & 0xf) << 44;
109
110 if (retry_fault) {
111 /* Returning 1 here also prevents sending the IV to the KFD */
112
113 /* Process it onyl if it's the first fault for this address */
114 if (entry->ih != &adev->irq.ih_soft &&
115 amdgpu_gmc_filter_faults(adev, entry->ih, addr, entry->pasid,
116 entry->timestamp))
117 return 1;
118
119 /* Delegate it to a different ring if the hardware hasn't
120 * already done it.
121 */
122 if (entry->ih == &adev->irq.ih) {
123 amdgpu_irq_delegate(adev, entry, 8);
124 return 1;
125 }
126
127 /* Try to handle the recoverable page faults by filling page
128 * tables
129 */
130 if (amdgpu_vm_handle_fault(adev, entry->pasid, addr, write_fault))
131 return 1;
132 }
133
134 if (!amdgpu_sriov_vf(adev)) {
135 /*
136 * Issue a dummy read to wait for the status register to
137 * be updated to avoid reading an incorrect value due to
138 * the new fast GRBM interface.
139 */
140 if ((entry->vmid_src == AMDGPU_GFXHUB_0) &&
141 (adev->ip_versions[GC_HWIP][0] < IP_VERSION(10, 3, 0)))
142 RREG32(hub->vm_l2_pro_fault_status);
143
144 status = RREG32(hub->vm_l2_pro_fault_status);
145 WREG32_P(hub->vm_l2_pro_fault_cntl, 1, ~1);
146 }
147
148 if (!printk_ratelimit())
149 return 0;
150
151 memset(&task_info, 0, sizeof(struct amdgpu_task_info));
152 amdgpu_vm_get_task_info(adev, entry->pasid, &task_info);
153
154 dev_err(adev->dev,
155 "[%s] page fault (src_id:%u ring:%u vmid:%u pasid:%u, "
156 "for process %s pid %d thread %s pid %d)\n",
157 entry->vmid_src ? "mmhub" : "gfxhub",
158 entry->src_id, entry->ring_id, entry->vmid,
159 entry->pasid, task_info.process_name, task_info.tgid,
160 task_info.task_name, task_info.pid);
161 dev_err(adev->dev, " in page starting at address 0x%016llx from client 0x%x (%s)\n",
162 addr, entry->client_id,
163 soc15_ih_clientid_name[entry->client_id]);
164
165 if (!amdgpu_sriov_vf(adev))
166 hub->vmhub_funcs->print_l2_protection_fault_status(adev,
167 status);
168
169 return 0;
170}
171
172static const struct amdgpu_irq_src_funcs gmc_v10_0_irq_funcs = {
173 .set = gmc_v10_0_vm_fault_interrupt_state,
174 .process = gmc_v10_0_process_interrupt,
175};
176
177static const struct amdgpu_irq_src_funcs gmc_v10_0_ecc_funcs = {
178 .set = gmc_v10_0_ecc_interrupt_state,
179 .process = amdgpu_umc_process_ecc_irq,
180};
181
182static void gmc_v10_0_set_irq_funcs(struct amdgpu_device *adev)
183{
184 adev->gmc.vm_fault.num_types = 1;
185 adev->gmc.vm_fault.funcs = &gmc_v10_0_irq_funcs;
186
187 if (!amdgpu_sriov_vf(adev)) {
188 adev->gmc.ecc_irq.num_types = 1;
189 adev->gmc.ecc_irq.funcs = &gmc_v10_0_ecc_funcs;
190 }
191}
192
193/**
194 * gmc_v10_0_use_invalidate_semaphore - judge whether to use semaphore
195 *
196 * @adev: amdgpu_device pointer
197 * @vmhub: vmhub type
198 *
199 */
200static bool gmc_v10_0_use_invalidate_semaphore(struct amdgpu_device *adev,
201 uint32_t vmhub)
202{
203 return ((vmhub == AMDGPU_MMHUB_0 ||
204 vmhub == AMDGPU_MMHUB_1) &&
205 (!amdgpu_sriov_vf(adev)));
206}
207
208static bool gmc_v10_0_get_atc_vmid_pasid_mapping_info(
209 struct amdgpu_device *adev,
210 uint8_t vmid, uint16_t *p_pasid)
211{
212 uint32_t value;
213
214 value = RREG32(SOC15_REG_OFFSET(ATHUB, 0, mmATC_VMID0_PASID_MAPPING)
215 + vmid);
216 *p_pasid = value & ATC_VMID0_PASID_MAPPING__PASID_MASK;
217
218 return !!(value & ATC_VMID0_PASID_MAPPING__VALID_MASK);
219}
220
221/*
222 * GART
223 * VMID 0 is the physical GPU addresses as used by the kernel.
224 * VMIDs 1-15 are used for userspace clients and are handled
225 * by the amdgpu vm/hsa code.
226 */
227
228static void gmc_v10_0_flush_vm_hub(struct amdgpu_device *adev, uint32_t vmid,
229 unsigned int vmhub, uint32_t flush_type)
230{
231 bool use_semaphore = gmc_v10_0_use_invalidate_semaphore(adev, vmhub);
232 struct amdgpu_vmhub *hub = &adev->vmhub[vmhub];
233 u32 inv_req = hub->vmhub_funcs->get_invalidate_req(vmid, flush_type);
234 u32 tmp;
235 /* Use register 17 for GART */
236 const unsigned eng = 17;
237 unsigned int i;
238 unsigned char hub_ip = 0;
239
240 hub_ip = (vmhub == AMDGPU_GFXHUB_0) ?
241 GC_HWIP : MMHUB_HWIP;
242
243 spin_lock(&adev->gmc.invalidate_lock);
244 /*
245 * It may lose gpuvm invalidate acknowldege state across power-gating
246 * off cycle, add semaphore acquire before invalidation and semaphore
247 * release after invalidation to avoid entering power gated state
248 * to WA the Issue
249 */
250
251 /* TODO: It needs to continue working on debugging with semaphore for GFXHUB as well. */
252 if (use_semaphore) {
253 for (i = 0; i < adev->usec_timeout; i++) {
254 /* a read return value of 1 means semaphore acuqire */
255 tmp = RREG32_RLC_NO_KIQ(hub->vm_inv_eng0_sem +
256 hub->eng_distance * eng, hub_ip);
257
258 if (tmp & 0x1)
259 break;
260 udelay(1);
261 }
262
263 if (i >= adev->usec_timeout)
264 DRM_ERROR("Timeout waiting for sem acquire in VM flush!\n");
265 }
266
267 WREG32_RLC_NO_KIQ(hub->vm_inv_eng0_req +
268 hub->eng_distance * eng,
269 inv_req, hub_ip);
270
271 /*
272 * Issue a dummy read to wait for the ACK register to be cleared
273 * to avoid a false ACK due to the new fast GRBM interface.
274 */
275 if ((vmhub == AMDGPU_GFXHUB_0) &&
276 (adev->ip_versions[GC_HWIP][0] < IP_VERSION(10, 3, 0)))
277 RREG32_RLC_NO_KIQ(hub->vm_inv_eng0_req +
278 hub->eng_distance * eng, hub_ip);
279
280 /* Wait for ACK with a delay.*/
281 for (i = 0; i < adev->usec_timeout; i++) {
282 tmp = RREG32_RLC_NO_KIQ(hub->vm_inv_eng0_ack +
283 hub->eng_distance * eng, hub_ip);
284
285 tmp &= 1 << vmid;
286 if (tmp)
287 break;
288
289 udelay(1);
290 }
291
292 /* TODO: It needs to continue working on debugging with semaphore for GFXHUB as well. */
293 if (use_semaphore)
294 /*
295 * add semaphore release after invalidation,
296 * write with 0 means semaphore release
297 */
298 WREG32_RLC_NO_KIQ(hub->vm_inv_eng0_sem +
299 hub->eng_distance * eng, 0, hub_ip);
300
301 spin_unlock(&adev->gmc.invalidate_lock);
302
303 if (i < adev->usec_timeout)
304 return;
305
306 DRM_ERROR("Timeout waiting for VM flush hub: %d!\n", vmhub);
307}
308
309/**
310 * gmc_v10_0_flush_gpu_tlb - gart tlb flush callback
311 *
312 * @adev: amdgpu_device pointer
313 * @vmid: vm instance to flush
314 * @vmhub: vmhub type
315 * @flush_type: the flush type
316 *
317 * Flush the TLB for the requested page table.
318 */
319static void gmc_v10_0_flush_gpu_tlb(struct amdgpu_device *adev, uint32_t vmid,
320 uint32_t vmhub, uint32_t flush_type)
321{
322 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
323 struct dma_fence *fence;
324 struct amdgpu_job *job;
325
326 int r;
327
328 /* flush hdp cache */
329 adev->hdp.funcs->flush_hdp(adev, NULL);
330
331 /* For SRIOV run time, driver shouldn't access the register through MMIO
332 * Directly use kiq to do the vm invalidation instead
333 */
334 if (adev->gfx.kiq.ring.sched.ready && !adev->enable_mes &&
335 (amdgpu_sriov_runtime(adev) || !amdgpu_sriov_vf(adev)) &&
336 down_read_trylock(&adev->reset_domain->sem)) {
337 struct amdgpu_vmhub *hub = &adev->vmhub[vmhub];
338 const unsigned eng = 17;
339 u32 inv_req = hub->vmhub_funcs->get_invalidate_req(vmid, flush_type);
340 u32 req = hub->vm_inv_eng0_req + hub->eng_distance * eng;
341 u32 ack = hub->vm_inv_eng0_ack + hub->eng_distance * eng;
342
343 amdgpu_virt_kiq_reg_write_reg_wait(adev, req, ack, inv_req,
344 1 << vmid);
345
346 up_read(&adev->reset_domain->sem);
347 return;
348 }
349
350 mutex_lock(&adev->mman.gtt_window_lock);
351
352 if (vmhub == AMDGPU_MMHUB_0) {
353 gmc_v10_0_flush_vm_hub(adev, vmid, AMDGPU_MMHUB_0, 0);
354 mutex_unlock(&adev->mman.gtt_window_lock);
355 return;
356 }
357
358 BUG_ON(vmhub != AMDGPU_GFXHUB_0);
359
360 if (!adev->mman.buffer_funcs_enabled ||
361 !adev->ib_pool_ready ||
362 amdgpu_in_reset(adev) ||
363 ring->sched.ready == false) {
364 gmc_v10_0_flush_vm_hub(adev, vmid, AMDGPU_GFXHUB_0, 0);
365 mutex_unlock(&adev->mman.gtt_window_lock);
366 return;
367 }
368
369 /* The SDMA on Navi has a bug which can theoretically result in memory
370 * corruption if an invalidation happens at the same time as an VA
371 * translation. Avoid this by doing the invalidation from the SDMA
372 * itself.
373 */
374 r = amdgpu_job_alloc_with_ib(ring->adev, &adev->mman.entity,
375 AMDGPU_FENCE_OWNER_UNDEFINED,
376 16 * 4, AMDGPU_IB_POOL_IMMEDIATE,
377 &job);
378 if (r)
379 goto error_alloc;
380
381 job->vm_pd_addr = amdgpu_gmc_pd_addr(adev->gart.bo);
382 job->vm_needs_flush = true;
383 job->ibs->ptr[job->ibs->length_dw++] = ring->funcs->nop;
384 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
385 fence = amdgpu_job_submit(job);
386
387 mutex_unlock(&adev->mman.gtt_window_lock);
388
389 dma_fence_wait(fence, false);
390 dma_fence_put(fence);
391
392 return;
393
394error_alloc:
395 mutex_unlock(&adev->mman.gtt_window_lock);
396 DRM_ERROR("Error flushing GPU TLB using the SDMA (%d)!\n", r);
397}
398
399/**
400 * gmc_v10_0_flush_gpu_tlb_pasid - tlb flush via pasid
401 *
402 * @adev: amdgpu_device pointer
403 * @pasid: pasid to be flush
404 * @flush_type: the flush type
405 * @all_hub: Used with PACKET3_INVALIDATE_TLBS_ALL_HUB()
406 *
407 * Flush the TLB for the requested pasid.
408 */
409static int gmc_v10_0_flush_gpu_tlb_pasid(struct amdgpu_device *adev,
410 uint16_t pasid, uint32_t flush_type,
411 bool all_hub)
412{
413 int vmid, i;
414 signed long r;
415 uint32_t seq;
416 uint16_t queried_pasid;
417 bool ret;
418 u32 usec_timeout = amdgpu_sriov_vf(adev) ? SRIOV_USEC_TIMEOUT : adev->usec_timeout;
419 struct amdgpu_ring *ring = &adev->gfx.kiq.ring;
420 struct amdgpu_kiq *kiq = &adev->gfx.kiq;
421
422 if (amdgpu_emu_mode == 0 && ring->sched.ready) {
423 spin_lock(&adev->gfx.kiq.ring_lock);
424 /* 2 dwords flush + 8 dwords fence */
425 amdgpu_ring_alloc(ring, kiq->pmf->invalidate_tlbs_size + 8);
426 kiq->pmf->kiq_invalidate_tlbs(ring,
427 pasid, flush_type, all_hub);
428 r = amdgpu_fence_emit_polling(ring, &seq, MAX_KIQ_REG_WAIT);
429 if (r) {
430 amdgpu_ring_undo(ring);
431 spin_unlock(&adev->gfx.kiq.ring_lock);
432 return -ETIME;
433 }
434
435 amdgpu_ring_commit(ring);
436 spin_unlock(&adev->gfx.kiq.ring_lock);
437 r = amdgpu_fence_wait_polling(ring, seq, usec_timeout);
438 if (r < 1) {
439 dev_err(adev->dev, "wait for kiq fence error: %ld.\n", r);
440 return -ETIME;
441 }
442
443 return 0;
444 }
445
446 for (vmid = 1; vmid < AMDGPU_NUM_VMID; vmid++) {
447
448 ret = gmc_v10_0_get_atc_vmid_pasid_mapping_info(adev, vmid,
449 &queried_pasid);
450 if (ret && queried_pasid == pasid) {
451 if (all_hub) {
452 for (i = 0; i < adev->num_vmhubs; i++)
453 gmc_v10_0_flush_gpu_tlb(adev, vmid,
454 i, flush_type);
455 } else {
456 gmc_v10_0_flush_gpu_tlb(adev, vmid,
457 AMDGPU_GFXHUB_0, flush_type);
458 }
459 if (!adev->enable_mes)
460 break;
461 }
462 }
463
464 return 0;
465}
466
467static uint64_t gmc_v10_0_emit_flush_gpu_tlb(struct amdgpu_ring *ring,
468 unsigned vmid, uint64_t pd_addr)
469{
470 bool use_semaphore = gmc_v10_0_use_invalidate_semaphore(ring->adev, ring->funcs->vmhub);
471 struct amdgpu_vmhub *hub = &ring->adev->vmhub[ring->funcs->vmhub];
472 uint32_t req = hub->vmhub_funcs->get_invalidate_req(vmid, 0);
473 unsigned eng = ring->vm_inv_eng;
474
475 /*
476 * It may lose gpuvm invalidate acknowldege state across power-gating
477 * off cycle, add semaphore acquire before invalidation and semaphore
478 * release after invalidation to avoid entering power gated state
479 * to WA the Issue
480 */
481
482 /* TODO: It needs to continue working on debugging with semaphore for GFXHUB as well. */
483 if (use_semaphore)
484 /* a read return value of 1 means semaphore acuqire */
485 amdgpu_ring_emit_reg_wait(ring,
486 hub->vm_inv_eng0_sem +
487 hub->eng_distance * eng, 0x1, 0x1);
488
489 amdgpu_ring_emit_wreg(ring, hub->ctx0_ptb_addr_lo32 +
490 (hub->ctx_addr_distance * vmid),
491 lower_32_bits(pd_addr));
492
493 amdgpu_ring_emit_wreg(ring, hub->ctx0_ptb_addr_hi32 +
494 (hub->ctx_addr_distance * vmid),
495 upper_32_bits(pd_addr));
496
497 amdgpu_ring_emit_reg_write_reg_wait(ring, hub->vm_inv_eng0_req +
498 hub->eng_distance * eng,
499 hub->vm_inv_eng0_ack +
500 hub->eng_distance * eng,
501 req, 1 << vmid);
502
503 /* TODO: It needs to continue working on debugging with semaphore for GFXHUB as well. */
504 if (use_semaphore)
505 /*
506 * add semaphore release after invalidation,
507 * write with 0 means semaphore release
508 */
509 amdgpu_ring_emit_wreg(ring, hub->vm_inv_eng0_sem +
510 hub->eng_distance * eng, 0);
511
512 return pd_addr;
513}
514
515static void gmc_v10_0_emit_pasid_mapping(struct amdgpu_ring *ring, unsigned vmid,
516 unsigned pasid)
517{
518 struct amdgpu_device *adev = ring->adev;
519 uint32_t reg;
520
521 /* MES fw manages IH_VMID_x_LUT updating */
522 if (ring->is_mes_queue)
523 return;
524
525 if (ring->funcs->vmhub == AMDGPU_GFXHUB_0)
526 reg = SOC15_REG_OFFSET(OSSSYS, 0, mmIH_VMID_0_LUT) + vmid;
527 else
528 reg = SOC15_REG_OFFSET(OSSSYS, 0, mmIH_VMID_0_LUT_MM) + vmid;
529
530 amdgpu_ring_emit_wreg(ring, reg, pasid);
531}
532
533/*
534 * PTE format on NAVI 10:
535 * 63:59 reserved
536 * 58 reserved and for sienna_cichlid is used for MALL noalloc
537 * 57 reserved
538 * 56 F
539 * 55 L
540 * 54 reserved
541 * 53:52 SW
542 * 51 T
543 * 50:48 mtype
544 * 47:12 4k physical page base address
545 * 11:7 fragment
546 * 6 write
547 * 5 read
548 * 4 exe
549 * 3 Z
550 * 2 snooped
551 * 1 system
552 * 0 valid
553 *
554 * PDE format on NAVI 10:
555 * 63:59 block fragment size
556 * 58:55 reserved
557 * 54 P
558 * 53:48 reserved
559 * 47:6 physical base address of PD or PTE
560 * 5:3 reserved
561 * 2 C
562 * 1 system
563 * 0 valid
564 */
565
566static uint64_t gmc_v10_0_map_mtype(struct amdgpu_device *adev, uint32_t flags)
567{
568 switch (flags) {
569 case AMDGPU_VM_MTYPE_DEFAULT:
570 return AMDGPU_PTE_MTYPE_NV10(MTYPE_NC);
571 case AMDGPU_VM_MTYPE_NC:
572 return AMDGPU_PTE_MTYPE_NV10(MTYPE_NC);
573 case AMDGPU_VM_MTYPE_WC:
574 return AMDGPU_PTE_MTYPE_NV10(MTYPE_WC);
575 case AMDGPU_VM_MTYPE_CC:
576 return AMDGPU_PTE_MTYPE_NV10(MTYPE_CC);
577 case AMDGPU_VM_MTYPE_UC:
578 return AMDGPU_PTE_MTYPE_NV10(MTYPE_UC);
579 default:
580 return AMDGPU_PTE_MTYPE_NV10(MTYPE_NC);
581 }
582}
583
584static void gmc_v10_0_get_vm_pde(struct amdgpu_device *adev, int level,
585 uint64_t *addr, uint64_t *flags)
586{
587 if (!(*flags & AMDGPU_PDE_PTE) && !(*flags & AMDGPU_PTE_SYSTEM))
588 *addr = amdgpu_gmc_vram_mc2pa(adev, *addr);
589 BUG_ON(*addr & 0xFFFF00000000003FULL);
590
591 if (!adev->gmc.translate_further)
592 return;
593
594 if (level == AMDGPU_VM_PDB1) {
595 /* Set the block fragment size */
596 if (!(*flags & AMDGPU_PDE_PTE))
597 *flags |= AMDGPU_PDE_BFS(0x9);
598
599 } else if (level == AMDGPU_VM_PDB0) {
600 if (*flags & AMDGPU_PDE_PTE)
601 *flags &= ~AMDGPU_PDE_PTE;
602 else
603 *flags |= AMDGPU_PTE_TF;
604 }
605}
606
607static void gmc_v10_0_get_vm_pte(struct amdgpu_device *adev,
608 struct amdgpu_bo_va_mapping *mapping,
609 uint64_t *flags)
610{
611 struct amdgpu_bo *bo = mapping->bo_va->base.bo;
612
613 *flags &= ~AMDGPU_PTE_EXECUTABLE;
614 *flags |= mapping->flags & AMDGPU_PTE_EXECUTABLE;
615
616 *flags &= ~AMDGPU_PTE_MTYPE_NV10_MASK;
617 *flags |= (mapping->flags & AMDGPU_PTE_MTYPE_NV10_MASK);
618
619 *flags &= ~AMDGPU_PTE_NOALLOC;
620 *flags |= (mapping->flags & AMDGPU_PTE_NOALLOC);
621
622 if (mapping->flags & AMDGPU_PTE_PRT) {
623 *flags |= AMDGPU_PTE_PRT;
624 *flags |= AMDGPU_PTE_SNOOPED;
625 *flags |= AMDGPU_PTE_LOG;
626 *flags |= AMDGPU_PTE_SYSTEM;
627 *flags &= ~AMDGPU_PTE_VALID;
628 }
629
630 if (bo && bo->flags & (AMDGPU_GEM_CREATE_COHERENT |
631 AMDGPU_GEM_CREATE_UNCACHED))
632 *flags = (*flags & ~AMDGPU_PTE_MTYPE_NV10_MASK) |
633 AMDGPU_PTE_MTYPE_NV10(MTYPE_UC);
634}
635
636static unsigned gmc_v10_0_get_vbios_fb_size(struct amdgpu_device *adev)
637{
638 u32 d1vga_control = RREG32_SOC15(DCE, 0, mmD1VGA_CONTROL);
639 unsigned size;
640
641 if (REG_GET_FIELD(d1vga_control, D1VGA_CONTROL, D1VGA_MODE_ENABLE)) {
642 size = AMDGPU_VBIOS_VGA_ALLOCATION;
643 } else {
644 u32 viewport;
645 u32 pitch;
646
647 viewport = RREG32_SOC15(DCE, 0, mmHUBP0_DCSURF_PRI_VIEWPORT_DIMENSION);
648 pitch = RREG32_SOC15(DCE, 0, mmHUBPREQ0_DCSURF_SURFACE_PITCH);
649 size = (REG_GET_FIELD(viewport,
650 HUBP0_DCSURF_PRI_VIEWPORT_DIMENSION, PRI_VIEWPORT_HEIGHT) *
651 REG_GET_FIELD(pitch, HUBPREQ0_DCSURF_SURFACE_PITCH, PITCH) *
652 4);
653 }
654
655 return size;
656}
657
658static const struct amdgpu_gmc_funcs gmc_v10_0_gmc_funcs = {
659 .flush_gpu_tlb = gmc_v10_0_flush_gpu_tlb,
660 .flush_gpu_tlb_pasid = gmc_v10_0_flush_gpu_tlb_pasid,
661 .emit_flush_gpu_tlb = gmc_v10_0_emit_flush_gpu_tlb,
662 .emit_pasid_mapping = gmc_v10_0_emit_pasid_mapping,
663 .map_mtype = gmc_v10_0_map_mtype,
664 .get_vm_pde = gmc_v10_0_get_vm_pde,
665 .get_vm_pte = gmc_v10_0_get_vm_pte,
666 .get_vbios_fb_size = gmc_v10_0_get_vbios_fb_size,
667};
668
669static void gmc_v10_0_set_gmc_funcs(struct amdgpu_device *adev)
670{
671 if (adev->gmc.gmc_funcs == NULL)
672 adev->gmc.gmc_funcs = &gmc_v10_0_gmc_funcs;
673}
674
675static void gmc_v10_0_set_umc_funcs(struct amdgpu_device *adev)
676{
677 switch (adev->ip_versions[UMC_HWIP][0]) {
678 case IP_VERSION(8, 7, 0):
679 adev->umc.max_ras_err_cnt_per_query = UMC_V8_7_TOTAL_CHANNEL_NUM;
680 adev->umc.channel_inst_num = UMC_V8_7_CHANNEL_INSTANCE_NUM;
681 adev->umc.umc_inst_num = UMC_V8_7_UMC_INSTANCE_NUM;
682 adev->umc.channel_offs = UMC_V8_7_PER_CHANNEL_OFFSET_SIENNA;
683 adev->umc.channel_idx_tbl = &umc_v8_7_channel_idx_tbl[0][0];
684 adev->umc.ras = &umc_v8_7_ras;
685 break;
686 default:
687 break;
688 }
689 if (adev->umc.ras) {
690 amdgpu_ras_register_ras_block(adev, &adev->umc.ras->ras_block);
691
692 strcpy(adev->umc.ras->ras_block.ras_comm.name, "umc");
693 adev->umc.ras->ras_block.ras_comm.block = AMDGPU_RAS_BLOCK__UMC;
694 adev->umc.ras->ras_block.ras_comm.type = AMDGPU_RAS_ERROR__MULTI_UNCORRECTABLE;
695 adev->umc.ras_if = &adev->umc.ras->ras_block.ras_comm;
696
697 /* If don't define special ras_late_init function, use default ras_late_init */
698 if (!adev->umc.ras->ras_block.ras_late_init)
699 adev->umc.ras->ras_block.ras_late_init = amdgpu_umc_ras_late_init;
700
701 /* If not defined special ras_cb function, use default ras_cb */
702 if (!adev->umc.ras->ras_block.ras_cb)
703 adev->umc.ras->ras_block.ras_cb = amdgpu_umc_process_ras_data_cb;
704 }
705}
706
707
708static void gmc_v10_0_set_mmhub_funcs(struct amdgpu_device *adev)
709{
710 switch (adev->ip_versions[MMHUB_HWIP][0]) {
711 case IP_VERSION(2, 3, 0):
712 case IP_VERSION(2, 4, 0):
713 case IP_VERSION(2, 4, 1):
714 adev->mmhub.funcs = &mmhub_v2_3_funcs;
715 break;
716 default:
717 adev->mmhub.funcs = &mmhub_v2_0_funcs;
718 break;
719 }
720}
721
722static void gmc_v10_0_set_gfxhub_funcs(struct amdgpu_device *adev)
723{
724 switch (adev->ip_versions[GC_HWIP][0]) {
725 case IP_VERSION(10, 3, 0):
726 case IP_VERSION(10, 3, 2):
727 case IP_VERSION(10, 3, 1):
728 case IP_VERSION(10, 3, 4):
729 case IP_VERSION(10, 3, 5):
730 case IP_VERSION(10, 3, 6):
731 case IP_VERSION(10, 3, 3):
732 case IP_VERSION(10, 3, 7):
733 adev->gfxhub.funcs = &gfxhub_v2_1_funcs;
734 break;
735 default:
736 adev->gfxhub.funcs = &gfxhub_v2_0_funcs;
737 break;
738 }
739}
740
741
742static int gmc_v10_0_early_init(void *handle)
743{
744 int r;
745 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
746
747 gmc_v10_0_set_mmhub_funcs(adev);
748 gmc_v10_0_set_gfxhub_funcs(adev);
749 gmc_v10_0_set_gmc_funcs(adev);
750 gmc_v10_0_set_irq_funcs(adev);
751 gmc_v10_0_set_umc_funcs(adev);
752
753 adev->gmc.shared_aperture_start = 0x2000000000000000ULL;
754 adev->gmc.shared_aperture_end =
755 adev->gmc.shared_aperture_start + (4ULL << 30) - 1;
756 adev->gmc.private_aperture_start = 0x1000000000000000ULL;
757 adev->gmc.private_aperture_end =
758 adev->gmc.private_aperture_start + (4ULL << 30) - 1;
759
760 r = amdgpu_gmc_ras_early_init(adev);
761 if (r)
762 return r;
763
764 return 0;
765}
766
767static int gmc_v10_0_late_init(void *handle)
768{
769 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
770 int r;
771
772 r = amdgpu_gmc_allocate_vm_inv_eng(adev);
773 if (r)
774 return r;
775
776 r = amdgpu_gmc_ras_late_init(adev);
777 if (r)
778 return r;
779
780 return amdgpu_irq_get(adev, &adev->gmc.vm_fault, 0);
781}
782
783static void gmc_v10_0_vram_gtt_location(struct amdgpu_device *adev,
784 struct amdgpu_gmc *mc)
785{
786 u64 base = 0;
787
788 base = adev->gfxhub.funcs->get_fb_location(adev);
789
790 /* add the xgmi offset of the physical node */
791 base += adev->gmc.xgmi.physical_node_id * adev->gmc.xgmi.node_segment_size;
792
793 amdgpu_gmc_vram_location(adev, &adev->gmc, base);
794 amdgpu_gmc_gart_location(adev, mc);
795 amdgpu_gmc_agp_location(adev, mc);
796
797 /* base offset of vram pages */
798 adev->vm_manager.vram_base_offset = adev->gfxhub.funcs->get_mc_fb_offset(adev);
799
800 /* add the xgmi offset of the physical node */
801 adev->vm_manager.vram_base_offset +=
802 adev->gmc.xgmi.physical_node_id * adev->gmc.xgmi.node_segment_size;
803}
804
805/**
806 * gmc_v10_0_mc_init - initialize the memory controller driver params
807 *
808 * @adev: amdgpu_device pointer
809 *
810 * Look up the amount of vram, vram width, and decide how to place
811 * vram and gart within the GPU's physical address space.
812 * Returns 0 for success.
813 */
814static int gmc_v10_0_mc_init(struct amdgpu_device *adev)
815{
816 int r;
817
818 /* size in MB on si */
819 adev->gmc.mc_vram_size =
820 adev->nbio.funcs->get_memsize(adev) * 1024ULL * 1024ULL;
821 adev->gmc.real_vram_size = adev->gmc.mc_vram_size;
822
823 if (!(adev->flags & AMD_IS_APU)) {
824 r = amdgpu_device_resize_fb_bar(adev);
825 if (r)
826 return r;
827 }
828 adev->gmc.aper_base = pci_resource_start(adev->pdev, 0);
829 adev->gmc.aper_size = pci_resource_len(adev->pdev, 0);
830
831#ifdef CONFIG_X86_64
832 if ((adev->flags & AMD_IS_APU) && !amdgpu_passthrough(adev)) {
833 adev->gmc.aper_base = adev->gfxhub.funcs->get_mc_fb_offset(adev);
834 adev->gmc.aper_size = adev->gmc.real_vram_size;
835 }
836#endif
837
838 /* In case the PCI BAR is larger than the actual amount of vram */
839 adev->gmc.visible_vram_size = adev->gmc.aper_size;
840 if (adev->gmc.visible_vram_size > adev->gmc.real_vram_size)
841 adev->gmc.visible_vram_size = adev->gmc.real_vram_size;
842
843 /* set the gart size */
844 if (amdgpu_gart_size == -1) {
845 switch (adev->ip_versions[GC_HWIP][0]) {
846 default:
847 adev->gmc.gart_size = 512ULL << 20;
848 break;
849 case IP_VERSION(10, 3, 1): /* DCE SG support */
850 case IP_VERSION(10, 3, 3): /* DCE SG support */
851 case IP_VERSION(10, 3, 6): /* DCE SG support */
852 case IP_VERSION(10, 3, 7): /* DCE SG support */
853 adev->gmc.gart_size = 1024ULL << 20;
854 break;
855 }
856 } else {
857 adev->gmc.gart_size = (u64)amdgpu_gart_size << 20;
858 }
859
860 gmc_v10_0_vram_gtt_location(adev, &adev->gmc);
861
862 return 0;
863}
864
865static int gmc_v10_0_gart_init(struct amdgpu_device *adev)
866{
867 int r;
868
869 if (adev->gart.bo) {
870 WARN(1, "NAVI10 PCIE GART already initialized\n");
871 return 0;
872 }
873
874 /* Initialize common gart structure */
875 r = amdgpu_gart_init(adev);
876 if (r)
877 return r;
878
879 adev->gart.table_size = adev->gart.num_gpu_pages * 8;
880 adev->gart.gart_pte_flags = AMDGPU_PTE_MTYPE_NV10(MTYPE_UC) |
881 AMDGPU_PTE_EXECUTABLE;
882
883 return amdgpu_gart_table_vram_alloc(adev);
884}
885
886static int gmc_v10_0_sw_init(void *handle)
887{
888 int r, vram_width = 0, vram_type = 0, vram_vendor = 0;
889 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
890
891 adev->gfxhub.funcs->init(adev);
892
893 adev->mmhub.funcs->init(adev);
894
895 spin_lock_init(&adev->gmc.invalidate_lock);
896
897 if ((adev->flags & AMD_IS_APU) && amdgpu_emu_mode == 1) {
898 adev->gmc.vram_type = AMDGPU_VRAM_TYPE_DDR4;
899 adev->gmc.vram_width = 64;
900 } else if (amdgpu_emu_mode == 1) {
901 adev->gmc.vram_type = AMDGPU_VRAM_TYPE_GDDR6;
902 adev->gmc.vram_width = 1 * 128; /* numchan * chansize */
903 } else {
904 r = amdgpu_atomfirmware_get_vram_info(adev,
905 &vram_width, &vram_type, &vram_vendor);
906 adev->gmc.vram_width = vram_width;
907
908 adev->gmc.vram_type = vram_type;
909 adev->gmc.vram_vendor = vram_vendor;
910 }
911
912 switch (adev->ip_versions[GC_HWIP][0]) {
913 case IP_VERSION(10, 3, 0):
914 adev->gmc.mall_size = 128 * 1024 * 1024;
915 break;
916 case IP_VERSION(10, 3, 2):
917 adev->gmc.mall_size = 96 * 1024 * 1024;
918 break;
919 case IP_VERSION(10, 3, 4):
920 adev->gmc.mall_size = 32 * 1024 * 1024;
921 break;
922 case IP_VERSION(10, 3, 5):
923 adev->gmc.mall_size = 16 * 1024 * 1024;
924 break;
925 default:
926 adev->gmc.mall_size = 0;
927 break;
928 }
929
930 switch (adev->ip_versions[GC_HWIP][0]) {
931 case IP_VERSION(10, 1, 10):
932 case IP_VERSION(10, 1, 1):
933 case IP_VERSION(10, 1, 2):
934 case IP_VERSION(10, 1, 3):
935 case IP_VERSION(10, 1, 4):
936 case IP_VERSION(10, 3, 0):
937 case IP_VERSION(10, 3, 2):
938 case IP_VERSION(10, 3, 1):
939 case IP_VERSION(10, 3, 4):
940 case IP_VERSION(10, 3, 5):
941 case IP_VERSION(10, 3, 6):
942 case IP_VERSION(10, 3, 3):
943 case IP_VERSION(10, 3, 7):
944 adev->num_vmhubs = 2;
945 /*
946 * To fulfill 4-level page support,
947 * vm size is 256TB (48bit), maximum size of Navi10/Navi14/Navi12,
948 * block size 512 (9bit)
949 */
950 amdgpu_vm_adjust_size(adev, 256 * 1024, 9, 3, 48);
951 break;
952 default:
953 break;
954 }
955
956 /* This interrupt is VMC page fault.*/
957 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_VMC,
958 VMC_1_0__SRCID__VM_FAULT,
959 &adev->gmc.vm_fault);
960
961 if (r)
962 return r;
963
964 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_UTCL2,
965 UTCL2_1_0__SRCID__FAULT,
966 &adev->gmc.vm_fault);
967 if (r)
968 return r;
969
970 if (!amdgpu_sriov_vf(adev)) {
971 /* interrupt sent to DF. */
972 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DF, 0,
973 &adev->gmc.ecc_irq);
974 if (r)
975 return r;
976 }
977
978 /*
979 * Set the internal MC address mask This is the max address of the GPU's
980 * internal address space.
981 */
982 adev->gmc.mc_mask = 0xffffffffffffULL; /* 48 bit MC */
983
984 r = dma_set_mask_and_coherent(adev->dev, DMA_BIT_MASK(44));
985 if (r) {
986 printk(KERN_WARNING "amdgpu: No suitable DMA available.\n");
987 return r;
988 }
989
990 adev->need_swiotlb = drm_need_swiotlb(44);
991
992 r = gmc_v10_0_mc_init(adev);
993 if (r)
994 return r;
995
996 amdgpu_gmc_get_vbios_allocations(adev);
997
998 /* Memory manager */
999 r = amdgpu_bo_init(adev);
1000 if (r)
1001 return r;
1002
1003 r = gmc_v10_0_gart_init(adev);
1004 if (r)
1005 return r;
1006
1007 /*
1008 * number of VMs
1009 * VMID 0 is reserved for System
1010 * amdgpu graphics/compute will use VMIDs 1-7
1011 * amdkfd will use VMIDs 8-15
1012 */
1013 adev->vm_manager.first_kfd_vmid = 8;
1014
1015 amdgpu_vm_manager_init(adev);
1016
1017 return 0;
1018}
1019
1020/**
1021 * gmc_v10_0_gart_fini - vm fini callback
1022 *
1023 * @adev: amdgpu_device pointer
1024 *
1025 * Tears down the driver GART/VM setup (CIK).
1026 */
1027static void gmc_v10_0_gart_fini(struct amdgpu_device *adev)
1028{
1029 amdgpu_gart_table_vram_free(adev);
1030}
1031
1032static int gmc_v10_0_sw_fini(void *handle)
1033{
1034 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1035
1036 amdgpu_vm_manager_fini(adev);
1037 gmc_v10_0_gart_fini(adev);
1038 amdgpu_gem_force_release(adev);
1039 amdgpu_bo_fini(adev);
1040
1041 return 0;
1042}
1043
1044static void gmc_v10_0_init_golden_registers(struct amdgpu_device *adev)
1045{
1046}
1047
1048/**
1049 * gmc_v10_0_gart_enable - gart enable
1050 *
1051 * @adev: amdgpu_device pointer
1052 */
1053static int gmc_v10_0_gart_enable(struct amdgpu_device *adev)
1054{
1055 int r;
1056 bool value;
1057
1058 if (adev->gart.bo == NULL) {
1059 dev_err(adev->dev, "No VRAM object for PCIE GART.\n");
1060 return -EINVAL;
1061 }
1062
1063 amdgpu_gtt_mgr_recover(&adev->mman.gtt_mgr);
1064 r = adev->gfxhub.funcs->gart_enable(adev);
1065 if (r)
1066 return r;
1067
1068 r = adev->mmhub.funcs->gart_enable(adev);
1069 if (r)
1070 return r;
1071
1072 adev->hdp.funcs->init_registers(adev);
1073
1074 /* Flush HDP after it is initialized */
1075 adev->hdp.funcs->flush_hdp(adev, NULL);
1076
1077 value = (amdgpu_vm_fault_stop == AMDGPU_VM_FAULT_STOP_ALWAYS) ?
1078 false : true;
1079
1080 adev->gfxhub.funcs->set_fault_enable_default(adev, value);
1081 adev->mmhub.funcs->set_fault_enable_default(adev, value);
1082 gmc_v10_0_flush_gpu_tlb(adev, 0, AMDGPU_MMHUB_0, 0);
1083 gmc_v10_0_flush_gpu_tlb(adev, 0, AMDGPU_GFXHUB_0, 0);
1084
1085 DRM_INFO("PCIE GART of %uM enabled (table at 0x%016llX).\n",
1086 (unsigned)(adev->gmc.gart_size >> 20),
1087 (unsigned long long)amdgpu_bo_gpu_offset(adev->gart.bo));
1088
1089 return 0;
1090}
1091
1092static int gmc_v10_0_hw_init(void *handle)
1093{
1094 int r;
1095 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1096
1097 /* The sequence of these two function calls matters.*/
1098 gmc_v10_0_init_golden_registers(adev);
1099
1100 /*
1101 * harvestable groups in gc_utcl2 need to be programmed before any GFX block
1102 * register setup within GMC, or else system hang when harvesting SA.
1103 */
1104 if (adev->gfxhub.funcs && adev->gfxhub.funcs->utcl2_harvest)
1105 adev->gfxhub.funcs->utcl2_harvest(adev);
1106
1107 r = gmc_v10_0_gart_enable(adev);
1108 if (r)
1109 return r;
1110
1111 if (amdgpu_emu_mode == 1) {
1112 r = amdgpu_gmc_vram_checking(adev);
1113 if (r)
1114 return r;
1115 }
1116
1117 if (adev->umc.funcs && adev->umc.funcs->init_registers)
1118 adev->umc.funcs->init_registers(adev);
1119
1120 return 0;
1121}
1122
1123/**
1124 * gmc_v10_0_gart_disable - gart disable
1125 *
1126 * @adev: amdgpu_device pointer
1127 *
1128 * This disables all VM page table.
1129 */
1130static void gmc_v10_0_gart_disable(struct amdgpu_device *adev)
1131{
1132 adev->gfxhub.funcs->gart_disable(adev);
1133 adev->mmhub.funcs->gart_disable(adev);
1134}
1135
1136static int gmc_v10_0_hw_fini(void *handle)
1137{
1138 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1139
1140 gmc_v10_0_gart_disable(adev);
1141
1142 if (amdgpu_sriov_vf(adev)) {
1143 /* full access mode, so don't touch any GMC register */
1144 DRM_DEBUG("For SRIOV client, shouldn't do anything.\n");
1145 return 0;
1146 }
1147
1148 amdgpu_irq_put(adev, &adev->gmc.ecc_irq, 0);
1149 amdgpu_irq_put(adev, &adev->gmc.vm_fault, 0);
1150
1151 return 0;
1152}
1153
1154static int gmc_v10_0_suspend(void *handle)
1155{
1156 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1157
1158 gmc_v10_0_hw_fini(adev);
1159
1160 return 0;
1161}
1162
1163static int gmc_v10_0_resume(void *handle)
1164{
1165 int r;
1166 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1167
1168 r = gmc_v10_0_hw_init(adev);
1169 if (r)
1170 return r;
1171
1172 amdgpu_vmid_reset_all(adev);
1173
1174 return 0;
1175}
1176
1177static bool gmc_v10_0_is_idle(void *handle)
1178{
1179 /* MC is always ready in GMC v10.*/
1180 return true;
1181}
1182
1183static int gmc_v10_0_wait_for_idle(void *handle)
1184{
1185 /* There is no need to wait for MC idle in GMC v10.*/
1186 return 0;
1187}
1188
1189static int gmc_v10_0_soft_reset(void *handle)
1190{
1191 return 0;
1192}
1193
1194static int gmc_v10_0_set_clockgating_state(void *handle,
1195 enum amd_clockgating_state state)
1196{
1197 int r;
1198 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1199
1200 /*
1201 * The issue mmhub can't disconnect from DF with MMHUB clock gating being disabled
1202 * is a new problem observed at DF 3.0.3, however with the same suspend sequence not
1203 * seen any issue on the DF 3.0.2 series platform.
1204 */
1205 if (adev->in_s0ix && adev->ip_versions[DF_HWIP][0] > IP_VERSION(3, 0, 2)) {
1206 dev_dbg(adev->dev, "keep mmhub clock gating being enabled for s0ix\n");
1207 return 0;
1208 }
1209
1210 r = adev->mmhub.funcs->set_clockgating(adev, state);
1211 if (r)
1212 return r;
1213
1214 if (adev->ip_versions[ATHUB_HWIP][0] >= IP_VERSION(2, 1, 0))
1215 return athub_v2_1_set_clockgating(adev, state);
1216 else
1217 return athub_v2_0_set_clockgating(adev, state);
1218}
1219
1220static void gmc_v10_0_get_clockgating_state(void *handle, u64 *flags)
1221{
1222 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1223
1224 if (adev->ip_versions[GC_HWIP][0] == IP_VERSION(10, 1, 3) ||
1225 adev->ip_versions[GC_HWIP][0] == IP_VERSION(10, 1, 4))
1226 return;
1227
1228 adev->mmhub.funcs->get_clockgating(adev, flags);
1229
1230 if (adev->ip_versions[ATHUB_HWIP][0] >= IP_VERSION(2, 1, 0))
1231 athub_v2_1_get_clockgating(adev, flags);
1232 else
1233 athub_v2_0_get_clockgating(adev, flags);
1234}
1235
1236static int gmc_v10_0_set_powergating_state(void *handle,
1237 enum amd_powergating_state state)
1238{
1239 return 0;
1240}
1241
1242const struct amd_ip_funcs gmc_v10_0_ip_funcs = {
1243 .name = "gmc_v10_0",
1244 .early_init = gmc_v10_0_early_init,
1245 .late_init = gmc_v10_0_late_init,
1246 .sw_init = gmc_v10_0_sw_init,
1247 .sw_fini = gmc_v10_0_sw_fini,
1248 .hw_init = gmc_v10_0_hw_init,
1249 .hw_fini = gmc_v10_0_hw_fini,
1250 .suspend = gmc_v10_0_suspend,
1251 .resume = gmc_v10_0_resume,
1252 .is_idle = gmc_v10_0_is_idle,
1253 .wait_for_idle = gmc_v10_0_wait_for_idle,
1254 .soft_reset = gmc_v10_0_soft_reset,
1255 .set_clockgating_state = gmc_v10_0_set_clockgating_state,
1256 .set_powergating_state = gmc_v10_0_set_powergating_state,
1257 .get_clockgating_state = gmc_v10_0_get_clockgating_state,
1258};
1259
1260const struct amdgpu_ip_block_version gmc_v10_0_ip_block =
1261{
1262 .type = AMD_IP_BLOCK_TYPE_GMC,
1263 .major = 10,
1264 .minor = 0,
1265 .rev = 0,
1266 .funcs = &gmc_v10_0_ip_funcs,
1267};
1/*
2 * Copyright 2019 Advanced Micro Devices, Inc.
3 *
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:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
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.
21 *
22 */
23#include <linux/firmware.h>
24#include <linux/pci.h>
25
26#include <drm/drm_cache.h>
27
28#include "amdgpu.h"
29#include "amdgpu_atomfirmware.h"
30#include "gmc_v10_0.h"
31#include "umc_v8_7.h"
32
33#include "athub/athub_2_0_0_sh_mask.h"
34#include "athub/athub_2_0_0_offset.h"
35#include "dcn/dcn_2_0_0_offset.h"
36#include "dcn/dcn_2_0_0_sh_mask.h"
37#include "oss/osssys_5_0_0_offset.h"
38#include "ivsrcid/vmc/irqsrcs_vmc_1_0.h"
39#include "navi10_enum.h"
40
41#include "soc15.h"
42#include "soc15d.h"
43#include "soc15_common.h"
44
45#include "nbio_v2_3.h"
46
47#include "gfxhub_v2_0.h"
48#include "gfxhub_v2_1.h"
49#include "mmhub_v2_0.h"
50#include "mmhub_v2_3.h"
51#include "athub_v2_0.h"
52#include "athub_v2_1.h"
53
54static int gmc_v10_0_ecc_interrupt_state(struct amdgpu_device *adev,
55 struct amdgpu_irq_src *src,
56 unsigned int type,
57 enum amdgpu_interrupt_state state)
58{
59 return 0;
60}
61
62static int
63gmc_v10_0_vm_fault_interrupt_state(struct amdgpu_device *adev,
64 struct amdgpu_irq_src *src, unsigned int type,
65 enum amdgpu_interrupt_state state)
66{
67 switch (state) {
68 case AMDGPU_IRQ_STATE_DISABLE:
69 /* MM HUB */
70 amdgpu_gmc_set_vm_fault_masks(adev, AMDGPU_MMHUB0(0), false);
71 /* GFX HUB */
72 /* This works because this interrupt is only
73 * enabled at init/resume and disabled in
74 * fini/suspend, so the overall state doesn't
75 * change over the course of suspend/resume.
76 */
77 if (!adev->in_s0ix)
78 amdgpu_gmc_set_vm_fault_masks(adev, AMDGPU_GFXHUB(0), false);
79 break;
80 case AMDGPU_IRQ_STATE_ENABLE:
81 /* MM HUB */
82 amdgpu_gmc_set_vm_fault_masks(adev, AMDGPU_MMHUB0(0), true);
83 /* GFX HUB */
84 /* This works because this interrupt is only
85 * enabled at init/resume and disabled in
86 * fini/suspend, so the overall state doesn't
87 * change over the course of suspend/resume.
88 */
89 if (!adev->in_s0ix)
90 amdgpu_gmc_set_vm_fault_masks(adev, AMDGPU_GFXHUB(0), true);
91 break;
92 default:
93 break;
94 }
95
96 return 0;
97}
98
99static int gmc_v10_0_process_interrupt(struct amdgpu_device *adev,
100 struct amdgpu_irq_src *source,
101 struct amdgpu_iv_entry *entry)
102{
103 uint32_t vmhub_index = entry->client_id == SOC15_IH_CLIENTID_VMC ?
104 AMDGPU_MMHUB0(0) : AMDGPU_GFXHUB(0);
105 struct amdgpu_vmhub *hub = &adev->vmhub[vmhub_index];
106 bool retry_fault = !!(entry->src_data[1] & 0x80);
107 bool write_fault = !!(entry->src_data[1] & 0x20);
108 struct amdgpu_task_info *task_info;
109 uint32_t status = 0;
110 u64 addr;
111
112 addr = (u64)entry->src_data[0] << 12;
113 addr |= ((u64)entry->src_data[1] & 0xf) << 44;
114
115 if (retry_fault) {
116 /* Returning 1 here also prevents sending the IV to the KFD */
117
118 /* Process it onyl if it's the first fault for this address */
119 if (entry->ih != &adev->irq.ih_soft &&
120 amdgpu_gmc_filter_faults(adev, entry->ih, addr, entry->pasid,
121 entry->timestamp))
122 return 1;
123
124 /* Delegate it to a different ring if the hardware hasn't
125 * already done it.
126 */
127 if (entry->ih == &adev->irq.ih) {
128 amdgpu_irq_delegate(adev, entry, 8);
129 return 1;
130 }
131
132 /* Try to handle the recoverable page faults by filling page
133 * tables
134 */
135 if (amdgpu_vm_handle_fault(adev, entry->pasid, 0, 0, addr,
136 entry->timestamp, write_fault))
137 return 1;
138 }
139
140 if (!amdgpu_sriov_vf(adev)) {
141 /*
142 * Issue a dummy read to wait for the status register to
143 * be updated to avoid reading an incorrect value due to
144 * the new fast GRBM interface.
145 */
146 if ((entry->vmid_src == AMDGPU_GFXHUB(0)) &&
147 (amdgpu_ip_version(adev, GC_HWIP, 0) <
148 IP_VERSION(10, 3, 0)))
149 RREG32(hub->vm_l2_pro_fault_status);
150
151 status = RREG32(hub->vm_l2_pro_fault_status);
152 WREG32_P(hub->vm_l2_pro_fault_cntl, 1, ~1);
153
154 amdgpu_vm_update_fault_cache(adev, entry->pasid, addr, status,
155 entry->vmid_src ? AMDGPU_MMHUB0(0) : AMDGPU_GFXHUB(0));
156 }
157
158 if (!printk_ratelimit())
159 return 0;
160
161 dev_err(adev->dev,
162 "[%s] page fault (src_id:%u ring:%u vmid:%u pasid:%u)\n",
163 entry->vmid_src ? "mmhub" : "gfxhub",
164 entry->src_id, entry->ring_id, entry->vmid, entry->pasid);
165 task_info = amdgpu_vm_get_task_info_pasid(adev, entry->pasid);
166 if (task_info) {
167 dev_err(adev->dev,
168 " in process %s pid %d thread %s pid %d\n",
169 task_info->process_name, task_info->tgid,
170 task_info->task_name, task_info->pid);
171 amdgpu_vm_put_task_info(task_info);
172 }
173
174 dev_err(adev->dev, " in page starting at address 0x%016llx from client 0x%x (%s)\n",
175 addr, entry->client_id,
176 soc15_ih_clientid_name[entry->client_id]);
177
178 /* Only print L2 fault status if the status register could be read and
179 * contains useful information
180 */
181 if (status != 0)
182 hub->vmhub_funcs->print_l2_protection_fault_status(adev,
183 status);
184
185 return 0;
186}
187
188static const struct amdgpu_irq_src_funcs gmc_v10_0_irq_funcs = {
189 .set = gmc_v10_0_vm_fault_interrupt_state,
190 .process = gmc_v10_0_process_interrupt,
191};
192
193static const struct amdgpu_irq_src_funcs gmc_v10_0_ecc_funcs = {
194 .set = gmc_v10_0_ecc_interrupt_state,
195 .process = amdgpu_umc_process_ecc_irq,
196};
197
198static void gmc_v10_0_set_irq_funcs(struct amdgpu_device *adev)
199{
200 adev->gmc.vm_fault.num_types = 1;
201 adev->gmc.vm_fault.funcs = &gmc_v10_0_irq_funcs;
202
203 if (!amdgpu_sriov_vf(adev)) {
204 adev->gmc.ecc_irq.num_types = 1;
205 adev->gmc.ecc_irq.funcs = &gmc_v10_0_ecc_funcs;
206 }
207}
208
209/**
210 * gmc_v10_0_use_invalidate_semaphore - judge whether to use semaphore
211 *
212 * @adev: amdgpu_device pointer
213 * @vmhub: vmhub type
214 *
215 */
216static bool gmc_v10_0_use_invalidate_semaphore(struct amdgpu_device *adev,
217 uint32_t vmhub)
218{
219 return ((vmhub == AMDGPU_MMHUB0(0)) &&
220 (!amdgpu_sriov_vf(adev)));
221}
222
223static bool gmc_v10_0_get_atc_vmid_pasid_mapping_info(
224 struct amdgpu_device *adev,
225 uint8_t vmid, uint16_t *p_pasid)
226{
227 uint32_t value;
228
229 value = RREG32(SOC15_REG_OFFSET(ATHUB, 0, mmATC_VMID0_PASID_MAPPING)
230 + vmid);
231 *p_pasid = value & ATC_VMID0_PASID_MAPPING__PASID_MASK;
232
233 return !!(value & ATC_VMID0_PASID_MAPPING__VALID_MASK);
234}
235
236/*
237 * GART
238 * VMID 0 is the physical GPU addresses as used by the kernel.
239 * VMIDs 1-15 are used for userspace clients and are handled
240 * by the amdgpu vm/hsa code.
241 */
242
243/**
244 * gmc_v10_0_flush_gpu_tlb - gart tlb flush callback
245 *
246 * @adev: amdgpu_device pointer
247 * @vmid: vm instance to flush
248 * @vmhub: vmhub type
249 * @flush_type: the flush type
250 *
251 * Flush the TLB for the requested page table.
252 */
253static void gmc_v10_0_flush_gpu_tlb(struct amdgpu_device *adev, uint32_t vmid,
254 uint32_t vmhub, uint32_t flush_type)
255{
256 bool use_semaphore = gmc_v10_0_use_invalidate_semaphore(adev, vmhub);
257 struct amdgpu_vmhub *hub = &adev->vmhub[vmhub];
258 u32 inv_req = hub->vmhub_funcs->get_invalidate_req(vmid, flush_type);
259 /* Use register 17 for GART */
260 const unsigned int eng = 17;
261 unsigned char hub_ip = 0;
262 u32 sem, req, ack;
263 unsigned int i;
264 u32 tmp;
265
266 sem = hub->vm_inv_eng0_sem + hub->eng_distance * eng;
267 req = hub->vm_inv_eng0_req + hub->eng_distance * eng;
268 ack = hub->vm_inv_eng0_ack + hub->eng_distance * eng;
269
270 /* flush hdp cache */
271 adev->hdp.funcs->flush_hdp(adev, NULL);
272
273 /* This is necessary for SRIOV as well as for GFXOFF to function
274 * properly under bare metal
275 */
276 if (adev->gfx.kiq[0].ring.sched.ready && !adev->enable_mes &&
277 (amdgpu_sriov_runtime(adev) || !amdgpu_sriov_vf(adev))) {
278 amdgpu_gmc_fw_reg_write_reg_wait(adev, req, ack, inv_req,
279 1 << vmid, GET_INST(GC, 0));
280 return;
281 }
282
283 /* This path is needed before KIQ/MES/GFXOFF are set up */
284 hub_ip = (vmhub == AMDGPU_GFXHUB(0)) ? GC_HWIP : MMHUB_HWIP;
285
286 spin_lock(&adev->gmc.invalidate_lock);
287 /*
288 * It may lose gpuvm invalidate acknowldege state across power-gating
289 * off cycle, add semaphore acquire before invalidation and semaphore
290 * release after invalidation to avoid entering power gated state
291 * to WA the Issue
292 */
293
294 /* TODO: It needs to continue working on debugging with semaphore for GFXHUB as well. */
295 if (use_semaphore) {
296 for (i = 0; i < adev->usec_timeout; i++) {
297 /* a read return value of 1 means semaphore acuqire */
298 tmp = RREG32_RLC_NO_KIQ(sem, hub_ip);
299 if (tmp & 0x1)
300 break;
301 udelay(1);
302 }
303
304 if (i >= adev->usec_timeout)
305 DRM_ERROR("Timeout waiting for sem acquire in VM flush!\n");
306 }
307
308 WREG32_RLC_NO_KIQ(req, inv_req, hub_ip);
309
310 /*
311 * Issue a dummy read to wait for the ACK register to be cleared
312 * to avoid a false ACK due to the new fast GRBM interface.
313 */
314 if ((vmhub == AMDGPU_GFXHUB(0)) &&
315 (amdgpu_ip_version(adev, GC_HWIP, 0) < IP_VERSION(10, 3, 0)))
316 RREG32_RLC_NO_KIQ(req, hub_ip);
317
318 /* Wait for ACK with a delay.*/
319 for (i = 0; i < adev->usec_timeout; i++) {
320 tmp = RREG32_RLC_NO_KIQ(ack, hub_ip);
321 tmp &= 1 << vmid;
322 if (tmp)
323 break;
324
325 udelay(1);
326 }
327
328 /* TODO: It needs to continue working on debugging with semaphore for GFXHUB as well. */
329 if (use_semaphore)
330 WREG32_RLC_NO_KIQ(sem, 0, hub_ip);
331
332 spin_unlock(&adev->gmc.invalidate_lock);
333
334 if (i >= adev->usec_timeout)
335 dev_err(adev->dev, "Timeout waiting for VM flush hub: %d!\n",
336 vmhub);
337}
338
339/**
340 * gmc_v10_0_flush_gpu_tlb_pasid - tlb flush via pasid
341 *
342 * @adev: amdgpu_device pointer
343 * @pasid: pasid to be flush
344 * @flush_type: the flush type
345 * @all_hub: Used with PACKET3_INVALIDATE_TLBS_ALL_HUB()
346 * @inst: is used to select which instance of KIQ to use for the invalidation
347 *
348 * Flush the TLB for the requested pasid.
349 */
350static void gmc_v10_0_flush_gpu_tlb_pasid(struct amdgpu_device *adev,
351 uint16_t pasid, uint32_t flush_type,
352 bool all_hub, uint32_t inst)
353{
354 uint16_t queried;
355 int vmid, i;
356
357 for (vmid = 1; vmid < AMDGPU_NUM_VMID; vmid++) {
358 bool valid;
359
360 valid = gmc_v10_0_get_atc_vmid_pasid_mapping_info(adev, vmid,
361 &queried);
362 if (!valid || queried != pasid)
363 continue;
364
365 if (all_hub) {
366 for_each_set_bit(i, adev->vmhubs_mask,
367 AMDGPU_MAX_VMHUBS)
368 gmc_v10_0_flush_gpu_tlb(adev, vmid, i,
369 flush_type);
370 } else {
371 gmc_v10_0_flush_gpu_tlb(adev, vmid, AMDGPU_GFXHUB(0),
372 flush_type);
373 }
374 }
375}
376
377static uint64_t gmc_v10_0_emit_flush_gpu_tlb(struct amdgpu_ring *ring,
378 unsigned int vmid, uint64_t pd_addr)
379{
380 bool use_semaphore = gmc_v10_0_use_invalidate_semaphore(ring->adev, ring->vm_hub);
381 struct amdgpu_vmhub *hub = &ring->adev->vmhub[ring->vm_hub];
382 uint32_t req = hub->vmhub_funcs->get_invalidate_req(vmid, 0);
383 unsigned int eng = ring->vm_inv_eng;
384
385 /*
386 * It may lose gpuvm invalidate acknowldege state across power-gating
387 * off cycle, add semaphore acquire before invalidation and semaphore
388 * release after invalidation to avoid entering power gated state
389 * to WA the Issue
390 */
391
392 /* TODO: It needs to continue working on debugging with semaphore for GFXHUB as well. */
393 if (use_semaphore)
394 /* a read return value of 1 means semaphore acuqire */
395 amdgpu_ring_emit_reg_wait(ring,
396 hub->vm_inv_eng0_sem +
397 hub->eng_distance * eng, 0x1, 0x1);
398
399 amdgpu_ring_emit_wreg(ring, hub->ctx0_ptb_addr_lo32 +
400 (hub->ctx_addr_distance * vmid),
401 lower_32_bits(pd_addr));
402
403 amdgpu_ring_emit_wreg(ring, hub->ctx0_ptb_addr_hi32 +
404 (hub->ctx_addr_distance * vmid),
405 upper_32_bits(pd_addr));
406
407 amdgpu_ring_emit_reg_write_reg_wait(ring, hub->vm_inv_eng0_req +
408 hub->eng_distance * eng,
409 hub->vm_inv_eng0_ack +
410 hub->eng_distance * eng,
411 req, 1 << vmid);
412
413 /* TODO: It needs to continue working on debugging with semaphore for GFXHUB as well. */
414 if (use_semaphore)
415 /*
416 * add semaphore release after invalidation,
417 * write with 0 means semaphore release
418 */
419 amdgpu_ring_emit_wreg(ring, hub->vm_inv_eng0_sem +
420 hub->eng_distance * eng, 0);
421
422 return pd_addr;
423}
424
425static void gmc_v10_0_emit_pasid_mapping(struct amdgpu_ring *ring, unsigned int vmid,
426 unsigned int pasid)
427{
428 struct amdgpu_device *adev = ring->adev;
429 uint32_t reg;
430
431 /* MES fw manages IH_VMID_x_LUT updating */
432 if (ring->is_mes_queue)
433 return;
434
435 if (ring->vm_hub == AMDGPU_GFXHUB(0))
436 reg = SOC15_REG_OFFSET(OSSSYS, 0, mmIH_VMID_0_LUT) + vmid;
437 else
438 reg = SOC15_REG_OFFSET(OSSSYS, 0, mmIH_VMID_0_LUT_MM) + vmid;
439
440 amdgpu_ring_emit_wreg(ring, reg, pasid);
441}
442
443/*
444 * PTE format on NAVI 10:
445 * 63:59 reserved
446 * 58 reserved and for sienna_cichlid is used for MALL noalloc
447 * 57 reserved
448 * 56 F
449 * 55 L
450 * 54 reserved
451 * 53:52 SW
452 * 51 T
453 * 50:48 mtype
454 * 47:12 4k physical page base address
455 * 11:7 fragment
456 * 6 write
457 * 5 read
458 * 4 exe
459 * 3 Z
460 * 2 snooped
461 * 1 system
462 * 0 valid
463 *
464 * PDE format on NAVI 10:
465 * 63:59 block fragment size
466 * 58:55 reserved
467 * 54 P
468 * 53:48 reserved
469 * 47:6 physical base address of PD or PTE
470 * 5:3 reserved
471 * 2 C
472 * 1 system
473 * 0 valid
474 */
475
476static uint64_t gmc_v10_0_map_mtype(struct amdgpu_device *adev, uint32_t flags)
477{
478 switch (flags) {
479 case AMDGPU_VM_MTYPE_DEFAULT:
480 return AMDGPU_PTE_MTYPE_NV10(0ULL, MTYPE_NC);
481 case AMDGPU_VM_MTYPE_NC:
482 return AMDGPU_PTE_MTYPE_NV10(0ULL, MTYPE_NC);
483 case AMDGPU_VM_MTYPE_WC:
484 return AMDGPU_PTE_MTYPE_NV10(0ULL, MTYPE_WC);
485 case AMDGPU_VM_MTYPE_CC:
486 return AMDGPU_PTE_MTYPE_NV10(0ULL, MTYPE_CC);
487 case AMDGPU_VM_MTYPE_UC:
488 return AMDGPU_PTE_MTYPE_NV10(0ULL, MTYPE_UC);
489 default:
490 return AMDGPU_PTE_MTYPE_NV10(0ULL, MTYPE_NC);
491 }
492}
493
494static void gmc_v10_0_get_vm_pde(struct amdgpu_device *adev, int level,
495 uint64_t *addr, uint64_t *flags)
496{
497 if (!(*flags & AMDGPU_PDE_PTE) && !(*flags & AMDGPU_PTE_SYSTEM))
498 *addr = amdgpu_gmc_vram_mc2pa(adev, *addr);
499 BUG_ON(*addr & 0xFFFF00000000003FULL);
500
501 if (!adev->gmc.translate_further)
502 return;
503
504 if (level == AMDGPU_VM_PDB1) {
505 /* Set the block fragment size */
506 if (!(*flags & AMDGPU_PDE_PTE))
507 *flags |= AMDGPU_PDE_BFS(0x9);
508
509 } else if (level == AMDGPU_VM_PDB0) {
510 if (*flags & AMDGPU_PDE_PTE)
511 *flags &= ~AMDGPU_PDE_PTE;
512 else
513 *flags |= AMDGPU_PTE_TF;
514 }
515}
516
517static void gmc_v10_0_get_vm_pte(struct amdgpu_device *adev,
518 struct amdgpu_bo_va_mapping *mapping,
519 uint64_t *flags)
520{
521 struct amdgpu_bo *bo = mapping->bo_va->base.bo;
522
523 *flags &= ~AMDGPU_PTE_EXECUTABLE;
524 *flags |= mapping->flags & AMDGPU_PTE_EXECUTABLE;
525
526 *flags &= ~AMDGPU_PTE_MTYPE_NV10_MASK;
527 *flags |= (mapping->flags & AMDGPU_PTE_MTYPE_NV10_MASK);
528
529 *flags &= ~AMDGPU_PTE_NOALLOC;
530 *flags |= (mapping->flags & AMDGPU_PTE_NOALLOC);
531
532 if (mapping->flags & AMDGPU_PTE_PRT) {
533 *flags |= AMDGPU_PTE_PRT;
534 *flags |= AMDGPU_PTE_SNOOPED;
535 *flags |= AMDGPU_PTE_LOG;
536 *flags |= AMDGPU_PTE_SYSTEM;
537 *flags &= ~AMDGPU_PTE_VALID;
538 }
539
540 if (bo && bo->flags & (AMDGPU_GEM_CREATE_COHERENT |
541 AMDGPU_GEM_CREATE_EXT_COHERENT |
542 AMDGPU_GEM_CREATE_UNCACHED))
543 *flags = AMDGPU_PTE_MTYPE_NV10(*flags, MTYPE_UC);
544}
545
546static unsigned int gmc_v10_0_get_vbios_fb_size(struct amdgpu_device *adev)
547{
548 u32 d1vga_control = RREG32_SOC15(DCE, 0, mmD1VGA_CONTROL);
549 unsigned int size;
550
551 if (REG_GET_FIELD(d1vga_control, D1VGA_CONTROL, D1VGA_MODE_ENABLE)) {
552 size = AMDGPU_VBIOS_VGA_ALLOCATION;
553 } else {
554 u32 viewport;
555 u32 pitch;
556
557 viewport = RREG32_SOC15(DCE, 0, mmHUBP0_DCSURF_PRI_VIEWPORT_DIMENSION);
558 pitch = RREG32_SOC15(DCE, 0, mmHUBPREQ0_DCSURF_SURFACE_PITCH);
559 size = (REG_GET_FIELD(viewport,
560 HUBP0_DCSURF_PRI_VIEWPORT_DIMENSION, PRI_VIEWPORT_HEIGHT) *
561 REG_GET_FIELD(pitch, HUBPREQ0_DCSURF_SURFACE_PITCH, PITCH) *
562 4);
563 }
564
565 return size;
566}
567
568static const struct amdgpu_gmc_funcs gmc_v10_0_gmc_funcs = {
569 .flush_gpu_tlb = gmc_v10_0_flush_gpu_tlb,
570 .flush_gpu_tlb_pasid = gmc_v10_0_flush_gpu_tlb_pasid,
571 .emit_flush_gpu_tlb = gmc_v10_0_emit_flush_gpu_tlb,
572 .emit_pasid_mapping = gmc_v10_0_emit_pasid_mapping,
573 .map_mtype = gmc_v10_0_map_mtype,
574 .get_vm_pde = gmc_v10_0_get_vm_pde,
575 .get_vm_pte = gmc_v10_0_get_vm_pte,
576 .get_vbios_fb_size = gmc_v10_0_get_vbios_fb_size,
577};
578
579static void gmc_v10_0_set_gmc_funcs(struct amdgpu_device *adev)
580{
581 if (adev->gmc.gmc_funcs == NULL)
582 adev->gmc.gmc_funcs = &gmc_v10_0_gmc_funcs;
583}
584
585static void gmc_v10_0_set_umc_funcs(struct amdgpu_device *adev)
586{
587 switch (amdgpu_ip_version(adev, UMC_HWIP, 0)) {
588 case IP_VERSION(8, 7, 0):
589 adev->umc.max_ras_err_cnt_per_query = UMC_V8_7_TOTAL_CHANNEL_NUM;
590 adev->umc.channel_inst_num = UMC_V8_7_CHANNEL_INSTANCE_NUM;
591 adev->umc.umc_inst_num = UMC_V8_7_UMC_INSTANCE_NUM;
592 adev->umc.channel_offs = UMC_V8_7_PER_CHANNEL_OFFSET_SIENNA;
593 adev->umc.retire_unit = 1;
594 adev->umc.channel_idx_tbl = &umc_v8_7_channel_idx_tbl[0][0];
595 adev->umc.ras = &umc_v8_7_ras;
596 break;
597 default:
598 break;
599 }
600}
601
602static void gmc_v10_0_set_mmhub_funcs(struct amdgpu_device *adev)
603{
604 switch (amdgpu_ip_version(adev, MMHUB_HWIP, 0)) {
605 case IP_VERSION(2, 3, 0):
606 case IP_VERSION(2, 4, 0):
607 case IP_VERSION(2, 4, 1):
608 adev->mmhub.funcs = &mmhub_v2_3_funcs;
609 break;
610 default:
611 adev->mmhub.funcs = &mmhub_v2_0_funcs;
612 break;
613 }
614}
615
616static void gmc_v10_0_set_gfxhub_funcs(struct amdgpu_device *adev)
617{
618 switch (amdgpu_ip_version(adev, GC_HWIP, 0)) {
619 case IP_VERSION(10, 3, 0):
620 case IP_VERSION(10, 3, 2):
621 case IP_VERSION(10, 3, 1):
622 case IP_VERSION(10, 3, 4):
623 case IP_VERSION(10, 3, 5):
624 case IP_VERSION(10, 3, 6):
625 case IP_VERSION(10, 3, 3):
626 case IP_VERSION(10, 3, 7):
627 adev->gfxhub.funcs = &gfxhub_v2_1_funcs;
628 break;
629 default:
630 adev->gfxhub.funcs = &gfxhub_v2_0_funcs;
631 break;
632 }
633}
634
635
636static int gmc_v10_0_early_init(struct amdgpu_ip_block *ip_block)
637{
638 struct amdgpu_device *adev = ip_block->adev;
639
640 gmc_v10_0_set_mmhub_funcs(adev);
641 gmc_v10_0_set_gfxhub_funcs(adev);
642 gmc_v10_0_set_gmc_funcs(adev);
643 gmc_v10_0_set_irq_funcs(adev);
644 gmc_v10_0_set_umc_funcs(adev);
645
646 adev->gmc.shared_aperture_start = 0x2000000000000000ULL;
647 adev->gmc.shared_aperture_end =
648 adev->gmc.shared_aperture_start + (4ULL << 30) - 1;
649 adev->gmc.private_aperture_start = 0x1000000000000000ULL;
650 adev->gmc.private_aperture_end =
651 adev->gmc.private_aperture_start + (4ULL << 30) - 1;
652 adev->gmc.noretry_flags = AMDGPU_VM_NORETRY_FLAGS_TF;
653
654 return 0;
655}
656
657static int gmc_v10_0_late_init(struct amdgpu_ip_block *ip_block)
658{
659 struct amdgpu_device *adev = ip_block->adev;
660 int r;
661
662 r = amdgpu_gmc_allocate_vm_inv_eng(adev);
663 if (r)
664 return r;
665
666 r = amdgpu_gmc_ras_late_init(adev);
667 if (r)
668 return r;
669
670 return amdgpu_irq_get(adev, &adev->gmc.vm_fault, 0);
671}
672
673static void gmc_v10_0_vram_gtt_location(struct amdgpu_device *adev,
674 struct amdgpu_gmc *mc)
675{
676 u64 base = 0;
677
678 base = adev->gfxhub.funcs->get_fb_location(adev);
679
680 /* add the xgmi offset of the physical node */
681 base += adev->gmc.xgmi.physical_node_id * adev->gmc.xgmi.node_segment_size;
682
683 amdgpu_gmc_set_agp_default(adev, mc);
684 amdgpu_gmc_vram_location(adev, &adev->gmc, base);
685 amdgpu_gmc_gart_location(adev, mc, AMDGPU_GART_PLACEMENT_BEST_FIT);
686 if (!amdgpu_sriov_vf(adev) && (amdgpu_agp == 1))
687 amdgpu_gmc_agp_location(adev, mc);
688
689 /* base offset of vram pages */
690 adev->vm_manager.vram_base_offset = adev->gfxhub.funcs->get_mc_fb_offset(adev);
691
692 /* add the xgmi offset of the physical node */
693 adev->vm_manager.vram_base_offset +=
694 adev->gmc.xgmi.physical_node_id * adev->gmc.xgmi.node_segment_size;
695}
696
697/**
698 * gmc_v10_0_mc_init - initialize the memory controller driver params
699 *
700 * @adev: amdgpu_device pointer
701 *
702 * Look up the amount of vram, vram width, and decide how to place
703 * vram and gart within the GPU's physical address space.
704 * Returns 0 for success.
705 */
706static int gmc_v10_0_mc_init(struct amdgpu_device *adev)
707{
708 int r;
709
710 /* size in MB on si */
711 adev->gmc.mc_vram_size =
712 adev->nbio.funcs->get_memsize(adev) * 1024ULL * 1024ULL;
713 adev->gmc.real_vram_size = adev->gmc.mc_vram_size;
714
715 if (!(adev->flags & AMD_IS_APU)) {
716 r = amdgpu_device_resize_fb_bar(adev);
717 if (r)
718 return r;
719 }
720 adev->gmc.aper_base = pci_resource_start(adev->pdev, 0);
721 adev->gmc.aper_size = pci_resource_len(adev->pdev, 0);
722
723#ifdef CONFIG_X86_64
724 if ((adev->flags & AMD_IS_APU) && !amdgpu_passthrough(adev)) {
725 adev->gmc.aper_base = adev->gfxhub.funcs->get_mc_fb_offset(adev);
726 adev->gmc.aper_size = adev->gmc.real_vram_size;
727 }
728#endif
729
730 adev->gmc.visible_vram_size = adev->gmc.aper_size;
731
732 /* set the gart size */
733 if (amdgpu_gart_size == -1) {
734 switch (amdgpu_ip_version(adev, GC_HWIP, 0)) {
735 default:
736 adev->gmc.gart_size = 512ULL << 20;
737 break;
738 case IP_VERSION(10, 3, 1): /* DCE SG support */
739 case IP_VERSION(10, 3, 3): /* DCE SG support */
740 case IP_VERSION(10, 3, 6): /* DCE SG support */
741 case IP_VERSION(10, 3, 7): /* DCE SG support */
742 adev->gmc.gart_size = 1024ULL << 20;
743 break;
744 }
745 } else {
746 adev->gmc.gart_size = (u64)amdgpu_gart_size << 20;
747 }
748
749 gmc_v10_0_vram_gtt_location(adev, &adev->gmc);
750
751 return 0;
752}
753
754static int gmc_v10_0_gart_init(struct amdgpu_device *adev)
755{
756 int r;
757
758 if (adev->gart.bo) {
759 WARN(1, "NAVI10 PCIE GART already initialized\n");
760 return 0;
761 }
762
763 /* Initialize common gart structure */
764 r = amdgpu_gart_init(adev);
765 if (r)
766 return r;
767
768 adev->gart.table_size = adev->gart.num_gpu_pages * 8;
769 adev->gart.gart_pte_flags = AMDGPU_PTE_MTYPE_NV10(0ULL, MTYPE_UC) |
770 AMDGPU_PTE_EXECUTABLE;
771
772 return amdgpu_gart_table_vram_alloc(adev);
773}
774
775static int gmc_v10_0_sw_init(struct amdgpu_ip_block *ip_block)
776{
777 int r, vram_width = 0, vram_type = 0, vram_vendor = 0;
778 struct amdgpu_device *adev = ip_block->adev;
779
780 adev->gfxhub.funcs->init(adev);
781
782 adev->mmhub.funcs->init(adev);
783
784 spin_lock_init(&adev->gmc.invalidate_lock);
785
786 if ((adev->flags & AMD_IS_APU) && amdgpu_emu_mode == 1) {
787 adev->gmc.vram_type = AMDGPU_VRAM_TYPE_DDR4;
788 adev->gmc.vram_width = 64;
789 } else if (amdgpu_emu_mode == 1) {
790 adev->gmc.vram_type = AMDGPU_VRAM_TYPE_GDDR6;
791 adev->gmc.vram_width = 1 * 128; /* numchan * chansize */
792 } else {
793 r = amdgpu_atomfirmware_get_vram_info(adev,
794 &vram_width, &vram_type, &vram_vendor);
795 adev->gmc.vram_width = vram_width;
796
797 adev->gmc.vram_type = vram_type;
798 adev->gmc.vram_vendor = vram_vendor;
799 }
800
801 switch (amdgpu_ip_version(adev, GC_HWIP, 0)) {
802 case IP_VERSION(10, 3, 0):
803 adev->gmc.mall_size = 128 * 1024 * 1024;
804 break;
805 case IP_VERSION(10, 3, 2):
806 adev->gmc.mall_size = 96 * 1024 * 1024;
807 break;
808 case IP_VERSION(10, 3, 4):
809 adev->gmc.mall_size = 32 * 1024 * 1024;
810 break;
811 case IP_VERSION(10, 3, 5):
812 adev->gmc.mall_size = 16 * 1024 * 1024;
813 break;
814 default:
815 adev->gmc.mall_size = 0;
816 break;
817 }
818
819 switch (amdgpu_ip_version(adev, GC_HWIP, 0)) {
820 case IP_VERSION(10, 1, 10):
821 case IP_VERSION(10, 1, 1):
822 case IP_VERSION(10, 1, 2):
823 case IP_VERSION(10, 1, 3):
824 case IP_VERSION(10, 1, 4):
825 case IP_VERSION(10, 3, 0):
826 case IP_VERSION(10, 3, 2):
827 case IP_VERSION(10, 3, 1):
828 case IP_VERSION(10, 3, 4):
829 case IP_VERSION(10, 3, 5):
830 case IP_VERSION(10, 3, 6):
831 case IP_VERSION(10, 3, 3):
832 case IP_VERSION(10, 3, 7):
833 set_bit(AMDGPU_GFXHUB(0), adev->vmhubs_mask);
834 set_bit(AMDGPU_MMHUB0(0), adev->vmhubs_mask);
835 /*
836 * To fulfill 4-level page support,
837 * vm size is 256TB (48bit), maximum size of Navi10/Navi14/Navi12,
838 * block size 512 (9bit)
839 */
840 amdgpu_vm_adjust_size(adev, 256 * 1024, 9, 3, 48);
841 break;
842 default:
843 break;
844 }
845
846 /* This interrupt is VMC page fault.*/
847 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_VMC,
848 VMC_1_0__SRCID__VM_FAULT,
849 &adev->gmc.vm_fault);
850
851 if (r)
852 return r;
853
854 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_UTCL2,
855 UTCL2_1_0__SRCID__FAULT,
856 &adev->gmc.vm_fault);
857 if (r)
858 return r;
859
860 if (!amdgpu_sriov_vf(adev)) {
861 /* interrupt sent to DF. */
862 r = amdgpu_irq_add_id(adev, SOC15_IH_CLIENTID_DF, 0,
863 &adev->gmc.ecc_irq);
864 if (r)
865 return r;
866 }
867
868 /*
869 * Set the internal MC address mask This is the max address of the GPU's
870 * internal address space.
871 */
872 adev->gmc.mc_mask = 0xffffffffffffULL; /* 48 bit MC */
873
874 r = dma_set_mask_and_coherent(adev->dev, DMA_BIT_MASK(44));
875 if (r) {
876 dev_warn(adev->dev, "amdgpu: No suitable DMA available.\n");
877 return r;
878 }
879
880 adev->need_swiotlb = drm_need_swiotlb(44);
881
882 r = gmc_v10_0_mc_init(adev);
883 if (r)
884 return r;
885
886 amdgpu_gmc_get_vbios_allocations(adev);
887
888 /* Memory manager */
889 r = amdgpu_bo_init(adev);
890 if (r)
891 return r;
892
893 r = gmc_v10_0_gart_init(adev);
894 if (r)
895 return r;
896
897 /*
898 * number of VMs
899 * VMID 0 is reserved for System
900 * amdgpu graphics/compute will use VMIDs 1-7
901 * amdkfd will use VMIDs 8-15
902 */
903 adev->vm_manager.first_kfd_vmid = 8;
904
905 amdgpu_vm_manager_init(adev);
906
907 r = amdgpu_gmc_ras_sw_init(adev);
908 if (r)
909 return r;
910
911 return 0;
912}
913
914/**
915 * gmc_v10_0_gart_fini - vm fini callback
916 *
917 * @adev: amdgpu_device pointer
918 *
919 * Tears down the driver GART/VM setup (CIK).
920 */
921static void gmc_v10_0_gart_fini(struct amdgpu_device *adev)
922{
923 amdgpu_gart_table_vram_free(adev);
924}
925
926static int gmc_v10_0_sw_fini(struct amdgpu_ip_block *ip_block)
927{
928 struct amdgpu_device *adev = ip_block->adev;
929
930 amdgpu_vm_manager_fini(adev);
931 gmc_v10_0_gart_fini(adev);
932 amdgpu_gem_force_release(adev);
933 amdgpu_bo_fini(adev);
934
935 return 0;
936}
937
938static void gmc_v10_0_init_golden_registers(struct amdgpu_device *adev)
939{
940}
941
942/**
943 * gmc_v10_0_gart_enable - gart enable
944 *
945 * @adev: amdgpu_device pointer
946 */
947static int gmc_v10_0_gart_enable(struct amdgpu_device *adev)
948{
949 int r;
950 bool value;
951
952 if (adev->gart.bo == NULL) {
953 dev_err(adev->dev, "No VRAM object for PCIE GART.\n");
954 return -EINVAL;
955 }
956
957 amdgpu_gtt_mgr_recover(&adev->mman.gtt_mgr);
958
959 if (!adev->in_s0ix) {
960 r = adev->gfxhub.funcs->gart_enable(adev);
961 if (r)
962 return r;
963 }
964
965 r = adev->mmhub.funcs->gart_enable(adev);
966 if (r)
967 return r;
968
969 adev->hdp.funcs->init_registers(adev);
970
971 /* Flush HDP after it is initialized */
972 adev->hdp.funcs->flush_hdp(adev, NULL);
973
974 value = (amdgpu_vm_fault_stop == AMDGPU_VM_FAULT_STOP_ALWAYS) ?
975 false : true;
976
977 if (!adev->in_s0ix)
978 adev->gfxhub.funcs->set_fault_enable_default(adev, value);
979 adev->mmhub.funcs->set_fault_enable_default(adev, value);
980 gmc_v10_0_flush_gpu_tlb(adev, 0, AMDGPU_MMHUB0(0), 0);
981 if (!adev->in_s0ix)
982 gmc_v10_0_flush_gpu_tlb(adev, 0, AMDGPU_GFXHUB(0), 0);
983
984 DRM_INFO("PCIE GART of %uM enabled (table at 0x%016llX).\n",
985 (unsigned int)(adev->gmc.gart_size >> 20),
986 (unsigned long long)amdgpu_bo_gpu_offset(adev->gart.bo));
987
988 return 0;
989}
990
991static int gmc_v10_0_hw_init(struct amdgpu_ip_block *ip_block)
992{
993 struct amdgpu_device *adev = ip_block->adev;
994 int r;
995
996 adev->gmc.flush_pasid_uses_kiq = !amdgpu_emu_mode;
997
998 /* The sequence of these two function calls matters.*/
999 gmc_v10_0_init_golden_registers(adev);
1000
1001 /*
1002 * harvestable groups in gc_utcl2 need to be programmed before any GFX block
1003 * register setup within GMC, or else system hang when harvesting SA.
1004 */
1005 if (!adev->in_s0ix && adev->gfxhub.funcs && adev->gfxhub.funcs->utcl2_harvest)
1006 adev->gfxhub.funcs->utcl2_harvest(adev);
1007
1008 r = gmc_v10_0_gart_enable(adev);
1009 if (r)
1010 return r;
1011
1012 if (amdgpu_emu_mode == 1) {
1013 r = amdgpu_gmc_vram_checking(adev);
1014 if (r)
1015 return r;
1016 }
1017
1018 if (adev->umc.funcs && adev->umc.funcs->init_registers)
1019 adev->umc.funcs->init_registers(adev);
1020
1021 return 0;
1022}
1023
1024/**
1025 * gmc_v10_0_gart_disable - gart disable
1026 *
1027 * @adev: amdgpu_device pointer
1028 *
1029 * This disables all VM page table.
1030 */
1031static void gmc_v10_0_gart_disable(struct amdgpu_device *adev)
1032{
1033 if (!adev->in_s0ix)
1034 adev->gfxhub.funcs->gart_disable(adev);
1035 adev->mmhub.funcs->gart_disable(adev);
1036}
1037
1038static int gmc_v10_0_hw_fini(struct amdgpu_ip_block *ip_block)
1039{
1040 struct amdgpu_device *adev = ip_block->adev;
1041
1042 gmc_v10_0_gart_disable(adev);
1043
1044 if (amdgpu_sriov_vf(adev)) {
1045 /* full access mode, so don't touch any GMC register */
1046 DRM_DEBUG("For SRIOV client, shouldn't do anything.\n");
1047 return 0;
1048 }
1049
1050 amdgpu_irq_put(adev, &adev->gmc.vm_fault, 0);
1051
1052 if (adev->gmc.ecc_irq.funcs &&
1053 amdgpu_ras_is_supported(adev, AMDGPU_RAS_BLOCK__UMC))
1054 amdgpu_irq_put(adev, &adev->gmc.ecc_irq, 0);
1055
1056 return 0;
1057}
1058
1059static int gmc_v10_0_suspend(struct amdgpu_ip_block *ip_block)
1060{
1061 gmc_v10_0_hw_fini(ip_block);
1062
1063 return 0;
1064}
1065
1066static int gmc_v10_0_resume(struct amdgpu_ip_block *ip_block)
1067{
1068 int r;
1069
1070 r = gmc_v10_0_hw_init(ip_block);
1071 if (r)
1072 return r;
1073
1074 amdgpu_vmid_reset_all(ip_block->adev);
1075
1076 return 0;
1077}
1078
1079static bool gmc_v10_0_is_idle(void *handle)
1080{
1081 /* MC is always ready in GMC v10.*/
1082 return true;
1083}
1084
1085static int gmc_v10_0_wait_for_idle(struct amdgpu_ip_block *ip_block)
1086{
1087 /* There is no need to wait for MC idle in GMC v10.*/
1088 return 0;
1089}
1090
1091static int gmc_v10_0_set_clockgating_state(void *handle,
1092 enum amd_clockgating_state state)
1093{
1094 int r;
1095 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1096
1097 /*
1098 * The issue mmhub can't disconnect from DF with MMHUB clock gating being disabled
1099 * is a new problem observed at DF 3.0.3, however with the same suspend sequence not
1100 * seen any issue on the DF 3.0.2 series platform.
1101 */
1102 if (adev->in_s0ix &&
1103 amdgpu_ip_version(adev, DF_HWIP, 0) > IP_VERSION(3, 0, 2)) {
1104 dev_dbg(adev->dev, "keep mmhub clock gating being enabled for s0ix\n");
1105 return 0;
1106 }
1107
1108 r = adev->mmhub.funcs->set_clockgating(adev, state);
1109 if (r)
1110 return r;
1111
1112 if (amdgpu_ip_version(adev, ATHUB_HWIP, 0) >= IP_VERSION(2, 1, 0))
1113 return athub_v2_1_set_clockgating(adev, state);
1114 else
1115 return athub_v2_0_set_clockgating(adev, state);
1116}
1117
1118static void gmc_v10_0_get_clockgating_state(void *handle, u64 *flags)
1119{
1120 struct amdgpu_device *adev = (struct amdgpu_device *)handle;
1121
1122 if (amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(10, 1, 3) ||
1123 amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(10, 1, 4))
1124 return;
1125
1126 adev->mmhub.funcs->get_clockgating(adev, flags);
1127
1128 if (amdgpu_ip_version(adev, ATHUB_HWIP, 0) >= IP_VERSION(2, 1, 0))
1129 athub_v2_1_get_clockgating(adev, flags);
1130 else
1131 athub_v2_0_get_clockgating(adev, flags);
1132}
1133
1134static int gmc_v10_0_set_powergating_state(void *handle,
1135 enum amd_powergating_state state)
1136{
1137 return 0;
1138}
1139
1140const struct amd_ip_funcs gmc_v10_0_ip_funcs = {
1141 .name = "gmc_v10_0",
1142 .early_init = gmc_v10_0_early_init,
1143 .late_init = gmc_v10_0_late_init,
1144 .sw_init = gmc_v10_0_sw_init,
1145 .sw_fini = gmc_v10_0_sw_fini,
1146 .hw_init = gmc_v10_0_hw_init,
1147 .hw_fini = gmc_v10_0_hw_fini,
1148 .suspend = gmc_v10_0_suspend,
1149 .resume = gmc_v10_0_resume,
1150 .is_idle = gmc_v10_0_is_idle,
1151 .wait_for_idle = gmc_v10_0_wait_for_idle,
1152 .set_clockgating_state = gmc_v10_0_set_clockgating_state,
1153 .set_powergating_state = gmc_v10_0_set_powergating_state,
1154 .get_clockgating_state = gmc_v10_0_get_clockgating_state,
1155};
1156
1157const struct amdgpu_ip_block_version gmc_v10_0_ip_block = {
1158 .type = AMD_IP_BLOCK_TYPE_GMC,
1159 .major = 10,
1160 .minor = 0,
1161 .rev = 0,
1162 .funcs = &gmc_v10_0_ip_funcs,
1163};