2 * Copyright 2017 Red Hat 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 <core/client.h>
25 #include <subdev/fb.h>
26 #include <subdev/ltc.h>
27 #include <subdev/timer.h>
28 #include <engine/gr.h>
30 #include <nvif/ifc00d.h>
31 #include <nvif/unpack.h>
34 gp100_vmm_pfn_unmap(struct nvkm_vmm
*vmm
,
35 struct nvkm_mmu_pt
*pt
, u32 ptei
, u32 ptes
)
37 struct device
*dev
= vmm
->mmu
->subdev
.device
->dev
;
40 nvkm_kmap(pt
->memory
);
42 u32 datalo
= nvkm_ro32(pt
->memory
, pt
->base
+ ptei
* 8 + 0);
43 u32 datahi
= nvkm_ro32(pt
->memory
, pt
->base
+ ptei
* 8 + 4);
44 u64 data
= (u64
)datahi
<< 32 | datalo
;
45 if ((data
& (3ULL << 1)) != 0) {
46 addr
= (data
>> 8) << 12;
47 dma_unmap_page(dev
, addr
, PAGE_SIZE
, DMA_BIDIRECTIONAL
);
51 nvkm_done(pt
->memory
);
55 gp100_vmm_pfn_clear(struct nvkm_vmm
*vmm
,
56 struct nvkm_mmu_pt
*pt
, u32 ptei
, u32 ptes
)
59 nvkm_kmap(pt
->memory
);
61 u32 datalo
= nvkm_ro32(pt
->memory
, pt
->base
+ ptei
* 8 + 0);
62 u32 datahi
= nvkm_ro32(pt
->memory
, pt
->base
+ ptei
* 8 + 4);
63 u64 data
= (u64
)datahi
<< 32 | datalo
;
64 if ((data
& BIT_ULL(0)) && (data
& (3ULL << 1)) != 0) {
65 VMM_WO064(pt
, vmm
, ptei
* 8, data
& ~BIT_ULL(0));
70 nvkm_done(pt
->memory
);
75 gp100_vmm_pgt_pfn(struct nvkm_vmm
*vmm
, struct nvkm_mmu_pt
*pt
,
76 u32 ptei
, u32 ptes
, struct nvkm_vmm_map
*map
)
78 struct device
*dev
= vmm
->mmu
->subdev
.device
->dev
;
81 nvkm_kmap(pt
->memory
);
84 if (!(*map
->pfn
& NVKM_VMM_PFN_W
))
85 data
|= BIT_ULL(6); /* RO. */
87 if (!(*map
->pfn
& NVKM_VMM_PFN_VRAM
)) {
88 addr
= *map
->pfn
>> NVKM_VMM_PFN_ADDR_SHIFT
;
89 addr
= dma_map_page(dev
, pfn_to_page(addr
), 0,
90 PAGE_SIZE
, DMA_BIDIRECTIONAL
);
91 if (!WARN_ON(dma_mapping_error(dev
, addr
))) {
93 data
|= 2ULL << 1; /* SYSTEM_COHERENT_MEMORY. */
94 data
|= BIT_ULL(3); /* VOL. */
95 data
|= BIT_ULL(0); /* VALID. */
98 data
|= (*map
->pfn
& NVKM_VMM_PFN_ADDR
) >> 4;
99 data
|= BIT_ULL(0); /* VALID. */
102 VMM_WO064(pt
, vmm
, ptei
++ * 8, data
);
105 nvkm_done(pt
->memory
);
109 gp100_vmm_pgt_pte(struct nvkm_vmm
*vmm
, struct nvkm_mmu_pt
*pt
,
110 u32 ptei
, u32 ptes
, struct nvkm_vmm_map
*map
, u64 addr
)
112 u64 data
= (addr
>> 4) | map
->type
;
114 map
->type
+= ptes
* map
->ctag
;
117 VMM_WO064(pt
, vmm
, ptei
++ * 8, data
);
123 gp100_vmm_pgt_sgl(struct nvkm_vmm
*vmm
, struct nvkm_mmu_pt
*pt
,
124 u32 ptei
, u32 ptes
, struct nvkm_vmm_map
*map
)
126 VMM_MAP_ITER_SGL(vmm
, pt
, ptei
, ptes
, map
, gp100_vmm_pgt_pte
);
130 gp100_vmm_pgt_dma(struct nvkm_vmm
*vmm
, struct nvkm_mmu_pt
*pt
,
131 u32 ptei
, u32 ptes
, struct nvkm_vmm_map
*map
)
133 if (map
->page
->shift
== PAGE_SHIFT
) {
134 VMM_SPAM(vmm
, "DMAA %08x %08x PTE(s)", ptei
, ptes
);
135 nvkm_kmap(pt
->memory
);
137 const u64 data
= (*map
->dma
++ >> 4) | map
->type
;
138 VMM_WO064(pt
, vmm
, ptei
++ * 8, data
);
139 map
->type
+= map
->ctag
;
141 nvkm_done(pt
->memory
);
145 VMM_MAP_ITER_DMA(vmm
, pt
, ptei
, ptes
, map
, gp100_vmm_pgt_pte
);
149 gp100_vmm_pgt_mem(struct nvkm_vmm
*vmm
, struct nvkm_mmu_pt
*pt
,
150 u32 ptei
, u32 ptes
, struct nvkm_vmm_map
*map
)
152 VMM_MAP_ITER_MEM(vmm
, pt
, ptei
, ptes
, map
, gp100_vmm_pgt_pte
);
156 gp100_vmm_pgt_sparse(struct nvkm_vmm
*vmm
,
157 struct nvkm_mmu_pt
*pt
, u32 ptei
, u32 ptes
)
159 /* VALID_FALSE + VOL tells the MMU to treat the PTE as sparse. */
160 VMM_FO064(pt
, vmm
, ptei
* 8, BIT_ULL(3) /* VOL. */, ptes
);
163 static const struct nvkm_vmm_desc_func
164 gp100_vmm_desc_spt
= {
165 .unmap
= gf100_vmm_pgt_unmap
,
166 .sparse
= gp100_vmm_pgt_sparse
,
167 .mem
= gp100_vmm_pgt_mem
,
168 .dma
= gp100_vmm_pgt_dma
,
169 .sgl
= gp100_vmm_pgt_sgl
,
170 .pfn
= gp100_vmm_pgt_pfn
,
171 .pfn_clear
= gp100_vmm_pfn_clear
,
172 .pfn_unmap
= gp100_vmm_pfn_unmap
,
176 gp100_vmm_lpt_invalid(struct nvkm_vmm
*vmm
,
177 struct nvkm_mmu_pt
*pt
, u32 ptei
, u32 ptes
)
179 /* VALID_FALSE + PRIV tells the MMU to ignore corresponding SPTEs. */
180 VMM_FO064(pt
, vmm
, ptei
* 8, BIT_ULL(5) /* PRIV. */, ptes
);
183 static const struct nvkm_vmm_desc_func
184 gp100_vmm_desc_lpt
= {
185 .invalid
= gp100_vmm_lpt_invalid
,
186 .unmap
= gf100_vmm_pgt_unmap
,
187 .sparse
= gp100_vmm_pgt_sparse
,
188 .mem
= gp100_vmm_pgt_mem
,
192 gp100_vmm_pd0_pte(struct nvkm_vmm
*vmm
, struct nvkm_mmu_pt
*pt
,
193 u32 ptei
, u32 ptes
, struct nvkm_vmm_map
*map
, u64 addr
)
195 u64 data
= (addr
>> 4) | map
->type
;
197 map
->type
+= ptes
* map
->ctag
;
200 VMM_WO128(pt
, vmm
, ptei
++ * 0x10, data
, 0ULL);
206 gp100_vmm_pd0_mem(struct nvkm_vmm
*vmm
, struct nvkm_mmu_pt
*pt
,
207 u32 ptei
, u32 ptes
, struct nvkm_vmm_map
*map
)
209 VMM_MAP_ITER_MEM(vmm
, pt
, ptei
, ptes
, map
, gp100_vmm_pd0_pte
);
213 gp100_vmm_pde(struct nvkm_mmu_pt
*pt
, u64
*data
)
215 switch (nvkm_memory_target(pt
->memory
)) {
216 case NVKM_MEM_TARGET_VRAM
: *data
|= 1ULL << 1; break;
217 case NVKM_MEM_TARGET_HOST
: *data
|= 2ULL << 1;
218 *data
|= BIT_ULL(3); /* VOL. */
220 case NVKM_MEM_TARGET_NCOH
: *data
|= 3ULL << 1; break;
225 *data
|= pt
->addr
>> 4;
230 gp100_vmm_pd0_pde(struct nvkm_vmm
*vmm
, struct nvkm_vmm_pt
*pgd
, u32 pdei
)
232 struct nvkm_vmm_pt
*pgt
= pgd
->pde
[pdei
];
233 struct nvkm_mmu_pt
*pd
= pgd
->pt
[0];
236 if (pgt
->pt
[0] && !gp100_vmm_pde(pgt
->pt
[0], &data
[0]))
238 if (pgt
->pt
[1] && !gp100_vmm_pde(pgt
->pt
[1], &data
[1]))
241 nvkm_kmap(pd
->memory
);
242 VMM_WO128(pd
, vmm
, pdei
* 0x10, data
[0], data
[1]);
243 nvkm_done(pd
->memory
);
247 gp100_vmm_pd0_sparse(struct nvkm_vmm
*vmm
,
248 struct nvkm_mmu_pt
*pt
, u32 pdei
, u32 pdes
)
250 /* VALID_FALSE + VOL_BIG tells the MMU to treat the PDE as sparse. */
251 VMM_FO128(pt
, vmm
, pdei
* 0x10, BIT_ULL(3) /* VOL_BIG. */, 0ULL, pdes
);
255 gp100_vmm_pd0_unmap(struct nvkm_vmm
*vmm
,
256 struct nvkm_mmu_pt
*pt
, u32 pdei
, u32 pdes
)
258 VMM_FO128(pt
, vmm
, pdei
* 0x10, 0ULL, 0ULL, pdes
);
261 static const struct nvkm_vmm_desc_func
262 gp100_vmm_desc_pd0
= {
263 .unmap
= gp100_vmm_pd0_unmap
,
264 .sparse
= gp100_vmm_pd0_sparse
,
265 .pde
= gp100_vmm_pd0_pde
,
266 .mem
= gp100_vmm_pd0_mem
,
270 gp100_vmm_pd1_pde(struct nvkm_vmm
*vmm
, struct nvkm_vmm_pt
*pgd
, u32 pdei
)
272 struct nvkm_vmm_pt
*pgt
= pgd
->pde
[pdei
];
273 struct nvkm_mmu_pt
*pd
= pgd
->pt
[0];
276 if (!gp100_vmm_pde(pgt
->pt
[0], &data
))
279 nvkm_kmap(pd
->memory
);
280 VMM_WO064(pd
, vmm
, pdei
* 8, data
);
281 nvkm_done(pd
->memory
);
284 static const struct nvkm_vmm_desc_func
285 gp100_vmm_desc_pd1
= {
286 .unmap
= gf100_vmm_pgt_unmap
,
287 .sparse
= gp100_vmm_pgt_sparse
,
288 .pde
= gp100_vmm_pd1_pde
,
291 const struct nvkm_vmm_desc
292 gp100_vmm_desc_16
[] = {
293 { LPT
, 5, 8, 0x0100, &gp100_vmm_desc_lpt
},
294 { PGD
, 8, 16, 0x1000, &gp100_vmm_desc_pd0
},
295 { PGD
, 9, 8, 0x1000, &gp100_vmm_desc_pd1
},
296 { PGD
, 9, 8, 0x1000, &gp100_vmm_desc_pd1
},
297 { PGD
, 2, 8, 0x1000, &gp100_vmm_desc_pd1
},
301 const struct nvkm_vmm_desc
302 gp100_vmm_desc_12
[] = {
303 { SPT
, 9, 8, 0x1000, &gp100_vmm_desc_spt
},
304 { PGD
, 8, 16, 0x1000, &gp100_vmm_desc_pd0
},
305 { PGD
, 9, 8, 0x1000, &gp100_vmm_desc_pd1
},
306 { PGD
, 9, 8, 0x1000, &gp100_vmm_desc_pd1
},
307 { PGD
, 2, 8, 0x1000, &gp100_vmm_desc_pd1
},
312 gp100_vmm_valid(struct nvkm_vmm
*vmm
, void *argv
, u32 argc
,
313 struct nvkm_vmm_map
*map
)
315 const enum nvkm_memory_target target
= nvkm_memory_target(map
->memory
);
316 const struct nvkm_vmm_page
*page
= map
->page
;
318 struct gp100_vmm_map_vn vn
;
319 struct gp100_vmm_map_v0 v0
;
321 struct nvkm_device
*device
= vmm
->mmu
->subdev
.device
;
322 struct nvkm_memory
*memory
= map
->memory
;
323 u8 kind
, kind_inv
, priv
, ro
, vol
;
324 int kindn
, aper
, ret
= -ENOSYS
;
327 map
->next
= (1ULL << page
->shift
) >> 4;
330 if (!(ret
= nvif_unpack(ret
, &argv
, &argc
, args
->v0
, 0, 0, false))) {
331 vol
= !!args
->v0
.vol
;
333 priv
= !!args
->v0
.priv
;
334 kind
= args
->v0
.kind
;
336 if (!(ret
= nvif_unvers(ret
, &argv
, &argc
, args
->vn
))) {
337 vol
= target
== NVKM_MEM_TARGET_HOST
;
342 VMM_DEBUG(vmm
, "args");
346 aper
= vmm
->func
->aper(target
);
347 if (WARN_ON(aper
< 0))
350 kindm
= vmm
->mmu
->func
->kind(vmm
->mmu
, &kindn
, &kind_inv
);
351 if (kind
>= kindn
|| kindm
[kind
] == kind_inv
) {
352 VMM_DEBUG(vmm
, "kind %02x", kind
);
356 if (kindm
[kind
] != kind
) {
357 u64 tags
= nvkm_memory_size(memory
) >> 16;
358 if (aper
!= 0 || !(page
->type
& NVKM_VMM_PAGE_COMP
)) {
359 VMM_DEBUG(vmm
, "comp %d %02x", aper
, page
->type
);
363 ret
= nvkm_memory_tags_get(memory
, device
, tags
,
367 VMM_DEBUG(vmm
, "comp %d", ret
);
372 tags
= map
->tags
->mn
->offset
+ (map
->offset
>> 16);
373 map
->ctag
|= ((1ULL << page
->shift
) >> 16) << 36;
374 map
->type
|= tags
<< 36;
375 map
->next
|= map
->ctag
;
382 map
->type
|= (u64
)aper
<< 1;
383 map
->type
|= (u64
) vol
<< 3;
384 map
->type
|= (u64
)priv
<< 5;
385 map
->type
|= (u64
) ro
<< 6;
386 map
->type
|= (u64
)kind
<< 56;
391 gp100_vmm_fault_cancel(struct nvkm_vmm
*vmm
, void *argv
, u32 argc
)
393 struct nvkm_device
*device
= vmm
->mmu
->subdev
.device
;
395 struct gp100_vmm_fault_cancel_v0 v0
;
400 if ((ret
= nvif_unpack(ret
, &argv
, &argc
, args
->v0
, 0, 0, false)))
403 /* Translate MaxwellFaultBufferA instance pointer to the same
404 * format as the NV_GR_FECS_CURRENT_CTX register.
406 aper
= (args
->v0
.inst
>> 8) & 3;
407 args
->v0
.inst
>>= 12;
408 args
->v0
.inst
|= aper
<< 28;
409 args
->v0
.inst
|= 0x80000000;
411 if (!WARN_ON(nvkm_gr_ctxsw_pause(device
))) {
412 if ((inst
= nvkm_gr_ctxsw_inst(device
)) == args
->v0
.inst
) {
413 gf100_vmm_invalidate(vmm
, 0x0000001b
414 /* CANCEL_TARGETED. */ |
415 (args
->v0
.hub
<< 20) |
416 (args
->v0
.gpc
<< 15) |
417 (args
->v0
.client
<< 9));
419 WARN_ON(nvkm_gr_ctxsw_resume(device
));
426 gp100_vmm_fault_replay(struct nvkm_vmm
*vmm
, void *argv
, u32 argc
)
429 struct gp100_vmm_fault_replay_vn vn
;
433 if (!(ret
= nvif_unvers(ret
, &argv
, &argc
, args
->vn
))) {
434 gf100_vmm_invalidate(vmm
, 0x0000000b); /* REPLAY_GLOBAL. */
441 gp100_vmm_mthd(struct nvkm_vmm
*vmm
,
442 struct nvkm_client
*client
, u32 mthd
, void *argv
, u32 argc
)
446 case GP100_VMM_VN_FAULT_REPLAY
:
447 return gp100_vmm_fault_replay(vmm
, argv
, argc
);
448 case GP100_VMM_VN_FAULT_CANCEL
:
449 return gp100_vmm_fault_cancel(vmm
, argv
, argc
);
458 gp100_vmm_invalidate_pdb(struct nvkm_vmm
*vmm
, u64 addr
)
460 struct nvkm_device
*device
= vmm
->mmu
->subdev
.device
;
461 nvkm_wr32(device
, 0x100cb8, lower_32_bits(addr
));
462 nvkm_wr32(device
, 0x100cec, upper_32_bits(addr
));
466 gp100_vmm_flush(struct nvkm_vmm
*vmm
, int depth
)
468 u32 type
= (5 /* CACHE_LEVEL_UP_TO_PDE3 */ - depth
) << 24;
469 type
= 0; /*XXX: need to confirm stuff works with depth enabled... */
470 if (atomic_read(&vmm
->engref
[NVKM_SUBDEV_BAR
]))
471 type
|= 0x00000004; /* HUB_ONLY */
472 type
|= 0x00000001; /* PAGE_ALL */
473 gf100_vmm_invalidate(vmm
, type
);
477 gp100_vmm_join(struct nvkm_vmm
*vmm
, struct nvkm_memory
*inst
)
479 u64 base
= BIT_ULL(10) /* VER2 */ | BIT_ULL(11) /* 64KiB */;
481 base
|= BIT_ULL(4); /* FAULT_REPLAY_TEX */
482 base
|= BIT_ULL(5); /* FAULT_REPLAY_GCC */
484 return gf100_vmm_join_(vmm
, inst
, base
);
487 static const struct nvkm_vmm_func
489 .join
= gp100_vmm_join
,
490 .part
= gf100_vmm_part
,
491 .aper
= gf100_vmm_aper
,
492 .valid
= gp100_vmm_valid
,
493 .flush
= gp100_vmm_flush
,
494 .mthd
= gp100_vmm_mthd
,
495 .invalidate_pdb
= gp100_vmm_invalidate_pdb
,
497 { 47, &gp100_vmm_desc_16
[4], NVKM_VMM_PAGE_Sxxx
},
498 { 38, &gp100_vmm_desc_16
[3], NVKM_VMM_PAGE_Sxxx
},
499 { 29, &gp100_vmm_desc_16
[2], NVKM_VMM_PAGE_Sxxx
},
500 { 21, &gp100_vmm_desc_16
[1], NVKM_VMM_PAGE_SVxC
},
501 { 16, &gp100_vmm_desc_16
[0], NVKM_VMM_PAGE_SVxC
},
502 { 12, &gp100_vmm_desc_12
[0], NVKM_VMM_PAGE_SVHx
},
508 gp100_vmm_new_(const struct nvkm_vmm_func
*func
,
509 struct nvkm_mmu
*mmu
, bool managed
, u64 addr
, u64 size
,
510 void *argv
, u32 argc
, struct lock_class_key
*key
,
511 const char *name
, struct nvkm_vmm
**pvmm
)
514 struct gp100_vmm_vn vn
;
515 struct gp100_vmm_v0 v0
;
520 if (!(ret
= nvif_unpack(ret
, &argv
, &argc
, args
->v0
, 0, 0, false))) {
521 replay
= args
->v0
.fault_replay
!= 0;
523 if (!(ret
= nvif_unvers(ret
, &argv
, &argc
, args
->vn
))) {
528 ret
= nvkm_vmm_new_(func
, mmu
, 0, managed
, addr
, size
, key
, name
, pvmm
);
532 (*pvmm
)->replay
= replay
;
537 gp100_vmm_new(struct nvkm_mmu
*mmu
, bool managed
, u64 addr
, u64 size
,
538 void *argv
, u32 argc
, struct lock_class_key
*key
,
539 const char *name
, struct nvkm_vmm
**pvmm
)
541 return gp100_vmm_new_(&gp100_vmm
, mmu
, managed
, addr
, size
,
542 argv
, argc
, key
, name
, pvmm
);