summaryrefslogtreecommitdiff
path: root/linux-core/drm-gem.txt
blob: 5cda87f8eb4913b63f32b5fd8df35af947dfe375 (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
                  The Graphics Execution Manager
	      Part of the Direct Rendering Manager
                  ==============================
		  
		 Keith Packard <keithp@keithp.com>
		   Eric Anholt <eric@anholt.net>
			   2008-5-9

Contents:

 1. GEM Overview
 2. API overview and conventions
 3. Object Creation/Destruction
 4. Reading/writing contents
 5. Mapping objects to userspace
 6. Memory Domains
 7. Execution (Intel specific)
 8. Other misc Intel-specific functions

1. Graphics Execution Manager Overview

Gem is designed to manage graphics memory, control access to the graphics
device execution context and handle the essentially NUMA environment unique
to modern graphics hardware. Gem allows multiple applications to share
graphics device resources without the need to constantly reload the entire
graphics card. Data may be shared between multiple applications with gem
ensuring that the correct memory synchronization occurs.

Graphics data can consume arbitrary amounts of memory, with 3D applications
constructing ever larger sets of textures and vertices. With graphics cards
memory space growing larger every year, and graphics APIs growing more
complex, we can no longer insist that each application save a complete copy
of their graphics state so that the card can be re-initialized from user
space at each context switch. Ensuring that graphics data remains persistent
across context switches allows applications significant new functionality
while also improving performance for existing APIs.

Modern linux desktops include significant 3D rendering as a fundemental
component of the desktop image construction process. 2D and 3D applications
paint their content to offscreen storage and the central 'compositing
manager' constructs the final screen image from those window contents.  This
means that pixel image data from these applications must move within reach
of the compositing manager and used as source operands for screen image
rendering operations.

Gem provides simple mechanisms to manage graphics data and control execution
flow within the linux operating system. Using many existing kernel
subsystems, it does this with a modest amount of code.

2. API Overview and Conventions

All APIs here are defined in terms of ioctls appplied to the DRM file
descriptor. To create and manipulate objects, an application must be
'authorized' using the DRI or DRI2 protocols with the X server. To relax
that, we will need to implement some better access control mechanisms within
the hardware portion of the driver to prevent inappropriate
cross-application data access.

Any DRM driver which does not support GEM will return -ENODEV for all of
these ioctls. Invalid object handles return -EINVAL. Invalid object names
return -ENOENT. Other errors are as documented in the specific API below.

To avoid the need to translate ioctl contents on mixed-size systems (with
32-bit user space running on a 64-bit kernel), the ioctl data structures
contain explicitly sized objects, using 64-bits for all size and pointer
data and 32-bits for identifiers. In addition, the 64-bit objects are all
carefully aligned on 64-bit boundaries. Because of this, all pointers in the
ioctl data structures are passed as uint64_t values. Suitable casts will
be necessary.

One significant operation which is explicitly left out of this API is object
locking. Applications are expected to perform locking of shared objects
outside of the GEM api. This kind of locking is not necessary to safely
manipulate the graphics engine, and with multiple objects interacting in
unknown ways, per-object locking would likely introduce all kinds of
lock-order issues. Punting this to the application seems like the only
sensible plan. Given that DRM already offers a global lock on the hardware,
this doesn't change the current situation.

3. Object Creation and Destruction

Gem provides explicit memory management primitives. System pages are
allocated when the object is created, either as the fundemental storage for
hardware where system memory is used by the graphics processor directly, or
as backing store for graphics-processor resident memory.

Objects are referenced from user space using handles. These are, for all
intents and purposes, equivalent to file descriptors. We could simply use
file descriptors were it not for the small limit (1024) of file descriptors
available to applications, and for the fact that the X server (a rather
significant user of this API) uses 'select' and has a limited maximum file
descriptor for that operation. Given the ability to allocate more file
descriptors, and given the ability to place these 'higher' in the file
descriptor space, we'd love to simply use file descriptors.

Objects may be published with a name so that other applications can access
them. The name remains valid as long as the object exists. Right now, our
DRI APIs use 32-bit integer names, so that's what we expose here

 A. Creation

		struct drm_gem_create {
			/**
			 * Requested size for the object.
			 *
			 * The (page-aligned) allocated size for the object
			 * will be returned.
			 */
			uint64_t size;
			/**
			 * Returned handle for the object.
			 *
			 * Object handles are nonzero.
			 */
			uint32_t handle;
			uint32_t pad;
		};
	
		/* usage */
    		create.size = 16384;
		ret = ioctl (fd, DRM_IOCTL_GEM_CREATE, &create);
		if (ret == 0)
			return create.handle;

	Note that the size is rounded up to a page boundary, and that
	the rounded-up size is returned in 'size'. No name is assigned to
	this object, making it local to this process.

	If insufficient memory is availabe, -ENOMEM will be returned.

 B. Closing

		struct drm_gem_close {
			/** Handle of the object to be closed. */
			uint32_t handle;
			uint32_t pad;
		};
		

		/* usage */
		close.handle = <handle>;
		ret = ioctl (fd, DRM_IOCTL_GEM_CLOSE, &close);

	This call makes the specified handle invalid, and if no other
	applications are using the object, any necessary graphics hardware
	synchronization is performed and the resources used by the object
	released.

 C. Naming

		struct drm_gem_flink {
			/** Handle for the object being named */
			uint32_t handle;
		
			/** Returned global name */
			uint32_t name;
		};
		
		/* usage */
		flink.handle = <handle>;
		ret = ioctl (fd, DRM_IOCTL_GEM_FLINK, &flink);
		if (ret == 0)
			return flink.name;

	Flink creates a name for the object and returns it to the
	application. This name can be used by other applications to gain
	access to the same object.

 D. Opening by name

		struct drm_gem_open {
			/** Name of object being opened */
			uint32_t name;
		
			/** Returned handle for the object */
			uint32_t handle;
			
			/** Returned size of the object */
			uint64_t size;
		};
		
		/* usage */
		open.name = <name>;
		ret = ioctl (fd, DRM_IOCTL_GEM_OPEN, &open);
		if (ret == 0) {
			*sizep = open.size;
			return open.handle;
		}

	Open accesses an existing object and returns a handle for it. If the
	object doesn't exist, -ENOENT is returned. The size of the object is
	also returned. This handle has all the same capabilities as the
	handle used to create the object. In particular, the object is not
	destroyed until all handles are closed.

4. Basic read/write operations

By default, gem objects are not mapped to the applications address space,
getting data in and out of them is done with I/O operations instead. This
allows the data to reside in otherwise unmapped pages, including pages in
video memory on an attached discrete graphics card. In addition, using
explicit I/O operations allows better control over cache contents, as
graphics devices are generally not cache coherent with the CPU, mapping
pages used for graphics into an application address space requires the use
of expensive cache flushing operations. Providing direct control over
graphics data access ensures that data are handled in the most efficient
possible fashion.

 A. Reading

		struct drm_gem_pread {
			/** Handle for the object being read. */
			uint32_t handle;
			uint32_t pad;
			/** Offset into the object to read from */
			uint64_t offset;
			/** Length of data to read */
			uint64_t size;
			/** Pointer to write the data into. */
			uint64_t data_ptr;	/* void * */
		};

	This copies data into the specified object at the specified
	position. Any necessary graphics device synchronization and
	flushing will be done automatically.
		
		struct drm_gem_pwrite {
			/** Handle for the object being written to. */
			uint32_t handle;
			uint32_t pad;
			/** Offset into the object to write to */
			uint64_t offset;
			/** Length of data to write */
			uint64_t size;
			/** Pointer to read the data from. */
			uint64_t data_ptr;	/* void * */
		};
		
	This copies data out of the specified object into the
	waiting user memory. Again, device synchronization will
	be handled by the kernel to ensure user space sees a
	consistent view of the graphics device.

5. Mapping objects to user space

For most objects, reading/writing is the preferred interaction mode.
However, when the CPU is involved in rendering to cover deficiencies in
hardware support for particular operations, the CPU will want to directly
access the relevant objects. 

Because mmap is fairly heavyweight, we allow applications to retain maps to
objects persistently and then update how they're using the memory through a
separate interface. Applications which fail to use this separate interface
may exhibit unpredictable behaviour as memory consistency will not be
preserved.

 A. Mapping

		struct drm_gem_mmap {
			/** Handle for the object being mapped. */
			uint32_t handle;
			uint32_t pad;
			/** Offset in the object to map. */
			uint64_t offset;
			/**
			 * Length of data to map.
			 *
			 * The value will be page-aligned.
			 */
			uint64_t size;
			/** Returned pointer the data was mapped at */
			uint64_t addr_ptr;	/* void * */
		};
		
		/* usage */
		mmap.handle = <handle>;
		mmap.offset = <offset>;
		mmap.size = <size>;
		ret = ioctl (fd, DRM_IOCTL_GEM_MMAP, &mmap);
		if (ret == 0)
			return (void *) (uintptr_t) mmap.addr_ptr;


 B. Unmapping

		munmap (addr, length);

	Nothing strange here, just use the normal munmap syscall.

6. Memory Domains

Graphics devices remain a strong bastion of non cache-coherent memory. As a
result, accessing data through one functional unit will end up loading that
cache with data which then needs to be manually synchronized when that data
is used with another functional unit.

Tracking where data are resident is done by identifying how functional units
deal with caches. Each cache is labeled as a separate memory domain. Then,
each sequence of operations is expected to load data into various read
domains and leave data in at most one write domain. Gem tracks the read and
write memory domains of each object and performs the necessary
synchronization operations when objects move from one domain set to another.

For example, if operation 'A' constructs an image that is immediately used
by operation 'B', then when the read domain for 'B' is not the same as the
write domain for 'A', then the write domain must be flushed, and the read
domain invalidated. If these two operations are both executed in the same
command queue, then the flush operation can go inbetween them in the same
queue, avoiding any kind of CPU-based synchronization and leaving the GPU to
do the work itself.

6.1 Memory Domains (GPU-independent)

 * DRM_GEM_DOMAIN_CPU.

 Objects in this domain are using caches which are connected to the CPU.
 Moving objects from non-CPU domains into the CPU domain can involve waiting
 for the GPU to finish with operations using this object. Moving objects
 from this domain to a GPU domain can involve flushing CPU caches and chipset
 buffers.

6.1 GPU-independent memory domain ioctl

This ioctl is independent of the GPU in use. So far, no use other than
synchronizing objects to the CPU domain have been found; if that turns out
to be generally true, this ioctl may be simplified further.
   
 A. Explicit domain control

		struct drm_gem_set_domain {
			/** Handle for the object */
			uint32_t handle;
		
			/** New read domains */
			uint32_t read_domains;
		
			/** New write domain */
			uint32_t write_domain;
		};

		/* usage */
		set_domain.handle = <handle>;
		set_domain.read_domains = <read_domains>;
		set_domain.write_domain = <write_domain>;
		ret = ioctl (fd, DRM_IOCTL_GEM_SET_DOMAIN, &set_domain);
		
	When the application wants to explicitly manage memory domains for
	an object, it can use this function. Usually, this is only used
	when the application wants to synchronize object contents between
	the GPU and CPU-based application rendering. In that case,
	the <read_domains> would be set to DRM_GEM_DOMAIN_CPU, and if the
	application were going to write to the object, the <write_domain>
	would also be set to DRM_GEM_DOMAIN_CPU. After the call, gem
	guarantees that all previous rendering operations involving this
	object are complete. The application is then free to access the
	object through the address returned by the mmap call. Afterwards,
	when the application again uses the object through the GPU, any
	necessary CPU flushing will occur and the object will be correctly
	synchronized with the GPU.

	Note that this synchronization is not required for any accesses
	going through the driver itself. The pread, pwrite and execbuffer
	ioctls all perform the necessary domain management internally.
	Explicit synchronization is only necessary when accessing the object
	through the mmap'd address.

7. Execution (Intel specific)

Managing the command buffers is inherently chip-specific, so the core of gem
doesn't have any intrinsic functions. Rather, execution is left to the
device-specific portions of the driver.

The Intel DRM_I915_GEM_EXECBUFFER ioctl takes a list of gem objects, all of
which are mapped to the graphics device. The last object in the list is the
command buffer.

7.1. Relocations
 
Command buffers often refer to other objects, and to allow the kernel driver
to move objects around, a sequence of relocations is associated with each
object. Device-specific relocation operations are used to place the
target-object relative value into the object.

The Intel driver has a single relocation type:

		struct drm_i915_gem_relocation_entry {
			/**
			 * Handle of the buffer being pointed to by this
			 * relocation entry.
			 *
			 * It's appealing to make this be an index into the
			 * mm_validate_entry list to refer to the buffer,
			 * but this allows the driver to create a relocation
			 * list for state buffers and not re-write it per
			 * exec using the buffer.
			 */
			uint32_t target_handle;
		
			/**
			 * Value to be added to the offset of the target
			 * buffer to make up the relocation entry.
			 */
			uint32_t delta;
		
			/**
			 * Offset in the buffer the relocation entry will be
			 * written into
			 */
			uint64_t offset;
		
			/**
			 * Offset value of the target buffer that the
			 * relocation entry was last written as.
			 *
			 * If the buffer has the same offset as last time, we
			 * can skip syncing and writing the relocation.  This
			 * value is written back out by the execbuffer ioctl
			 * when the relocation is written.
			 */
			uint64_t presumed_offset;
		
			/**
			 * Target memory domains read by this operation.
			 */
			uint32_t read_domains;
		
			/*
			 * Target memory domains written by this operation.
			 *
			 * Note that only one domain may be written by the
			 * whole execbuffer operation, so that where there are
			 * conflicts, the application will get -EINVAL back.
			 */
			uint32_t write_domain;
		};
		
	'target_handle', the handle to the target object. This object must
	be one of the objects listed in the execbuffer request or
	bad things will happen. The kernel doesn't check for this.

	'offset' is where, in the source object, the relocation data
	are written. Each relocation value is a 32-bit value consisting
	of the location of the target object in the GPU memory space plus
	the 'delta' value included in the relocation.

	'presumed_offset' is where user-space believes the target object
	lies in GPU memory space. If this value matches where the object
	actually is, then no relocation data are written, the kernel
	assumes that user space has set up data in the source object
	using this presumption. This offers a fairly important optimization
	as writing relocation data requires mapping of the source object
	into the kernel memory space.

	'read_domains' and 'write_domains' list the usage by the source
	object of the target object. The kernel unions all of the domain
	information from all relocations in the execbuffer request. No more
	than one write_domain is allowed, otherwise an EINVAL error is
	returned. read_domains must contain write_domain. This domain
	information is used to synchronize buffer contents as described
	above in the section on domains.

7.1.1 Memory Domains (Intel specific)

The Intel GPU has several internal caches which are not coherent and hence
require explicit synchronization. Memory domains provide the necessary data
to synchronize what is needed while leaving other cache contents intact.

 * DRM_GEM_DOMAIN_I915_RENDER.
   The GPU 3D and 2D rendering operations use a unified rendering cache, so
   operations doing 3D painting and 2D blts will use this domain
   
 * DRM_GEM_DOMAIN_I915_SAMPLER
   Textures are loaded by the sampler through a separate cache, so
   any texture reading will use this domain. Note that the sampler
   and renderer use different caches, so moving an object from render target
   to texture source will require a domain transfer.
   
 * DRM_GEM_DOMAIN_I915_COMMAND
   The command buffer doesn't have an explicit cache (although it does
   read ahead quite a bit), so this domain just indicates that the object
   needs to be flushed to the GPU.
   
 * DRM_GEM_DOMAIN_I915_INSTRUCTION
   All of the programs on Gen4 and later chips use an instruction cache to
   speed program execution. It must be explicitly flushed when new programs
   are written to memory by the CPU.

 * DRM_GEM_DOMAIN_I915_VERTEX
   Vertex data uses two different vertex caches, but they're
   both flushed with the same instruction.

