xref: /linux/drivers/gpu/drm/nouveau/nvkm/subdev/mmu/nv04.c (revision 4de93a086eb0315f0bd8e1d6da40186842670b57)
1 /*
2  * Copyright 2012 Red Hat 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  * Authors: Ben Skeggs
23  */
24 #include "nv04.h"
25 
26 #include <core/gpuobj.h>
27 
28 #define NV04_PDMA_SIZE (128 * 1024 * 1024)
29 #define NV04_PDMA_PAGE (  4 * 1024)
30 
31 /*******************************************************************************
32  * VM map/unmap callbacks
33  ******************************************************************************/
34 
35 static void
36 nv04_vm_map_sg(struct nvkm_vma *vma, struct nvkm_gpuobj *pgt,
37 	       struct nvkm_mem *mem, u32 pte, u32 cnt, dma_addr_t *list)
38 {
39 	pte = 0x00008 + (pte * 4);
40 	while (cnt) {
41 		u32 page = PAGE_SIZE / NV04_PDMA_PAGE;
42 		u32 phys = (u32)*list++;
43 		while (cnt && page--) {
44 			nv_wo32(pgt, pte, phys | 3);
45 			phys += NV04_PDMA_PAGE;
46 			pte += 4;
47 			cnt -= 1;
48 		}
49 	}
50 }
51 
52 static void
53 nv04_vm_unmap(struct nvkm_gpuobj *pgt, u32 pte, u32 cnt)
54 {
55 	pte = 0x00008 + (pte * 4);
56 	while (cnt--) {
57 		nv_wo32(pgt, pte, 0x00000000);
58 		pte += 4;
59 	}
60 }
61 
62 static void
63 nv04_vm_flush(struct nvkm_vm *vm)
64 {
65 }
66 
67 /*******************************************************************************
68  * VM object
69  ******************************************************************************/
70 
71 int
72 nv04_vm_create(struct nvkm_mmu *mmu, u64 offset, u64 length, u64 mmstart,
73 	       struct nvkm_vm **pvm)
74 {
75 	return -EINVAL;
76 }
77 
78 /*******************************************************************************
79  * MMU subdev
80  ******************************************************************************/
81 
82 static int
83 nv04_mmu_ctor(struct nvkm_object *parent, struct nvkm_object *engine,
84 	      struct nvkm_oclass *oclass, void *data, u32 size,
85 	      struct nvkm_object **pobject)
86 {
87 	struct nv04_mmu *mmu;
88 	struct nvkm_gpuobj *dma;
89 	int ret;
90 
91 	ret = nvkm_mmu_create(parent, engine, oclass, "PCIGART",
92 			      "mmu", &mmu);
93 	*pobject = nv_object(mmu);
94 	if (ret)
95 		return ret;
96 
97 	mmu->base.create = nv04_vm_create;
98 	mmu->base.limit = NV04_PDMA_SIZE;
99 	mmu->base.dma_bits = 32;
100 	mmu->base.pgt_bits = 32 - 12;
101 	mmu->base.spg_shift = 12;
102 	mmu->base.lpg_shift = 12;
103 	mmu->base.map_sg = nv04_vm_map_sg;
104 	mmu->base.unmap = nv04_vm_unmap;
105 	mmu->base.flush = nv04_vm_flush;
106 
107 	ret = nvkm_vm_create(&mmu->base, 0, NV04_PDMA_SIZE, 0, 4096,
108 			     &mmu->vm);
109 	if (ret)
110 		return ret;
111 
112 	ret = nvkm_gpuobj_new(nv_object(mmu), NULL,
113 			      (NV04_PDMA_SIZE / NV04_PDMA_PAGE) * 4 + 8,
114 			      16, NVOBJ_FLAG_ZERO_ALLOC,
115 			      &mmu->vm->pgt[0].obj[0]);
116 	dma = mmu->vm->pgt[0].obj[0];
117 	mmu->vm->pgt[0].refcount[0] = 1;
118 	if (ret)
119 		return ret;
120 
121 	nv_wo32(dma, 0x00000, 0x0002103d); /* PCI, RW, PT, !LN */
122 	nv_wo32(dma, 0x00004, NV04_PDMA_SIZE - 1);
123 	return 0;
124 }
125 
126 void
127 nv04_mmu_dtor(struct nvkm_object *object)
128 {
129 	struct nv04_mmu *mmu = (void *)object;
130 	if (mmu->vm) {
131 		nvkm_gpuobj_ref(NULL, &mmu->vm->pgt[0].obj[0]);
132 		nvkm_vm_ref(NULL, &mmu->vm, NULL);
133 	}
134 	if (mmu->nullp) {
135 		pci_free_consistent(nv_device(mmu)->pdev, 16 * 1024,
136 				    mmu->nullp, mmu->null);
137 	}
138 	nvkm_mmu_destroy(&mmu->base);
139 }
140 
141 struct nvkm_oclass
142 nv04_mmu_oclass = {
143 	.handle = NV_SUBDEV(MMU, 0x04),
144 	.ofuncs = &(struct nvkm_ofuncs) {
145 		.ctor = nv04_mmu_ctor,
146 		.dtor = nv04_mmu_dtor,
147 		.init = _nvkm_mmu_init,
148 		.fini = _nvkm_mmu_fini,
149 	},
150 };
151