2 * Copyright (C) 2006 Ben Skeggs.
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:
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.
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.
30 * Ben Skeggs <darktama@iinet.net.au>
35 #include "nouveau_drv.h"
36 #include "nouveau_drm.h"
38 /* NVidia uses context objects to drive drawing operations.
40 Context objects can be selected into 8 subchannels in the FIFO,
41 and then used via DMA command buffers.
43 A context object is referenced by a user defined handle (CARD32). The HW
44 looks up graphics objects in a hash table in the instance RAM.
46 An entry in the hash table consists of 2 CARD32. The first CARD32 contains
47 the handle, the second one a bitfield, that contains the address of the
48 object in instance RAM.
50 The format of the second CARD32 seems to be:
54 15: 0 instance_addr >> 4
55 17:16 engine (here uses 1 = graphics)
56 28:24 channel id (here uses 0)
61 15: 0 instance_addr >> 4 (maybe 19-0)
62 21:20 engine (here uses 1 = graphics)
63 I'm unsure about the other bits, but using 0 seems to work.
65 The key into the hash table depends on the object handle and channel id and
69 nouveau_ramht_hash_handle(struct drm_device *dev, int channel, uint32_t handle)
71 struct drm_nouveau_private *dev_priv=dev->dev_private;
75 for (i=32;i>0;i-=dev_priv->ramht_bits) {
76 hash ^= (handle & ((1 << dev_priv->ramht_bits) - 1));
77 handle >>= dev_priv->ramht_bits;
79 if (dev_priv->card_type < NV_50)
80 hash ^= channel << (dev_priv->ramht_bits - 4);
83 DRM_DEBUG("ch%d handle=0x%08x hash=0x%08x\n", channel, handle, hash);
88 nouveau_ramht_entry_valid(struct drm_device *dev, struct nouveau_gpuobj *ramht,
91 struct drm_nouveau_private *dev_priv=dev->dev_private;
92 uint32_t ctx = INSTANCE_RD(ramht, (offset + 4)/4);
94 if (dev_priv->card_type < NV_40)
95 return ((ctx & NV_RAMHT_CONTEXT_VALID) != 0);
100 nouveau_ramht_insert(struct drm_device *dev, struct nouveau_gpuobj_ref *ref)
102 struct drm_nouveau_private *dev_priv=dev->dev_private;
103 struct nouveau_channel *chan = dev_priv->fifos[ref->channel];
104 struct nouveau_gpuobj *ramht = chan->ramht ? chan->ramht->gpuobj : NULL;
105 struct nouveau_gpuobj *gpuobj = ref->gpuobj;
106 uint32_t ctx, co, ho;
109 DRM_ERROR("No hash table!\n");
113 if (dev_priv->card_type < NV_40) {
114 ctx = NV_RAMHT_CONTEXT_VALID | (ref->instance >> 4) |
115 (ref->channel << NV_RAMHT_CONTEXT_CHANNEL_SHIFT) |
116 (gpuobj->engine << NV_RAMHT_CONTEXT_ENGINE_SHIFT);
118 if (dev_priv->card_type < NV_50) {
119 ctx = (ref->instance >> 4) |
120 (ref->channel << NV40_RAMHT_CONTEXT_CHANNEL_SHIFT) |
121 (gpuobj->engine << NV40_RAMHT_CONTEXT_ENGINE_SHIFT);
123 ctx = (ref->instance >> 4) |
124 (gpuobj->engine << NV40_RAMHT_CONTEXT_ENGINE_SHIFT);
127 co = ho = nouveau_ramht_hash_handle(dev, ref->channel, ref->handle);
129 if (!nouveau_ramht_entry_valid(dev, ramht, co)) {
130 DRM_DEBUG("insert ch%d 0x%08x: h=0x%08x, c=0x%08x\n",
131 ref->channel, co, ref->handle, ctx);
132 INSTANCE_WR(ramht, (co + 0)/4, ref->handle);
133 INSTANCE_WR(ramht, (co + 4)/4, ctx);
135 list_add_tail(&ref->list, &chan->ramht_refs);
138 DRM_DEBUG("collision ch%d 0x%08x: h=0x%08x\n",
139 ref->channel, co, INSTANCE_RD(ramht, co/4));
142 if (co >= dev_priv->ramht_size)
146 DRM_ERROR("RAMHT space exhausted. ch=%d\n", ref->channel);
151 nouveau_ramht_remove(struct drm_device *dev, struct nouveau_gpuobj_ref *ref)
153 struct drm_nouveau_private *dev_priv = dev->dev_private;
154 struct nouveau_channel *chan = dev_priv->fifos[ref->channel];
155 struct nouveau_gpuobj *ramht = chan->ramht ? chan->ramht->gpuobj : NULL;
159 DRM_ERROR("No hash table!\n");
163 co = ho = nouveau_ramht_hash_handle(dev, ref->channel, ref->handle);
165 if (nouveau_ramht_entry_valid(dev, ramht, co) &&
166 (ref->handle == INSTANCE_RD(ramht, (co/4)))) {
167 DRM_DEBUG("remove ch%d 0x%08x: h=0x%08x, c=0x%08x\n",
168 ref->channel, co, ref->handle,
169 INSTANCE_RD(ramht, (co + 4)));
170 INSTANCE_WR(ramht, (co + 0)/4, 0x00000000);
171 INSTANCE_WR(ramht, (co + 4)/4, 0x00000000);
173 list_del(&ref->list);
178 if (co >= dev_priv->ramht_size)
182 DRM_ERROR("RAMHT entry not found. ch=%d, handle=0x%08x\n",
183 ref->channel, ref->handle);
187 nouveau_gpuobj_new(struct drm_device *dev, struct nouveau_channel *chan,
188 int size, int align, uint32_t flags,
189 struct nouveau_gpuobj **gpuobj_ret)
191 struct drm_nouveau_private *dev_priv = dev->dev_private;
192 struct nouveau_engine *engine = &dev_priv->Engine;
193 struct nouveau_gpuobj *gpuobj;
194 struct mem_block *pramin = NULL;
197 DRM_DEBUG("ch%d size=%d align=%d flags=0x%08x\n",
198 chan ? chan->id : -1, size, align, flags);
200 if (!dev_priv || !gpuobj_ret || *gpuobj_ret != NULL)
203 gpuobj = drm_calloc(1, sizeof(*gpuobj), DRM_MEM_DRIVER);
206 DRM_DEBUG("gpuobj %p\n", gpuobj);
207 gpuobj->flags = flags;
208 gpuobj->im_channel = chan ? chan->id : -1;
210 list_add_tail(&gpuobj->list, &dev_priv->gpuobj_list);
212 /* Choose between global instmem heap, and per-channel private
213 * instmem heap. On <NV50 allow requests for private instmem
214 * to be satisfied from global heap if no per-channel area
218 if (chan->ramin_heap) {
219 DRM_DEBUG("private heap\n");
220 pramin = chan->ramin_heap;
222 if (dev_priv->card_type < NV_50) {
223 DRM_DEBUG("global heap fallback\n");
224 pramin = dev_priv->ramin_heap;
227 DRM_DEBUG("global heap\n");
228 pramin = dev_priv->ramin_heap;
232 DRM_ERROR("No PRAMIN heap!\n");
236 if (!chan && (ret = engine->instmem.populate(dev, gpuobj, &size))) {
237 nouveau_gpuobj_del(dev, &gpuobj);
241 /* Allocate a chunk of the PRAMIN aperture */
242 gpuobj->im_pramin = nouveau_mem_alloc_block(pramin, size,
244 (struct drm_file *)-2);
245 if (!gpuobj->im_pramin) {
246 nouveau_gpuobj_del(dev, &gpuobj);
249 gpuobj->im_pramin->flags = NOUVEAU_MEM_INSTANCE;
251 if (!chan && (ret = engine->instmem.bind(dev, gpuobj))) {
252 nouveau_gpuobj_del(dev, &gpuobj);
256 if (gpuobj->flags & NVOBJ_FLAG_ZERO_ALLOC) {
259 for (i = 0; i < gpuobj->im_pramin->size; i += 4)
260 INSTANCE_WR(gpuobj, i/4, 0);
263 *gpuobj_ret = gpuobj;
268 nouveau_gpuobj_early_init(struct drm_device *dev)
270 struct drm_nouveau_private *dev_priv = dev->dev_private;
274 INIT_LIST_HEAD(&dev_priv->gpuobj_list);
280 nouveau_gpuobj_init(struct drm_device *dev)
282 struct drm_nouveau_private *dev_priv = dev->dev_private;
287 if (dev_priv->card_type < NV_50) {
288 if ((ret = nouveau_gpuobj_new_fake(dev, dev_priv->ramht_offset,
289 dev_priv->ramht_size,
290 NVOBJ_FLAG_ZERO_ALLOC |
291 NVOBJ_FLAG_ALLOW_NO_REFS,
292 &dev_priv->ramht, NULL)))
300 nouveau_gpuobj_takedown(struct drm_device *dev)
302 struct drm_nouveau_private *dev_priv = dev->dev_private;
306 nouveau_gpuobj_del(dev, &dev_priv->ramht);
310 nouveau_gpuobj_late_takedown(struct drm_device *dev)
312 struct drm_nouveau_private *dev_priv = dev->dev_private;
313 struct nouveau_gpuobj *gpuobj = NULL;
314 struct list_head *entry, *tmp;
318 list_for_each_safe(entry, tmp, &dev_priv->gpuobj_list) {
319 gpuobj = list_entry(entry, struct nouveau_gpuobj, list);
321 DRM_ERROR("gpuobj %p still exists at takedown, refs=%d\n",
322 gpuobj, gpuobj->refcount);
323 gpuobj->refcount = 0;
324 nouveau_gpuobj_del(dev, &gpuobj);
329 nouveau_gpuobj_del(struct drm_device *dev, struct nouveau_gpuobj **pgpuobj)
331 struct drm_nouveau_private *dev_priv = dev->dev_private;
332 struct nouveau_engine *engine = &dev_priv->Engine;
333 struct nouveau_gpuobj *gpuobj;
335 DRM_DEBUG("gpuobj %p\n", pgpuobj ? *pgpuobj : NULL);
337 if (!dev_priv || !pgpuobj || !(*pgpuobj))
341 if (gpuobj->refcount != 0) {
342 DRM_ERROR("gpuobj refcount is %d\n", gpuobj->refcount);
347 gpuobj->dtor(dev, gpuobj);
349 engine->instmem.clear(dev, gpuobj);
351 if (gpuobj->im_pramin) {
352 if (gpuobj->flags & NVOBJ_FLAG_FAKE)
353 drm_free(gpuobj->im_pramin, sizeof(*gpuobj->im_pramin),
356 nouveau_mem_free_block(gpuobj->im_pramin);
359 list_del(&gpuobj->list);
362 drm_free(gpuobj, sizeof(*gpuobj), DRM_MEM_DRIVER);
367 nouveau_gpuobj_instance_get(struct drm_device *dev,
368 struct nouveau_channel *chan,
369 struct nouveau_gpuobj *gpuobj, uint32_t *inst)
371 struct drm_nouveau_private *dev_priv = dev->dev_private;
372 struct nouveau_gpuobj *cpramin;
374 /* <NV50 use PRAMIN address everywhere */
375 if (dev_priv->card_type < NV_50) {
376 *inst = gpuobj->im_pramin->start;
380 if (chan && gpuobj->im_channel != chan->id) {
381 DRM_ERROR("Channel mismatch: obj %d, ref %d\n",
382 gpuobj->im_channel, chan->id);
386 /* NV50 channel-local instance */
388 cpramin = chan->ramin->gpuobj;
389 *inst = gpuobj->im_pramin->start - cpramin->im_pramin->start;
393 /* NV50 global (VRAM) instance */
394 if (gpuobj->im_channel < 0) {
395 /* ...from global heap */
396 if (!gpuobj->im_backing) {
397 DRM_ERROR("AII, no VRAM backing gpuobj\n");
400 *inst = gpuobj->im_backing->start;
403 /* ...from local heap */
404 cpramin = dev_priv->fifos[gpuobj->im_channel]->ramin->gpuobj;
405 *inst = cpramin->im_backing->start +
406 (gpuobj->im_pramin->start - cpramin->im_pramin->start);
414 nouveau_gpuobj_ref_add(struct drm_device *dev, struct nouveau_channel *chan,
415 uint32_t handle, struct nouveau_gpuobj *gpuobj,
416 struct nouveau_gpuobj_ref **ref_ret)
418 struct drm_nouveau_private *dev_priv = dev->dev_private;
419 struct nouveau_gpuobj_ref *ref;
423 DRM_DEBUG("ch%d h=0x%08x gpuobj=%p\n",
424 chan ? chan->id : -1, handle, gpuobj);
426 if (!dev_priv || !gpuobj || (ref_ret && *ref_ret != NULL))
429 if (!chan && !ref_ret)
432 ret = nouveau_gpuobj_instance_get(dev, chan, gpuobj, &instance);
436 ref = drm_calloc(1, sizeof(*ref), DRM_MEM_DRIVER);
439 ref->gpuobj = gpuobj;
440 ref->channel = chan ? chan->id : -1;
441 ref->instance = instance;
444 ref->handle = handle;
446 ret = nouveau_ramht_insert(dev, ref);
448 drm_free(ref, sizeof(*ref), DRM_MEM_DRIVER);
456 ref->gpuobj->refcount++;
460 int nouveau_gpuobj_ref_del(struct drm_device *dev, struct nouveau_gpuobj_ref **pref)
462 struct nouveau_gpuobj_ref *ref;
464 DRM_DEBUG("ref %p\n", pref ? *pref : NULL);
466 if (!dev || !pref || *pref == NULL)
470 if (ref->handle != ~0)
471 nouveau_ramht_remove(dev, ref);
474 ref->gpuobj->refcount--;
476 if (ref->gpuobj->refcount == 0) {
477 if (!(ref->gpuobj->flags & NVOBJ_FLAG_ALLOW_NO_REFS))
478 nouveau_gpuobj_del(dev, &ref->gpuobj);
483 drm_free(ref, sizeof(ref), DRM_MEM_DRIVER);
488 nouveau_gpuobj_new_ref(struct drm_device *dev,
489 struct nouveau_channel *oc, struct nouveau_channel *rc,
490 uint32_t handle, int size, int align, uint32_t flags,
491 struct nouveau_gpuobj_ref **ref)
493 struct nouveau_gpuobj *gpuobj = NULL;
496 if ((ret = nouveau_gpuobj_new(dev, oc, size, align, flags, &gpuobj)))
499 if ((ret = nouveau_gpuobj_ref_add(dev, rc, handle, gpuobj, ref))) {
500 nouveau_gpuobj_del(dev, &gpuobj);
508 nouveau_gpuobj_ref_find(struct nouveau_channel *chan, uint32_t handle,
509 struct nouveau_gpuobj_ref **ref_ret)
511 struct nouveau_gpuobj_ref *ref;
512 struct list_head *entry, *tmp;
514 list_for_each_safe(entry, tmp, &chan->ramht_refs) {
515 ref = list_entry(entry, struct nouveau_gpuobj_ref, list);
517 if (ref->handle == handle) {
528 nouveau_gpuobj_new_fake(struct drm_device *dev, uint32_t offset, uint32_t size,
529 uint32_t flags, struct nouveau_gpuobj **pgpuobj,
530 struct nouveau_gpuobj_ref **pref)
532 struct drm_nouveau_private *dev_priv = dev->dev_private;
533 struct nouveau_gpuobj *gpuobj = NULL;
536 DRM_DEBUG("offset=0x%08x size=0x%08x flags=0x%08x\n",
537 offset, size, flags);
539 gpuobj = drm_calloc(1, sizeof(*gpuobj), DRM_MEM_DRIVER);
542 DRM_DEBUG("gpuobj %p\n", gpuobj);
543 gpuobj->im_channel = -1;
544 gpuobj->flags = flags | NVOBJ_FLAG_FAKE;
546 list_add_tail(&gpuobj->list, &dev_priv->gpuobj_list);
548 gpuobj->im_pramin = drm_calloc(1, sizeof(struct mem_block),
550 if (!gpuobj->im_pramin) {
551 nouveau_gpuobj_del(dev, &gpuobj);
554 gpuobj->im_pramin->start = offset;
555 gpuobj->im_pramin->size = size;
557 if (gpuobj->flags & NVOBJ_FLAG_ZERO_ALLOC) {
558 for (i = 0; i < gpuobj->im_pramin->size; i += 4)
559 INSTANCE_WR(gpuobj, i/4, 0);
563 if ((i = nouveau_gpuobj_ref_add(dev, NULL, 0, gpuobj, pref))) {
564 nouveau_gpuobj_del(dev, &gpuobj);
576 nouveau_gpuobj_class_instmem_size(struct drm_device *dev, int class)
578 struct drm_nouveau_private *dev_priv = dev->dev_private;
580 /*XXX: dodgy hack for now */
581 if (dev_priv->card_type >= NV_50)
583 if (dev_priv->card_type >= NV_40)
589 DMA objects are used to reference a piece of memory in the
590 framebuffer, PCI or AGP address space. Each object is 16 bytes big
591 and looks as follows:
594 11:0 class (seems like I can always use 0 here)
595 12 page table present?
596 13 page entry linear?
597 15:14 access: 0 rw, 1 ro, 2 wo
598 17:16 target: 0 NV memory, 1 NV memory tiled, 2 PCI, 3 AGP
599 31:20 dma adjust (bits 0-11 of the address)
601 dma limit (size of transfer)
603 1 0 readonly, 1 readwrite
604 31:12 dma frame address of the page (bits 12-31 of the address)
606 page table terminator, same value as the first pte, as does nvidia
607 rivatv uses 0xffffffff
609 Non linear page tables need a list of frame addresses afterwards,
610 the rivatv project has some info on this.
612 The method below creates a DMA object in instance RAM and returns a handle
613 to it that can be used to set up context objects.
616 nouveau_gpuobj_dma_new(struct nouveau_channel *chan, int class,
617 uint64_t offset, uint64_t size, int access,
618 int target, struct nouveau_gpuobj **gpuobj)
620 struct drm_device *dev = chan->dev;
621 struct drm_nouveau_private *dev_priv = dev->dev_private;
623 uint32_t is_scatter_gather = 0;
625 /* Total number of pages covered by the request.
627 const unsigned int page_count = (size + PAGE_SIZE - 1) / PAGE_SIZE;
630 DRM_DEBUG("ch%d class=0x%04x offset=0x%llx size=0x%llx\n",
631 chan->id, class, offset, size);
632 DRM_DEBUG("access=%d target=%d\n", access, target);
635 case NV_DMA_TARGET_AGP:
636 offset += dev_priv->gart_info.aper_base;
638 case NV_DMA_TARGET_PCI_NONLINEAR:
639 /*assume the "offset" is a virtual memory address*/
640 is_scatter_gather = 1;
641 /*put back the right value*/
642 target = NV_DMA_TARGET_PCI;
648 ret = nouveau_gpuobj_new(dev, chan,
649 is_scatter_gather ? ((page_count << 2) + 12) : nouveau_gpuobj_class_instmem_size(dev, class),
651 NVOBJ_FLAG_ZERO_ALLOC | NVOBJ_FLAG_ZERO_FREE,
654 DRM_ERROR("Error creating gpuobj: %d\n", ret);
658 if (dev_priv->card_type < NV_50) {
659 uint32_t frame, adjust, pte_flags = 0;
660 adjust = offset & 0x00000fff;
661 if (access != NV_DMA_ACCESS_RO)
664 if ( ! is_scatter_gather )
666 frame = offset & ~0x00000fff;
668 INSTANCE_WR(*gpuobj, 0, ((1<<12) | (1<<13) |
673 INSTANCE_WR(*gpuobj, 1, size - 1);
674 INSTANCE_WR(*gpuobj, 2, frame | pte_flags);
675 INSTANCE_WR(*gpuobj, 3, frame | pte_flags);
679 /* Intial page entry in the scatter-gather area that
680 * corresponds to the base offset
682 unsigned int idx = offset / PAGE_SIZE;
684 uint32_t instance_offset;
687 if ((idx + page_count) > dev->sg->pages) {
688 DRM_ERROR("Requested page range exceedes "
689 "allocated scatter-gather range!");
693 DRM_DEBUG("Creating PCI DMA object using virtual zone starting at %#llx, size %d\n", offset, (uint32_t)size);
694 INSTANCE_WR(*gpuobj, 0, ((1<<12) | (0<<13) |
699 INSTANCE_WR(*gpuobj, 1, (uint32_t) size-1);
702 /*write starting at the third dword*/
705 /*for each PAGE, get its bus address, fill in the page table entry, and advance*/
706 for (i = 0; i < page_count; i++) {
707 if (dev->sg->busaddr[idx] == 0) {
708 dev->sg->busaddr[idx] =
709 pci_map_page(dev->pdev,
710 dev->sg->pagelist[idx],
715 if (dma_mapping_error(dev->sg->busaddr[idx])) {
720 frame = (uint32_t) dev->sg->busaddr[idx];
721 INSTANCE_WR(*gpuobj, instance_offset,
729 uint32_t flags0, flags5;
731 if (target == NV_DMA_TARGET_VIDMEM) {
739 INSTANCE_WR(*gpuobj, 0, flags0 | class);
740 INSTANCE_WR(*gpuobj, 1, offset + size - 1);
741 INSTANCE_WR(*gpuobj, 2, offset);
742 INSTANCE_WR(*gpuobj, 5, flags5);
745 (*gpuobj)->engine = NVOBJ_ENGINE_SW;
746 (*gpuobj)->class = class;
751 nouveau_gpuobj_gart_dma_new(struct nouveau_channel *chan,
752 uint64_t offset, uint64_t size, int access,
753 struct nouveau_gpuobj **gpuobj,
756 struct drm_device *dev = chan->dev;
757 struct drm_nouveau_private *dev_priv = dev->dev_private;
760 if (dev_priv->gart_info.type == NOUVEAU_GART_AGP ||
761 (dev_priv->card_type >= NV_50 &&
762 dev_priv->gart_info.type == NOUVEAU_GART_SGDMA)) {
763 ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
764 offset, size, access,
765 NV_DMA_TARGET_AGP, gpuobj);
769 if (dev_priv->gart_info.type == NOUVEAU_GART_SGDMA) {
770 *gpuobj = dev_priv->gart_info.sg_ctxdma;
771 if (offset & ~0xffffffffULL) {
772 DRM_ERROR("obj offset exceeds 32-bits\n");
776 *o_ret = (uint32_t)offset;
777 ret = (*gpuobj != NULL) ? 0 : -EINVAL;
779 DRM_ERROR("Invalid GART type %d\n", dev_priv->gart_info.type);
786 /* Context objects in the instance RAM have the following structure.
787 * On NV40 they are 32 byte long, on NV30 and smaller 16 bytes.
797 scrcopy_and, rop_and, blend_and, scrcopy, srccopy_pre, blend_pre
798 18 synchronize enable
799 19 endian: 1 big, 0 little
801 23 single step enable
802 24 patch status: 0 invalid, 1 valid
803 25 context_surface 0: 1 valid
804 26 context surface 1: 1 valid
805 27 context pattern: 1 valid
806 28 context rop: 1 valid
807 29,30 context beta, beta4
811 31:16 notify instance address
813 15:0 dma 0 instance address
814 31:16 dma 1 instance address
819 No idea what the exact format is. Here's what can be deducted:
822 11:0 class (maybe uses more bits here?)
825 25 patch status valid ?
827 15:0 DMA notifier (maybe 20:0)
829 15:0 DMA 0 instance (maybe 20:0)
832 15:0 DMA 1 instance (maybe 20:0)
838 nouveau_gpuobj_gr_new(struct nouveau_channel *chan, int class,
839 struct nouveau_gpuobj **gpuobj)
841 struct drm_device *dev = chan->dev;
842 struct drm_nouveau_private *dev_priv = dev->dev_private;
845 DRM_DEBUG("ch%d class=0x%04x\n", chan->id, class);
847 ret = nouveau_gpuobj_new(dev, chan,
848 nouveau_gpuobj_class_instmem_size(dev, class),
850 NVOBJ_FLAG_ZERO_ALLOC | NVOBJ_FLAG_ZERO_FREE,
853 DRM_ERROR("Error creating gpuobj: %d\n", ret);
857 if (dev_priv->card_type >= NV_50) {
858 INSTANCE_WR(*gpuobj, 0, class);
859 INSTANCE_WR(*gpuobj, 5, 0x00010000);
863 INSTANCE_WR(*gpuobj, 0, 0x00001030);
864 INSTANCE_WR(*gpuobj, 1, 0xFFFFFFFF);
867 if (dev_priv->card_type >= NV_40) {
868 INSTANCE_WR(*gpuobj, 0, class);
870 INSTANCE_WR(*gpuobj, 2, 0x01000000);
874 INSTANCE_WR(*gpuobj, 0, class | 0x00080000);
876 INSTANCE_WR(*gpuobj, 0, class);
882 (*gpuobj)->engine = NVOBJ_ENGINE_GR;
883 (*gpuobj)->class = class;
888 nouveau_gpuobj_channel_init_pramin(struct nouveau_channel *chan)
890 struct drm_device *dev = chan->dev;
891 struct drm_nouveau_private *dev_priv = dev->dev_private;
892 struct nouveau_gpuobj *pramin = NULL;
895 DRM_DEBUG("ch%d\n", chan->id);
897 /* Base amount for object storage (4KiB enough?) */
903 if (dev_priv->card_type == NV_50) {
904 /* Various fixed table thingos */
905 size += 0x1400; /* mostly unknown stuff */
906 size += 0x4000; /* vm pd */
908 /* RAMHT, not sure about setting size yet, 32KiB to be safe */
916 DRM_DEBUG("ch%d PRAMIN size: 0x%08x bytes, base alloc=0x%08x\n",
917 chan->id, size, base);
918 ret = nouveau_gpuobj_new_ref(dev, NULL, NULL, 0, size, 0x1000, 0,
921 DRM_ERROR("Error allocating channel PRAMIN: %d\n", ret);
924 pramin = chan->ramin->gpuobj;
926 ret = nouveau_mem_init_heap(&chan->ramin_heap,
927 pramin->im_pramin->start + base, size);
929 DRM_ERROR("Error creating PRAMIN heap: %d\n", ret);
930 nouveau_gpuobj_ref_del(dev, &chan->ramin);
938 nouveau_gpuobj_channel_init(struct nouveau_channel *chan,
939 uint32_t vram_h, uint32_t tt_h)
941 struct drm_device *dev = chan->dev;
942 struct drm_nouveau_private *dev_priv = dev->dev_private;
943 struct nouveau_gpuobj *vram = NULL, *tt = NULL;
946 INIT_LIST_HEAD(&chan->ramht_refs);
948 DRM_DEBUG("ch%d vram=0x%08x tt=0x%08x\n", chan->id, vram_h, tt_h);
950 /* Reserve a block of PRAMIN for the channel
951 *XXX: maybe on <NV50 too at some point
953 if (0 || dev_priv->card_type == NV_50) {
954 ret = nouveau_gpuobj_channel_init_pramin(chan);
959 /* NV50 VM, point offset 0-512MiB at shared PCIEGART table */
960 if (dev_priv->card_type >= NV_50) {
963 vm_offset = (dev_priv->chipset & 0xf0) == 0x50 ? 0x1400 : 0x200;
964 vm_offset += chan->ramin->gpuobj->im_pramin->start;
965 if ((ret = nouveau_gpuobj_new_fake(dev, vm_offset, 0x4000,
966 0, &chan->vm_pd, NULL)))
968 for (i=0; i<0x4000; i+=8) {
969 INSTANCE_WR(chan->vm_pd, (i+0)/4, 0x00000000);
970 INSTANCE_WR(chan->vm_pd, (i+4)/4, 0xdeadcafe);
973 if ((ret = nouveau_gpuobj_ref_add(dev, NULL, 0,
974 dev_priv->gart_info.sg_ctxdma,
977 INSTANCE_WR(chan->vm_pd, (0+0)/4,
978 chan->vm_gart_pt->instance | 0x03);
979 INSTANCE_WR(chan->vm_pd, (0+4)/4, 0x00000000);
983 if (dev_priv->card_type < NV_50) {
984 ret = nouveau_gpuobj_ref_add(dev, NULL, 0, dev_priv->ramht,
989 ret = nouveau_gpuobj_new_ref(dev, chan, chan, 0,
991 NVOBJ_FLAG_ZERO_ALLOC,
998 if ((ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
999 0, dev_priv->fb_available_size,
1001 NV_DMA_TARGET_VIDMEM, &vram))) {
1002 DRM_ERROR("Error creating VRAM ctxdma: %d\n", ret);
1006 if ((ret = nouveau_gpuobj_ref_add(dev, chan, vram_h, vram, NULL))) {
1007 DRM_ERROR("Error referencing VRAM ctxdma: %d\n", ret);
1011 /* TT memory ctxdma */
1012 if (dev_priv->gart_info.type != NOUVEAU_GART_NONE) {
1013 ret = nouveau_gpuobj_gart_dma_new(chan, 0,
1014 dev_priv->gart_info.aper_size,
1015 NV_DMA_ACCESS_RW, &tt, NULL);
1017 if (dev_priv->pci_heap) {
1018 ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
1019 0, dev->sg->pages * PAGE_SIZE,
1021 NV_DMA_TARGET_PCI_NONLINEAR, &tt);
1023 DRM_ERROR("Invalid GART type %d\n", dev_priv->gart_info.type);
1028 DRM_ERROR("Error creating TT ctxdma: %d\n", ret);
1032 ret = nouveau_gpuobj_ref_add(dev, chan, tt_h, tt, NULL);
1034 DRM_ERROR("Error referencing TT ctxdma: %d\n", ret);
1042 nouveau_gpuobj_channel_takedown(struct nouveau_channel *chan)
1044 struct drm_device *dev = chan->dev;
1045 struct list_head *entry, *tmp;
1046 struct nouveau_gpuobj_ref *ref;
1048 DRM_DEBUG("ch%d\n", chan->id);
1050 list_for_each_safe(entry, tmp, &chan->ramht_refs) {
1051 ref = list_entry(entry, struct nouveau_gpuobj_ref, list);
1053 nouveau_gpuobj_ref_del(dev, &ref);
1056 nouveau_gpuobj_ref_del(dev, &chan->ramht);
1058 nouveau_gpuobj_del(dev, &chan->vm_pd);
1059 nouveau_gpuobj_ref_del(dev, &chan->vm_gart_pt);
1061 if (chan->ramin_heap)
1062 nouveau_mem_takedown(&chan->ramin_heap);
1064 nouveau_gpuobj_ref_del(dev, &chan->ramin);
1068 int nouveau_ioctl_grobj_alloc(struct drm_device *dev, void *data,
1069 struct drm_file *file_priv)
1071 struct nouveau_channel *chan;
1072 struct drm_nouveau_grobj_alloc *init = data;
1073 struct nouveau_gpuobj *gr = NULL;
1076 NOUVEAU_CHECK_INITIALISED_WITH_RETURN;
1077 NOUVEAU_GET_USER_CHANNEL_WITH_RETURN(init->channel, file_priv, chan);
1079 //FIXME: check args, only allow trusted objects to be created
1081 if (init->handle == ~0)
1084 if (nouveau_gpuobj_ref_find(chan, init->handle, NULL) == 0)
1087 ret = nouveau_gpuobj_gr_new(chan, init->class, &gr);
1089 DRM_ERROR("Error creating gr object: %d (%d/0x%08x)\n",
1090 ret, init->channel, init->handle);
1094 if ((ret = nouveau_gpuobj_ref_add(dev, chan, init->handle, gr, NULL))) {
1095 DRM_ERROR("Error referencing gr object: %d (%d/0x%08x\n)",
1096 ret, init->channel, init->handle);
1097 nouveau_gpuobj_del(dev, &gr);
1104 int nouveau_ioctl_gpuobj_free(struct drm_device *dev, void *data,
1105 struct drm_file *file_priv)
1107 struct drm_nouveau_gpuobj_free *objfree = data;
1108 struct nouveau_gpuobj_ref *ref;
1109 struct nouveau_channel *chan;
1112 NOUVEAU_CHECK_INITIALISED_WITH_RETURN;
1113 NOUVEAU_GET_USER_CHANNEL_WITH_RETURN(objfree->channel, file_priv, chan);
1115 if ((ret = nouveau_gpuobj_ref_find(chan, objfree->handle, &ref)))
1117 nouveau_gpuobj_ref_del(dev, &ref);