summaryrefslogtreecommitdiff
path: root/libdrm/nouveau/nouveau_bo.c
blob: 023c6bedf7b95aa1a409cd64b2a1a625c8fdd27b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
/*
 * Copyright 2007 Nouveau Project
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include <stdint.h>
#include <stdlib.h>
#include <errno.h>
#include <assert.h>

#include <sys/mman.h>
#include <sys/ioctl.h>

#include "nouveau_private.h"

int
nouveau_bo_init(struct nouveau_device *dev)
{
	return 0;
}

void
nouveau_bo_takedown(struct nouveau_device *dev)
{
}

static int
nouveau_bo_allocated(struct nouveau_bo_priv *nvbo)
{
	if (nvbo->sysmem || nvbo->handle || (nvbo->flags & NOUVEAU_BO_PIN))
		return 1;
	return 0;
}

static int
nouveau_bo_ualloc(struct nouveau_bo_priv *nvbo)
{
	if (nvbo->user || nvbo->sysmem) {
		assert(nvbo->sysmem);
		return 0;
	}

	nvbo->sysmem = malloc(nvbo->size);
	if (!nvbo->sysmem)
		return -ENOMEM;

	return 0;
}

static void
nouveau_bo_ufree(struct nouveau_bo_priv *nvbo)
{
	if (nvbo->sysmem) {
		if (!nvbo->user)
			free(nvbo->sysmem);
		nvbo->sysmem = NULL;
	}
}

static void
nouveau_bo_kfree_nomm(struct nouveau_bo_priv *nvbo)
{
	struct nouveau_device_priv *nvdev = nouveau_device(nvbo->base.device);
	struct drm_nouveau_mem_free req;

	if (nvbo->map) {
		drmUnmap(nvbo->map, nvbo->size);
		nvbo->map = NULL;
	}

	req.offset = nvbo->offset;
	if (nvbo->domain & NOUVEAU_BO_GART)
		req.flags = NOUVEAU_MEM_AGP | NOUVEAU_MEM_PCI;
	else
	if (nvbo->domain & NOUVEAU_BO_VRAM)
		req.flags = NOUVEAU_MEM_FB;
	drmCommandWrite(nvdev->fd, DRM_NOUVEAU_MEM_FREE, &req, sizeof(req));

	nvbo->handle = 0;
}

static void
nouveau_bo_kfree(struct nouveau_bo_priv *nvbo)
{
	struct nouveau_device_priv *nvdev = nouveau_device(nvbo->base.device);
	struct drm_gem_close req;

	if (!nvbo->handle)
		return;

	if (!nvdev->mm_enabled) {
		nouveau_bo_kfree_nomm(nvbo);
		return;
	}

	if (nvbo->map) {
		munmap(nvbo->map, nvbo->size);
		nvbo->map = NULL;
	}

	req.handle = nvbo->handle;
	nvbo->handle = 0;
	ioctl(nvdev->fd, DRM_IOCTL_GEM_CLOSE, &req);
}

static int
nouveau_bo_kalloc_nomm(struct nouveau_bo_priv *nvbo)
{
	struct nouveau_device_priv *nvdev = nouveau_device(nvbo->base.device);
	struct drm_nouveau_mem_alloc req;
	int ret;

	if (nvbo->handle)
		return 0;

	if (!(nvbo->flags & (NOUVEAU_BO_VRAM|NOUVEAU_BO_GART)))
		nvbo->flags |= (NOUVEAU_BO_GART | NOUVEAU_BO_VRAM);

	req.size = nvbo->size;
	req.alignment = nvbo->align;
	req.flags = 0;
	if (nvbo->flags & NOUVEAU_BO_VRAM)
		req.flags |= NOUVEAU_MEM_FB;
	if (nvbo->flags & NOUVEAU_BO_GART)
		req.flags |= (NOUVEAU_MEM_AGP | NOUVEAU_MEM_PCI);
	if (nvbo->flags & NOUVEAU_BO_TILED) {
		req.flags |= NOUVEAU_MEM_TILE;
		if (nvbo->flags & NOUVEAU_BO_ZTILE)
			req.flags |= NOUVEAU_MEM_TILE_ZETA;
	}
	req.flags |= NOUVEAU_MEM_MAPPED;

	ret = drmCommandWriteRead(nvdev->fd, DRM_NOUVEAU_MEM_ALLOC,
				  &req, sizeof(req));
	if (ret)
		return ret;

	nvbo->handle = req.map_handle;
	nvbo->size = req.size;
	nvbo->offset = req.offset;
	if (req.flags & (NOUVEAU_MEM_AGP | NOUVEAU_MEM_PCI))
		nvbo->domain = NOUVEAU_BO_GART;
	else
	if (req.flags & NOUVEAU_MEM_FB)
		nvbo->domain = NOUVEAU_BO_VRAM;

	return 0;
}

static int
nouveau_bo_kalloc(struct nouveau_bo_priv *nvbo, struct nouveau_channel *chan)
{
	struct nouveau_device_priv *nvdev = nouveau_device(nvbo->base.device);
	struct drm_nouveau_gem_new req;
	int ret;

	if (nvbo->handle || (nvbo->flags & NOUVEAU_BO_PIN))
		return 0;

	if (!nvdev->mm_enabled)
		return nouveau_bo_kalloc_nomm(nvbo);

	req.channel_hint = chan ? chan->id : 0;

	req.size = nvbo->size;
	req.align = nvbo->align;

	req.domain = 0;

	if (nvbo->flags & NOUVEAU_BO_VRAM)
		req.domain |= NOUVEAU_GEM_DOMAIN_VRAM;

	if (nvbo->flags & NOUVEAU_BO_GART)
		req.domain |= NOUVEAU_GEM_DOMAIN_GART;

	if (nvbo->flags & NOUVEAU_BO_TILED) {
		req.domain |= NOUVEAU_GEM_DOMAIN_TILE;
		if (nvbo->flags & NOUVEAU_BO_ZTILE)
			req.domain |= NOUVEAU_GEM_DOMAIN_TILE_ZETA;
	}

	if (!req.domain) {
		req.domain |= (NOUVEAU_GEM_DOMAIN_VRAM |
			       NOUVEAU_GEM_DOMAIN_GART);
	}

	ret = drmCommandWriteRead(nvdev->fd, DRM_NOUVEAU_GEM_NEW,
				  &req, sizeof(req));
	if (ret)
		return ret;
	nvbo->handle = nvbo->base.handle = req.handle;
	nvbo->size = req.size;
	nvbo->domain = req.domain;
	nvbo->offset = req.offset;

	return 0;
}

static int
nouveau_bo_kmap_nomm(struct nouveau_bo_priv *nvbo)
{
	struct nouveau_device_priv *nvdev = nouveau_device(nvbo->base.device);
	int ret;

	ret = drmMap(nvdev->fd, nvbo->handle, nvbo->size, &nvbo->map);
	if (ret) {
		nvbo->map = NULL;
		return ret;
	}

	return 0;
}

static int
nouveau_bo_kmap(struct nouveau_bo_priv *nvbo)
{
	struct nouveau_device_priv *nvdev = nouveau_device(nvbo->base.device);
	struct drm_nouveau_gem_mmap req;
	int ret;

	if (nvbo->map)
		return 0;

	if (!nvbo->handle)
		return -EINVAL;

	if (!nvdev->mm_enabled)
		return nouveau_bo_kmap_nomm(nvbo);

	req.handle = nvbo->handle;
	ret = drmCommandWriteRead(nvdev->fd, DRM_NOUVEAU_GEM_MMAP,
				  &req, sizeof(req));
	if (ret)
		return ret;

	nvbo->map = (void *)(unsigned long)req.vaddr;
	return 0;
}

int
nouveau_bo_new(struct nouveau_device *dev, uint32_t flags, int align,
	       int size, struct nouveau_bo **bo)
{
	struct nouveau_bo_priv *nvbo;
	int ret;

	if (!dev || !bo || *bo)
		return -EINVAL;

	nvbo = calloc(1, sizeof(struct nouveau_bo_priv));
	if (!nvbo)
		return -ENOMEM;
	nvbo->base.device = dev;
	nvbo->base.size = size;

	nvbo->refcount = 1;
	/* Don't set NOUVEAU_BO_PIN here, or nouveau_bo_allocated() will
	 * decided the buffer's already allocated when it's not.  The
	 * call to nouveau_bo_pin() later will set this flag.
	 */
	nvbo->flags = (flags & ~NOUVEAU_BO_PIN);
	nvbo->size = size;
	nvbo->align = align;

	/*XXX: murder me violently */
	if (flags & NOUVEAU_BO_TILED) {
		nvbo->base.tiled = 1;
		if (flags & NOUVEAU_BO_ZTILE)
			nvbo->base.tiled |= 2;
	}

	if (flags & NOUVEAU_BO_PIN) {
		ret = nouveau_bo_pin((void *)nvbo, nvbo->flags);
		if (ret) {
			nouveau_bo_ref(NULL, (void *)nvbo);
			return ret;
		}
	}

	*bo = &nvbo->base;
	return 0;
}

