nouveau_ramht.c 6.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
 * Copyright 2010 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"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * 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 "nouveau_ramht.h"

static uint32_t
31
nouveau_ramht_hash_handle(struct nouveau_channel *chan, uint32_t handle)
32
{
33
	struct drm_device *dev = chan->dev;
34
	struct drm_nouveau_private *dev_priv = dev->dev_private;
35
	struct nouveau_ramht *ramht = chan->ramht;
36 37 38
	uint32_t hash = 0;
	int i;

39
	NV_DEBUG(dev, "ch%d handle=0x%08x\n", chan->id, handle);
40

41 42 43
	for (i = 32; i > 0; i -= ramht->bits) {
		hash ^= (handle & ((1 << ramht->bits) - 1));
		handle >>= ramht->bits;
44 45 46
	}

	if (dev_priv->card_type < NV_50)
47
		hash ^= chan->id << (ramht->bits - 4);
48 49 50 51 52 53 54 55 56 57 58
	hash <<= 3;

	NV_DEBUG(dev, "hash=0x%08x\n", hash);
	return hash;
}

static int
nouveau_ramht_entry_valid(struct drm_device *dev, struct nouveau_gpuobj *ramht,
			  uint32_t offset)
{
	struct drm_nouveau_private *dev_priv = dev->dev_private;
59
	uint32_t ctx = nv_ro32(ramht, offset + 4);
60 61 62 63 64 65 66

	if (dev_priv->card_type < NV_40)
		return ((ctx & NV_RAMHT_CONTEXT_VALID) != 0);
	return (ctx != 0);
}

int
67 68
nouveau_ramht_insert(struct nouveau_channel *chan, u32 handle,
		     struct nouveau_gpuobj *gpuobj)
69
{
70
	struct drm_device *dev = chan->dev;
71 72
	struct drm_nouveau_private *dev_priv = dev->dev_private;
	struct nouveau_instmem_engine *instmem = &dev_priv->engine.instmem;
73 74
	struct nouveau_ramht_entry *entry;
	struct nouveau_gpuobj *ramht = chan->ramht->gpuobj;
75 76
	uint32_t ctx, co, ho;

77 78 79 80 81 82 83 84 85 86 87
	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;
	list_add(&entry->head, &chan->ramht->entries);
	nouveau_gpuobj_ref(gpuobj, &entry->gpuobj);
88 89

	if (dev_priv->card_type < NV_40) {
90
		ctx = NV_RAMHT_CONTEXT_VALID | (gpuobj->cinst >> 4) |
91
		      (chan->id << NV_RAMHT_CONTEXT_CHANNEL_SHIFT) |
92
		      (gpuobj->engine << NV_RAMHT_CONTEXT_ENGINE_SHIFT);
93 94
	} else
	if (dev_priv->card_type < NV_50) {
95
		ctx = (gpuobj->cinst >> 4) |
96
		      (chan->id << NV40_RAMHT_CONTEXT_CHANNEL_SHIFT) |
97
		      (gpuobj->engine << NV40_RAMHT_CONTEXT_ENGINE_SHIFT);
98
	} else {
99 100
		if (gpuobj->engine == NVOBJ_ENGINE_DISPLAY) {
			ctx = (gpuobj->cinst << 10) | 2;
101
		} else {
102 103
			ctx = (gpuobj->cinst >> 4) |
			      ((gpuobj->engine <<
104 105 106 107
				NV40_RAMHT_CONTEXT_ENGINE_SHIFT));
		}
	}

108
	co = ho = nouveau_ramht_hash_handle(chan, handle);
109 110 111 112
	do {
		if (!nouveau_ramht_entry_valid(dev, ramht, co)) {
			NV_DEBUG(dev,
				 "insert ch%d 0x%08x: h=0x%08x, c=0x%08x\n",
113 114
				 chan->id, co, handle, ctx);
			nv_wo32(ramht, co + 0, handle);
115
			nv_wo32(ramht, co + 4, ctx);
116 117 118 119 120

			instmem->flush(dev);
			return 0;
		}
		NV_DEBUG(dev, "collision ch%d 0x%08x: h=0x%08x\n",
121
			 chan->id, co, nv_ro32(ramht, co));
122 123

		co += 8;
124
		if (co >= ramht->size)
125 126 127 128
			co = 0;
	} while (co != ho);

	NV_ERROR(dev, "RAMHT space exhausted. ch=%d\n", chan->id);
129 130
	list_del(&entry->head);
	kfree(entry);
131 132 133 134
	return -ENOMEM;
}

