summaryrefslogtreecommitdiff
path: root/lib/i915/gem_mman.c
blob: aa9ac6f3d7d81dfb2034c68e199f16a69d78ff8f (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
/*
 * Copyright © 2007, 2011, 2013, 2014, 2019 Intel Corporation
 *
 * 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 (including the next
 * paragraph) 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 OR COPYRIGHT HOLDERS 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 <stdbool.h>
#include <sys/ioctl.h>
#include <errno.h>

#include "igt_core.h"
#include "igt_gt.h"
#include "igt_device.h"
#include "ioctl_wrappers.h"
#include "intel_chipset.h"

#include "gem_create.h"
#include "gem_mman.h"

#ifdef HAVE_VALGRIND
#include <valgrind/valgrind.h>
#include <valgrind/memcheck.h>

#define VG(x) x
#else
#define VG(x) do {} while (0)
#endif

static int gem_mmap_gtt_version(int fd)
{
	struct drm_i915_getparam gp;
	int gtt_version = -1;

	memset(&gp, 0, sizeof(gp));
	gp.param = I915_PARAM_MMAP_GTT_VERSION;
	gp.value = &gtt_version;
	ioctl(fd, DRM_IOCTL_I915_GETPARAM, &gp);

	return gtt_version;
}

bool gem_has_mmap_offset(int fd)
{
	int gtt_version = gem_mmap_gtt_version(fd);

	return gtt_version >= 4;
}

bool gem_has_legacy_mmap(int fd)
{
	struct drm_i915_gem_mmap arg = { .handle = ~0U };

	igt_assert_eq(igt_ioctl(fd, DRM_IOCTL_I915_GEM_MMAP, &arg), -1);

	return errno != EOPNOTSUPP;
}

/**
 * gem_has_mmap_offset_type:
 * @fd: open i915 drm file descriptor
 * @*t: pointer to mmap_offset
 *
 * This functions checks the mmap offset type is supported or not.
 * For discrete memory only FIXED mmap_offset type is supported
 * and for non-discrete memory all other offset type except FIXED
 * are supported.
 *
 * Returns: True if supported or False if not.
 */
bool gem_has_mmap_offset_type(int fd, const struct mmap_offset *t)
{
	if (gem_has_mmap_offset(fd))
		if (gem_has_lmem(fd))
			return t->type == I915_MMAP_OFFSET_FIXED;
		else
			return t->type != I915_MMAP_OFFSET_FIXED;
	else
		return t->type == I915_MMAP_OFFSET_GTT;
}

/**
 * __gem_mmap__gtt:
 * @fd: open i915 drm file descriptor
 * @handle: gem buffer object handle
 * @size: size of the gem buffer
 * @prot: memory protection bits as used by mmap()
 *
 * This functions wraps up procedure to establish a memory mapping through the
 * GTT.
 *
 * Returns: A pointer to the created memory mapping, NULL on failure.
 */
void *__gem_mmap__gtt(int fd, uint32_t handle, uint64_t size, unsigned prot)
{
	struct drm_i915_gem_mmap_gtt mmap_arg;
	void *ptr;

	memset(&mmap_arg, 0, sizeof(mmap_arg));
	mmap_arg.handle = handle;
	if (igt_ioctl(fd, DRM_IOCTL_I915_GEM_MMAP_GTT, &mmap_arg))
		return NULL;

	ptr = mmap64(0, size, prot, MAP_SHARED, fd, mmap_arg.offset);
	if (ptr == MAP_FAILED)
		ptr = NULL;
	else
		errno = 0;

	VG(VALGRIND_MAKE_MEM_DEFINED(ptr, size));

	return ptr;
}

/**
 * gem_mmap__gtt:
 * @fd: open i915 drm file descriptor
 * @handle: gem buffer object handle
 * @size: size of the gem buffer
 * @prot: memory protection bits as used by mmap()
 *
 * Like __gem_mmap__gtt() except we assert on failure.
 *
 * Returns: A pointer to the created memory mapping
 */
void *gem_mmap__gtt(int fd, uint32_t handle, uint64_t size, unsigned prot)
{
	void *ptr = __gem_mmap__gtt(fd, handle, size, prot);
	igt_assert(ptr);
	return ptr;
}

int gem_munmap(void *ptr, uint64_t size)
{
	int ret = munmap(ptr, size);

	if (ret == 0)
		VG(VALGRIND_MAKE_MEM_NOACCESS(ptr, size));

	return ret;
}

bool gem_mmap__has_wc(int fd)
{
	int has_wc = 0;

	struct drm_i915_getparam gp;
	int mmap_version = -1;

	if (gem_mmap_offset__has_wc(fd))
		return true;

	memset(&gp, 0, sizeof(gp));
	gp.param = I915_PARAM_MMAP_VERSION;
	gp.value = &mmap_version;
	ioctl(fd, DRM_IOCTL_I915_GETPARAM, &gp);

	/* Do we have the mmap_ioctl with DOMAIN_WC? */
	if (mmap_version >= 1 && gem_mmap_gtt_version(fd) >= 2) {
		struct drm_i915_gem_mmap arg;

		/* Does this device support wc-mmaps ? */
		memset(&arg, 0, sizeof(arg));
		arg.handle = gem_create(fd, 4096);
		arg.offset = 0;
		arg.size = 4096;
		arg.flags = I915_MMAP_WC;
		has_wc = igt_ioctl(fd, DRM_IOCTL_I915_GEM_MMAP, &arg) == 0;
		gem_close(fd, arg.handle);

		if (has_wc && from_user_pointer(arg.addr_ptr))
			munmap(from_user_pointer(arg.addr_ptr), arg.size);
	}
	errno = 0;

	return has_wc > 0;
}

bool gem_mmap_offset__has_wc(int fd)
{
	int has_wc = 0;
	struct drm_i915_gem_mmap_offset arg;

	if (!gem_has_mmap_offset(fd))
		return false;

	/* Does this device support wc-mmaps ? */
	memset(&arg, 0, sizeof(arg));
	arg.handle = gem_create(fd, 4096);
	arg.offset = 0;
	arg.flags = I915_MMAP_OFFSET_WC;
	has_wc = igt_ioctl(fd, DRM_IOCTL_I915_GEM_MMAP_OFFSET,
			   &arg) == 0;
	gem_close(fd, arg.handle);

	errno = 0;

	return has_wc > 0;
}

bool gem_mmap__has_device_coherent(int fd)
{
	struct drm_i915_gem_mmap_offset arg;
	bool supported;

	if (gem_mmap__has_wc(fd))
		return true;

	/* Maybe we still have GTT mmaps? */
	memset(&arg, 0, sizeof(arg));
	arg.handle = gem_create(fd, 4096);
	arg.offset = 0;
	arg.flags = I915_MMAP_OFFSET_GTT;
	supported = igt_ioctl(fd, DRM_IOCTL_I915_GEM_MMAP_OFFSET,
			      &arg) == 0;
	gem_close(fd, arg.handle);

	errno = 0;

	if (supported)
		return true;

	/*
	 * Maybe this is a discrete device, which only supports fixed mmaps?
	 * Such mappings should also be considered device coherent.
	 */
	memset(&arg, 0, sizeof(arg));
	arg.handle = gem_create(fd, 4096);
	arg.offset = 0;
	arg.flags = I915_MMAP_OFFSET_FIXED;
	supported = igt_ioctl(fd, DRM_IOCTL_I915_GEM_MMAP_OFFSET,
			      &arg) == 0;
	gem_close(fd, arg.handle);

	errno = 0;

	return supported;
}

/**
 * __gem_mmap:
 * @fd: open i915 drm file descriptor
 * @handle: gem buffer object handle
 * @offset: offset in the gem buffer of the mmap arena
 * @size: size of the mmap arena
 * @prot: memory protection bits as used by mmap()
 * @flags: flags used to determine caching
 *
 * This functions wraps up procedure to establish a memory mapping through
 * direct cpu access, bypassing the gpu (valid for wc == false). For wc == true
 * it also bypass cpu caches completely and GTT system agent (i.e. there is no
 * automatic tiling of the mmapping through the fence registers).
 *
 * Returns: A pointer to the created memory mapping, NULL on failure.
 */
static void *__gem_mmap(int fd, uint32_t handle, uint64_t offset, uint64_t size,
			unsigned int prot, uint64_t flags)
{
	struct drm_i915_gem_mmap arg;
	int ret;

	memset(&arg, 0, sizeof(arg));
	arg.handle = handle;
	arg.offset = offset;
	arg.size = size;
	arg.flags = flags;

	ret = igt_ioctl(fd, DRM_IOCTL_I915_GEM_MMAP, &arg);
	if (ret == -1 && errno == EOPNOTSUPP)
		return __gem_mmap_offset(fd, handle, offset, size, prot,
					 flags == I915_MMAP_WC ?
						I915_MMAP_OFFSET_WC :
						I915_MMAP_OFFSET_WB);
	else if (ret)
		return NULL;

	VG(VALGRIND_MAKE_MEM_DEFINED(from_user_pointer(arg.addr_ptr), arg.size));

	errno = 0;
	return from_user_pointer(arg.addr_ptr);
}

/**
 * __gem_mmap_offset:
 * @fd: open i915 drm file descriptor
 * @handle: gem buffer object handle
 * @offset: offset in the gem buffer of the mmap arena
 * @size: size of the mmap arena
 * @prot: memory protection bits as used by mmap()
 * @flags: flags used to determine caching
 *
 * Mmap the gem buffer memory on offset returned in GEM_MMAP_OFFSET ioctl.
 * Offset argument passed in function call must be 0. In the future
 * when driver will allow slice mapping of buffer object this restriction
 * will be removed.
 *
 * Returns: A pointer to the created memory mapping, NULL on failure.
 */
void *__gem_mmap_offset(int fd, uint32_t handle, uint64_t offset, uint64_t size,
			unsigned int prot, uint64_t flags)
{
	struct drm_i915_gem_mmap_offset arg;
	void *ptr;

	if (!gem_has_mmap_offset(fd))
		return NULL;

	igt_assert(offset == 0);

	memset(&arg, 0, sizeof(arg));
	arg.handle = handle;
	arg.flags = flags;

	if (igt_ioctl(fd, DRM_IOCTL_I915_GEM_MMAP_OFFSET, &arg))
		return NULL;

	ptr = mmap64(0, size, prot, MAP_SHARED, fd, arg.offset + offset);

	if (ptr == MAP_FAILED)
		ptr = NULL;
	else
		errno = 0;

	return ptr;
}

/**
 * __gem_mmap__wc:
 * @fd: open i915 drm file descriptor
 * @handle: gem buffer object handle
 * @offset: offset in the gem buffer of the mmap arena
 * @size: size of the mmap arena
 * @prot: memory protection bits as used by mmap()
 *
 * This function wraps up procedure to establish a memory mapping through
 * direct cpu access, bypassing the gpu and cpu caches completely and also
 * bypassing the GTT system agent (i.e. there is no automatic tiling of
 * the mmapping through the fence registers).
 *
 * Returns: A pointer to the created memory mapping, NULL on failure.
 */
void *__gem_mmap__wc(int fd, uint32_t handle, uint64_t offset, uint64_t size, unsigned prot)
{
	return __gem_mmap(fd, handle, offset, size, prot, I915_MMAP_WC);
}

/**
 * gem_mmap__wc:
 * @fd: open i915 drm file descriptor
 * @handle: gem buffer object handle
 * @offset: offset in the gem buffer of the mmap arena
 * @size: size of the mmap arena
 * @prot: memory protection bits as used by mmap()
 *
 * Try to __gem_mmap__wc(). Assert on failure.
 *
 * Returns: A pointer to the created memory mapping
 */
void *gem_mmap__wc(int fd, uint32_t handle, uint64_t offset, uint64_t size, unsigned prot)
{
	void *ptr = __gem_mmap__wc(fd, handle, offset, size, prot);
	igt_assert(ptr);
	return ptr;
}

/**
 * __gem_mmap_offset__wc:
 * @fd: open i915 drm file descriptor
 * @handle: gem buffer object handle
 * @offset: offset in the gem buffer of the mmap arena
 * @size: size of the mmap arena
 * @prot: memory protection bits as used by mmap()
 *
 * This function wraps up procedure to establish a memory mapping through
 * direct cpu access, bypassing the gpu and cpu caches completely and also
 * bypassing the GTT system agent (i.e. there is no automatic tiling of
 * the mmapping through the fence registers).
 *
 * Returns: A pointer to the created memory mapping, NULL on failure.
 */
void *__gem_mmap_offset__wc(int fd, uint32_t handle, uint64_t offset,
			    uint64_t size, unsigned prot)
{
	return __gem_mmap_offset(fd, handle, offset, size, prot,
				 I915_MMAP_OFFSET_WC);
}

/**
 * gem_mmap_offset__wc:
 * @fd: open i915 drm file descriptor
 * @handle: gem buffer object handle
 * @offset: offset in the gem buffer of the mmap arena
 * @size: size of the mmap arena
 * @prot: memory protection bits as used by mmap()
 *
 * Try to __gem_mmap_offset__wc(). Assert on failure.
 *
 * Returns: A pointer to the created memory mapping
 */
void *gem_mmap_offset__wc(int fd, uint32_t handle, uint64_t offset,
			  uint64_t size, unsigned prot)
{
	void *ptr = __gem_mmap_offset__wc(fd, handle, offset, size, prot);

	igt_assert(ptr);
	return ptr;
}

/**
 * __gem_mmap__device_coherent:
 * @fd: open i915 drm file descriptor
 * @handle: gem buffer object handle
 * @offset: offset in the gem buffer of the mmap arena
 * @size: size of the mmap arena
 * @prot: memory protection bits as used by mmap()
 *
 * Returns: A pointer to a block of linear device memory mapped into the
 * process with WC semantics. When no WC is available try to mmap using GGTT.
 */
void *__gem_mmap__device_coherent(int fd, uint32_t handle, uint64_t offset,
				  uint64_t size, unsigned prot)
{
	void *ptr = __gem_mmap_offset(fd, handle, offset, size, prot,
				      I915_MMAP_OFFSET_WC);

	if (!ptr)
		ptr = __gem_mmap_offset__fixed(fd, handle, offset, size, prot);
	if (!ptr)
		ptr = __gem_mmap__wc(fd, handle, offset, size, prot);
	if (!ptr)
		ptr = __gem_mmap__gtt(fd, handle, size, prot);

	return ptr;
}

/**
 * gem_mmap__device_coherent:
 * @fd: open i915 drm file descriptor
 * @handle: gem buffer object handle
 * @offset: offset in the gem buffer of the mmap arena
 * @size: size of the mmap arena
 * @prot: memory protection bits as used by mmap()
 *
 * Call __gem_mmap__device__coherent(), asserts on fail.
 * Offset argument passed in function call must be 0. In the future
 * when driver will allow slice mapping of buffer object this restriction
 * will be removed.
 *
 * Returns: A pointer to the created memory mapping.
 */
void *gem_mmap__device_coherent(int fd, uint32_t handle, uint64_t offset,
				uint64_t size, unsigned prot)
{
	void *ptr;

	igt_assert(offset == 0);

	ptr = __gem_mmap__device_coherent(fd, handle, offset, size, prot);
	igt_assert(ptr);

	return ptr;
}

/**
 * __gem_mmap__cpu:
 * @fd: open i915 drm file descriptor
 * @handle: gem buffer object handle
 * @offset: offset in the gem buffer of the mmap arena
 * @size: size of the mmap arena
 * @prot: memory protection bits as used by mmap()
 *
 * This functions wraps up procedure to establish a memory mapping through
 * direct cpu access, bypassing the gpu completely.
 *
 * Returns: A pointer to the created memory mapping, NULL on failure.
 */
void *__gem_mmap__cpu(int fd, uint32_t handle, uint64_t offset, uint64_t size, unsigned prot)
{
	void *ptr;

	ptr = __gem_mmap(fd, handle, offset, size, prot, 0);
	if (!ptr)
		ptr = __gem_mmap_offset__fixed(fd, handle, offset, size, prot);

	return ptr;
}

/**
 * gem_mmap__cpu:
 * @fd: open i915 drm file descriptor
 * @handle: gem buffer object handle
 * @offset: offset in the gem buffer of the mmap arena
 * @size: size of the mmap arena
 * @prot: memory protection bits as used by mmap()
 *
 * Like __gem_mmap__cpu() except we assert on failure.
 *
 * Returns: A pointer to the created memory mapping
 */
void *gem_mmap__cpu(int fd, uint32_t handle, uint64_t offset, uint64_t size, unsigned prot)
{
	void *ptr = __gem_mmap__cpu(fd, handle, offset, size, prot);
	igt_assert(ptr);
	return ptr;
}

/**
 * __gem_mmap_offset__cpu:
 * @fd: open i915 drm file descriptor
 * @handle: gem buffer object handle
 * @offset: offset in the gem buffer of the mmap arena
 * @size: size of the mmap arena
 * @prot: memory protection bits as used by mmap()
 *
 * This function wraps up procedure to establish a memory mapping through
 * direct cpu access.
 *
 * Returns: A pointer to the created memory mapping, NULL on failure.
 */
void *__gem_mmap_offset__cpu(int fd, uint32_t handle, uint64_t offset,
			     uint64_t size, unsigned prot)
{
	void *ptr;

	ptr = __gem_mmap_offset(fd, handle, offset, size, prot,
				 I915_MMAP_OFFSET_WB);
	if (!ptr)
		ptr = __gem_mmap_offset__fixed(fd, handle, offset, size, prot);

	return ptr;
}

/**
 * gem_mmap_offset__cpu:
 * @fd: open i915 drm file descriptor
 * @handle: gem buffer object handle
 * @offset: offset in the gem buffer of the mmap arena
 * @size: size of the mmap arena
 * @prot: memory protection bits as used by mmap()
 *
 * Like __gem_mmap__cpu() except we assert on failure.
 *
 * Returns: A pointer to the created memory mapping
 */
void *gem_mmap_offset__cpu(int fd, uint32_t handle, uint64_t offset,
			   uint64_t size, unsigned prot)
{
	void *ptr = __gem_mmap_offset(fd, handle, offset, size, prot,
				      I915_MMAP_OFFSET_WB);

	igt_assert(ptr);
	return ptr;
}

void *__gem_mmap_offset__fixed(int fd, uint32_t handle, uint64_t offset,
			       uint64_t size, unsigned prot)
{
	return __gem_mmap_offset(fd, handle, offset, size, prot,
				 I915_MMAP_OFFSET_FIXED);
}

/**
 * gem_mmap_offset__fixed: Used to mmap objects on discrete platforms
 * @fd: open i915 drm file descriptor
 * @handle: gem buffer object handle
 * @offset: offset in the gem buffer of the mmap arena
 * @size: size of the mmap arena
 * @prot: memory protection bits as used by mmap()
 *
 * Like __gem_mmap_offset__fixed() except we assert on failure.
 *
 * For discrete the caching attributes for the pages are fixed at allocation
 * time, and can't be changed. The FIXED mode will simply use the same caching *
 * mode of the allocated pages. This mode will always be coherent with GPU
 * access.
 *
 * On non-discrete platforms this mode is not supported.
 *
 * Returns: A pointer to the created memory mapping
 */
void *gem_mmap_offset__fixed(int fd, uint32_t handle, uint64_t offset,
			   uint64_t size, unsigned prot)
{
	void *ptr = __gem_mmap_offset__fixed(fd, handle, offset, size, prot);

	igt_assert(ptr);
	return ptr;
}

/**
 * __gem_mmap__cpu_coherent:
 * @fd: open i915 drm file descriptor
 * @handle: gem buffer object handle
 * @offset: offset in the gem buffer of the mmap arena
 * @size: size of the mmap arena
 * @prot: memory protection bits as used by mmap()
 *
 * This function wraps up procedure to establish a memory mapping through
 * direct cpu access.
 */
void *__gem_mmap__cpu_coherent(int fd, uint32_t handle, uint64_t offset,
			       uint64_t size, unsigned prot)
{
	void *ptr = __gem_mmap_offset__cpu(fd, handle, offset, size, prot);

	if (!ptr)
		ptr = __gem_mmap__cpu(fd, handle, offset, size, prot);

	return ptr;
}

/**
 * gem_mmap__cpu_coherent:
 * @fd: open i915 drm file descriptor
 * @handle: gem buffer object handle
 * @offset: offset in the gem buffer of the mmap arena
 * @size: size of the mmap arena
 * @prot: memory protection bits as used by mmap()
 *
 * Call __gem_mmap__cpu__coherent(), asserts on fail.
 * Offset argument passed in function call must be 0. In the future
 * when driver will allow slice mapping of buffer object this restriction
 * will be removed.
 *
 * Returns: A pointer to the created memory mapping.
 */
void *gem_mmap__cpu_coherent(int fd, uint32_t handle, uint64_t offset,
			     uint64_t size, unsigned prot)
{
	void *ptr;

	igt_assert(offset == 0);

	ptr = __gem_mmap__cpu_coherent(fd, handle, offset, size, prot);
	igt_assert(ptr);

	return ptr;
}

bool gem_has_mappable_ggtt(int i915)
{
	struct drm_i915_gem_mmap_gtt arg = {};
	int err;

	err = 0;
	if (ioctl(i915, DRM_IOCTL_I915_GEM_MMAP_GTT, &arg))
		err = errno;
	errno = 0;

	return err != ENODEV;
}

void gem_require_mappable_ggtt(int i915)
{
	igt_require_f(gem_has_mappable_ggtt(i915),
		      "HW & kernel support for indirect detiling aperture\n");
}

const struct mmap_offset mmap_offset_types[] = {
	{ "gtt", I915_MMAP_OFFSET_GTT, I915_GEM_DOMAIN_GTT },
	{ "wb", I915_MMAP_OFFSET_WB, I915_GEM_DOMAIN_CPU },
	{ "wc", I915_MMAP_OFFSET_WC, I915_GEM_DOMAIN_WC },
	{ "uc", I915_MMAP_OFFSET_UC, I915_GEM_DOMAIN_WC },
	{ "fixed", I915_MMAP_OFFSET_FIXED, 0},
	{},
};

/**
 * gem_available_aperture_size:
 * @fd: open i915 drm file descriptor
 *
 * Feature test macro to query the kernel for the available gpu aperture size
 * usable in a batchbuffer.
 *
 * Returns: The available gtt address space size.
 */
uint64_t gem_available_aperture_size(int fd)
{
	struct drm_i915_gem_get_aperture aperture = {
		aperture.aper_available_size = 256*1024*1024,
	};

	igt_ioctl(fd, DRM_IOCTL_I915_GEM_GET_APERTURE, &aperture);
	errno = 0;

	return aperture.aper_available_size;
}

/**
 * gem_aperture_size:
 * @fd: open i915 drm file descriptor
 *
 * Feature test macro to query the kernel for the total gpu aperture size.
 *
 * Returns: The total gtt address space size.
 */
uint64_t gem_aperture_size(int fd)
{
	struct drm_i915_gem_context_param p = {
		.param = I915_CONTEXT_PARAM_GTT_SIZE
	};

	if (__gem_context_get_param(fd, &p))
		p.value = gem_global_aperture_size(fd);

	return p.value;
}

/**
 * gem_mappable_aperture_size:
 *
 * Feature test macro to query the kernel for the mappable gpu aperture size.
 * This is the area available for GTT memory mappings.
 *
 * Returns: The mappable gtt address space size.
 */
uint64_t gem_mappable_aperture_size(int fd)
{
	struct pci_device *pci_dev = igt_device_get_pci_device(fd);
	int bar;

	if (intel_gen(pci_dev->device_id) < 3)
		bar = 0;
	else
		bar = 2;

	return pci_dev->regions[bar].size;
}

/**
 * gem_global_aperture_size:
 * @fd: open i915 drm file descriptor
 *
 * Feature test macro to query the kernel for the global gpu aperture size.
 * This is the area available for the kernel to perform address translations.
 *
 * Returns: The gtt address space size.
 */
uint64_t gem_global_aperture_size(int fd)
{
	struct drm_i915_gem_get_aperture aperture = {
		aperture.aper_size = 256*1024*1024
	};

	igt_ioctl(fd, DRM_IOCTL_I915_GEM_GET_APERTURE, &aperture);
	errno = 0;

	return aperture.aper_size;
}

/**
 * gem_available_fences:
 * @fd: open i915 drm file descriptor
 *
 * Feature test macro to query the kernel for the number of available fences
 * usable in a batchbuffer. Only relevant for pre-gen4.
 *
 * Returns: The number of available fences.
 */
int gem_available_fences(int fd)
{
	int num_fences = 0;
	struct drm_i915_getparam gp = {
		gp.param = I915_PARAM_NUM_FENCES_AVAIL,
		gp.value = &num_fences,
	};

	ioctl(fd, DRM_IOCTL_I915_GETPARAM, &gp, sizeof(gp));
	errno = 0;

	return num_fences;
}