int
nouveau_bo_user(struct nouveau_device *dev, void *ptr, int size,
		struct nouveau_bo **bo)
{
	struct nouveau_bo_priv *nvbo;
	int ret;

	ret = nouveau_bo_new(dev, 0, 0, size, bo);
	if (ret)
		return ret;
	nvbo = nouveau_bo(*bo);

	nvbo->sysmem = ptr;
	nvbo->user = 1;
	return 0;
}

int
nouveau_bo_fake(struct nouveau_device *dev, uint64_t offset, uint32_t flags,
		uint32_t size, void *map, struct nouveau_bo **bo)
{
	struct nouveau_bo_priv *nvbo;
	int ret;

	ret = nouveau_bo_new(dev, flags & ~NOUVEAU_BO_PIN, 0, size, bo);
	if (ret)
		return ret;
	nvbo = nouveau_bo(*bo);

	nvbo->flags = flags | NOUVEAU_BO_PIN;
	nvbo->domain = (flags & (NOUVEAU_BO_VRAM|NOUVEAU_BO_GART));
	nvbo->offset = offset;
	nvbo->size = nvbo->base.size = size;
	nvbo->map = map;
	nvbo->base.flags = nvbo->flags;
	nvbo->base.offset = nvbo->offset;
	return 0;
}

