blob: a361d1612bd74162fdfa4686163b8e41cd221ac4 [file] [log] [blame]
Ben Skeggs6ee73862009-12-11 19:24:15 +10001/*
2 * Copyright (C) 2007 Ben Skeggs.
3 *
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial
16 * portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 */
27
28#include "drmP.h"
29#include "drm.h"
30#include "nouveau_drv.h"
31
32struct nv50_instmem_priv {
33 uint32_t save1700[5]; /* 0x1700->0x1710 */
34
35 struct nouveau_gpuobj_ref *pramin_pt;
36 struct nouveau_gpuobj_ref *pramin_bar;
37 struct nouveau_gpuobj_ref *fb_bar;
38
39 bool last_access_wr;
40};
41
42#define NV50_INSTMEM_PAGE_SHIFT 12
43#define NV50_INSTMEM_PAGE_SIZE (1 << NV50_INSTMEM_PAGE_SHIFT)
44#define NV50_INSTMEM_PT_SIZE(a) (((a) >> 12) << 3)
45
46/*NOTE: - Assumes 0x1700 already covers the correct MiB of PRAMIN
47 */
48#define BAR0_WI32(g, o, v) do { \
49 uint32_t offset; \
50 if ((g)->im_backing) { \
51 offset = (g)->im_backing_start; \
52 } else { \
53 offset = chan->ramin->gpuobj->im_backing_start; \
54 offset += (g)->im_pramin->start; \
55 } \
56 offset += (o); \
57 nv_wr32(dev, NV_RAMIN + (offset & 0xfffff), (v)); \
58} while (0)
59
60int
61nv50_instmem_init(struct drm_device *dev)
62{
63 struct drm_nouveau_private *dev_priv = dev->dev_private;
64 struct nouveau_channel *chan;
65 uint32_t c_offset, c_size, c_ramfc, c_vmpd, c_base, pt_size;
Ben Skeggs494ab822010-03-19 12:49:59 +100066 uint32_t save_nv001700;
67 uint64_t v;
Ben Skeggs6ee73862009-12-11 19:24:15 +100068 struct nv50_instmem_priv *priv;
69 int ret, i;
Ben Skeggs6ee73862009-12-11 19:24:15 +100070
71 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
72 if (!priv)
73 return -ENOMEM;
74 dev_priv->engine.instmem.priv = priv;
75
76 /* Save state, will restore at takedown. */
77 for (i = 0x1700; i <= 0x1710; i += 4)
78 priv->save1700[(i-0x1700)/4] = nv_rd32(dev, i);
79
80 /* Reserve the last MiB of VRAM, we should probably try to avoid
81 * setting up the below tables over the top of the VBIOS image at
82 * some point.
83 */
84 dev_priv->ramin_rsvd_vram = 1 << 20;
Ben Skeggsa76fb4e2010-03-18 09:45:20 +100085 c_offset = dev_priv->vram_size - dev_priv->ramin_rsvd_vram;
Ben Skeggs6ee73862009-12-11 19:24:15 +100086 c_size = 128 << 10;
87 c_vmpd = ((dev_priv->chipset & 0xf0) == 0x50) ? 0x1400 : 0x200;
88 c_ramfc = ((dev_priv->chipset & 0xf0) == 0x50) ? 0x0 : 0x20;
89 c_base = c_vmpd + 0x4000;
90 pt_size = NV50_INSTMEM_PT_SIZE(dev_priv->ramin_size);
91
92 NV_DEBUG(dev, " Rsvd VRAM base: 0x%08x\n", c_offset);
93 NV_DEBUG(dev, " VBIOS image: 0x%08x\n",
94 (nv_rd32(dev, 0x619f04) & ~0xff) << 8);
95 NV_DEBUG(dev, " Aperture size: %d MiB\n", dev_priv->ramin_size >> 20);
96 NV_DEBUG(dev, " PT size: %d KiB\n", pt_size >> 10);
97
98 /* Determine VM layout, we need to do this first to make sure
99 * we allocate enough memory for all the page tables.
100 */
101 dev_priv->vm_gart_base = roundup(NV50_VM_BLOCK, NV50_VM_BLOCK);
102 dev_priv->vm_gart_size = NV50_VM_BLOCK;
103
104 dev_priv->vm_vram_base = dev_priv->vm_gart_base + dev_priv->vm_gart_size;
Ben Skeggsa76fb4e2010-03-18 09:45:20 +1000105 dev_priv->vm_vram_size = dev_priv->vram_size;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000106 if (dev_priv->vm_vram_size > NV50_VM_MAX_VRAM)
107 dev_priv->vm_vram_size = NV50_VM_MAX_VRAM;
108 dev_priv->vm_vram_size = roundup(dev_priv->vm_vram_size, NV50_VM_BLOCK);
109 dev_priv->vm_vram_pt_nr = dev_priv->vm_vram_size / NV50_VM_BLOCK;
110
111 dev_priv->vm_end = dev_priv->vm_vram_base + dev_priv->vm_vram_size;
112
113 NV_DEBUG(dev, "NV50VM: GART 0x%016llx-0x%016llx\n",
114 dev_priv->vm_gart_base,
115 dev_priv->vm_gart_base + dev_priv->vm_gart_size - 1);
116 NV_DEBUG(dev, "NV50VM: VRAM 0x%016llx-0x%016llx\n",
117 dev_priv->vm_vram_base,
118 dev_priv->vm_vram_base + dev_priv->vm_vram_size - 1);
119
120 c_size += dev_priv->vm_vram_pt_nr * (NV50_VM_BLOCK / 65536 * 8);
121
122 /* Map BAR0 PRAMIN aperture over the memory we want to use */
123 save_nv001700 = nv_rd32(dev, NV50_PUNK_BAR0_PRAMIN);
124 nv_wr32(dev, NV50_PUNK_BAR0_PRAMIN, (c_offset >> 16));
125
126 /* Create a fake channel, and use it as our "dummy" channels 0/127.
127 * The main reason for creating a channel is so we can use the gpuobj
128 * code. However, it's probably worth noting that NVIDIA also setup
129 * their channels 0/127 with the same values they configure here.
130 * So, there may be some other reason for doing this.
131 *
132 * Have to create the entire channel manually, as the real channel
133 * creation code assumes we have PRAMIN access, and we don't until
134 * we're done here.
135 */
136 chan = kzalloc(sizeof(*chan), GFP_KERNEL);
137 if (!chan)
138 return -ENOMEM;
139 chan->id = 0;
140 chan->dev = dev;
141 chan->file_priv = (struct drm_file *)-2;
142 dev_priv->fifos[0] = dev_priv->fifos[127] = chan;
143
144 /* Channel's PRAMIN object + heap */
145 ret = nouveau_gpuobj_new_fake(dev, 0, c_offset, c_size, 0,
146 NULL, &chan->ramin);
147 if (ret)
148 return ret;
149
Ben Skeggsb833ac22010-06-01 15:32:24 +1000150 if (drm_mm_init(&chan->ramin_heap, c_base, c_size - c_base))
Ben Skeggs6ee73862009-12-11 19:24:15 +1000151 return -ENOMEM;
152
153 /* RAMFC + zero channel's PRAMIN up to start of VM pagedir */
154 ret = nouveau_gpuobj_new_fake(dev, c_ramfc, c_offset + c_ramfc,
155 0x4000, 0, NULL, &chan->ramfc);
156 if (ret)
157 return ret;
158
159 for (i = 0; i < c_vmpd; i += 4)
160 BAR0_WI32(chan->ramin->gpuobj, i, 0);
161
162 /* VM page directory */
163 ret = nouveau_gpuobj_new_fake(dev, c_vmpd, c_offset + c_vmpd,
164 0x4000, 0, &chan->vm_pd, NULL);
165 if (ret)
166 return ret;
167 for (i = 0; i < 0x4000; i += 8) {
168 BAR0_WI32(chan->vm_pd, i + 0x00, 0x00000000);
169 BAR0_WI32(chan->vm_pd, i + 0x04, 0x00000000);
170 }
171
172 /* PRAMIN page table, cheat and map into VM at 0x0000000000.
173 * We map the entire fake channel into the start of the PRAMIN BAR
174 */
175 ret = nouveau_gpuobj_new_ref(dev, chan, NULL, 0, pt_size, 0x1000,
Ben Skeggs76befb82010-02-20 08:06:36 +1000176 0, &priv->pramin_pt);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000177 if (ret)
178 return ret;
179
Ben Skeggs76befb82010-02-20 08:06:36 +1000180 v = c_offset | 1;
181 if (dev_priv->vram_sys_base) {
182 v += dev_priv->vram_sys_base;
183 v |= 0x30;
184 }
185
186 i = 0;
187 while (v < dev_priv->vram_sys_base + c_offset + c_size) {
Ben Skeggs494ab822010-03-19 12:49:59 +1000188 BAR0_WI32(priv->pramin_pt->gpuobj, i + 0, lower_32_bits(v));
189 BAR0_WI32(priv->pramin_pt->gpuobj, i + 4, upper_32_bits(v));
Ben Skeggs76befb82010-02-20 08:06:36 +1000190 v += 0x1000;
191 i += 8;
192 }
193
194 while (i < pt_size) {
195 BAR0_WI32(priv->pramin_pt->gpuobj, i + 0, 0x00000000);
196 BAR0_WI32(priv->pramin_pt->gpuobj, i + 4, 0x00000000);
197 i += 8;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000198 }
199
200 BAR0_WI32(chan->vm_pd, 0x00, priv->pramin_pt->instance | 0x63);
201 BAR0_WI32(chan->vm_pd, 0x04, 0x00000000);
202
203 /* VRAM page table(s), mapped into VM at +1GiB */
204 for (i = 0; i < dev_priv->vm_vram_pt_nr; i++) {
205 ret = nouveau_gpuobj_new_ref(dev, chan, NULL, 0,
206 NV50_VM_BLOCK/65536*8, 0, 0,
207 &chan->vm_vram_pt[i]);
208 if (ret) {
209 NV_ERROR(dev, "Error creating VRAM page tables: %d\n",
210 ret);
211 dev_priv->vm_vram_pt_nr = i;
212 return ret;
213 }
214 dev_priv->vm_vram_pt[i] = chan->vm_vram_pt[i]->gpuobj;
215
216 for (v = 0; v < dev_priv->vm_vram_pt[i]->im_pramin->size;
217 v += 4)
218 BAR0_WI32(dev_priv->vm_vram_pt[i], v, 0);
219
220 BAR0_WI32(chan->vm_pd, 0x10 + (i*8),
221 chan->vm_vram_pt[i]->instance | 0x61);
222 BAR0_WI32(chan->vm_pd, 0x14 + (i*8), 0);
223 }
224
225 /* DMA object for PRAMIN BAR */
226 ret = nouveau_gpuobj_new_ref(dev, chan, chan, 0, 6*4, 16, 0,
227 &priv->pramin_bar);
228 if (ret)
229 return ret;
230 BAR0_WI32(priv->pramin_bar->gpuobj, 0x00, 0x7fc00000);
231 BAR0_WI32(priv->pramin_bar->gpuobj, 0x04, dev_priv->ramin_size - 1);
232 BAR0_WI32(priv->pramin_bar->gpuobj, 0x08, 0x00000000);
233 BAR0_WI32(priv->pramin_bar->gpuobj, 0x0c, 0x00000000);
234 BAR0_WI32(priv->pramin_bar->gpuobj, 0x10, 0x00000000);
235 BAR0_WI32(priv->pramin_bar->gpuobj, 0x14, 0x00000000);
236
237 /* DMA object for FB BAR */
238 ret = nouveau_gpuobj_new_ref(dev, chan, chan, 0, 6*4, 16, 0,
239 &priv->fb_bar);
240 if (ret)
241 return ret;
242 BAR0_WI32(priv->fb_bar->gpuobj, 0x00, 0x7fc00000);
243 BAR0_WI32(priv->fb_bar->gpuobj, 0x04, 0x40000000 +
Jordan Crouse01d73a62010-05-27 13:40:24 -0600244 pci_resource_len(dev->pdev, 1) - 1);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000245 BAR0_WI32(priv->fb_bar->gpuobj, 0x08, 0x40000000);
246 BAR0_WI32(priv->fb_bar->gpuobj, 0x0c, 0x00000000);
247 BAR0_WI32(priv->fb_bar->gpuobj, 0x10, 0x00000000);
248 BAR0_WI32(priv->fb_bar->gpuobj, 0x14, 0x00000000);
249
250 /* Poke the relevant regs, and pray it works :) */
251 nv_wr32(dev, NV50_PUNK_BAR_CFG_BASE, (chan->ramin->instance >> 12));
252 nv_wr32(dev, NV50_PUNK_UNK1710, 0);
253 nv_wr32(dev, NV50_PUNK_BAR_CFG_BASE, (chan->ramin->instance >> 12) |
254 NV50_PUNK_BAR_CFG_BASE_VALID);
255 nv_wr32(dev, NV50_PUNK_BAR1_CTXDMA, (priv->fb_bar->instance >> 4) |
256 NV50_PUNK_BAR1_CTXDMA_VALID);
257 nv_wr32(dev, NV50_PUNK_BAR3_CTXDMA, (priv->pramin_bar->instance >> 4) |
258 NV50_PUNK_BAR3_CTXDMA_VALID);
259
260 for (i = 0; i < 8; i++)
261 nv_wr32(dev, 0x1900 + (i*4), 0);
262
263 /* Assume that praying isn't enough, check that we can re-read the
264 * entire fake channel back from the PRAMIN BAR */
265 dev_priv->engine.instmem.prepare_access(dev, false);
266 for (i = 0; i < c_size; i += 4) {
267 if (nv_rd32(dev, NV_RAMIN + i) != nv_ri32(dev, i)) {
268 NV_ERROR(dev, "Error reading back PRAMIN at 0x%08x\n",
269 i);
270 dev_priv->engine.instmem.finish_access(dev);
271 return -EINVAL;
272 }
273 }
274 dev_priv->engine.instmem.finish_access(dev);
275
276 nv_wr32(dev, NV50_PUNK_BAR0_PRAMIN, save_nv001700);
277
278 /* Global PRAMIN heap */
Ben Skeggsb833ac22010-06-01 15:32:24 +1000279 if (drm_mm_init(&dev_priv->ramin_heap, c_size, dev_priv->ramin_size - c_size)) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000280 NV_ERROR(dev, "Failed to init RAMIN heap\n");
281 }
282
283 /*XXX: incorrect, but needed to make hash func "work" */
284 dev_priv->ramht_offset = 0x10000;
285 dev_priv->ramht_bits = 9;
286 dev_priv->ramht_size = (1 << dev_priv->ramht_bits);
287 return 0;
288}
289
290void
291nv50_instmem_takedown(struct drm_device *dev)
292{
293 struct drm_nouveau_private *dev_priv = dev->dev_private;
294 struct nv50_instmem_priv *priv = dev_priv->engine.instmem.priv;
295 struct nouveau_channel *chan = dev_priv->fifos[0];
296 int i;
297
298 NV_DEBUG(dev, "\n");
299
300 if (!priv)
301 return;
302
303 /* Restore state from before init */
304 for (i = 0x1700; i <= 0x1710; i += 4)
305 nv_wr32(dev, i, priv->save1700[(i - 0x1700) / 4]);
306
307 nouveau_gpuobj_ref_del(dev, &priv->fb_bar);
308 nouveau_gpuobj_ref_del(dev, &priv->pramin_bar);
309 nouveau_gpuobj_ref_del(dev, &priv->pramin_pt);
310
311 /* Destroy dummy channel */
312 if (chan) {
313 for (i = 0; i < dev_priv->vm_vram_pt_nr; i++) {
314 nouveau_gpuobj_ref_del(dev, &chan->vm_vram_pt[i]);
315 dev_priv->vm_vram_pt[i] = NULL;
316 }
317 dev_priv->vm_vram_pt_nr = 0;
318
319 nouveau_gpuobj_del(dev, &chan->vm_pd);
320 nouveau_gpuobj_ref_del(dev, &chan->ramfc);
321 nouveau_gpuobj_ref_del(dev, &chan->ramin);
Ben Skeggsb833ac22010-06-01 15:32:24 +1000322 drm_mm_takedown(&chan->ramin_heap);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000323
324 dev_priv->fifos[0] = dev_priv->fifos[127] = NULL;
325 kfree(chan);
326 }
327
328 dev_priv->engine.instmem.priv = NULL;
329 kfree(priv);
330}
331
332int
333nv50_instmem_suspend(struct drm_device *dev)
334{
335 struct drm_nouveau_private *dev_priv = dev->dev_private;
336 struct nouveau_channel *chan = dev_priv->fifos[0];
337 struct nouveau_gpuobj *ramin = chan->ramin->gpuobj;
338 int i;
339
340 ramin->im_backing_suspend = vmalloc(ramin->im_pramin->size);
341 if (!ramin->im_backing_suspend)
342 return -ENOMEM;
343
344 for (i = 0; i < ramin->im_pramin->size; i += 4)
345 ramin->im_backing_suspend[i/4] = nv_ri32(dev, i);
346 return 0;
347}
348
349void
350nv50_instmem_resume(struct drm_device *dev)
351{
352 struct drm_nouveau_private *dev_priv = dev->dev_private;
353 struct nv50_instmem_priv *priv = dev_priv->engine.instmem.priv;
354 struct nouveau_channel *chan = dev_priv->fifos[0];
355 struct nouveau_gpuobj *ramin = chan->ramin->gpuobj;
356 int i;
357
358 nv_wr32(dev, NV50_PUNK_BAR0_PRAMIN, (ramin->im_backing_start >> 16));
359 for (i = 0; i < ramin->im_pramin->size; i += 4)
360 BAR0_WI32(ramin, i, ramin->im_backing_suspend[i/4]);
361 vfree(ramin->im_backing_suspend);
362 ramin->im_backing_suspend = NULL;
363
364 /* Poke the relevant regs, and pray it works :) */
365 nv_wr32(dev, NV50_PUNK_BAR_CFG_BASE, (chan->ramin->instance >> 12));
366 nv_wr32(dev, NV50_PUNK_UNK1710, 0);
367 nv_wr32(dev, NV50_PUNK_BAR_CFG_BASE, (chan->ramin->instance >> 12) |
368 NV50_PUNK_BAR_CFG_BASE_VALID);
369 nv_wr32(dev, NV50_PUNK_BAR1_CTXDMA, (priv->fb_bar->instance >> 4) |
370 NV50_PUNK_BAR1_CTXDMA_VALID);
371 nv_wr32(dev, NV50_PUNK_BAR3_CTXDMA, (priv->pramin_bar->instance >> 4) |
372 NV50_PUNK_BAR3_CTXDMA_VALID);
373
374 for (i = 0; i < 8; i++)
375 nv_wr32(dev, 0x1900 + (i*4), 0);
376}
377
378int
379nv50_instmem_populate(struct drm_device *dev, struct nouveau_gpuobj *gpuobj,
380 uint32_t *sz)
381{
382 int ret;
383
384 if (gpuobj->im_backing)
385 return -EINVAL;
386
Matt Turner3bfc7d22010-02-24 23:27:10 -0500387 *sz = ALIGN(*sz, NV50_INSTMEM_PAGE_SIZE);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000388 if (*sz == 0)
389 return -EINVAL;
390
391 ret = nouveau_bo_new(dev, NULL, *sz, 0, TTM_PL_FLAG_VRAM, 0, 0x0000,
392 true, false, &gpuobj->im_backing);
393 if (ret) {
394 NV_ERROR(dev, "error getting PRAMIN backing pages: %d\n", ret);
395 return ret;
396 }
397
398 ret = nouveau_bo_pin(gpuobj->im_backing, TTM_PL_FLAG_VRAM);
399 if (ret) {
400 NV_ERROR(dev, "error pinning PRAMIN backing VRAM: %d\n", ret);
401 nouveau_bo_ref(NULL, &gpuobj->im_backing);
402 return ret;
403 }
404
405 gpuobj->im_backing_start = gpuobj->im_backing->bo.mem.mm_node->start;
406 gpuobj->im_backing_start <<= PAGE_SHIFT;
407
408 return 0;
409}
410
411void
412nv50_instmem_clear(struct drm_device *dev, struct nouveau_gpuobj *gpuobj)
413{
414 struct drm_nouveau_private *dev_priv = dev->dev_private;
415
416 if (gpuobj && gpuobj->im_backing) {
417 if (gpuobj->im_bound)
418 dev_priv->engine.instmem.unbind(dev, gpuobj);
419 nouveau_bo_unpin(gpuobj->im_backing);
420 nouveau_bo_ref(NULL, &gpuobj->im_backing);
421 gpuobj->im_backing = NULL;
422 }
423}
424
425int
426nv50_instmem_bind(struct drm_device *dev, struct nouveau_gpuobj *gpuobj)
427{
428 struct drm_nouveau_private *dev_priv = dev->dev_private;
429 struct nv50_instmem_priv *priv = dev_priv->engine.instmem.priv;
Ben Skeggs76befb82010-02-20 08:06:36 +1000430 struct nouveau_gpuobj *pramin_pt = priv->pramin_pt->gpuobj;
431 uint32_t pte, pte_end;
432 uint64_t vram;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000433
434 if (!gpuobj->im_backing || !gpuobj->im_pramin || gpuobj->im_bound)
435 return -EINVAL;
436
Ben Skeggsb833ac22010-06-01 15:32:24 +1000437 NV_DEBUG(dev, "st=0x%lx sz=0x%lx\n",
Ben Skeggs6ee73862009-12-11 19:24:15 +1000438 gpuobj->im_pramin->start, gpuobj->im_pramin->size);
439
Ben Skeggs76befb82010-02-20 08:06:36 +1000440 pte = (gpuobj->im_pramin->start >> 12) << 1;
441 pte_end = ((gpuobj->im_pramin->size >> 12) << 1) + pte;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000442 vram = gpuobj->im_backing_start;
443
Ben Skeggsb833ac22010-06-01 15:32:24 +1000444 NV_DEBUG(dev, "pramin=0x%lx, pte=%d, pte_end=%d\n",
Ben Skeggs6ee73862009-12-11 19:24:15 +1000445 gpuobj->im_pramin->start, pte, pte_end);
446 NV_DEBUG(dev, "first vram page: 0x%08x\n", gpuobj->im_backing_start);
447
Ben Skeggs76befb82010-02-20 08:06:36 +1000448 vram |= 1;
449 if (dev_priv->vram_sys_base) {
450 vram += dev_priv->vram_sys_base;
451 vram |= 0x30;
452 }
453
Ben Skeggs6ee73862009-12-11 19:24:15 +1000454 dev_priv->engine.instmem.prepare_access(dev, true);
455 while (pte < pte_end) {
Ben Skeggs76befb82010-02-20 08:06:36 +1000456 nv_wo32(dev, pramin_pt, pte++, lower_32_bits(vram));
457 nv_wo32(dev, pramin_pt, pte++, upper_32_bits(vram));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000458 vram += NV50_INSTMEM_PAGE_SIZE;
459 }
460 dev_priv->engine.instmem.finish_access(dev);
461
462 nv_wr32(dev, 0x100c80, 0x00040001);
463 if (!nv_wait(0x100c80, 0x00000001, 0x00000000)) {
464 NV_ERROR(dev, "timeout: (0x100c80 & 1) == 0 (1)\n");
465 NV_ERROR(dev, "0x100c80 = 0x%08x\n", nv_rd32(dev, 0x100c80));
466 return -EBUSY;
467 }
468
469 nv_wr32(dev, 0x100c80, 0x00060001);
470 if (!nv_wait(0x100c80, 0x00000001, 0x00000000)) {
471 NV_ERROR(dev, "timeout: (0x100c80 & 1) == 0 (2)\n");
472 NV_ERROR(dev, "0x100c80 = 0x%08x\n", nv_rd32(dev, 0x100c80));
473 return -EBUSY;
474 }
475
476 gpuobj->im_bound = 1;
477 return 0;
478}
479
480int
481nv50_instmem_unbind(struct drm_device *dev, struct nouveau_gpuobj *gpuobj)
482{
483 struct drm_nouveau_private *dev_priv = dev->dev_private;
484 struct nv50_instmem_priv *priv = dev_priv->engine.instmem.priv;
485 uint32_t pte, pte_end;
486
487 if (gpuobj->im_bound == 0)
488 return -EINVAL;
489
Ben Skeggs76befb82010-02-20 08:06:36 +1000490 pte = (gpuobj->im_pramin->start >> 12) << 1;
491 pte_end = ((gpuobj->im_pramin->size >> 12) << 1) + pte;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000492
493 dev_priv->engine.instmem.prepare_access(dev, true);
494 while (pte < pte_end) {
Ben Skeggs76befb82010-02-20 08:06:36 +1000495 nv_wo32(dev, priv->pramin_pt->gpuobj, pte++, 0x00000000);
496 nv_wo32(dev, priv->pramin_pt->gpuobj, pte++, 0x00000000);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000497 }
498 dev_priv->engine.instmem.finish_access(dev);
499
500 gpuobj->im_bound = 0;
501 return 0;
502}
503
504void
505nv50_instmem_prepare_access(struct drm_device *dev, bool write)
506{
507 struct drm_nouveau_private *dev_priv = dev->dev_private;
508 struct nv50_instmem_priv *priv = dev_priv->engine.instmem.priv;
509
510 priv->last_access_wr = write;
511}
512
513void
514nv50_instmem_finish_access(struct drm_device *dev)
515{
516 struct drm_nouveau_private *dev_priv = dev->dev_private;
517 struct nv50_instmem_priv *priv = dev_priv->engine.instmem.priv;
518
519 if (priv->last_access_wr) {
520 nv_wr32(dev, 0x070000, 0x00000001);
521 if (!nv_wait(0x070000, 0x00000001, 0x00000000))
522 NV_ERROR(dev, "PRAMIN flush timeout\n");
523 }
524}
525