blob: 924653c30783c01594f2fbfb32df7163e7a94ed2 [file] [log] [blame]
Ben Skeggs6ee73862009-12-11 19:24:15 +10001/*
2 * Copyright (C) 2006 Ben Skeggs.
3 *
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial
16 * portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 */
27
28/*
29 * Authors:
30 * Ben Skeggs <darktama@iinet.net.au>
31 */
32
33#include "drmP.h"
34#include "drm.h"
35#include "nouveau_drv.h"
36#include "nouveau_drm.h"
Ben Skeggs479dcae2010-09-01 15:24:28 +100037#include "nouveau_ramht.h"
Ben Skeggs6ee73862009-12-11 19:24:15 +100038
Ben Skeggsb8c157d2010-10-20 10:39:35 +100039struct nouveau_gpuobj_method {
40 struct list_head head;
41 u32 mthd;
42 int (*exec)(struct nouveau_channel *, u32 class, u32 mthd, u32 data);
43};
44
45struct nouveau_gpuobj_class {
46 struct list_head head;
47 struct list_head methods;
48 u32 id;
49 u32 engine;
50};
51
52int
53nouveau_gpuobj_class_new(struct drm_device *dev, u32 class, u32 engine)
54{
55 struct drm_nouveau_private *dev_priv = dev->dev_private;
56 struct nouveau_gpuobj_class *oc;
57
58 oc = kzalloc(sizeof(*oc), GFP_KERNEL);
59 if (!oc)
60 return -ENOMEM;
61
62 INIT_LIST_HEAD(&oc->methods);
63 oc->id = class;
64 oc->engine = engine;
65 list_add(&oc->head, &dev_priv->classes);
66 return 0;
67}
68
69int
70nouveau_gpuobj_mthd_new(struct drm_device *dev, u32 class, u32 mthd,
71 int (*exec)(struct nouveau_channel *, u32, u32, u32))
72{
73 struct drm_nouveau_private *dev_priv = dev->dev_private;
74 struct nouveau_gpuobj_method *om;
75 struct nouveau_gpuobj_class *oc;
76
77 list_for_each_entry(oc, &dev_priv->classes, head) {
78 if (oc->id == class)
79 goto found;
80 }
81
82 return -EINVAL;
83
84found:
85 om = kzalloc(sizeof(*om), GFP_KERNEL);
86 if (!om)
87 return -ENOMEM;
88
89 om->mthd = mthd;
90 om->exec = exec;
91 list_add(&om->head, &oc->methods);
92 return 0;
93}
94
95int
96nouveau_gpuobj_mthd_call(struct nouveau_channel *chan,
97 u32 class, u32 mthd, u32 data)
98{
99 struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
100 struct nouveau_gpuobj_method *om;
101 struct nouveau_gpuobj_class *oc;
102
103 list_for_each_entry(oc, &dev_priv->classes, head) {
104 if (oc->id != class)
105 continue;
106
107 list_for_each_entry(om, &oc->methods, head) {
108 if (om->mthd == mthd)
109 return om->exec(chan, class, mthd, data);
110 }
111 }
112
113 return -ENOENT;
114}
115
Ben Skeggs274fec92010-11-03 13:16:18 +1000116int
117nouveau_gpuobj_mthd_call2(struct drm_device *dev, int chid,
118 u32 class, u32 mthd, u32 data)
119{
120 struct drm_nouveau_private *dev_priv = dev->dev_private;
121 struct nouveau_channel *chan = NULL;
122 unsigned long flags;
123 int ret = -EINVAL;
124
125 spin_lock_irqsave(&dev_priv->channels.lock, flags);
126 if (chid > 0 && chid < dev_priv->engine.fifo.channels)
127 chan = dev_priv->channels.ptr[chid];
128 if (chan)
129 ret = nouveau_gpuobj_mthd_call(chan, class, mthd, data);
130 spin_unlock_irqrestore(&dev_priv->channels.lock, flags);
131 return ret;
132}
133
Ben Skeggs6ee73862009-12-11 19:24:15 +1000134/* NVidia uses context objects to drive drawing operations.
135
136 Context objects can be selected into 8 subchannels in the FIFO,
137 and then used via DMA command buffers.
138
139 A context object is referenced by a user defined handle (CARD32). The HW
140 looks up graphics objects in a hash table in the instance RAM.
141
142 An entry in the hash table consists of 2 CARD32. The first CARD32 contains
143 the handle, the second one a bitfield, that contains the address of the
144 object in instance RAM.
145
146 The format of the second CARD32 seems to be:
147
148 NV4 to NV30:
149
150 15: 0 instance_addr >> 4
151 17:16 engine (here uses 1 = graphics)
152 28:24 channel id (here uses 0)
153 31 valid (use 1)
154
155 NV40:
156
157 15: 0 instance_addr >> 4 (maybe 19-0)
158 21:20 engine (here uses 1 = graphics)
159 I'm unsure about the other bits, but using 0 seems to work.
160
161 The key into the hash table depends on the object handle and channel id and
162 is given as:
163*/
Ben Skeggs6ee73862009-12-11 19:24:15 +1000164
165int
166nouveau_gpuobj_new(struct drm_device *dev, struct nouveau_channel *chan,
167 uint32_t size, int align, uint32_t flags,
168 struct nouveau_gpuobj **gpuobj_ret)
169{
170 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggse41115d2010-11-01 11:45:02 +1000171 struct nouveau_instmem_engine *instmem = &dev_priv->engine.instmem;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000172 struct nouveau_gpuobj *gpuobj;
Ben Skeggs5125bfd2010-09-01 15:24:33 +1000173 struct drm_mm_node *ramin = NULL;
Ben Skeggse41115d2010-11-01 11:45:02 +1000174 int ret, i;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000175
176 NV_DEBUG(dev, "ch%d size=%u align=%d flags=0x%08x\n",
177 chan ? chan->id : -1, size, align, flags);
178
Ben Skeggs6ee73862009-12-11 19:24:15 +1000179 gpuobj = kzalloc(sizeof(*gpuobj), GFP_KERNEL);
180 if (!gpuobj)
181 return -ENOMEM;
182 NV_DEBUG(dev, "gpuobj %p\n", gpuobj);
Ben Skeggsb3beb162010-09-01 15:24:29 +1000183 gpuobj->dev = dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000184 gpuobj->flags = flags;
Ben Skeggseb9bcbd2010-09-01 15:24:37 +1000185 kref_init(&gpuobj->refcount);
Ben Skeggs43efc9c2010-09-01 15:24:32 +1000186 gpuobj->size = size;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000187
Ben Skeggse05d7ea2010-09-01 15:24:38 +1000188 spin_lock(&dev_priv->ramin_lock);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000189 list_add_tail(&gpuobj->list, &dev_priv->gpuobj_list);
Ben Skeggse05d7ea2010-09-01 15:24:38 +1000190 spin_unlock(&dev_priv->ramin_lock);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000191
Ben Skeggs6ee73862009-12-11 19:24:15 +1000192 if (chan) {
Ben Skeggs5125bfd2010-09-01 15:24:33 +1000193 ramin = drm_mm_search_free(&chan->ramin_heap, size, align, 0);
194 if (ramin)
195 ramin = drm_mm_get_block(ramin, size, align);
Ben Skeggs5125bfd2010-09-01 15:24:33 +1000196 if (!ramin) {
197 nouveau_gpuobj_ref(NULL, &gpuobj);
198 return -ENOMEM;
199 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000200
Ben Skeggs5125bfd2010-09-01 15:24:33 +1000201 gpuobj->pinst = chan->ramin->pinst;
202 if (gpuobj->pinst != ~0)
Ben Skeggse41115d2010-11-01 11:45:02 +1000203 gpuobj->pinst += ramin->start;
Ben Skeggs5125bfd2010-09-01 15:24:33 +1000204
Ben Skeggse41115d2010-11-01 11:45:02 +1000205 if (dev_priv->card_type < NV_50)
Ben Skeggsde3a6c02010-09-01 15:24:30 +1000206 gpuobj->cinst = gpuobj->pinst;
Ben Skeggs5125bfd2010-09-01 15:24:33 +1000207 else
Ben Skeggse41115d2010-11-01 11:45:02 +1000208 gpuobj->cinst = ramin->start;
209
210 gpuobj->vinst = ramin->start + chan->ramin->vinst;
211 gpuobj->node = ramin;
212 } else {
213 ret = instmem->get(gpuobj, size, align);
214 if (ret) {
215 nouveau_gpuobj_ref(NULL, &gpuobj);
216 return ret;
217 }
218
219 ret = -ENOSYS;
220 if (dev_priv->ramin_available)
221 ret = instmem->map(gpuobj);
222 if (ret)
Ben Skeggs5125bfd2010-09-01 15:24:33 +1000223 gpuobj->pinst = ~0;
Ben Skeggse41115d2010-11-01 11:45:02 +1000224
225 gpuobj->cinst = NVOBJ_CINST_GLOBAL;
Ben Skeggsde3a6c02010-09-01 15:24:30 +1000226 }
227
Ben Skeggs6ee73862009-12-11 19:24:15 +1000228 if (gpuobj->flags & NVOBJ_FLAG_ZERO_ALLOC) {
Ben Skeggs43efc9c2010-09-01 15:24:32 +1000229 for (i = 0; i < gpuobj->size; i += 4)
Ben Skeggsb3beb162010-09-01 15:24:29 +1000230 nv_wo32(gpuobj, i, 0);
Ben Skeggse41115d2010-11-01 11:45:02 +1000231 instmem->flush(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000232 }
233
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000234
Ben Skeggs6ee73862009-12-11 19:24:15 +1000235 *gpuobj_ret = gpuobj;
236 return 0;
237}
238
239int
Ben Skeggsfbd2895e2010-09-01 15:24:34 +1000240nouveau_gpuobj_init(struct drm_device *dev)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000241{
242 struct drm_nouveau_private *dev_priv = dev->dev_private;
243
244 NV_DEBUG(dev, "\n");
245
246 INIT_LIST_HEAD(&dev_priv->gpuobj_list);
Ben Skeggsbd2e5972010-10-19 20:06:01 +1000247 INIT_LIST_HEAD(&dev_priv->classes);
Ben Skeggs5125bfd2010-09-01 15:24:33 +1000248 spin_lock_init(&dev_priv->ramin_lock);
249 dev_priv->ramin_base = ~0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000250
251 return 0;
252}
253
Ben Skeggs6ee73862009-12-11 19:24:15 +1000254void
255nouveau_gpuobj_takedown(struct drm_device *dev)
256{
257 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggsb8c157d2010-10-20 10:39:35 +1000258 struct nouveau_gpuobj_method *om, *tm;
259 struct nouveau_gpuobj_class *oc, *tc;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000260
261 NV_DEBUG(dev, "\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000262
Ben Skeggsb8c157d2010-10-20 10:39:35 +1000263 list_for_each_entry_safe(oc, tc, &dev_priv->classes, head) {
264 list_for_each_entry_safe(om, tm, &oc->methods, head) {
265 list_del(&om->head);
266 kfree(om);
267 }
268 list_del(&oc->head);
269 kfree(oc);
270 }
271
Ben Skeggseb9bcbd2010-09-01 15:24:37 +1000272 BUG_ON(!list_empty(&dev_priv->gpuobj_list));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000273}
274
Ben Skeggs185abec2010-09-01 15:24:39 +1000275
Ben Skeggseb9bcbd2010-09-01 15:24:37 +1000276static void
277nouveau_gpuobj_del(struct kref *ref)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000278{
Ben Skeggseb9bcbd2010-09-01 15:24:37 +1000279 struct nouveau_gpuobj *gpuobj =
280 container_of(ref, struct nouveau_gpuobj, refcount);
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000281 struct drm_device *dev = gpuobj->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000282 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggse41115d2010-11-01 11:45:02 +1000283 struct nouveau_instmem_engine *instmem = &dev_priv->engine.instmem;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000284 int i;
285
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000286 NV_DEBUG(dev, "gpuobj %p\n", gpuobj);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000287
Ben Skeggse41115d2010-11-01 11:45:02 +1000288 if (gpuobj->node && (gpuobj->flags & NVOBJ_FLAG_ZERO_FREE)) {
Ben Skeggs43efc9c2010-09-01 15:24:32 +1000289 for (i = 0; i < gpuobj->size; i += 4)
Ben Skeggsb3beb162010-09-01 15:24:29 +1000290 nv_wo32(gpuobj, i, 0);
Ben Skeggse41115d2010-11-01 11:45:02 +1000291 instmem->flush(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000292 }
293
294 if (gpuobj->dtor)
295 gpuobj->dtor(dev, gpuobj);
296
Ben Skeggse41115d2010-11-01 11:45:02 +1000297 if (gpuobj->cinst == NVOBJ_CINST_GLOBAL) {
298 if (gpuobj->node) {
299 instmem->unmap(gpuobj);
300 instmem->put(gpuobj);
301 }
302 } else {
303 if (gpuobj->node) {
304 spin_lock(&dev_priv->ramin_lock);
305 drm_mm_put_block(gpuobj->node);
306 spin_unlock(&dev_priv->ramin_lock);
307 }
308 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000309
Ben Skeggse05d7ea2010-09-01 15:24:38 +1000310 spin_lock(&dev_priv->ramin_lock);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000311 list_del(&gpuobj->list);
Ben Skeggse05d7ea2010-09-01 15:24:38 +1000312 spin_unlock(&dev_priv->ramin_lock);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000313
Ben Skeggs6ee73862009-12-11 19:24:15 +1000314 kfree(gpuobj);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000315}
316
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000317void
318nouveau_gpuobj_ref(struct nouveau_gpuobj *ref, struct nouveau_gpuobj **ptr)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000319{
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000320 if (ref)
Ben Skeggseb9bcbd2010-09-01 15:24:37 +1000321 kref_get(&ref->refcount);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000322
Ben Skeggseb9bcbd2010-09-01 15:24:37 +1000323 if (*ptr)
324 kref_put(&(*ptr)->refcount, nouveau_gpuobj_del);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000325
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000326 *ptr = ref;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000327}
328
329int
Ben Skeggs43efc9c2010-09-01 15:24:32 +1000330nouveau_gpuobj_new_fake(struct drm_device *dev, u32 pinst, u64 vinst,
331 u32 size, u32 flags, struct nouveau_gpuobj **pgpuobj)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000332{
333 struct drm_nouveau_private *dev_priv = dev->dev_private;
334 struct nouveau_gpuobj *gpuobj = NULL;
335 int i;
336
337 NV_DEBUG(dev,
Ben Skeggs43efc9c2010-09-01 15:24:32 +1000338 "pinst=0x%08x vinst=0x%010llx size=0x%08x flags=0x%08x\n",
339 pinst, vinst, size, flags);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000340
341 gpuobj = kzalloc(sizeof(*gpuobj), GFP_KERNEL);
342 if (!gpuobj)
343 return -ENOMEM;
344 NV_DEBUG(dev, "gpuobj %p\n", gpuobj);
Ben Skeggsb3beb162010-09-01 15:24:29 +1000345 gpuobj->dev = dev;
Ben Skeggs43efc9c2010-09-01 15:24:32 +1000346 gpuobj->flags = flags;
Ben Skeggseb9bcbd2010-09-01 15:24:37 +1000347 kref_init(&gpuobj->refcount);
Ben Skeggs43efc9c2010-09-01 15:24:32 +1000348 gpuobj->size = size;
349 gpuobj->pinst = pinst;
Ben Skeggse41115d2010-11-01 11:45:02 +1000350 gpuobj->cinst = NVOBJ_CINST_GLOBAL;
Ben Skeggs43efc9c2010-09-01 15:24:32 +1000351 gpuobj->vinst = vinst;
Ben Skeggsde3a6c02010-09-01 15:24:30 +1000352
Ben Skeggs6ee73862009-12-11 19:24:15 +1000353 if (gpuobj->flags & NVOBJ_FLAG_ZERO_ALLOC) {
Ben Skeggs43efc9c2010-09-01 15:24:32 +1000354 for (i = 0; i < gpuobj->size; i += 4)
Ben Skeggsb3beb162010-09-01 15:24:29 +1000355 nv_wo32(gpuobj, i, 0);
Ben Skeggsf56cb862010-07-08 11:29:10 +1000356 dev_priv->engine.instmem.flush(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000357 }
358
Ben Skeggse05d7ea2010-09-01 15:24:38 +1000359 spin_lock(&dev_priv->ramin_lock);
Ben Skeggs43efc9c2010-09-01 15:24:32 +1000360 list_add_tail(&gpuobj->list, &dev_priv->gpuobj_list);
Ben Skeggse05d7ea2010-09-01 15:24:38 +1000361 spin_unlock(&dev_priv->ramin_lock);
Ben Skeggs43efc9c2010-09-01 15:24:32 +1000362 *pgpuobj = gpuobj;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000363 return 0;
364}
365
366
367static uint32_t
368nouveau_gpuobj_class_instmem_size(struct drm_device *dev, int class)
369{
370 struct drm_nouveau_private *dev_priv = dev->dev_private;
371
372 /*XXX: dodgy hack for now */
373 if (dev_priv->card_type >= NV_50)
374 return 24;
375 if (dev_priv->card_type >= NV_40)
376 return 32;
377 return 16;
378}
379
380/*
381 DMA objects are used to reference a piece of memory in the
382 framebuffer, PCI or AGP address space. Each object is 16 bytes big
383 and looks as follows:
384
385 entry[0]
386 11:0 class (seems like I can always use 0 here)
387 12 page table present?
388 13 page entry linear?
389 15:14 access: 0 rw, 1 ro, 2 wo
390 17:16 target: 0 NV memory, 1 NV memory tiled, 2 PCI, 3 AGP
391 31:20 dma adjust (bits 0-11 of the address)
392 entry[1]
393 dma limit (size of transfer)
394 entry[X]
395 1 0 readonly, 1 readwrite
396 31:12 dma frame address of the page (bits 12-31 of the address)
397 entry[N]
398 page table terminator, same value as the first pte, as does nvidia
399 rivatv uses 0xffffffff
400
401 Non linear page tables need a list of frame addresses afterwards,
402 the rivatv project has some info on this.
403
404 The method below creates a DMA object in instance RAM and returns a handle
405 to it that can be used to set up context objects.
406*/
Ben Skeggs6ee73862009-12-11 19:24:15 +1000407
Ben Skeggs7f4a1952010-11-16 11:50:09 +1000408void
409nv50_gpuobj_dma_init(struct nouveau_gpuobj *obj, u32 offset, int class,
410 u64 base, u64 size, int target, int access,
411 u32 type, u32 comp)
412{
413 struct drm_nouveau_private *dev_priv = obj->dev->dev_private;
414 struct nouveau_instmem_engine *pinstmem = &dev_priv->engine.instmem;
415 u32 flags0;
416
417 flags0 = (comp << 29) | (type << 22) | class;
418 flags0 |= 0x00100000;
419
420 switch (access) {
421 case NV_MEM_ACCESS_RO: flags0 |= 0x00040000; break;
422 case NV_MEM_ACCESS_RW:
423 case NV_MEM_ACCESS_WO: flags0 |= 0x00080000; break;
424 default:
425 break;
426 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000427
428 switch (target) {
Ben Skeggs7f4a1952010-11-16 11:50:09 +1000429 case NV_MEM_TARGET_VRAM:
430 flags0 |= 0x00010000;
431 break;
432 case NV_MEM_TARGET_PCI:
433 flags0 |= 0x00020000;
434 break;
435 case NV_MEM_TARGET_PCI_NOSNOOP:
436 flags0 |= 0x00030000;
437 break;
438 case NV_MEM_TARGET_GART:
439 base += dev_priv->vm_gart_base;
440 default:
441 flags0 &= ~0x00100000;
442 break;
443 }
444
445 /* convert to base + limit */
446 size = (base + size) - 1;
447
448 nv_wo32(obj, offset + 0x00, flags0);
449 nv_wo32(obj, offset + 0x04, lower_32_bits(size));
450 nv_wo32(obj, offset + 0x08, lower_32_bits(base));
451 nv_wo32(obj, offset + 0x0c, upper_32_bits(size) << 24 |
452 upper_32_bits(base));
453 nv_wo32(obj, offset + 0x10, 0x00000000);
454 nv_wo32(obj, offset + 0x14, 0x00000000);
455
456 pinstmem->flush(obj->dev);
457}
458
459int
460nv50_gpuobj_dma_new(struct nouveau_channel *chan, int class, u64 base, u64 size,
461 int target, int access, u32 type, u32 comp,
462 struct nouveau_gpuobj **pobj)
463{
464 struct drm_device *dev = chan->dev;
465 int ret;
466
467 ret = nouveau_gpuobj_new(dev, chan, 24, 16, NVOBJ_FLAG_ZERO_ALLOC |
468 NVOBJ_FLAG_ZERO_FREE, pobj);
469 if (ret)
470 return ret;
471
472 nv50_gpuobj_dma_init(*pobj, 0, class, base, size, target,
473 access, type, comp);
474 return 0;
475}
476
477int
478nouveau_gpuobj_dma_new(struct nouveau_channel *chan, int class, u64 base,
479 u64 size, int access, int target,
480 struct nouveau_gpuobj **pobj)
481{
482 struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
483 struct drm_device *dev = chan->dev;
484 struct nouveau_gpuobj *obj;
485 u32 page_addr, flags0, flags2;
486 int ret;
487
488 if (dev_priv->card_type >= NV_50) {
489 u32 comp = (target == NV_MEM_TARGET_VM) ? NV_MEM_COMP_VM : 0;
490 u32 type = (target == NV_MEM_TARGET_VM) ? NV_MEM_TYPE_VM : 0;
491
492 return nv50_gpuobj_dma_new(chan, class, base, size,
493 target, access, type, comp, pobj);
494 }
495
496 if (target == NV_MEM_TARGET_GART) {
497 if (dev_priv->gart_info.type == NOUVEAU_GART_AGP) {
498 target = NV_MEM_TARGET_PCI_NOSNOOP;
499 base += dev_priv->gart_info.aper_base;
500 } else
501 if (base != 0) {
502 ret = nouveau_sgdma_get_page(dev, base, &page_addr);
503 if (ret)
504 return ret;
505
506 target = NV_MEM_TARGET_PCI;
507 base = page_addr;
508 } else {
509 nouveau_gpuobj_ref(dev_priv->gart_info.sg_ctxdma, pobj);
510 return 0;
511 }
512 }
513
514 flags0 = class;
515 flags0 |= 0x00003000; /* PT present, PT linear */
516 flags2 = 0;
517
518 switch (target) {
519 case NV_MEM_TARGET_PCI:
520 flags0 |= 0x00020000;
521 break;
522 case NV_MEM_TARGET_PCI_NOSNOOP:
523 flags0 |= 0x00030000;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000524 break;
525 default:
526 break;
527 }
528
Ben Skeggs7f4a1952010-11-16 11:50:09 +1000529 switch (access) {
530 case NV_MEM_ACCESS_RO:
531 flags0 |= 0x00004000;
532 break;
533 case NV_MEM_ACCESS_WO:
534 flags0 |= 0x00008000;
535 default:
536 flags2 |= 0x00000002;
537 break;
538 }
539
540 flags0 |= (base & 0x00000fff) << 20;
541 flags2 |= (base & 0xfffff000);
542
543 ret = nouveau_gpuobj_new(dev, chan, (dev_priv->card_type >= NV_40) ?
544 32 : 16, 16, NVOBJ_FLAG_ZERO_ALLOC |
545 NVOBJ_FLAG_ZERO_FREE, &obj);
546 if (ret)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000547 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000548
Ben Skeggs7f4a1952010-11-16 11:50:09 +1000549 nv_wo32(obj, 0x00, flags0);
550 nv_wo32(obj, 0x04, size - 1);
551 nv_wo32(obj, 0x08, flags2);
552 nv_wo32(obj, 0x0c, flags2);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000553
Ben Skeggs7f4a1952010-11-16 11:50:09 +1000554 obj->engine = NVOBJ_ENGINE_SW;
555 obj->class = class;
556 *pobj = obj;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000557 return 0;
558}
559
Ben Skeggs6ee73862009-12-11 19:24:15 +1000560/* Context objects in the instance RAM have the following structure.
561 * On NV40 they are 32 byte long, on NV30 and smaller 16 bytes.
562
563 NV4 - NV30:
564
565 entry[0]
566 11:0 class
567 12 chroma key enable
568 13 user clip enable
569 14 swizzle enable
570 17:15 patch config:
571 scrcopy_and, rop_and, blend_and, scrcopy, srccopy_pre, blend_pre
572 18 synchronize enable
573 19 endian: 1 big, 0 little
574 21:20 dither mode
575 23 single step enable
576 24 patch status: 0 invalid, 1 valid
577 25 context_surface 0: 1 valid
578 26 context surface 1: 1 valid
579 27 context pattern: 1 valid
580 28 context rop: 1 valid
581 29,30 context beta, beta4
582 entry[1]
583 7:0 mono format
584 15:8 color format
585 31:16 notify instance address
586 entry[2]
587 15:0 dma 0 instance address
588 31:16 dma 1 instance address
589 entry[3]
590 dma method traps
591
592 NV40:
593 No idea what the exact format is. Here's what can be deducted:
594
595 entry[0]:
596 11:0 class (maybe uses more bits here?)
597 17 user clip enable
598 21:19 patch config
599 25 patch status valid ?
600 entry[1]:
601 15:0 DMA notifier (maybe 20:0)
602 entry[2]:
603 15:0 DMA 0 instance (maybe 20:0)
604 24 big endian
605 entry[3]:
606 15:0 DMA 1 instance (maybe 20:0)
607 entry[4]:
608 entry[5]:
609 set to 0?
610*/
Ben Skeggsa6a1a382010-10-19 19:57:34 +1000611static int
612nouveau_gpuobj_sw_new(struct nouveau_channel *chan, int class,
613 struct nouveau_gpuobj **gpuobj_ret)
614{
615 struct drm_nouveau_private *dev_priv;
616 struct nouveau_gpuobj *gpuobj;
617
618 if (!chan || !gpuobj_ret || *gpuobj_ret != NULL)
619 return -EINVAL;
620 dev_priv = chan->dev->dev_private;
621
622 gpuobj = kzalloc(sizeof(*gpuobj), GFP_KERNEL);
623 if (!gpuobj)
624 return -ENOMEM;
625 gpuobj->dev = chan->dev;
626 gpuobj->engine = NVOBJ_ENGINE_SW;
627 gpuobj->class = class;
628 kref_init(&gpuobj->refcount);
629 gpuobj->cinst = 0x40;
630
631 spin_lock(&dev_priv->ramin_lock);
632 list_add_tail(&gpuobj->list, &dev_priv->gpuobj_list);
633 spin_unlock(&dev_priv->ramin_lock);
634 *gpuobj_ret = gpuobj;
635 return 0;
636}
637
Ben Skeggs6ee73862009-12-11 19:24:15 +1000638int
639nouveau_gpuobj_gr_new(struct nouveau_channel *chan, int class,
640 struct nouveau_gpuobj **gpuobj)
641{
Ben Skeggsa6a1a382010-10-19 19:57:34 +1000642 struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000643 struct drm_device *dev = chan->dev;
Ben Skeggsb8c157d2010-10-20 10:39:35 +1000644 struct nouveau_gpuobj_class *oc;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000645 int ret;
646
647 NV_DEBUG(dev, "ch%d class=0x%04x\n", chan->id, class);
648
Ben Skeggsb8c157d2010-10-20 10:39:35 +1000649 list_for_each_entry(oc, &dev_priv->classes, head) {
650 if (oc->id == class)
651 goto found;
Ben Skeggsa6a1a382010-10-19 19:57:34 +1000652 }
653
Ben Skeggsb8c157d2010-10-20 10:39:35 +1000654 NV_ERROR(dev, "illegal object class: 0x%x\n", class);
655 return -EINVAL;
Ben Skeggsa6a1a382010-10-19 19:57:34 +1000656
Ben Skeggsb8c157d2010-10-20 10:39:35 +1000657found:
658 if (oc->engine == NVOBJ_ENGINE_SW)
Ben Skeggsa6a1a382010-10-19 19:57:34 +1000659 return nouveau_gpuobj_sw_new(chan, class, gpuobj);
660
Ben Skeggsf4512e62010-10-20 11:47:09 +1000661 switch (oc->engine) {
662 case NVOBJ_ENGINE_GR:
663 if (dev_priv->card_type >= NV_50 && !chan->ramin_grctx) {
664 struct nouveau_pgraph_engine *pgraph =
665 &dev_priv->engine.graph;
666
667 ret = pgraph->create_context(chan);
668 if (ret)
669 return ret;
670 }
671 break;
672 case NVOBJ_ENGINE_CRYPT:
673 if (!chan->crypt_ctx) {
674 struct nouveau_crypt_engine *pcrypt =
675 &dev_priv->engine.crypt;
676
677 ret = pcrypt->create_context(chan);
678 if (ret)
679 return ret;
680 }
681 break;
682 }
683
Ben Skeggs6ee73862009-12-11 19:24:15 +1000684 ret = nouveau_gpuobj_new(dev, chan,
685 nouveau_gpuobj_class_instmem_size(dev, class),
686 16,
687 NVOBJ_FLAG_ZERO_ALLOC | NVOBJ_FLAG_ZERO_FREE,
688 gpuobj);
689 if (ret) {
Ben Skeggsa6a1a382010-10-19 19:57:34 +1000690 NV_ERROR(dev, "error creating gpuobj: %d\n", ret);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000691 return ret;
692 }
693
Ben Skeggs6ee73862009-12-11 19:24:15 +1000694 if (dev_priv->card_type >= NV_50) {
Ben Skeggsb3beb162010-09-01 15:24:29 +1000695 nv_wo32(*gpuobj, 0, class);
696 nv_wo32(*gpuobj, 20, 0x00010000);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000697 } else {
698 switch (class) {
699 case NV_CLASS_NULL:
Ben Skeggsb3beb162010-09-01 15:24:29 +1000700 nv_wo32(*gpuobj, 0, 0x00001030);
701 nv_wo32(*gpuobj, 4, 0xFFFFFFFF);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000702 break;
703 default:
704 if (dev_priv->card_type >= NV_40) {
Ben Skeggsb3beb162010-09-01 15:24:29 +1000705 nv_wo32(*gpuobj, 0, class);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000706#ifdef __BIG_ENDIAN
Ben Skeggsb3beb162010-09-01 15:24:29 +1000707 nv_wo32(*gpuobj, 8, 0x01000000);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000708#endif
709 } else {
710#ifdef __BIG_ENDIAN
Ben Skeggsb3beb162010-09-01 15:24:29 +1000711 nv_wo32(*gpuobj, 0, class | 0x00080000);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000712#else
Ben Skeggsb3beb162010-09-01 15:24:29 +1000713 nv_wo32(*gpuobj, 0, class);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000714#endif
715 }
716 }
717 }
Ben Skeggsf56cb862010-07-08 11:29:10 +1000718 dev_priv->engine.instmem.flush(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000719
Ben Skeggsb8c157d2010-10-20 10:39:35 +1000720 (*gpuobj)->engine = oc->engine;
721 (*gpuobj)->class = oc->id;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000722 return 0;
723}
724
Ben Skeggs6ee73862009-12-11 19:24:15 +1000725static int
726nouveau_gpuobj_channel_init_pramin(struct nouveau_channel *chan)
727{
728 struct drm_device *dev = chan->dev;
729 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000730 uint32_t size;
731 uint32_t base;
732 int ret;
733
734 NV_DEBUG(dev, "ch%d\n", chan->id);
735
736 /* Base amount for object storage (4KiB enough?) */
Ben Skeggsbd2e5972010-10-19 20:06:01 +1000737 size = 0x2000;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000738 base = 0;
739
740 /* PGRAPH context */
Ben Skeggs816544b2010-07-08 13:15:05 +1000741 size += dev_priv->engine.graph.grctx_size;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000742
743 if (dev_priv->card_type == NV_50) {
744 /* Various fixed table thingos */
745 size += 0x1400; /* mostly unknown stuff */
746 size += 0x4000; /* vm pd */
747 base = 0x6000;
748 /* RAMHT, not sure about setting size yet, 32KiB to be safe */
749 size += 0x8000;
750 /* RAMFC */
751 size += 0x1000;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000752 }
753
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000754 ret = nouveau_gpuobj_new(dev, NULL, size, 0x1000, 0, &chan->ramin);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000755 if (ret) {
756 NV_ERROR(dev, "Error allocating channel PRAMIN: %d\n", ret);
757 return ret;
758 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000759
Ben Skeggsde3a6c02010-09-01 15:24:30 +1000760 ret = drm_mm_init(&chan->ramin_heap, base, size);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000761 if (ret) {
762 NV_ERROR(dev, "Error creating PRAMIN heap: %d\n", ret);
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000763 nouveau_gpuobj_ref(NULL, &chan->ramin);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000764 return ret;
765 }
766
767 return 0;
768}
769
770int
771nouveau_gpuobj_channel_init(struct nouveau_channel *chan,
772 uint32_t vram_h, uint32_t tt_h)
773{
774 struct drm_device *dev = chan->dev;
775 struct drm_nouveau_private *dev_priv = dev->dev_private;
776 struct nouveau_instmem_engine *instmem = &dev_priv->engine.instmem;
777 struct nouveau_gpuobj *vram = NULL, *tt = NULL;
778 int ret, i;
779
Ben Skeggs6ee73862009-12-11 19:24:15 +1000780 NV_DEBUG(dev, "ch%d vram=0x%08x tt=0x%08x\n", chan->id, vram_h, tt_h);
781
Ben Skeggs816544b2010-07-08 13:15:05 +1000782 /* Allocate a chunk of memory for per-channel object storage */
783 ret = nouveau_gpuobj_channel_init_pramin(chan);
784 if (ret) {
785 NV_ERROR(dev, "init pramin\n");
786 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000787 }
788
789 /* NV50 VM
790 * - Allocate per-channel page-directory
791 * - Map GART and VRAM into the channel's address space at the
792 * locations determined during init.
793 */
794 if (dev_priv->card_type >= NV_50) {
Ben Skeggs5125bfd2010-09-01 15:24:33 +1000795 u32 pgd_offs = (dev_priv->chipset == 0x50) ? 0x1400 : 0x0200;
796 u64 vm_vinst = chan->ramin->vinst + pgd_offs;
797 u32 vm_pinst = chan->ramin->pinst;
798 u32 pde;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000799
Ben Skeggs5125bfd2010-09-01 15:24:33 +1000800 if (vm_pinst != ~0)
801 vm_pinst += pgd_offs;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000802
Ben Skeggs5125bfd2010-09-01 15:24:33 +1000803 ret = nouveau_gpuobj_new_fake(dev, vm_pinst, vm_vinst, 0x4000,
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000804 0, &chan->vm_pd);
Ben Skeggsf56cb862010-07-08 11:29:10 +1000805 if (ret)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000806 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000807 for (i = 0; i < 0x4000; i += 8) {
Ben Skeggsb3beb162010-09-01 15:24:29 +1000808 nv_wo32(chan->vm_pd, i + 0, 0x00000000);
809 nv_wo32(chan->vm_pd, i + 4, 0xdeadcafe);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000810 }
811
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000812 nouveau_gpuobj_ref(dev_priv->gart_info.sg_ctxdma,
813 &chan->vm_gart_pt);
Ben Skeggsb3beb162010-09-01 15:24:29 +1000814 pde = (dev_priv->vm_gart_base / (512*1024*1024)) * 8;
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000815 nv_wo32(chan->vm_pd, pde + 0, chan->vm_gart_pt->vinst | 3);
Ben Skeggsb3beb162010-09-01 15:24:29 +1000816 nv_wo32(chan->vm_pd, pde + 4, 0x00000000);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000817
Ben Skeggsb3beb162010-09-01 15:24:29 +1000818 pde = (dev_priv->vm_vram_base / (512*1024*1024)) * 8;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000819 for (i = 0; i < dev_priv->vm_vram_pt_nr; i++) {
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000820 nouveau_gpuobj_ref(dev_priv->vm_vram_pt[i],
821 &chan->vm_vram_pt[i]);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000822
Ben Skeggsb3beb162010-09-01 15:24:29 +1000823 nv_wo32(chan->vm_pd, pde + 0,
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000824 chan->vm_vram_pt[i]->vinst | 0x61);
Ben Skeggsb3beb162010-09-01 15:24:29 +1000825 nv_wo32(chan->vm_pd, pde + 4, 0x00000000);
826 pde += 8;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000827 }
828
Ben Skeggsf56cb862010-07-08 11:29:10 +1000829 instmem->flush(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000830 }
831
832 /* RAMHT */
833 if (dev_priv->card_type < NV_50) {
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000834 nouveau_ramht_ref(dev_priv->ramht, &chan->ramht, NULL);
835 } else {
836 struct nouveau_gpuobj *ramht = NULL;
837
838 ret = nouveau_gpuobj_new(dev, chan, 0x8000, 16,
839 NVOBJ_FLAG_ZERO_ALLOC, &ramht);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000840 if (ret)
841 return ret;
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000842
843 ret = nouveau_ramht_new(dev, ramht, &chan->ramht);
844 nouveau_gpuobj_ref(NULL, &ramht);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000845 if (ret)
846 return ret;
847 }
848
849 /* VRAM ctxdma */
850 if (dev_priv->card_type >= NV_50) {
851 ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
852 0, dev_priv->vm_end,
Ben Skeggs7f4a1952010-11-16 11:50:09 +1000853 NV_MEM_ACCESS_RW,
854 NV_MEM_TARGET_VM, &vram);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000855 if (ret) {
856 NV_ERROR(dev, "Error creating VRAM ctxdma: %d\n", ret);
857 return ret;
858 }
859 } else {
860 ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000861 0, dev_priv->fb_available_size,
Ben Skeggs7f4a1952010-11-16 11:50:09 +1000862 NV_MEM_ACCESS_RW,
863 NV_MEM_TARGET_VRAM, &vram);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000864 if (ret) {
865 NV_ERROR(dev, "Error creating VRAM ctxdma: %d\n", ret);
866 return ret;
867 }
868 }
869
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000870 ret = nouveau_ramht_insert(chan, vram_h, vram);
871 nouveau_gpuobj_ref(NULL, &vram);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000872 if (ret) {
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000873 NV_ERROR(dev, "Error adding VRAM ctxdma to RAMHT: %d\n", ret);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000874 return ret;
875 }
876
877 /* TT memory ctxdma */
878 if (dev_priv->card_type >= NV_50) {
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000879 ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
880 0, dev_priv->vm_end,
Ben Skeggs7f4a1952010-11-16 11:50:09 +1000881 NV_MEM_ACCESS_RW,
882 NV_MEM_TARGET_VM, &tt);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000883 } else {
Ben Skeggs7f4a1952010-11-16 11:50:09 +1000884 ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
885 0, dev_priv->gart_info.aper_size,
886 NV_MEM_ACCESS_RW,
887 NV_MEM_TARGET_GART, &tt);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000888 }
889
890 if (ret) {
891 NV_ERROR(dev, "Error creating TT ctxdma: %d\n", ret);
892 return ret;
893 }
894
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000895 ret = nouveau_ramht_insert(chan, tt_h, tt);
896 nouveau_gpuobj_ref(NULL, &tt);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000897 if (ret) {
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000898 NV_ERROR(dev, "Error adding TT ctxdma to RAMHT: %d\n", ret);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000899 return ret;
900 }
901
902 return 0;
903}
904
905void
906nouveau_gpuobj_channel_takedown(struct nouveau_channel *chan)
907{
908 struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
909 struct drm_device *dev = chan->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000910 int i;
911
912 NV_DEBUG(dev, "ch%d\n", chan->id);
913
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000914 if (!chan->ramht)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000915 return;
916
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000917 nouveau_ramht_ref(NULL, &chan->ramht, chan);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000918
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000919 nouveau_gpuobj_ref(NULL, &chan->vm_pd);
920 nouveau_gpuobj_ref(NULL, &chan->vm_gart_pt);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000921 for (i = 0; i < dev_priv->vm_vram_pt_nr; i++)
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000922 nouveau_gpuobj_ref(NULL, &chan->vm_vram_pt[i]);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000923
Ben Skeggsb833ac22010-06-01 15:32:24 +1000924 if (chan->ramin_heap.free_stack.next)
925 drm_mm_takedown(&chan->ramin_heap);
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000926 nouveau_gpuobj_ref(NULL, &chan->ramin);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000927}
928
929int
930nouveau_gpuobj_suspend(struct drm_device *dev)
931{
932 struct drm_nouveau_private *dev_priv = dev->dev_private;
933 struct nouveau_gpuobj *gpuobj;
934 int i;
935
Ben Skeggs6ee73862009-12-11 19:24:15 +1000936 list_for_each_entry(gpuobj, &dev_priv->gpuobj_list, list) {
Ben Skeggse41115d2010-11-01 11:45:02 +1000937 if (gpuobj->cinst != NVOBJ_CINST_GLOBAL)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000938 continue;
939
Ben Skeggsdc1e5c02010-10-25 15:23:59 +1000940 gpuobj->suspend = vmalloc(gpuobj->size);
941 if (!gpuobj->suspend) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000942 nouveau_gpuobj_resume(dev);
943 return -ENOMEM;
944 }
945
Ben Skeggs43efc9c2010-09-01 15:24:32 +1000946 for (i = 0; i < gpuobj->size; i += 4)
Ben Skeggsdc1e5c02010-10-25 15:23:59 +1000947 gpuobj->suspend[i/4] = nv_ro32(gpuobj, i);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000948 }
949
950 return 0;
951}
952
953void
Ben Skeggs6ee73862009-12-11 19:24:15 +1000954nouveau_gpuobj_resume(struct drm_device *dev)
955{
956 struct drm_nouveau_private *dev_priv = dev->dev_private;
957 struct nouveau_gpuobj *gpuobj;
958 int i;
959
Ben Skeggs6ee73862009-12-11 19:24:15 +1000960 list_for_each_entry(gpuobj, &dev_priv->gpuobj_list, list) {
Ben Skeggsdc1e5c02010-10-25 15:23:59 +1000961 if (!gpuobj->suspend)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000962 continue;
963
Ben Skeggs43efc9c2010-09-01 15:24:32 +1000964 for (i = 0; i < gpuobj->size; i += 4)
Ben Skeggsdc1e5c02010-10-25 15:23:59 +1000965 nv_wo32(gpuobj, i, gpuobj->suspend[i/4]);
966
967 vfree(gpuobj->suspend);
968 gpuobj->suspend = NULL;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000969 }
970
Ben Skeggsdc1e5c02010-10-25 15:23:59 +1000971 dev_priv->engine.instmem.flush(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000972}
973
974int nouveau_ioctl_grobj_alloc(struct drm_device *dev, void *data,
975 struct drm_file *file_priv)
976{
Ben Skeggs6ee73862009-12-11 19:24:15 +1000977 struct drm_nouveau_grobj_alloc *init = data;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000978 struct nouveau_gpuobj *gr = NULL;
979 struct nouveau_channel *chan;
980 int ret;
981
Ben Skeggs6ee73862009-12-11 19:24:15 +1000982 if (init->handle == ~0)
983 return -EINVAL;
984
Ben Skeggscff5c132010-10-06 16:16:59 +1000985 chan = nouveau_channel_get(dev, file_priv, init->channel);
986 if (IS_ERR(chan))
987 return PTR_ERR(chan);
988
989 if (nouveau_ramht_find(chan, init->handle)) {
990 ret = -EEXIST;
991 goto out;
992 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000993
Ben Skeggsa6a1a382010-10-19 19:57:34 +1000994 ret = nouveau_gpuobj_gr_new(chan, init->class, &gr);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000995 if (ret) {
996 NV_ERROR(dev, "Error creating object: %d (%d/0x%08x)\n",
997 ret, init->channel, init->handle);
Ben Skeggscff5c132010-10-06 16:16:59 +1000998 goto out;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000999 }
1000
Ben Skeggsa8eaebc2010-09-01 15:24:31 +10001001 ret = nouveau_ramht_insert(chan, init->handle, gr);
1002 nouveau_gpuobj_ref(NULL, &gr);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001003 if (ret) {
1004 NV_ERROR(dev, "Error referencing object: %d (%d/0x%08x)\n",
1005 ret, init->channel, init->handle);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001006 }
1007
Ben Skeggscff5c132010-10-06 16:16:59 +10001008out:
1009 nouveau_channel_put(&chan);
1010 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001011}
1012
1013int nouveau_ioctl_gpuobj_free(struct drm_device *dev, void *data,
1014 struct drm_file *file_priv)
1015{
1016 struct drm_nouveau_gpuobj_free *objfree = data;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001017 struct nouveau_channel *chan;
Ben Skeggs18a16a72010-10-12 10:11:00 +10001018 int ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001019
Ben Skeggscff5c132010-10-06 16:16:59 +10001020 chan = nouveau_channel_get(dev, file_priv, objfree->channel);
1021 if (IS_ERR(chan))
1022 return PTR_ERR(chan);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001023
Ben Skeggs18a16a72010-10-12 10:11:00 +10001024 ret = nouveau_ramht_remove(chan, objfree->handle);
Ben Skeggscff5c132010-10-06 16:16:59 +10001025 nouveau_channel_put(&chan);
1026 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001027}
Ben Skeggsb3beb162010-09-01 15:24:29 +10001028
1029u32
1030nv_ro32(struct nouveau_gpuobj *gpuobj, u32 offset)
1031{
Ben Skeggs5125bfd2010-09-01 15:24:33 +10001032 struct drm_nouveau_private *dev_priv = gpuobj->dev->dev_private;
1033 struct drm_device *dev = gpuobj->dev;
1034
1035 if (gpuobj->pinst == ~0 || !dev_priv->ramin_available) {
1036 u64 ptr = gpuobj->vinst + offset;
1037 u32 base = ptr >> 16;
1038 u32 val;
1039
1040 spin_lock(&dev_priv->ramin_lock);
1041 if (dev_priv->ramin_base != base) {
1042 dev_priv->ramin_base = base;
1043 nv_wr32(dev, 0x001700, dev_priv->ramin_base);
1044 }
1045 val = nv_rd32(dev, 0x700000 + (ptr & 0xffff));
1046 spin_unlock(&dev_priv->ramin_lock);
1047 return val;
1048 }
1049
1050 return nv_ri32(dev, gpuobj->pinst + offset);
Ben Skeggsb3beb162010-09-01 15:24:29 +10001051}
1052
1053void
1054nv_wo32(struct nouveau_gpuobj *gpuobj, u32 offset, u32 val)
1055{
Ben Skeggs5125bfd2010-09-01 15:24:33 +10001056 struct drm_nouveau_private *dev_priv = gpuobj->dev->dev_private;
1057 struct drm_device *dev = gpuobj->dev;
1058
1059 if (gpuobj->pinst == ~0 || !dev_priv->ramin_available) {
1060 u64 ptr = gpuobj->vinst + offset;
1061 u32 base = ptr >> 16;
1062
1063 spin_lock(&dev_priv->ramin_lock);
1064 if (dev_priv->ramin_base != base) {
1065 dev_priv->ramin_base = base;
1066 nv_wr32(dev, 0x001700, dev_priv->ramin_base);
1067 }
1068 nv_wr32(dev, 0x700000 + (ptr & 0xffff), val);
1069 spin_unlock(&dev_priv->ramin_lock);
1070 return;
1071 }
1072
1073 nv_wi32(dev, gpuobj->pinst + offset, val);
Ben Skeggsb3beb162010-09-01 15:24:29 +10001074}