2 * Copyright 2005-2006 Stephane Marchesin
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
27 #include "nouveau_drv.h"
28 #include "nouveau_drm.h"
31 /* returns the number of hw fifos */
32 int nouveau_fifo_number(drm_device_t* dev)
34 drm_nouveau_private_t *dev_priv=dev->dev_private;
35 switch(dev_priv->card_type)
47 /* returns the size of fifo context */
48 static int nouveau_fifo_ctx_size(drm_device_t* dev)
50 drm_nouveau_private_t *dev_priv=dev->dev_private;
52 if (dev_priv->card_type >= NV_40)
54 else if (dev_priv->card_type >= NV_10)
60 /***********************************
61 * functions doing the actual work
62 ***********************************/
64 /* voir nv_xaa.c : NVResetGraphics
65 * mémoire mappée par nv_driver.c : NVMapMem
66 * voir nv_driver.c : NVPreInit
69 static int nouveau_fifo_instmem_configure(drm_device_t *dev)
71 drm_nouveau_private_t *dev_priv = dev->dev_private;
74 /* Clear start of RAMIN, enough to cover RAMFC/HT/RO basically */
75 for (i=0x00710000; i<0x00730000; i++)
76 NV_WRITE(i, 0x00000000);
78 /* FIFO hash table (RAMHT)
79 * use 4k hash table at RAMIN+0x10000
80 * TODO: extend the hash table
82 dev_priv->ramht_offset = 0x10000;
83 dev_priv->ramht_bits = 9;
84 dev_priv->ramht_size = (1 << dev_priv->ramht_bits);
85 NV_WRITE(NV03_PFIFO_RAMHT,
86 (0x03 << 24) /* search 128 */ |
87 ((dev_priv->ramht_bits - 9) << 16) |
88 (dev_priv->ramht_offset >> 8)
90 DRM_DEBUG("RAMHT offset=0x%x, size=%d\n",
91 dev_priv->ramht_offset,
92 dev_priv->ramht_size);
94 /* FIFO runout table (RAMRO) - 512k at 0x11200 */
95 dev_priv->ramro_offset = 0x11200;
96 dev_priv->ramro_size = 512;
97 NV_WRITE(NV03_PFIFO_RAMRO, dev_priv->ramro_offset>>8);
98 DRM_DEBUG("RAMRO offset=0x%x, size=%d\n",
99 dev_priv->ramro_offset,
100 dev_priv->ramro_size);
102 /* FIFO context table (RAMFC)
103 * NV40 : Not sure exactly how to position RAMFC on some cards,
104 * 0x30002 seems to position it at RAMIN+0x20000 on these
105 * cards. RAMFC is 4kb (32 fifos, 128byte entries).
106 * Others: Position RAMFC at RAMIN+0x11400
108 switch(dev_priv->card_type)
112 dev_priv->ramfc_offset = 0x20000;
113 dev_priv->ramfc_size = nouveau_fifo_number(dev) * nouveau_fifo_ctx_size(dev);
114 NV_WRITE(NV40_PFIFO_RAMFC, 0x30002);
117 dev_priv->ramfc_offset = 0x20000;
118 dev_priv->ramfc_size = nouveau_fifo_number(dev) * nouveau_fifo_ctx_size(dev);
119 NV_WRITE(NV40_PFIFO_RAMFC, ((nouveau_mem_fb_amount(dev)-512*1024+dev_priv->ramfc_offset)>>16) |
125 dev_priv->ramfc_offset = 0x11400;
126 dev_priv->ramfc_size = nouveau_fifo_number(dev) * nouveau_fifo_ctx_size(dev);
127 NV_WRITE(NV03_PFIFO_RAMFC, (dev_priv->ramfc_offset>>8) |
128 (1 << 16) /* 64 Bytes entry*/);
132 dev_priv->ramfc_offset = 0x11400;
133 dev_priv->ramfc_size = nouveau_fifo_number(dev) * nouveau_fifo_ctx_size(dev);
134 NV_WRITE(NV03_PFIFO_RAMFC, dev_priv->ramfc_offset>>8);
137 DRM_DEBUG("RAMFC offset=0x%x, size=%d\n",
138 dev_priv->ramfc_offset,
139 dev_priv->ramfc_size);
141 if (nouveau_instmem_init(dev, dev_priv->ramfc_offset +
142 dev_priv->ramfc_size))
148 int nouveau_fifo_init(drm_device_t *dev)
150 drm_nouveau_private_t *dev_priv = dev->dev_private;
153 NV_WRITE(NV03_PFIFO_CACHES, 0x00000000);
155 ret = nouveau_fifo_instmem_configure(dev);
157 DRM_ERROR("Failed to configure instance memory\n");
161 /* FIXME remove all the stuff that's done in nouveau_fifo_alloc */
163 DRM_DEBUG("Setting defaults for remaining PFIFO regs\n");
165 /* All channels into PIO mode */
166 NV_WRITE(NV04_PFIFO_MODE, 0x00000000);
168 NV_WRITE(NV03_PFIFO_CACHE1_PUSH0, 0x00000000);
169 NV_WRITE(NV04_PFIFO_CACHE1_PULL0, 0x00000000);
170 /* Channel 0 active, PIO mode */
171 NV_WRITE(NV03_PFIFO_CACHE1_PUSH1, 0x00000000);
172 /* PUT and GET to 0 */
173 NV_WRITE(NV04_PFIFO_CACHE1_DMA_PUT, 0x00000000);
174 NV_WRITE(NV04_PFIFO_CACHE1_DMA_GET, 0x00000000);
175 /* No cmdbuf object */
176 NV_WRITE(NV04_PFIFO_CACHE1_DMA_INSTANCE, 0x00000000);
177 NV_WRITE(NV03_PFIFO_CACHE0_PUSH0, 0x00000000);
178 NV_WRITE(NV03_PFIFO_CACHE0_PULL0, 0x00000000);
179 NV_WRITE(NV04_PFIFO_SIZE, 0x0000FFFF);
180 NV_WRITE(NV04_PFIFO_CACHE1_HASH, 0x0000FFFF);
181 NV_WRITE(NV04_PFIFO_CACHE0_PULL1, 0x00000001);
182 NV_WRITE(NV04_PFIFO_CACHE1_DMA_CTL, 0x00000000);
183 NV_WRITE(NV04_PFIFO_CACHE1_DMA_STATE, 0x00000000);
184 NV_WRITE(NV04_PFIFO_CACHE1_ENGINE, 0x00000000);
186 NV_WRITE(NV04_PFIFO_CACHE1_DMA_FETCH, NV_PFIFO_CACHE1_DMA_FETCH_TRIG_112_BYTES |
187 NV_PFIFO_CACHE1_DMA_FETCH_SIZE_128_BYTES |
188 NV_PFIFO_CACHE1_DMA_FETCH_MAX_REQS_4 |
190 NV_PFIFO_CACHE1_BIG_ENDIAN |
194 NV_WRITE(NV04_PFIFO_CACHE1_DMA_PUSH, 0x00000001);
195 NV_WRITE(NV03_PFIFO_CACHE1_PUSH0, 0x00000001);
196 NV_WRITE(NV04_PFIFO_CACHE1_PULL0, 0x00000001);
197 NV_WRITE(NV04_PFIFO_CACHE1_PULL1, 0x00000001);
200 if (dev_priv->card_type >= NV_10) {
201 NV_WRITE(NV10_PGRAPH_CTX_USER, 0x0);
202 NV_WRITE(NV04_PFIFO_DELAY_0, 0xff /* retrycount*/ );
203 if (dev_priv->card_type >= NV_40)
204 NV_WRITE(NV10_PGRAPH_CTX_CONTROL, 0x00002001);
206 NV_WRITE(NV10_PGRAPH_CTX_CONTROL, 0x10110000);
208 NV_WRITE(NV04_PGRAPH_CTX_USER, 0x0);
209 NV_WRITE(NV04_PFIFO_DELAY_0, 0xff /* retrycount*/ );
210 NV_WRITE(NV04_PGRAPH_CTX_CONTROL, 0x10110000);
213 NV_WRITE(NV04_PFIFO_DMA_TIMESLICE, 0x001fffff);
214 NV_WRITE(NV03_PFIFO_CACHES, 0x00000001);
219 nouveau_fifo_cmdbuf_alloc(struct drm_device *dev, int channel)
221 drm_nouveau_private_t *dev_priv = dev->dev_private;
222 struct nouveau_config *config = &dev_priv->config;
223 struct mem_block *cb;
224 struct nouveau_object *cb_dma = NULL;
225 int cb_min_size = max(NV03_FIFO_SIZE,PAGE_SIZE);
227 /* Defaults for unconfigured values */
228 if (!config->cmdbuf.location)
229 config->cmdbuf.location = NOUVEAU_MEM_FB;
230 if (!config->cmdbuf.size || config->cmdbuf.size < cb_min_size)
231 config->cmdbuf.size = cb_min_size;
233 cb = nouveau_mem_alloc(dev, 0, config->cmdbuf.size,
234 config->cmdbuf.location | NOUVEAU_MEM_MAPPED,
237 DRM_ERROR("Couldn't allocate DMA command buffer.\n");
238 return DRM_ERR(ENOMEM);
241 if (cb->flags & NOUVEAU_MEM_AGP) {
242 cb_dma = nouveau_dma_object_create(dev, NV_CLASS_DMA_IN_MEMORY,
243 cb->start - dev_priv->agp_phys,
245 NV_DMA_ACCESS_RO, NV_DMA_TARGET_AGP);
246 } else if (dev_priv->card_type != NV_04) {
247 cb_dma = nouveau_dma_object_create(dev, NV_CLASS_DMA_IN_MEMORY,
248 cb->start - drm_get_resource_start(dev, 1),
250 NV_DMA_ACCESS_RO, NV_DMA_TARGET_VIDMEM);
252 /* NV04 cmdbuf hack, from original ddx.. not sure of it's
253 * exact reason for existing :) PCI access to cmdbuf in
256 cb_dma = nouveau_dma_object_create(dev, NV_CLASS_DMA_IN_MEMORY,
258 NV_DMA_ACCESS_RO, NV_DMA_TARGET_PCI);
262 nouveau_mem_free(dev, cb);
263 DRM_ERROR("Failed to alloc DMA object for command buffer\n");
264 return DRM_ERR(ENOMEM);
267 dev_priv->fifos[channel].cmdbuf_mem = cb;
268 dev_priv->fifos[channel].cmdbuf_obj = cb_dma;
272 #define RAMFC_WR(offset, val) NV_WRITE(fifoctx + NV04_RAMFC_##offset, (val))
273 static void nouveau_nv04_context_init(drm_device_t *dev,
274 drm_nouveau_fifo_alloc_t *init)
276 drm_nouveau_private_t *dev_priv = dev->dev_private;
277 struct nouveau_object *cb_obj;
278 uint32_t fifoctx, ctx_size = 32;
281 cb_obj = dev_priv->fifos[init->channel].cmdbuf_obj;
283 fifoctx=NV_RAMIN+dev_priv->ramfc_offset+init->channel*ctx_size;
285 // clear the fifo context
286 for(i=0;i<ctx_size/4;i++)
287 NV_WRITE(fifoctx+4*i,0x0);
289 RAMFC_WR(DMA_PUT , init->put_base);
290 RAMFC_WR(DMA_GET , init->put_base);
291 RAMFC_WR(DMA_INSTANCE , nouveau_chip_instance_get(dev, cb_obj->instance));
293 RAMFC_WR(DMA_FETCH, NV_PFIFO_CACHE1_DMA_FETCH_TRIG_112_BYTES |
294 NV_PFIFO_CACHE1_DMA_FETCH_SIZE_128_BYTES |
295 NV_PFIFO_CACHE1_DMA_FETCH_MAX_REQS_4 |
297 NV_PFIFO_CACHE1_BIG_ENDIAN |
303 #define RAMFC_WR(offset, val) NV_WRITE(fifoctx + NV10_RAMFC_##offset, (val))
304 static void nouveau_nv10_context_init(drm_device_t *dev,
305 drm_nouveau_fifo_alloc_t *init)
307 drm_nouveau_private_t *dev_priv = dev->dev_private;
308 struct nouveau_object *cb_obj;
311 cb_obj = dev_priv->fifos[init->channel].cmdbuf_obj;
312 fifoctx = NV_RAMIN + dev_priv->ramfc_offset + init->channel*64;
315 NV_WRITE(fifoctx + i, 0);
317 /* Fill entries that are seen filled in dumps of nvidia driver just
318 * after channel's is put into DMA mode
321 RAMFC_WR(DMA_PUT , init->put_base);
322 RAMFC_WR(DMA_GET , init->put_base);
323 RAMFC_WR(DMA_INSTANCE , nouveau_chip_instance_get(dev,
326 RAMFC_WR(DMA_FETCH, NV_PFIFO_CACHE1_DMA_FETCH_TRIG_112_BYTES |
327 NV_PFIFO_CACHE1_DMA_FETCH_SIZE_128_BYTES |
328 NV_PFIFO_CACHE1_DMA_FETCH_MAX_REQS_4 |
330 NV_PFIFO_CACHE1_BIG_ENDIAN |
335 static void nouveau_nv30_context_init(drm_device_t *dev,
336 drm_nouveau_fifo_alloc_t *init)
338 drm_nouveau_private_t *dev_priv = dev->dev_private;
339 struct nouveau_fifo *chan = &dev_priv->fifos[init->channel];
340 struct nouveau_object *cb_obj;
341 uint32_t fifoctx, grctx_inst, cb_inst, ctx_size = 64;
344 cb_obj = dev_priv->fifos[init->channel].cmdbuf_obj;
345 cb_inst = nouveau_chip_instance_get(dev, chan->cmdbuf_obj->instance);
346 grctx_inst = nouveau_chip_instance_get(dev, chan->ramin_grctx);
347 fifoctx = NV_RAMIN + dev_priv->ramfc_offset + init->channel * ctx_size;
349 for (i = 0; i < ctx_size; i += 4)
350 NV_WRITE(fifoctx + i, 0);
352 RAMFC_WR(DMA_PUT, init->put_base);
353 RAMFC_WR(DMA_GET, init->put_base);
354 RAMFC_WR(REF_CNT, NV_READ(NV10_PFIFO_CACHE1_REF_CNT));
355 RAMFC_WR(DMA_INSTANCE, cb_inst);
356 RAMFC_WR(DMA_STATE, NV_READ(NV04_PFIFO_CACHE1_DMA_STATE));
357 RAMFC_WR(DMA_FETCH, NV_PFIFO_CACHE1_DMA_FETCH_TRIG_128_BYTES |
358 NV_PFIFO_CACHE1_DMA_FETCH_SIZE_128_BYTES |
359 NV_PFIFO_CACHE1_DMA_FETCH_MAX_REQS_8 |
361 NV_PFIFO_CACHE1_BIG_ENDIAN |
365 RAMFC_WR(ENGINE, NV_READ(NV04_PFIFO_CACHE1_ENGINE));
366 RAMFC_WR(PULL1_ENGINE, NV_READ(NV04_PFIFO_CACHE1_PULL1));
367 RAMFC_WR(ACQUIRE_VALUE, NV_READ(NV10_PFIFO_CACHE1_ACQUIRE_VALUE));
368 RAMFC_WR(ACQUIRE_TIMESTAMP, NV_READ(NV10_PFIFO_CACHE1_ACQUIRE_TIMESTAMP));
369 RAMFC_WR(ACQUIRE_TIMEOUT, NV_READ(NV10_PFIFO_CACHE1_ACQUIRE_TIMEOUT));
370 RAMFC_WR(SEMAPHORE, NV_READ(NV10_PFIFO_CACHE1_SEMAPHORE));
372 RAMFC_WR(DMA_SUBROUTINE, init->put_base);
375 static void nouveau_nv10_context_save(drm_device_t *dev)
377 drm_nouveau_private_t *dev_priv = dev->dev_private;
381 channel = NV_READ(NV03_PFIFO_CACHE1_PUSH1) & (nouveau_fifo_number(dev)-1);
382 fifoctx = NV_RAMIN + dev_priv->ramfc_offset + channel*64;
384 RAMFC_WR(DMA_PUT , NV_READ(NV04_PFIFO_CACHE1_DMA_PUT));
385 RAMFC_WR(DMA_GET , NV_READ(NV04_PFIFO_CACHE1_DMA_GET));
386 RAMFC_WR(REF_CNT , NV_READ(NV10_PFIFO_CACHE1_REF_CNT));
387 RAMFC_WR(DMA_INSTANCE , NV_READ(NV04_PFIFO_CACHE1_DMA_INSTANCE));
388 RAMFC_WR(DMA_STATE , NV_READ(NV04_PFIFO_CACHE1_DMA_STATE));
389 RAMFC_WR(DMA_FETCH , NV_READ(NV04_PFIFO_CACHE1_DMA_FETCH));
390 RAMFC_WR(ENGINE , NV_READ(NV04_PFIFO_CACHE1_ENGINE));
391 RAMFC_WR(PULL1_ENGINE , NV_READ(NV04_PFIFO_CACHE1_PULL1));
392 RAMFC_WR(ACQUIRE_VALUE , NV_READ(NV10_PFIFO_CACHE1_ACQUIRE_VALUE));
393 RAMFC_WR(ACQUIRE_TIMESTAMP, NV_READ(NV10_PFIFO_CACHE1_ACQUIRE_TIMESTAMP));
394 RAMFC_WR(ACQUIRE_TIMEOUT , NV_READ(NV10_PFIFO_CACHE1_ACQUIRE_TIMEOUT));
395 RAMFC_WR(SEMAPHORE , NV_READ(NV10_PFIFO_CACHE1_SEMAPHORE));
396 RAMFC_WR(DMA_SUBROUTINE , NV_READ(NV10_PFIFO_CACHE1_DMA_SUBROUTINE));
400 #define RAMFC_WR(offset, val) NV_WRITE(fifoctx + NV40_RAMFC_##offset, (val))
401 static void nouveau_nv40_context_init(drm_device_t *dev,
402 drm_nouveau_fifo_alloc_t *init)
404 drm_nouveau_private_t *dev_priv = dev->dev_private;
405 struct nouveau_fifo *chan = &dev_priv->fifos[init->channel];
406 uint32_t fifoctx, cb_inst, grctx_inst;
409 cb_inst = nouveau_chip_instance_get(dev, chan->cmdbuf_obj->instance);
410 grctx_inst = nouveau_chip_instance_get(dev, chan->ramin_grctx);
411 fifoctx = NV_RAMIN + dev_priv->ramfc_offset + init->channel*128;
413 NV_WRITE(fifoctx + i, 0);
415 /* Fill entries that are seen filled in dumps of nvidia driver just
416 * after channel's is put into DMA mode
418 RAMFC_WR(DMA_PUT , init->put_base);
419 RAMFC_WR(DMA_GET , init->put_base);
420 RAMFC_WR(DMA_INSTANCE , cb_inst);
421 RAMFC_WR(DMA_FETCH , NV_PFIFO_CACHE1_DMA_FETCH_TRIG_128_BYTES |
422 NV_PFIFO_CACHE1_DMA_FETCH_SIZE_128_BYTES |
423 NV_PFIFO_CACHE1_DMA_FETCH_MAX_REQS_8 |
425 NV_PFIFO_CACHE1_BIG_ENDIAN |
427 0x30000000 /* no idea.. */);
428 RAMFC_WR(DMA_SUBROUTINE, init->put_base);
429 RAMFC_WR(GRCTX_INSTANCE, grctx_inst);
430 RAMFC_WR(DMA_TIMESLICE , 0x0001FFFF);
433 static void nouveau_nv40_context_save(drm_device_t *dev)
435 drm_nouveau_private_t *dev_priv = dev->dev_private;
439 channel = NV_READ(NV03_PFIFO_CACHE1_PUSH1) & (nouveau_fifo_number(dev)-1);
440 fifoctx = NV_RAMIN + dev_priv->ramfc_offset + channel*128;
442 RAMFC_WR(DMA_PUT , NV_READ(NV04_PFIFO_CACHE1_DMA_PUT));
443 RAMFC_WR(DMA_GET , NV_READ(NV04_PFIFO_CACHE1_DMA_GET));
444 RAMFC_WR(REF_CNT , NV_READ(NV10_PFIFO_CACHE1_REF_CNT));
445 RAMFC_WR(DMA_INSTANCE , NV_READ(NV04_PFIFO_CACHE1_DMA_INSTANCE));
446 RAMFC_WR(DMA_DCOUNT , NV_READ(NV10_PFIFO_CACHE1_DMA_DCOUNT));
447 RAMFC_WR(DMA_STATE , NV_READ(NV04_PFIFO_CACHE1_DMA_STATE));
448 RAMFC_WR(DMA_FETCH , NV_READ(NV04_PFIFO_CACHE1_DMA_FETCH));
449 RAMFC_WR(ENGINE , NV_READ(NV04_PFIFO_CACHE1_ENGINE));
450 RAMFC_WR(PULL1_ENGINE , NV_READ(NV04_PFIFO_CACHE1_PULL1));
451 RAMFC_WR(ACQUIRE_VALUE , NV_READ(NV10_PFIFO_CACHE1_ACQUIRE_VALUE));
452 RAMFC_WR(ACQUIRE_TIMESTAMP, NV_READ(NV10_PFIFO_CACHE1_ACQUIRE_TIMESTAMP));
453 RAMFC_WR(ACQUIRE_TIMEOUT , NV_READ(NV10_PFIFO_CACHE1_ACQUIRE_TIMEOUT));
454 RAMFC_WR(SEMAPHORE , NV_READ(NV10_PFIFO_CACHE1_SEMAPHORE));
455 RAMFC_WR(DMA_SUBROUTINE , NV_READ(NV04_PFIFO_CACHE1_DMA_GET));
456 RAMFC_WR(GRCTX_INSTANCE , NV_READ(NV40_PFIFO_GRCTX_INSTANCE));
457 RAMFC_WR(DMA_TIMESLICE , NV_READ(NV04_PFIFO_DMA_TIMESLICE) & 0x1FFFF);
458 RAMFC_WR(UNK_40 , NV_READ(NV40_PFIFO_UNK32E4));
462 /* This function should load values from RAMFC into PFIFO, but for now
463 * it just clobbers PFIFO with what nouveau_fifo_alloc used to setup
467 nouveau_fifo_context_restore(drm_device_t *dev, int channel)
469 drm_nouveau_private_t *dev_priv = dev->dev_private;
470 struct nouveau_fifo *chan = &dev_priv->fifos[channel];
473 cb_inst = nouveau_chip_instance_get(dev, chan->cmdbuf_obj->instance);
475 // FIXME check if we need to refill the time quota with something like NV_WRITE(0x204C, 0x0003FFFF);
477 if (dev_priv->card_type >= NV_40)
478 NV_WRITE(NV03_PFIFO_CACHE1_PUSH1, 0x00010000|channel);
480 NV_WRITE(NV03_PFIFO_CACHE1_PUSH1, 0x00000100|channel);
482 NV_WRITE(NV04_PFIFO_CACHE1_DMA_PUT, 0 /*RAMFC_DMA_PUT*/);
483 NV_WRITE(NV04_PFIFO_CACHE1_DMA_GET, 0 /*RAMFC_DMA_GET*/);
484 NV_WRITE(NV04_PFIFO_CACHE1_DMA_INSTANCE, cb_inst);
485 NV_WRITE(NV04_PFIFO_SIZE , 0x0000FFFF);
486 NV_WRITE(NV04_PFIFO_CACHE1_HASH, 0x0000FFFF);
488 NV_WRITE(NV04_PFIFO_CACHE0_PULL1, 0x00000001);
489 NV_WRITE(NV04_PFIFO_CACHE1_DMA_CTL, 0x00000000);
490 NV_WRITE(NV04_PFIFO_CACHE1_DMA_STATE, 0x00000000);
491 NV_WRITE(NV04_PFIFO_CACHE1_ENGINE, 0x00000000);
493 NV_WRITE(NV04_PFIFO_CACHE1_DMA_FETCH, NV_PFIFO_CACHE1_DMA_FETCH_TRIG_112_BYTES |
494 NV_PFIFO_CACHE1_DMA_FETCH_SIZE_128_BYTES |
495 NV_PFIFO_CACHE1_DMA_FETCH_MAX_REQS_4 |
497 NV_PFIFO_CACHE1_BIG_ENDIAN |
502 /* allocates and initializes a fifo for user space consumption */
503 static int nouveau_fifo_alloc(drm_device_t* dev,drm_nouveau_fifo_alloc_t* init, DRMFILE filp)
507 drm_nouveau_private_t *dev_priv = dev->dev_private;
508 struct nouveau_object *cb_obj;
511 * Alright, here is the full story
512 * Nvidia cards have multiple hw fifo contexts (praise them for that,
513 * no complicated crash-prone context switches)
514 * We allocate a new context for each app and let it write to it directly
515 * (woo, full userspace command submission !)
516 * When there are no more contexts, you lost
518 for(i=0;i<nouveau_fifo_number(dev);i++)
519 if (dev_priv->fifos[i].used==0)
522 DRM_INFO("Allocating FIFO number %d\n", i);
523 /* no more fifos. you lost. */
524 if (i==nouveau_fifo_number(dev))
525 return DRM_ERR(EINVAL);
527 /* allocate a command buffer, and create a dma object for the gpu */
528 ret = nouveau_fifo_cmdbuf_alloc(dev, i);
530 cb_obj = dev_priv->fifos[i].cmdbuf_obj;
532 /* that fifo is used */
533 dev_priv->fifos[i].used=1;
534 dev_priv->fifos[i].filp=filp;
538 dev_priv->cur_fifo = init->channel;
540 nouveau_wait_for_idle(dev);
542 /* disable the fifo caches */
543 NV_WRITE(NV03_PFIFO_CACHES, 0x00000000);
544 NV_WRITE(NV04_PFIFO_CACHE1_DMA_PUSH, NV_READ(NV04_PFIFO_CACHE1_DMA_PUSH)&(~0x1));
545 NV_WRITE(NV03_PFIFO_CACHE1_PUSH0, 0x00000000);
546 NV_WRITE(NV04_PFIFO_CACHE1_PULL0, 0x00000000);
548 /* Construct inital RAMFC for new channel */
549 switch(dev_priv->card_type)
553 nv04_graph_context_create(dev, init->channel);
554 nouveau_nv04_context_init(dev, init);
557 nv10_graph_context_create(dev, init->channel);
558 nouveau_nv10_context_init(dev, init);
561 ret = nv20_graph_context_create(dev, init->channel);
563 nouveau_fifo_free(dev, init->channel);
566 nouveau_nv10_context_init(dev, init);
569 ret = nv30_graph_context_create(dev, init->channel);
571 nouveau_fifo_free(dev, init->channel);
574 nouveau_nv30_context_init(dev, init);
579 ret = nv40_graph_context_create(dev, init->channel);
581 nouveau_fifo_free(dev, init->channel);
584 nouveau_nv40_context_init(dev, init);
588 /* enable the fifo dma operation */
589 NV_WRITE(NV04_PFIFO_MODE,NV_READ(NV04_PFIFO_MODE)|(1<<init->channel));
591 /* setup channel's default get/put values */
592 NV_WRITE(NV03_FIFO_REGS_DMAPUT(init->channel), init->put_base);
593 NV_WRITE(NV03_FIFO_REGS_DMAGET(init->channel), init->put_base);
595 /* If this is the first channel, setup PFIFO ourselves. For any
596 * other case, the GPU will handle this when it switches contexts.
598 if (dev_priv->fifo_alloc_count == 0) {
599 nouveau_fifo_context_restore(dev, init->channel);
600 if (dev_priv->card_type >= NV_30) {
601 struct nouveau_fifo *chan;
604 chan = &dev_priv->fifos[init->channel];
605 inst = nouveau_chip_instance_get(dev,
608 /* see comments in nv40_graph_context_restore() */
609 NV_WRITE(NV10_PGRAPH_CHANNEL_CTX_SIZE, inst);
610 if (dev_priv->card_type >= NV_40) {
611 NV_WRITE(0x40032C, inst | 0x01000000);
612 NV_WRITE(NV40_PFIFO_GRCTX_INSTANCE, inst);
617 NV_WRITE(NV04_PFIFO_CACHE1_DMA_PUSH, 0x00000001);
618 NV_WRITE(NV03_PFIFO_CACHE1_PUSH0, 0x00000001);
619 NV_WRITE(NV04_PFIFO_CACHE1_PULL0, 0x00000001);
620 NV_WRITE(NV04_PFIFO_CACHE1_PULL1, 0x00000001);
622 /* reenable the fifo caches */
623 NV_WRITE(NV03_PFIFO_CACHES, 0x00000001);
625 /* make the fifo available to user space */
626 /* first, the fifo control regs */
627 init->ctrl = dev_priv->mmio->offset + NV03_FIFO_REGS(init->channel);
628 init->ctrl_size = NV03_FIFO_REGS_SIZE;
629 ret = drm_addmap(dev, init->ctrl, init->ctrl_size, _DRM_REGISTERS,
630 0, &dev_priv->fifos[init->channel].regs);
634 /* pass back FIFO map info to the caller */
635 init->cmdbuf = dev_priv->fifos[init->channel].cmdbuf_mem->start;
636 init->cmdbuf_size = dev_priv->fifos[init->channel].cmdbuf_mem->size;
638 /* FIFO has no objects yet */
639 dev_priv->fifos[init->channel].objs = NULL;
640 dev_priv->fifo_alloc_count++;
642 DRM_INFO("%s: initialised FIFO %d\n", __func__, init->channel);
647 void nouveau_fifo_free(drm_device_t* dev,int n)
649 drm_nouveau_private_t *dev_priv = dev->dev_private;
651 int ctx_size = nouveau_fifo_ctx_size(dev);
653 dev_priv->fifos[n].used=0;
654 DRM_INFO("%s: freeing fifo %d\n", __func__, n);
656 /* disable the fifo caches */
657 NV_WRITE(NV03_PFIFO_CACHES, 0x00000000);
659 NV_WRITE(NV04_PFIFO_MODE,NV_READ(NV04_PFIFO_MODE)&~(1<<n));
660 // FIXME XXX needs more code
663 for (i=0;i<ctx_size;i+=4) {
664 DRM_DEBUG("RAMFC +%02x: 0x%08x\n", i, NV_READ(NV_RAMIN +
665 dev_priv->ramfc_offset + n*ctx_size + i));
666 NV_WRITE(NV_RAMIN + dev_priv->ramfc_offset + n*ctx_size + i, 0);
669 if (dev_priv->card_type >= NV_40)
670 nouveau_instmem_free(dev, dev_priv->fifos[n].ramin_grctx);
671 else if (dev_priv->card_type >= NV_30) {
673 else if (dev_priv->card_type >= NV_20) {
674 /* clear ctx table */
675 INSTANCE_WR(dev_priv->ctx_table, n, 0);
676 nouveau_instmem_free(dev, dev_priv->fifos[n].ramin_grctx);
679 /* reenable the fifo caches */
680 NV_WRITE(NV03_PFIFO_CACHES, 0x00000001);
682 /* Deallocate command buffer, and dma object */
683 nouveau_mem_free(dev, dev_priv->fifos[n].cmdbuf_mem);
685 dev_priv->fifo_alloc_count--;
688 /* cleanups all the fifos from filp */
689 void nouveau_fifo_cleanup(drm_device_t* dev, DRMFILE filp)
692 drm_nouveau_private_t *dev_priv = dev->dev_private;
694 DRM_DEBUG("clearing FIFO enables from filp\n");
695 for(i=0;i<nouveau_fifo_number(dev);i++)
696 if (dev_priv->fifos[i].used && dev_priv->fifos[i].filp==filp)
697 nouveau_fifo_free(dev,i);
699 /* check we still point at an active channel */
700 if (dev_priv->fifos[dev_priv->cur_fifo].used == 0) {
701 DRM_DEBUG("%s: cur_fifo is no longer owned.\n", __func__);
702 for (i=0;i<nouveau_fifo_number(dev);i++)
703 if (dev_priv->fifos[i].used) break;
704 if (i==nouveau_fifo_number(dev))
706 DRM_DEBUG("%s: new cur_fifo is %d\n", __func__, i);
707 dev_priv->cur_fifo = i;
710 /* if (dev_priv->cmdbuf_alloc)
711 nouveau_fifo_init(dev);*/
714 int nouveau_fifo_id_get(drm_device_t* dev, DRMFILE filp)
716 drm_nouveau_private_t *dev_priv=dev->dev_private;
719 for(i=0;i<nouveau_fifo_number(dev);i++)
720 if (dev_priv->fifos[i].used && dev_priv->fifos[i].filp == filp)
725 /***********************************
726 * ioctls wrapping the functions
727 ***********************************/
729 static int nouveau_ioctl_fifo_alloc(DRM_IOCTL_ARGS)
732 drm_nouveau_fifo_alloc_t init;
734 DRM_COPY_FROM_USER_IOCTL(init, (drm_nouveau_fifo_alloc_t __user *) data, sizeof(init));
736 res=nouveau_fifo_alloc(dev,&init,filp);
738 DRM_COPY_TO_USER_IOCTL((drm_nouveau_fifo_alloc_t __user *)data, init, sizeof(init));
743 /***********************************
744 * finally, the ioctl table
745 ***********************************/
747 drm_ioctl_desc_t nouveau_ioctls[] = {
748 [DRM_IOCTL_NR(DRM_NOUVEAU_FIFO_ALLOC)] = {nouveau_ioctl_fifo_alloc, DRM_AUTH},
749 [DRM_IOCTL_NR(DRM_NOUVEAU_OBJECT_INIT)] = {nouveau_ioctl_object_init, DRM_AUTH},
750 [DRM_IOCTL_NR(DRM_NOUVEAU_DMA_OBJECT_INIT)] = {nouveau_ioctl_dma_object_init, DRM_AUTH},
751 [DRM_IOCTL_NR(DRM_NOUVEAU_MEM_ALLOC)] = {nouveau_ioctl_mem_alloc, DRM_AUTH},
752 [DRM_IOCTL_NR(DRM_NOUVEAU_MEM_FREE)] = {nouveau_ioctl_mem_free, DRM_AUTH},
753 [DRM_IOCTL_NR(DRM_NOUVEAU_GETPARAM)] = {nouveau_ioctl_getparam, DRM_AUTH},
754 [DRM_IOCTL_NR(DRM_NOUVEAU_SETPARAM)] = {nouveau_ioctl_setparam, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY},
757 int nouveau_max_ioctl = DRM_ARRAY_SIZE(nouveau_ioctls);