-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgem.c
1196 lines (965 loc) · 28.3 KB
/
gem.c
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
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright © 2015 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 <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/sysmacros.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdint.h>
#include <signal.h>
#include <unistd.h>
#include <error.h>
#include <errno.h>
#include <dlfcn.h>
#include <sys/prctl.h>
#include "drm_uapi/i915_drm.h"
#include "ksim.h"
#define DRM_MAJOR 226
static int (*libc_close)(int fd);
static int (*libc_ioctl)(int fd, unsigned long request, void *argp);
static void *(*libc_mmap)(void *addr, size_t length, int prot, int flags, int fd, off_t offset);
static int (*libc_munmap)(void *addr, size_t length);
static int drm_fd = -1;
static int memfd = -1;
static int memfd_size = MEMFD_INITIAL_SIZE;
#define STUB_BO_USERPTR 1
#define STUB_BO_PRIME 2
struct stub_bo {
union {
/* Offset into memfd when in use */
uint64_t offset;
/* Next pointer when on free list. */
void *next;
};
uint64_t gtt_offset;
uint32_t size;
uint32_t stride; /* tiling in lower 2 bits */
void *map;
uint32_t kernel_handle;
};
struct gtt_entry {
uint32_t handle;
};
struct gtt_map {
struct stub_bo *bo;
void *virtual;
size_t length;
off_t offset;
int prot;
struct list link;
};
static LIST_INITIALIZER(pending_map_list);
static LIST_INITIALIZER(dirty_map_list);
static LIST_INITIALIZER(clean_map_list);
static struct stub_bo bos[4096], *bo_free_list;
static int next_handle = 1;
#define gtt_order 20
static const uint64_t gtt_size = 4096ul << gtt_order;
static struct gtt_entry gtt[1 << gtt_order];
static uint64_t next_offset = 0ul;
static uint64_t
alloc_range(size_t size)
{
uint64_t offset;
if (memfd == -1) {
memfd = memfd_create("ksim bo", MFD_CLOEXEC);
ftruncate(memfd, MEMFD_INITIAL_SIZE);
}
offset = memfd_size;
memfd_size += align_u64(size, 4096);
ftruncate(memfd, memfd_size);
return offset;
}
static void
free_range(uint64_t offset, size_t size)
{
}
static struct stub_bo *
create_bo(uint64_t size)
{
struct stub_bo *bo;
if (bo_free_list) {
bo = bo_free_list;
bo_free_list = bo->next;
} else {
ksim_assert(next_handle < ARRAY_LENGTH(bos));
bo = &bos[next_handle++];
}
bo->gtt_offset = NOT_BOUND;
bo->size = size;
bo->stride = 0;
return bo;
}
static struct stub_bo *
get_bo(int handle)
{
if (0 < handle && handle < next_handle &&
bos[handle].gtt_offset != FREED)
return &bos[handle];
return NULL;
}
static inline uint32_t
get_handle(struct stub_bo *bo)
{
return bo - bos;
}
void
bind_bo(struct stub_bo *bo, uint64_t offset)
{
uint32_t num_pages = (bo->size + 4095) >> 12;
uint32_t start_page = offset >> 12;
ksim_assert(bo->size > 0);
ksim_assert(offset < gtt_size);
ksim_assert(offset + bo->size < gtt_size);
bo->gtt_offset = offset;
for (uint32_t p = 0; p < num_pages; p++) {
ksim_assert(gtt[start_page + p].handle == 0);
gtt[start_page + p].handle = get_handle(bo);
}
}
void *
map_gtt_offset(uint64_t offset, uint64_t *range)
{
struct gtt_entry entry;
struct stub_bo *bo;
ksim_assert(offset < gtt_size);
entry = gtt[offset >> 12];
bo = get_bo(entry.handle);
ksim_assert(bo != NULL);
ksim_assert(bo->gtt_offset != NOT_BOUND && bo->size > 0);
ksim_assert(bo->gtt_offset <= offset);
ksim_assert(offset < bo->gtt_offset + bo->size);
*range = bo->gtt_offset + bo->size - offset;
return bo->map + (offset - bo->gtt_offset);
}
static void
close_bo(struct stub_bo *bo)
{
free_range(bo->offset, bo->size);
if (bo->kernel_handle) {
struct drm_gem_close gem_close;
int ret;
ksim_assert(bo->map != NULL);
gem_close.handle = bo->kernel_handle;
ret = libc_ioctl(drm_fd, DRM_IOCTL_GEM_CLOSE, &gem_close);
ksim_assert(ret == 0);
bo->map = NULL;
bo->kernel_handle = 0;
}
/* Anything on the dirty or clean list will be freed when at
* munmap time. */
struct gtt_map *m;
if (list_find(m, &pending_map_list, link, m->bo == bo)) {
list_remove(&m->link);
free(m);
}
if (bo->offset != STUB_BO_USERPTR)
munmap(bo->map, bo->size);
bo->gtt_offset = FREED;
bo->next = bo_free_list;
bo_free_list = bo;
}
static void
set_kernel_tiling(struct stub_bo *bo)
{
int ret;
struct drm_i915_gem_set_tiling set_tiling = {
.handle = bo->kernel_handle,
.tiling_mode = bo->stride & 3,
.stride = bo->stride & ~3u,
.swizzle_mode = 0,
};
ret = libc_ioctl(drm_fd, DRM_IOCTL_I915_GEM_SET_TILING, &set_tiling);
ksim_assert(ret != -1);
}
static uint32_t
get_kernel_handle(struct stub_bo *bo)
{
struct drm_i915_gem_userptr userptr;
int ret;
if (bo->kernel_handle)
return bo->kernel_handle;
bo->map = mmap(NULL, bo->size, PROT_READ | PROT_WRITE,
MAP_SHARED, memfd, bo->offset);
ksim_assert(bo->map != MAP_FAILED);
userptr.user_ptr = (uint64_t) bo->map;
userptr.user_size = bo->size;
userptr.flags = 0;
ret = libc_ioctl(drm_fd, DRM_IOCTL_I915_GEM_USERPTR, &userptr);
ksim_assert(ret != -1);
bo->kernel_handle = userptr.handle;
set_kernel_tiling(bo);
return bo->kernel_handle;
}
__attribute__ ((visibility ("default"))) int
close(int fd)
{
if (fd == drm_fd)
drm_fd = -1;
return libc_close(fd);
}
static int
dispatch_getparam(int fd, unsigned long request,
struct drm_i915_getparam *getparam)
{
switch (getparam->param) {
case I915_PARAM_IRQ_ACTIVE:
case I915_PARAM_ALLOW_BATCHBUFFER:
case I915_PARAM_LAST_DISPATCH:
case I915_PARAM_NUM_FENCES_AVAIL:
case I915_PARAM_HAS_OVERLAY:
case I915_PARAM_HAS_PAGEFLIPPING:
case I915_PARAM_HAS_PRIME_VMAP_FLUSH:
case I915_PARAM_HAS_SECURE_BATCHES:
case I915_PARAM_HAS_PINNED_BATCHES:
errno = EINVAL;
return -1;
case I915_PARAM_CHIPSET_ID:
*getparam->value = 0x1916;
return 0;
case I915_PARAM_HAS_GEM:
case I915_PARAM_HAS_EXECBUF2:
case I915_PARAM_HAS_RELAXED_FENCING:
case I915_PARAM_HAS_LLC:
case I915_PARAM_HAS_WAIT_TIMEOUT:
case I915_PARAM_HAS_EXEC_NO_RELOC:
case I915_PARAM_HAS_EXEC_HANDLE_LUT:
case I915_PARAM_HAS_COHERENT_RINGS:
case I915_PARAM_HAS_EXEC_CONSTANTS:
case I915_PARAM_HAS_RELAXED_DELTA:
case I915_PARAM_HAS_GEN7_SOL_RESET:
case I915_PARAM_HAS_ALIASING_PPGTT:
case I915_PARAM_HAS_SEMAPHORES:
case I915_PARAM_HAS_WT:
case I915_PARAM_HAS_COHERENT_PHYS_GTT:
case I915_PARAM_HAS_EXEC_SOFTPIN:
case I915_PARAM_HAS_EXEC_ASYNC:
*getparam->value = 1;
return 0;
case I915_PARAM_HAS_BSD:
case I915_PARAM_HAS_BLT:
case I915_PARAM_HAS_VEBOX:
case I915_PARAM_HAS_BSD2:
case I915_PARAM_HAS_RESOURCE_STREAMER:
case I915_PARAM_HAS_EXEC_FENCE:
case I915_PARAM_HAS_EXEC_BATCH_FIRST:
case I915_PARAM_HAS_EXEC_FENCE_ARRAY:
*getparam->value = 0;
return 0;
case I915_PARAM_CMD_PARSER_VERSION:
*getparam->value = 0;
return 0;
case I915_PARAM_REVISION:
*getparam->value = 0;
return 0;
case I915_PARAM_SUBSLICE_TOTAL:
*getparam->value = 3;
return 0;
case I915_PARAM_EU_TOTAL:
*getparam->value = 24;
return 0;
case I915_PARAM_MMAP_VERSION:
*getparam->value = 1;
return 0;
case I915_PARAM_MMAP_GTT_VERSION:
*getparam->value = 2;
return 0;
default:
trace(TRACE_WARN, "unhandled getparam %d\n",
getparam->param);
errno = EINVAL;
return -1;
}
}
static void
tile_xmajor(struct stub_bo *bo, void *shadow)
{
int stride = bo->stride & ~3u;
int tile_stride = stride / 512;
ksim_assert((stride & 511) == 0);
/* We don't know the actual height of the buffer. Round down
* to a multiple of tile height so we get an integer number of
* tiles. The buffer size often gets rounded up to a power of
* two, but the buffer has to be a complete rectangulare grid
* of tiles.
*/
int height = bo->size / stride & ~7;
for (int y = 0; y < height; y++) {
int tile_y = y / 8;
int iy = y & 7;
void *src = shadow + y * stride;
void *dst = bo->map + tile_y * tile_stride * 4096 + iy * 512;
for (int x = 0; x < tile_stride; x++) {
for (int c = 0; c < 512; c += 32) {
__m256i m = _mm256_load_si256(src + x * 512 + c);
_mm256_store_si256(dst + x * 4096 + c, m);
}
}
}
}
static void
tile_ymajor(struct stub_bo *bo, void *shadow)
{
int stride = bo->stride & ~3u;
int tile_stride = stride / 128;
const int column_stride = 32 * 16;
int columns = stride / 16;
/* Same comment as for height above in tile_xmajor(). */
int height = (bo->size / stride) & ~31;
ksim_assert((stride & 127) == 0);
for (int y = 0; y < height; y += 2) {
int tile_y = y / 32;
int iy = y & 31;
void *src = shadow + y * stride;
void *dst = bo->map + tile_y * tile_stride * 4096 + iy * 16;
for (int x = 0; x < columns; x++) {
__m128i lo = _mm_load_si128((src + x * 16));
__m128i hi = _mm_load_si128((src + x * 16 + stride));
__m256i p = _mm256_inserti128_si256(_mm256_castsi128_si256(lo), hi, 1);
_mm256_store_si256((dst + x * column_stride), p);
}
}
}
static void
flush_gtt_map(struct gtt_map *m)
{
switch (m->bo->stride & 3) {
case I915_TILING_X:
tile_xmajor(m->bo, m->virtual);
break;
case I915_TILING_Y:
tile_ymajor(m->bo, m->virtual);
break;
default:
ksim_unreachable();
}
mprotect(m->virtual, m->length, m->prot & ~PROT_WRITE);
trace(TRACE_GEM, "remapping bo %d as read-only\n", get_handle(m->bo));
}
static void
flush_dirty_maps(void)
{
struct gtt_map *m;
list_for_each_entry(m, &dirty_map_list, link)
flush_gtt_map(m);
list_insert_list(clean_map_list.prev, &dirty_map_list);
list_init(&dirty_map_list);
}
static int
dispatch_execbuffer2(int fd, unsigned long request,
struct drm_i915_gem_execbuffer2 *execbuffer2)
{
struct drm_i915_gem_exec_object2 *buffers =
(void *) (uintptr_t) execbuffer2->buffers_ptr;
const uint32_t count = execbuffer2->buffer_count;
trace(TRACE_GEM, "DRM_IOCTL_I915_GEM_EXECBUFFER2:\n");
ksim_assert(count > 0);
ksim_assert((execbuffer2->batch_len & 7) == 0);
ksim_assert(execbuffer2->num_cliprects == 0);
ksim_assert(execbuffer2->DR1 == 0);
ksim_assert(execbuffer2->DR4 == 0);
flush_dirty_maps();
bool all_matches = true, all_bound = true;
for (uint32_t i = 0; i < count; i++) {
struct stub_bo *bo = get_bo(buffers[i].handle);
/* Userspace can use an invalid BOs to check for
* supported features (it is assumed that the kernel
* will return an error if a flag is unsupported,
* meaning it will fail before checking that the BO
* used doesn't exist) :
*
* https://cgit.freedesktop.org/mesa/mesa/tree/src/intel/vulkan/anv_gem.c#n338
*
* The following implementation will report unknown
* BOs, meaning we make ksim support any feature.
*/
if (bo == NULL) {
errno = ENOENT;
return -1;
}
trace(TRACE_GEM, " bo %d, size %ld, ",
buffers[i].handle, bo->size);
if (buffers[i].flags & EXEC_OBJECT_PINNED) {
bo->gtt_offset = buffers[i].offset;
bind_bo(bo, bo->gtt_offset);
trace(TRACE_GEM, "pinning to %08x\n", bo->gtt_offset);
} else if (bo->gtt_offset == NOT_BOUND &&
next_offset + bo->size <= gtt_size) {
uint64_t alignment = max_u64(buffers[i].alignment, 4096);
bo->gtt_offset = align_u64(next_offset, alignment);
next_offset = bo->gtt_offset + bo->size;
bind_bo(bo, bo->gtt_offset);
trace(TRACE_GEM, "binding to %08x\n", bo->gtt_offset);
} else {
trace(TRACE_GEM, "keeping at %08x\n", bo->gtt_offset);
}
if (bo->gtt_offset == NOT_BOUND)
all_bound = false;
if (bo->gtt_offset != buffers[i].offset)
all_matches = false;
}
if (!all_bound) {
/* FIXME: Evict all and try again */
ksim_assert(all_bound);
}
if (all_matches && (execbuffer2->flags & I915_EXEC_NO_RELOC))
/* can skip relocs */;
for (uint32_t i = 0; i < count; i++) {
struct stub_bo *bo = get_bo(buffers[i].handle);
struct drm_i915_gem_relocation_entry *relocs =
(void *) (uintptr_t) buffers[i].relocs_ptr;
for (uint32_t j = 0; j < buffers[i].relocation_count; j++) {
uint32_t handle;
struct stub_bo *target;
uint64_t *dst;
if (execbuffer2->flags & I915_EXEC_HANDLE_LUT) {
ksim_assert(relocs[j].target_handle <
execbuffer2->buffer_count);
handle = buffers[relocs[j].target_handle].handle;
} else {
handle = relocs[j].target_handle;
}
target = get_bo(handle);
ksim_assert(target != NULL);
ksim_assert(relocs[j].offset + sizeof(*dst) <= bo->size);
dst = bo->map + relocs[j].offset;
if (relocs[j].presumed_offset != target->gtt_offset)
*dst = target->gtt_offset + relocs[j].delta;
}
}
uint32_t ring = execbuffer2->flags & I915_EXEC_RING_MASK;
switch (ring) {
case I915_EXEC_RENDER:
case I915_EXEC_BLT:
break;
default:
ksim_unreachable("unhandled ring");
}
struct stub_bo *bo = get_bo(buffers[count - 1].handle);
ksim_assert(bo != NULL);
uint64_t offset = bo->gtt_offset + execbuffer2->batch_start_offset;
start_batch_buffer(offset, ring);
return 0;
}
static int
dispatch_throttle(int fd, unsigned long request, void *p)
{
trace(TRACE_GEM, "DRM_IOCTL_I915_GEM_THROTTLE\n");
return 0;
}
static int
dispatch_create(int fd, unsigned long request,
struct drm_i915_gem_create *create)
{
struct stub_bo *bo = create_bo(create->size);
bo->offset = alloc_range(create->size);
create->handle = get_handle(bo);
bo->map = mmap(NULL, bo->size, PROT_READ | PROT_WRITE,
MAP_SHARED, memfd, bo->offset);
trace(TRACE_GEM, "DRM_IOCTL_I915_GEM_CREATE: "
"new bo %d, size %ld\n", create->handle, bo->size);
return 0;
}
static int
dispatch_pread(int fd, unsigned long request,
struct drm_i915_gem_pread *gem_pread)
{
struct stub_bo *bo = get_bo(gem_pread->handle);
if (bo == NULL) {
errno = ENOENT;
return -1;
}
trace(TRACE_GEM, "DRM_IOCTL_I915_GEM_PREAD\n");
/* Check for integer overflow */
ksim_assert(gem_pread->offset + gem_pread->size > gem_pread->offset);
ksim_assert(gem_pread->offset + gem_pread->size <= bo->size);
return pread(memfd, (void *) (uintptr_t) gem_pread->data_ptr,
gem_pread->size, bo->offset + gem_pread->offset);
}
static int
dispatch_pwrite(int fd, unsigned long request,
struct drm_i915_gem_pwrite *gem_pwrite)
{
struct stub_bo *bo = get_bo(gem_pwrite->handle);
if (bo == NULL) {
errno = ENOENT;
return -1;
}
trace(TRACE_GEM, "DRM_IOCTL_I915_GEM_PWRITE: "
"bo %d, offset %d, size %d, bo size %ld\n",
gem_pwrite->handle,
gem_pwrite->offset, gem_pwrite->size, bo->size);
ksim_assert(gem_pwrite->offset + gem_pwrite->size > gem_pwrite->offset);
ksim_assert(gem_pwrite->offset + gem_pwrite->size <= bo->size);
return pwrite(memfd, (void *) (uintptr_t) gem_pwrite->data_ptr,
gem_pwrite->size, bo->offset + gem_pwrite->offset);
}
static int
dispatch_mmap(int fd, unsigned long request,
struct drm_i915_gem_mmap *gem_mmap)
{
struct stub_bo *bo = get_bo(gem_mmap->handle);
void *p;
trace(TRACE_GEM, "DRM_IOCTL_I915_GEM_MMAP\n");
if (bo == NULL) {
errno = ENOENT;
return -1;
}
ksim_assert(bo->offset != STUB_BO_USERPTR);
if (bo->offset == STUB_BO_PRIME) {
ksim_assert(bo->kernel_handle);
return libc_ioctl(fd, request, gem_mmap);
}
ksim_assert(gem_mmap->flags == 0);
ksim_assert(gem_mmap->offset + gem_mmap->size > gem_mmap->offset);
ksim_assert(gem_mmap->offset + gem_mmap->size <= bo->size);
p = mmap(NULL, gem_mmap->size, PROT_READ | PROT_WRITE,
MAP_SHARED, memfd, bo->offset + gem_mmap->offset);
gem_mmap->addr_ptr = (uint64_t) p;
return p != MAP_FAILED ? 0 : -1;
}
static int
dispatch_mmap_gtt(int fd, unsigned long request,
struct drm_i915_gem_mmap_gtt *map_gtt)
{
struct stub_bo *bo = get_bo(map_gtt->handle);
trace(TRACE_GEM, "DRM_IOCTL_I915_GEM_MMAP_GTT: handle %d\n",
map_gtt->handle);
if (bo == NULL) {
errno = ENOENT;
return -1;
}
uint32_t tiling = bo->stride & 3;
uint32_t stride = bo->stride & ~3u;
if (tiling != I915_TILING_NONE) {
ksim_assert((stride & 127) == 0);
struct gtt_map *m = malloc(sizeof(*m));
if (m == NULL)
return -1;
m->offset = alloc_range(bo->size);
m->bo = bo;
list_insert(pending_map_list.prev, &m->link);
map_gtt->offset = m->offset;
} else {
map_gtt->offset = bo->offset;
}
return 0;
}
static int
dispatch_set_domain(int fd, unsigned long request,
struct drm_i915_gem_set_domain *set_domain)
{
trace(TRACE_GEM, "DRM_IOCTL_I915_GEM_SET_DOMAIN\n");
return 0;
}
static int
dispatch_set_tiling(int fd, unsigned long request,
struct drm_i915_gem_set_tiling *set_tiling)
{
struct stub_bo *bo = get_bo(set_tiling->handle);
if (bo == NULL) {
errno = ENOENT;
return -1;
}
ksim_assert((set_tiling->stride & 3u) == 0);
ksim_assert((set_tiling->tiling_mode & ~3u) == 0);
bo->stride = set_tiling->stride | set_tiling->tiling_mode;
if (bo->kernel_handle)
set_kernel_tiling(bo);
trace(TRACE_GEM, "DRM_IOCTL_I915_GEM_SET_TILING: bo %d, mode %d, stride %d\n",
set_tiling->handle, set_tiling->tiling_mode, set_tiling->stride);
return 0;
}
static int
dispatch_get_tiling(int fd, unsigned long request,
struct drm_i915_gem_get_tiling *get_tiling)
{
struct stub_bo *bo = get_bo(get_tiling->handle);
if (bo == NULL) {
errno = ENOENT;
return -1;
}
get_tiling->tiling_mode = bo->stride & 3u;
get_tiling->swizzle_mode = 0;
get_tiling->phys_swizzle_mode = 0;
trace(TRACE_GEM, "DRM_IOCTL_I915_GEM_GET_TILING\n");
return 0;
}
static int
dispatch_userptr(int fd, unsigned long request,
struct drm_i915_gem_userptr *userptr)
{
struct stub_bo *bo = create_bo(userptr->user_size);
int ret;
ret = libc_ioctl(fd, request, userptr);
if (ret == -1)
return ret;
bo->offset = STUB_BO_USERPTR;
bo->map = (void *) (uintptr_t) userptr->user_ptr;
bo->kernel_handle = userptr->handle;
userptr->handle = get_handle(bo);
trace(TRACE_GEM, "DRM_IOCTL_I915_GEM_USERPTR size=%llu -> handle=%u\n",
userptr->user_size, userptr->handle);
return 0;
}
static int
dispatch_context_getparam(int fd, unsigned long request,
struct drm_i915_gem_context_param *param)
{
switch (param->param) {
case I915_CONTEXT_PARAM_GTT_SIZE:
param->value = 4245561344;
return 0;
default:
return -EINVAL;
}
}
static int
dispatch_context_setparam(int fd, unsigned long request,
struct drm_i915_gem_userptr *userptr)
{
return 0;
}
static int
dispatch_close(int fd, unsigned long request,
struct drm_gem_close *gem_close)
{
struct stub_bo *bo = get_bo(gem_close->handle);
trace(TRACE_GEM, "DRM_IOCTL_GEM_CLOSE\n");
if (bo == NULL) {
errno = ENOENT;
return -1;
}
close_bo(bo);
return 0;
}
static int
dispatch_prime_fd_to_handle(int fd, unsigned long request,
struct drm_prime_handle *prime)
{
struct stub_bo *bo;
int ret, size;
size = lseek(fd, 0, SEEK_END);
bo = create_bo(size);
ret = libc_ioctl(fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, prime);
if (ret == -1)
return -1;
bo->offset = STUB_BO_PRIME;
bo->kernel_handle = prime->handle;
trace(TRACE_GEM, "DRM_IOCTL_PRIME_FD_TO_HANDLE size=%llu -> handle=%u\n",
bo->size, get_handle(bo));
return 0;
}
static int
dispatch_version(int fd, unsigned long request,
struct drm_version *version)
{
static const char name[] = "i915";
static const char date[] = "20160919";
static const char desc[] = "Intel Graphics";
version->version_major = 1;
version->version_minor = 6;
version->version_patchlevel = 0;
strncpy(version->name, name, version->name_len);
version->name_len = strlen(name);
strncpy(version->date, date, version->date_len);
version->date_len = strlen(date);
strncpy(version->desc, desc, version->desc_len);
version->desc_len = strlen(desc);
return 0;
}
static int
dispatch_prime_handle_to_fd(int fd, unsigned long request,
struct drm_prime_handle *prime)
{
struct stub_bo *bo = get_bo(prime->handle);
struct drm_prime_handle p;
int ret;
if (bo == NULL) {
errno = ENOENT;
return -1;
}
p.handle = get_kernel_handle(bo);
p.flags = prime->flags;
ret = libc_ioctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &p);
prime->fd = p.fd;
trace(TRACE_GEM, "DRM_IOCTL_PRIME_HANDLE_TO_FD: "
"handle %d -> fd %d\n", prime->handle, p.fd);
return ret;
}
__attribute__ ((visibility ("default"))) int
ioctl(int fd, unsigned long request, ...)
{
va_list args;
void *argp;
struct stat buf;
va_start(args, request);
argp = va_arg(args, void *);
va_end(args);
if (_IOC_TYPE(request) == DRM_IOCTL_BASE &&
drm_fd != fd && fstat(fd, &buf) == 0 &&
(buf.st_mode & S_IFMT) == S_IFCHR && major(buf.st_rdev) == DRM_MAJOR) {
drm_fd = fd;
trace(TRACE_GEM, "intercept drm ioctl on fd %d\n", fd);
}
if (fd != drm_fd)
return libc_ioctl(fd, request, argp);
switch (request) {
case DRM_IOCTL_I915_GETPARAM:
return dispatch_getparam(fd, request, argp);
case DRM_IOCTL_I915_SETPARAM: {
struct drm_i915_setparam *setparam = argp;
trace(TRACE_GEM, "DRM_IOCTL_I915_SETPARAM: param %d, value %d\n",
setparam->param, setparam->value);
return 0;
}
case DRM_IOCTL_I915_GEM_EXECBUFFER:
trace(TRACE_GEM, "DRM_IOCTL_I915_GEM_EXECBUFFER: unhandled\n");
return -1;
case DRM_IOCTL_I915_GEM_EXECBUFFER2:
case DRM_IOCTL_I915_GEM_EXECBUFFER2_WR:
return dispatch_execbuffer2(fd, request, argp);
case DRM_IOCTL_I915_GEM_BUSY:
trace(TRACE_GEM, "DRM_IOCTL_I915_GEM_BUSY\n");
return 0;
case DRM_IOCTL_I915_GEM_SET_CACHING:
trace(TRACE_GEM, "DRM_IOCTL_I915_GEM_SET_CACHING\n");
return 0;
case DRM_IOCTL_I915_GEM_GET_CACHING:
trace(TRACE_GEM, "DRM_IOCTL_I915_GEM_GET_CACHING\n");
return 0;
case DRM_IOCTL_I915_GEM_THROTTLE:
return dispatch_throttle(fd, request, argp);
case DRM_IOCTL_I915_GEM_CREATE:
return dispatch_create(fd, request, argp);
case DRM_IOCTL_I915_GEM_PREAD:
return dispatch_pread(fd, request, argp);
case DRM_IOCTL_I915_GEM_PWRITE:
return dispatch_pwrite(fd, request, argp);
case DRM_IOCTL_I915_GEM_MMAP:
return dispatch_mmap(fd, request, argp);
case DRM_IOCTL_I915_GEM_MMAP_GTT:
return dispatch_mmap_gtt(fd, request, argp);
case DRM_IOCTL_I915_GEM_SET_DOMAIN:
return dispatch_set_domain(fd, request, argp);
case DRM_IOCTL_I915_GEM_SW_FINISH:
trace(TRACE_GEM, "DRM_IOCTL_I915_GEM_SW_FINISH\n");
return 0;
case DRM_IOCTL_I915_GEM_SET_TILING:
return dispatch_set_tiling(fd, request, argp);
case DRM_IOCTL_I915_GEM_GET_TILING:
return dispatch_get_tiling(fd, request, argp);
case DRM_IOCTL_I915_GEM_GET_APERTURE: {
struct drm_i915_gem_get_aperture *get_aperture = argp;
trace(TRACE_GEM, "DRM_IOCTL_I915_GEM_GET_APERTURE\n");
get_aperture->aper_available_size = 4245561344; /* bdw gt3 */
return 0;
}
case DRM_IOCTL_I915_GEM_MADVISE:
trace(TRACE_GEM, "DRM_IOCTL_I915_GEM_MADVISE\n");
return 0;
case DRM_IOCTL_I915_GEM_WAIT:
trace(TRACE_GEM, "DRM_IOCTL_I915_GEM_WAIT\n");
return 0;
case DRM_IOCTL_I915_GEM_CONTEXT_CREATE: {
struct drm_i915_gem_context_create *gem_context_create = argp;
gem_context_create->ctx_id = 1;
return 0;
}
case DRM_IOCTL_I915_GEM_CONTEXT_DESTROY:
trace(TRACE_GEM, "DRM_IOCTL_I915_GEM_CONTEXT_DESTROY\n");
return 0;
case DRM_IOCTL_I915_REG_READ:
trace(TRACE_GEM, "DRM_IOCTL_I915_REG_READ\n");
return 0;
case DRM_IOCTL_I915_GET_RESET_STATS: