xref: /linux/drivers/gpu/drm/nouveau/nvkm/subdev/mmu/nv44.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 #include <core/option.h>
28 #include <subdev/timer.h>
29 
30 #define NV44_GART_SIZE (512 * 1024 * 1024)
31 #define NV44_GART_PAGE (  4 * 1024)
32 
33 /*******************************************************************************
34  * VM map/unmap callbacks
35  ******************************************************************************/
36 
37 static void
38 nv44_vm_fill(struct nvkm_gpuobj *pgt, dma_addr_t null,
39 	     dma_addr_t *list, u32 pte, u32 cnt)
40 {
41 	u32 base = (pte << 2) & ~0x0000000f;
42 	u32 tmp[4];
43 
44 	tmp[0] = nv_ro32(pgt, base + 0x0);
45 	tmp[1] = nv_ro32(pgt, base + 0x4);
46 	tmp[2] = nv_ro32(pgt, base + 0x8);
47 	tmp[3] = nv_ro32(pgt, base + 0xc);
48 
49 	while (cnt--) {
50 		u32 addr = list ? (*list++ >> 12) : (null >> 12);
51 		switch (pte++ & 0x3) {
52 		case 0:
53 			tmp[0] &= ~0x07ffffff;
54 			tmp[0] |= addr;
55 			break;
56 		case 1:
57 			tmp[0] &= ~0xf8000000;
58 			tmp[0] |= addr << 27;
59 			tmp[1] &= ~0x003fffff;
60 			tmp[1] |= addr >> 5;
61 			break;
62 		case 2:
63 			tmp[1] &= ~0xffc00000;
64 			tmp[1] |= addr << 22;
65 			tmp[2] &= ~0x0001ffff;
66 			tmp[2] |= addr >> 10;
67 			break;
68 		case 3:
69 			tmp[2] &= ~0xfffe0000;
70 			tmp[2] |= addr << 17;
71 			tmp[3] &= ~0x00000fff;
72 			tmp[3] |= addr >> 15;
73 			break;
74 		}
75 	}
76 
77 	nv_wo32(pgt, base + 0x0, tmp[0]);
78 	nv_wo32(pgt, base + 0x4, tmp[1]);
79 	nv_wo32(pgt, base + 0x8, tmp[2]);
80 	nv_wo32(pgt, base + 0xc, tmp[3] | 0x40000000);
81 }
82 
83 static void
84 nv44_vm_map_sg(struct nvkm_vma *vma, struct nvkm_gpuobj *pgt,
85 	       struct nvkm_mem *mem, u32 pte, u32 cnt, dma_addr_t *list)
86 {
87 	struct nv04_mmu *mmu = (void *)vma->vm->mmu;
88 	u32 tmp[4];
89 	int i;
90 
91 	if (pte & 3) {
92 		u32  max = 4 - (pte & 3);
93 		u32 part = (cnt > max) ? max : cnt;
94 		nv44_vm_fill(pgt, mmu->null, list, pte, part);
95 		pte  += part;
96 		list += part;
97 		cnt  -= part;
98 	}
99 
100 	while (cnt >= 4) {
101 		for (i = 0; i < 4; i++)
102 			tmp[i] = *list++ >> 12;
103 		nv_wo32(pgt, pte++ * 4, tmp[0] >>  0 | tmp[1] << 27);
104 		nv_wo32(pgt, pte++ * 4, tmp[1] >>  5 | tmp[2] << 22);
105 		nv_wo32(pgt, pte++ * 4, tmp[2] >> 10 | tmp[3] << 17);
106 		nv_wo32(pgt, pte++ * 4, tmp[3] >> 15 | 0x40000000);
107 		cnt -= 4;
108 	}
109 
110 	if (cnt)
111 		nv44_vm_fill(pgt, mmu->null, list, pte, cnt);
112 }
113 
114 static void
115 nv44_vm_unmap(struct nvkm_gpuobj *pgt, u32 pte, u32 cnt)
116 {
117 	struct nv04_mmu *mmu = (void *)nvkm_mmu(pgt);
118 
119 	if (pte & 3) {
120 		u32  max = 4 - (pte & 3);
121 		u32 part = (cnt > max) ? max : cnt;
122 		nv44_vm_fill(pgt, mmu->null, NULL, pte, part);
123 		pte  += part;
124 		cnt  -= part;
125 	}
126 
127 	while (cnt >= 4) {
128 		nv_wo32(pgt, pte++ * 4, 0x00000000);
129 		nv_wo32(pgt, pte++ * 4, 0x00000000);
130 		nv_wo32(pgt, pte++ * 4, 0x00000000);
131 		nv_wo32(pgt, pte++ * 4, 0x00000000);
132 		cnt -= 4;
133 	}
134 
135 	if (cnt)
136 		nv44_vm_fill(pgt, mmu->null, NULL, pte, cnt);
137 }
138 
139 static void
140 nv44_vm_flush(struct nvkm_vm *vm)
141 {
142 	struct nv04_mmu *mmu = (void *)vm->mmu;
143 	nv_wr32(mmu, 0x100814, mmu->base.limit - NV44_GART_PAGE);
144 	nv_wr32(mmu, 0x100808, 0x00000020);
145 	if (!nv_wait(mmu, 0x100808, 0x00000001, 0x00000001))
146 		nv_error(mmu, "timeout: 0x%08x\n", nv_rd32(mmu, 0x100808));
147 	nv_wr32(mmu, 0x100808, 0x00000000);
148 }
149 
150 /*******************************************************************************
151  * MMU subdev
152  ******************************************************************************/
153 
154 static int
155 nv44_mmu_ctor(struct nvkm_object *parent, struct nvkm_object *engine,
156 	      struct nvkm_oclass *oclass, void *data, u32 size,
157 	      struct nvkm_object **pobject)
158 {
159 	struct nvkm_device *device = nv_device(parent);
160 	struct nv04_mmu *mmu;
161 	int ret;
162 
163 	if (pci_find_capability(device->pdev, PCI_CAP_ID_AGP) ||
164 	    !nvkm_boolopt(device->cfgopt, "NvPCIE", true)) {
165 		return nvkm_object_ctor(parent, engine, &nv04_mmu_oclass,
166 					data, size, pobject);
167 	}
168 
169 	ret = nvkm_mmu_create(parent, engine, oclass, "PCIEGART",
170 			      "mmu", &mmu);
171 	*pobject = nv_object(mmu);
172 	if (ret)
173 		return ret;
174 
175 	mmu->base.create = nv04_vm_create;
176 	mmu->base.limit = NV44_GART_SIZE;
177 	mmu->base.dma_bits = 39;
178 	mmu->base.pgt_bits = 32 - 12;
179 	mmu->base.spg_shift = 12;
180 	mmu->base.lpg_shift = 12;
181 	mmu->base.map_sg = nv44_vm_map_sg;
182 	mmu->base.unmap = nv44_vm_unmap;
183 	mmu->base.flush = nv44_vm_flush;
184 
185 	mmu->nullp = pci_alloc_consistent(device->pdev, 16 * 1024, &mmu->null);
186 	if (!mmu->nullp) {
187 		nv_warn(mmu, "unable to allocate dummy pages\n");
188 		mmu->null = 0;
189 	}
190 
191 	ret = nvkm_vm_create(&mmu->base, 0, NV44_GART_SIZE, 0, 4096,
192 			     &mmu->vm);
193 	if (ret)
194 		return ret;
195 
196 	ret = nvkm_gpuobj_new(nv_object(mmu), NULL,
197 			      (NV44_GART_SIZE / NV44_GART_PAGE) * 4,
198 			      512 * 1024, NVOBJ_FLAG_ZERO_ALLOC,
199 			      &mmu->vm->pgt[0].obj[0]);
200 	mmu->vm->pgt[0].refcount[0] = 1;
201 	if (ret)
202 		return ret;
203 
204 	return 0;
205 }
206 
207 static int
208 nv44_mmu_init(struct nvkm_object *object)
209 {
210 	struct nv04_mmu *mmu = (void *)object;
211 	struct nvkm_gpuobj *gart = mmu->vm->pgt[0].obj[0];
212 	u32 addr;
213 	int ret;
214 
215 	ret = nvkm_mmu_init(&mmu->base);
216 	if (ret)
217 		return ret;
218 
219 	/* calculate vram address of this PRAMIN block, object must be
220 	 * allocated on 512KiB alignment, and not exceed a total size
221 	 * of 512KiB for this to work correctly
222 	 */
223 	addr  = nv_rd32(mmu, 0x10020c);
224 	addr -= ((gart->addr >> 19) + 1) << 19;
225 
226 	nv_wr32(mmu, 0x100850, 0x80000000);
227 	nv_wr32(mmu, 0x100818, mmu->null);
228 	nv_wr32(mmu, 0x100804, NV44_GART_SIZE);
229 	nv_wr32(mmu, 0x100850, 0x00008000);
230 	nv_mask(mmu, 0x10008c, 0x00000200, 0x00000200);
231 	nv_wr32(mmu, 0x100820, 0x00000000);
232 	nv_wr32(mmu, 0x10082c, 0x00000001);
233 	nv_wr32(mmu, 0x100800, addr | 0x00000010);
234 	return 0;
235 }
236 
237 struct nvkm_oclass
238 nv44_mmu_oclass = {
239 	.handle = NV_SUBDEV(MMU, 0x44),
240 	.ofuncs = &(struct nvkm_ofuncs) {
241 		.ctor = nv44_mmu_ctor,
242 		.dtor = nv04_mmu_dtor,
243 		.init = nv44_mmu_init,
244 		.fini = _nvkm_mmu_fini,
245 	},
246 };
247