blob: 611c83e6d9f466c4d23ff683ada472d13c30e8b9 [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
31#define NV04_RAMFC(c) (dev_priv->ramfc_offset + ((c) * NV04_RAMFC__SIZE))
32#define NV04_RAMFC__SIZE 32
33#define NV04_RAMFC_DMA_PUT 0x00
34#define NV04_RAMFC_DMA_GET 0x04
35#define NV04_RAMFC_DMA_INSTANCE 0x08
36#define NV04_RAMFC_DMA_STATE 0x0C
37#define NV04_RAMFC_DMA_FETCH 0x10
38#define NV04_RAMFC_ENGINE 0x14
39#define NV04_RAMFC_PULL1_ENGINE 0x18
40
41#define RAMFC_WR(offset, val) nv_wo32(dev, chan->ramfc->gpuobj, \
42 NV04_RAMFC_##offset/4, (val))
43#define RAMFC_RD(offset) nv_ro32(dev, chan->ramfc->gpuobj, \
44 NV04_RAMFC_##offset/4)
45
46void
47nv04_fifo_disable(struct drm_device *dev)
48{
49 uint32_t tmp;
50
51 tmp = nv_rd32(dev, NV04_PFIFO_CACHE1_DMA_PUSH);
52 nv_wr32(dev, NV04_PFIFO_CACHE1_DMA_PUSH, tmp & ~1);
53 nv_wr32(dev, NV03_PFIFO_CACHE1_PUSH0, 0);
54 tmp = nv_rd32(dev, NV03_PFIFO_CACHE1_PULL1);
55 nv_wr32(dev, NV04_PFIFO_CACHE1_PULL0, tmp & ~1);
56}
57
58void
59nv04_fifo_enable(struct drm_device *dev)
60{
61 nv_wr32(dev, NV03_PFIFO_CACHE1_PUSH0, 1);
62 nv_wr32(dev, NV04_PFIFO_CACHE1_PULL0, 1);
63}
64
65bool
66nv04_fifo_reassign(struct drm_device *dev, bool enable)
67{
68 uint32_t reassign = nv_rd32(dev, NV03_PFIFO_CACHES);
69
70 nv_wr32(dev, NV03_PFIFO_CACHES, enable ? 1 : 0);
71 return (reassign == 1);
72}
73
Francisco Jerez588d7d12009-12-13 20:07:42 +010074bool
75nv04_fifo_cache_flush(struct drm_device *dev)
76{
77 struct drm_nouveau_private *dev_priv = dev->dev_private;
78 struct nouveau_timer_engine *ptimer = &dev_priv->engine.timer;
79 uint64_t start = ptimer->read(dev);
80
81 do {
82 if (nv_rd32(dev, NV03_PFIFO_CACHE1_GET) ==
83 nv_rd32(dev, NV03_PFIFO_CACHE1_PUT))
84 return true;
85
86 } while (ptimer->read(dev) - start < 100000000);
87
88 NV_ERROR(dev, "Timeout flushing the PFIFO cache.\n");
89
90 return false;
91}
92
93bool
94nv04_fifo_cache_pull(struct drm_device *dev, bool enable)
95{
96 uint32_t pull = nv_rd32(dev, NV04_PFIFO_CACHE1_PULL0);
97
98 if (enable) {
99 nv_wr32(dev, NV04_PFIFO_CACHE1_PULL0, pull | 1);
100 } else {
101 nv_wr32(dev, NV04_PFIFO_CACHE1_PULL0, pull & ~1);
102 nv_wr32(dev, NV04_PFIFO_CACHE1_HASH, 0);
103 }
104
105 return !!(pull & 1);
106}
107
Ben Skeggs6ee73862009-12-11 19:24:15 +1000108int
109nv04_fifo_channel_id(struct drm_device *dev)
110{
111 return nv_rd32(dev, NV03_PFIFO_CACHE1_PUSH1) &
112 NV03_PFIFO_CACHE1_PUSH1_CHID_MASK;
113}
114
Francisco Jerez6e86e042010-07-03 18:36:39 +0200115#ifdef __BIG_ENDIAN
116#define DMA_FETCH_ENDIANNESS NV_PFIFO_CACHE1_BIG_ENDIAN
117#else
118#define DMA_FETCH_ENDIANNESS 0
119#endif
120
Ben Skeggs6ee73862009-12-11 19:24:15 +1000121int
122nv04_fifo_create_context(struct nouveau_channel *chan)
123{
124 struct drm_device *dev = chan->dev;
125 struct drm_nouveau_private *dev_priv = dev->dev_private;
Maarten Maathuisff9e5272010-02-01 20:58:27 +0100126 unsigned long flags;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000127 int ret;
128
129 ret = nouveau_gpuobj_new_fake(dev, NV04_RAMFC(chan->id), ~0,
130 NV04_RAMFC__SIZE,
131 NVOBJ_FLAG_ZERO_ALLOC |
132 NVOBJ_FLAG_ZERO_FREE,
133 NULL, &chan->ramfc);
134 if (ret)
135 return ret;
136
Maarten Maathuisff9e5272010-02-01 20:58:27 +0100137 spin_lock_irqsave(&dev_priv->context_switch_lock, flags);
138
Ben Skeggs6ee73862009-12-11 19:24:15 +1000139 /* Setup initial state */
140 dev_priv->engine.instmem.prepare_access(dev, true);
141 RAMFC_WR(DMA_PUT, chan->pushbuf_base);
142 RAMFC_WR(DMA_GET, chan->pushbuf_base);
143 RAMFC_WR(DMA_INSTANCE, chan->pushbuf->instance >> 4);
144 RAMFC_WR(DMA_FETCH, (NV_PFIFO_CACHE1_DMA_FETCH_TRIG_128_BYTES |
145 NV_PFIFO_CACHE1_DMA_FETCH_SIZE_128_BYTES |
146 NV_PFIFO_CACHE1_DMA_FETCH_MAX_REQS_8 |
Francisco Jerez6e86e042010-07-03 18:36:39 +0200147 DMA_FETCH_ENDIANNESS));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000148 dev_priv->engine.instmem.finish_access(dev);
149
150 /* enable the fifo dma operation */
151 nv_wr32(dev, NV04_PFIFO_MODE,
152 nv_rd32(dev, NV04_PFIFO_MODE) | (1 << chan->id));
Maarten Maathuisff9e5272010-02-01 20:58:27 +0100153
154 spin_unlock_irqrestore(&dev_priv->context_switch_lock, flags);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000155 return 0;
156}
157
158void
159nv04_fifo_destroy_context(struct nouveau_channel *chan)
160{
161 struct drm_device *dev = chan->dev;
162
163 nv_wr32(dev, NV04_PFIFO_MODE,
164 nv_rd32(dev, NV04_PFIFO_MODE) & ~(1 << chan->id));
165
166 nouveau_gpuobj_ref_del(dev, &chan->ramfc);
167}
168
169static void
170nv04_fifo_do_load_context(struct drm_device *dev, int chid)
171{
172 struct drm_nouveau_private *dev_priv = dev->dev_private;
173 uint32_t fc = NV04_RAMFC(chid), tmp;
174
175 dev_priv->engine.instmem.prepare_access(dev, false);
176
177 nv_wr32(dev, NV04_PFIFO_CACHE1_DMA_PUT, nv_ri32(dev, fc + 0));
178 nv_wr32(dev, NV04_PFIFO_CACHE1_DMA_GET, nv_ri32(dev, fc + 4));
179 tmp = nv_ri32(dev, fc + 8);
180 nv_wr32(dev, NV04_PFIFO_CACHE1_DMA_INSTANCE, tmp & 0xFFFF);
181 nv_wr32(dev, NV04_PFIFO_CACHE1_DMA_DCOUNT, tmp >> 16);
182 nv_wr32(dev, NV04_PFIFO_CACHE1_DMA_STATE, nv_ri32(dev, fc + 12));
183 nv_wr32(dev, NV04_PFIFO_CACHE1_DMA_FETCH, nv_ri32(dev, fc + 16));
184 nv_wr32(dev, NV04_PFIFO_CACHE1_ENGINE, nv_ri32(dev, fc + 20));
185 nv_wr32(dev, NV04_PFIFO_CACHE1_PULL1, nv_ri32(dev, fc + 24));
186
187 dev_priv->engine.instmem.finish_access(dev);
188
189 nv_wr32(dev, NV03_PFIFO_CACHE1_GET, 0);
190 nv_wr32(dev, NV03_PFIFO_CACHE1_PUT, 0);
191}
192
193int
194nv04_fifo_load_context(struct nouveau_channel *chan)
195{
196 uint32_t tmp;
197
198 nv_wr32(chan->dev, NV03_PFIFO_CACHE1_PUSH1,
199 NV03_PFIFO_CACHE1_PUSH1_DMA | chan->id);
200 nv04_fifo_do_load_context(chan->dev, chan->id);
201 nv_wr32(chan->dev, NV04_PFIFO_CACHE1_DMA_PUSH, 1);
202
203 /* Reset NV04_PFIFO_CACHE1_DMA_CTL_AT_INFO to INVALID */
204 tmp = nv_rd32(chan->dev, NV04_PFIFO_CACHE1_DMA_CTL) & ~(1 << 31);
205 nv_wr32(chan->dev, NV04_PFIFO_CACHE1_DMA_CTL, tmp);
206
207 return 0;
208}
209
210int
211nv04_fifo_unload_context(struct drm_device *dev)
212{
213 struct drm_nouveau_private *dev_priv = dev->dev_private;
214 struct nouveau_fifo_engine *pfifo = &dev_priv->engine.fifo;
215 struct nouveau_channel *chan = NULL;
216 uint32_t tmp;
217 int chid;
218
219 chid = pfifo->channel_id(dev);
220 if (chid < 0 || chid >= dev_priv->engine.fifo.channels)
221 return 0;
222
223 chan = dev_priv->fifos[chid];
224 if (!chan) {
225 NV_ERROR(dev, "Inactive channel on PFIFO: %d\n", chid);
226 return -EINVAL;
227 }
228
229 dev_priv->engine.instmem.prepare_access(dev, true);
230 RAMFC_WR(DMA_PUT, nv_rd32(dev, NV04_PFIFO_CACHE1_DMA_PUT));
231 RAMFC_WR(DMA_GET, nv_rd32(dev, NV04_PFIFO_CACHE1_DMA_GET));
232 tmp = nv_rd32(dev, NV04_PFIFO_CACHE1_DMA_DCOUNT) << 16;
233 tmp |= nv_rd32(dev, NV04_PFIFO_CACHE1_DMA_INSTANCE);
234 RAMFC_WR(DMA_INSTANCE, tmp);
235 RAMFC_WR(DMA_STATE, nv_rd32(dev, NV04_PFIFO_CACHE1_DMA_STATE));
236 RAMFC_WR(DMA_FETCH, nv_rd32(dev, NV04_PFIFO_CACHE1_DMA_FETCH));
237 RAMFC_WR(ENGINE, nv_rd32(dev, NV04_PFIFO_CACHE1_ENGINE));
238 RAMFC_WR(PULL1_ENGINE, nv_rd32(dev, NV04_PFIFO_CACHE1_PULL1));
239 dev_priv->engine.instmem.finish_access(dev);
240
241 nv04_fifo_do_load_context(dev, pfifo->channels - 1);
242 nv_wr32(dev, NV03_PFIFO_CACHE1_PUSH1, pfifo->channels - 1);
243 return 0;
244}
245
246static void
247nv04_fifo_init_reset(struct drm_device *dev)
248{
249 nv_wr32(dev, NV03_PMC_ENABLE,
250 nv_rd32(dev, NV03_PMC_ENABLE) & ~NV_PMC_ENABLE_PFIFO);
251 nv_wr32(dev, NV03_PMC_ENABLE,
252 nv_rd32(dev, NV03_PMC_ENABLE) | NV_PMC_ENABLE_PFIFO);
253
254 nv_wr32(dev, 0x003224, 0x000f0078);
255 nv_wr32(dev, 0x002044, 0x0101ffff);
256 nv_wr32(dev, 0x002040, 0x000000ff);
257 nv_wr32(dev, 0x002500, 0x00000000);
258 nv_wr32(dev, 0x003000, 0x00000000);
259 nv_wr32(dev, 0x003050, 0x00000000);
260 nv_wr32(dev, 0x003200, 0x00000000);
261 nv_wr32(dev, 0x003250, 0x00000000);
262 nv_wr32(dev, 0x003220, 0x00000000);
263
264 nv_wr32(dev, 0x003250, 0x00000000);
265 nv_wr32(dev, 0x003270, 0x00000000);
266 nv_wr32(dev, 0x003210, 0x00000000);
267}
268
269static void
270nv04_fifo_init_ramxx(struct drm_device *dev)
271{
272 struct drm_nouveau_private *dev_priv = dev->dev_private;
273
274 nv_wr32(dev, NV03_PFIFO_RAMHT, (0x03 << 24) /* search 128 */ |
275 ((dev_priv->ramht_bits - 9) << 16) |
276 (dev_priv->ramht_offset >> 8));
277 nv_wr32(dev, NV03_PFIFO_RAMRO, dev_priv->ramro_offset>>8);
278 nv_wr32(dev, NV03_PFIFO_RAMFC, dev_priv->ramfc_offset >> 8);
279}
280
281static void
282nv04_fifo_init_intr(struct drm_device *dev)
283{
284 nv_wr32(dev, 0x002100, 0xffffffff);
285 nv_wr32(dev, 0x002140, 0xffffffff);
286}
287
288int
289nv04_fifo_init(struct drm_device *dev)
290{
291 struct drm_nouveau_private *dev_priv = dev->dev_private;
292 struct nouveau_fifo_engine *pfifo = &dev_priv->engine.fifo;
293 int i;
294
295 nv04_fifo_init_reset(dev);
296 nv04_fifo_init_ramxx(dev);
297
298 nv04_fifo_do_load_context(dev, pfifo->channels - 1);
299 nv_wr32(dev, NV03_PFIFO_CACHE1_PUSH1, pfifo->channels - 1);
300
301 nv04_fifo_init_intr(dev);
302 pfifo->enable(dev);
303
304 for (i = 0; i < dev_priv->engine.fifo.channels; i++) {
305 if (dev_priv->fifos[i]) {
306 uint32_t mode = nv_rd32(dev, NV04_PFIFO_MODE);
307 nv_wr32(dev, NV04_PFIFO_MODE, mode | (1 << i));
308 }
309 }
310
311 return 0;
312}
313