7.2 Execution object list (Intel specific)

		struct drm_i915_gem_exec_object {
			/**
			 * User's handle for a buffer to be bound into the GTT
			 * for this operation.
			 */
			uint32_t handle;
			
			/**
			 * List of relocations to be performed on this buffer
			 */
			uint32_t relocation_count;
			/* struct drm_i915_gem_relocation_entry *relocs */
			uint64_t relocs_ptr;
			
			/** 
			 * Required alignment in graphics aperture 
			 */
			uint64_t alignment;
		
			/**
			 * Returned value of the updated offset of the object,
			 * for future presumed_offset writes.
			 */
			uint64_t offset;
		};
			
	Each object involved in a particular execution operation must be
	listed using one of these structures.

	'handle' references the object.

	'relocs_ptr' is a user-mode pointer to a array of 'relocation_count'
	drm_i915_gem_relocation_entry structs (see above) that
	define the relocations necessary in this buffer. Note that all
	relocations must reference other exec_object structures in the same
	execbuffer ioctl and that those other buffers must come earlier in
	the exec_object array. In other words, the dependencies mapped by the
	exec_object relocations must form a directed acyclic graph.

	'alignment' is the byte alignment necessary for this buffer. Each
	object has specific alignment requirements, as the kernel doesn't
	know what each object is being used for, those requirements must be
	provided by user mode. If an object is used in two different ways,
	it's quite possible that the alignment requirements will differ.

	'offset' is a return value, receiving the location of the object
	during this execbuffer operation. The application should use this
	as the presumed offset in future operations; if the object does not
	move, then kernel need not write relocation data.

