drm/nouveau: port all engines to new engine module format
[deliverable/linux.git] / drivers / gpu / drm / nouveau / core / core / ramht.c
index 5c22864fbd2c3b614ff3e23ffc1eded8c76d96b7..86a64045dd602973a7c78bcef213bc2d3571d662 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2010 Red Hat Inc.
+ * Copyright 2012 Red Hat Inc.
  *
  * Permission is hereby granted, free of charge, to any person obtaining a
  * copy of this software and associated documentation files (the "Software"),
  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  * OTHER DEALINGS IN THE SOFTWARE.
- *
- * Authors: Ben Skeggs
  */
 
-#include "drmP.h"
-
-#include "nouveau_drv.h"
+#include <core/object.h>
 #include <core/ramht.h>
+#include <core/math.h>
+
+#include <subdev/bar.h>
 
 static u32
-nouveau_ramht_hash_handle(struct nouveau_channel *chan, u32 handle)
+nouveau_ramht_hash(struct nouveau_ramht *ramht, int chid, u32 handle)
 {
-       struct drm_device *dev = chan->dev;
-       struct drm_nouveau_private *dev_priv = dev->dev_private;
-       struct nouveau_ramht *ramht = chan->ramht;
        u32 hash = 0;
-       int i;
 
-       NV_DEBUG(dev, "ch%d handle=0x%08x\n", chan->id, handle);
-
-       for (i = 32; i > 0; i -= ramht->bits) {
+       while (handle) {
                hash ^= (handle & ((1 << ramht->bits) - 1));
                handle >>= ramht->bits;
        }
 
-       if (dev_priv->card_type < NV_50)
-               hash ^= chan->id << (ramht->bits - 4);
-       hash <<= 3;
-
-       NV_DEBUG(dev, "hash=0x%08x\n", hash);
+       hash ^= chid << (ramht->bits - 4);
+       hash  = hash << 3;
        return hash;
 }
 
-static int
-nouveau_ramht_entry_valid(struct drm_device *dev, struct nouveau_gpuobj *ramht,
-                         u32 offset)
-{
-       struct drm_nouveau_private *dev_priv = dev->dev_private;
-       u32 ctx = nv_ro32(ramht, offset + 4);
-
-       if (dev_priv->card_type < NV_40)
-               return ((ctx & NV_RAMHT_CONTEXT_VALID) != 0);
-       return (ctx != 0);
-}
-
-static int
-nouveau_ramht_entry_same_channel(struct nouveau_channel *chan,
-                                struct nouveau_gpuobj *ramht, u32 offset)
-{
-       struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
-       u32 ctx = nv_ro32(ramht, offset + 4);
-
-       if (dev_priv->card_type >= NV_50)
-               return true;
-       else if (dev_priv->card_type >= NV_40)
-               return chan->id ==
-                       ((ctx >> NV40_RAMHT_CONTEXT_CHANNEL_SHIFT) & 0x1f);
-       else
-               return chan->id ==
-                       ((ctx >> NV_RAMHT_CONTEXT_CHANNEL_SHIFT) & 0x1f);
-}
-
 int
-nouveau_ramht_insert(struct nouveau_channel *chan, u32 handle,
-                    struct nouveau_gpuobj *gpuobj)
+nouveau_ramht_insert(struct nouveau_ramht *ramht, int chid,
+                    u32 handle, u32 context)
 {
-       struct drm_device *dev = chan->dev;
-       struct drm_nouveau_private *dev_priv = dev->dev_private;
-       struct nouveau_ramht_entry *entry;
-       struct nouveau_gpuobj *ramht = chan->ramht->gpuobj;
-       unsigned long flags;
-       u32 ctx, co, ho;
-
-       if (nouveau_ramht_find(chan, handle))
-               return -EEXIST;
-
-       entry = kmalloc(sizeof(*entry), GFP_KERNEL);
-       if (!entry)
-               return -ENOMEM;
-       entry->channel = chan;
-       entry->gpuobj = NULL;
-       entry->handle = handle;
-       nouveau_gpuobj_ref(gpuobj, &entry->gpuobj);
-
-       if (dev_priv->card_type < NV_40) {
-               ctx = NV_RAMHT_CONTEXT_VALID | (gpuobj->addr >> 4) |
-                     (chan->id << NV_RAMHT_CONTEXT_CHANNEL_SHIFT) |
-                     (gpuobj->engine << NV_RAMHT_CONTEXT_ENGINE_SHIFT);
-       } else
-       if (dev_priv->card_type < NV_50) {
-               ctx = (gpuobj->addr >> 4) |
-                     (chan->id << NV40_RAMHT_CONTEXT_CHANNEL_SHIFT) |
-                     (gpuobj->engine << NV40_RAMHT_CONTEXT_ENGINE_SHIFT);
-       } else {
-               if (gpuobj->engine == NVOBJ_ENGINE_DISPLAY) {
-                       ctx = (gpuobj->node->offset << 10) |
-                             (chan->id << 28) |
-                             chan->id; /* HASH_TAG */
-               } else {
-                       ctx = (gpuobj->node->offset >> 4) |
-                             ((gpuobj->engine <<
-                               NV40_RAMHT_CONTEXT_ENGINE_SHIFT));
-               }
-       }
-
-       spin_lock_irqsave(&chan->ramht->lock, flags);
-       list_add(&entry->head, &chan->ramht->entries);
+       struct nouveau_bar *bar = nouveau_bar(ramht);
+       u32 co, ho;
 
-       co = ho = nouveau_ramht_hash_handle(chan, handle);
+       co = ho = nouveau_ramht_hash(ramht, chid, handle);
        do {
-               if (!nouveau_ramht_entry_valid(dev, ramht, co)) {
-                       NV_DEBUG(dev,
-                                "insert ch%d 0x%08x: h=0x%08x, c=0x%08x\n",
-                                chan->id, co, handle, ctx);
+               if (!nv_ro32(ramht, co + 4)) {
                        nv_wo32(ramht, co + 0, handle);
-                       nv_wo32(ramht, co + 4, ctx);
-
-                       spin_unlock_irqrestore(&chan->ramht->lock, flags);
-                       nvimem_flush(dev);
-                       return 0;
+                       nv_wo32(ramht, co + 4, context);
+                       if (bar)
+                               bar->flush(bar);
+                       return co;
                }
-               NV_DEBUG(dev, "collision ch%d 0x%08x: h=0x%08x\n",
-                        chan->id, co, nv_ro32(ramht, co));
 
                co += 8;
-               if (co >= ramht->size)
+               if (co >= nv_gpuobj(ramht)->size)
                        co = 0;
        } while (co != ho);
 
-       NV_ERROR(dev, "RAMHT space exhausted. ch=%d\n", chan->id);
-       list_del(&entry->head);
-       spin_unlock_irqrestore(&chan->ramht->lock, flags);
-       kfree(entry);
        return -ENOMEM;
 }
 
-static struct nouveau_ramht_entry *
-nouveau_ramht_remove_entry(struct nouveau_channel *chan, u32 handle)
-{
-       struct nouveau_ramht *ramht = chan ? chan->ramht : NULL;
-       struct nouveau_ramht_entry *entry;
-       unsigned long flags;
-
-       if (!ramht)
-               return NULL;
-
-       spin_lock_irqsave(&ramht->lock, flags);
-       list_for_each_entry(entry, &ramht->entries, head) {
-               if (entry->channel == chan &&
-                   (!handle || entry->handle == handle)) {
-                       list_del(&entry->head);
-                       spin_unlock_irqrestore(&ramht->lock, flags);
-
-                       return entry;
-               }
-       }
-       spin_unlock_irqrestore(&ramht->lock, flags);
-
-       return NULL;
-}
-
-static void
-nouveau_ramht_remove_hash(struct nouveau_channel *chan, u32 handle)
-{
-       struct drm_device *dev = chan->dev;
-       struct nouveau_gpuobj *ramht = chan->ramht->gpuobj;
-       unsigned long flags;
-       u32 co, ho;
-
-       spin_lock_irqsave(&chan->ramht->lock, flags);
-       co = ho = nouveau_ramht_hash_handle(chan, handle);
-       do {
-               if (nouveau_ramht_entry_valid(dev, ramht, co) &&
-                   nouveau_ramht_entry_same_channel(chan, ramht, co) &&
-                   (handle == nv_ro32(ramht, co))) {
-                       NV_DEBUG(dev,
-                                "remove ch%d 0x%08x: h=0x%08x, c=0x%08x\n",
-                                chan->id, co, handle, nv_ro32(ramht, co + 4));
-                       nv_wo32(ramht, co + 0, 0x00000000);
-                       nv_wo32(ramht, co + 4, 0x00000000);
-                       nvimem_flush(dev);
-                       goto out;
-               }
-
-               co += 8;
-               if (co >= ramht->size)
-                       co = 0;
-       } while (co != ho);
-
-       NV_ERROR(dev, "RAMHT entry not found. ch=%d, handle=0x%08x\n",
-                chan->id, handle);
-out:
-       spin_unlock_irqrestore(&chan->ramht->lock, flags);
-}
-
-int
-nouveau_ramht_remove(struct nouveau_channel *chan, u32 handle)
+void
+nouveau_ramht_remove(struct nouveau_ramht *ramht, int cookie)
 {
-       struct nouveau_ramht_entry *entry;
-
-       entry = nouveau_ramht_remove_entry(chan, handle);
-       if (!entry)
-               return -ENOENT;
-
-       nouveau_ramht_remove_hash(chan, entry->handle);
-       nouveau_gpuobj_ref(NULL, &entry->gpuobj);
-       kfree(entry);
-       return 0;
+       struct nouveau_bar *bar = nouveau_bar(ramht);
+       nv_wo32(ramht, cookie + 0, 0x00000000);
+       nv_wo32(ramht, cookie + 4, 0x00000000);
+       if (bar)
+               bar->flush(bar);
 }
 
-struct nouveau_gpuobj *
-nouveau_ramht_find(struct nouveau_channel *chan, u32 handle)
-{
-       struct nouveau_ramht *ramht = chan->ramht;
-       struct nouveau_ramht_entry *entry;
-       struct nouveau_gpuobj *gpuobj = NULL;
-       unsigned long flags;
-
-       if (unlikely(!chan->ramht))
-               return NULL;
-
-       spin_lock_irqsave(&ramht->lock, flags);
-       list_for_each_entry(entry, &chan->ramht->entries, head) {
-               if (entry->channel == chan && entry->handle == handle) {
-                       gpuobj = entry->gpuobj;
-                       break;
-               }
-       }
-       spin_unlock_irqrestore(&ramht->lock, flags);
-
-       return gpuobj;
-}
+static struct nouveau_oclass
+nouveau_ramht_oclass = {
+       .handle = 0x0000abcd,
+       .ofuncs = &(struct nouveau_ofuncs) {
+               .ctor = NULL,
+               .dtor = _nouveau_gpuobj_dtor,
+               .init = _nouveau_gpuobj_init,
+               .fini = _nouveau_gpuobj_fini,
+               .rd32 = _nouveau_gpuobj_rd32,
+               .wr32 = _nouveau_gpuobj_wr32,
+       },
+};
 
 int
-nouveau_ramht_new(struct drm_device *dev, struct nouveau_gpuobj *gpuobj,
-                 struct nouveau_ramht **pramht)
+nouveau_ramht_new(struct nouveau_object *parent, struct nouveau_object *pargpu,
+                 u32 size, u32 align, struct nouveau_ramht **pramht)
 {
        struct nouveau_ramht *ramht;
+       int ret;
 
-       ramht = kzalloc(sizeof(*ramht), GFP_KERNEL);
-       if (!ramht)
-               return -ENOMEM;
-
-       ramht->dev = dev;
-       kref_init(&ramht->refcount);
-       ramht->bits = drm_order(gpuobj->size / 8);
-       INIT_LIST_HEAD(&ramht->entries);
-       spin_lock_init(&ramht->lock);
-       nouveau_gpuobj_ref(gpuobj, &ramht->gpuobj);
-
+       ret = nouveau_gpuobj_create(parent, parent->engine ?
+                                   parent->engine : parent, /* <nv50 ramht */
+                                   &nouveau_ramht_oclass, 0, pargpu, size,
+                                   align, NVOBJ_FLAG_ZERO_ALLOC, &ramht);
        *pramht = ramht;
-       return 0;
-}
+       if (ret)
+               return ret;
 
-static void
-nouveau_ramht_del(struct kref *ref)
-{
-       struct nouveau_ramht *ramht =
-               container_of(ref, struct nouveau_ramht, refcount);
-
-       nouveau_gpuobj_ref(NULL, &ramht->gpuobj);
-       kfree(ramht);
-}
-
-void
-nouveau_ramht_ref(struct nouveau_ramht *ref, struct nouveau_ramht **ptr,
-                 struct nouveau_channel *chan)
-{
-       struct nouveau_ramht_entry *entry;
-       struct nouveau_ramht *ramht;
-
-       if (ref)
-               kref_get(&ref->refcount);
-
-       ramht = *ptr;
-       if (ramht) {
-               while ((entry = nouveau_ramht_remove_entry(chan, 0))) {
-                       nouveau_ramht_remove_hash(chan, entry->handle);
-                       nouveau_gpuobj_ref(NULL, &entry->gpuobj);
-                       kfree(entry);
-               }
-
-               kref_put(&ramht->refcount, nouveau_ramht_del);
-       }
-       *ptr = ref;
+       ramht->bits = log2i(nv_gpuobj(ramht)->size >> 3);
+       return 0;
 }
This page took 0.028836 seconds and 5 git commands to generate.