nouveau_ramht.c 7.7 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
/*
 * 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"

30 31
static u32
nouveau_ramht_hash_handle(struct nouveau_channel *chan, u32 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
	u32 hash = 0;
37 38
	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
	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,
56
			  u32 offset)
57 58
{
	struct drm_nouveau_private *dev_priv = dev->dev_private;
59
	u32 ctx = nv_ro32(ramht, offset + 4);
60 61 62 63 64 65

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

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
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);
}

83
int
84 85
nouveau_ramht_insert(struct nouveau_channel *chan, u32 handle,
		     struct nouveau_gpuobj *gpuobj)
86
{
87
	struct drm_device *dev = chan->dev;
88 89
	struct drm_nouveau_private *dev_priv = dev->dev_private;
	struct nouveau_instmem_engine *instmem = &dev_priv->engine.instmem;
90 91
	struct nouveau_ramht_entry *entry;
	struct nouveau_gpuobj *ramht = chan->ramht->gpuobj;
92 93
	unsigned long flags;
	u32 ctx, co, ho;
94

95 96 97 98 99 100 101 102 103 104
	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);
105 106

	if (dev_priv->card_type < NV_40) {
107
		ctx = NV_RAMHT_CONTEXT_VALID | (gpuobj->cinst >> 4) |
108
		      (chan->id << NV_RAMHT_CONTEXT_CHANNEL_SHIFT) |
109
		      (gpuobj->engine << NV_RAMHT_CONTEXT_ENGINE_SHIFT);
110 111
	} else
	if (dev_priv->card_type < NV_50) {
112
		ctx = (gpuobj->cinst >> 4) |
113
		      (chan->id << NV40_RAMHT_CONTEXT_CHANNEL_SHIFT) |
114
		      (gpuobj->engine << NV40_RAMHT_CONTEXT_ENGINE_SHIFT);
115
	} else {
116 117
		if (gpuobj->engine == NVOBJ_ENGINE_DISPLAY) {
			ctx = (gpuobj->cinst << 10) | 2;
118
		} else {
119 120
			ctx = (gpuobj->cinst >> 4) |
			      ((gpuobj->engine <<
121 122 123 124
				NV40_RAMHT_CONTEXT_ENGINE_SHIFT));
		}
	}

125 126 127
	spin_lock_irqsave(&chan->ramht->lock, flags);
	list_add(&entry->head, &chan->ramht->entries);

128
	co = ho = nouveau_ramht_hash_handle(chan, handle);
129 130 131 132
	do {
		if (!nouveau_ramht_entry_valid(dev, ramht, co)) {
			NV_DEBUG(dev,
				 "insert ch%d 0x%08x: h=0x%08x, c=0x%08x\n",
133 134
				 chan->id, co, handle, ctx);
			nv_wo32(ramht, co + 0, handle);
135
			nv_wo32(ramht, co + 4, ctx);
136

137
			spin_unlock_irqrestore(&chan->ramht->lock, flags);
138 139 140 141
			instmem->flush(dev);
			return 0;
		}
		NV_DEBUG(dev, "collision ch%d 0x%08x: h=0x%08x\n",
142
			 chan->id, co, nv_ro32(ramht, co));
143 144

		co += 8;
145
		if (co >= ramht->size)
146 147 148 149
			co = 0;
	} while (co != ho);

	NV_ERROR(dev, "RAMHT space exhausted. ch=%d\n", chan->id);
150
	list_del(&entry->head);
151
	spin_unlock_irqrestore(&chan->ramht->lock, flags);
152
	kfree(entry);
153 154 155
	return -ENOMEM;
}

156 157
static void
nouveau_ramht_remove_locked(struct nouveau_channel *chan, u32 handle)
158
{
159
	struct drm_device *dev = chan->dev;
160 161
	struct drm_nouveau_private *dev_priv = dev->dev_private;
	struct nouveau_instmem_engine *instmem = &dev_priv->engine.instmem;
162 163 164 165 166 167 168 169 170 171 172 173
	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;
174 175
	}

176
	co = ho = nouveau_ramht_hash_handle(chan, handle);
177 178
	do {
		if (nouveau_ramht_entry_valid(dev, ramht, co) &&
179
		    nouveau_ramht_entry_same_channel(chan, ramht, co) &&
180
		    (handle == nv_ro32(ramht, co))) {
181 182
			NV_DEBUG(dev,
				 "remove ch%d 0x%08x: h=0x%08x, c=0x%08x\n",
183
				 chan->id, co, handle, nv_ro32(ramht, co + 4));
184 185
			nv_wo32(ramht, co + 0, 0x00000000);
			nv_wo32(ramht, co + 4, 0x00000000);
186 187 188 189 190
			instmem->flush(dev);
			return;
		}

		co += 8;
191
		if (co >= ramht->size)
192 193 194 195
			co = 0;
	} while (co != ho);

	NV_ERROR(dev, "RAMHT entry not found. ch=%d, handle=0x%08x\n",
196 197 198
		 chan->id, handle);
}

199 200 201 202 203 204 205 206 207 208 209
void
nouveau_ramht_remove(struct nouveau_channel *chan, u32 handle)
{
	struct nouveau_ramht *ramht = chan->ramht;
	unsigned long flags;

	spin_lock_irqsave(&ramht->lock, flags);
	nouveau_ramht_remove_locked(chan, handle);
	spin_unlock_irqrestore(&ramht->lock, flags);
}

210 211 212
struct nouveau_gpuobj *
nouveau_ramht_find(struct nouveau_channel *chan, u32 handle)
{
213
	struct nouveau_ramht *ramht = chan->ramht;
214
	struct nouveau_ramht_entry *entry;
215 216
	struct nouveau_gpuobj *gpuobj = NULL;
	unsigned long flags;
217

218 219 220
	if (unlikely(!chan->ramht))
		return NULL;

221
	spin_lock_irqsave(&ramht->lock, flags);
222
	list_for_each_entry(entry, &chan->ramht->entries, head) {
223 224 225 226
		if (entry->channel == chan && entry->handle == handle) {
			gpuobj = entry->gpuobj;
			break;
		}
227
	}
228
	spin_unlock_irqrestore(&ramht->lock, flags);
229

230
	return gpuobj;
231 232 233 234 235 236 237 238 239 240 241 242 243
}

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;
244
	kref_init(&ramht->refcount);
245
	ramht->bits = drm_order(gpuobj->size / 8);
246
	INIT_LIST_HEAD(&ramht->entries);
247
	spin_lock_init(&ramht->lock);
248 249 250 251 252 253
	nouveau_gpuobj_ref(gpuobj, &ramht->gpuobj);

	*pramht = ramht;
	return 0;
}

254 255 256 257 258 259 260 261 262 263
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);
}

264 265 266 267 268 269
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;
270
	unsigned long flags;
271 272

	if (ref)
273
		kref_get(&ref->refcount);
274 275 276

	ramht = *ptr;
	if (ramht) {
277
		spin_lock_irqsave(&ramht->lock, flags);
278
		list_for_each_entry_safe(entry, tmp, &ramht->entries, head) {
279 280
			if (entry->channel != chan)
				continue;
281

282
			nouveau_ramht_remove_locked(chan, entry->handle);
283
		}
284 285 286
		spin_unlock_irqrestore(&ramht->lock, flags);

		kref_put(&ramht->refcount, nouveau_ramht_del);
287 288
	}
	*ptr = ref;
289
}