7.3 Execbuffer ioctl (Intel specific)

		struct drm_i915_gem_execbuffer {
			/**
			 * List of buffers to be validated with their
			 * relocations to be performend on them.
			 *
			 * These buffers must be listed in an order such that
			 * all relocations a buffer is performing refer to
			 * buffers that have already appeared in the validate
			 * list.
			 */
			/* struct drm_i915_gem_validate_entry *buffers */
			uint64_t buffers_ptr;
			uint32_t buffer_count;
		
			/**
			 * Offset in the batchbuffer to start execution from.
			 */
			uint32_t batch_start_offset;
			
			/**
			 * Bytes used in batchbuffer from batch_start_offset
			 */
			uint32_t batch_len;
			uint32_t DR1;
			uint32_t DR4;
			uint32_t num_cliprects;
			uint64_t cliprects_ptr;	/* struct drm_clip_rect *cliprects */
		};
		

	'buffers_ptr' is a user-mode pointer to an array of 'buffer_count'
	drm_i915_gem_exec_object structures which contains the complete set
	of objects required for this execbuffer operation. The last entry in
	this array, the 'batch buffer', is the buffer of commands which will
	be linked to the ring and executed.

	'batch_start_offset' is the byte offset within the batch buffer which
	contains the first command to execute. So far, we haven't found a
	reason to use anything other than '0' here, but the thought was that
	some space might be allocated for additional initialization which
	could be skipped in some cases. This must be a multiple of 4.

	'batch_len' is the length, in bytes, of the data to be executed
	(i.e., the amount of data after batch_start_offset). This must
	be a multiple of 4.

	'num_cliprects' and 'cliprects_ptr' reference an array of
	drm_clip_rect structures that is num_cliprects long. The entire
	batch buffer will be executed multiple times, once for each
	rectangle in this list. If num_cliprects is 0, then no clipping
	rectangle will be set.

	'DR1' and 'DR4' are portions of the 3DSTATE_DRAWING_RECTANGLE
	command which will be queued when this operation is clipped
	(num_cliprects != 0).

		DR1 bit		definition
		31		Fast Scissor Clip Disable (debug only).
				Disables a hardware optimization that
				improves performance. This should have
				no visible effect, other than reducing
				performance
				
		30		Depth Buffer Coordinate Offset Disable.
				This disables the addition of the
				depth buffer offset bits which are used
				to change the location of the depth buffer
				relative to the front buffer.

		27:26		X Dither Offset. Specifies the X pixel
				offset to use when accessing the dither table
				
		25:24		Y Dither Offset. Specifies the Y pixel
				offset to use when accessing the dither
				table.

		DR4 bit		definition
		31:16		Drawing Rectangle Origin Y. Specifies the Y
				origin of coordinates relative to the
				draw buffer.

		15:0		Drawing Rectangle Origin X. Specifies the X
				origin of coordinates relative to the
				draw buffer.

	As you can see, these two fields are necessary for correctly
	offsetting drawing within a buffer which contains multiple surfaces.
	Note that DR1 is only used on Gen3 and earlier hardware and that
	newer hardware sticks the dither offset elsewhere.

