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"
37 #include "nouveau_ramht.h"
38 #include "nouveau_vm.h"
40 struct nouveau_gpuobj_method {
41 struct list_head head;
43 int (*exec)(struct nouveau_channel *, u32 class, u32 mthd, u32 data);
46 struct nouveau_gpuobj_class {
47 struct list_head head;
48 struct list_head methods;
54 nouveau_gpuobj_class_new(struct drm_device *dev, u32 class, u32 engine)
56 struct drm_nouveau_private *dev_priv = dev->dev_private;
57 struct nouveau_gpuobj_class *oc;
59 oc = kzalloc(sizeof(*oc), GFP_KERNEL);
63 INIT_LIST_HEAD(&oc->methods);
66 list_add(&oc->head, &dev_priv->classes);
71 nouveau_gpuobj_mthd_new(struct drm_device *dev, u32 class, u32 mthd,
72 int (*exec)(struct nouveau_channel *, u32, u32, u32))
74 struct drm_nouveau_private *dev_priv = dev->dev_private;
75 struct nouveau_gpuobj_method *om;
76 struct nouveau_gpuobj_class *oc;
78 list_for_each_entry(oc, &dev_priv->classes, head) {
86 om = kzalloc(sizeof(*om), GFP_KERNEL);
92 list_add(&om->head, &oc->methods);
97 nouveau_gpuobj_mthd_call(struct nouveau_channel *chan,
98 u32 class, u32 mthd, u32 data)
100 struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
101 struct nouveau_gpuobj_method *om;
102 struct nouveau_gpuobj_class *oc;
104 list_for_each_entry(oc, &dev_priv->classes, head) {
108 list_for_each_entry(om, &oc->methods, head) {
109 if (om->mthd == mthd)
110 return om->exec(chan, class, mthd, data);
118 nouveau_gpuobj_mthd_call2(struct drm_device *dev, int chid,
119 u32 class, u32 mthd, u32 data)
121 struct drm_nouveau_private *dev_priv = dev->dev_private;
122 struct nouveau_channel *chan = NULL;
126 spin_lock_irqsave(&dev_priv->channels.lock, flags);
127 if (chid > 0 && chid < dev_priv->engine.fifo.channels)
128 chan = dev_priv->channels.ptr[chid];
130 ret = nouveau_gpuobj_mthd_call(chan, class, mthd, data);
131 spin_unlock_irqrestore(&dev_priv->channels.lock, flags);
135 /* NVidia uses context objects to drive drawing operations.
137 Context objects can be selected into 8 subchannels in the FIFO,
138 and then used via DMA command buffers.
140 A context object is referenced by a user defined handle (CARD32). The HW
141 looks up graphics objects in a hash table in the instance RAM.
143 An entry in the hash table consists of 2 CARD32. The first CARD32 contains
144 the handle, the second one a bitfield, that contains the address of the
145 object in instance RAM.
147 The format of the second CARD32 seems to be:
151 15: 0 instance_addr >> 4
152 17:16 engine (here uses 1 = graphics)
153 28:24 channel id (here uses 0)
158 15: 0 instance_addr >> 4 (maybe 19-0)
159 21:20 engine (here uses 1 = graphics)
160 I'm unsure about the other bits, but using 0 seems to work.
162 The key into the hash table depends on the object handle and channel id and
167 nouveau_gpuobj_new(struct drm_device *dev, struct nouveau_channel *chan,
168 uint32_t size, int align, uint32_t flags,
169 struct nouveau_gpuobj **gpuobj_ret)
171 struct drm_nouveau_private *dev_priv = dev->dev_private;
172 struct nouveau_instmem_engine *instmem = &dev_priv->engine.instmem;
173 struct nouveau_gpuobj *gpuobj;
174 struct drm_mm_node *ramin = NULL;
177 NV_DEBUG(dev, "ch%d size=%u align=%d flags=0x%08x\n",
178 chan ? chan->id : -1, size, align, flags);
180 gpuobj = kzalloc(sizeof(*gpuobj), GFP_KERNEL);
183 NV_DEBUG(dev, "gpuobj %p\n", gpuobj);
185 gpuobj->flags = flags;
186 kref_init(&gpuobj->refcount);
189 spin_lock(&dev_priv->ramin_lock);
190 list_add_tail(&gpuobj->list, &dev_priv->gpuobj_list);
191 spin_unlock(&dev_priv->ramin_lock);
194 ramin = drm_mm_search_free(&chan->ramin_heap, size, align, 0);
196 ramin = drm_mm_get_block(ramin, size, align);
198 nouveau_gpuobj_ref(NULL, &gpuobj);
202 gpuobj->pinst = chan->ramin->pinst;
203 if (gpuobj->pinst != ~0)
204 gpuobj->pinst += ramin->start;
206 gpuobj->cinst = ramin->start;
207 gpuobj->vinst = ramin->start + chan->ramin->vinst;
208 gpuobj->node = ramin;
210 ret = instmem->get(gpuobj, size, align);
212 nouveau_gpuobj_ref(NULL, &gpuobj);
217 if (!(flags & NVOBJ_FLAG_DONT_MAP))
218 ret = instmem->map(gpuobj);
222 gpuobj->cinst = NVOBJ_CINST_GLOBAL;
225 if (gpuobj->flags & NVOBJ_FLAG_ZERO_ALLOC) {
226 for (i = 0; i < gpuobj->size; i += 4)
227 nv_wo32(gpuobj, i, 0);
232 *gpuobj_ret = gpuobj;
237 nouveau_gpuobj_init(struct drm_device *dev)
239 struct drm_nouveau_private *dev_priv = dev->dev_private;
243 INIT_LIST_HEAD(&dev_priv->gpuobj_list);
244 INIT_LIST_HEAD(&dev_priv->classes);
245 spin_lock_init(&dev_priv->ramin_lock);
246 dev_priv->ramin_base = ~0;
252 nouveau_gpuobj_takedown(struct drm_device *dev)
254 struct drm_nouveau_private *dev_priv = dev->dev_private;
255 struct nouveau_gpuobj_method *om, *tm;
256 struct nouveau_gpuobj_class *oc, *tc;
260 list_for_each_entry_safe(oc, tc, &dev_priv->classes, head) {
261 list_for_each_entry_safe(om, tm, &oc->methods, head) {
269 BUG_ON(!list_empty(&dev_priv->gpuobj_list));
274 nouveau_gpuobj_del(struct kref *ref)
276 struct nouveau_gpuobj *gpuobj =
277 container_of(ref, struct nouveau_gpuobj, refcount);
278 struct drm_device *dev = gpuobj->dev;
279 struct drm_nouveau_private *dev_priv = dev->dev_private;
280 struct nouveau_instmem_engine *instmem = &dev_priv->engine.instmem;
283 NV_DEBUG(dev, "gpuobj %p\n", gpuobj);
285 if (gpuobj->node && (gpuobj->flags & NVOBJ_FLAG_ZERO_FREE)) {
286 for (i = 0; i < gpuobj->size; i += 4)
287 nv_wo32(gpuobj, i, 0);
292 gpuobj->dtor(dev, gpuobj);
294 if (gpuobj->cinst == NVOBJ_CINST_GLOBAL) {
296 instmem->unmap(gpuobj);
297 instmem->put(gpuobj);
301 spin_lock(&dev_priv->ramin_lock);
302 drm_mm_put_block(gpuobj->node);
303 spin_unlock(&dev_priv->ramin_lock);
307 spin_lock(&dev_priv->ramin_lock);
308 list_del(&gpuobj->list);
309 spin_unlock(&dev_priv->ramin_lock);
315 nouveau_gpuobj_ref(struct nouveau_gpuobj *ref, struct nouveau_gpuobj **ptr)
318 kref_get(&ref->refcount);
321 kref_put(&(*ptr)->refcount, nouveau_gpuobj_del);
327 nouveau_gpuobj_new_fake(struct drm_device *dev, u32 pinst, u64 vinst,
328 u32 size, u32 flags, struct nouveau_gpuobj **pgpuobj)
330 struct drm_nouveau_private *dev_priv = dev->dev_private;
331 struct nouveau_gpuobj *gpuobj = NULL;
335 "pinst=0x%08x vinst=0x%010llx size=0x%08x flags=0x%08x\n",
336 pinst, vinst, size, flags);
338 gpuobj = kzalloc(sizeof(*gpuobj), GFP_KERNEL);
341 NV_DEBUG(dev, "gpuobj %p\n", gpuobj);
343 gpuobj->flags = flags;
344 kref_init(&gpuobj->refcount);
346 gpuobj->pinst = pinst;
347 gpuobj->cinst = NVOBJ_CINST_GLOBAL;
348 gpuobj->vinst = vinst;
350 if (gpuobj->flags & NVOBJ_FLAG_ZERO_ALLOC) {
351 for (i = 0; i < gpuobj->size; i += 4)
352 nv_wo32(gpuobj, i, 0);
353 dev_priv->engine.instmem.flush(dev);
356 spin_lock(&dev_priv->ramin_lock);
357 list_add_tail(&gpuobj->list, &dev_priv->gpuobj_list);
358 spin_unlock(&dev_priv->ramin_lock);
365 nouveau_gpuobj_class_instmem_size(struct drm_device *dev, int class)
367 struct drm_nouveau_private *dev_priv = dev->dev_private;
369 /*XXX: dodgy hack for now */
370 if (dev_priv->card_type >= NV_50)
372 if (dev_priv->card_type >= NV_40)
378 DMA objects are used to reference a piece of memory in the
379 framebuffer, PCI or AGP address space. Each object is 16 bytes big
380 and looks as follows:
383 11:0 class (seems like I can always use 0 here)
384 12 page table present?
385 13 page entry linear?
386 15:14 access: 0 rw, 1 ro, 2 wo
387 17:16 target: 0 NV memory, 1 NV memory tiled, 2 PCI, 3 AGP
388 31:20 dma adjust (bits 0-11 of the address)
390 dma limit (size of transfer)
392 1 0 readonly, 1 readwrite
393 31:12 dma frame address of the page (bits 12-31 of the address)
395 page table terminator, same value as the first pte, as does nvidia
396 rivatv uses 0xffffffff
398 Non linear page tables need a list of frame addresses afterwards,
399 the rivatv project has some info on this.
401 The method below creates a DMA object in instance RAM and returns a handle
402 to it that can be used to set up context objects.
406 nv50_gpuobj_dma_init(struct nouveau_gpuobj *obj, u32 offset, int class,
407 u64 base, u64 size, int target, int access,
410 struct drm_nouveau_private *dev_priv = obj->dev->dev_private;
411 struct nouveau_instmem_engine *pinstmem = &dev_priv->engine.instmem;
414 flags0 = (comp << 29) | (type << 22) | class;
415 flags0 |= 0x00100000;
418 case NV_MEM_ACCESS_RO: flags0 |= 0x00040000; break;
419 case NV_MEM_ACCESS_RW:
420 case NV_MEM_ACCESS_WO: flags0 |= 0x00080000; break;
426 case NV_MEM_TARGET_VRAM:
427 flags0 |= 0x00010000;
429 case NV_MEM_TARGET_PCI:
430 flags0 |= 0x00020000;
432 case NV_MEM_TARGET_PCI_NOSNOOP:
433 flags0 |= 0x00030000;
435 case NV_MEM_TARGET_GART:
436 base += dev_priv->gart_info.aper_base;
438 flags0 &= ~0x00100000;
442 /* convert to base + limit */
443 size = (base + size) - 1;
445 nv_wo32(obj, offset + 0x00, flags0);
446 nv_wo32(obj, offset + 0x04, lower_32_bits(size));
447 nv_wo32(obj, offset + 0x08, lower_32_bits(base));
448 nv_wo32(obj, offset + 0x0c, upper_32_bits(size) << 24 |
449 upper_32_bits(base));
450 nv_wo32(obj, offset + 0x10, 0x00000000);
451 nv_wo32(obj, offset + 0x14, 0x00000000);
453 pinstmem->flush(obj->dev);
457 nv50_gpuobj_dma_new(struct nouveau_channel *chan, int class, u64 base, u64 size,
458 int target, int access, u32 type, u32 comp,
459 struct nouveau_gpuobj **pobj)
461 struct drm_device *dev = chan->dev;
464 ret = nouveau_gpuobj_new(dev, chan, 24, 16, NVOBJ_FLAG_ZERO_FREE, pobj);
468 nv50_gpuobj_dma_init(*pobj, 0, class, base, size, target,
474 nouveau_gpuobj_dma_new(struct nouveau_channel *chan, int class, u64 base,
475 u64 size, int access, int target,
476 struct nouveau_gpuobj **pobj)
478 struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
479 struct drm_device *dev = chan->dev;
480 struct nouveau_gpuobj *obj;
484 if (dev_priv->card_type >= NV_50) {
485 u32 comp = (target == NV_MEM_TARGET_VM) ? NV_MEM_COMP_VM : 0;
486 u32 type = (target == NV_MEM_TARGET_VM) ? NV_MEM_TYPE_VM : 0;
488 return nv50_gpuobj_dma_new(chan, class, base, size,
489 target, access, type, comp, pobj);
492 if (target == NV_MEM_TARGET_GART) {
493 if (dev_priv->gart_info.type == NOUVEAU_GART_AGP) {
494 target = NV_MEM_TARGET_PCI_NOSNOOP;
495 base += dev_priv->gart_info.aper_base;
498 base = nouveau_sgdma_get_physical(dev, base);
499 target = NV_MEM_TARGET_PCI;
501 nouveau_gpuobj_ref(dev_priv->gart_info.sg_ctxdma, pobj);
507 flags0 |= 0x00003000; /* PT present, PT linear */
511 case NV_MEM_TARGET_PCI:
512 flags0 |= 0x00020000;
514 case NV_MEM_TARGET_PCI_NOSNOOP:
515 flags0 |= 0x00030000;
522 case NV_MEM_ACCESS_RO:
523 flags0 |= 0x00004000;
525 case NV_MEM_ACCESS_WO:
526 flags0 |= 0x00008000;
528 flags2 |= 0x00000002;
532 flags0 |= (base & 0x00000fff) << 20;
533 flags2 |= (base & 0xfffff000);
535 ret = nouveau_gpuobj_new(dev, chan, 16, 16, NVOBJ_FLAG_ZERO_FREE, &obj);
539 nv_wo32(obj, 0x00, flags0);
540 nv_wo32(obj, 0x04, size - 1);
541 nv_wo32(obj, 0x08, flags2);
542 nv_wo32(obj, 0x0c, flags2);
544 obj->engine = NVOBJ_ENGINE_SW;
550 /* Context objects in the instance RAM have the following structure.
551 * On NV40 they are 32 byte long, on NV30 and smaller 16 bytes.
561 scrcopy_and, rop_and, blend_and, scrcopy, srccopy_pre, blend_pre
562 18 synchronize enable
563 19 endian: 1 big, 0 little
565 23 single step enable
566 24 patch status: 0 invalid, 1 valid
567 25 context_surface 0: 1 valid
568 26 context surface 1: 1 valid
569 27 context pattern: 1 valid
570 28 context rop: 1 valid
571 29,30 context beta, beta4
575 31:16 notify instance address
577 15:0 dma 0 instance address
578 31:16 dma 1 instance address
583 No idea what the exact format is. Here's what can be deducted:
586 11:0 class (maybe uses more bits here?)
589 25 patch status valid ?
591 15:0 DMA notifier (maybe 20:0)
593 15:0 DMA 0 instance (maybe 20:0)
596 15:0 DMA 1 instance (maybe 20:0)
602 nouveau_gpuobj_sw_new(struct nouveau_channel *chan, int class,
603 struct nouveau_gpuobj **gpuobj_ret)
605 struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
606 struct nouveau_gpuobj *gpuobj;
608 gpuobj = kzalloc(sizeof(*gpuobj), GFP_KERNEL);
611 gpuobj->dev = chan->dev;
612 gpuobj->engine = NVOBJ_ENGINE_SW;
613 gpuobj->class = class;
614 kref_init(&gpuobj->refcount);
615 gpuobj->cinst = 0x40;
617 spin_lock(&dev_priv->ramin_lock);
618 list_add_tail(&gpuobj->list, &dev_priv->gpuobj_list);
619 spin_unlock(&dev_priv->ramin_lock);
620 *gpuobj_ret = gpuobj;
625 nouveau_gpuobj_gr_new(struct nouveau_channel *chan, u32 handle, int class)
627 struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
628 struct drm_device *dev = chan->dev;
629 struct nouveau_gpuobj_class *oc;
630 struct nouveau_gpuobj *gpuobj;
633 NV_DEBUG(dev, "ch%d class=0x%04x\n", chan->id, class);
635 list_for_each_entry(oc, &dev_priv->classes, head) {
640 NV_ERROR(dev, "illegal object class: 0x%x\n", class);
644 switch (oc->engine) {
645 case NVOBJ_ENGINE_SW:
646 if (dev_priv->card_type < NV_C0) {
647 ret = nouveau_gpuobj_sw_new(chan, class, &gpuobj);
653 case NVOBJ_ENGINE_GR:
654 if ((dev_priv->card_type >= NV_20 && !chan->ramin_grctx) ||
655 (dev_priv->card_type < NV_20 && !chan->pgraph_ctx)) {
656 struct nouveau_pgraph_engine *pgraph =
657 &dev_priv->engine.graph;
659 ret = pgraph->create_context(chan);
664 case NVOBJ_ENGINE_CRYPT:
665 if (!chan->crypt_ctx) {
666 struct nouveau_crypt_engine *pcrypt =
667 &dev_priv->engine.crypt;
669 ret = pcrypt->create_context(chan);
676 /* we're done if this is fermi */
677 if (dev_priv->card_type >= NV_C0)
680 ret = nouveau_gpuobj_new(dev, chan,
681 nouveau_gpuobj_class_instmem_size(dev, class),
683 NVOBJ_FLAG_ZERO_ALLOC | NVOBJ_FLAG_ZERO_FREE,
686 NV_ERROR(dev, "error creating gpuobj: %d\n", ret);
690 if (dev_priv->card_type >= NV_50) {
691 nv_wo32(gpuobj, 0, class);
692 nv_wo32(gpuobj, 20, 0x00010000);
696 nv_wo32(gpuobj, 0, 0x00001030);
697 nv_wo32(gpuobj, 4, 0xFFFFFFFF);
700 if (dev_priv->card_type >= NV_40) {
701 nv_wo32(gpuobj, 0, class);
703 nv_wo32(gpuobj, 8, 0x01000000);
707 nv_wo32(gpuobj, 0, class | 0x00080000);
709 nv_wo32(gpuobj, 0, class);
714 dev_priv->engine.instmem.flush(dev);
716 gpuobj->engine = oc->engine;
717 gpuobj->class = oc->id;
720 ret = nouveau_ramht_insert(chan, handle, gpuobj);
722 NV_ERROR(dev, "error adding gpuobj to RAMHT: %d\n", ret);
723 nouveau_gpuobj_ref(NULL, &gpuobj);
728 nouveau_gpuobj_channel_init_pramin(struct nouveau_channel *chan)
730 struct drm_device *dev = chan->dev;
731 struct drm_nouveau_private *dev_priv = dev->dev_private;
736 NV_DEBUG(dev, "ch%d\n", chan->id);
738 /* Base amount for object storage (4KiB enough?) */
743 size += dev_priv->engine.graph.grctx_size;
745 if (dev_priv->card_type == NV_50) {
746 /* Various fixed table thingos */
747 size += 0x1400; /* mostly unknown stuff */
748 size += 0x4000; /* vm pd */
750 /* RAMHT, not sure about setting size yet, 32KiB to be safe */
756 ret = nouveau_gpuobj_new(dev, NULL, size, 0x1000, 0, &chan->ramin);
758 NV_ERROR(dev, "Error allocating channel PRAMIN: %d\n", ret);
762 ret = drm_mm_init(&chan->ramin_heap, base, size);
764 NV_ERROR(dev, "Error creating PRAMIN heap: %d\n", ret);
765 nouveau_gpuobj_ref(NULL, &chan->ramin);
773 nouveau_gpuobj_channel_init(struct nouveau_channel *chan,
774 uint32_t vram_h, uint32_t tt_h)
776 struct drm_device *dev = chan->dev;
777 struct drm_nouveau_private *dev_priv = dev->dev_private;
778 struct nouveau_gpuobj *vram = NULL, *tt = NULL;
781 NV_DEBUG(dev, "ch%d vram=0x%08x tt=0x%08x\n", chan->id, vram_h, tt_h);
783 if (dev_priv->card_type == NV_C0) {
784 struct nouveau_vm *vm = dev_priv->chan_vm;
785 struct nouveau_vm_pgd *vpgd;
787 ret = nouveau_gpuobj_new(dev, NULL, 4096, 0x1000, 0,
792 nouveau_vm_ref(vm, &chan->vm, NULL);
794 vpgd = list_first_entry(&vm->pgd_list, struct nouveau_vm_pgd, head);
795 nv_wo32(chan->ramin, 0x0200, lower_32_bits(vpgd->obj->vinst));
796 nv_wo32(chan->ramin, 0x0204, upper_32_bits(vpgd->obj->vinst));
797 nv_wo32(chan->ramin, 0x0208, 0xffffffff);
798 nv_wo32(chan->ramin, 0x020c, 0x000000ff);
802 /* Allocate a chunk of memory for per-channel object storage */
803 ret = nouveau_gpuobj_channel_init_pramin(chan);
805 NV_ERROR(dev, "init pramin\n");
810 * - Allocate per-channel page-directory
811 * - Link with shared channel VM
813 if (dev_priv->chan_vm) {
814 u32 pgd_offs = (dev_priv->chipset == 0x50) ? 0x1400 : 0x0200;
815 u64 vm_vinst = chan->ramin->vinst + pgd_offs;
816 u32 vm_pinst = chan->ramin->pinst;
819 vm_pinst += pgd_offs;
821 ret = nouveau_gpuobj_new_fake(dev, vm_pinst, vm_vinst, 0x4000,
826 nouveau_vm_ref(dev_priv->chan_vm, &chan->vm, chan->vm_pd);
830 if (dev_priv->card_type < NV_50) {
831 nouveau_ramht_ref(dev_priv->ramht, &chan->ramht, NULL);
833 struct nouveau_gpuobj *ramht = NULL;
835 ret = nouveau_gpuobj_new(dev, chan, 0x8000, 16,
836 NVOBJ_FLAG_ZERO_ALLOC, &ramht);
840 ret = nouveau_ramht_new(dev, ramht, &chan->ramht);
841 nouveau_gpuobj_ref(NULL, &ramht);
847 if (dev_priv->card_type >= NV_50) {
848 ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
849 0, (1ULL << 40), NV_MEM_ACCESS_RW,
850 NV_MEM_TARGET_VM, &vram);
852 NV_ERROR(dev, "Error creating VRAM ctxdma: %d\n", ret);
856 ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
857 0, dev_priv->fb_available_size,
859 NV_MEM_TARGET_VRAM, &vram);
861 NV_ERROR(dev, "Error creating VRAM ctxdma: %d\n", ret);
866 ret = nouveau_ramht_insert(chan, vram_h, vram);
867 nouveau_gpuobj_ref(NULL, &vram);
869 NV_ERROR(dev, "Error adding VRAM ctxdma to RAMHT: %d\n", ret);
873 /* TT memory ctxdma */
874 if (dev_priv->card_type >= NV_50) {
875 ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
876 0, (1ULL << 40), NV_MEM_ACCESS_RW,
877 NV_MEM_TARGET_VM, &tt);
879 ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
880 0, dev_priv->gart_info.aper_size,
882 NV_MEM_TARGET_GART, &tt);
886 NV_ERROR(dev, "Error creating TT ctxdma: %d\n", ret);
890 ret = nouveau_ramht_insert(chan, tt_h, tt);
891 nouveau_gpuobj_ref(NULL, &tt);
893 NV_ERROR(dev, "Error adding TT ctxdma to RAMHT: %d\n", ret);
901 nouveau_gpuobj_channel_takedown(struct nouveau_channel *chan)
903 struct drm_device *dev = chan->dev;
905 NV_DEBUG(dev, "ch%d\n", chan->id);
907 nouveau_ramht_ref(NULL, &chan->ramht, chan);
909 nouveau_vm_ref(NULL, &chan->vm, chan->vm_pd);
910 nouveau_gpuobj_ref(NULL, &chan->vm_pd);
912 if (chan->ramin_heap.free_stack.next)
913 drm_mm_takedown(&chan->ramin_heap);
914 nouveau_gpuobj_ref(NULL, &chan->ramin);
918 nouveau_gpuobj_suspend(struct drm_device *dev)
920 struct drm_nouveau_private *dev_priv = dev->dev_private;
921 struct nouveau_gpuobj *gpuobj;
924 list_for_each_entry(gpuobj, &dev_priv->gpuobj_list, list) {
925 if (gpuobj->cinst != NVOBJ_CINST_GLOBAL)
928 gpuobj->suspend = vmalloc(gpuobj->size);
929 if (!gpuobj->suspend) {
930 nouveau_gpuobj_resume(dev);
934 for (i = 0; i < gpuobj->size; i += 4)
935 gpuobj->suspend[i/4] = nv_ro32(gpuobj, i);
942 nouveau_gpuobj_resume(struct drm_device *dev)
944 struct drm_nouveau_private *dev_priv = dev->dev_private;
945 struct nouveau_gpuobj *gpuobj;
948 list_for_each_entry(gpuobj, &dev_priv->gpuobj_list, list) {
949 if (!gpuobj->suspend)
952 for (i = 0; i < gpuobj->size; i += 4)
953 nv_wo32(gpuobj, i, gpuobj->suspend[i/4]);
955 vfree(gpuobj->suspend);
956 gpuobj->suspend = NULL;
959 dev_priv->engine.instmem.flush(dev);
962 int nouveau_ioctl_grobj_alloc(struct drm_device *dev, void *data,
963 struct drm_file *file_priv)
965 struct drm_nouveau_grobj_alloc *init = data;
966 struct nouveau_channel *chan;
969 if (init->handle == ~0)
972 chan = nouveau_channel_get(dev, file_priv, init->channel);
974 return PTR_ERR(chan);
976 if (nouveau_ramht_find(chan, init->handle)) {
981 ret = nouveau_gpuobj_gr_new(chan, init->handle, init->class);
983 NV_ERROR(dev, "Error creating object: %d (%d/0x%08x)\n",
984 ret, init->channel, init->handle);
988 nouveau_channel_put(&chan);
992 int nouveau_ioctl_gpuobj_free(struct drm_device *dev, void *data,
993 struct drm_file *file_priv)
995 struct drm_nouveau_gpuobj_free *objfree = data;
996 struct nouveau_channel *chan;
999 chan = nouveau_channel_get(dev, file_priv, objfree->channel);
1001 return PTR_ERR(chan);
1003 /* Synchronize with the user channel */
1004 nouveau_channel_idle(chan);
1006 ret = nouveau_ramht_remove(chan, objfree->handle);
1007 nouveau_channel_put(&chan);
1012 nv_ro32(struct nouveau_gpuobj *gpuobj, u32 offset)
1014 struct drm_nouveau_private *dev_priv = gpuobj->dev->dev_private;
1015 struct drm_device *dev = gpuobj->dev;
1017 if (gpuobj->pinst == ~0 || !dev_priv->ramin_available) {
1018 u64 ptr = gpuobj->vinst + offset;
1019 u32 base = ptr >> 16;
1022 spin_lock(&dev_priv->ramin_lock);
1023 if (dev_priv->ramin_base != base) {
1024 dev_priv->ramin_base = base;
1025 nv_wr32(dev, 0x001700, dev_priv->ramin_base);
1027 val = nv_rd32(dev, 0x700000 + (ptr & 0xffff));
1028 spin_unlock(&dev_priv->ramin_lock);
1032 return nv_ri32(dev, gpuobj->pinst + offset);
1036 nv_wo32(struct nouveau_gpuobj *gpuobj, u32 offset, u32 val)
1038 struct drm_nouveau_private *dev_priv = gpuobj->dev->dev_private;
1039 struct drm_device *dev = gpuobj->dev;
1041 if (gpuobj->pinst == ~0 || !dev_priv->ramin_available) {
1042 u64 ptr = gpuobj->vinst + offset;
1043 u32 base = ptr >> 16;
1045 spin_lock(&dev_priv->ramin_lock);
1046 if (dev_priv->ramin_base != base) {
1047 dev_priv->ramin_base = base;
1048 nv_wr32(dev, 0x001700, dev_priv->ramin_base);
1050 nv_wr32(dev, 0x700000 + (ptr & 0xffff), val);
1051 spin_unlock(&dev_priv->ramin_lock);
1055 nv_wi32(dev, gpuobj->pinst + offset, val);