2 * Copyright 2010 Red Hat Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
27 #include "nouveau_drv.h"
28 #include "nouveau_ramht.h"
31 nouveau_ramht_hash_handle(struct drm_device *dev, int channel, uint32_t handle)
33 struct drm_nouveau_private *dev_priv = dev->dev_private;
37 NV_DEBUG(dev, "ch%d handle=0x%08x\n", channel, handle);
39 for (i = 32; i > 0; i -= dev_priv->ramht_bits) {
40 hash ^= (handle & ((1 << dev_priv->ramht_bits) - 1));
41 handle >>= dev_priv->ramht_bits;
44 if (dev_priv->card_type < NV_50)
45 hash ^= channel << (dev_priv->ramht_bits - 4);
48 NV_DEBUG(dev, "hash=0x%08x\n", hash);
53 nouveau_ramht_entry_valid(struct drm_device *dev, struct nouveau_gpuobj *ramht,
56 struct drm_nouveau_private *dev_priv = dev->dev_private;
57 uint32_t ctx = nv_ro32(ramht, offset + 4);
59 if (dev_priv->card_type < NV_40)
60 return ((ctx & NV_RAMHT_CONTEXT_VALID) != 0);
65 nouveau_ramht_insert(struct nouveau_channel *chan, u32 handle,
66 struct nouveau_gpuobj *gpuobj)
68 struct drm_device *dev = chan->dev;
69 struct drm_nouveau_private *dev_priv = dev->dev_private;
70 struct nouveau_instmem_engine *instmem = &dev_priv->engine.instmem;
71 struct nouveau_ramht_entry *entry;
72 struct nouveau_gpuobj *ramht = chan->ramht->gpuobj;
75 if (nouveau_ramht_find(chan, handle))
78 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
81 entry->channel = chan;
83 entry->handle = handle;
84 list_add(&entry->head, &chan->ramht->entries);
85 nouveau_gpuobj_ref(gpuobj, &entry->gpuobj);
87 if (dev_priv->card_type < NV_40) {
88 ctx = NV_RAMHT_CONTEXT_VALID | (gpuobj->cinst >> 4) |
89 (chan->id << NV_RAMHT_CONTEXT_CHANNEL_SHIFT) |
90 (gpuobj->engine << NV_RAMHT_CONTEXT_ENGINE_SHIFT);
92 if (dev_priv->card_type < NV_50) {
93 ctx = (gpuobj->cinst >> 4) |
94 (chan->id << NV40_RAMHT_CONTEXT_CHANNEL_SHIFT) |
95 (gpuobj->engine << NV40_RAMHT_CONTEXT_ENGINE_SHIFT);
97 if (gpuobj->engine == NVOBJ_ENGINE_DISPLAY) {
98 ctx = (gpuobj->cinst << 10) | 2;
100 ctx = (gpuobj->cinst >> 4) |
102 NV40_RAMHT_CONTEXT_ENGINE_SHIFT));
106 co = ho = nouveau_ramht_hash_handle(dev, chan->id, handle);
108 if (!nouveau_ramht_entry_valid(dev, ramht, co)) {
110 "insert ch%d 0x%08x: h=0x%08x, c=0x%08x\n",
111 chan->id, co, handle, ctx);
112 nv_wo32(ramht, co + 0, handle);
113 nv_wo32(ramht, co + 4, ctx);
118 NV_DEBUG(dev, "collision ch%d 0x%08x: h=0x%08x\n",
119 chan->id, co, nv_ro32(ramht, co));
122 if (co >= dev_priv->ramht_size)
126 NV_ERROR(dev, "RAMHT space exhausted. ch=%d\n", chan->id);
127 list_del(&entry->head);
133 nouveau_ramht_remove(struct nouveau_channel *chan, u32 handle)
135 struct drm_device *dev = chan->dev;
136 struct drm_nouveau_private *dev_priv = dev->dev_private;
137 struct nouveau_instmem_engine *instmem = &dev_priv->engine.instmem;
138 struct nouveau_gpuobj *ramht = chan->ramht->gpuobj;
139 struct nouveau_ramht_entry *entry, *tmp;
142 list_for_each_entry_safe(entry, tmp, &chan->ramht->entries, head) {
143 if (entry->channel != chan || entry->handle != handle)
146 nouveau_gpuobj_ref(NULL, &entry->gpuobj);
147 list_del(&entry->head);
152 co = ho = nouveau_ramht_hash_handle(dev, chan->id, handle);
154 if (nouveau_ramht_entry_valid(dev, ramht, co) &&
155 (handle == nv_ro32(ramht, co))) {
157 "remove ch%d 0x%08x: h=0x%08x, c=0x%08x\n",
158 chan->id, co, handle, nv_ro32(ramht, co + 4));
159 nv_wo32(ramht, co + 0, 0x00000000);
160 nv_wo32(ramht, co + 4, 0x00000000);
166 if (co >= dev_priv->ramht_size)
170 NV_ERROR(dev, "RAMHT entry not found. ch=%d, handle=0x%08x\n",
174 struct nouveau_gpuobj *
175 nouveau_ramht_find(struct nouveau_channel *chan, u32 handle)
177 struct nouveau_ramht_entry *entry;
179 list_for_each_entry(entry, &chan->ramht->entries, head) {
180 if (entry->channel == chan && entry->handle == handle)
181 return entry->gpuobj;
188 nouveau_ramht_new(struct drm_device *dev, struct nouveau_gpuobj *gpuobj,
189 struct nouveau_ramht **pramht)
191 struct nouveau_ramht *ramht;
193 ramht = kzalloc(sizeof(*ramht), GFP_KERNEL);
199 INIT_LIST_HEAD(&ramht->entries);
200 nouveau_gpuobj_ref(gpuobj, &ramht->gpuobj);
207 nouveau_ramht_ref(struct nouveau_ramht *ref, struct nouveau_ramht **ptr,
208 struct nouveau_channel *chan)
210 struct nouveau_ramht_entry *entry, *tmp;
211 struct nouveau_ramht *ramht;
218 list_for_each_entry_safe(entry, tmp, &ramht->entries, head) {
219 if (entry->channel == chan)
220 nouveau_ramht_remove(chan, entry->handle);
223 if (--ramht->refcount == 0) {
224 nouveau_gpuobj_ref(NULL, &ramht->gpuobj);