7.3.1 Detailed Execution Description

	Execution of a single batch buffer requires several preparatory
	steps to make the objects visible to the graphics engine and resolve
	relocations to account for their current addresses.

 A. Mapping and Relocation

	Each exec_object structure in the array is examined in turn. 
	
	If the object is not already bound to the GTT, it is assigned a
	location in the graphics address space. If no space is available in
	the GTT, some other object will be evicted. This may require waiting
	for previous execbuffer requests to complete before that object can
	be unmapped. With the location assigned, the pages for the object
	are pinned in memory using find_or_create_page and the GTT entries
	updated to point at the relevant pages using drm_agp_bind_pages.
	
	Then the array of relocations is traversed. Each relocation record
	looks up the target object and, if the presumed offset does not
	match the current offset (remember that this buffer has already been
	assigned an address as it must have been mapped earlier), the
	relocation value is computed using the current offset.  If the
	object is currently in use by the graphics engine, writing the data
	out must be preceeded by a delay while the object is still busy.
	Once it is idle, then the page containing the relocation is mapped
	by the CPU and the updated relocation data written out.

	The read_domains and write_domain entries in each relocation are
	used to compute the new read_domains and write_domain values for the
	target buffers. The actual execution of the domain changes must wait
	until all of the exec_object entries have been evaluated as the
	complete set of domain information will not be available until then.
	
 B. Memory Domain Resolution

	After all of the new memory domain data has been pulled out of the
	relocations and computed for each object, the list of objects is
	again traversed and the new memory domains compared against the
	current memory domains. There are two basic operations involved here:

 	 * Flushing the current write domain. If the new read domains
	   are not equal to the current write domain, then the current
	   write domain must be flushed. Otherwise, reads will not see data
	   present in the write domain cache. In addition, any new read domains
	   other than the current write domain must be invalidated to ensure
	   that the flushed data are re-read into their caches.

	 * Invaliding new read domains. Any domains which were not currently
	   used for this object must be invalidated as old objects which
	   were mapped at the same location may have stale data in the new
	   domain caches.

	If the CPU cache is being invalidated and some GPU cache is being
	flushed, then we'll have to wait for rendering to complete so that
	any pending GPU writes will be complete before we flush the GPU
	cache.

	If the CPU cache is being flushed, then we use 'clflush' to get data
	written from the CPU.

	Because the GPU caches cannot be partially flushed or invalidated,
	we don't actually flush them during this traversal stage. Rather, we
	gather the invalidate and flush bits up in the device structure.

	Once all of the object domain changes have been evaluated, then the
	gathered invalidate and flush bits are examined. For any GPU flush
	operations, we emit a single MI_FLUSH command that performs all of
	the necessary flushes. We then look to see if the CPU cache was
	flushed. If so, we use the chipset flush magic (writing to a special
	page) to get the data out of the chipset and into memory.

 C. Queuing Batch Buffer to the Ring

	With all of the objects resident in graphics memory space, and all
	of the caches prepared with appropriate data, the batch buffer
	object can be queued to the ring. If there are clip rectangles, then
	the buffer is queued once per rectangle, with suitable clipping
	inserted into the ring just before the batch buffer.

 D. Creating an IRQ Cookie

	Right after the batch buffer is placed in the ring, a request to
	generate an IRQ is added to the ring along with a command to write a
	marker into memory. When the IRQ fires, the driver can look at the
	memory location to see where in the ring the GPU has passed. This
	magic cookie value is stored in each object used in this execbuffer
	command; it is used whereever you saw 'wait for rendering' above in
	this document.

 E. Writing back the new object offsets

	So that the application has a better idea what to use for
	'presumed_offset' values later, the current object offsets are
	written back to the exec_object structures.