void
135
nouveau_ramht_remove(struct nouveau_channel *chan, u32 handle)
136
{
137
	struct drm_device *dev = chan->dev;
138 139
	struct drm_nouveau_private *dev_priv = dev->dev_private;
	struct nouveau_instmem_engine *instmem = &dev_priv->engine.instmem;
140 141 142 143 144 145 146 147 148 149 150 151
	struct nouveau_gpuobj *ramht = chan->ramht->gpuobj;
	struct nouveau_ramht_entry *entry, *tmp;
	u32 co, ho;

	list_for_each_entry_safe(entry, tmp, &chan->ramht->entries, head) {
		if (entry->channel != chan || entry->handle != handle)
			continue;

		nouveau_gpuobj_ref(NULL, &entry->gpuobj);
		list_del(&entry->head);
		kfree(entry);
		break;
152 153
	}

154
	co = ho = nouveau_ramht_hash_handle(chan, handle);
155 156
	do {
		if (nouveau_ramht_entry_valid(dev, ramht, co) &&
157
		    (handle == nv_ro32(ramht, co))) {
158 159
			NV_DEBUG(dev,
				 "remove ch%d 0x%08x: h=0x%08x, c=0x%08x\n",
160
				 chan->id, co, handle, nv_ro32(ramht, co + 4));
161 162
			nv_wo32(ramht, co + 0, 0x00000000);
			nv_wo32(ramht, co + 4, 0x00000000);
163 164 165 166 167
			instmem->flush(dev);
			return;
		}

		co += 8;
168
		if (co >= ramht->size)
169 170 171 172
			co = 0;
	} while (co != ho);

	NV_ERROR(dev, "RAMHT entry not found. ch=%d, handle=0x%08x\n",
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
		 chan->id, handle);
}

struct nouveau_gpuobj *
nouveau_ramht_find(struct nouveau_channel *chan, u32 handle)
{
	struct nouveau_ramht_entry *entry;

	list_for_each_entry(entry, &chan->ramht->entries, head) {
		if (entry->channel == chan && entry->handle == handle)
			return entry->gpuobj;
	}

	return NULL;
}

int
nouveau_ramht_new(struct drm_device *dev, struct nouveau_gpuobj *gpuobj,
		  struct nouveau_ramht **pramht)
{
	struct nouveau_ramht *ramht;

	ramht = kzalloc(sizeof(*ramht), GFP_KERNEL);
	if (!ramht)
		return -ENOMEM;

	ramht->dev = dev;
	ramht->refcount = 1;
201
	ramht->bits = drm_order(gpuobj->size / 8);
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
	INIT_LIST_HEAD(&ramht->entries);
	nouveau_gpuobj_ref(gpuobj, &ramht->gpuobj);

	*pramht = ramht;
	return 0;
}

void
nouveau_ramht_ref(struct nouveau_ramht *ref, struct nouveau_ramht **ptr,
		  struct nouveau_channel *chan)
{
	struct nouveau_ramht_entry *entry, *tmp;
	struct nouveau_ramht *ramht;

	if (ref)
		ref->refcount++;

	ramht = *ptr;
	if (ramht) {
		list_for_each_entry_safe(entry, tmp, &ramht->entries, head) {
			if (entry->channel == chan)
				nouveau_ramht_remove(chan, entry->handle);
		}

		if (--ramht->refcount == 0) {
			nouveau_gpuobj_ref(NULL, &ramht->gpuobj);
			kfree(ramht);
		}
	}
	*ptr = ref;
232
}