int
nouveau_bo_handle_get(struct nouveau_bo *bo, uint32_t *handle)
{
	struct nouveau_device_priv *nvdev = nouveau_device(bo->device);
	struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
	int ret;
 
	if (!bo || !handle)
		return -EINVAL;

	if (!nvbo->global_handle) {
		struct drm_gem_flink req;
 
		ret = nouveau_bo_kalloc(nvbo, NULL);
		if (ret)
			return ret;

		if (nvdev->mm_enabled) {
			req.handle = nvbo->handle;
			ret = ioctl(nvdev->fd, DRM_IOCTL_GEM_FLINK, &req);
			if (ret) {
				nouveau_bo_kfree(nvbo);
				return ret;
			}
	 
			nvbo->global_handle = req.name;
		} else {
			nvbo->global_handle = nvbo->offset;
		}
	}
 
	*handle = nvbo->global_handle;
	return 0;
}
 
int
nouveau_bo_handle_ref(struct nouveau_device *dev, uint32_t handle,
		      struct nouveau_bo **bo)
{
	struct nouveau_device_priv *nvdev = nouveau_device(dev);
	struct nouveau_bo_priv *nvbo;
	struct drm_gem_open req;
	int ret;

	ret = nouveau_bo_new(dev, 0, 0, 0, bo);
	if (ret)
		return ret;
	nvbo = nouveau_bo(*bo);

	if (!nvdev->mm_enabled) {
		nvbo->handle = 0;
		nvbo->offset =  handle;
		nvbo->domain = NOUVEAU_BO_VRAM;
		nvbo->flags = NOUVEAU_BO_VRAM | NOUVEAU_BO_PIN;
		nvbo->base.offset = nvbo->offset;
		nvbo->base.flags = nvbo->flags;
	} else {
		req.name = handle;
		ret = ioctl(nvdev->fd, DRM_IOCTL_GEM_OPEN, &req);
		if (ret) {
			nouveau_bo_ref(NULL, bo);
			return ret;
		}

		nvbo->size = req.size;
		nvbo->handle = req.handle;
	}
 
	return 0;
} 

static void
nouveau_bo_del_cb(void *priv)
{
	struct nouveau_bo_priv *nvbo = priv;

	nouveau_fence_ref(NULL, &nvbo->fence);
	nouveau_fence_ref(NULL, &nvbo->wr_fence);
	nouveau_bo_kfree(nvbo);
	free(nvbo);
}

static void
nouveau_bo_del(struct nouveau_bo **bo)
{
	struct nouveau_bo_priv *nvbo;

	if (!bo || !*bo)
		return;
	nvbo = nouveau_bo(*bo);
	*bo = NULL;

	if (--nvbo->refcount)
		return;

	if (nvbo->pending) {
		nvbo->pending = NULL;
		nouveau_pushbuf_flush(nvbo->pending_channel, 0);
	}

	nouveau_bo_ufree(nvbo);

	if (!nouveau_device(nvbo->base.device)->mm_enabled && nvbo->fence) {
		nouveau_fence_flush(nvbo->fence->channel);
		if (nouveau_fence(nvbo->fence)->signalled) {
			nouveau_bo_del_cb(nvbo);
		} else {
			nouveau_fence_signal_cb(nvbo->fence,
					        nouveau_bo_del_cb, nvbo);
		}
	} else {
		nouveau_bo_del_cb(nvbo);
	}
}