8. Other misc Intel-specific functions.

To complete the driver, a few other functions were necessary.

8.1 Initialization from the X server

As the X server is currently responsible for apportioning memory between 2D
and 3D, it must tell the kernel which region of the GTT aperture is
available for 3D objects to be mapped into.

		struct drm_i915_gem_init {
			/**
			 * Beginning offset in the GTT to be managed by the
			 * DRM memory manager.
			 */
			uint64_t gtt_start;
			/**
			 * Ending offset in the GTT to be managed by the DRM
			 * memory manager.
			 */
			uint64_t gtt_end;
		};
		/* usage */
		init.gtt_start = <gtt_start>;
		init.gtt_end = <gtt_end>;
		ret = ioctl (fd, DRM_IOCTL_I915_GEM_INIT, &init);

	The GTT aperture between gtt_start and gtt_end will be used to map
	objects. This also tells the kernel that the ring can be used,
	pulling the ring addresses from the device registers.

8.2 Pinning objects in the GTT

For scan-out buffers and the current shared depth and back buffers, we need
to have them always available in the GTT, at least for now. Pinning means to
lock their pages in memory along with keeping them at a fixed offset in the
graphics aperture. These operations are available only to root.
		
		struct drm_i915_gem_pin {
			/** Handle of the buffer to be pinned. */
			uint32_t handle;
			uint32_t pad;
			
			/** alignment required within the aperture */
			uint64_t alignment;
		
			/** Returned GTT offset of the buffer. */
			uint64_t offset;
		};

		/* usage */
		pin.handle = <handle>;
		pin.alignment = <alignment>;
		ret = ioctl (fd, DRM_IOCTL_I915_GEM_PIN, &pin);
		if (ret == 0)
			return pin.offset;

	Pinning an object ensures that it will not be evicted from the GTT
	or moved. It will stay resident until destroyed or unpinned.
		
		struct drm_i915_gem_unpin {
			/** Handle of the buffer to be unpinned. */
			uint32_t handle;
			uint32_t pad;
		};
		
		/* usage */
		unpin.handle = <handle>;
		ret = ioctl (fd, DRM_IOCTL_I915_GEM_UNPIN, &unpin);
		
	Unpinning an object makes it possible to evict this object from the
	GTT. It doesn't ensure that it will be evicted, just that it may.