UAPI: (Scripted) Convert #include "..." to #include <path/...> in drivers/gpu/
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / gpu / drm / nouveau / nouveau_gpuobj.c
CommitLineData
6ee73861
BS
1/*
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
760285e7 33#include <drm/drmP.h>
6ee73861 34#include "nouveau_drv.h"
760285e7 35#include <drm/nouveau_drm.h>
c420b2dc 36#include "nouveau_fifo.h"
479dcaea 37#include "nouveau_ramht.h"
20abd163 38#include "nouveau_software.h"
4c136142 39#include "nouveau_vm.h"
6ee73861 40
b8c157d3
BS
41struct nouveau_gpuobj_method {
42 struct list_head head;
43 u32 mthd;
44 int (*exec)(struct nouveau_channel *, u32 class, u32 mthd, u32 data);
45};
46
47struct nouveau_gpuobj_class {
48 struct list_head head;
49 struct list_head methods;
50 u32 id;
51 u32 engine;
52};
53
54int
55nouveau_gpuobj_class_new(struct drm_device *dev, u32 class, u32 engine)
56{
57 struct drm_nouveau_private *dev_priv = dev->dev_private;
58 struct nouveau_gpuobj_class *oc;
59
60 oc = kzalloc(sizeof(*oc), GFP_KERNEL);
61 if (!oc)
62 return -ENOMEM;
63
64 INIT_LIST_HEAD(&oc->methods);
65 oc->id = class;
66 oc->engine = engine;
67 list_add(&oc->head, &dev_priv->classes);
68 return 0;
69}
70
71int
72nouveau_gpuobj_mthd_new(struct drm_device *dev, u32 class, u32 mthd,
73 int (*exec)(struct nouveau_channel *, u32, u32, u32))
74{
75 struct drm_nouveau_private *dev_priv = dev->dev_private;
76 struct nouveau_gpuobj_method *om;
77 struct nouveau_gpuobj_class *oc;
78
79 list_for_each_entry(oc, &dev_priv->classes, head) {
80 if (oc->id == class)
81 goto found;
82 }
83
84 return -EINVAL;
85
86found:
87 om = kzalloc(sizeof(*om), GFP_KERNEL);
88 if (!om)
89 return -ENOMEM;
90
91 om->mthd = mthd;
92 om->exec = exec;
93 list_add(&om->head, &oc->methods);
94 return 0;
95}
96
97int
98nouveau_gpuobj_mthd_call(struct nouveau_channel *chan,
99 u32 class, u32 mthd, u32 data)
100{
101 struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
102 struct nouveau_gpuobj_method *om;
103 struct nouveau_gpuobj_class *oc;
104
105 list_for_each_entry(oc, &dev_priv->classes, head) {
106 if (oc->id != class)
107 continue;
108
109 list_for_each_entry(om, &oc->methods, head) {
110 if (om->mthd == mthd)
111 return om->exec(chan, class, mthd, data);
112 }
113 }
114
115 return -ENOENT;
116}
117
274fec93
BS
118int
119nouveau_gpuobj_mthd_call2(struct drm_device *dev, int chid,
120 u32 class, u32 mthd, u32 data)
121{
122 struct drm_nouveau_private *dev_priv = dev->dev_private;
c420b2dc 123 struct nouveau_fifo_priv *pfifo = nv_engine(dev, NVOBJ_ENGINE_FIFO);
274fec93
BS
124 struct nouveau_channel *chan = NULL;
125 unsigned long flags;
126 int ret = -EINVAL;
127
128 spin_lock_irqsave(&dev_priv->channels.lock, flags);
c420b2dc 129 if (chid >= 0 && chid < pfifo->channels)
274fec93
BS
130 chan = dev_priv->channels.ptr[chid];
131 if (chan)
132 ret = nouveau_gpuobj_mthd_call(chan, class, mthd, data);
133 spin_unlock_irqrestore(&dev_priv->channels.lock, flags);
134 return ret;
135}
136
6ee73861
BS
137int
138nouveau_gpuobj_new(struct drm_device *dev, struct nouveau_channel *chan,
139 uint32_t size, int align, uint32_t flags,
140 struct nouveau_gpuobj **gpuobj_ret)
141{
142 struct drm_nouveau_private *dev_priv = dev->dev_private;
e41115d0 143 struct nouveau_instmem_engine *instmem = &dev_priv->engine.instmem;
6ee73861 144 struct nouveau_gpuobj *gpuobj;
5125bfd8 145 struct drm_mm_node *ramin = NULL;
e41115d0 146 int ret, i;
6ee73861
BS
147
148 NV_DEBUG(dev, "ch%d size=%u align=%d flags=0x%08x\n",
149 chan ? chan->id : -1, size, align, flags);
150
6ee73861
BS
151 gpuobj = kzalloc(sizeof(*gpuobj), GFP_KERNEL);
152 if (!gpuobj)
153 return -ENOMEM;
154 NV_DEBUG(dev, "gpuobj %p\n", gpuobj);
b3beb167 155 gpuobj->dev = dev;
6ee73861 156 gpuobj->flags = flags;
eb9bcbdc 157 kref_init(&gpuobj->refcount);
43efc9ce 158 gpuobj->size = size;
6ee73861 159
e05d7eae 160 spin_lock(&dev_priv->ramin_lock);
6ee73861 161 list_add_tail(&gpuobj->list, &dev_priv->gpuobj_list);
e05d7eae 162 spin_unlock(&dev_priv->ramin_lock);
6ee73861 163
6e32fedc 164 if (!(flags & NVOBJ_FLAG_VM) && chan) {
5125bfd8
BS
165 ramin = drm_mm_search_free(&chan->ramin_heap, size, align, 0);
166 if (ramin)
167 ramin = drm_mm_get_block(ramin, size, align);
5125bfd8
BS
168 if (!ramin) {
169 nouveau_gpuobj_ref(NULL, &gpuobj);
170 return -ENOMEM;
171 }
6ee73861 172
e41115d0
BS
173 gpuobj->pinst = chan->ramin->pinst;
174 if (gpuobj->pinst != ~0)
175 gpuobj->pinst += ramin->start;
b833ac26 176
ca130c22 177 gpuobj->cinst = ramin->start;
e41115d0
BS
178 gpuobj->vinst = ramin->start + chan->ramin->vinst;
179 gpuobj->node = ramin;
180 } else {
6e32fedc 181 ret = instmem->get(gpuobj, chan, size, align);
6ee73861 182 if (ret) {
a8eaebc6 183 nouveau_gpuobj_ref(NULL, &gpuobj);
6ee73861
BS
184 return ret;
185 }
5125bfd8 186
e41115d0 187 ret = -ENOSYS;
a11c3198 188 if (!(flags & NVOBJ_FLAG_DONT_MAP))
e41115d0
BS
189 ret = instmem->map(gpuobj);
190 if (ret)
5125bfd8 191 gpuobj->pinst = ~0;
e41115d0
BS
192
193 gpuobj->cinst = NVOBJ_CINST_GLOBAL;
de3a6c0a
BS
194 }
195
6ee73861 196 if (gpuobj->flags & NVOBJ_FLAG_ZERO_ALLOC) {
43efc9ce 197 for (i = 0; i < gpuobj->size; i += 4)
b3beb167 198 nv_wo32(gpuobj, i, 0);
e41115d0 199 instmem->flush(dev);
6ee73861
BS
200 }
201
a8eaebc6 202
6ee73861
BS
203 *gpuobj_ret = gpuobj;
204 return 0;
205}
206
207int
fbd2895e 208nouveau_gpuobj_init(struct drm_device *dev)
6ee73861
BS
209{
210 struct drm_nouveau_private *dev_priv = dev->dev_private;
211
212 NV_DEBUG(dev, "\n");
213
214 INIT_LIST_HEAD(&dev_priv->gpuobj_list);
bd2e597d 215 INIT_LIST_HEAD(&dev_priv->classes);
5125bfd8
BS
216 spin_lock_init(&dev_priv->ramin_lock);
217 dev_priv->ramin_base = ~0;
6ee73861
BS
218
219 return 0;
220}
221
6ee73861
BS
222void
223nouveau_gpuobj_takedown(struct drm_device *dev)
224{
225 struct drm_nouveau_private *dev_priv = dev->dev_private;
b8c157d3
BS
226 struct nouveau_gpuobj_method *om, *tm;
227 struct nouveau_gpuobj_class *oc, *tc;
6ee73861
BS
228
229 NV_DEBUG(dev, "\n");
6ee73861 230
b8c157d3
BS
231 list_for_each_entry_safe(oc, tc, &dev_priv->classes, head) {
232 list_for_each_entry_safe(om, tm, &oc->methods, head) {
233 list_del(&om->head);
234 kfree(om);
235 }
236 list_del(&oc->head);
237 kfree(oc);
238 }
239
6e5a429b 240 WARN_ON(!list_empty(&dev_priv->gpuobj_list));
6ee73861
BS
241}
242
185abecc 243
eb9bcbdc
BS
244static void
245nouveau_gpuobj_del(struct kref *ref)
6ee73861 246{
eb9bcbdc
BS
247 struct nouveau_gpuobj *gpuobj =
248 container_of(ref, struct nouveau_gpuobj, refcount);
a8eaebc6 249 struct drm_device *dev = gpuobj->dev;
6ee73861 250 struct drm_nouveau_private *dev_priv = dev->dev_private;
e41115d0 251 struct nouveau_instmem_engine *instmem = &dev_priv->engine.instmem;
6ee73861
BS
252 int i;
253
a8eaebc6 254 NV_DEBUG(dev, "gpuobj %p\n", gpuobj);
6ee73861 255
e41115d0 256 if (gpuobj->node && (gpuobj->flags & NVOBJ_FLAG_ZERO_FREE)) {
43efc9ce 257 for (i = 0; i < gpuobj->size; i += 4)
b3beb167 258 nv_wo32(gpuobj, i, 0);
e41115d0 259 instmem->flush(dev);
6ee73861
BS
260 }
261
262 if (gpuobj->dtor)
263 gpuobj->dtor(dev, gpuobj);
264
e41115d0
BS
265 if (gpuobj->cinst == NVOBJ_CINST_GLOBAL) {
266 if (gpuobj->node) {
267 instmem->unmap(gpuobj);
268 instmem->put(gpuobj);
269 }
270 } else {
271 if (gpuobj->node) {
272 spin_lock(&dev_priv->ramin_lock);
273 drm_mm_put_block(gpuobj->node);
274 spin_unlock(&dev_priv->ramin_lock);
275 }
276 }
6ee73861 277
e05d7eae 278 spin_lock(&dev_priv->ramin_lock);
6ee73861 279 list_del(&gpuobj->list);
e05d7eae 280 spin_unlock(&dev_priv->ramin_lock);
6ee73861 281
6ee73861 282 kfree(gpuobj);
6ee73861
BS
283}
284
a8eaebc6
BS
285void
286nouveau_gpuobj_ref(struct nouveau_gpuobj *ref, struct nouveau_gpuobj **ptr)
6ee73861 287{
a8eaebc6 288 if (ref)
eb9bcbdc 289 kref_get(&ref->refcount);
6ee73861 290
eb9bcbdc
BS
291 if (*ptr)
292 kref_put(&(*ptr)->refcount, nouveau_gpuobj_del);
6ee73861 293
a8eaebc6 294 *ptr = ref;
6ee73861
BS
295}
296
297int
43efc9ce
BS
298nouveau_gpuobj_new_fake(struct drm_device *dev, u32 pinst, u64 vinst,
299 u32 size, u32 flags, struct nouveau_gpuobj **pgpuobj)
6ee73861
BS
300{
301 struct drm_nouveau_private *dev_priv = dev->dev_private;
302 struct nouveau_gpuobj *gpuobj = NULL;
303 int i;
304
305 NV_DEBUG(dev,
43efc9ce
BS
306 "pinst=0x%08x vinst=0x%010llx size=0x%08x flags=0x%08x\n",
307 pinst, vinst, size, flags);
6ee73861
BS
308
309 gpuobj = kzalloc(sizeof(*gpuobj), GFP_KERNEL);
310 if (!gpuobj)
311 return -ENOMEM;
312 NV_DEBUG(dev, "gpuobj %p\n", gpuobj);
b3beb167 313 gpuobj->dev = dev;
43efc9ce 314 gpuobj->flags = flags;
eb9bcbdc 315 kref_init(&gpuobj->refcount);
43efc9ce
BS
316 gpuobj->size = size;
317 gpuobj->pinst = pinst;
e41115d0 318 gpuobj->cinst = NVOBJ_CINST_GLOBAL;
43efc9ce 319 gpuobj->vinst = vinst;
de3a6c0a 320
6ee73861 321 if (gpuobj->flags & NVOBJ_FLAG_ZERO_ALLOC) {
43efc9ce 322 for (i = 0; i < gpuobj->size; i += 4)
b3beb167 323 nv_wo32(gpuobj, i, 0);
f56cb86f 324 dev_priv->engine.instmem.flush(dev);
6ee73861
BS
325 }
326
e05d7eae 327 spin_lock(&dev_priv->ramin_lock);
43efc9ce 328 list_add_tail(&gpuobj->list, &dev_priv->gpuobj_list);
e05d7eae 329 spin_unlock(&dev_priv->ramin_lock);
43efc9ce 330 *pgpuobj = gpuobj;
6ee73861
BS
331 return 0;
332}
333
7f4a195f
BS
334void
335nv50_gpuobj_dma_init(struct nouveau_gpuobj *obj, u32 offset, int class,
336 u64 base, u64 size, int target, int access,
337 u32 type, u32 comp)
6ee73861 338{
7f4a195f
BS
339 struct drm_nouveau_private *dev_priv = obj->dev->dev_private;
340 struct nouveau_instmem_engine *pinstmem = &dev_priv->engine.instmem;
341 u32 flags0;
6ee73861 342
7f4a195f
BS
343 flags0 = (comp << 29) | (type << 22) | class;
344 flags0 |= 0x00100000;
345
346 switch (access) {
347 case NV_MEM_ACCESS_RO: flags0 |= 0x00040000; break;
348 case NV_MEM_ACCESS_RW:
349 case NV_MEM_ACCESS_WO: flags0 |= 0x00080000; break;
350 default:
351 break;
352 }
6ee73861
BS
353
354 switch (target) {
7f4a195f
BS
355 case NV_MEM_TARGET_VRAM:
356 flags0 |= 0x00010000;
357 break;
358 case NV_MEM_TARGET_PCI:
359 flags0 |= 0x00020000;
360 break;
361 case NV_MEM_TARGET_PCI_NOSNOOP:
362 flags0 |= 0x00030000;
6ee73861 363 break;
7f4a195f 364 case NV_MEM_TARGET_GART:
b571fe21 365 base += dev_priv->gart_info.aper_base;
6ee73861 366 default:
7f4a195f 367 flags0 &= ~0x00100000;
6ee73861
BS
368 break;
369 }
370
7f4a195f
BS
371 /* convert to base + limit */
372 size = (base + size) - 1;
6ee73861 373
7f4a195f
BS
374 nv_wo32(obj, offset + 0x00, flags0);
375 nv_wo32(obj, offset + 0x04, lower_32_bits(size));
376 nv_wo32(obj, offset + 0x08, lower_32_bits(base));
377 nv_wo32(obj, offset + 0x0c, upper_32_bits(size) << 24 |
378 upper_32_bits(base));
379 nv_wo32(obj, offset + 0x10, 0x00000000);
380 nv_wo32(obj, offset + 0x14, 0x00000000);
6ee73861 381
7f4a195f
BS
382 pinstmem->flush(obj->dev);
383}
6ee73861 384
7f4a195f
BS
385int
386nv50_gpuobj_dma_new(struct nouveau_channel *chan, int class, u64 base, u64 size,
387 int target, int access, u32 type, u32 comp,
388 struct nouveau_gpuobj **pobj)
389{
390 struct drm_device *dev = chan->dev;
391 int ret;
6ee73861 392
a0fd9b9f 393 ret = nouveau_gpuobj_new(dev, chan, 24, 16, NVOBJ_FLAG_ZERO_FREE, pobj);
7f4a195f
BS
394 if (ret)
395 return ret;
6ee73861 396
7f4a195f
BS
397 nv50_gpuobj_dma_init(*pobj, 0, class, base, size, target,
398 access, type, comp);
6ee73861
BS
399 return 0;
400}
401
402int
7f4a195f
BS
403nouveau_gpuobj_dma_new(struct nouveau_channel *chan, int class, u64 base,
404 u64 size, int access, int target,
405 struct nouveau_gpuobj **pobj)
6ee73861 406{
7f4a195f 407 struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
6ee73861 408 struct drm_device *dev = chan->dev;
7f4a195f 409 struct nouveau_gpuobj *obj;
fd70b6cd 410 u32 flags0, flags2;
6ee73861
BS
411 int ret;
412
7f4a195f
BS
413 if (dev_priv->card_type >= NV_50) {
414 u32 comp = (target == NV_MEM_TARGET_VM) ? NV_MEM_COMP_VM : 0;
415 u32 type = (target == NV_MEM_TARGET_VM) ? NV_MEM_TYPE_VM : 0;
416
417 return nv50_gpuobj_dma_new(chan, class, base, size,
418 target, access, type, comp, pobj);
419 }
420
421 if (target == NV_MEM_TARGET_GART) {
58e6c7a9
BS
422 struct nouveau_gpuobj *gart = dev_priv->gart_info.sg_ctxdma;
423
424 if (dev_priv->gart_info.type == NOUVEAU_GART_PDMA) {
425 if (base == 0) {
426 nouveau_gpuobj_ref(gart, pobj);
427 return 0;
428 }
429
430 base = nouveau_sgdma_get_physical(dev, base);
7f4a195f 431 target = NV_MEM_TARGET_PCI;
7f4a195f 432 } else {
58e6c7a9
BS
433 base += dev_priv->gart_info.aper_base;
434 if (dev_priv->gart_info.type == NOUVEAU_GART_AGP)
435 target = NV_MEM_TARGET_PCI_NOSNOOP;
436 else
437 target = NV_MEM_TARGET_PCI;
6ee73861 438 }
6ee73861
BS
439 }
440
7f4a195f
BS
441 flags0 = class;
442 flags0 |= 0x00003000; /* PT present, PT linear */
443 flags2 = 0;
444
445 switch (target) {
446 case NV_MEM_TARGET_PCI:
447 flags0 |= 0x00020000;
448 break;
449 case NV_MEM_TARGET_PCI_NOSNOOP:
450 flags0 |= 0x00030000;
451 break;
452 default:
453 break;
454 }
455
456 switch (access) {
457 case NV_MEM_ACCESS_RO:
458 flags0 |= 0x00004000;
459 break;
460 case NV_MEM_ACCESS_WO:
461 flags0 |= 0x00008000;
462 default:
463 flags2 |= 0x00000002;
464 break;
465 }
466
467 flags0 |= (base & 0x00000fff) << 20;
468 flags2 |= (base & 0xfffff000);
469
a0fd9b9f 470 ret = nouveau_gpuobj_new(dev, chan, 16, 16, NVOBJ_FLAG_ZERO_FREE, &obj);
7f4a195f
BS
471 if (ret)
472 return ret;
473
474 nv_wo32(obj, 0x00, flags0);
475 nv_wo32(obj, 0x04, size - 1);
476 nv_wo32(obj, 0x08, flags2);
477 nv_wo32(obj, 0x0c, flags2);
478
479 obj->engine = NVOBJ_ENGINE_SW;
480 obj->class = class;
481 *pobj = obj;
482 return 0;
6ee73861
BS
483}
484
6ee73861 485int
ceac3099 486nouveau_gpuobj_gr_new(struct nouveau_channel *chan, u32 handle, int class)
6ee73861 487{
a6a1a380 488 struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
6ee73861 489 struct drm_device *dev = chan->dev;
b8c157d3 490 struct nouveau_gpuobj_class *oc;
6ee73861
BS
491 int ret;
492
493 NV_DEBUG(dev, "ch%d class=0x%04x\n", chan->id, class);
494
b8c157d3 495 list_for_each_entry(oc, &dev_priv->classes, head) {
a82dd49f 496 struct nouveau_exec_engine *eng = dev_priv->eng[oc->engine];
a6a1a380 497
a82dd49f
BS
498 if (oc->id != class)
499 continue;
a6a1a380 500
a82dd49f
BS
501 if (!chan->engctx[oc->engine]) {
502 ret = eng->context_new(chan, oc->engine);
503 if (ret)
504 return ret;
2703c21a 505 }
6ee73861 506
a82dd49f 507 return eng->object_new(chan, oc->engine, handle, class);
6ee73861 508 }
ceac3099 509
a82dd49f 510 return -EINVAL;
6ee73861
BS
511}
512
6ee73861
BS
513static int
514nouveau_gpuobj_channel_init_pramin(struct nouveau_channel *chan)
515{
516 struct drm_device *dev = chan->dev;
517 struct drm_nouveau_private *dev_priv = dev->dev_private;
6ee73861
BS
518 uint32_t size;
519 uint32_t base;
520 int ret;
521
522 NV_DEBUG(dev, "ch%d\n", chan->id);
523
524 /* Base amount for object storage (4KiB enough?) */
bd2e597d 525 size = 0x2000;
6ee73861
BS
526 base = 0;
527
6ee73861
BS
528 if (dev_priv->card_type == NV_50) {
529 /* Various fixed table thingos */
530 size += 0x1400; /* mostly unknown stuff */
531 size += 0x4000; /* vm pd */
532 base = 0x6000;
533 /* RAMHT, not sure about setting size yet, 32KiB to be safe */
534 size += 0x8000;
535 /* RAMFC */
536 size += 0x1000;
6ee73861
BS
537 }
538
a8eaebc6 539 ret = nouveau_gpuobj_new(dev, NULL, size, 0x1000, 0, &chan->ramin);
6ee73861
BS
540 if (ret) {
541 NV_ERROR(dev, "Error allocating channel PRAMIN: %d\n", ret);
542 return ret;
543 }
6ee73861 544
1a97b4ac 545 ret = drm_mm_init(&chan->ramin_heap, base, size - base);
6ee73861
BS
546 if (ret) {
547 NV_ERROR(dev, "Error creating PRAMIN heap: %d\n", ret);
a8eaebc6 548 nouveau_gpuobj_ref(NULL, &chan->ramin);
6ee73861
BS
549 return ret;
550 }
551
552 return 0;
553}
554
5de8037a
BS
555static int
556nvc0_gpuobj_channel_init(struct nouveau_channel *chan, struct nouveau_vm *vm)
557{
558 struct drm_device *dev = chan->dev;
559 struct nouveau_gpuobj *pgd = NULL;
560 struct nouveau_vm_pgd *vpgd;
35bcf5d5 561 int ret;
5de8037a
BS
562
563 ret = nouveau_gpuobj_new(dev, NULL, 4096, 0x1000, 0, &chan->ramin);
564 if (ret)
565 return ret;
566
567 /* create page directory for this vm if none currently exists,
568 * will be destroyed automagically when last reference to the
569 * vm is removed
570 */
571 if (list_empty(&vm->pgd_list)) {
572 ret = nouveau_gpuobj_new(dev, NULL, 65536, 0x1000, 0, &pgd);
573 if (ret)
574 return ret;
575 }
576 nouveau_vm_ref(vm, &chan->vm, pgd);
577 nouveau_gpuobj_ref(NULL, &pgd);
578
579 /* point channel at vm's page directory */
580 vpgd = list_first_entry(&vm->pgd_list, struct nouveau_vm_pgd, head);
581 nv_wo32(chan->ramin, 0x0200, lower_32_bits(vpgd->obj->vinst));
582 nv_wo32(chan->ramin, 0x0204, upper_32_bits(vpgd->obj->vinst));
583 nv_wo32(chan->ramin, 0x0208, 0xffffffff);
584 nv_wo32(chan->ramin, 0x020c, 0x000000ff);
585
5de8037a
BS
586 return 0;
587}
588
6ee73861
BS
589int
590nouveau_gpuobj_channel_init(struct nouveau_channel *chan,
591 uint32_t vram_h, uint32_t tt_h)
592{
593 struct drm_device *dev = chan->dev;
594 struct drm_nouveau_private *dev_priv = dev->dev_private;
0320d791
BS
595 struct nouveau_fpriv *fpriv = nouveau_fpriv(chan->file_priv);
596 struct nouveau_vm *vm = fpriv ? fpriv->vm : dev_priv->chan_vm;
6ee73861 597 struct nouveau_gpuobj *vram = NULL, *tt = NULL;
35bcf5d5 598 int ret;
6ee73861 599
6ee73861 600 NV_DEBUG(dev, "ch%d vram=0x%08x tt=0x%08x\n", chan->id, vram_h, tt_h);
2e9733ff 601 if (dev_priv->card_type >= NV_C0)
5de8037a 602 return nvc0_gpuobj_channel_init(chan, vm);
effd6e06 603
816544b2
BS
604 /* Allocate a chunk of memory for per-channel object storage */
605 ret = nouveau_gpuobj_channel_init_pramin(chan);
606 if (ret) {
607 NV_ERROR(dev, "init pramin\n");
608 return ret;
6ee73861
BS
609 }
610
effd6e06 611 /* NV50 VM
6ee73861 612 * - Allocate per-channel page-directory
4c136142 613 * - Link with shared channel VM
6ee73861 614 */
0320d791 615 if (vm) {
5125bfd8
BS
616 u32 pgd_offs = (dev_priv->chipset == 0x50) ? 0x1400 : 0x0200;
617 u64 vm_vinst = chan->ramin->vinst + pgd_offs;
618 u32 vm_pinst = chan->ramin->pinst;
6ee73861 619
5125bfd8
BS
620 if (vm_pinst != ~0)
621 vm_pinst += pgd_offs;
6ee73861 622
5125bfd8 623 ret = nouveau_gpuobj_new_fake(dev, vm_pinst, vm_vinst, 0x4000,
a8eaebc6 624 0, &chan->vm_pd);
f56cb86f 625 if (ret)
6ee73861 626 return ret;
6ee73861 627
0320d791 628 nouveau_vm_ref(vm, &chan->vm, chan->vm_pd);
6ee73861
BS
629 }
630
631 /* RAMHT */
632 if (dev_priv->card_type < NV_50) {
a8eaebc6
BS
633 nouveau_ramht_ref(dev_priv->ramht, &chan->ramht, NULL);
634 } else {
635 struct nouveau_gpuobj *ramht = NULL;
636
637 ret = nouveau_gpuobj_new(dev, chan, 0x8000, 16,
638 NVOBJ_FLAG_ZERO_ALLOC, &ramht);
6ee73861
BS
639 if (ret)
640 return ret;
a8eaebc6
BS
641
642 ret = nouveau_ramht_new(dev, ramht, &chan->ramht);
643 nouveau_gpuobj_ref(NULL, &ramht);
6ee73861
BS
644 if (ret)
645 return ret;
646 }
647
648 /* VRAM ctxdma */
649 if (dev_priv->card_type >= NV_50) {
650 ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
4c136142 651 0, (1ULL << 40), NV_MEM_ACCESS_RW,
7f4a195f 652 NV_MEM_TARGET_VM, &vram);
6ee73861
BS
653 if (ret) {
654 NV_ERROR(dev, "Error creating VRAM ctxdma: %d\n", ret);
655 return ret;
656 }
657 } else {
658 ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
a8eaebc6 659 0, dev_priv->fb_available_size,
7f4a195f
BS
660 NV_MEM_ACCESS_RW,
661 NV_MEM_TARGET_VRAM, &vram);
6ee73861
BS
662 if (ret) {
663 NV_ERROR(dev, "Error creating VRAM ctxdma: %d\n", ret);
664 return ret;
665 }
666 }
667
a8eaebc6
BS
668 ret = nouveau_ramht_insert(chan, vram_h, vram);
669 nouveau_gpuobj_ref(NULL, &vram);
6ee73861 670 if (ret) {
a8eaebc6 671 NV_ERROR(dev, "Error adding VRAM ctxdma to RAMHT: %d\n", ret);
6ee73861
BS
672 return ret;
673 }
674
675 /* TT memory ctxdma */
676 if (dev_priv->card_type >= NV_50) {
a8eaebc6 677 ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
4c136142 678 0, (1ULL << 40), NV_MEM_ACCESS_RW,
7f4a195f 679 NV_MEM_TARGET_VM, &tt);
6ee73861 680 } else {
7f4a195f
BS
681 ret = nouveau_gpuobj_dma_new(chan, NV_CLASS_DMA_IN_MEMORY,
682 0, dev_priv->gart_info.aper_size,
683 NV_MEM_ACCESS_RW,
684 NV_MEM_TARGET_GART, &tt);
6ee73861
BS
685 }
686
687 if (ret) {
688 NV_ERROR(dev, "Error creating TT ctxdma: %d\n", ret);
689 return ret;
690 }
691
a8eaebc6
BS
692 ret = nouveau_ramht_insert(chan, tt_h, tt);
693 nouveau_gpuobj_ref(NULL, &tt);
6ee73861 694 if (ret) {
a8eaebc6 695 NV_ERROR(dev, "Error adding TT ctxdma to RAMHT: %d\n", ret);
6ee73861
BS
696 return ret;
697 }
698
699 return 0;
700}
701
702void
703nouveau_gpuobj_channel_takedown(struct nouveau_channel *chan)
704{
35bcf5d5 705 NV_DEBUG(chan->dev, "ch%d\n", chan->id);
6ee73861 706
e432d48f
BS
707 nouveau_vm_ref(NULL, &chan->vm, chan->vm_pd);
708 nouveau_gpuobj_ref(NULL, &chan->vm_pd);
709
31a5b8ce 710 if (drm_mm_initialized(&chan->ramin_heap))
b833ac26 711 drm_mm_takedown(&chan->ramin_heap);
a8eaebc6 712 nouveau_gpuobj_ref(NULL, &chan->ramin);
6ee73861
BS
713}
714
715int
716nouveau_gpuobj_suspend(struct drm_device *dev)
717{
718 struct drm_nouveau_private *dev_priv = dev->dev_private;
719 struct nouveau_gpuobj *gpuobj;
720 int i;
721
6ee73861 722 list_for_each_entry(gpuobj, &dev_priv->gpuobj_list, list) {
e41115d0 723 if (gpuobj->cinst != NVOBJ_CINST_GLOBAL)
6ee73861
BS
724 continue;
725
dc1e5c0d
BS
726 gpuobj->suspend = vmalloc(gpuobj->size);
727 if (!gpuobj->suspend) {
6ee73861
BS
728 nouveau_gpuobj_resume(dev);
729 return -ENOMEM;
730 }
731
43efc9ce 732 for (i = 0; i < gpuobj->size; i += 4)
dc1e5c0d 733 gpuobj->suspend[i/4] = nv_ro32(gpuobj, i);
6ee73861
BS
734 }
735
736 return 0;
737}
738
6ee73861
BS
739void
740nouveau_gpuobj_resume(struct drm_device *dev)
741{
742 struct drm_nouveau_private *dev_priv = dev->dev_private;
743 struct nouveau_gpuobj *gpuobj;
744 int i;
745
6ee73861 746 list_for_each_entry(gpuobj, &dev_priv->gpuobj_list, list) {
dc1e5c0d 747 if (!gpuobj->suspend)
6ee73861
BS
748 continue;
749
43efc9ce 750 for (i = 0; i < gpuobj->size; i += 4)
dc1e5c0d
BS
751 nv_wo32(gpuobj, i, gpuobj->suspend[i/4]);
752
753 vfree(gpuobj->suspend);
754 gpuobj->suspend = NULL;
6ee73861
BS
755 }
756
dc1e5c0d 757 dev_priv->engine.instmem.flush(dev);
6ee73861
BS
758}
759
b3beb167
BS
760u32
761nv_ro32(struct nouveau_gpuobj *gpuobj, u32 offset)
762{
5125bfd8
BS
763 struct drm_nouveau_private *dev_priv = gpuobj->dev->dev_private;
764 struct drm_device *dev = gpuobj->dev;
04eb34a4 765 unsigned long flags;
5125bfd8
BS
766
767 if (gpuobj->pinst == ~0 || !dev_priv->ramin_available) {
768 u64 ptr = gpuobj->vinst + offset;
769 u32 base = ptr >> 16;
770 u32 val;
771
04eb34a4 772 spin_lock_irqsave(&dev_priv->vm_lock, flags);
5125bfd8
BS
773 if (dev_priv->ramin_base != base) {
774 dev_priv->ramin_base = base;
775 nv_wr32(dev, 0x001700, dev_priv->ramin_base);
776 }
777 val = nv_rd32(dev, 0x700000 + (ptr & 0xffff));
04eb34a4 778 spin_unlock_irqrestore(&dev_priv->vm_lock, flags);
5125bfd8
BS
779 return val;
780 }
781
782 return nv_ri32(dev, gpuobj->pinst + offset);
b3beb167
BS
783}
784
785void
786nv_wo32(struct nouveau_gpuobj *gpuobj, u32 offset, u32 val)
787{
5125bfd8
BS
788 struct drm_nouveau_private *dev_priv = gpuobj->dev->dev_private;
789 struct drm_device *dev = gpuobj->dev;
04eb34a4 790 unsigned long flags;
5125bfd8
BS
791
792 if (gpuobj->pinst == ~0 || !dev_priv->ramin_available) {
793 u64 ptr = gpuobj->vinst + offset;
794 u32 base = ptr >> 16;
795
04eb34a4 796 spin_lock_irqsave(&dev_priv->vm_lock, flags);
5125bfd8
BS
797 if (dev_priv->ramin_base != base) {
798 dev_priv->ramin_base = base;
799 nv_wr32(dev, 0x001700, dev_priv->ramin_base);
800 }
801 nv_wr32(dev, 0x700000 + (ptr & 0xffff), val);
04eb34a4 802 spin_unlock_irqrestore(&dev_priv->vm_lock, flags);
5125bfd8
BS
803 return;
804 }
805
806 nv_wi32(dev, gpuobj->pinst + offset, val);
b3beb167 807}