2 * Copyright (C) 2007 Ben Skeggs.
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:
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.
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.
29 #include "nouveau_drv.h"
30 #include "nouveau_ramht.h"
31 #include "nouveau_util.h"
33 #define NV04_RAMFC(c) (dev_priv->ramfc->pinst + ((c) * NV04_RAMFC__SIZE))
34 #define NV04_RAMFC__SIZE 32
35 #define NV04_RAMFC_DMA_PUT 0x00
36 #define NV04_RAMFC_DMA_GET 0x04
37 #define NV04_RAMFC_DMA_INSTANCE 0x08
38 #define NV04_RAMFC_DMA_STATE 0x0C
39 #define NV04_RAMFC_DMA_FETCH 0x10
40 #define NV04_RAMFC_ENGINE 0x14
41 #define NV04_RAMFC_PULL1_ENGINE 0x18
43 #define RAMFC_WR(offset, val) nv_wo32(chan->ramfc, NV04_RAMFC_##offset, (val))
44 #define RAMFC_RD(offset) nv_ro32(chan->ramfc, NV04_RAMFC_##offset)
47 nv04_fifo_disable(struct drm_device *dev)
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);
59 nv04_fifo_enable(struct drm_device *dev)
61 nv_wr32(dev, NV03_PFIFO_CACHE1_PUSH0, 1);
62 nv_wr32(dev, NV04_PFIFO_CACHE1_PULL0, 1);
66 nv04_fifo_reassign(struct drm_device *dev, bool enable)
68 uint32_t reassign = nv_rd32(dev, NV03_PFIFO_CACHES);
70 nv_wr32(dev, NV03_PFIFO_CACHES, enable ? 1 : 0);
71 return (reassign == 1);
75 nv04_fifo_cache_pull(struct drm_device *dev, bool enable)
77 int pull = nv_mask(dev, NV04_PFIFO_CACHE1_PULL0, 1, enable);
80 /* In some cases the PFIFO puller may be left in an
81 * inconsistent state if you try to stop it when it's
82 * busy translating handles. Sometimes you get a
83 * PFIFO_CACHE_ERROR, sometimes it just fails silently
84 * sending incorrect instance offsets to PGRAPH after
85 * it's started up again. To avoid the latter we
86 * invalidate the most recently calculated instance.
88 if (!nv_wait(dev, NV04_PFIFO_CACHE1_PULL0,
89 NV04_PFIFO_CACHE1_PULL0_HASH_BUSY, 0))
90 NV_ERROR(dev, "Timeout idling the PFIFO puller.\n");
92 if (nv_rd32(dev, NV04_PFIFO_CACHE1_PULL0) &
93 NV04_PFIFO_CACHE1_PULL0_HASH_FAILED)
94 nv_wr32(dev, NV03_PFIFO_INTR_0,
95 NV_PFIFO_INTR_CACHE_ERROR);
97 nv_wr32(dev, NV04_PFIFO_CACHE1_HASH, 0);
104 nv04_fifo_channel_id(struct drm_device *dev)
106 return nv_rd32(dev, NV03_PFIFO_CACHE1_PUSH1) &
107 NV03_PFIFO_CACHE1_PUSH1_CHID_MASK;
111 #define DMA_FETCH_ENDIANNESS NV_PFIFO_CACHE1_BIG_ENDIAN
113 #define DMA_FETCH_ENDIANNESS 0
117 nv04_fifo_create_context(struct nouveau_channel *chan)
119 struct drm_device *dev = chan->dev;
120 struct drm_nouveau_private *dev_priv = dev->dev_private;
124 ret = nouveau_gpuobj_new_fake(dev, NV04_RAMFC(chan->id), ~0,
126 NVOBJ_FLAG_ZERO_ALLOC |
127 NVOBJ_FLAG_ZERO_FREE,
132 spin_lock_irqsave(&dev_priv->context_switch_lock, flags);
134 /* Setup initial state */
135 RAMFC_WR(DMA_PUT, chan->pushbuf_base);
136 RAMFC_WR(DMA_GET, chan->pushbuf_base);
137 RAMFC_WR(DMA_INSTANCE, chan->pushbuf->pinst >> 4);
138 RAMFC_WR(DMA_FETCH, (NV_PFIFO_CACHE1_DMA_FETCH_TRIG_128_BYTES |
139 NV_PFIFO_CACHE1_DMA_FETCH_SIZE_128_BYTES |
140 NV_PFIFO_CACHE1_DMA_FETCH_MAX_REQS_8 |
141 DMA_FETCH_ENDIANNESS));
143 /* enable the fifo dma operation */
144 nv_wr32(dev, NV04_PFIFO_MODE,
145 nv_rd32(dev, NV04_PFIFO_MODE) | (1 << chan->id));
147 spin_unlock_irqrestore(&dev_priv->context_switch_lock, flags);
152 nv04_fifo_destroy_context(struct nouveau_channel *chan)
154 struct drm_device *dev = chan->dev;
155 struct drm_nouveau_private *dev_priv = dev->dev_private;
156 struct nouveau_fifo_engine *pfifo = &dev_priv->engine.fifo;
159 spin_lock_irqsave(&dev_priv->context_switch_lock, flags);
160 pfifo->reassign(dev, false);
162 /* Unload the context if it's the currently active one */
163 if (pfifo->channel_id(dev) == chan->id) {
165 pfifo->unload_context(dev);
169 /* Keep it from being rescheduled */
170 nv_mask(dev, NV04_PFIFO_MODE, 1 << chan->id, 0);
172 pfifo->reassign(dev, true);
173 spin_unlock_irqrestore(&dev_priv->context_switch_lock, flags);
175 /* Free the channel resources */
176 nouveau_gpuobj_ref(NULL, &chan->ramfc);
180 nv04_fifo_do_load_context(struct drm_device *dev, int chid)
182 struct drm_nouveau_private *dev_priv = dev->dev_private;
183 uint32_t fc = NV04_RAMFC(chid), tmp;
185 nv_wr32(dev, NV04_PFIFO_CACHE1_DMA_PUT, nv_ri32(dev, fc + 0));
186 nv_wr32(dev, NV04_PFIFO_CACHE1_DMA_GET, nv_ri32(dev, fc + 4));
187 tmp = nv_ri32(dev, fc + 8);
188 nv_wr32(dev, NV04_PFIFO_CACHE1_DMA_INSTANCE, tmp & 0xFFFF);
189 nv_wr32(dev, NV04_PFIFO_CACHE1_DMA_DCOUNT, tmp >> 16);
190 nv_wr32(dev, NV04_PFIFO_CACHE1_DMA_STATE, nv_ri32(dev, fc + 12));
191 nv_wr32(dev, NV04_PFIFO_CACHE1_DMA_FETCH, nv_ri32(dev, fc + 16));
192 nv_wr32(dev, NV04_PFIFO_CACHE1_ENGINE, nv_ri32(dev, fc + 20));
193 nv_wr32(dev, NV04_PFIFO_CACHE1_PULL1, nv_ri32(dev, fc + 24));
195 nv_wr32(dev, NV03_PFIFO_CACHE1_GET, 0);
196 nv_wr32(dev, NV03_PFIFO_CACHE1_PUT, 0);
200 nv04_fifo_load_context(struct nouveau_channel *chan)
204 nv_wr32(chan->dev, NV03_PFIFO_CACHE1_PUSH1,
205 NV03_PFIFO_CACHE1_PUSH1_DMA | chan->id);
206 nv04_fifo_do_load_context(chan->dev, chan->id);
207 nv_wr32(chan->dev, NV04_PFIFO_CACHE1_DMA_PUSH, 1);
209 /* Reset NV04_PFIFO_CACHE1_DMA_CTL_AT_INFO to INVALID */
210 tmp = nv_rd32(chan->dev, NV04_PFIFO_CACHE1_DMA_CTL) & ~(1 << 31);
211 nv_wr32(chan->dev, NV04_PFIFO_CACHE1_DMA_CTL, tmp);
217 nv04_fifo_unload_context(struct drm_device *dev)
219 struct drm_nouveau_private *dev_priv = dev->dev_private;
220 struct nouveau_fifo_engine *pfifo = &dev_priv->engine.fifo;
221 struct nouveau_channel *chan = NULL;
225 chid = pfifo->channel_id(dev);
226 if (chid < 0 || chid >= dev_priv->engine.fifo.channels)
229 chan = dev_priv->channels.ptr[chid];
231 NV_ERROR(dev, "Inactive channel on PFIFO: %d\n", chid);
235 RAMFC_WR(DMA_PUT, nv_rd32(dev, NV04_PFIFO_CACHE1_DMA_PUT));
236 RAMFC_WR(DMA_GET, nv_rd32(dev, NV04_PFIFO_CACHE1_DMA_GET));
237 tmp = nv_rd32(dev, NV04_PFIFO_CACHE1_DMA_DCOUNT) << 16;
238 tmp |= nv_rd32(dev, NV04_PFIFO_CACHE1_DMA_INSTANCE);
239 RAMFC_WR(DMA_INSTANCE, tmp);
240 RAMFC_WR(DMA_STATE, nv_rd32(dev, NV04_PFIFO_CACHE1_DMA_STATE));
241 RAMFC_WR(DMA_FETCH, nv_rd32(dev, NV04_PFIFO_CACHE1_DMA_FETCH));
242 RAMFC_WR(ENGINE, nv_rd32(dev, NV04_PFIFO_CACHE1_ENGINE));
243 RAMFC_WR(PULL1_ENGINE, nv_rd32(dev, NV04_PFIFO_CACHE1_PULL1));
245 nv04_fifo_do_load_context(dev, pfifo->channels - 1);
246 nv_wr32(dev, NV03_PFIFO_CACHE1_PUSH1, pfifo->channels - 1);
251 nv04_fifo_init_reset(struct drm_device *dev)
253 nv_wr32(dev, NV03_PMC_ENABLE,
254 nv_rd32(dev, NV03_PMC_ENABLE) & ~NV_PMC_ENABLE_PFIFO);
255 nv_wr32(dev, NV03_PMC_ENABLE,
256 nv_rd32(dev, NV03_PMC_ENABLE) | NV_PMC_ENABLE_PFIFO);
258 nv_wr32(dev, 0x003224, 0x000f0078);
259 nv_wr32(dev, 0x002044, 0x0101ffff);
260 nv_wr32(dev, 0x002040, 0x000000ff);
261 nv_wr32(dev, 0x002500, 0x00000000);
262 nv_wr32(dev, 0x003000, 0x00000000);
263 nv_wr32(dev, 0x003050, 0x00000000);
264 nv_wr32(dev, 0x003200, 0x00000000);
265 nv_wr32(dev, 0x003250, 0x00000000);
266 nv_wr32(dev, 0x003220, 0x00000000);
268 nv_wr32(dev, 0x003250, 0x00000000);
269 nv_wr32(dev, 0x003270, 0x00000000);
270 nv_wr32(dev, 0x003210, 0x00000000);
274 nv04_fifo_init_ramxx(struct drm_device *dev)
276 struct drm_nouveau_private *dev_priv = dev->dev_private;
278 nv_wr32(dev, NV03_PFIFO_RAMHT, (0x03 << 24) /* search 128 */ |
279 ((dev_priv->ramht->bits - 9) << 16) |
280 (dev_priv->ramht->gpuobj->pinst >> 8));
281 nv_wr32(dev, NV03_PFIFO_RAMRO, dev_priv->ramro->pinst >> 8);
282 nv_wr32(dev, NV03_PFIFO_RAMFC, dev_priv->ramfc->pinst >> 8);
286 nv04_fifo_init_intr(struct drm_device *dev)
288 nouveau_irq_register(dev, 8, nv04_fifo_isr);
289 nv_wr32(dev, 0x002100, 0xffffffff);
290 nv_wr32(dev, 0x002140, 0xffffffff);
294 nv04_fifo_init(struct drm_device *dev)
296 struct drm_nouveau_private *dev_priv = dev->dev_private;
297 struct nouveau_fifo_engine *pfifo = &dev_priv->engine.fifo;
300 nv04_fifo_init_reset(dev);
301 nv04_fifo_init_ramxx(dev);
303 nv04_fifo_do_load_context(dev, pfifo->channels - 1);
304 nv_wr32(dev, NV03_PFIFO_CACHE1_PUSH1, pfifo->channels - 1);
306 nv04_fifo_init_intr(dev);
308 pfifo->reassign(dev, true);
310 for (i = 0; i < dev_priv->engine.fifo.channels; i++) {
311 if (dev_priv->channels.ptr[i]) {
312 uint32_t mode = nv_rd32(dev, NV04_PFIFO_MODE);
313 nv_wr32(dev, NV04_PFIFO_MODE, mode | (1 << i));
321 nv04_fifo_fini(struct drm_device *dev)
323 nv_wr32(dev, 0x2140, 0x00000000);
324 nouveau_irq_unregister(dev, 8);
328 nouveau_fifo_swmthd(struct drm_device *dev, u32 chid, u32 addr, u32 data)
330 struct drm_nouveau_private *dev_priv = dev->dev_private;
331 struct nouveau_channel *chan = NULL;
332 struct nouveau_gpuobj *obj;
334 const int subc = (addr >> 13) & 0x7;
335 const int mthd = addr & 0x1ffc;
336 bool handled = false;
339 spin_lock_irqsave(&dev_priv->channels.lock, flags);
340 if (likely(chid >= 0 && chid < dev_priv->engine.fifo.channels))
341 chan = dev_priv->channels.ptr[chid];
346 case 0x0000: /* bind object to subchannel */
347 obj = nouveau_ramht_find(chan, data);
348 if (unlikely(!obj || obj->engine != NVOBJ_ENGINE_SW))
351 chan->sw_subchannel[subc] = obj->class;
352 engine = 0x0000000f << (subc * 4);
354 nv_mask(dev, NV04_PFIFO_CACHE1_ENGINE, engine, 0x00000000);
358 engine = nv_rd32(dev, NV04_PFIFO_CACHE1_ENGINE);
359 if (unlikely(((engine >> (subc * 4)) & 0xf) != 0))
362 if (!nouveau_gpuobj_mthd_call(chan, chan->sw_subchannel[subc],
369 spin_unlock_irqrestore(&dev_priv->channels.lock, flags);
374 nv04_fifo_isr(struct drm_device *dev)
376 struct drm_nouveau_private *dev_priv = dev->dev_private;
377 struct nouveau_engine *engine = &dev_priv->engine;
378 uint32_t status, reassign;
381 reassign = nv_rd32(dev, NV03_PFIFO_CACHES) & 1;
382 while ((status = nv_rd32(dev, NV03_PFIFO_INTR_0)) && (cnt++ < 100)) {
385 nv_wr32(dev, NV03_PFIFO_CACHES, 0);
387 chid = engine->fifo.channel_id(dev);
388 get = nv_rd32(dev, NV03_PFIFO_CACHE1_GET);
390 if (status & NV_PFIFO_INTR_CACHE_ERROR) {
394 /* NV_PFIFO_CACHE1_GET actually goes to 0xffc before
395 * wrapping on my G80 chips, but CACHE1 isn't big
396 * enough for this much data.. Tests show that it
397 * wraps around to the start at GET=0x800.. No clue
400 ptr = (get & 0x7ff) >> 2;
402 if (dev_priv->card_type < NV_40) {
404 NV04_PFIFO_CACHE1_METHOD(ptr));
406 NV04_PFIFO_CACHE1_DATA(ptr));
409 NV40_PFIFO_CACHE1_METHOD(ptr));
411 NV40_PFIFO_CACHE1_DATA(ptr));
414 if (!nouveau_fifo_swmthd(dev, chid, mthd, data)) {
415 NV_INFO(dev, "PFIFO_CACHE_ERROR - Ch %d/%d "
416 "Mthd 0x%04x Data 0x%08x\n",
417 chid, (mthd >> 13) & 7, mthd & 0x1ffc,
421 nv_wr32(dev, NV04_PFIFO_CACHE1_DMA_PUSH, 0);
422 nv_wr32(dev, NV03_PFIFO_INTR_0,
423 NV_PFIFO_INTR_CACHE_ERROR);
425 nv_wr32(dev, NV03_PFIFO_CACHE1_PUSH0,
426 nv_rd32(dev, NV03_PFIFO_CACHE1_PUSH0) & ~1);
427 nv_wr32(dev, NV03_PFIFO_CACHE1_GET, get + 4);
428 nv_wr32(dev, NV03_PFIFO_CACHE1_PUSH0,
429 nv_rd32(dev, NV03_PFIFO_CACHE1_PUSH0) | 1);
430 nv_wr32(dev, NV04_PFIFO_CACHE1_HASH, 0);
432 nv_wr32(dev, NV04_PFIFO_CACHE1_DMA_PUSH,
433 nv_rd32(dev, NV04_PFIFO_CACHE1_DMA_PUSH) | 1);
434 nv_wr32(dev, NV04_PFIFO_CACHE1_PULL0, 1);
436 status &= ~NV_PFIFO_INTR_CACHE_ERROR;
439 if (status & NV_PFIFO_INTR_DMA_PUSHER) {
440 u32 dma_get = nv_rd32(dev, 0x003244);
441 u32 dma_put = nv_rd32(dev, 0x003240);
442 u32 push = nv_rd32(dev, 0x003220);
443 u32 state = nv_rd32(dev, 0x003228);
445 if (dev_priv->card_type == NV_50) {
446 u32 ho_get = nv_rd32(dev, 0x003328);
447 u32 ho_put = nv_rd32(dev, 0x003320);
448 u32 ib_get = nv_rd32(dev, 0x003334);
449 u32 ib_put = nv_rd32(dev, 0x003330);
451 if (nouveau_ratelimit())
452 NV_INFO(dev, "PFIFO_DMA_PUSHER - Ch %d Get 0x%02x%08x "
453 "Put 0x%02x%08x IbGet 0x%08x IbPut 0x%08x "
454 "State 0x%08x Push 0x%08x\n",
455 chid, ho_get, dma_get, ho_put,
456 dma_put, ib_get, ib_put, state,
459 /* METHOD_COUNT, in DMA_STATE on earlier chipsets */
460 nv_wr32(dev, 0x003364, 0x00000000);
461 if (dma_get != dma_put || ho_get != ho_put) {
462 nv_wr32(dev, 0x003244, dma_put);
463 nv_wr32(dev, 0x003328, ho_put);
465 if (ib_get != ib_put) {
466 nv_wr32(dev, 0x003334, ib_put);
469 NV_INFO(dev, "PFIFO_DMA_PUSHER - Ch %d Get 0x%08x "
470 "Put 0x%08x State 0x%08x Push 0x%08x\n",
471 chid, dma_get, dma_put, state, push);
473 if (dma_get != dma_put)
474 nv_wr32(dev, 0x003244, dma_put);
477 nv_wr32(dev, 0x003228, 0x00000000);
478 nv_wr32(dev, 0x003220, 0x00000001);
479 nv_wr32(dev, 0x002100, NV_PFIFO_INTR_DMA_PUSHER);
480 status &= ~NV_PFIFO_INTR_DMA_PUSHER;
483 if (status & NV_PFIFO_INTR_SEMAPHORE) {
486 status &= ~NV_PFIFO_INTR_SEMAPHORE;
487 nv_wr32(dev, NV03_PFIFO_INTR_0,
488 NV_PFIFO_INTR_SEMAPHORE);
490 sem = nv_rd32(dev, NV10_PFIFO_CACHE1_SEMAPHORE);
491 nv_wr32(dev, NV10_PFIFO_CACHE1_SEMAPHORE, sem | 0x1);
493 nv_wr32(dev, NV03_PFIFO_CACHE1_GET, get + 4);
494 nv_wr32(dev, NV04_PFIFO_CACHE1_PULL0, 1);
497 if (dev_priv->card_type == NV_50) {
498 if (status & 0x00000010) {
499 nv50_fb_vm_trap(dev, 1, "PFIFO_BAR_FAULT");
500 status &= ~0x00000010;
501 nv_wr32(dev, 0x002100, 0x00000010);
506 if (nouveau_ratelimit())
507 NV_INFO(dev, "PFIFO_INTR 0x%08x - Ch %d\n",
509 nv_wr32(dev, NV03_PFIFO_INTR_0, status);
513 nv_wr32(dev, NV03_PFIFO_CACHES, reassign);
517 NV_INFO(dev, "PFIFO still angry after %d spins, halt\n", cnt);
518 nv_wr32(dev, 0x2140, 0);
519 nv_wr32(dev, 0x140, 0);
522 nv_wr32(dev, NV03_PMC_INTR_0, NV_PMC_INTR_0_PFIFO_PENDING);