int
nouveau_bo_ref(struct nouveau_bo *ref, struct nouveau_bo **pbo)
{
	if (!pbo)
		return -EINVAL;

	if (ref)
		nouveau_bo(ref)->refcount++;

	if (*pbo)
		nouveau_bo_del(pbo);

	*pbo = ref;
	return 0;
}

static int
nouveau_bo_wait_nomm(struct nouveau_bo *bo, int cpu_write)
{
	struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
	int ret = 0;

	if (cpu_write)
		ret = nouveau_fence_wait(&nvbo->fence);
	else
		ret = nouveau_fence_wait(&nvbo->wr_fence);
	if (ret)
		return ret;

	nvbo->write_marker = 0;
	return 0;
}

static int
nouveau_bo_wait(struct nouveau_bo *bo, int cpu_write)
{
	struct nouveau_device_priv *nvdev = nouveau_device(bo->device);
	struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
	struct drm_nouveau_gem_cpu_prep req;
	int ret;

	if (!nvbo->global_handle && !nvbo->write_marker && !cpu_write)
		return 0;

	if (nvbo->pending &&
	    (nvbo->pending->write_domains || cpu_write)) {
		nvbo->pending = NULL;
		nouveau_pushbuf_flush(nvbo->pending_channel, 0);
	}

	if (!nvdev->mm_enabled)
		return nouveau_bo_wait_nomm(bo, cpu_write);

	req.handle = nvbo->handle;
	ret = drmCommandWrite(nvdev->fd, DRM_NOUVEAU_GEM_CPU_PREP,
			      &req, sizeof(req));
	if (ret)
		return ret;

	nvbo->write_marker = 0;
	return 0;
}

int
nouveau_bo_map(struct nouveau_bo *bo, uint32_t flags)
{
	struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
	int ret;

	if (!nvbo || bo->map)
		return -EINVAL;

	if (!nouveau_bo_allocated(nvbo)) {
		if (nvbo->flags & (NOUVEAU_BO_VRAM | NOUVEAU_BO_GART)) {
			ret = nouveau_bo_kalloc(nvbo, NULL);
			if (ret)
				return ret;
		}

		if (!nouveau_bo_allocated(nvbo)) {
			ret = nouveau_bo_ualloc(nvbo);
			if (ret)
				return ret;
		}
	}

	if (nvbo->sysmem) {
		bo->map = nvbo->sysmem;
	} else {
		ret = nouveau_bo_kmap(nvbo);
		if (ret)
			return ret;

		ret = nouveau_bo_wait(bo, (flags & NOUVEAU_BO_WR));
		if (ret)
			return ret;

		bo->map = nvbo->map;
	}

	return 0;
}

void
nouveau_bo_unmap(struct nouveau_bo *bo)
{
	struct nouveau_device_priv *nvdev = nouveau_device(bo->device);
	struct nouveau_bo_priv *nvbo = nouveau_bo(bo);

	if (nvdev->mm_enabled && bo->map && !nvbo->sysmem) {
		struct nouveau_device_priv *nvdev = nouveau_device(bo->device);
		struct drm_nouveau_gem_cpu_fini req;

		req.handle = nvbo->handle;
		drmCommandWrite(nvdev->fd, DRM_NOUVEAU_GEM_CPU_FINI,
				&req, sizeof(req));
	}

	bo->map = NULL;
}

