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"
39 struct nouveau_gpuobj_method {
40 struct list_head head;
42 int (*exec)(struct nouveau_channel *, u32 class, u32 mthd, u32 data);
45 struct nouveau_gpuobj_class {
46 struct list_head head;
47 struct list_head methods;
53 nouveau_gpuobj_class_new(struct drm_device *dev, u32 class, u32 engine)
55 struct drm_nouveau_private *dev_priv = dev->dev_private;
56 struct nouveau_gpuobj_class *oc;
58 oc = kzalloc(sizeof(*oc), GFP_KERNEL);
62 INIT_LIST_HEAD(&oc->methods);
65 list_add(&oc->head, &dev_priv->classes);
70 nouveau_gpuobj_mthd_new(struct drm_device *dev, u32 class, u32 mthd,
71 int (*exec)(struct nouveau_channel *, u32, u32, u32))
73 struct drm_nouveau_private *dev_priv = dev->dev_private;
74 struct nouveau_gpuobj_method *om;
75 struct nouveau_gpuobj_class *oc;
77 list_for_each_entry(oc, &dev_priv->classes, head) {
85 om = kzalloc(sizeof(*om), GFP_KERNEL);
91 list_add(&om->head, &oc->methods);
96 nouveau_gpuobj_mthd_call(struct nouveau_channel *chan,
97 u32 class, u32 mthd, u32 data)
99 struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
100 struct nouveau_gpuobj_method *om;
101 struct nouveau_gpuobj_class *oc;
103 list_for_each_entry(oc, &dev_priv->classes, head) {
107 list_for_each_entry(om, &oc->methods, head) {
108 if (om->mthd == mthd)
109 return om->exec(chan, class, mthd, data);
116 /* NVidia uses context objects to drive drawing operations.
118 Context objects can be selected into 8 subchannels in the FIFO,
119 and then used via DMA command buffers.
121 A context object is referenced by a user defined handle (CARD32). The HW
122 looks up graphics objects in a hash table in the instance RAM.
124 An entry in the hash table consists of 2 CARD32. The first CARD32 contains
125 the handle, the second one a bitfield, that contains the address of the
126 object in instance RAM.
128 The format of the second CARD32 seems to be:
132 15: 0 instance_addr >> 4
133 17:16 engine (here uses 1 = graphics)
134 28:24 channel id (here uses 0)
139 15: 0 instance_addr >> 4 (maybe 19-0)
140 21:20 engine (here uses 1 = graphics)
141 I'm unsure about the other bits, but using 0 seems to work.
143 The key into the hash table depends on the object handle and channel id and
148 nouveau_gpuobj_new(struct drm_device *dev, struct nouveau_channel *chan,
149 uint32_t size, int align, uint32_t flags,
150 struct nouveau_gpuobj **gpuobj_ret)
152 struct drm_nouveau_private *dev_priv = dev->dev_private;
153 struct nouveau_engine *engine = &dev_priv->engine;
154 struct nouveau_gpuobj *gpuobj;
155 struct drm_mm_node *ramin = NULL;
158 NV_DEBUG(dev, "ch%d size=%u align=%d flags=0x%08x\n",
159 chan ? chan->id : -1, size, align, flags);
161 if (!dev_priv || !gpuobj_ret || *gpuobj_ret != NULL)
164 gpuobj = kzalloc(sizeof(*gpuobj), GFP_KERNEL);
167 NV_DEBUG(dev, "gpuobj %p\n", gpuobj);
169 gpuobj->flags = flags;
170 kref_init(&gpuobj->refcount);
173 spin_lock(&dev_priv->ramin_lock);
174 list_add_tail(&gpuobj->list, &dev_priv->gpuobj_list);
175 spin_unlock(&dev_priv->ramin_lock);
178 NV_DEBUG(dev, "channel heap\n");
180 ramin = drm_mm_search_free(&chan->ramin_heap, size, align, 0);
182 ramin = drm_mm_get_block(ramin, size, align);
185 nouveau_gpuobj_ref(NULL, &gpuobj);
189 NV_DEBUG(dev, "global heap\n");
191 /* allocate backing pages, sets vinst */
192 ret = engine->instmem.populate(dev, gpuobj, &size, align);
194 nouveau_gpuobj_ref(NULL, &gpuobj);
198 /* try and get aperture space */
200 if (drm_mm_pre_get(&dev_priv->ramin_heap))
203 spin_lock(&dev_priv->ramin_lock);
204 ramin = drm_mm_search_free(&dev_priv->ramin_heap, size,
207 spin_unlock(&dev_priv->ramin_lock);
208 nouveau_gpuobj_ref(NULL, &gpuobj);
212 ramin = drm_mm_get_block_atomic(ramin, size, align);
213 spin_unlock(&dev_priv->ramin_lock);
214 } while (ramin == NULL);
216 /* on nv50 it's ok to fail, we have a fallback path */
217 if (!ramin && dev_priv->card_type < NV_50) {
218 nouveau_gpuobj_ref(NULL, &gpuobj);
223 /* if we got a chunk of the aperture, map pages into it */
224 gpuobj->im_pramin = ramin;
225 if (!chan && gpuobj->im_pramin && dev_priv->ramin_available) {
226 ret = engine->instmem.bind(dev, gpuobj);
228 nouveau_gpuobj_ref(NULL, &gpuobj);
233 /* calculate the various different addresses for the object */
235 gpuobj->pinst = chan->ramin->pinst;
236 if (gpuobj->pinst != ~0)
237 gpuobj->pinst += gpuobj->im_pramin->start;
239 if (dev_priv->card_type < NV_50) {
240 gpuobj->cinst = gpuobj->pinst;
242 gpuobj->cinst = gpuobj->im_pramin->start;
243 gpuobj->vinst = gpuobj->im_pramin->start +
247 if (gpuobj->im_pramin)
248 gpuobj->pinst = gpuobj->im_pramin->start;
251 gpuobj->cinst = 0xdeadbeef;
254 if (gpuobj->flags & NVOBJ_FLAG_ZERO_ALLOC) {
257 for (i = 0; i < gpuobj->size; i += 4)
258 nv_wo32(gpuobj, i, 0);
259 engine->instmem.flush(dev);
263 *gpuobj_ret = gpuobj;
268 nouveau_gpuobj_init(struct drm_device *dev)
270 struct drm_nouveau_private *dev_priv = dev->dev_private;
274 INIT_LIST_HEAD(&dev_priv->gpuobj_list);
275 INIT_LIST_HEAD(&dev_priv->classes);
276 spin_lock_init(&dev_priv->ramin_lock);
277 dev_priv->ramin_base = ~0;
283 nouveau_gpuobj_takedown(struct drm_device *dev)
285 struct drm_nouveau_private *dev_priv = dev->dev_private;
286 struct nouveau_gpuobj_method *om, *tm;
287 struct nouveau_gpuobj_class *oc, *tc;
291 list_for_each_entry_safe(oc, tc, &dev_priv->classes, head) {
292 list_for_each_entry_safe(om, tm, &oc->methods, head) {
300 BUG_ON(!list_empty(&dev_priv->gpuobj_list));
305 nouveau_gpuobj_del(struct kref *ref)
307 struct nouveau_gpuobj *gpuobj =
308 container_of(ref, struct nouveau_gpuobj, refcount);
309 struct drm_device *dev = gpuobj->dev;
310 struct drm_nouveau_private *dev_priv = dev->dev_private;
311 struct nouveau_engine *engine = &dev_priv->engine;
314 NV_DEBUG(dev, "gpuobj %p\n", gpuobj);
316 if (gpuobj->im_pramin && (gpuobj->flags & NVOBJ_FLAG_ZERO_FREE)) {
317 for (i = 0; i < gpuobj->size; i += 4)
318 nv_wo32(gpuobj, i, 0);
319 engine->instmem.flush(dev);
323 gpuobj->dtor(dev, gpuobj);
325 if (gpuobj->im_backing)
326 engine->instmem.clear(dev, gpuobj);
328 spin_lock(&dev_priv->ramin_lock);
329 if (gpuobj->im_pramin)
330 drm_mm_put_block(gpuobj->im_pramin);
331 list_del(&gpuobj->list);
332 spin_unlock(&dev_priv->ramin_lock);
338 nouveau_gpuobj_ref(struct nouveau_gpuobj *ref, struct nouveau_gpuobj **ptr)
341 kref_get(&ref->refcount);
344 kref_put(&(*ptr)->refcount, nouveau_gpuobj_del);
350 nouveau_gpuobj_new_fake(struct drm_device *dev, u32 pinst, u64 vinst,
351 u32 size, u32 flags, struct nouveau_gpuobj **pgpuobj)
353 struct drm_nouveau_private *dev_priv = dev->dev_private;
354 struct nouveau_gpuobj *gpuobj = NULL;
358 "pinst=0x%08x vinst=0x%010llx size=0x%08x flags=0x%08x\n",
359 pinst, vinst, size, flags);
361 gpuobj = kzalloc(sizeof(*gpuobj), GFP_KERNEL);
364 NV_DEBUG(dev, "gpuobj %p\n", gpuobj);
366 gpuobj->flags = flags;
367 kref_init(&gpuobj->refcount);
369 gpuobj->pinst = pinst;
370 gpuobj->cinst = 0xdeadbeef;
371 gpuobj->vinst = vinst;
373 if (gpuobj->flags & NVOBJ_FLAG_ZERO_ALLOC) {
374 for (i = 0; i < gpuobj->size; i += 4)
375 nv_wo32(gpuobj, i, 0);
376 dev_priv->engine.instmem.flush(dev);
379 spin_lock(&dev_priv->ramin_lock);
380 list_add_tail(&gpuobj->list, &dev_priv->gpuobj_list);
381 spin_unlock(&dev_priv->ramin_lock);
388 nouveau_gpuobj_class_instmem_size(struct drm_device *dev, int class)
390 struct drm_nouveau_private *dev_priv = dev->dev_private;
392 /*XXX: dodgy hack for now */
393 if (dev_priv->card_type >= NV_50)
395 if (dev_priv->card_type >= NV_40)
401 DMA objects are used to reference a piece of memory in the
402 framebuffer, PCI or AGP address space. Each object is 16 bytes big
403 and looks as follows:
406 11:0 class (seems like I can always use 0 here)
407 12 page table present?
408 13 page entry linear?
409 15:14 access: 0 rw, 1 ro, 2 wo
410 17:16 target: 0 NV memory, 1 NV memory tiled, 2 PCI, 3 AGP
411 31:20 dma adjust (bits 0-11 of the address)
413 dma limit (size of transfer)
415 1 0 readonly, 1 readwrite
416 31:12 dma frame address of the page (bits 12-31 of the address)
418 page table terminator, same value as the first pte, as does nvidia
419 rivatv uses 0xffffffff
421 Non linear page tables need a list of frame addresses afterwards,
422 the rivatv project has some info on this.
424 The method below creates a DMA object in instance RAM and returns a handle
425 to it that can be used to set up context objects.
428 nouveau_gpuobj_dma_new(struct nouveau_channel *chan, int class,
429 uint64_t offset, uint64_t size, int access,
430 int target, struct nouveau_gpuobj **gpuobj)
432 struct drm_device *dev = chan->dev;
433 struct drm_nouveau_private *dev_priv = dev->dev_private;
434 struct nouveau_instmem_engine *instmem = &dev_priv->engine.instmem;
437 NV_DEBUG(dev, "ch%d class=0x%04x offset=0x%llx size=0x%llx\n",
438 chan->id, class, offset, size);
439 NV_DEBUG(dev, "access=%d target=%d\n", access, target);
442 case NV_DMA_TARGET_AGP:
443 offset += dev_priv->gart_info.aper_base;
449 ret = nouveau_gpuobj_new(dev, chan,
450 nouveau_gpuobj_class_instmem_size(dev, class),
451 16, NVOBJ_FLAG_ZERO_ALLOC |
452 NVOBJ_FLAG_ZERO_FREE, gpuobj);
454 NV_ERROR(dev, "Error creating gpuobj: %d\n", ret);
458 if (dev_priv->card_type < NV_50) {
459 uint32_t frame, adjust, pte_flags = 0;
461 if (access != NV_DMA_ACCESS_RO)
463 adjust = offset & 0x00000fff;
464 frame = offset & ~0x00000fff;
466 nv_wo32(*gpuobj, 0, ((1<<12) | (1<<13) | (adjust << 20) |
467 (access << 14) | (target << 16) |
469 nv_wo32(*gpuobj, 4, size - 1);
470 nv_wo32(*gpuobj, 8, frame | pte_flags);
471 nv_wo32(*gpuobj, 12, frame | pte_flags);
473 uint64_t limit = offset + size - 1;
474 uint32_t flags0, flags5;
476 if (target == NV_DMA_TARGET_VIDMEM) {
484 nv_wo32(*gpuobj, 0, flags0 | class);
485 nv_wo32(*gpuobj, 4, lower_32_bits(limit));
486 nv_wo32(*gpuobj, 8, lower_32_bits(offset));
487 nv_wo32(*gpuobj, 12, ((upper_32_bits(limit) & 0xff) << 24) |
488 (upper_32_bits(offset) & 0xff));
489 nv_wo32(*gpuobj, 20, flags5);
494 (*gpuobj)->engine = NVOBJ_ENGINE_SW;
495 (*gpuobj)->class = class;
500 nouveau_gpuobj_gart_dma_new(struct nouveau_channel *chan,
501 uint64_t offset, uint64_t size, int access,
502 struct nouveau_gpuobj **gpuobj,
505 struct drm_device *dev = chan->dev;
506 struct drm_nouveau_private *dev_priv = dev->dev_private;
509 if (dev_priv->gart_info.type == NOUVEAU_GART_AGP ||
510 (dev_priv->card_type >= NV_50 &&
511 dev_priv->gart_info.type == NOUVEAU_GART_SGDMA)) {
512 ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
513 offset + dev_priv->vm_gart_base,
514 size, access, NV_DMA_TARGET_AGP,
519 if (dev_priv->gart_info.type == NOUVEAU_GART_SGDMA) {
520 nouveau_gpuobj_ref(dev_priv->gart_info.sg_ctxdma, gpuobj);
521 if (offset & ~0xffffffffULL) {
522 NV_ERROR(dev, "obj offset exceeds 32-bits\n");
526 *o_ret = (uint32_t)offset;
527 ret = (*gpuobj != NULL) ? 0 : -EINVAL;
529 NV_ERROR(dev, "Invalid GART type %d\n", dev_priv->gart_info.type);
536 /* Context objects in the instance RAM have the following structure.
537 * On NV40 they are 32 byte long, on NV30 and smaller 16 bytes.
547 scrcopy_and, rop_and, blend_and, scrcopy, srccopy_pre, blend_pre
548 18 synchronize enable
549 19 endian: 1 big, 0 little
551 23 single step enable
552 24 patch status: 0 invalid, 1 valid
553 25 context_surface 0: 1 valid
554 26 context surface 1: 1 valid
555 27 context pattern: 1 valid
556 28 context rop: 1 valid
557 29,30 context beta, beta4
561 31:16 notify instance address
563 15:0 dma 0 instance address
564 31:16 dma 1 instance address
569 No idea what the exact format is. Here's what can be deducted:
572 11:0 class (maybe uses more bits here?)
575 25 patch status valid ?
577 15:0 DMA notifier (maybe 20:0)
579 15:0 DMA 0 instance (maybe 20:0)
582 15:0 DMA 1 instance (maybe 20:0)
588 nouveau_gpuobj_sw_new(struct nouveau_channel *chan, int class,
589 struct nouveau_gpuobj **gpuobj_ret)
591 struct drm_nouveau_private *dev_priv;
592 struct nouveau_gpuobj *gpuobj;
594 if (!chan || !gpuobj_ret || *gpuobj_ret != NULL)
596 dev_priv = chan->dev->dev_private;
598 gpuobj = kzalloc(sizeof(*gpuobj), GFP_KERNEL);
601 gpuobj->dev = chan->dev;
602 gpuobj->engine = NVOBJ_ENGINE_SW;
603 gpuobj->class = class;
604 kref_init(&gpuobj->refcount);
605 gpuobj->cinst = 0x40;
607 spin_lock(&dev_priv->ramin_lock);
608 list_add_tail(&gpuobj->list, &dev_priv->gpuobj_list);
609 spin_unlock(&dev_priv->ramin_lock);
610 *gpuobj_ret = gpuobj;
615 nouveau_gpuobj_gr_new(struct nouveau_channel *chan, int class,
616 struct nouveau_gpuobj **gpuobj)
618 struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
619 struct drm_device *dev = chan->dev;
620 struct nouveau_gpuobj_class *oc;
623 NV_DEBUG(dev, "ch%d class=0x%04x\n", chan->id, class);
625 list_for_each_entry(oc, &dev_priv->classes, head) {
630 NV_ERROR(dev, "illegal object class: 0x%x\n", class);
634 if (oc->engine == NVOBJ_ENGINE_SW)
635 return nouveau_gpuobj_sw_new(chan, class, gpuobj);
637 switch (oc->engine) {
638 case NVOBJ_ENGINE_GR:
639 if (dev_priv->card_type >= NV_50 && !chan->ramin_grctx) {
640 struct nouveau_pgraph_engine *pgraph =
641 &dev_priv->engine.graph;
643 ret = pgraph->create_context(chan);
648 case NVOBJ_ENGINE_CRYPT:
649 if (!chan->crypt_ctx) {
650 struct nouveau_crypt_engine *pcrypt =
651 &dev_priv->engine.crypt;
653 ret = pcrypt->create_context(chan);
660 ret = nouveau_gpuobj_new(dev, chan,
661 nouveau_gpuobj_class_instmem_size(dev, class),
663 NVOBJ_FLAG_ZERO_ALLOC | NVOBJ_FLAG_ZERO_FREE,
666 NV_ERROR(dev, "error creating gpuobj: %d\n", ret);
670 if (dev_priv->card_type >= NV_50) {
671 nv_wo32(*gpuobj, 0, class);
672 nv_wo32(*gpuobj, 20, 0x00010000);
676 nv_wo32(*gpuobj, 0, 0x00001030);
677 nv_wo32(*gpuobj, 4, 0xFFFFFFFF);
680 if (dev_priv->card_type >= NV_40) {
681 nv_wo32(*gpuobj, 0, class);
683 nv_wo32(*gpuobj, 8, 0x01000000);
687 nv_wo32(*gpuobj, 0, class | 0x00080000);
689 nv_wo32(*gpuobj, 0, class);
694 dev_priv->engine.instmem.flush(dev);
696 (*gpuobj)->engine = oc->engine;
697 (*gpuobj)->class = oc->id;
702 nouveau_gpuobj_channel_init_pramin(struct nouveau_channel *chan)
704 struct drm_device *dev = chan->dev;
705 struct drm_nouveau_private *dev_priv = dev->dev_private;
710 NV_DEBUG(dev, "ch%d\n", chan->id);
712 /* Base amount for object storage (4KiB enough?) */
717 size += dev_priv->engine.graph.grctx_size;
719 if (dev_priv->card_type == NV_50) {
720 /* Various fixed table thingos */
721 size += 0x1400; /* mostly unknown stuff */
722 size += 0x4000; /* vm pd */
724 /* RAMHT, not sure about setting size yet, 32KiB to be safe */
730 ret = nouveau_gpuobj_new(dev, NULL, size, 0x1000, 0, &chan->ramin);
732 NV_ERROR(dev, "Error allocating channel PRAMIN: %d\n", ret);
736 ret = drm_mm_init(&chan->ramin_heap, base, size);
738 NV_ERROR(dev, "Error creating PRAMIN heap: %d\n", ret);
739 nouveau_gpuobj_ref(NULL, &chan->ramin);
747 nouveau_gpuobj_channel_init(struct nouveau_channel *chan,
748 uint32_t vram_h, uint32_t tt_h)
750 struct drm_device *dev = chan->dev;
751 struct drm_nouveau_private *dev_priv = dev->dev_private;
752 struct nouveau_instmem_engine *instmem = &dev_priv->engine.instmem;
753 struct nouveau_gpuobj *vram = NULL, *tt = NULL;
756 NV_DEBUG(dev, "ch%d vram=0x%08x tt=0x%08x\n", chan->id, vram_h, tt_h);
758 /* Allocate a chunk of memory for per-channel object storage */
759 ret = nouveau_gpuobj_channel_init_pramin(chan);
761 NV_ERROR(dev, "init pramin\n");
766 * - Allocate per-channel page-directory
767 * - Map GART and VRAM into the channel's address space at the
768 * locations determined during init.
770 if (dev_priv->card_type >= NV_50) {
771 u32 pgd_offs = (dev_priv->chipset == 0x50) ? 0x1400 : 0x0200;
772 u64 vm_vinst = chan->ramin->vinst + pgd_offs;
773 u32 vm_pinst = chan->ramin->pinst;
777 vm_pinst += pgd_offs;
779 ret = nouveau_gpuobj_new_fake(dev, vm_pinst, vm_vinst, 0x4000,
783 for (i = 0; i < 0x4000; i += 8) {
784 nv_wo32(chan->vm_pd, i + 0, 0x00000000);
785 nv_wo32(chan->vm_pd, i + 4, 0xdeadcafe);
788 nouveau_gpuobj_ref(dev_priv->gart_info.sg_ctxdma,
790 pde = (dev_priv->vm_gart_base / (512*1024*1024)) * 8;
791 nv_wo32(chan->vm_pd, pde + 0, chan->vm_gart_pt->vinst | 3);
792 nv_wo32(chan->vm_pd, pde + 4, 0x00000000);
794 pde = (dev_priv->vm_vram_base / (512*1024*1024)) * 8;
795 for (i = 0; i < dev_priv->vm_vram_pt_nr; i++) {
796 nouveau_gpuobj_ref(dev_priv->vm_vram_pt[i],
797 &chan->vm_vram_pt[i]);
799 nv_wo32(chan->vm_pd, pde + 0,
800 chan->vm_vram_pt[i]->vinst | 0x61);
801 nv_wo32(chan->vm_pd, pde + 4, 0x00000000);
809 if (dev_priv->card_type < NV_50) {
810 nouveau_ramht_ref(dev_priv->ramht, &chan->ramht, NULL);
812 struct nouveau_gpuobj *ramht = NULL;
814 ret = nouveau_gpuobj_new(dev, chan, 0x8000, 16,
815 NVOBJ_FLAG_ZERO_ALLOC, &ramht);
819 ret = nouveau_ramht_new(dev, ramht, &chan->ramht);
820 nouveau_gpuobj_ref(NULL, &ramht);
826 if (dev_priv->card_type >= NV_50) {
827 ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
830 NV_DMA_TARGET_AGP, &vram);
832 NV_ERROR(dev, "Error creating VRAM ctxdma: %d\n", ret);
836 ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
837 0, dev_priv->fb_available_size,
839 NV_DMA_TARGET_VIDMEM, &vram);
841 NV_ERROR(dev, "Error creating VRAM ctxdma: %d\n", ret);
846 ret = nouveau_ramht_insert(chan, vram_h, vram);
847 nouveau_gpuobj_ref(NULL, &vram);
849 NV_ERROR(dev, "Error adding VRAM ctxdma to RAMHT: %d\n", ret);
853 /* TT memory ctxdma */
854 if (dev_priv->card_type >= NV_50) {
855 ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
858 NV_DMA_TARGET_AGP, &tt);
860 NV_ERROR(dev, "Error creating VRAM ctxdma: %d\n", ret);
864 if (dev_priv->gart_info.type != NOUVEAU_GART_NONE) {
865 ret = nouveau_gpuobj_gart_dma_new(chan, 0,
866 dev_priv->gart_info.aper_size,
867 NV_DMA_ACCESS_RW, &tt, NULL);
869 NV_ERROR(dev, "Invalid GART type %d\n", dev_priv->gart_info.type);
874 NV_ERROR(dev, "Error creating TT ctxdma: %d\n", ret);
878 ret = nouveau_ramht_insert(chan, tt_h, tt);
879 nouveau_gpuobj_ref(NULL, &tt);
881 NV_ERROR(dev, "Error adding TT ctxdma to RAMHT: %d\n", ret);
889 nouveau_gpuobj_channel_takedown(struct nouveau_channel *chan)
891 struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
892 struct drm_device *dev = chan->dev;
895 NV_DEBUG(dev, "ch%d\n", chan->id);
900 nouveau_ramht_ref(NULL, &chan->ramht, chan);
902 nouveau_gpuobj_ref(NULL, &chan->vm_pd);
903 nouveau_gpuobj_ref(NULL, &chan->vm_gart_pt);
904 for (i = 0; i < dev_priv->vm_vram_pt_nr; i++)
905 nouveau_gpuobj_ref(NULL, &chan->vm_vram_pt[i]);
907 if (chan->ramin_heap.free_stack.next)
908 drm_mm_takedown(&chan->ramin_heap);
909 nouveau_gpuobj_ref(NULL, &chan->ramin);
913 nouveau_gpuobj_suspend(struct drm_device *dev)
915 struct drm_nouveau_private *dev_priv = dev->dev_private;
916 struct nouveau_gpuobj *gpuobj;
919 if (dev_priv->card_type < NV_50) {
920 dev_priv->susres.ramin_copy = vmalloc(dev_priv->ramin_rsvd_vram);
921 if (!dev_priv->susres.ramin_copy)
924 for (i = 0; i < dev_priv->ramin_rsvd_vram; i += 4)
925 dev_priv->susres.ramin_copy[i/4] = nv_ri32(dev, i);
929 list_for_each_entry(gpuobj, &dev_priv->gpuobj_list, list) {
930 if (!gpuobj->im_backing)
933 gpuobj->im_backing_suspend = vmalloc(gpuobj->size);
934 if (!gpuobj->im_backing_suspend) {
935 nouveau_gpuobj_resume(dev);
939 for (i = 0; i < gpuobj->size; i += 4)
940 gpuobj->im_backing_suspend[i/4] = nv_ro32(gpuobj, i);
947 nouveau_gpuobj_suspend_cleanup(struct drm_device *dev)
949 struct drm_nouveau_private *dev_priv = dev->dev_private;
950 struct nouveau_gpuobj *gpuobj;
952 if (dev_priv->card_type < NV_50) {
953 vfree(dev_priv->susres.ramin_copy);
954 dev_priv->susres.ramin_copy = NULL;
958 list_for_each_entry(gpuobj, &dev_priv->gpuobj_list, list) {
959 if (!gpuobj->im_backing_suspend)
962 vfree(gpuobj->im_backing_suspend);
963 gpuobj->im_backing_suspend = NULL;
968 nouveau_gpuobj_resume(struct drm_device *dev)
970 struct drm_nouveau_private *dev_priv = dev->dev_private;
971 struct nouveau_gpuobj *gpuobj;
974 if (dev_priv->card_type < NV_50) {
975 for (i = 0; i < dev_priv->ramin_rsvd_vram; i += 4)
976 nv_wi32(dev, i, dev_priv->susres.ramin_copy[i/4]);
977 nouveau_gpuobj_suspend_cleanup(dev);
981 list_for_each_entry(gpuobj, &dev_priv->gpuobj_list, list) {
982 if (!gpuobj->im_backing_suspend)
985 for (i = 0; i < gpuobj->size; i += 4)
986 nv_wo32(gpuobj, i, gpuobj->im_backing_suspend[i/4]);
987 dev_priv->engine.instmem.flush(dev);
990 nouveau_gpuobj_suspend_cleanup(dev);
993 int nouveau_ioctl_grobj_alloc(struct drm_device *dev, void *data,
994 struct drm_file *file_priv)
996 struct drm_nouveau_grobj_alloc *init = data;
997 struct nouveau_gpuobj *gr = NULL;
998 struct nouveau_channel *chan;
1001 if (init->handle == ~0)
1004 chan = nouveau_channel_get(dev, file_priv, init->channel);
1006 return PTR_ERR(chan);
1008 if (nouveau_ramht_find(chan, init->handle)) {
1013 ret = nouveau_gpuobj_gr_new(chan, init->class, &gr);
1015 NV_ERROR(dev, "Error creating object: %d (%d/0x%08x)\n",
1016 ret, init->channel, init->handle);
1020 ret = nouveau_ramht_insert(chan, init->handle, gr);
1021 nouveau_gpuobj_ref(NULL, &gr);
1023 NV_ERROR(dev, "Error referencing object: %d (%d/0x%08x)\n",
1024 ret, init->channel, init->handle);
1028 nouveau_channel_put(&chan);
1032 int nouveau_ioctl_gpuobj_free(struct drm_device *dev, void *data,
1033 struct drm_file *file_priv)
1035 struct drm_nouveau_gpuobj_free *objfree = data;
1036 struct nouveau_channel *chan;
1039 chan = nouveau_channel_get(dev, file_priv, objfree->channel);
1041 return PTR_ERR(chan);
1043 ret = nouveau_ramht_remove(chan, objfree->handle);
1044 nouveau_channel_put(&chan);
1049 nv_ro32(struct nouveau_gpuobj *gpuobj, u32 offset)
1051 struct drm_nouveau_private *dev_priv = gpuobj->dev->dev_private;
1052 struct drm_device *dev = gpuobj->dev;
1054 if (gpuobj->pinst == ~0 || !dev_priv->ramin_available) {
1055 u64 ptr = gpuobj->vinst + offset;
1056 u32 base = ptr >> 16;
1059 spin_lock(&dev_priv->ramin_lock);
1060 if (dev_priv->ramin_base != base) {
1061 dev_priv->ramin_base = base;
1062 nv_wr32(dev, 0x001700, dev_priv->ramin_base);
1064 val = nv_rd32(dev, 0x700000 + (ptr & 0xffff));
1065 spin_unlock(&dev_priv->ramin_lock);
1069 return nv_ri32(dev, gpuobj->pinst + offset);
1073 nv_wo32(struct nouveau_gpuobj *gpuobj, u32 offset, u32 val)
1075 struct drm_nouveau_private *dev_priv = gpuobj->dev->dev_private;
1076 struct drm_device *dev = gpuobj->dev;
1078 if (gpuobj->pinst == ~0 || !dev_priv->ramin_available) {
1079 u64 ptr = gpuobj->vinst + offset;
1080 u32 base = ptr >> 16;
1082 spin_lock(&dev_priv->ramin_lock);
1083 if (dev_priv->ramin_base != base) {
1084 dev_priv->ramin_base = base;
1085 nv_wr32(dev, 0x001700, dev_priv->ramin_base);
1087 nv_wr32(dev, 0x700000 + (ptr & 0xffff), val);
1088 spin_unlock(&dev_priv->ramin_lock);
1092 nv_wi32(dev, gpuobj->pinst + offset, val);