1 /*
2  * Copyright © 2008-2021 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  * Authors:
24  *    Eric Anholt <eric@anholt.net>
25  *
26  */
27 
28 /**
29  * @file mos_bufmgr.h
30  *
31  * Public definitions of Intel-specific bufmgr functions.
32  */
33 
34 #ifndef MOS_BUFMGR_H
35 #define MOS_BUFMGR_H
36 
37 #include <stdio.h>
38 #include <stdint.h>
39 #include <stdio.h>
40 #include "libdrm_macros.h"
41 
42 #define S_SUCCESS 0
43 #define mos_safe_free(p)        \
44         if(p) free(p);          \
45 
46 #define CHK_CONDITION(condition, _str, _ret)    \
47     if (condition) {                            \
48         fprintf(stderr, _str);                  \
49         return _ret;                            \
50     }
51 
52 struct drm_clip_rect;
53 struct mos_bufmgr;
54 struct mos_linux_context;
55 
56 typedef struct mos_linux_bo MOS_LINUX_BO;
57 typedef struct mos_linux_context MOS_LINUX_CONTEXT;
58 typedef struct mos_bufmgr MOS_BUFMGR;
59 
60 #include "mos_os_specific.h"
61 
62 enum mos_memory_zone {
63    MEMZONE_SYS,
64    MEMZONE_DEVICE,
65    MEMZONE_PRIME, //for imported PRIME buffers
66    MEMZONE_COUNT,
67 };
68 
69 #define MEMZONE_SYS_START     (1ull << 16)
70 #define MEMZONE_DEVICE_START  (1ull << 40)
71 #define MEMZONE_SYS_SIZE      (MEMZONE_DEVICE_START - MEMZONE_SYS_START)
72 #define MEMZONE_DEVICE_SIZE   (1ull << 40)
73 #define MEMZONE_PRIME_START   (MEMZONE_DEVICE_START + MEMZONE_DEVICE_SIZE)
74 #define MEMZONE_PRIME_SIZE    (1ull << 40)
75 #define MEMZONE_TOTAL         (1ull << 48)
76 #define PAGE_SIZE_64K         (1ull << 16)
77 #define PAGE_SIZE_2M          (1ull << 21)
78 #define PAGE_SIZE_4G          (1ull << 32)
79 #define ARRAY_INIT_SIZE       5
80 
81 struct mos_linux_context {
82     unsigned int ctx_id;
83     struct mos_bufmgr *bufmgr;
84     struct _MOS_OS_CONTEXT    *pOsContext;
85     struct drm_i915_gem_vm_control* vm;
86 };
87 
88 struct mos_linux_bo {
89     /**
90      * Size in bytes of the buffer object.
91      *
92      * The size may be larger than the size originally requested for the
93      * allocation, such as being aligned to page size.
94      */
95     unsigned long size;
96 
97     /**
98      * Alignment requirement for object
99      *
100      * Used for GTT mapping & pinning the object.
101      */
102     unsigned long align;
103 
104     /**
105      * Deprecated field containing (possibly the low 32-bits of) the last
106      * seen virtual card address.  Use offset64 instead.
107      */
108     unsigned long offset;
109 
110     /**
111      * Virtual address for accessing the buffer data.  Only valid while
112      * mapped.
113      */
114 #ifdef __cplusplus
115     void *virt;
116 #else
117     void *virtual;
118 #endif
119 
120     /** Buffer manager context associated with this buffer object */
121     struct mos_bufmgr *bufmgr;
122 
123     /**
124      * MM-specific handle for accessing object
125      */
126     int handle;
127 
128     /**
129      * Last seen card virtual address (offset from the beginning of the
130      * aperture) for the object.  This should be used to fill relocation
131      * entries when calling drm_intel_bo_emit_reloc()
132      */
133     uint64_t offset64;
134 
135     /**
136      * indicate if the bo mapped into aux table
137      */
138     bool aux_mapped;
139 };
140 
141 enum mos_aub_dump_bmp_format {
142     MOS_AUB_DUMP_BMP_FORMAT_8BIT = 1,
143     MOS_AUB_DUMP_BMP_FORMAT_ARGB_4444 = 4,
144     MOS_AUB_DUMP_BMP_FORMAT_ARGB_0888 = 6,
145     MOS_AUB_DUMP_BMP_FORMAT_ARGB_8888 = 7,
146 };
147 
148 struct mos_aub_annotation {
149     uint32_t type;
150     uint32_t subtype;
151     uint32_t ending_offset;
152 };
153 
154 #define BO_ALLOC_FOR_RENDER (1<<0)
155 
156 struct mos_linux_bo *mos_bo_alloc(struct mos_bufmgr *bufmgr, const char *name,
157                  unsigned long size, unsigned int alignment, int mem_type);
158 struct mos_linux_bo *mos_bo_alloc_for_render(struct mos_bufmgr *bufmgr,
159                         const char *name,
160                         unsigned long size,
161                         unsigned int alignment,
162                         int mem_type);
163 struct mos_linux_bo *mos_bo_alloc_userptr(struct mos_bufmgr *bufmgr,
164                     const char *name,
165                     void *addr, uint32_t tiling_mode,
166                     uint32_t stride, unsigned long size,
167                     unsigned long flags);
168 struct mos_linux_bo *mos_bo_alloc_tiled(struct mos_bufmgr *bufmgr,
169                        const char *name,
170                        int x, int y, int cpp,
171                        uint32_t *tiling_mode,
172                        unsigned long *pitch,
173                        unsigned long flags,
174                        int mem_type);
175 void mos_bo_reference(struct mos_linux_bo *bo);
176 void mos_bo_unreference(struct mos_linux_bo *bo);
177 int mos_bo_map(struct mos_linux_bo *bo, int write_enable);
178 int mos_bo_unmap(struct mos_linux_bo *bo);
179 
180 void mos_bo_wait_rendering(struct mos_linux_bo *bo);
181 
182 void mos_bufmgr_set_debug(struct mos_bufmgr *bufmgr, int enable_debug);
183 void mos_bufmgr_destroy(struct mos_bufmgr *bufmgr);
184 int mos_bo_exec(struct mos_linux_bo *bo, int used,
185               struct drm_clip_rect *cliprects, int num_cliprects, int DR4);
186 int mos_bo_mrb_exec(struct mos_linux_bo *bo, int used,
187             struct drm_clip_rect *cliprects, int num_cliprects, int DR4,
188             unsigned int flags);
189 int mos_bufmgr_check_aperture_space(struct mos_linux_bo ** bo_array, int count);
190 
191 int mos_bo_emit_reloc(struct mos_linux_bo *bo, uint32_t offset,
192                 struct mos_linux_bo *target_bo, uint32_t target_offset,
193                 uint32_t read_domains, uint32_t write_domain);
194 int mos_bo_emit_reloc2(struct mos_linux_bo *bo, uint32_t offset,
195          struct mos_linux_bo *target_bo, uint32_t target_offset,
196          uint32_t read_domains, uint32_t write_domain,
197          uint64_t presumed_offset);
198 int mos_bo_emit_reloc_fence(struct mos_linux_bo *bo, uint32_t offset,
199                   struct mos_linux_bo *target_bo,
200                   uint32_t target_offset,
201                   uint32_t read_domains, uint32_t write_domain);
202 int mos_bo_pin(struct mos_linux_bo *bo, uint32_t alignment);
203 int mos_bo_unpin(struct mos_linux_bo *bo);
204 int mos_bo_set_tiling(struct mos_linux_bo *bo, uint32_t * tiling_mode,
205                 uint32_t stride);
206 int mos_bo_get_tiling(struct mos_linux_bo *bo, uint32_t * tiling_mode,
207                 uint32_t * swizzle_mode);
208 
209 int mos_bo_flink(struct mos_linux_bo *bo, uint32_t * name);
210 int mos_bo_busy(struct mos_linux_bo *bo);
211 int mos_bo_madvise(struct mos_linux_bo *bo, int madv);
212 int mos_bo_use_48b_address_range(struct mos_linux_bo *bo, uint32_t enable);
213 void mos_bo_set_object_async(struct mos_linux_bo *bo);
214 void mos_bo_set_exec_object_async(struct mos_linux_bo *bo, struct mos_linux_bo *target_bo);
215 void mos_bo_set_object_capture(struct mos_linux_bo *bo);
216 int mos_bo_set_softpin(struct mos_linux_bo *bo);
217 int mos_bo_add_softpin_target(struct mos_linux_bo *bo, struct mos_linux_bo *target_bo, bool write_flag);
218 
219 int mos_bo_disable_reuse(struct mos_linux_bo *bo);
220 int mos_bo_is_reusable(struct mos_linux_bo *bo);
221 int mos_bo_references(struct mos_linux_bo *bo, struct mos_linux_bo *target_bo);
222 int mos_bo_pad_to_size(struct mos_linux_bo *bo, uint64_t pad_to_size);
223 
224 /* drm_intel_bufmgr_gem.c */
225 struct mos_bufmgr *mos_bufmgr_gem_init(int fd, int batch_size);
226 struct mos_linux_bo *mos_bo_gem_create_from_name(struct mos_bufmgr *bufmgr,
227                         const char *name,
228                         unsigned int handle);
229 void mos_bufmgr_gem_enable_reuse(struct mos_bufmgr *bufmgr);
230 void mos_bufmgr_gem_enable_fenced_relocs(struct mos_bufmgr *bufmgr);
231 void mos_bufmgr_gem_enable_softpin(struct mos_bufmgr *bufmgr);
232 void mos_bufmgr_gem_set_vma_cache_size(struct mos_bufmgr *bufmgr,
233                          int limit);
234 int mos_bufmgr_gem_get_memory_info(struct mos_bufmgr *bufmgr, char *info, uint32_t length);
235 int mos_gem_bo_map_unsynchronized(struct mos_linux_bo *bo);
236 int mos_gem_bo_map_gtt(struct mos_linux_bo *bo);
237 int mos_gem_bo_unmap_gtt(struct mos_linux_bo *bo);
238 int mos_gem_bo_map_wc_unsynchronized(struct mos_linux_bo *bo);
239 int mos_gem_bo_unmap_wc(struct mos_linux_bo *bo);
240 
241 int mos_gem_bo_get_fake_offset(struct mos_linux_bo *bo);
242 int mos_gem_bo_get_reloc_count(struct mos_linux_bo *bo);
243 void mos_gem_bo_start_gtt_access(struct mos_linux_bo *bo, int write_enable);
244 
245 void
246 mos_bufmgr_gem_set_aub_filename(struct mos_bufmgr *bufmgr,
247                       const char *filename);
248 void mos_bufmgr_gem_set_aub_dump(struct mos_bufmgr *bufmgr, int enable);
249 void mos_gem_bo_aub_dump_bmp(struct mos_linux_bo *bo,
250                    int x1, int y1, int width, int height,
251                    enum mos_aub_dump_bmp_format format,
252                    int pitch, int offset);
253 void
254 mos_bufmgr_gem_set_aub_annotations(struct mos_linux_bo *bo,
255                      struct mos_aub_annotation *annotations,
256                      unsigned count);
257 
258 int mos_get_pipe_from_crtc_id(struct mos_bufmgr *bufmgr, int crtc_id);
259 
260 int mos_bufmgr_gem_get_devid(struct mos_bufmgr *bufmgr);
261 
262 struct mos_linux_context *mos_gem_context_create(struct mos_bufmgr *bufmgr);
263 struct mos_linux_context *mos_gem_context_create_ext(
264                             struct mos_bufmgr *bufmgr,
265                             __u32 flags);
266 struct mos_linux_context *mos_gem_context_create_shared(
267                             struct mos_bufmgr *bufmgr,
268                             mos_linux_context* ctx,
269                             __u32 flags);
270 struct drm_i915_gem_vm_control* mos_gem_vm_create(struct mos_bufmgr *bufmgr);
271 void mos_gem_vm_destroy(struct mos_bufmgr *bufmgr, struct drm_i915_gem_vm_control* vm);
272 
273 #define MAX_ENGINE_INSTANCE_NUM 8
274 #define MAX_PARALLEN_CMD_BO_NUM MAX_ENGINE_INSTANCE_NUM
275 
276 int mos_query_engines_count(struct mos_bufmgr *bufmgr,
277                       unsigned int *nengine);
278 
279 int mos_query_engines(struct mos_bufmgr *bufmgr,
280                       __u16 engine_class,
281                       __u64 caps,
282                       unsigned int *nengine,
283                       struct i915_engine_class_instance *ci);
284 
285 int mos_set_context_param_parallel(struct mos_linux_context *ctx,
286                          struct i915_engine_class_instance *ci,
287                          unsigned int count);
288 
289 int mos_set_context_param_load_balance(struct mos_linux_context *ctx,
290                          struct i915_engine_class_instance *ci,
291                          unsigned int count);
292 int mos_set_context_param_bond(struct mos_linux_context *ctx,
293                         struct i915_engine_class_instance master_ci,
294                         struct i915_engine_class_instance *bond_ci,
295                         unsigned int bond_count);
296 
297 void mos_gem_context_destroy(struct mos_linux_context *ctx);
298 int mos_gem_bo_context_exec(struct mos_linux_bo *bo, struct mos_linux_context *ctx,
299                   int used, unsigned int flags);
300 int
301 mos_gem_bo_context_exec2(struct mos_linux_bo *bo, int used, struct mos_linux_context *ctx,
302                                struct drm_clip_rect *cliprects, int num_cliprects, int DR4,
303                                unsigned int flags, int *fence);
304 
305 int
306 mos_gem_bo_context_exec3(struct mos_linux_bo **bo, int num_bo, struct mos_linux_context *ctx,
307                                struct drm_clip_rect *cliprects, int num_cliprects, int DR4,
308                                unsigned int flags, int *fence);
309 
310 int mos_bo_gem_export_to_prime(struct mos_linux_bo *bo, int *prime_fd);
311 struct mos_linux_bo *mos_bo_gem_create_from_prime(struct mos_bufmgr *bufmgr,
312                         int prime_fd, int size);
313 
314 /* drm_intel_bufmgr_fake.c */
315 struct mos_bufmgr *mos_bufmgr_fake_init(int fd,
316                          unsigned long low_offset,
317                          void *low_virtual,
318                          unsigned long size,
319                          volatile unsigned int
320                          *last_dispatch);
321 void mos_bufmgr_fake_set_last_dispatch(struct mos_bufmgr *bufmgr,
322                          volatile unsigned int
323                          *last_dispatch);
324 void mos_bufmgr_fake_set_exec_callback(struct mos_bufmgr *bufmgr,
325                          int (*exec) (struct mos_linux_bo *bo,
326                               unsigned int used,
327                               void *priv),
328                          void *priv);
329 void mos_bufmgr_fake_set_fence_callback(struct mos_bufmgr *bufmgr,
330                           unsigned int (*emit) (void *priv),
331                           void (*wait) (unsigned int fence,
332                                 void *priv),
333                           void *priv);
334 struct mos_linux__bo *mos_bo_fake_alloc_static(struct mos_bufmgr *bufmgr,
335                          const char *name,
336                          unsigned long offset,
337                          unsigned long size, void *virt);
338 void mos_bo_fake_disable_backing_store(struct mos_linux_bo *bo,
339                      void (*invalidate_cb) (struct mos_linux_bo * bo,
340                                     void *ptr),
341                          void *ptr);
342 
343 void mos_bufmgr_fake_contended_lock_take(struct mos_bufmgr *bufmgr);
344 void mos_bufmgr_fake_evict_all(struct mos_bufmgr *bufmgr);
345 
346 struct mos_decode *mos_decode_context_alloc(uint32_t devid);
347 void mos_decode_context_free(struct mos_decode *ctx);
348 void mos_decode_set_batch_pointer(struct mos_decode *ctx,
349                     void *data, uint32_t hw_offset,
350                     int count);
351 void mos_decode_set_dump_past_end(struct mos_decode *ctx,
352                     int dump_past_end);
353 void mos_decode_set_head_tail(struct mos_decode *ctx,
354                     uint32_t head, uint32_t tail);
355 void mos_decode_set_output_file(struct mos_decode *ctx, FILE *out);
356 void mos_decode(struct mos_decode *ctx);
357 
358 int mos_reg_read(struct mos_bufmgr *bufmgr,
359                uint32_t offset,
360                uint64_t *result);
361 
362 int mos_get_reset_stats(struct mos_linux_context *ctx,
363                   uint32_t *reset_count,
364                   uint32_t *active,
365                   uint32_t *pending);
366 
367 int mos_get_context_param(struct mos_linux_context *ctx,
368                            uint32_t size,
369                            uint64_t param,
370                            uint64_t *value);
371 
372 int mos_set_context_param(struct mos_linux_context *ctx,
373                 uint32_t size,
374                 uint64_t param,
375                 uint64_t value);
376 
377 int mos_get_subslice_total(int fd, unsigned int *subslice_total);
378 int mos_get_eu_total(int fd, unsigned int *eu_total);
379 
380 int mos_get_context_param_sseu(struct mos_linux_context *ctx,
381                 struct drm_i915_gem_context_param_sseu *sseu);
382 int mos_set_context_param_sseu(struct mos_linux_context *ctx,
383                 struct drm_i915_gem_context_param_sseu sseu);
384 int mos_get_subslice_mask(int fd, unsigned int *subslice_mask);
385 int mos_get_slice_mask(int fd, unsigned int *slice_mask);
386 uint8_t mos_switch_off_n_bits(uint8_t in_mask, int n);
387 unsigned int mos_hweight8(uint8_t w);
388 int mos_query_device_blob(int fd, MEDIA_SYSTEM_INFO* gfx_info);
389 
390 #if defined(__cplusplus)
391 extern "C" {
392 #endif
393 
394 drm_export int mos_gem_bo_map_wc(struct mos_linux_bo *bo);
395 drm_export void mos_gem_bo_clear_relocs(struct mos_linux_bo *bo, int start);
396 drm_export int mos_gem_bo_wait(struct mos_linux_bo *bo, int64_t timeout_ns);
397 drm_export struct mos_linux_bo *
398 mos_gem_bo_alloc_internal(struct mos_bufmgr *bufmgr,
399                 const char *name,
400                 unsigned long size,
401                 unsigned long flags,
402                 uint32_t tiling_mode,
403                 unsigned long stride,
404                 unsigned int alignment,
405                 int mem_type);
406 drm_export int
407 mos_gem_bo_exec(struct mos_linux_bo *bo, int used,
408               drm_clip_rect_t * cliprects, int num_cliprects, int DR4);
409 drm_export int do_exec2(struct mos_linux_bo *bo, int used, struct mos_linux_context *ctx,
410      drm_clip_rect_t *cliprects, int num_cliprects, int DR4,
411      unsigned int flags, int *fence);
412 
413 drm_export int do_exec3(struct mos_linux_bo **bo, int num_bo, struct mos_linux_context *ctx,
414      drm_clip_rect_t *cliprects, int num_cliprects, int DR4,
415      unsigned int flags, int *fence);
416 
417 drm_export void mos_gem_bo_free(struct mos_linux_bo *bo);
418 drm_export void mos_gem_bo_unreference_final(struct mos_linux_bo *bo, time_t time);
419 drm_export int mos_gem_bo_map(struct mos_linux_bo *bo, int write_enable);
420 drm_export int map_gtt(struct mos_linux_bo *bo);
421 drm_export int mos_gem_bo_unmap(struct mos_linux_bo *bo);
422 
423 drm_export bool mos_gem_bo_is_softpin(struct mos_linux_bo *bo);
424 drm_export bool mos_gem_bo_is_exec_object_async(struct mos_linux_bo *bo);
425 #if defined(__cplusplus)
426 }
427 #endif
428 
429 #endif /* INTEL_BUFMGR_H */
430