Merge branch 'stable/xenbus' of git://git.kernel.org/pub/scm/linux/kernel/git/konrad/xen
[pandora-kernel.git] / drivers / gpu / drm / nouveau / nouveau_ramht.c
1 /*
2  * Copyright 2010 Red Hat Inc.
3  *
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:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
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.
21  *
22  * Authors: Ben Skeggs
23  */
24
25 #include "drmP.h"
26
27 #include "nouveau_drv.h"
28 #include "nouveau_ramht.h"
29
30 static u32
31 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         int i;
38
39         NV_DEBUG(dev, "ch%d handle=0x%08x\n", chan->id, handle);
40
41         for (i = 32; i > 0; i -= ramht->bits) {
42                 hash ^= (handle & ((1 << ramht->bits) - 1));
43                 handle >>= ramht->bits;
44         }
45
46         if (dev_priv->card_type < NV_50)
47                 hash ^= chan->id << (ramht->bits - 4);
48         hash <<= 3;
49
50         NV_DEBUG(dev, "hash=0x%08x\n", hash);
51         return hash;
52 }
53
54 static int
55 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         if (dev_priv->card_type < NV_40)
62                 return ((ctx & NV_RAMHT_CONTEXT_VALID) != 0);
63         return (ctx != 0);
64 }
65
66 static int
67 nouveau_ramht_entry_same_channel(struct nouveau_channel *chan,
68                                  struct nouveau_gpuobj *ramht, u32 offset)
69 {
70         struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
71         u32 ctx = nv_ro32(ramht, offset + 4);
72
73         if (dev_priv->card_type >= NV_50)
74                 return true;
75         else if (dev_priv->card_type >= NV_40)
76                 return chan->id ==
77                         ((ctx >> NV40_RAMHT_CONTEXT_CHANNEL_SHIFT) & 0x1f);
78         else
79                 return chan->id ==
80                         ((ctx >> NV_RAMHT_CONTEXT_CHANNEL_SHIFT) & 0x1f);
81 }
82
83 int
84 nouveau_ramht_insert(struct nouveau_channel *chan, u32 handle,
85                      struct nouveau_gpuobj *gpuobj)
86 {
87         struct drm_device *dev = chan->dev;
88         struct drm_nouveau_private *dev_priv = dev->dev_private;
89         struct nouveau_instmem_engine *instmem = &dev_priv->engine.instmem;
90         struct nouveau_ramht_entry *entry;
91         struct nouveau_gpuobj *ramht = chan->ramht->gpuobj;
92         unsigned long flags;
93         u32 ctx, co, ho;
94
95         if (nouveau_ramht_find(chan, handle))
96                 return -EEXIST;
97
98         entry = kmalloc(sizeof(*entry), GFP_KERNEL);
99         if (!entry)
100                 return -ENOMEM;
101         entry->channel = chan;
102         entry->gpuobj = NULL;
103         entry->handle = handle;
104         nouveau_gpuobj_ref(gpuobj, &entry->gpuobj);
105
106         if (dev_priv->card_type < NV_40) {
107                 ctx = NV_RAMHT_CONTEXT_VALID | (gpuobj->pinst >> 4) |
108                       (chan->id << NV_RAMHT_CONTEXT_CHANNEL_SHIFT) |
109                       (gpuobj->engine << NV_RAMHT_CONTEXT_ENGINE_SHIFT);
110         } else
111         if (dev_priv->card_type < NV_50) {
112                 ctx = (gpuobj->pinst >> 4) |
113                       (chan->id << NV40_RAMHT_CONTEXT_CHANNEL_SHIFT) |
114                       (gpuobj->engine << NV40_RAMHT_CONTEXT_ENGINE_SHIFT);
115         } else {
116                 if (gpuobj->engine == NVOBJ_ENGINE_DISPLAY) {
117                         ctx = (gpuobj->cinst << 10) | chan->id;
118                 } else {
119                         ctx = (gpuobj->cinst >> 4) |
120                               ((gpuobj->engine <<
121                                 NV40_RAMHT_CONTEXT_ENGINE_SHIFT));
122                 }
123         }
124
125         spin_lock_irqsave(&chan->ramht->lock, flags);
126         list_add(&entry->head, &chan->ramht->entries);
127
128         co = ho = nouveau_ramht_hash_handle(chan, handle);
129         do {
130                 if (!nouveau_ramht_entry_valid(dev, ramht, co)) {
131                         NV_DEBUG(dev,
132                                  "insert ch%d 0x%08x: h=0x%08x, c=0x%08x\n",
133                                  chan->id, co, handle, ctx);
134                         nv_wo32(ramht, co + 0, handle);
135                         nv_wo32(ramht, co + 4, ctx);
136
137                         spin_unlock_irqrestore(&chan->ramht->lock, flags);
138                         instmem->flush(dev);
139                         return 0;
140                 }
141                 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                         co = 0;
147         } while (co != ho);
148
149         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         return -ENOMEM;
154 }
155
156 static struct nouveau_ramht_entry *
157 nouveau_ramht_remove_entry(struct nouveau_channel *chan, u32 handle)
158 {
159         struct nouveau_ramht *ramht = chan ? chan->ramht : NULL;
160         struct nouveau_ramht_entry *entry;
161         unsigned long flags;
162
163         if (!ramht)
164                 return NULL;
165
166         spin_lock_irqsave(&ramht->lock, flags);
167         list_for_each_entry(entry, &ramht->entries, head) {
168                 if (entry->channel == chan &&
169                     (!handle || entry->handle == handle)) {
170                         list_del(&entry->head);
171                         spin_unlock_irqrestore(&ramht->lock, flags);
172
173                         return entry;
174                 }
175         }
176         spin_unlock_irqrestore(&ramht->lock, flags);
177
178         return NULL;
179 }
180
181 static void
182 nouveau_ramht_remove_hash(struct nouveau_channel *chan, u32 handle)
183 {
184         struct drm_device *dev = chan->dev;
185         struct drm_nouveau_private *dev_priv = dev->dev_private;
186         struct nouveau_instmem_engine *instmem = &dev_priv->engine.instmem;
187         struct nouveau_gpuobj *ramht = chan->ramht->gpuobj;
188         unsigned long flags;
189         u32 co, ho;
190
191         spin_lock_irqsave(&chan->ramht->lock, flags);
192         co = ho = nouveau_ramht_hash_handle(chan, handle);
193         do {
194                 if (nouveau_ramht_entry_valid(dev, ramht, co) &&
195                     nouveau_ramht_entry_same_channel(chan, ramht, co) &&
196                     (handle == nv_ro32(ramht, co))) {
197                         NV_DEBUG(dev,
198                                  "remove ch%d 0x%08x: h=0x%08x, c=0x%08x\n",
199                                  chan->id, co, handle, nv_ro32(ramht, co + 4));
200                         nv_wo32(ramht, co + 0, 0x00000000);
201                         nv_wo32(ramht, co + 4, 0x00000000);
202                         instmem->flush(dev);
203                         goto out;
204                 }
205
206                 co += 8;
207                 if (co >= ramht->size)
208                         co = 0;
209         } while (co != ho);
210
211         NV_ERROR(dev, "RAMHT entry not found. ch=%d, handle=0x%08x\n",
212                  chan->id, handle);
213 out:
214         spin_unlock_irqrestore(&chan->ramht->lock, flags);
215 }
216
217 int
218 nouveau_ramht_remove(struct nouveau_channel *chan, u32 handle)
219 {
220         struct nouveau_ramht_entry *entry;
221
222         entry = nouveau_ramht_remove_entry(chan, handle);
223         if (!entry)
224                 return -ENOENT;
225
226         nouveau_ramht_remove_hash(chan, entry->handle);
227         nouveau_gpuobj_ref(NULL, &entry->gpuobj);
228         kfree(entry);
229         return 0;
230 }
231
232 struct nouveau_gpuobj *
233 nouveau_ramht_find(struct nouveau_channel *chan, u32 handle)
234 {
235         struct nouveau_ramht *ramht = chan->ramht;
236         struct nouveau_ramht_entry *entry;
237         struct nouveau_gpuobj *gpuobj = NULL;
238         unsigned long flags;
239
240         if (unlikely(!chan->ramht))
241                 return NULL;
242
243         spin_lock_irqsave(&ramht->lock, flags);
244         list_for_each_entry(entry, &chan->ramht->entries, head) {
245                 if (entry->channel == chan && entry->handle == handle) {
246                         gpuobj = entry->gpuobj;
247                         break;
248                 }
249         }
250         spin_unlock_irqrestore(&ramht->lock, flags);
251
252         return gpuobj;
253 }
254
255 int
256 nouveau_ramht_new(struct drm_device *dev, struct nouveau_gpuobj *gpuobj,
257                   struct nouveau_ramht **pramht)
258 {
259         struct nouveau_ramht *ramht;
260
261         ramht = kzalloc(sizeof(*ramht), GFP_KERNEL);
262         if (!ramht)
263                 return -ENOMEM;
264
265         ramht->dev = dev;
266         kref_init(&ramht->refcount);
267         ramht->bits = drm_order(gpuobj->size / 8);
268         INIT_LIST_HEAD(&ramht->entries);
269         spin_lock_init(&ramht->lock);
270         nouveau_gpuobj_ref(gpuobj, &ramht->gpuobj);
271
272         *pramht = ramht;
273         return 0;
274 }
275
276 static void
277 nouveau_ramht_del(struct kref *ref)
278 {
279         struct nouveau_ramht *ramht =
280                 container_of(ref, struct nouveau_ramht, refcount);
281
282         nouveau_gpuobj_ref(NULL, &ramht->gpuobj);
283         kfree(ramht);
284 }
285
286 void
287 nouveau_ramht_ref(struct nouveau_ramht *ref, struct nouveau_ramht **ptr,
288                   struct nouveau_channel *chan)
289 {
290         struct nouveau_ramht_entry *entry;
291         struct nouveau_ramht *ramht;
292
293         if (ref)
294                 kref_get(&ref->refcount);
295
296         ramht = *ptr;
297         if (ramht) {
298                 while ((entry = nouveau_ramht_remove_entry(chan, 0))) {
299                         nouveau_ramht_remove_hash(chan, entry->handle);
300                         nouveau_gpuobj_ref(NULL, &entry->gpuobj);
301                         kfree(entry);
302                 }
303
304                 kref_put(&ramht->refcount, nouveau_ramht_del);
305         }
306         *ptr = ref;
307 }