OSDN Git Service

drm/nouveau/mmu: directly use instmem for page tables
[uclinux-h8/linux.git] / drivers / gpu / drm / nouveau / nvkm / subdev / mmu / nv50.c
1 /*
2  * Copyright 2010 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 <subdev/mmu.h>
25 #include <subdev/fb.h>
26 #include <subdev/timer.h>
27
28 #include <core/engine.h>
29 #include <core/gpuobj.h>
30
31 static void
32 nv50_vm_map_pgt(struct nvkm_gpuobj *pgd, u32 pde, struct nvkm_memory *pgt[2])
33 {
34         u64 phys = 0xdeadcafe00000000ULL;
35         u32 coverage = 0;
36
37         if (pgt[0]) {
38                 /* present, 4KiB pages */
39                 phys = 0x00000003 | nvkm_memory_addr(pgt[0]);
40                 coverage = (nvkm_memory_size(pgt[0]) >> 3) << 12;
41         } else
42         if (pgt[1]) {
43                 /* present, 64KiB pages  */
44                 phys = 0x00000001 | nvkm_memory_addr(pgt[1]);
45                 coverage = (nvkm_memory_size(pgt[1]) >> 3) << 16;
46         }
47
48         if (phys & 1) {
49                 if (coverage <= 32 * 1024 * 1024)
50                         phys |= 0x60;
51                 else if (coverage <= 64 * 1024 * 1024)
52                         phys |= 0x40;
53                 else if (coverage <= 128 * 1024 * 1024)
54                         phys |= 0x20;
55         }
56
57         nvkm_kmap(pgd);
58         nvkm_wo32(pgd, (pde * 8) + 0, lower_32_bits(phys));
59         nvkm_wo32(pgd, (pde * 8) + 4, upper_32_bits(phys));
60         nvkm_done(pgd);
61 }
62
63 static inline u64
64 vm_addr(struct nvkm_vma *vma, u64 phys, u32 memtype, u32 target)
65 {
66         phys |= 1; /* present */
67         phys |= (u64)memtype << 40;
68         phys |= target << 4;
69         if (vma->access & NV_MEM_ACCESS_SYS)
70                 phys |= (1 << 6);
71         if (!(vma->access & NV_MEM_ACCESS_WO))
72                 phys |= (1 << 3);
73         return phys;
74 }
75
76 static void
77 nv50_vm_map(struct nvkm_vma *vma, struct nvkm_memory *pgt,
78             struct nvkm_mem *mem, u32 pte, u32 cnt, u64 phys, u64 delta)
79 {
80         u32 comp = (mem->memtype & 0x180) >> 7;
81         u32 block, target;
82         int i;
83
84         /* IGPs don't have real VRAM, re-target to stolen system memory */
85         target = 0;
86         if (nvkm_fb(vma->vm->mmu)->ram->stolen) {
87                 phys += nvkm_fb(vma->vm->mmu)->ram->stolen;
88                 target = 3;
89         }
90
91         phys  = vm_addr(vma, phys, mem->memtype, target);
92         pte <<= 3;
93         cnt <<= 3;
94
95         nvkm_kmap(pgt);
96         while (cnt) {
97                 u32 offset_h = upper_32_bits(phys);
98                 u32 offset_l = lower_32_bits(phys);
99
100                 for (i = 7; i >= 0; i--) {
101                         block = 1 << (i + 3);
102                         if (cnt >= block && !(pte & (block - 1)))
103                                 break;
104                 }
105                 offset_l |= (i << 7);
106
107                 phys += block << (vma->node->type - 3);
108                 cnt  -= block;
109                 if (comp) {
110                         u32 tag = mem->tag->offset + ((delta >> 16) * comp);
111                         offset_h |= (tag << 17);
112                         delta    += block << (vma->node->type - 3);
113                 }
114
115                 while (block) {
116                         nvkm_wo32(pgt, pte + 0, offset_l);
117                         nvkm_wo32(pgt, pte + 4, offset_h);
118                         pte += 8;
119                         block -= 8;
120                 }
121         }
122         nvkm_done(pgt);
123 }
124
125 static void
126 nv50_vm_map_sg(struct nvkm_vma *vma, struct nvkm_memory *pgt,
127                struct nvkm_mem *mem, u32 pte, u32 cnt, dma_addr_t *list)
128 {
129         u32 target = (vma->access & NV_MEM_ACCESS_NOSNOOP) ? 3 : 2;
130         pte <<= 3;
131         nvkm_kmap(pgt);
132         while (cnt--) {
133                 u64 phys = vm_addr(vma, (u64)*list++, mem->memtype, target);
134                 nvkm_wo32(pgt, pte + 0, lower_32_bits(phys));
135                 nvkm_wo32(pgt, pte + 4, upper_32_bits(phys));
136                 pte += 8;
137         }
138         nvkm_done(pgt);
139 }
140
141 static void
142 nv50_vm_unmap(struct nvkm_vma *vma, struct nvkm_memory *pgt, u32 pte, u32 cnt)
143 {
144         pte <<= 3;
145         nvkm_kmap(pgt);
146         while (cnt--) {
147                 nvkm_wo32(pgt, pte + 0, 0x00000000);
148                 nvkm_wo32(pgt, pte + 4, 0x00000000);
149                 pte += 8;
150         }
151         nvkm_done(pgt);
152 }
153
154 static void
155 nv50_vm_flush(struct nvkm_vm *vm)
156 {
157         struct nvkm_mmu *mmu = (void *)vm->mmu;
158         struct nvkm_subdev *subdev = &mmu->subdev;
159         struct nvkm_device *device = subdev->device;
160         struct nvkm_engine *engine;
161         int i, vme;
162
163         mutex_lock(&subdev->mutex);
164         for (i = 0; i < NVDEV_SUBDEV_NR; i++) {
165                 if (!atomic_read(&vm->engref[i]))
166                         continue;
167
168                 /* unfortunate hw bug workaround... */
169                 engine = nvkm_engine(mmu, i);
170                 if (engine && engine->tlb_flush) {
171                         engine->tlb_flush(engine);
172                         continue;
173                 }
174
175                 switch (i) {
176                 case NVDEV_ENGINE_GR    : vme = 0x00; break;
177                 case NVDEV_ENGINE_VP    :
178                 case NVDEV_ENGINE_MSPDEC: vme = 0x01; break;
179                 case NVDEV_SUBDEV_BAR   : vme = 0x06; break;
180                 case NVDEV_ENGINE_MSPPP :
181                 case NVDEV_ENGINE_MPEG  : vme = 0x08; break;
182                 case NVDEV_ENGINE_BSP   :
183                 case NVDEV_ENGINE_MSVLD : vme = 0x09; break;
184                 case NVDEV_ENGINE_CIPHER:
185                 case NVDEV_ENGINE_SEC   : vme = 0x0a; break;
186                 case NVDEV_ENGINE_CE0   : vme = 0x0d; break;
187                 default:
188                         continue;
189                 }
190
191                 nvkm_wr32(device, 0x100c80, (vme << 16) | 1);
192                 if (nvkm_msec(device, 2000,
193                         if (!(nvkm_rd32(device, 0x100c80) & 0x00000001))
194                                 break;
195                 ) < 0)
196                         nvkm_error(subdev, "vm flush timeout: engine %d\n", vme);
197         }
198         mutex_unlock(&subdev->mutex);
199 }
200
201 static int
202 nv50_vm_create(struct nvkm_mmu *mmu, u64 offset, u64 length, u64 mm_offset,
203                struct lock_class_key *key, struct nvkm_vm **pvm)
204 {
205         u32 block = (1 << (mmu->pgt_bits + 12));
206         if (block > length)
207                 block = length;
208
209         return nvkm_vm_create(mmu, offset, length, mm_offset, block, key, pvm);
210 }
211
212 static int
213 nv50_mmu_ctor(struct nvkm_object *parent, struct nvkm_object *engine,
214               struct nvkm_oclass *oclass, void *data, u32 size,
215               struct nvkm_object **pobject)
216 {
217         struct nvkm_mmu *mmu;
218         int ret;
219
220         ret = nvkm_mmu_create(parent, engine, oclass, "VM", "mmu", &mmu);
221         *pobject = nv_object(mmu);
222         if (ret)
223                 return ret;
224
225         mmu->limit = 1ULL << 40;
226         mmu->dma_bits = 40;
227         mmu->pgt_bits  = 29 - 12;
228         mmu->spg_shift = 12;
229         mmu->lpg_shift = 16;
230         mmu->create = nv50_vm_create;
231         mmu->map_pgt = nv50_vm_map_pgt;
232         mmu->map = nv50_vm_map;
233         mmu->map_sg = nv50_vm_map_sg;
234         mmu->unmap = nv50_vm_unmap;
235         mmu->flush = nv50_vm_flush;
236         return 0;
237 }
238
239 struct nvkm_oclass
240 nv50_mmu_oclass = {
241         .handle = NV_SUBDEV(MMU, 0x50),
242         .ofuncs = &(struct nvkm_ofuncs) {
243                 .ctor = nv50_mmu_ctor,
244                 .dtor = _nvkm_mmu_dtor,
245                 .init = _nvkm_mmu_init,
246                 .fini = _nvkm_mmu_fini,
247         },
248 };