blob: 177d8229336f1d59806411ddd02a9ca86540d6ec [file] [log] [blame]
Ben Skeggs6ee73862009-12-11 19:24:15 +10001/*
2 * Copyright (C) 2007 Ben Skeggs.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 */
26
27#include "drmP.h"
28#include "drm.h"
29#include "nouveau_drv.h"
30
31MODULE_FIRMWARE("nouveau/nv50.ctxprog");
32MODULE_FIRMWARE("nouveau/nv50.ctxvals");
33MODULE_FIRMWARE("nouveau/nv84.ctxprog");
34MODULE_FIRMWARE("nouveau/nv84.ctxvals");
35MODULE_FIRMWARE("nouveau/nv86.ctxprog");
36MODULE_FIRMWARE("nouveau/nv86.ctxvals");
37MODULE_FIRMWARE("nouveau/nv92.ctxprog");
38MODULE_FIRMWARE("nouveau/nv92.ctxvals");
39MODULE_FIRMWARE("nouveau/nv94.ctxprog");
40MODULE_FIRMWARE("nouveau/nv94.ctxvals");
41MODULE_FIRMWARE("nouveau/nv96.ctxprog");
42MODULE_FIRMWARE("nouveau/nv96.ctxvals");
43MODULE_FIRMWARE("nouveau/nv98.ctxprog");
44MODULE_FIRMWARE("nouveau/nv98.ctxvals");
45MODULE_FIRMWARE("nouveau/nva0.ctxprog");
46MODULE_FIRMWARE("nouveau/nva0.ctxvals");
47MODULE_FIRMWARE("nouveau/nva5.ctxprog");
48MODULE_FIRMWARE("nouveau/nva5.ctxvals");
49MODULE_FIRMWARE("nouveau/nva8.ctxprog");
50MODULE_FIRMWARE("nouveau/nva8.ctxvals");
51MODULE_FIRMWARE("nouveau/nvaa.ctxprog");
52MODULE_FIRMWARE("nouveau/nvaa.ctxvals");
53MODULE_FIRMWARE("nouveau/nvac.ctxprog");
54MODULE_FIRMWARE("nouveau/nvac.ctxvals");
55
56#define IS_G80 ((dev_priv->chipset & 0xf0) == 0x50)
57
58static void
59nv50_graph_init_reset(struct drm_device *dev)
60{
61 uint32_t pmc_e = NV_PMC_ENABLE_PGRAPH | (1 << 21);
62
63 NV_DEBUG(dev, "\n");
64
65 nv_wr32(dev, NV03_PMC_ENABLE, nv_rd32(dev, NV03_PMC_ENABLE) & ~pmc_e);
66 nv_wr32(dev, NV03_PMC_ENABLE, nv_rd32(dev, NV03_PMC_ENABLE) | pmc_e);
67}
68
69static void
70nv50_graph_init_intr(struct drm_device *dev)
71{
72 NV_DEBUG(dev, "\n");
73
74 nv_wr32(dev, NV03_PGRAPH_INTR, 0xffffffff);
75 nv_wr32(dev, 0x400138, 0xffffffff);
76 nv_wr32(dev, NV40_PGRAPH_INTR_EN, 0xffffffff);
77}
78
79static void
80nv50_graph_init_regs__nv(struct drm_device *dev)
81{
82 NV_DEBUG(dev, "\n");
83
84 nv_wr32(dev, 0x400804, 0xc0000000);
85 nv_wr32(dev, 0x406800, 0xc0000000);
86 nv_wr32(dev, 0x400c04, 0xc0000000);
87 nv_wr32(dev, 0x401804, 0xc0000000);
88 nv_wr32(dev, 0x405018, 0xc0000000);
89 nv_wr32(dev, 0x402000, 0xc0000000);
90
91 nv_wr32(dev, 0x400108, 0xffffffff);
92
93 nv_wr32(dev, 0x400824, 0x00004000);
94 nv_wr32(dev, 0x400500, 0x00010001);
95}
96
97static void
98nv50_graph_init_regs(struct drm_device *dev)
99{
100 NV_DEBUG(dev, "\n");
101
102 nv_wr32(dev, NV04_PGRAPH_DEBUG_3,
103 (1 << 2) /* HW_CONTEXT_SWITCH_ENABLED */);
104 nv_wr32(dev, 0x402ca8, 0x800);
105}
106
107static int
108nv50_graph_init_ctxctl(struct drm_device *dev)
109{
110 NV_DEBUG(dev, "\n");
111
112 nv40_grctx_init(dev);
113
114 nv_wr32(dev, 0x400320, 4);
115 nv_wr32(dev, NV40_PGRAPH_CTXCTL_CUR, 0);
116 nv_wr32(dev, NV20_PGRAPH_CHANNEL_CTX_POINTER, 0);
117 return 0;
118}
119
120int
121nv50_graph_init(struct drm_device *dev)
122{
123 int ret;
124
125 NV_DEBUG(dev, "\n");
126
127 nv50_graph_init_reset(dev);
128 nv50_graph_init_regs__nv(dev);
129 nv50_graph_init_regs(dev);
130 nv50_graph_init_intr(dev);
131
132 ret = nv50_graph_init_ctxctl(dev);
133 if (ret)
134 return ret;
135
136 return 0;
137}
138
139void
140nv50_graph_takedown(struct drm_device *dev)
141{
142 NV_DEBUG(dev, "\n");
143 nv40_grctx_fini(dev);
144}
145
146void
147nv50_graph_fifo_access(struct drm_device *dev, bool enabled)
148{
149 const uint32_t mask = 0x00010001;
150
151 if (enabled)
152 nv_wr32(dev, 0x400500, nv_rd32(dev, 0x400500) | mask);
153 else
154 nv_wr32(dev, 0x400500, nv_rd32(dev, 0x400500) & ~mask);
155}
156
157struct nouveau_channel *
158nv50_graph_channel(struct drm_device *dev)
159{
160 struct drm_nouveau_private *dev_priv = dev->dev_private;
161 uint32_t inst;
162 int i;
163
164 inst = nv_rd32(dev, NV50_PGRAPH_CTXCTL_CUR);
165 if (!(inst & NV50_PGRAPH_CTXCTL_CUR_LOADED))
166 return NULL;
167 inst = (inst & NV50_PGRAPH_CTXCTL_CUR_INSTANCE) << 12;
168
169 for (i = 0; i < dev_priv->engine.fifo.channels; i++) {
170 struct nouveau_channel *chan = dev_priv->fifos[i];
171
172 if (chan && chan->ramin && chan->ramin->instance == inst)
173 return chan;
174 }
175
176 return NULL;
177}
178
179int
180nv50_graph_create_context(struct nouveau_channel *chan)
181{
182 struct drm_device *dev = chan->dev;
183 struct drm_nouveau_private *dev_priv = dev->dev_private;
184 struct nouveau_gpuobj *ramin = chan->ramin->gpuobj;
185 struct nouveau_gpuobj *ctx;
186 uint32_t grctx_size = 0x70000;
187 int hdr, ret;
188
189 NV_DEBUG(dev, "ch%d\n", chan->id);
190
191 ret = nouveau_gpuobj_new_ref(dev, chan, NULL, 0, grctx_size, 0x1000,
192 NVOBJ_FLAG_ZERO_ALLOC |
193 NVOBJ_FLAG_ZERO_FREE, &chan->ramin_grctx);
194 if (ret)
195 return ret;
196 ctx = chan->ramin_grctx->gpuobj;
197
198 hdr = IS_G80 ? 0x200 : 0x20;
199 dev_priv->engine.instmem.prepare_access(dev, true);
200 nv_wo32(dev, ramin, (hdr + 0x00)/4, 0x00190002);
201 nv_wo32(dev, ramin, (hdr + 0x04)/4, chan->ramin_grctx->instance +
202 grctx_size - 1);
203 nv_wo32(dev, ramin, (hdr + 0x08)/4, chan->ramin_grctx->instance);
204 nv_wo32(dev, ramin, (hdr + 0x0c)/4, 0);
205 nv_wo32(dev, ramin, (hdr + 0x10)/4, 0);
206 nv_wo32(dev, ramin, (hdr + 0x14)/4, 0x00010000);
207 dev_priv->engine.instmem.finish_access(dev);
208
209 dev_priv->engine.instmem.prepare_access(dev, true);
210 nv40_grctx_vals_load(dev, ctx);
211 nv_wo32(dev, ctx, 0x00000/4, chan->ramin->instance >> 12);
212 if ((dev_priv->chipset & 0xf0) == 0xa0)
213 nv_wo32(dev, ctx, 0x00004/4, 0x00000000);
214 else
215 nv_wo32(dev, ctx, 0x0011c/4, 0x00000000);
216 dev_priv->engine.instmem.finish_access(dev);
217
218 return 0;
219}
220
221void
222nv50_graph_destroy_context(struct nouveau_channel *chan)
223{
224 struct drm_device *dev = chan->dev;
225 struct drm_nouveau_private *dev_priv = dev->dev_private;
226 int i, hdr = IS_G80 ? 0x200 : 0x20;
227
228 NV_DEBUG(dev, "ch%d\n", chan->id);
229
230 if (!chan->ramin || !chan->ramin->gpuobj)
231 return;
232
233 dev_priv->engine.instmem.prepare_access(dev, true);
234 for (i = hdr; i < hdr + 24; i += 4)
235 nv_wo32(dev, chan->ramin->gpuobj, i/4, 0);
236 dev_priv->engine.instmem.finish_access(dev);
237
238 nouveau_gpuobj_ref_del(dev, &chan->ramin_grctx);
239}
240
241static int
242nv50_graph_do_load_context(struct drm_device *dev, uint32_t inst)
243{
244 uint32_t fifo = nv_rd32(dev, 0x400500);
245
246 nv_wr32(dev, 0x400500, fifo & ~1);
247 nv_wr32(dev, 0x400784, inst);
248 nv_wr32(dev, 0x400824, nv_rd32(dev, 0x400824) | 0x40);
249 nv_wr32(dev, 0x400320, nv_rd32(dev, 0x400320) | 0x11);
250 nv_wr32(dev, 0x400040, 0xffffffff);
251 (void)nv_rd32(dev, 0x400040);
252 nv_wr32(dev, 0x400040, 0x00000000);
253 nv_wr32(dev, 0x400304, nv_rd32(dev, 0x400304) | 1);
254
255 if (nouveau_wait_for_idle(dev))
256 nv_wr32(dev, 0x40032c, inst | (1<<31));
257 nv_wr32(dev, 0x400500, fifo);
258
259 return 0;
260}
261
262int
263nv50_graph_load_context(struct nouveau_channel *chan)
264{
265 uint32_t inst = chan->ramin->instance >> 12;
266
267 NV_DEBUG(chan->dev, "ch%d\n", chan->id);
268 return nv50_graph_do_load_context(chan->dev, inst);
269}
270
271int
272nv50_graph_unload_context(struct drm_device *dev)
273{
274 uint32_t inst, fifo = nv_rd32(dev, 0x400500);
275
276 inst = nv_rd32(dev, NV50_PGRAPH_CTXCTL_CUR);
277 if (!(inst & NV50_PGRAPH_CTXCTL_CUR_LOADED))
278 return 0;
279 inst &= NV50_PGRAPH_CTXCTL_CUR_INSTANCE;
280
281 nv_wr32(dev, 0x400500, fifo & ~1);
282 nv_wr32(dev, 0x400784, inst);
283 nv_wr32(dev, 0x400824, nv_rd32(dev, 0x400824) | 0x20);
284 nv_wr32(dev, 0x400304, nv_rd32(dev, 0x400304) | 0x01);
285 nouveau_wait_for_idle(dev);
286 nv_wr32(dev, 0x400500, fifo);
287
288 nv_wr32(dev, NV50_PGRAPH_CTXCTL_CUR, inst);
289 return 0;
290}
291
292void
293nv50_graph_context_switch(struct drm_device *dev)
294{
295 uint32_t inst;
296
297 nv50_graph_unload_context(dev);
298
299 inst = nv_rd32(dev, NV50_PGRAPH_CTXCTL_NEXT);
300 inst &= NV50_PGRAPH_CTXCTL_NEXT_INSTANCE;
301 nv50_graph_do_load_context(dev, inst);
302
303 nv_wr32(dev, NV40_PGRAPH_INTR_EN, nv_rd32(dev,
304 NV40_PGRAPH_INTR_EN) | NV_PGRAPH_INTR_CONTEXT_SWITCH);
305}
306
307static int
308nv50_graph_nvsw_dma_vblsem(struct nouveau_channel *chan, int grclass,
309 int mthd, uint32_t data)
310{
311 struct nouveau_gpuobj_ref *ref = NULL;
312
313 if (nouveau_gpuobj_ref_find(chan, data, &ref))
314 return -ENOENT;
315
316 if (nouveau_notifier_offset(ref->gpuobj, NULL))
317 return -EINVAL;
318
319 chan->nvsw.vblsem = ref->gpuobj;
320 chan->nvsw.vblsem_offset = ~0;
321 return 0;
322}
323
324static int
325nv50_graph_nvsw_vblsem_offset(struct nouveau_channel *chan, int grclass,
326 int mthd, uint32_t data)
327{
328 if (nouveau_notifier_offset(chan->nvsw.vblsem, &data))
329 return -ERANGE;
330
331 chan->nvsw.vblsem_offset = data >> 2;
332 return 0;
333}
334
335static int
336nv50_graph_nvsw_vblsem_release_val(struct nouveau_channel *chan, int grclass,
337 int mthd, uint32_t data)
338{
339 chan->nvsw.vblsem_rval = data;
340 return 0;
341}
342
343static int
344nv50_graph_nvsw_vblsem_release(struct nouveau_channel *chan, int grclass,
345 int mthd, uint32_t data)
346{
347 struct drm_device *dev = chan->dev;
348 struct drm_nouveau_private *dev_priv = dev->dev_private;
349
350 if (!chan->nvsw.vblsem || chan->nvsw.vblsem_offset == ~0 || data > 1)
351 return -EINVAL;
352
353 if (!(nv_rd32(dev, NV50_PDISPLAY_INTR_EN) &
354 NV50_PDISPLAY_INTR_EN_VBLANK_CRTC_(data))) {
355 nv_wr32(dev, NV50_PDISPLAY_INTR_1,
356 NV50_PDISPLAY_INTR_1_VBLANK_CRTC_(data));
357 nv_wr32(dev, NV50_PDISPLAY_INTR_EN, nv_rd32(dev,
358 NV50_PDISPLAY_INTR_EN) |
359 NV50_PDISPLAY_INTR_EN_VBLANK_CRTC_(data));
360 }
361
362 list_add(&chan->nvsw.vbl_wait, &dev_priv->vbl_waiting);
363 return 0;
364}
365
366static struct nouveau_pgraph_object_method nv50_graph_nvsw_methods[] = {
367 { 0x018c, nv50_graph_nvsw_dma_vblsem },
368 { 0x0400, nv50_graph_nvsw_vblsem_offset },
369 { 0x0404, nv50_graph_nvsw_vblsem_release_val },
370 { 0x0408, nv50_graph_nvsw_vblsem_release },
371 {}
372};
373
374struct nouveau_pgraph_object_class nv50_graph_grclass[] = {
375 { 0x506e, true, nv50_graph_nvsw_methods }, /* nvsw */
376 { 0x0030, false, NULL }, /* null */
377 { 0x5039, false, NULL }, /* m2mf */
378 { 0x502d, false, NULL }, /* 2d */
379 { 0x50c0, false, NULL }, /* compute */
380 { 0x5097, false, NULL }, /* tesla (nv50) */
381 { 0x8297, false, NULL }, /* tesla (nv80/nv90) */
382 { 0x8397, false, NULL }, /* tesla (nva0) */
383 { 0x8597, false, NULL }, /* tesla (nva8) */
384 {}
385};