int
nouveau_bo_validate_nomm(struct nouveau_bo_priv *nvbo, uint32_t flags)
{
	struct nouveau_bo *new = NULL;
	uint32_t t_handle, t_domain, t_offset, t_size;
	void *t_map;
	int ret;

	if ((flags & NOUVEAU_BO_VRAM) && nvbo->domain == NOUVEAU_BO_VRAM)
		return 0;
	if ((flags & NOUVEAU_BO_GART) && nvbo->domain == NOUVEAU_BO_GART)
		return 0;
	assert(flags & (NOUVEAU_BO_VRAM|NOUVEAU_BO_GART));

	/* Keep tiling info */
	flags |= (nvbo->flags & (NOUVEAU_BO_TILED|NOUVEAU_BO_ZTILE));

	ret = nouveau_bo_new(nvbo->base.device, flags, 0, nvbo->size, &new);
	if (ret)
		return ret;

	ret = nouveau_bo_kalloc(nouveau_bo(new), NULL);
	if (ret) {
		nouveau_bo_ref(NULL, &new);
		return ret;
	}

	if (nvbo->handle || nvbo->sysmem) {
	nouveau_bo_kmap(nouveau_bo(new));

	if (!nvbo->base.map) {
		nouveau_bo_map(&nvbo->base, NOUVEAU_BO_RD);
		memcpy(nouveau_bo(new)->map, nvbo->base.map, nvbo->base.size);
		nouveau_bo_unmap(&nvbo->base);
	} else {
		memcpy(nouveau_bo(new)->map, nvbo->base.map, nvbo->base.size);
	}
	}

	t_handle = nvbo->handle;
	t_domain = nvbo->domain;
	t_offset = nvbo->offset;
	t_size = nvbo->size;
	t_map = nvbo->map;

	nvbo->handle = nouveau_bo(new)->handle;
	nvbo->domain = nouveau_bo(new)->domain;
	nvbo->offset = nouveau_bo(new)->offset;
	nvbo->size = nouveau_bo(new)->size;
	nvbo->map = nouveau_bo(new)->map;

	nouveau_bo(new)->handle = t_handle;
	nouveau_bo(new)->domain = t_domain;
	nouveau_bo(new)->offset = t_offset;
	nouveau_bo(new)->size = t_size;
	nouveau_bo(new)->map = t_map;

	nouveau_bo_ref(NULL, &new);

	return 0;
}

static int
nouveau_bo_pin_nomm(struct nouveau_bo *bo, uint32_t flags)
{
	struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
	int ret;

	if (!nvbo->handle) {
		if (!(flags & (NOUVEAU_BO_VRAM | NOUVEAU_BO_GART)))
			return -EINVAL;

		ret = nouveau_bo_validate_nomm(nvbo, flags & ~NOUVEAU_BO_PIN);
		if (ret)
			return ret;
	}

	nvbo->pinned = 1;

	/* Fill in public nouveau_bo members */
	bo->flags = nvbo->domain;
	bo->offset = nvbo->offset;

	return 0;
}

int
nouveau_bo_pin(struct nouveau_bo *bo, uint32_t flags)
{
	struct nouveau_device_priv *nvdev = nouveau_device(bo->device);
	struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
	struct drm_nouveau_gem_pin req;
	int ret;

	if (nvbo->pinned)
		return 0;

	if (!nvdev->mm_enabled)
		return nouveau_bo_pin_nomm(bo, flags);

	/* Ensure we have a kernel object... */
	if (!nvbo->handle) {
		if (!(flags & (NOUVEAU_BO_VRAM | NOUVEAU_BO_GART)))
			return -EINVAL;
		nvbo->flags = flags;

		ret = nouveau_bo_kalloc(nvbo, NULL);
		if (ret)
			return ret;
	}

	/* Now force it to stay put :) */
	req.handle = nvbo->handle;
	req.domain = 0;
	if (nvbo->flags & NOUVEAU_BO_VRAM)
		req.domain |= NOUVEAU_GEM_DOMAIN_VRAM;
	if (nvbo->flags & NOUVEAU_BO_GART)
		req.domain |= NOUVEAU_GEM_DOMAIN_GART;

	ret = drmCommandWriteRead(nvdev->fd, DRM_NOUVEAU_GEM_PIN, &req,
				  sizeof(struct drm_nouveau_gem_pin));
	if (ret)
		return ret;
	nvbo->offset = req.offset;
	nvbo->domain = req.domain;
	nvbo->pinned = 1;
	nvbo->flags |= NOUVEAU_BO_PIN;

	/* Fill in public nouveau_bo members */
	if (nvbo->domain & NOUVEAU_GEM_DOMAIN_VRAM)
		bo->flags = NOUVEAU_BO_VRAM;
	if (nvbo->domain & NOUVEAU_GEM_DOMAIN_GART)
		bo->flags = NOUVEAU_BO_GART;
	bo->offset = nvbo->offset;

	return 0;
}

