15f300fedSBen Skeggs /*
25f300fedSBen Skeggs * Copyright 2017 Red Hat Inc.
35f300fedSBen Skeggs *
45f300fedSBen Skeggs * Permission is hereby granted, free of charge, to any person obtaining a
55f300fedSBen Skeggs * copy of this software and associated documentation files (the "Software"),
65f300fedSBen Skeggs * to deal in the Software without restriction, including without limitation
75f300fedSBen Skeggs * the rights to use, copy, modify, merge, publish, distribute, sublicense,
85f300fedSBen Skeggs * and/or sell copies of the Software, and to permit persons to whom the
95f300fedSBen Skeggs * Software is furnished to do so, subject to the following conditions:
105f300fedSBen Skeggs *
115f300fedSBen Skeggs * The above copyright notice and this permission notice shall be included in
125f300fedSBen Skeggs * all copies or substantial portions of the Software.
135f300fedSBen Skeggs *
145f300fedSBen Skeggs * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
155f300fedSBen Skeggs * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
165f300fedSBen Skeggs * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
175f300fedSBen Skeggs * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
185f300fedSBen Skeggs * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
195f300fedSBen Skeggs * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
205f300fedSBen Skeggs * OTHER DEALINGS IN THE SOFTWARE.
215f300fedSBen Skeggs */
225f300fedSBen Skeggs #include "vmm.h"
235f300fedSBen Skeggs
245f300fedSBen Skeggs #include <nvif/ifb00d.h>
255f300fedSBen Skeggs #include <nvif/unpack.h>
265f300fedSBen Skeggs
27e12cf6adSBen Skeggs static void
gm200_vmm_pgt_sparse(struct nvkm_vmm * vmm,struct nvkm_mmu_pt * pt,u32 ptei,u32 ptes)28e12cf6adSBen Skeggs gm200_vmm_pgt_sparse(struct nvkm_vmm *vmm,
29e12cf6adSBen Skeggs struct nvkm_mmu_pt *pt, u32 ptei, u32 ptes)
30e12cf6adSBen Skeggs {
31e12cf6adSBen Skeggs /* VALID_FALSE + VOL tells the MMU to treat the PTE as sparse. */
32e12cf6adSBen Skeggs VMM_FO064(pt, vmm, ptei * 8, BIT_ULL(32) /* VOL. */, ptes);
33e12cf6adSBen Skeggs }
34e12cf6adSBen Skeggs
355f300fedSBen Skeggs static const struct nvkm_vmm_desc_func
365f300fedSBen Skeggs gm200_vmm_spt = {
37e12cf6adSBen Skeggs .unmap = gf100_vmm_pgt_unmap,
38e12cf6adSBen Skeggs .sparse = gm200_vmm_pgt_sparse,
39e12cf6adSBen Skeggs .mem = gf100_vmm_pgt_mem,
40e12cf6adSBen Skeggs .dma = gf100_vmm_pgt_dma,
41e12cf6adSBen Skeggs .sgl = gf100_vmm_pgt_sgl,
425f300fedSBen Skeggs };
435f300fedSBen Skeggs
445f300fedSBen Skeggs static const struct nvkm_vmm_desc_func
455f300fedSBen Skeggs gm200_vmm_lpt = {
46e12cf6adSBen Skeggs .invalid = gk104_vmm_lpt_invalid,
47e12cf6adSBen Skeggs .unmap = gf100_vmm_pgt_unmap,
48e12cf6adSBen Skeggs .sparse = gm200_vmm_pgt_sparse,
49e12cf6adSBen Skeggs .mem = gf100_vmm_pgt_mem,
505f300fedSBen Skeggs };
515f300fedSBen Skeggs
52e12cf6adSBen Skeggs static void
gm200_vmm_pgd_sparse(struct nvkm_vmm * vmm,struct nvkm_mmu_pt * pt,u32 pdei,u32 pdes)53e12cf6adSBen Skeggs gm200_vmm_pgd_sparse(struct nvkm_vmm *vmm,
54e12cf6adSBen Skeggs struct nvkm_mmu_pt *pt, u32 pdei, u32 pdes)
55e12cf6adSBen Skeggs {
56e12cf6adSBen Skeggs /* VALID_FALSE + VOL_BIG tells the MMU to treat the PDE as sparse. */
57e12cf6adSBen Skeggs VMM_FO064(pt, vmm, pdei * 8, BIT_ULL(35) /* VOL_BIG. */, pdes);
58e12cf6adSBen Skeggs }
59e12cf6adSBen Skeggs
605f300fedSBen Skeggs static const struct nvkm_vmm_desc_func
615f300fedSBen Skeggs gm200_vmm_pgd = {
62e12cf6adSBen Skeggs .unmap = gf100_vmm_pgt_unmap,
63e12cf6adSBen Skeggs .sparse = gm200_vmm_pgd_sparse,
64e12cf6adSBen Skeggs .pde = gf100_vmm_pgd_pde,
655f300fedSBen Skeggs };
665f300fedSBen Skeggs
675f300fedSBen Skeggs const struct nvkm_vmm_desc
685f300fedSBen Skeggs gm200_vmm_desc_17_12[] = {
695f300fedSBen Skeggs { SPT, 15, 8, 0x1000, &gm200_vmm_spt },
705f300fedSBen Skeggs { PGD, 13, 8, 0x1000, &gm200_vmm_pgd },
715f300fedSBen Skeggs {}
725f300fedSBen Skeggs };
735f300fedSBen Skeggs
745f300fedSBen Skeggs const struct nvkm_vmm_desc
755f300fedSBen Skeggs gm200_vmm_desc_17_17[] = {
765f300fedSBen Skeggs { LPT, 10, 8, 0x1000, &gm200_vmm_lpt },
775f300fedSBen Skeggs { PGD, 13, 8, 0x1000, &gm200_vmm_pgd },
785f300fedSBen Skeggs {}
795f300fedSBen Skeggs };
805f300fedSBen Skeggs
815f300fedSBen Skeggs const struct nvkm_vmm_desc
825f300fedSBen Skeggs gm200_vmm_desc_16_12[] = {
835f300fedSBen Skeggs { SPT, 14, 8, 0x1000, &gm200_vmm_spt },
845f300fedSBen Skeggs { PGD, 14, 8, 0x1000, &gm200_vmm_pgd },
855f300fedSBen Skeggs {}
865f300fedSBen Skeggs };
875f300fedSBen Skeggs
885f300fedSBen Skeggs const struct nvkm_vmm_desc
895f300fedSBen Skeggs gm200_vmm_desc_16_16[] = {
905f300fedSBen Skeggs { LPT, 10, 8, 0x1000, &gm200_vmm_lpt },
915f300fedSBen Skeggs { PGD, 14, 8, 0x1000, &gm200_vmm_pgd },
925f300fedSBen Skeggs {}
935f300fedSBen Skeggs };
945f300fedSBen Skeggs
955f300fedSBen Skeggs int
gm200_vmm_join_(struct nvkm_vmm * vmm,struct nvkm_memory * inst,u64 base)965f300fedSBen Skeggs gm200_vmm_join_(struct nvkm_vmm *vmm, struct nvkm_memory *inst, u64 base)
975f300fedSBen Skeggs {
985f300fedSBen Skeggs if (vmm->func->page[1].shift == 16)
995f300fedSBen Skeggs base |= BIT_ULL(11);
1005f300fedSBen Skeggs return gf100_vmm_join_(vmm, inst, base);
1015f300fedSBen Skeggs }
1025f300fedSBen Skeggs
1035f300fedSBen Skeggs int
gm200_vmm_join(struct nvkm_vmm * vmm,struct nvkm_memory * inst)1045f300fedSBen Skeggs gm200_vmm_join(struct nvkm_vmm *vmm, struct nvkm_memory *inst)
1055f300fedSBen Skeggs {
1065f300fedSBen Skeggs return gm200_vmm_join_(vmm, inst, 0);
1075f300fedSBen Skeggs }
1085f300fedSBen Skeggs
1095f300fedSBen Skeggs static const struct nvkm_vmm_func
1105f300fedSBen Skeggs gm200_vmm_17 = {
1115f300fedSBen Skeggs .join = gm200_vmm_join,
1125f300fedSBen Skeggs .part = gf100_vmm_part,
113e12cf6adSBen Skeggs .aper = gf100_vmm_aper,
114e12cf6adSBen Skeggs .valid = gf100_vmm_valid,
115e12cf6adSBen Skeggs .flush = gf100_vmm_flush,
116d389fd4fSBen Skeggs .invalidate_pdb = gf100_vmm_invalidate_pdb,
1175f300fedSBen Skeggs .page = {
1185f300fedSBen Skeggs { 27, &gm200_vmm_desc_17_17[1], NVKM_VMM_PAGE_Sxxx },
1195f300fedSBen Skeggs { 17, &gm200_vmm_desc_17_17[0], NVKM_VMM_PAGE_SVxC },
1205f300fedSBen Skeggs { 12, &gm200_vmm_desc_17_12[0], NVKM_VMM_PAGE_SVHx },
1215f300fedSBen Skeggs {}
1225f300fedSBen Skeggs }
1235f300fedSBen Skeggs };
1245f300fedSBen Skeggs
1255f300fedSBen Skeggs static const struct nvkm_vmm_func
1265f300fedSBen Skeggs gm200_vmm_16 = {
1275f300fedSBen Skeggs .join = gm200_vmm_join,
1285f300fedSBen Skeggs .part = gf100_vmm_part,
129e12cf6adSBen Skeggs .aper = gf100_vmm_aper,
130e12cf6adSBen Skeggs .valid = gf100_vmm_valid,
131e12cf6adSBen Skeggs .flush = gf100_vmm_flush,
132d389fd4fSBen Skeggs .invalidate_pdb = gf100_vmm_invalidate_pdb,
1335f300fedSBen Skeggs .page = {
1345f300fedSBen Skeggs { 27, &gm200_vmm_desc_16_16[1], NVKM_VMM_PAGE_Sxxx },
1355f300fedSBen Skeggs { 16, &gm200_vmm_desc_16_16[0], NVKM_VMM_PAGE_SVxC },
1365f300fedSBen Skeggs { 12, &gm200_vmm_desc_16_12[0], NVKM_VMM_PAGE_SVHx },
1375f300fedSBen Skeggs {}
1385f300fedSBen Skeggs }
1395f300fedSBen Skeggs };
1405f300fedSBen Skeggs
1415f300fedSBen Skeggs int
gm200_vmm_new_(const struct nvkm_vmm_func * func_16,const struct nvkm_vmm_func * func_17,struct nvkm_mmu * mmu,bool managed,u64 addr,u64 size,void * argv,u32 argc,struct lock_class_key * key,const char * name,struct nvkm_vmm ** pvmm)1425f300fedSBen Skeggs gm200_vmm_new_(const struct nvkm_vmm_func *func_16,
1435f300fedSBen Skeggs const struct nvkm_vmm_func *func_17,
144*2606f291SBen Skeggs struct nvkm_mmu *mmu, bool managed, u64 addr, u64 size,
145*2606f291SBen Skeggs void *argv, u32 argc, struct lock_class_key *key,
146*2606f291SBen Skeggs const char *name, struct nvkm_vmm **pvmm)
1475f300fedSBen Skeggs {
1485f300fedSBen Skeggs const struct nvkm_vmm_func *func;
1495f300fedSBen Skeggs union {
1505f300fedSBen Skeggs struct gm200_vmm_vn vn;
1515f300fedSBen Skeggs struct gm200_vmm_v0 v0;
1525f300fedSBen Skeggs } *args = argv;
1535f300fedSBen Skeggs int ret = -ENOSYS;
1545f300fedSBen Skeggs
1555f300fedSBen Skeggs if (!(ret = nvif_unpack(ret, &argv, &argc, args->v0, 0, 0, false))) {
1565f300fedSBen Skeggs switch (args->v0.bigpage) {
1575f300fedSBen Skeggs case 16: func = func_16; break;
1585f300fedSBen Skeggs case 17: func = func_17; break;
1595f300fedSBen Skeggs default:
1605f300fedSBen Skeggs return -EINVAL;
1615f300fedSBen Skeggs }
1625f300fedSBen Skeggs } else
1635f300fedSBen Skeggs if (!(ret = nvif_unvers(ret, &argv, &argc, args->vn))) {
1645f300fedSBen Skeggs func = func_17;
1655f300fedSBen Skeggs } else
1665f300fedSBen Skeggs return ret;
1675f300fedSBen Skeggs
168*2606f291SBen Skeggs return nvkm_vmm_new_(func, mmu, 0, managed, addr, size, key, name, pvmm);
1695f300fedSBen Skeggs }
1705f300fedSBen Skeggs
1715f300fedSBen Skeggs int
gm200_vmm_new(struct nvkm_mmu * mmu,bool managed,u64 addr,u64 size,void * argv,u32 argc,struct lock_class_key * key,const char * name,struct nvkm_vmm ** pvmm)172*2606f291SBen Skeggs gm200_vmm_new(struct nvkm_mmu *mmu, bool managed, u64 addr, u64 size,
173*2606f291SBen Skeggs void *argv, u32 argc, struct lock_class_key *key,
174*2606f291SBen Skeggs const char *name, struct nvkm_vmm **pvmm)
1755f300fedSBen Skeggs {
176*2606f291SBen Skeggs return gm200_vmm_new_(&gm200_vmm_16, &gm200_vmm_17, mmu, managed, addr,
1775f300fedSBen Skeggs size, argv, argc, key, name, pvmm);
1785f300fedSBen Skeggs }
1795f300fedSBen Skeggs
1805f300fedSBen Skeggs int
gm200_vmm_new_fixed(struct nvkm_mmu * mmu,bool managed,u64 addr,u64 size,void * argv,u32 argc,struct lock_class_key * key,const char * name,struct nvkm_vmm ** pvmm)181*2606f291SBen Skeggs gm200_vmm_new_fixed(struct nvkm_mmu *mmu, bool managed, u64 addr, u64 size,
1825f300fedSBen Skeggs void *argv, u32 argc, struct lock_class_key *key,
1835f300fedSBen Skeggs const char *name, struct nvkm_vmm **pvmm)
1845f300fedSBen Skeggs {
185*2606f291SBen Skeggs return gf100_vmm_new_(&gm200_vmm_16, &gm200_vmm_17, mmu, managed, addr,
1865f300fedSBen Skeggs size, argv, argc, key, name, pvmm);
1875f300fedSBen Skeggs }
188