-
Notifications
You must be signed in to change notification settings - Fork 351
/
Copy pathmos_bufmgr.c
5800 lines (4967 loc) · 178 KB
/
mos_bufmgr.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 © 2007 Red Hat Inc.
* Copyright © 2007-2022 Intel Corporation
* Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., USA
* All Rights Reserved.
*
* 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, sub license, 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 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 NON-INFRINGEMENT. IN NO EVENT SHALL
* THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS 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.
*
* The above copyright notice and this permission notice (including the
* next paragraph) shall be included in all copies or substantial portions
* of the Software.
*
*
**************************************************************************/
/*
* Authors: Thomas Hellström <thomas-at-tungstengraphics-dot-com>
* Keith Whitwell <keithw-at-tungstengraphics-dot-com>
* Eric Anholt <eric@anholt.net>
* Dave Airlie <airlied@linux.ie>
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "xf86drm.h"
#include "xf86atomic.h"
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include <pthread.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdbool.h>
#include "errno.h"
#ifndef ETIME
#define ETIME ETIMEDOUT
#endif
#include "libdrm_macros.h"
#include "libdrm_lists.h"
#include "mos_bufmgr.h"
#include "mos_bufmgr_priv.h"
#ifdef ENABLE_XE_KMD
#include "mos_bufmgr_xe.h"
#endif
#include "string.h"
#include "i915_drm.h"
#include "mos_vma.h"
#include "mos_util_debug.h"
#include "mos_oca_defs_specific.h"
#include "intel_hwconfig_types.h"
#include "mos_utilities.h"
#include "linux_system_info.h"
#include "mos_os_specific.h"
#ifdef HAVE_VALGRIND
#include <valgrind.h>
#include <memcheck.h>
#define VG(x) x
#else
#define VG(x)
#endif
#define memclear(s) memset(&s, 0, sizeof(s))
#define MOS_DBG(...) do { \
if (bufmgr_gem != nullptr && bufmgr_gem->bufmgr.debug) \
fprintf(stderr, __VA_ARGS__); \
} while (0)
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#define MAX2(A, B) ((A) > (B) ? (A) : (B))
/**
* upper_32_bits - return bits 32-63 of a number
* @n: the number we're accessing
*
* A basic shift-right of a 64- or 32-bit quantity. Use this to suppress
* the "right shift count >= width of type" warning when that quantity is
* 32-bits.
*/
#define upper_32_bits(n) ((__u32)(((n) >> 16) >> 16))
/**
* lower_32_bits - return bits 0-31 of a number
* @n: the number we're accessing
*/
#define lower_32_bits(n) ((__u32)(n))
#define PCI_CHIP_I915_G 0x2582
#define PCI_CHIP_E7221_G 0x258A
#define PCI_CHIP_I915_GM 0x2592
#define IS_915(devid) ((devid) == PCI_CHIP_I915_G || \
(devid) == PCI_CHIP_E7221_G || \
(devid) == PCI_CHIP_I915_GM)
#define INITIAL_SOFTPIN_TARGET_COUNT 1024
struct mos_gem_bo_bucket {
drmMMListHead head;
unsigned long size;
};
struct mos_bufmgr_gem {
struct mos_bufmgr bufmgr;
atomic_t refcount;
int fd;
int max_relocs;
pthread_mutex_t lock;
struct drm_i915_gem_exec_object *exec_objects;
struct drm_i915_gem_exec_object2 *exec2_objects;
struct mos_linux_bo **exec_bos;
int exec_size;
int exec_count;
struct mos_exec_fences exec_fences;
/** Array of lists of cached gem objects of power-of-two sizes */
struct mos_gem_bo_bucket cache_bucket[64];
int num_buckets;
time_t time;
drmMMListHead managers;
drmMMListHead named;
uint64_t gtt_size;
int available_fences;
int pci_device;
unsigned int has_bsd : 1;
unsigned int has_bsd2 : 1;
unsigned int has_blt : 1;
unsigned int has_relaxed_fencing : 1;
unsigned int has_llc : 1;
unsigned int has_wait_timeout : 1;
unsigned int bo_reuse : 1;
unsigned int no_exec : 1;
unsigned int has_vebox : 1;
unsigned int has_ext_mmap : 1;
unsigned int has_fence_reg : 1;
unsigned int has_lmem : 1;
unsigned int has_mmap_offset : 1;
bool fenced_relocs;
struct {
void *ptr;
uint32_t handle;
} userptr_active;
// manage address for softpin buffer object
mos_vma_heap vma_heap[MEMZONE_COUNT];
bool use_softpin;
bool softpin_va1Malign;
bool object_capture_disabled;
#define MEM_PROFILER_BUFFER_SIZE 256
char mem_profiler_buffer[MEM_PROFILER_BUFFER_SIZE];
char* mem_profiler_path;
int mem_profiler_fd;
int device_type;
uint32_t ts_freq;
} mos_bufmgr_gem;
#define DRM_INTEL_RELOC_FENCE (1<<0)
struct mos_reloc_target {
struct mos_linux_bo *bo;
int flags;
};
struct mos_softpin_target {
struct mos_linux_bo *bo;
int flags;
};
struct mos_bo_gem {
struct mos_linux_bo bo;
atomic_t refcount;
uint32_t gem_handle;
const char *name;
/**
* Kenel-assigned global name for this object
*
* List contains both flink named and prime fd'd objects
*/
unsigned int global_name;
drmMMListHead name_list;
/**
* Index of the buffer within the validation list while preparing a
* batchbuffer execution.
*/
int validate_index;
/**
* Current tiling mode
*/
uint32_t tiling_mode;
uint32_t swizzle_mode;
unsigned long stride;
time_t free_time;
/** Array passed to the DRM containing relocation information. */
struct drm_i915_gem_relocation_entry *relocs;
/**
* Array of info structs corresponding to relocs[i].target_handle etc
*/
struct mos_reloc_target *reloc_target_info;
/** Number of entries in relocs */
int reloc_count;
/** Array of BOs that are referenced by this buffer and will be softpinned */
struct mos_softpin_target *softpin_target;
/** Number softpinned BOs that are referenced by this buffer */
int softpin_target_count;
/** Maximum amount of softpinned BOs that are referenced by this buffer */
int max_softpin_target_count;
/** Mapped address for the buffer, saved across map/unmap cycles */
void *mem_virtual;
/** Uncached Mapped address for the buffer, saved across map/unmap cycles */
void *mem_wc_virtual;
/** GTT virtual address for the buffer, saved across map/unmap cycles */
void *gtt_virtual;
/**
* Virtual address of the buffer allocated by user, used for userptr
* objects only.
*/
void *user_virtual;
int map_count;
/** BO cache list */
drmMMListHead head;
/**
* Boolean of whether this BO and its children have been included in
* the current drm_intel_bufmgr_check_aperture_space() total.
*/
bool included_in_check_aperture;
/**
* Boolean of whether this buffer has been used as a relocation
* target and had its size accounted for, and thus can't have any
* further relocations added to it.
*/
bool used_as_reloc_target;
/**
* Boolean of whether we have encountered an error whilst building the relocation tree.
*/
bool has_error;
/**
* Boolean of whether this buffer can be re-used
*/
bool reusable;
/**
* Boolean of whether the GPU is definitely not accessing the buffer.
*
* This is only valid when reusable, since non-reusable
* buffers are those that have been shared wth other
* processes, so we don't know their state.
*/
bool idle;
/**
* Boolean of whether this buffer was allocated with userptr
*/
bool is_userptr;
/**
* Boolean of whether this buffer can be placed in the full 48-bit
* address range on gen8+.
*
* By default, buffers will be keep in a 32-bit range, unless this
* flag is explicitly set.
*/
bool use_48b_address_range;
/**
* Whether this buffer is softpinned at offset specified by the user
*/
bool is_softpin;
/*
* Whether to remove the dependency of this bo in exebuf.
*/
bool exec_async;
/*
* Whether to remove the dependency of this bo in exebuf.
*/
bool exec_capture;
/**
* Size in bytes of this buffer and its relocation descendents.
*
* Used to avoid costly tree walking in
* drm_intel_bufmgr_check_aperture in the common case.
*/
int reloc_tree_size;
/**
* Number of potential fence registers required by this buffer and its
* relocations.
*/
int reloc_tree_fences;
/** Flags that we may need to do the SW_FINSIH ioctl on unmap. */
bool mapped_cpu_write;
/**
* Size to pad the object to.
*
*/
uint64_t pad_to_size;
/**
* Memory Type on created the surfaces for local/system memory
*/
int mem_region;
/**
* PAT Index
*/
unsigned int pat_index;
/**
* Is cpu cacheable
*/
bool cpu_cacheable;
};
struct mos_exec_info {
/* save all exec2_objects for res*/
struct drm_i915_gem_exec_object2* obj;
/* save batch buffer*/
struct drm_i915_gem_exec_object2* batch_obj;
/* save previous ptr to void mem leak when free*/
struct drm_i915_gem_exec_object2* pSavePreviousExec2Objects;
/*bo resource count*/
uint32_t obj_count;
/*batch buffer bo count*/
uint32_t batch_count;
/*remain size of 'obj'*/
uint32_t obj_remain_size;
#define OBJ512_SIZE 512
};
static unsigned int
mos_gem_estimate_batch_space(struct mos_linux_bo ** bo_array, int count);
static unsigned int
mos_gem_compute_batch_space(struct mos_linux_bo ** bo_array, int count);
static int
mos_gem_bo_get_tiling(struct mos_linux_bo *bo, uint32_t * tiling_mode,
uint32_t * swizzle_mode);
static int
mos_gem_bo_check_mem_region_internal(struct mos_linux_bo *bo,
int mem_type);
static int
mos_gem_bo_set_tiling_internal(struct mos_linux_bo *bo,
uint32_t tiling_mode,
uint32_t stride);
static void mos_gem_bo_unreference_locked_timed(struct mos_linux_bo *bo,
time_t time);
static void mos_gem_bo_unreference(struct mos_linux_bo *bo);
static bool mos_gem_bo_is_softpin(struct mos_linux_bo *bo);
static void mos_gem_bo_start_gtt_access(struct mos_linux_bo *bo, int write_enable);
static void mos_gem_bo_free(struct mos_linux_bo *bo);
static int mos_bufmgr_get_driver_info(struct mos_bufmgr *bufmgr, struct LinuxDriverInfo *drvInfo);
static inline struct mos_bo_gem *to_bo_gem(struct mos_linux_bo *bo)
{
return (struct mos_bo_gem *)bo;
}
static unsigned long
mos_gem_bo_tile_size(struct mos_bufmgr_gem *bufmgr_gem, unsigned long size,
uint32_t *tiling_mode)
{
unsigned long min_size, max_size;
unsigned long i;
if (*tiling_mode == I915_TILING_NONE)
return size;
/* 965+ just need multiples of page size for tiling */
return ROUND_UP_TO(size, 4096);
}
/*
* Round a given pitch up to the minimum required for X tiling on a
* given chip. We use 512 as the minimum to allow for a later tiling
* change.
*/
static unsigned long
mos_gem_bo_tile_pitch(struct mos_bufmgr_gem *bufmgr_gem,
unsigned long pitch, uint32_t *tiling_mode)
{
unsigned long tile_width;
unsigned long i;
/* If untiled, then just align it so that we can do rendering
* to it with the 3D engine.
*/
if (*tiling_mode == I915_TILING_NONE)
return ALIGN(pitch, 64);
if (*tiling_mode == I915_TILING_X
|| (IS_915(bufmgr_gem->pci_device)
&& *tiling_mode == I915_TILING_Y))
tile_width = 512;
else
tile_width = 128;
/* 965 is flexible */
return ROUND_UP_TO(pitch, tile_width);
}
static struct mos_gem_bo_bucket *
mos_gem_bo_bucket_for_size(struct mos_bufmgr_gem *bufmgr_gem,
unsigned long size)
{
int i;
for (i = 0; i < bufmgr_gem->num_buckets; i++) {
struct mos_gem_bo_bucket *bucket =
&bufmgr_gem->cache_bucket[i];
if (bucket->size >= size) {
return bucket;
}
}
return nullptr;
}
static void
mos_gem_dump_validation_list(struct mos_bufmgr_gem *bufmgr_gem)
{
int i, j;
for (i = 0; i < bufmgr_gem->exec_count; i++) {
struct mos_linux_bo *bo = bufmgr_gem->exec_bos[i];
struct mos_bo_gem *bo_gem = (struct mos_bo_gem *) bo;
if (bo_gem->relocs == nullptr || bo_gem->softpin_target == nullptr) {
MOS_DBG("%2d: %d %s(%s)\n", i, bo_gem->gem_handle,
bo_gem->is_softpin ? "*" : "",
bo_gem->name);
continue;
}
for (j = 0; j < bo_gem->reloc_count; j++) {
struct mos_linux_bo *target_bo = bo_gem->reloc_target_info[j].bo;
struct mos_bo_gem *target_gem =
(struct mos_bo_gem *) target_bo;
MOS_DBG("%2d: %d %s(%s)@0x%08x %08x -> "
"%d (%s)@0x%08x %08x + 0x%08x\n",
i,
bo_gem->gem_handle,
bo_gem->is_softpin ? "*" : "",
bo_gem->name,
upper_32_bits(bo_gem->relocs[j].offset),
lower_32_bits(bo_gem->relocs[j].offset),
target_gem->gem_handle,
target_gem->name,
upper_32_bits(target_bo->offset64),
lower_32_bits(target_bo->offset64),
bo_gem->relocs[j].delta);
}
for (j = 0; j < bo_gem->softpin_target_count; j++) {
struct mos_linux_bo *target_bo = bo_gem->softpin_target[j].bo;
struct mos_bo_gem *target_gem =
(struct mos_bo_gem *) target_bo;
MOS_DBG("%2d: %d %s(%s) -> "
"%d *(%s)@0x%08x %08x\n",
i,
bo_gem->gem_handle,
bo_gem->is_softpin ? "*" : "",
bo_gem->name,
target_gem->gem_handle,
target_gem->name,
upper_32_bits(target_bo->offset64),
lower_32_bits(target_bo->offset64));
}
}
}
static inline void
mos_gem_bo_reference(struct mos_linux_bo *bo)
{
struct mos_bo_gem *bo_gem = (struct mos_bo_gem *) bo;
atomic_inc(&bo_gem->refcount);
}
/**
* Adds the given buffer to the list of buffers to be validated (moved into the
* appropriate memory type) with the next batch submission.
*
* If a buffer is validated multiple times in a batch submission, it ends up
* with the intersection of the memory type flags and the union of the
* access flags.
*/
static void
mos_add_validate_buffer(struct mos_linux_bo *bo)
{
struct mos_bufmgr_gem *bufmgr_gem = (struct mos_bufmgr_gem *) bo->bufmgr;
struct mos_bo_gem *bo_gem = (struct mos_bo_gem *) bo;
int index;
struct drm_i915_gem_exec_object *exec_objects;
struct mos_linux_bo **exec_bos;
if (bo_gem->validate_index != -1)
return;
/* Extend the array of validation entries as necessary. */
if (bufmgr_gem->exec_count == bufmgr_gem->exec_size) {
int new_size = bufmgr_gem->exec_size * 2;
if (new_size == 0)
new_size = ARRAY_INIT_SIZE;
exec_objects = (struct drm_i915_gem_exec_object *)realloc(bufmgr_gem->exec_objects,
sizeof(*bufmgr_gem->exec_objects) * new_size);
if (!exec_objects)
return;
bufmgr_gem->exec_objects = exec_objects;
exec_bos = (struct mos_linux_bo **)realloc(bufmgr_gem->exec_bos,
sizeof(*bufmgr_gem->exec_bos) * new_size);
if (!exec_bos)
return;
bufmgr_gem->exec_bos = exec_bos;
bufmgr_gem->exec_size = new_size;
}
index = bufmgr_gem->exec_count;
bo_gem->validate_index = index;
/* Fill in array entry */
bufmgr_gem->exec_objects[index].handle = bo_gem->gem_handle;
bufmgr_gem->exec_objects[index].relocation_count = bo_gem->reloc_count;
bufmgr_gem->exec_objects[index].relocs_ptr = (uintptr_t) bo_gem->relocs;
bufmgr_gem->exec_objects[index].alignment = bo->align;
bufmgr_gem->exec_objects[index].offset = 0;
bufmgr_gem->exec_bos[index] = bo;
bufmgr_gem->exec_count++;
}
static void
mos_add_validate_buffer2(struct mos_linux_bo *bo, int need_fence)
{
struct mos_bufmgr_gem *bufmgr_gem = (struct mos_bufmgr_gem *)bo->bufmgr;
struct mos_bo_gem *bo_gem = (struct mos_bo_gem *)bo;
int index;
struct drm_i915_gem_exec_object2 *exec2_objects;
struct mos_linux_bo **exec_bos;
int flags = 0;
if (need_fence)
flags |= EXEC_OBJECT_NEEDS_FENCE;
if (bo_gem->pad_to_size)
flags |= EXEC_OBJECT_PAD_TO_SIZE;
if (bo_gem->use_48b_address_range)
flags |= EXEC_OBJECT_SUPPORTS_48B_ADDRESS;
if (bo_gem->is_softpin)
flags |= EXEC_OBJECT_PINNED;
if (bo_gem->exec_async)
flags |= EXEC_OBJECT_ASYNC;
if (bo_gem->exec_capture)
flags |= EXEC_OBJECT_CAPTURE;
if (bo_gem->validate_index != -1) {
bufmgr_gem->exec2_objects[bo_gem->validate_index].flags |= flags;
return;
}
/* Extend the array of validation entries as necessary. */
if (bufmgr_gem->exec_count == bufmgr_gem->exec_size) {
int new_size = bufmgr_gem->exec_size * 2;
if (new_size == 0)
new_size = ARRAY_INIT_SIZE;
exec2_objects = (struct drm_i915_gem_exec_object2 *)
realloc(bufmgr_gem->exec2_objects,
sizeof(*bufmgr_gem->exec2_objects) * new_size);
if (!exec2_objects)
return;
bufmgr_gem->exec2_objects = exec2_objects;
exec_bos = (struct mos_linux_bo **)realloc(bufmgr_gem->exec_bos,
sizeof(*bufmgr_gem->exec_bos) * new_size);
if (!exec_bos)
return;
bufmgr_gem->exec_bos = exec_bos;
bufmgr_gem->exec_size = new_size;
}
index = bufmgr_gem->exec_count;
bo_gem->validate_index = index;
/* Fill in array entry */
bufmgr_gem->exec2_objects[index].handle = bo_gem->gem_handle;
bufmgr_gem->exec2_objects[index].relocation_count = bo_gem->reloc_count;
bufmgr_gem->exec2_objects[index].relocs_ptr = (uintptr_t)bo_gem->relocs;
bufmgr_gem->exec2_objects[index].alignment = bo->align;
bufmgr_gem->exec2_objects[index].offset = bo_gem->is_softpin ?
bo->offset64 : 0;
bufmgr_gem->exec_bos[index] = bo;
bufmgr_gem->exec2_objects[index].flags = flags;
bufmgr_gem->exec2_objects[index].rsvd1 = 0;
bufmgr_gem->exec2_objects[index].pad_to_size = bo_gem->pad_to_size;
bufmgr_gem->exec2_objects[index].rsvd2 = 0;
bufmgr_gem->exec_count++;
}
static void
mos_add_reloc_objects(struct mos_reloc_target reloc_target)
{
struct mos_bufmgr_gem *bufmgr_gem = (struct mos_bufmgr_gem *)reloc_target.bo->bufmgr;
struct mos_bo_gem *bo_gem = (struct mos_bo_gem *)reloc_target.bo;
int index;
struct drm_i915_gem_exec_object2 *exec2_objects;
struct mos_linux_bo **exec_bos;
if (bo_gem->validate_index != -1) {
bufmgr_gem->exec2_objects[bo_gem->validate_index].flags |= reloc_target.flags;
return;
}
/* Extend the array of validation entries as necessary. */
if (bufmgr_gem->exec_count == bufmgr_gem->exec_size) {
int new_size = bufmgr_gem->exec_size * 2;
if (new_size == 0)
new_size = ARRAY_INIT_SIZE;
exec2_objects = (struct drm_i915_gem_exec_object2 *)
realloc(bufmgr_gem->exec2_objects,
sizeof(*bufmgr_gem->exec2_objects) * new_size);
if (!exec2_objects)
{
MOS_DBG("realloc exec2_objects failed!\n");
return;
}
bufmgr_gem->exec2_objects = exec2_objects;
exec_bos = (struct mos_linux_bo **)realloc(bufmgr_gem->exec_bos,
sizeof(*bufmgr_gem->exec_bos) * new_size);
if (!exec_bos)
{
MOS_DBG("realloc exec_bo failed!\n");
return;
}
bufmgr_gem->exec_bos = exec_bos;
bufmgr_gem->exec_size = new_size;
}
index = bufmgr_gem->exec_count;
bo_gem->validate_index = index;
/* Fill in array entry */
bufmgr_gem->exec2_objects[index].handle = bo_gem->gem_handle;
bufmgr_gem->exec2_objects[index].relocation_count = bo_gem->reloc_count;
bufmgr_gem->exec2_objects[index].relocs_ptr = (uintptr_t)bo_gem->relocs;
bufmgr_gem->exec2_objects[index].alignment = reloc_target.bo->align;
bufmgr_gem->exec2_objects[index].offset = 0;
bufmgr_gem->exec_bos[index] = reloc_target.bo;
bufmgr_gem->exec2_objects[index].flags = reloc_target.flags;
bufmgr_gem->exec2_objects[index].rsvd1 = 0;
bufmgr_gem->exec2_objects[index].pad_to_size = bo_gem->pad_to_size;
bufmgr_gem->exec2_objects[index].rsvd2 = 0;
bufmgr_gem->exec_count++;
}
static void
mos_add_softpin_objects(struct mos_softpin_target softpin_target)
{
struct mos_bufmgr_gem *bufmgr_gem = (struct mos_bufmgr_gem *)softpin_target.bo->bufmgr;
struct mos_bo_gem *bo_gem = (struct mos_bo_gem *)softpin_target.bo;
int index;
struct drm_i915_gem_exec_object2 *exec2_objects;
struct mos_linux_bo **exec_bos;
if (bo_gem->validate_index != -1) {
bufmgr_gem->exec2_objects[bo_gem->validate_index].flags |= softpin_target.flags;
return;
}
/* Extend the array of validation entries as necessary. */
if (bufmgr_gem->exec_count == bufmgr_gem->exec_size) {
int new_size = bufmgr_gem->exec_size * 2;
if (new_size == 0)
new_size = ARRAY_INIT_SIZE;
exec2_objects = (struct drm_i915_gem_exec_object2 *)
realloc(bufmgr_gem->exec2_objects,
sizeof(*bufmgr_gem->exec2_objects) * new_size);
if (!exec2_objects)
{
MOS_DBG("realloc exec2_objects failed!\n");
return;
}
bufmgr_gem->exec2_objects = exec2_objects;
exec_bos = (struct mos_linux_bo **)realloc(bufmgr_gem->exec_bos,
sizeof(*bufmgr_gem->exec_bos) * new_size);
if (!exec_bos)
{
MOS_DBG("realloc exec_bo failed!\n");
return;
}
bufmgr_gem->exec_bos = exec_bos;
bufmgr_gem->exec_size = new_size;
}
index = bufmgr_gem->exec_count;
bo_gem->validate_index = index;
/* Fill in array entry */
bufmgr_gem->exec2_objects[index].handle = bo_gem->gem_handle;
bufmgr_gem->exec2_objects[index].relocation_count = bo_gem->reloc_count;
bufmgr_gem->exec2_objects[index].relocs_ptr = (uintptr_t)bo_gem->relocs;
bufmgr_gem->exec2_objects[index].alignment = softpin_target.bo->align;
bufmgr_gem->exec2_objects[index].offset = softpin_target.bo->offset64;
bufmgr_gem->exec2_objects[index].flags = softpin_target.flags;
bufmgr_gem->exec2_objects[index].pad_to_size = bo_gem->pad_to_size;
bufmgr_gem->exec2_objects[index].rsvd1 = 0;
bufmgr_gem->exec2_objects[index].rsvd2 = 0;
bufmgr_gem->exec_bos[index] = softpin_target.bo;
bufmgr_gem->exec_count++;
}
#define RELOC_BUF_SIZE(x) ((I915_RELOC_HEADER + x * I915_RELOC0_STRIDE) * \
sizeof(uint32_t))
static void
mos_bo_gem_set_in_aperture_size(struct mos_bufmgr_gem *bufmgr_gem,
struct mos_bo_gem *bo_gem,
unsigned int alignment)
{
unsigned int size;
assert(!bo_gem->used_as_reloc_target);
/* The older chipsets are far-less flexible in terms of tiling,
* and require tiled buffer to be size aligned in the aperture.
* This means that in the worst possible case we will need a hole
* twice as large as the object in order for it to fit into the
* aperture. Optimal packing is for wimps.
*/
size = bo_gem->bo.size;
bo_gem->reloc_tree_size = size + alignment;
}
static int
mos_setup_reloc_list(struct mos_linux_bo *bo)
{
struct mos_bo_gem *bo_gem = (struct mos_bo_gem *) bo;
struct mos_bufmgr_gem *bufmgr_gem = (struct mos_bufmgr_gem *) bo->bufmgr;
unsigned int max_relocs = bufmgr_gem->max_relocs;
if (bo->size / 4 < max_relocs)
max_relocs = bo->size / 4;
bo_gem->relocs = (struct drm_i915_gem_relocation_entry *)malloc(max_relocs *
sizeof(struct drm_i915_gem_relocation_entry));
bo_gem->reloc_target_info = (struct mos_reloc_target *)malloc(max_relocs *
sizeof(struct mos_reloc_target));
if (bo_gem->relocs == nullptr || bo_gem->reloc_target_info == nullptr) {
bo_gem->has_error = true;
free (bo_gem->relocs);
bo_gem->relocs = nullptr;
free (bo_gem->reloc_target_info);
bo_gem->reloc_target_info = nullptr;
return 1;
}
return 0;
}
static int
mos_gem_bo_busy(struct mos_linux_bo *bo)
{
struct mos_bufmgr_gem *bufmgr_gem = (struct mos_bufmgr_gem *) bo->bufmgr;
struct mos_bo_gem *bo_gem = (struct mos_bo_gem *) bo;
struct drm_i915_gem_busy busy;
int ret;
if (bo_gem->reusable && bo_gem->idle)
return false;
memclear(busy);
busy.handle = bo_gem->gem_handle;
ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_BUSY, &busy);
if (ret == 0) {
bo_gem->idle = !busy.busy;
return busy.busy;
} else {
return false;
}
return (ret == 0 && busy.busy);
}
static int
mos_gem_bo_madvise_internal(struct mos_bufmgr_gem *bufmgr_gem,
struct mos_bo_gem *bo_gem, int state)
{
struct drm_i915_gem_madvise madv;
memclear(madv);
madv.handle = bo_gem->gem_handle;
madv.madv = state;
madv.retained = 1;
drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_MADVISE, &madv);
return madv.retained;
}
static int
mos_gem_bo_madvise(struct mos_linux_bo *bo, int madv)
{
return mos_gem_bo_madvise_internal
((struct mos_bufmgr_gem *) bo->bufmgr,
(struct mos_bo_gem *) bo,
madv);
}
/* drop the oldest entries that have been purged by the kernel */
static void
mos_gem_bo_cache_purge_bucket(struct mos_bufmgr_gem *bufmgr_gem,
struct mos_gem_bo_bucket *bucket)
{
while (!DRMLISTEMPTY(&bucket->head)) {
struct mos_bo_gem *bo_gem;
bo_gem = DRMLISTENTRY(struct mos_bo_gem,
bucket->head.next, head);
if (mos_gem_bo_madvise_internal
(bufmgr_gem, bo_gem, I915_MADV_DONTNEED))
break;
DRMLISTDEL(&bo_gem->head);
mos_gem_bo_free(&bo_gem->bo);
}
}
static int
mos_gem_query_items(int fd, struct drm_i915_query_item *items, uint32_t n_items)
{
struct drm_i915_query q;
memclear(q);
q.num_items = n_items;
q.items_ptr = (uintptr_t)items;
return drmIoctl(fd, DRM_IOCTL_I915_QUERY, &q);
}
/**
* query mechanism for memory regions.
*/
static struct drm_i915_query_memory_regions *mos_gem_get_query_memory_regions(int fd)
{
int ret;
struct drm_i915_query_item item;
struct drm_i915_query_memory_regions *query_info;
memclear(item);
item.query_id = DRM_I915_QUERY_MEMORY_REGIONS;
ret = mos_gem_query_items(fd, &item, 1);
if (ret != 0 || item.length <= 0)
return NULL;
query_info = (drm_i915_query_memory_regions*)calloc(1, item.length);
item.data_ptr = (uintptr_t)query_info;
ret = mos_gem_query_items(fd, &item, 1);
if (ret != 0) {
free(query_info);
return NULL;
}
return query_info;
}
/**
* check how many lmem regions are available on device.
*/
static uint8_t mos_gem_get_lmem_region_count(int fd)
{
struct drm_i915_query_memory_regions *query_info;
uint8_t num_regions = 0;
uint8_t lmem_regions = 0;
query_info = mos_gem_get_query_memory_regions(fd);
if(query_info)
{
num_regions = query_info->num_regions;
for (int i = 0; i < num_regions; i++) {
if (query_info->regions[i].region.memory_class == I915_MEMORY_CLASS_DEVICE)
{
lmem_regions += 1;
}
}
free(query_info);
}
return lmem_regions;
}
/**
* check if lmem is available on device.
*/
static bool mos_gem_has_lmem(int fd)
{
return mos_gem_get_lmem_region_count(fd) > 0;
}
static enum mos_memory_zone
mos_gem_bo_memzone_for_address(uint64_t address)
{
if (address >= MEMZONE_DEVICE_START)
return MEMZONE_DEVICE;
else
return MEMZONE_SYS;
}
/**
* Allocate a section of virtual memory for a buffer, assigning an address.
*/
static uint64_t
mos_gem_bo_vma_alloc(struct mos_bufmgr *bufmgr,
enum mos_memory_zone memzone,
uint64_t size,
uint64_t alignment)
{
CHK_CONDITION(bufmgr == nullptr, "nullptr bufmgr.\n", 0);
struct mos_bufmgr_gem *bufmgr_gem = (struct mos_bufmgr_gem *) bufmgr;
/* Force alignment to be some number of pages */
alignment = ALIGN(alignment, PAGE_SIZE);
uint64_t addr = mos_vma_heap_alloc(&bufmgr_gem->vma_heap[memzone], size, alignment);
// currently only support 48bit range address
CHK_CONDITION((addr >> 48ull) != 0, "invalid address, over 48bit range.\n", 0);