blob: 54078186fe655a5399f76abd19171a9aa7adec69 [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 Skeggs6ee73862009-12-11 19:24:15 +1000116/* NVidia uses context objects to drive drawing operations.
117
118 Context objects can be selected into 8 subchannels in the FIFO,
119 and then used via DMA command buffers.
120
121 A context object is referenced by a user defined handle (CARD32). The HW
122 looks up graphics objects in a hash table in the instance RAM.
123
124 An entry in the hash table consists of 2 CARD32. The first CARD32 contains
125 the handle, the second one a bitfield, that contains the address of the
126 object in instance RAM.
127
128 The format of the second CARD32 seems to be:
129
130 NV4 to NV30:
131
132 15: 0 instance_addr >> 4
133 17:16 engine (here uses 1 = graphics)
134 28:24 channel id (here uses 0)
135 31 valid (use 1)
136
137 NV40:
138
139 15: 0 instance_addr >> 4 (maybe 19-0)
140 21:20 engine (here uses 1 = graphics)
141 I'm unsure about the other bits, but using 0 seems to work.
142
143 The key into the hash table depends on the object handle and channel id and
144 is given as:
145*/
Ben Skeggs6ee73862009-12-11 19:24:15 +1000146
147int
148nouveau_gpuobj_new(struct drm_device *dev, struct nouveau_channel *chan,
149 uint32_t size, int align, uint32_t flags,
150 struct nouveau_gpuobj **gpuobj_ret)
151{
152 struct drm_nouveau_private *dev_priv = dev->dev_private;
153 struct nouveau_engine *engine = &dev_priv->engine;
154 struct nouveau_gpuobj *gpuobj;
Ben Skeggs5125bfd2010-09-01 15:24:33 +1000155 struct drm_mm_node *ramin = NULL;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000156 int ret;
157
158 NV_DEBUG(dev, "ch%d size=%u align=%d flags=0x%08x\n",
159 chan ? chan->id : -1, size, align, flags);
160
161 if (!dev_priv || !gpuobj_ret || *gpuobj_ret != NULL)
162 return -EINVAL;
163
164 gpuobj = kzalloc(sizeof(*gpuobj), GFP_KERNEL);
165 if (!gpuobj)
166 return -ENOMEM;
167 NV_DEBUG(dev, "gpuobj %p\n", gpuobj);
Ben Skeggsb3beb162010-09-01 15:24:29 +1000168 gpuobj->dev = dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000169 gpuobj->flags = flags;
Ben Skeggseb9bcbd2010-09-01 15:24:37 +1000170 kref_init(&gpuobj->refcount);
Ben Skeggs43efc9c2010-09-01 15:24:32 +1000171 gpuobj->size = size;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000172
Ben Skeggse05d7ea2010-09-01 15:24:38 +1000173 spin_lock(&dev_priv->ramin_lock);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000174 list_add_tail(&gpuobj->list, &dev_priv->gpuobj_list);
Ben Skeggse05d7ea2010-09-01 15:24:38 +1000175 spin_unlock(&dev_priv->ramin_lock);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000176
Ben Skeggs6ee73862009-12-11 19:24:15 +1000177 if (chan) {
Ben Skeggs816544b2010-07-08 13:15:05 +1000178 NV_DEBUG(dev, "channel heap\n");
Ben Skeggs5125bfd2010-09-01 15:24:33 +1000179
180 ramin = drm_mm_search_free(&chan->ramin_heap, size, align, 0);
181 if (ramin)
182 ramin = drm_mm_get_block(ramin, size, align);
183
184 if (!ramin) {
185 nouveau_gpuobj_ref(NULL, &gpuobj);
186 return -ENOMEM;
187 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000188 } else {
189 NV_DEBUG(dev, "global heap\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000190
Ben Skeggs5125bfd2010-09-01 15:24:33 +1000191 /* allocate backing pages, sets vinst */
Ben Skeggs91004682010-10-15 09:15:26 +1000192 ret = engine->instmem.populate(dev, gpuobj, &size, align);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000193 if (ret) {
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000194 nouveau_gpuobj_ref(NULL, &gpuobj);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000195 return ret;
196 }
Ben Skeggs5125bfd2010-09-01 15:24:33 +1000197
198 /* try and get aperture space */
Ben Skeggse05d7ea2010-09-01 15:24:38 +1000199 do {
200 if (drm_mm_pre_get(&dev_priv->ramin_heap))
201 return -ENOMEM;
202
203 spin_lock(&dev_priv->ramin_lock);
204 ramin = drm_mm_search_free(&dev_priv->ramin_heap, size,
205 align, 0);
206 if (ramin == NULL) {
207 spin_unlock(&dev_priv->ramin_lock);
208 nouveau_gpuobj_ref(NULL, &gpuobj);
Francisco Jerezdd661e52010-11-01 18:06:28 +0100209 return -ENOMEM;
Ben Skeggse05d7ea2010-09-01 15:24:38 +1000210 }
211
212 ramin = drm_mm_get_block_atomic(ramin, size, align);
213 spin_unlock(&dev_priv->ramin_lock);
214 } while (ramin == NULL);
Ben Skeggs5125bfd2010-09-01 15:24:33 +1000215
216 /* on nv50 it's ok to fail, we have a fallback path */
217 if (!ramin && dev_priv->card_type < NV_50) {
218 nouveau_gpuobj_ref(NULL, &gpuobj);
219 return -ENOMEM;
220 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000221 }
222
Ben Skeggs5125bfd2010-09-01 15:24:33 +1000223 /* if we got a chunk of the aperture, map pages into it */
224 gpuobj->im_pramin = ramin;
Ben Skeggsfbd2895e2010-09-01 15:24:34 +1000225 if (!chan && gpuobj->im_pramin && dev_priv->ramin_available) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000226 ret = engine->instmem.bind(dev, gpuobj);
227 if (ret) {
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000228 nouveau_gpuobj_ref(NULL, &gpuobj);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000229 return ret;
230 }
231 }
232
Ben Skeggsde3a6c02010-09-01 15:24:30 +1000233 /* calculate the various different addresses for the object */
234 if (chan) {
Ben Skeggs5125bfd2010-09-01 15:24:33 +1000235 gpuobj->pinst = chan->ramin->pinst;
236 if (gpuobj->pinst != ~0)
237 gpuobj->pinst += gpuobj->im_pramin->start;
238
Ben Skeggsde3a6c02010-09-01 15:24:30 +1000239 if (dev_priv->card_type < NV_50) {
240 gpuobj->cinst = gpuobj->pinst;
241 } else {
242 gpuobj->cinst = gpuobj->im_pramin->start;
243 gpuobj->vinst = gpuobj->im_pramin->start +
Ben Skeggs43efc9c2010-09-01 15:24:32 +1000244 chan->ramin->vinst;
Ben Skeggsde3a6c02010-09-01 15:24:30 +1000245 }
246 } else {
Ben Skeggs5125bfd2010-09-01 15:24:33 +1000247 if (gpuobj->im_pramin)
248 gpuobj->pinst = gpuobj->im_pramin->start;
249 else
250 gpuobj->pinst = ~0;
Ben Skeggsde3a6c02010-09-01 15:24:30 +1000251 gpuobj->cinst = 0xdeadbeef;
Ben Skeggsde3a6c02010-09-01 15:24:30 +1000252 }
253
Ben Skeggs6ee73862009-12-11 19:24:15 +1000254 if (gpuobj->flags & NVOBJ_FLAG_ZERO_ALLOC) {
255 int i;
256
Ben Skeggs43efc9c2010-09-01 15:24:32 +1000257 for (i = 0; i < gpuobj->size; i += 4)
Ben Skeggsb3beb162010-09-01 15:24:29 +1000258 nv_wo32(gpuobj, i, 0);
Ben Skeggsf56cb862010-07-08 11:29:10 +1000259 engine->instmem.flush(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000260 }
261
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000262
Ben Skeggs6ee73862009-12-11 19:24:15 +1000263 *gpuobj_ret = gpuobj;
264 return 0;
265}
266
267int
Ben Skeggsfbd2895e2010-09-01 15:24:34 +1000268nouveau_gpuobj_init(struct drm_device *dev)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000269{
270 struct drm_nouveau_private *dev_priv = dev->dev_private;
271
272 NV_DEBUG(dev, "\n");
273
274 INIT_LIST_HEAD(&dev_priv->gpuobj_list);
Ben Skeggsbd2e5972010-10-19 20:06:01 +1000275 INIT_LIST_HEAD(&dev_priv->classes);
Ben Skeggs5125bfd2010-09-01 15:24:33 +1000276 spin_lock_init(&dev_priv->ramin_lock);
277 dev_priv->ramin_base = ~0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000278
279 return 0;
280}
281
Ben Skeggs6ee73862009-12-11 19:24:15 +1000282void
283nouveau_gpuobj_takedown(struct drm_device *dev)
284{
285 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggsb8c157d2010-10-20 10:39:35 +1000286 struct nouveau_gpuobj_method *om, *tm;
287 struct nouveau_gpuobj_class *oc, *tc;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000288
289 NV_DEBUG(dev, "\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000290
Ben Skeggsb8c157d2010-10-20 10:39:35 +1000291 list_for_each_entry_safe(oc, tc, &dev_priv->classes, head) {
292 list_for_each_entry_safe(om, tm, &oc->methods, head) {
293 list_del(&om->head);
294 kfree(om);
295 }
296 list_del(&oc->head);
297 kfree(oc);
298 }
299
Ben Skeggseb9bcbd2010-09-01 15:24:37 +1000300 BUG_ON(!list_empty(&dev_priv->gpuobj_list));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000301}
302
Ben Skeggs185abec2010-09-01 15:24:39 +1000303
Ben Skeggseb9bcbd2010-09-01 15:24:37 +1000304static void
305nouveau_gpuobj_del(struct kref *ref)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000306{
Ben Skeggseb9bcbd2010-09-01 15:24:37 +1000307 struct nouveau_gpuobj *gpuobj =
308 container_of(ref, struct nouveau_gpuobj, refcount);
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000309 struct drm_device *dev = gpuobj->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000310 struct drm_nouveau_private *dev_priv = dev->dev_private;
311 struct nouveau_engine *engine = &dev_priv->engine;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000312 int i;
313
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000314 NV_DEBUG(dev, "gpuobj %p\n", gpuobj);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000315
316 if (gpuobj->im_pramin && (gpuobj->flags & NVOBJ_FLAG_ZERO_FREE)) {
Ben Skeggs43efc9c2010-09-01 15:24:32 +1000317 for (i = 0; i < gpuobj->size; i += 4)
Ben Skeggsb3beb162010-09-01 15:24:29 +1000318 nv_wo32(gpuobj, i, 0);
Ben Skeggsf56cb862010-07-08 11:29:10 +1000319 engine->instmem.flush(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000320 }
321
322 if (gpuobj->dtor)
323 gpuobj->dtor(dev, gpuobj);
324
Ben Skeggs43efc9c2010-09-01 15:24:32 +1000325 if (gpuobj->im_backing)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000326 engine->instmem.clear(dev, gpuobj);
327
Ben Skeggse05d7ea2010-09-01 15:24:38 +1000328 spin_lock(&dev_priv->ramin_lock);
Ben Skeggs43efc9c2010-09-01 15:24:32 +1000329 if (gpuobj->im_pramin)
330 drm_mm_put_block(gpuobj->im_pramin);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000331 list_del(&gpuobj->list);
Ben Skeggse05d7ea2010-09-01 15:24:38 +1000332 spin_unlock(&dev_priv->ramin_lock);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000333
Ben Skeggs6ee73862009-12-11 19:24:15 +1000334 kfree(gpuobj);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000335}
336
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000337void
338nouveau_gpuobj_ref(struct nouveau_gpuobj *ref, struct nouveau_gpuobj **ptr)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000339{
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000340 if (ref)
Ben Skeggseb9bcbd2010-09-01 15:24:37 +1000341 kref_get(&ref->refcount);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000342
Ben Skeggseb9bcbd2010-09-01 15:24:37 +1000343 if (*ptr)
344 kref_put(&(*ptr)->refcount, nouveau_gpuobj_del);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000345
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000346 *ptr = ref;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000347}
348
349int
Ben Skeggs43efc9c2010-09-01 15:24:32 +1000350nouveau_gpuobj_new_fake(struct drm_device *dev, u32 pinst, u64 vinst,
351 u32 size, u32 flags, struct nouveau_gpuobj **pgpuobj)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000352{
353 struct drm_nouveau_private *dev_priv = dev->dev_private;
354 struct nouveau_gpuobj *gpuobj = NULL;
355 int i;
356
357 NV_DEBUG(dev,
Ben Skeggs43efc9c2010-09-01 15:24:32 +1000358 "pinst=0x%08x vinst=0x%010llx size=0x%08x flags=0x%08x\n",
359 pinst, vinst, size, flags);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000360
361 gpuobj = kzalloc(sizeof(*gpuobj), GFP_KERNEL);
362 if (!gpuobj)
363 return -ENOMEM;
364 NV_DEBUG(dev, "gpuobj %p\n", gpuobj);
Ben Skeggsb3beb162010-09-01 15:24:29 +1000365 gpuobj->dev = dev;
Ben Skeggs43efc9c2010-09-01 15:24:32 +1000366 gpuobj->flags = flags;
Ben Skeggseb9bcbd2010-09-01 15:24:37 +1000367 kref_init(&gpuobj->refcount);
Ben Skeggs43efc9c2010-09-01 15:24:32 +1000368 gpuobj->size = size;
369 gpuobj->pinst = pinst;
Ben Skeggsde3a6c02010-09-01 15:24:30 +1000370 gpuobj->cinst = 0xdeadbeef;
Ben Skeggs43efc9c2010-09-01 15:24:32 +1000371 gpuobj->vinst = vinst;
Ben Skeggsde3a6c02010-09-01 15:24:30 +1000372
Ben Skeggs6ee73862009-12-11 19:24:15 +1000373 if (gpuobj->flags & NVOBJ_FLAG_ZERO_ALLOC) {
Ben Skeggs43efc9c2010-09-01 15:24:32 +1000374 for (i = 0; i < gpuobj->size; i += 4)
Ben Skeggsb3beb162010-09-01 15:24:29 +1000375 nv_wo32(gpuobj, i, 0);
Ben Skeggsf56cb862010-07-08 11:29:10 +1000376 dev_priv->engine.instmem.flush(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000377 }
378
Ben Skeggse05d7ea2010-09-01 15:24:38 +1000379 spin_lock(&dev_priv->ramin_lock);
Ben Skeggs43efc9c2010-09-01 15:24:32 +1000380 list_add_tail(&gpuobj->list, &dev_priv->gpuobj_list);
Ben Skeggse05d7ea2010-09-01 15:24:38 +1000381 spin_unlock(&dev_priv->ramin_lock);
Ben Skeggs43efc9c2010-09-01 15:24:32 +1000382 *pgpuobj = gpuobj;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000383 return 0;
384}
385
386
387static uint32_t
388nouveau_gpuobj_class_instmem_size(struct drm_device *dev, int class)
389{
390 struct drm_nouveau_private *dev_priv = dev->dev_private;
391
392 /*XXX: dodgy hack for now */
393 if (dev_priv->card_type >= NV_50)
394 return 24;
395 if (dev_priv->card_type >= NV_40)
396 return 32;
397 return 16;
398}
399
400/*
401 DMA objects are used to reference a piece of memory in the
402 framebuffer, PCI or AGP address space. Each object is 16 bytes big
403 and looks as follows:
404
405 entry[0]
406 11:0 class (seems like I can always use 0 here)
407 12 page table present?
408 13 page entry linear?
409 15:14 access: 0 rw, 1 ro, 2 wo
410 17:16 target: 0 NV memory, 1 NV memory tiled, 2 PCI, 3 AGP
411 31:20 dma adjust (bits 0-11 of the address)
412 entry[1]
413 dma limit (size of transfer)
414 entry[X]
415 1 0 readonly, 1 readwrite
416 31:12 dma frame address of the page (bits 12-31 of the address)
417 entry[N]
418 page table terminator, same value as the first pte, as does nvidia
419 rivatv uses 0xffffffff
420
421 Non linear page tables need a list of frame addresses afterwards,
422 the rivatv project has some info on this.
423
424 The method below creates a DMA object in instance RAM and returns a handle
425 to it that can be used to set up context objects.
426*/
427int
428nouveau_gpuobj_dma_new(struct nouveau_channel *chan, int class,
429 uint64_t offset, uint64_t size, int access,
430 int target, struct nouveau_gpuobj **gpuobj)
431{
432 struct drm_device *dev = chan->dev;
433 struct drm_nouveau_private *dev_priv = dev->dev_private;
434 struct nouveau_instmem_engine *instmem = &dev_priv->engine.instmem;
435 int ret;
436
437 NV_DEBUG(dev, "ch%d class=0x%04x offset=0x%llx size=0x%llx\n",
438 chan->id, class, offset, size);
439 NV_DEBUG(dev, "access=%d target=%d\n", access, target);
440
441 switch (target) {
442 case NV_DMA_TARGET_AGP:
443 offset += dev_priv->gart_info.aper_base;
444 break;
445 default:
446 break;
447 }
448
449 ret = nouveau_gpuobj_new(dev, chan,
450 nouveau_gpuobj_class_instmem_size(dev, class),
451 16, NVOBJ_FLAG_ZERO_ALLOC |
452 NVOBJ_FLAG_ZERO_FREE, gpuobj);
453 if (ret) {
454 NV_ERROR(dev, "Error creating gpuobj: %d\n", ret);
455 return ret;
456 }
457
Ben Skeggs6ee73862009-12-11 19:24:15 +1000458 if (dev_priv->card_type < NV_50) {
459 uint32_t frame, adjust, pte_flags = 0;
460
461 if (access != NV_DMA_ACCESS_RO)
462 pte_flags |= (1<<1);
463 adjust = offset & 0x00000fff;
464 frame = offset & ~0x00000fff;
465
Ben Skeggsb3beb162010-09-01 15:24:29 +1000466 nv_wo32(*gpuobj, 0, ((1<<12) | (1<<13) | (adjust << 20) |
467 (access << 14) | (target << 16) |
468 class));
469 nv_wo32(*gpuobj, 4, size - 1);
470 nv_wo32(*gpuobj, 8, frame | pte_flags);
471 nv_wo32(*gpuobj, 12, frame | pte_flags);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000472 } else {
473 uint64_t limit = offset + size - 1;
474 uint32_t flags0, flags5;
475
476 if (target == NV_DMA_TARGET_VIDMEM) {
477 flags0 = 0x00190000;
478 flags5 = 0x00010000;
479 } else {
480 flags0 = 0x7fc00000;
481 flags5 = 0x00080000;
482 }
483
Ben Skeggsb3beb162010-09-01 15:24:29 +1000484 nv_wo32(*gpuobj, 0, flags0 | class);
485 nv_wo32(*gpuobj, 4, lower_32_bits(limit));
486 nv_wo32(*gpuobj, 8, lower_32_bits(offset));
487 nv_wo32(*gpuobj, 12, ((upper_32_bits(limit) & 0xff) << 24) |
488 (upper_32_bits(offset) & 0xff));
489 nv_wo32(*gpuobj, 20, flags5);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000490 }
491
Ben Skeggsf56cb862010-07-08 11:29:10 +1000492 instmem->flush(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000493
494 (*gpuobj)->engine = NVOBJ_ENGINE_SW;
495 (*gpuobj)->class = class;
496 return 0;
497}
498
499int
500nouveau_gpuobj_gart_dma_new(struct nouveau_channel *chan,
501 uint64_t offset, uint64_t size, int access,
502 struct nouveau_gpuobj **gpuobj,
503 uint32_t *o_ret)
504{
505 struct drm_device *dev = chan->dev;
506 struct drm_nouveau_private *dev_priv = dev->dev_private;
507 int ret;
508
509 if (dev_priv->gart_info.type == NOUVEAU_GART_AGP ||
510 (dev_priv->card_type >= NV_50 &&
511 dev_priv->gart_info.type == NOUVEAU_GART_SGDMA)) {
512 ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
513 offset + dev_priv->vm_gart_base,
514 size, access, NV_DMA_TARGET_AGP,
515 gpuobj);
516 if (o_ret)
517 *o_ret = 0;
518 } else
519 if (dev_priv->gart_info.type == NOUVEAU_GART_SGDMA) {
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000520 nouveau_gpuobj_ref(dev_priv->gart_info.sg_ctxdma, gpuobj);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000521 if (offset & ~0xffffffffULL) {
522 NV_ERROR(dev, "obj offset exceeds 32-bits\n");
523 return -EINVAL;
524 }
525 if (o_ret)
526 *o_ret = (uint32_t)offset;
527 ret = (*gpuobj != NULL) ? 0 : -EINVAL;
528 } else {
529 NV_ERROR(dev, "Invalid GART type %d\n", dev_priv->gart_info.type);
530 return -EINVAL;
531 }
532
533 return ret;
534}
535
536/* Context objects in the instance RAM have the following structure.
537 * On NV40 they are 32 byte long, on NV30 and smaller 16 bytes.
538
539 NV4 - NV30:
540
541 entry[0]
542 11:0 class
543 12 chroma key enable
544 13 user clip enable
545 14 swizzle enable
546 17:15 patch config:
547 scrcopy_and, rop_and, blend_and, scrcopy, srccopy_pre, blend_pre
548 18 synchronize enable
549 19 endian: 1 big, 0 little
550 21:20 dither mode
551 23 single step enable
552 24 patch status: 0 invalid, 1 valid
553 25 context_surface 0: 1 valid
554 26 context surface 1: 1 valid
555 27 context pattern: 1 valid
556 28 context rop: 1 valid
557 29,30 context beta, beta4
558 entry[1]
559 7:0 mono format
560 15:8 color format
561 31:16 notify instance address
562 entry[2]
563 15:0 dma 0 instance address
564 31:16 dma 1 instance address
565 entry[3]
566 dma method traps
567
568 NV40:
569 No idea what the exact format is. Here's what can be deducted:
570
571 entry[0]:
572 11:0 class (maybe uses more bits here?)
573 17 user clip enable
574 21:19 patch config
575 25 patch status valid ?
576 entry[1]:
577 15:0 DMA notifier (maybe 20:0)
578 entry[2]:
579 15:0 DMA 0 instance (maybe 20:0)
580 24 big endian
581 entry[3]:
582 15:0 DMA 1 instance (maybe 20:0)
583 entry[4]:
584 entry[5]:
585 set to 0?
586*/
Ben Skeggsa6a1a382010-10-19 19:57:34 +1000587static int
588nouveau_gpuobj_sw_new(struct nouveau_channel *chan, int class,
589 struct nouveau_gpuobj **gpuobj_ret)
590{
591 struct drm_nouveau_private *dev_priv;
592 struct nouveau_gpuobj *gpuobj;
593
594 if (!chan || !gpuobj_ret || *gpuobj_ret != NULL)
595 return -EINVAL;
596 dev_priv = chan->dev->dev_private;
597
598 gpuobj = kzalloc(sizeof(*gpuobj), GFP_KERNEL);
599 if (!gpuobj)
600 return -ENOMEM;
601 gpuobj->dev = chan->dev;
602 gpuobj->engine = NVOBJ_ENGINE_SW;
603 gpuobj->class = class;
604 kref_init(&gpuobj->refcount);
605 gpuobj->cinst = 0x40;
606
607 spin_lock(&dev_priv->ramin_lock);
608 list_add_tail(&gpuobj->list, &dev_priv->gpuobj_list);
609 spin_unlock(&dev_priv->ramin_lock);
610 *gpuobj_ret = gpuobj;
611 return 0;
612}
613
Ben Skeggs6ee73862009-12-11 19:24:15 +1000614int
615nouveau_gpuobj_gr_new(struct nouveau_channel *chan, int class,
616 struct nouveau_gpuobj **gpuobj)
617{
Ben Skeggsa6a1a382010-10-19 19:57:34 +1000618 struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000619 struct drm_device *dev = chan->dev;
Ben Skeggsb8c157d2010-10-20 10:39:35 +1000620 struct nouveau_gpuobj_class *oc;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000621 int ret;
622
623 NV_DEBUG(dev, "ch%d class=0x%04x\n", chan->id, class);
624
Ben Skeggsb8c157d2010-10-20 10:39:35 +1000625 list_for_each_entry(oc, &dev_priv->classes, head) {
626 if (oc->id == class)
627 goto found;
Ben Skeggsa6a1a382010-10-19 19:57:34 +1000628 }
629
Ben Skeggsb8c157d2010-10-20 10:39:35 +1000630 NV_ERROR(dev, "illegal object class: 0x%x\n", class);
631 return -EINVAL;
Ben Skeggsa6a1a382010-10-19 19:57:34 +1000632
Ben Skeggsb8c157d2010-10-20 10:39:35 +1000633found:
634 if (oc->engine == NVOBJ_ENGINE_SW)
Ben Skeggsa6a1a382010-10-19 19:57:34 +1000635 return nouveau_gpuobj_sw_new(chan, class, gpuobj);
636
Ben Skeggsf4512e62010-10-20 11:47:09 +1000637 switch (oc->engine) {
638 case NVOBJ_ENGINE_GR:
639 if (dev_priv->card_type >= NV_50 && !chan->ramin_grctx) {
640 struct nouveau_pgraph_engine *pgraph =
641 &dev_priv->engine.graph;
642
643 ret = pgraph->create_context(chan);
644 if (ret)
645 return ret;
646 }
647 break;
648 case NVOBJ_ENGINE_CRYPT:
649 if (!chan->crypt_ctx) {
650 struct nouveau_crypt_engine *pcrypt =
651 &dev_priv->engine.crypt;
652
653 ret = pcrypt->create_context(chan);
654 if (ret)
655 return ret;
656 }
657 break;
658 }
659
Ben Skeggs6ee73862009-12-11 19:24:15 +1000660 ret = nouveau_gpuobj_new(dev, chan,
661 nouveau_gpuobj_class_instmem_size(dev, class),
662 16,
663 NVOBJ_FLAG_ZERO_ALLOC | NVOBJ_FLAG_ZERO_FREE,
664 gpuobj);
665 if (ret) {
Ben Skeggsa6a1a382010-10-19 19:57:34 +1000666 NV_ERROR(dev, "error creating gpuobj: %d\n", ret);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000667 return ret;
668 }
669
Ben Skeggs6ee73862009-12-11 19:24:15 +1000670 if (dev_priv->card_type >= NV_50) {
Ben Skeggsb3beb162010-09-01 15:24:29 +1000671 nv_wo32(*gpuobj, 0, class);
672 nv_wo32(*gpuobj, 20, 0x00010000);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000673 } else {
674 switch (class) {
675 case NV_CLASS_NULL:
Ben Skeggsb3beb162010-09-01 15:24:29 +1000676 nv_wo32(*gpuobj, 0, 0x00001030);
677 nv_wo32(*gpuobj, 4, 0xFFFFFFFF);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000678 break;
679 default:
680 if (dev_priv->card_type >= NV_40) {
Ben Skeggsb3beb162010-09-01 15:24:29 +1000681 nv_wo32(*gpuobj, 0, class);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000682#ifdef __BIG_ENDIAN
Ben Skeggsb3beb162010-09-01 15:24:29 +1000683 nv_wo32(*gpuobj, 8, 0x01000000);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000684#endif
685 } else {
686#ifdef __BIG_ENDIAN
Ben Skeggsb3beb162010-09-01 15:24:29 +1000687 nv_wo32(*gpuobj, 0, class | 0x00080000);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000688#else
Ben Skeggsb3beb162010-09-01 15:24:29 +1000689 nv_wo32(*gpuobj, 0, class);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000690#endif
691 }
692 }
693 }
Ben Skeggsf56cb862010-07-08 11:29:10 +1000694 dev_priv->engine.instmem.flush(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000695
Ben Skeggsb8c157d2010-10-20 10:39:35 +1000696 (*gpuobj)->engine = oc->engine;
697 (*gpuobj)->class = oc->id;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000698 return 0;
699}
700
Ben Skeggs6ee73862009-12-11 19:24:15 +1000701static int
702nouveau_gpuobj_channel_init_pramin(struct nouveau_channel *chan)
703{
704 struct drm_device *dev = chan->dev;
705 struct drm_nouveau_private *dev_priv = dev->dev_private;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000706 uint32_t size;
707 uint32_t base;
708 int ret;
709
710 NV_DEBUG(dev, "ch%d\n", chan->id);
711
712 /* Base amount for object storage (4KiB enough?) */
Ben Skeggsbd2e5972010-10-19 20:06:01 +1000713 size = 0x2000;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000714 base = 0;
715
716 /* PGRAPH context */
Ben Skeggs816544b2010-07-08 13:15:05 +1000717 size += dev_priv->engine.graph.grctx_size;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000718
719 if (dev_priv->card_type == NV_50) {
720 /* Various fixed table thingos */
721 size += 0x1400; /* mostly unknown stuff */
722 size += 0x4000; /* vm pd */
723 base = 0x6000;
724 /* RAMHT, not sure about setting size yet, 32KiB to be safe */
725 size += 0x8000;
726 /* RAMFC */
727 size += 0x1000;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000728 }
729
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000730 ret = nouveau_gpuobj_new(dev, NULL, size, 0x1000, 0, &chan->ramin);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000731 if (ret) {
732 NV_ERROR(dev, "Error allocating channel PRAMIN: %d\n", ret);
733 return ret;
734 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000735
Ben Skeggsde3a6c02010-09-01 15:24:30 +1000736 ret = drm_mm_init(&chan->ramin_heap, base, size);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000737 if (ret) {
738 NV_ERROR(dev, "Error creating PRAMIN heap: %d\n", ret);
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000739 nouveau_gpuobj_ref(NULL, &chan->ramin);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000740 return ret;
741 }
742
743 return 0;
744}
745
746int
747nouveau_gpuobj_channel_init(struct nouveau_channel *chan,
748 uint32_t vram_h, uint32_t tt_h)
749{
750 struct drm_device *dev = chan->dev;
751 struct drm_nouveau_private *dev_priv = dev->dev_private;
752 struct nouveau_instmem_engine *instmem = &dev_priv->engine.instmem;
753 struct nouveau_gpuobj *vram = NULL, *tt = NULL;
754 int ret, i;
755
Ben Skeggs6ee73862009-12-11 19:24:15 +1000756 NV_DEBUG(dev, "ch%d vram=0x%08x tt=0x%08x\n", chan->id, vram_h, tt_h);
757
Ben Skeggs816544b2010-07-08 13:15:05 +1000758 /* Allocate a chunk of memory for per-channel object storage */
759 ret = nouveau_gpuobj_channel_init_pramin(chan);
760 if (ret) {
761 NV_ERROR(dev, "init pramin\n");
762 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000763 }
764
765 /* NV50 VM
766 * - Allocate per-channel page-directory
767 * - Map GART and VRAM into the channel's address space at the
768 * locations determined during init.
769 */
770 if (dev_priv->card_type >= NV_50) {
Ben Skeggs5125bfd2010-09-01 15:24:33 +1000771 u32 pgd_offs = (dev_priv->chipset == 0x50) ? 0x1400 : 0x0200;
772 u64 vm_vinst = chan->ramin->vinst + pgd_offs;
773 u32 vm_pinst = chan->ramin->pinst;
774 u32 pde;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000775
Ben Skeggs5125bfd2010-09-01 15:24:33 +1000776 if (vm_pinst != ~0)
777 vm_pinst += pgd_offs;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000778
Ben Skeggs5125bfd2010-09-01 15:24:33 +1000779 ret = nouveau_gpuobj_new_fake(dev, vm_pinst, vm_vinst, 0x4000,
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000780 0, &chan->vm_pd);
Ben Skeggsf56cb862010-07-08 11:29:10 +1000781 if (ret)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000782 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000783 for (i = 0; i < 0x4000; i += 8) {
Ben Skeggsb3beb162010-09-01 15:24:29 +1000784 nv_wo32(chan->vm_pd, i + 0, 0x00000000);
785 nv_wo32(chan->vm_pd, i + 4, 0xdeadcafe);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000786 }
787
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000788 nouveau_gpuobj_ref(dev_priv->gart_info.sg_ctxdma,
789 &chan->vm_gart_pt);
Ben Skeggsb3beb162010-09-01 15:24:29 +1000790 pde = (dev_priv->vm_gart_base / (512*1024*1024)) * 8;
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000791 nv_wo32(chan->vm_pd, pde + 0, chan->vm_gart_pt->vinst | 3);
Ben Skeggsb3beb162010-09-01 15:24:29 +1000792 nv_wo32(chan->vm_pd, pde + 4, 0x00000000);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000793
Ben Skeggsb3beb162010-09-01 15:24:29 +1000794 pde = (dev_priv->vm_vram_base / (512*1024*1024)) * 8;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000795 for (i = 0; i < dev_priv->vm_vram_pt_nr; i++) {
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000796 nouveau_gpuobj_ref(dev_priv->vm_vram_pt[i],
797 &chan->vm_vram_pt[i]);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000798
Ben Skeggsb3beb162010-09-01 15:24:29 +1000799 nv_wo32(chan->vm_pd, pde + 0,
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000800 chan->vm_vram_pt[i]->vinst | 0x61);
Ben Skeggsb3beb162010-09-01 15:24:29 +1000801 nv_wo32(chan->vm_pd, pde + 4, 0x00000000);
802 pde += 8;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000803 }
804
Ben Skeggsf56cb862010-07-08 11:29:10 +1000805 instmem->flush(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000806 }
807
808 /* RAMHT */
809 if (dev_priv->card_type < NV_50) {
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000810 nouveau_ramht_ref(dev_priv->ramht, &chan->ramht, NULL);
811 } else {
812 struct nouveau_gpuobj *ramht = NULL;
813
814 ret = nouveau_gpuobj_new(dev, chan, 0x8000, 16,
815 NVOBJ_FLAG_ZERO_ALLOC, &ramht);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000816 if (ret)
817 return ret;
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000818
819 ret = nouveau_ramht_new(dev, ramht, &chan->ramht);
820 nouveau_gpuobj_ref(NULL, &ramht);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000821 if (ret)
822 return ret;
823 }
824
825 /* VRAM ctxdma */
826 if (dev_priv->card_type >= NV_50) {
827 ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
828 0, dev_priv->vm_end,
829 NV_DMA_ACCESS_RW,
830 NV_DMA_TARGET_AGP, &vram);
831 if (ret) {
832 NV_ERROR(dev, "Error creating VRAM ctxdma: %d\n", ret);
833 return ret;
834 }
835 } else {
836 ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000837 0, dev_priv->fb_available_size,
838 NV_DMA_ACCESS_RW,
839 NV_DMA_TARGET_VIDMEM, &vram);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000840 if (ret) {
841 NV_ERROR(dev, "Error creating VRAM ctxdma: %d\n", ret);
842 return ret;
843 }
844 }
845
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000846 ret = nouveau_ramht_insert(chan, vram_h, vram);
847 nouveau_gpuobj_ref(NULL, &vram);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000848 if (ret) {
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000849 NV_ERROR(dev, "Error adding VRAM ctxdma to RAMHT: %d\n", ret);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000850 return ret;
851 }
852
853 /* TT memory ctxdma */
854 if (dev_priv->card_type >= NV_50) {
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000855 ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
856 0, dev_priv->vm_end,
857 NV_DMA_ACCESS_RW,
858 NV_DMA_TARGET_AGP, &tt);
859 if (ret) {
860 NV_ERROR(dev, "Error creating VRAM ctxdma: %d\n", ret);
861 return ret;
862 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000863 } else
864 if (dev_priv->gart_info.type != NOUVEAU_GART_NONE) {
865 ret = nouveau_gpuobj_gart_dma_new(chan, 0,
866 dev_priv->gart_info.aper_size,
867 NV_DMA_ACCESS_RW, &tt, NULL);
868 } else {
869 NV_ERROR(dev, "Invalid GART type %d\n", dev_priv->gart_info.type);
870 ret = -EINVAL;
871 }
872
873 if (ret) {
874 NV_ERROR(dev, "Error creating TT ctxdma: %d\n", ret);
875 return ret;
876 }
877
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000878 ret = nouveau_ramht_insert(chan, tt_h, tt);
879 nouveau_gpuobj_ref(NULL, &tt);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000880 if (ret) {
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000881 NV_ERROR(dev, "Error adding TT ctxdma to RAMHT: %d\n", ret);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000882 return ret;
883 }
884
885 return 0;
886}
887
888void
889nouveau_gpuobj_channel_takedown(struct nouveau_channel *chan)
890{
891 struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
892 struct drm_device *dev = chan->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000893 int i;
894
895 NV_DEBUG(dev, "ch%d\n", chan->id);
896
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000897 if (!chan->ramht)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000898 return;
899
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000900 nouveau_ramht_ref(NULL, &chan->ramht, chan);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000901
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000902 nouveau_gpuobj_ref(NULL, &chan->vm_pd);
903 nouveau_gpuobj_ref(NULL, &chan->vm_gart_pt);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000904 for (i = 0; i < dev_priv->vm_vram_pt_nr; i++)
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000905 nouveau_gpuobj_ref(NULL, &chan->vm_vram_pt[i]);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000906
Ben Skeggsb833ac22010-06-01 15:32:24 +1000907 if (chan->ramin_heap.free_stack.next)
908 drm_mm_takedown(&chan->ramin_heap);
Ben Skeggsa8eaebc2010-09-01 15:24:31 +1000909 nouveau_gpuobj_ref(NULL, &chan->ramin);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000910}
911
912int
913nouveau_gpuobj_suspend(struct drm_device *dev)
914{
915 struct drm_nouveau_private *dev_priv = dev->dev_private;
916 struct nouveau_gpuobj *gpuobj;
917 int i;
918
919 if (dev_priv->card_type < NV_50) {
920 dev_priv->susres.ramin_copy = vmalloc(dev_priv->ramin_rsvd_vram);
921 if (!dev_priv->susres.ramin_copy)
922 return -ENOMEM;
923
924 for (i = 0; i < dev_priv->ramin_rsvd_vram; i += 4)
925 dev_priv->susres.ramin_copy[i/4] = nv_ri32(dev, i);
926 return 0;
927 }
928
929 list_for_each_entry(gpuobj, &dev_priv->gpuobj_list, list) {
Ben Skeggs43efc9c2010-09-01 15:24:32 +1000930 if (!gpuobj->im_backing)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000931 continue;
932
Ben Skeggs43efc9c2010-09-01 15:24:32 +1000933 gpuobj->im_backing_suspend = vmalloc(gpuobj->size);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000934 if (!gpuobj->im_backing_suspend) {
935 nouveau_gpuobj_resume(dev);
936 return -ENOMEM;
937 }
938
Ben Skeggs43efc9c2010-09-01 15:24:32 +1000939 for (i = 0; i < gpuobj->size; i += 4)
Ben Skeggsb3beb162010-09-01 15:24:29 +1000940 gpuobj->im_backing_suspend[i/4] = nv_ro32(gpuobj, i);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000941 }
942
943 return 0;
944}
945
946void
947nouveau_gpuobj_suspend_cleanup(struct drm_device *dev)
948{
949 struct drm_nouveau_private *dev_priv = dev->dev_private;
950 struct nouveau_gpuobj *gpuobj;
951
952 if (dev_priv->card_type < NV_50) {
953 vfree(dev_priv->susres.ramin_copy);
954 dev_priv->susres.ramin_copy = NULL;
955 return;
956 }
957
958 list_for_each_entry(gpuobj, &dev_priv->gpuobj_list, list) {
959 if (!gpuobj->im_backing_suspend)
960 continue;
961
962 vfree(gpuobj->im_backing_suspend);
963 gpuobj->im_backing_suspend = NULL;
964 }
965}
966
967void
968nouveau_gpuobj_resume(struct drm_device *dev)
969{
970 struct drm_nouveau_private *dev_priv = dev->dev_private;
971 struct nouveau_gpuobj *gpuobj;
972 int i;
973
974 if (dev_priv->card_type < NV_50) {
975 for (i = 0; i < dev_priv->ramin_rsvd_vram; i += 4)
976 nv_wi32(dev, i, dev_priv->susres.ramin_copy[i/4]);
977 nouveau_gpuobj_suspend_cleanup(dev);
978 return;
979 }
980
981 list_for_each_entry(gpuobj, &dev_priv->gpuobj_list, list) {
982 if (!gpuobj->im_backing_suspend)
983 continue;
984
Ben Skeggs43efc9c2010-09-01 15:24:32 +1000985 for (i = 0; i < gpuobj->size; i += 4)
Ben Skeggsb3beb162010-09-01 15:24:29 +1000986 nv_wo32(gpuobj, i, gpuobj->im_backing_suspend[i/4]);
Ben Skeggsf56cb862010-07-08 11:29:10 +1000987 dev_priv->engine.instmem.flush(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000988 }
989
990 nouveau_gpuobj_suspend_cleanup(dev);
991}
992
993int nouveau_ioctl_grobj_alloc(struct drm_device *dev, void *data,
994 struct drm_file *file_priv)
995{
Ben Skeggs6ee73862009-12-11 19:24:15 +1000996 struct drm_nouveau_grobj_alloc *init = data;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000997 struct nouveau_gpuobj *gr = NULL;
998 struct nouveau_channel *chan;
999 int ret;
1000
Ben Skeggs6ee73862009-12-11 19:24:15 +10001001 if (init->handle == ~0)
1002 return -EINVAL;
1003
Ben Skeggscff5c132010-10-06 16:16:59 +10001004 chan = nouveau_channel_get(dev, file_priv, init->channel);
1005 if (IS_ERR(chan))
1006 return PTR_ERR(chan);
1007
1008 if (nouveau_ramht_find(chan, init->handle)) {
1009 ret = -EEXIST;
1010 goto out;
1011 }
Ben Skeggs6ee73862009-12-11 19:24:15 +10001012
Ben Skeggsa6a1a382010-10-19 19:57:34 +10001013 ret = nouveau_gpuobj_gr_new(chan, init->class, &gr);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001014 if (ret) {
1015 NV_ERROR(dev, "Error creating object: %d (%d/0x%08x)\n",
1016 ret, init->channel, init->handle);
Ben Skeggscff5c132010-10-06 16:16:59 +10001017 goto out;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001018 }
1019
Ben Skeggsa8eaebc2010-09-01 15:24:31 +10001020 ret = nouveau_ramht_insert(chan, init->handle, gr);
1021 nouveau_gpuobj_ref(NULL, &gr);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001022 if (ret) {
1023 NV_ERROR(dev, "Error referencing object: %d (%d/0x%08x)\n",
1024 ret, init->channel, init->handle);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001025 }
1026
Ben Skeggscff5c132010-10-06 16:16:59 +10001027out:
1028 nouveau_channel_put(&chan);
1029 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001030}
1031
1032int nouveau_ioctl_gpuobj_free(struct drm_device *dev, void *data,
1033 struct drm_file *file_priv)
1034{
1035 struct drm_nouveau_gpuobj_free *objfree = data;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001036 struct nouveau_channel *chan;
Ben Skeggs18a16a72010-10-12 10:11:00 +10001037 int ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001038
Ben Skeggscff5c132010-10-06 16:16:59 +10001039 chan = nouveau_channel_get(dev, file_priv, objfree->channel);
1040 if (IS_ERR(chan))
1041 return PTR_ERR(chan);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001042
Ben Skeggs18a16a72010-10-12 10:11:00 +10001043 ret = nouveau_ramht_remove(chan, objfree->handle);
Ben Skeggscff5c132010-10-06 16:16:59 +10001044 nouveau_channel_put(&chan);
1045 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001046}
Ben Skeggsb3beb162010-09-01 15:24:29 +10001047
1048u32
1049nv_ro32(struct nouveau_gpuobj *gpuobj, u32 offset)
1050{
Ben Skeggs5125bfd2010-09-01 15:24:33 +10001051 struct drm_nouveau_private *dev_priv = gpuobj->dev->dev_private;
1052 struct drm_device *dev = gpuobj->dev;
1053
1054 if (gpuobj->pinst == ~0 || !dev_priv->ramin_available) {
1055 u64 ptr = gpuobj->vinst + offset;
1056 u32 base = ptr >> 16;
1057 u32 val;
1058
1059 spin_lock(&dev_priv->ramin_lock);
1060 if (dev_priv->ramin_base != base) {
1061 dev_priv->ramin_base = base;
1062 nv_wr32(dev, 0x001700, dev_priv->ramin_base);
1063 }
1064 val = nv_rd32(dev, 0x700000 + (ptr & 0xffff));
1065 spin_unlock(&dev_priv->ramin_lock);
1066 return val;
1067 }
1068
1069 return nv_ri32(dev, gpuobj->pinst + offset);
Ben Skeggsb3beb162010-09-01 15:24:29 +10001070}
1071
1072void
1073nv_wo32(struct nouveau_gpuobj *gpuobj, u32 offset, u32 val)
1074{
Ben Skeggs5125bfd2010-09-01 15:24:33 +10001075 struct drm_nouveau_private *dev_priv = gpuobj->dev->dev_private;
1076 struct drm_device *dev = gpuobj->dev;
1077
1078 if (gpuobj->pinst == ~0 || !dev_priv->ramin_available) {
1079 u64 ptr = gpuobj->vinst + offset;
1080 u32 base = ptr >> 16;
1081
1082 spin_lock(&dev_priv->ramin_lock);
1083 if (dev_priv->ramin_base != base) {
1084 dev_priv->ramin_base = base;
1085 nv_wr32(dev, 0x001700, dev_priv->ramin_base);
1086 }
1087 nv_wr32(dev, 0x700000 + (ptr & 0xffff), val);
1088 spin_unlock(&dev_priv->ramin_lock);
1089 return;
1090 }
1091
1092 nv_wi32(dev, gpuobj->pinst + offset, val);
Ben Skeggsb3beb162010-09-01 15:24:29 +10001093}