void
nouveau_bo_unpin(struct nouveau_bo *bo)
{
	struct nouveau_device_priv *nvdev = nouveau_device(bo->device);
	struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
	struct drm_nouveau_gem_unpin req;

	if (!nvbo->pinned)
		return;

	if (nvdev->mm_enabled) {
		req.handle = nvbo->handle;
		drmCommandWrite(nvdev->fd, DRM_NOUVEAU_GEM_UNPIN,
				&req, sizeof(req));
	}

	nvbo->pinned = bo->offset = bo->flags = 0;
}

int
nouveau_bo_tile(struct nouveau_bo *bo, uint32_t flags, uint32_t delta,
		uint32_t size)
{
	struct nouveau_device_priv *nvdev = nouveau_device(bo->device);
	struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
	uint32_t kern_flags = 0;
	int ret = 0;

	if (flags & NOUVEAU_BO_TILED) {
		kern_flags |= NOUVEAU_MEM_TILE;
		if (flags & NOUVEAU_BO_ZTILE)
			kern_flags |= NOUVEAU_MEM_TILE_ZETA;
	}

	if (nvdev->mm_enabled) {
		struct drm_nouveau_gem_tile req;

		req.handle = nvbo->handle;
		req.delta = delta;
		req.size = size;
		req.flags = kern_flags;
		ret = drmCommandWrite(nvdev->fd, DRM_NOUVEAU_GEM_TILE,
				      &req, sizeof(req));
	} else {
		struct drm_nouveau_mem_tile req;

		req.offset = nvbo->offset;
		req.delta = delta;
		req.size = size;
		req.flags = kern_flags;

		if (flags & NOUVEAU_BO_VRAM)
			req.flags |= NOUVEAU_MEM_FB;
		if (flags & NOUVEAU_BO_GART)
			req.flags |= NOUVEAU_MEM_AGP;

		ret = drmCommandWrite(nvdev->fd, DRM_NOUVEAU_MEM_TILE,
				      &req, sizeof(req));
	}

	return 0;
}

int
nouveau_bo_busy(struct nouveau_bo *bo, uint32_t access)
{
	struct nouveau_device_priv *nvdev = nouveau_device(bo->device);
	struct nouveau_bo_priv *nvbo = nouveau_bo(bo);

	if (!nvdev->mm_enabled) {
		struct nouveau_fence *fence;

		if (nvbo->pending && (nvbo->pending->write_domains ||
				      (access & NOUVEAU_BO_WR)))
			return 1;

		if (access & NOUVEAU_BO_WR)
			fence = nvbo->fence;
		else
			fence = nvbo->wr_fence;
		return !nouveau_fence(fence)->signalled;
	}

	return 1;
}

struct drm_nouveau_gem_pushbuf_bo *
nouveau_bo_emit_buffer(struct nouveau_channel *chan, struct nouveau_bo *bo)
{
	struct nouveau_pushbuf_priv *nvpb = nouveau_pushbuf(chan->pushbuf);
	struct nouveau_bo_priv *nvbo = nouveau_bo(bo);
	struct drm_nouveau_gem_pushbuf_bo *pbbo;
	struct nouveau_bo *ref = NULL;
	int ret;

	if (nvbo->pending)
		return nvbo->pending;

	if (!nvbo->handle) {
		ret = nouveau_bo_kalloc(nvbo, chan);
		if (ret)
			return NULL;

		if (nvbo->sysmem) {
			void *sysmem_tmp = nvbo->sysmem;

			nvbo->sysmem = NULL;
			ret = nouveau_bo_map(bo, NOUVEAU_BO_WR);
			if (ret)
				return NULL;
			nvbo->sysmem = sysmem_tmp;

			memcpy(bo->map, nvbo->sysmem, nvbo->base.size);
			nouveau_bo_unmap(bo);
			nouveau_bo_ufree(nvbo);
		}
	}

	if (nvpb->nr_buffers >= NOUVEAU_PUSHBUF_MAX_BUFFERS)
		return NULL;
	pbbo = nvpb->buffers + nvpb->nr_buffers++;
	nvbo->pending = pbbo;
	nvbo->pending_channel = chan;

	nouveau_bo_ref(bo, &ref);
	pbbo->user_priv = (uint64_t)(unsigned long)ref;
	pbbo->handle = nvbo->handle;
	pbbo->valid_domains = NOUVEAU_GEM_DOMAIN_VRAM | NOUVEAU_GEM_DOMAIN_GART;
	pbbo->read_domains = 0;
	pbbo->write_domains = 0;
	pbbo->presumed_domain = nvbo->domain;
	pbbo->presumed_offset = nvbo->offset;
	pbbo->presumed_ok = 1;
	return pbbo;
}