Debian Codegraph @

media_softlet/linux/common/os/i915/include/mos_bufmgr_api.h

raw
1 /*
2 * Copyright (c) 2023, 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 shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22
23 //!
24 //! \file mos_bufmgr_api.h
25 //! \brief Public definitions of Intel-specific bufmgr api functions.
26 //!
27
28 #ifndef MOS_BUFMGR_API_H
29 #define MOS_BUFMGR_API_H
30
31 #include <stdio.h>
32 #include "drm.h"
33 #include "libdrm_macros.h"
34 #include "media_skuwa_specific.h"
35
36 #define S_SUCCESS 0
37
38 #define TILING_NONE 0
39 #define TILING_X 1
40 #define TILING_Y 2
41
42 #define DRM_ENGINE_CLASS_RENDER 0
43 #define DRM_ENGINE_CLASS_COPY 1
44 #define DRM_ENGINE_CLASS_VIDEO_DECODE 2
45 #define DRM_ENGINE_CLASS_VIDEO_ENHANCE 3
46 #define DRM_ENGINE_CLASS_COMPUTE 4
47
48 #define DRM_EXEC_DEFAULT 0
49 #define DRM_EXEC_RENDER 1
50 #define DRM_EXEC_BSD 2
51 #define DRM_EXEC_BLT 3
52 #define DRM_EXEC_VEBOX 4
53 #define DRM_EXEC_COMPUTE 5
54 #define DRM_EXEC_VCS2 6
55
56 #define DRM_CONTEXT_PARAM_PRIORITY 0x6
57 #define DRM_CONTEXT_PARAM_RECOVERABLE 0x8
58
59 #define mos_safe_free(p) \
60 if(p) free(p); \
61
62 #define CHK_CONDITION(condition, _str, _ret) \
63 if (condition) { \
64 fprintf(stderr, _str); \
65 return _ret; \
66 }
67
68 #define INVALID_VM -1
69
70 #ifndef MOS_UNUSED
71 #define MOS_UNUSED(param) (void)(param)
72 #endif
73
74 /**
75 * Supported data type:
76 * MOS_USER_FEATURE_VALUE_TYPE_BOOL
77 * MOS_USER_FEATURE_VALUE_TYPE_INT32
78 * MOS_USER_FEATURE_VALUE_TYPE_INT64
79 * MOS_USER_FEATURE_VALUE_TYPE_UINT32
80 * MOS_USER_FEATURE_VALUE_TYPE_UINT64
81 * MOS_USER_FEATURE_VALUE_TYPE_FLOAT
82 */
83 #define MOS_READ_ENV_VARIABLE(env_key, data_type, out_value) \
84 { \
85 MediaUserSetting::Value v(out_value); \
86 int ret = MosUtilities::MosReadEnvVariable(ENV_VARIABLE_TABLE[env_key], data_type, v); \
87 if(ret == 0) \
88 { \
89 if(data_type == MOS_USER_FEATURE_VALUE_TYPE_INT64) \
90 out_value = v.Get<int64_t>(); \
91 else if(data_type == MOS_USER_FEATURE_VALUE_TYPE_UINT64) \
92 out_value = v.Get<uint64_t>(); \
93 else if(data_type == MOS_USER_FEATURE_VALUE_TYPE_INT32) \
94 out_value = v.Get<int32_t>(); \
95 else if(data_type == MOS_USER_FEATURE_VALUE_TYPE_UINT32) \
96 out_value = v.Get<uint32_t>(); \
97 else if(data_type == MOS_USER_FEATURE_VALUE_TYPE_FLOAT) \
98 out_value = v.Get<float>(); \
99 else if(data_type == MOS_USER_FEATURE_VALUE_TYPE_BOOL) \
100 out_value = v.Get<bool>(); \
101 } \
102 }
103
104
105 struct mos_linux_bo;
106 struct mos_linux_context;
107 struct mos_bufmgr;
108 struct MOS_OCA_EXEC_LIST_INFO;
109
110 typedef struct mos_linux_bo MOS_LINUX_BO;
111 typedef struct mos_linux_context MOS_LINUX_CONTEXT;
112 typedef struct mos_bufmgr MOS_BUFMGR;
113 typedef struct MOS_OCA_EXEC_LIST_INFO mos_oca_exec_list_info;
114
115 struct _MOS_OS_CONTEXT;
116
117 enum mos_memory_zone {
118 MEMZONE_SYS,
119 MEMZONE_DEVICE,
120 MEMZONE_PRIME, //for imported PRIME buffers
121 MEMZONE_COUNT,
122 };
123
124 enum device_type {
125 DEVICE_TYPE_I915,
126 DEVICE_TYPE_XE,
127 DEVICE_TYPE_COUNT,
128 };
129
130 #define MEMZONE_SYS_START (1ull << 16)
131 #define MEMZONE_DEVICE_START (1ull << 40)
132 #define MEMZONE_SYS_SIZE (MEMZONE_DEVICE_START - MEMZONE_SYS_START)
133 #define MEMZONE_DEVICE_SIZE (1ull << 40)
134 #define MEMZONE_PRIME_START (MEMZONE_DEVICE_START + MEMZONE_DEVICE_SIZE)
135 #define MEMZONE_PRIME_SIZE (1ull << 40)
136 #define MEMZONE_TOTAL (1ull << 48)
137 #define PAGE_SIZE_4K (1ull << 12)
138 #define PAGE_SIZE_64K (1ull << 16)
139 #define PAGE_SIZE_1M (1ull << 20)
140 #define PAGE_SIZE_2M (1ull << 21)
141 #define PAGE_SIZE_4G (1ull << 32)
142 #define ARRAY_INIT_SIZE 5
143
144 struct mos_linux_context {
145 unsigned int ctx_id;
146 struct mos_bufmgr *bufmgr;
147 struct _MOS_OS_CONTEXT *pOsContext;
148 __u32 vm_id;
149 };
150
151 struct mos_linux_bo {
152 /**
153 * Size in bytes of the buffer object.
154 *
155 * The size may be larger than the size originally requested for the
156 * allocation, such as being aligned to page size.
157 */
158 unsigned long size;
159
160 /**
161 * Alignment requirement for object
162 *
163 * Used for GTT mapping & pinning the object.
164 */
165 unsigned long align;
166
167 /**
168 * Deprecated field containing (possibly the low 32-bits of) the last
169 * seen virtual card address. Use offset64 instead.
170 */
171 unsigned long offset;
172
173 /**
174 * Virtual address for accessing the buffer data. Only valid while
175 * mapped.
176 */
177 #ifdef __cplusplus
178 void *virt;
179 #else
180 void *virtual;
181 #endif
182
183 /** Buffer manager context associated with this buffer object */
184 struct mos_bufmgr *bufmgr;
185
186 /**
187 * MM-specific handle for accessing object
188 */
189 int handle;
190
191 /**
192 * Last seen card virtual address (offset from the beginning of the
193 * aperture) for the object. This should be used to fill relocation
194 * entries when calling drm_intel_bo_emit_reloc()
195 */
196 uint64_t offset64;
197
198 /**
199 * indicate if the bo mapped into aux table
200 */
201 bool aux_mapped;
202
203 __u32 vm_id;
204 };
205
206 #define BO_ALLOC_FOR_RENDER (1<<0)
207
208 #define PAT_INDEX_INVALID ((uint16_t)-1)
209 struct mos_drm_bo_alloc_ext{
210 unsigned long flags = 0;
211 uint32_t tiling_mode = TILING_NONE;
212 int mem_type = 0;
213 uint16_t pat_index = PAT_INDEX_INVALID;
214 bool cpu_cacheable = true;
215 bool scanout_surf = false;
216 };
217
218 struct mos_drm_bo_alloc {
219 const char *name = "\0";
220 unsigned long size = 0;
221 unsigned int alignment = 0;
222 unsigned long stride = 0;
223
224 struct mos_drm_bo_alloc_ext ext;
225 };
226
227 struct mos_drm_bo_alloc_userptr {
228 const char *name = "\0";
229 void *addr = 0;
230 uint32_t tiling_mode = 0;
231 uint32_t stride = 0;
232 unsigned long size = 0;
233 unsigned long flags = 0;
234 uint16_t pat_index = 0;
235 };
236
237 struct mos_drm_bo_alloc_tiled {
238 const char *name = "\0";
239 int x = 0;
240 int y = 0;
241 int cpp = 0;
242 unsigned long pitch = 0;
243 unsigned int alignment = 0;
244
245 struct mos_drm_bo_alloc_ext ext;
246 };
247
248 struct mos_drm_bo_alloc_prime {
249 const char *name = "\0";
250 int prime_fd;
251 int size;
252 uint16_t pat_index;
253 };
254
255 struct mos_drm_uc_version {
256 #define UC_TYPE_GUC_SUBMISSION 0
257 #define UC_TYPE_HUC 1
258 #define UC_TYPE_MAX 2
259 #define UC_TYPE_INVALID (uint16_t)-1
260 uint16_t uc_type;
261 uint32_t major_version;
262 uint32_t minor_version;
263 };
264
265 struct mos_linux_bo *mos_bo_alloc(struct mos_bufmgr *bufmgr,
266 struct mos_drm_bo_alloc *alloc);
267 struct mos_linux_bo *mos_bo_alloc_userptr(struct mos_bufmgr *bufmgr,
268 struct mos_drm_bo_alloc_userptr *alloc_uptr);
269 struct mos_linux_bo *mos_bo_alloc_tiled(struct mos_bufmgr *bufmgr,
270 struct mos_drm_bo_alloc_tiled *alloc_tiled);
271 void mos_bo_reference(struct mos_linux_bo *bo);
272 void mos_bo_unreference(struct mos_linux_bo *bo);
273 int mos_bo_map(struct mos_linux_bo *bo, int write_enable);
274 int mos_bo_unmap(struct mos_linux_bo *bo);
275
276 void mos_bo_wait_rendering(struct mos_linux_bo *bo);
277
278 void mos_bufmgr_set_debug(struct mos_bufmgr *bufmgr, int enable_debug);
279 void mos_bufmgr_destroy(struct mos_bufmgr *bufmgr);
280 int mos_bo_exec(struct mos_linux_bo *bo, int used,
281 struct drm_clip_rect *cliprects, int num_cliprects, int DR4);
282 int mos_bo_mrb_exec(struct mos_linux_bo *bo, int used,
283 struct drm_clip_rect *cliprects, int num_cliprects, int DR4,
284 unsigned int flags);
285 int mos_bufmgr_check_aperture_space(struct mos_linux_bo ** bo_array, int count);
286
287 int mos_bo_emit_reloc(struct mos_linux_bo *bo, uint32_t offset,
288 struct mos_linux_bo *target_bo, uint32_t target_offset,
289 uint32_t read_domains, uint32_t write_domain,
290 uint64_t presumed_offset);
291 int mos_bo_set_tiling(struct mos_linux_bo *bo, uint32_t * tiling_mode,
292 uint32_t stride);
293 int mos_bo_get_tiling(struct mos_linux_bo *bo, uint32_t * tiling_mode,
294 uint32_t * swizzle_mode);
295
296 int mos_bo_flink(struct mos_linux_bo *bo, uint32_t * name);
297 int mos_bo_busy(struct mos_linux_bo *bo);
298 int mos_bo_madvise(struct mos_linux_bo *bo, int madv);
299 int mos_bo_use_48b_address_range(struct mos_linux_bo *bo, uint32_t enable);
300 void mos_bo_set_object_async(struct mos_linux_bo *bo);
301 void mos_bo_set_exec_object_async(struct mos_linux_bo *bo, struct mos_linux_bo *target_bo);
302 void mos_bo_set_object_capture(struct mos_linux_bo *bo);
303 int mos_bo_set_softpin(struct mos_linux_bo *bo);
304 int mos_bo_add_softpin_target(struct mos_linux_bo *bo, struct mos_linux_bo *target_bo, bool write_flag);
305 mos_oca_exec_list_info* mos_bo_get_softpin_targets_info(struct mos_linux_bo *bo, int *count);
306
307 int mos_bo_disable_reuse(struct mos_linux_bo *bo);
308 int mos_bo_is_reusable(struct mos_linux_bo *bo);
309 int mos_bo_references(struct mos_linux_bo *bo, struct mos_linux_bo *target_bo);
310 int mos_bo_pad_to_size(struct mos_linux_bo *bo, uint64_t pad_to_size);
311
312 /* drm_intel_bufmgr_gem.c */
313 struct mos_bufmgr *mos_bufmgr_gem_init(int fd, int batch_size, int *device_type = nullptr);
314 struct mos_linux_bo *mos_bo_create_from_name(struct mos_bufmgr *bufmgr,
315 const char *name,
316 unsigned int handle);
317 void mos_bufmgr_enable_reuse(struct mos_bufmgr *bufmgr);
318 void mos_bufmgr_enable_softpin(struct mos_bufmgr *bufmgr, bool va1m_align);
319 void mos_bufmgr_enable_vmbind(struct mos_bufmgr *bufmgr);
320 void mos_bufmgr_disable_object_capture(struct mos_bufmgr *bufmgr);
321 int mos_bufmgr_get_memory_info(struct mos_bufmgr *bufmgr, char *info, uint32_t length);
322 int mos_bufmgr_get_devid(struct mos_bufmgr *bufmgr);
323 void mos_bufmgr_realloc_cache(struct mos_bufmgr *bufmgr, uint8_t alloc_mode);
324
325 int mos_bo_map_unsynchronized(struct mos_linux_bo *bo);
326 int mos_bo_map_gtt(struct mos_linux_bo *bo);
327 int mos_bo_unmap_gtt(struct mos_linux_bo *bo);
328 int mos_bo_unmap_wc(struct mos_linux_bo *bo);
329
330 void mos_bo_start_gtt_access(struct mos_linux_bo *bo, int write_enable);
331
332 struct mos_linux_context *mos_context_create(struct mos_bufmgr *bufmgr);
333 struct mos_linux_context *mos_context_create_ext(
334 struct mos_bufmgr *bufmgr,
335 __u32 flags,
336 bool bContextProtected);
337 struct mos_linux_context *mos_context_create_shared(
338 struct mos_bufmgr *bufmgr,
339 mos_linux_context* ctx,
340 __u32 flags,
341 bool bContextProtected,
342 void *engine_map,
343 uint8_t ctx_width,
344 uint8_t num_placements,
345 uint32_t ctx_type);
346
347 __u32 mos_vm_create(struct mos_bufmgr *bufmgr);
348 void mos_vm_destroy(struct mos_bufmgr *bufmgr, __u32 vm_id);
349
350 #define MAX_ENGINE_INSTANCE_NUM 8
351 #define MAX_PARALLEN_CMD_BO_NUM MAX_ENGINE_INSTANCE_NUM
352
353 int mos_query_engines_count(struct mos_bufmgr *bufmgr,
354 unsigned int *nengine);
355
356 int mos_query_engines(struct mos_bufmgr *bufmgr,
357 __u16 engine_class,
358 __u64 caps,
359 unsigned int *nengine,
360 void *ci);
361
362 size_t mos_get_engine_class_size(struct mos_bufmgr *bufmgr);
363
364 void mos_select_fixed_engine(struct mos_bufmgr *bufmgr,
365 void *engine_map,
366 uint32_t *nengine,
367 uint32_t fixed_instance_mask);
368
369 void mos_context_destroy(struct mos_linux_context *ctx);
370
371 int
372 mos_bo_context_exec2(struct mos_linux_bo *bo, int used, struct mos_linux_context *ctx,
373 struct drm_clip_rect *cliprects, int num_cliprects, int DR4,
374 unsigned int flags, int *fence);
375
376 int
377 mos_bo_context_exec3(struct mos_linux_bo **bo, int num_bo, struct mos_linux_context *ctx,
378 struct drm_clip_rect *cliprects, int num_cliprects, int DR4,
379 unsigned int flags, int *fence);
380
381 int mos_bo_export_to_prime(struct mos_linux_bo *bo, int *prime_fd);
382 struct mos_linux_bo *mos_bo_create_from_prime(struct mos_bufmgr *bufmgr,
383 struct mos_drm_bo_alloc_prime *alloc_prime);
384
385 int mos_reg_read(struct mos_bufmgr *bufmgr,
386 uint32_t offset,
387 uint64_t *result);
388
389 int mos_get_reset_stats(struct mos_linux_context *ctx,
390 uint32_t *reset_count,
391 uint32_t *active,
392 uint32_t *pending);
393
394 int mos_get_context_param(struct mos_linux_context *ctx,
395 uint32_t size,
396 uint64_t param,
397 uint64_t *value);
398
399 int mos_set_context_param(struct mos_linux_context *ctx,
400 uint32_t size,
401 uint64_t param,
402 uint64_t value);
403
404 uint8_t mos_switch_off_n_bits(struct mos_linux_context *ctx, uint8_t in_mask, int n);
405 unsigned int mos_hweight8(struct mos_linux_context *ctx, uint8_t w);
406
407 int mos_query_sys_engines(struct mos_bufmgr *bufmgr, MEDIA_SYSTEM_INFO* gfx_info);
408 int mos_query_device_blob(struct mos_bufmgr *bufmgr, MEDIA_SYSTEM_INFO* gfx_info);
409 int mos_query_hw_ip_version(struct mos_bufmgr *bufmgr, __u16 engine_class, void *ip_ver_info);
410 int mos_get_param(int fd, int32_t param, uint32_t *param_value);
411
412 struct LinuxDriverInfo;
413 int mos_get_driver_info(struct mos_bufmgr *bufmgr, struct LinuxDriverInfo *drvInfo);
414
415 int mos_get_device_id(int fd, uint32_t *deviceId);
416
417 #if defined(__cplusplus)
418 extern "C" {
419 #endif
420 extern drm_export int drmIoctl(int fd, unsigned long request, void *arg);
421
422 static inline int mos_query_device_type(int fd)
423 {
424 int device_type = DEVICE_TYPE_COUNT;
425 drm_version_t version;
426 char name[5] = "";
427
428 memset(&version, 0, sizeof(version));
429 version.name_len = 4;
430 version.name = name;
431
432 if (drmIoctl(fd, DRM_IOCTL_VERSION, &version))
433 {
434 fprintf(stderr, "DRM_IOCTL_VERSION failed: %s\n", strerror(errno));
435 return device_type;
436 }
437
438 if(strcmp("i915", name) == 0)
439 {
440 device_type = DEVICE_TYPE_I915;
441 }
442 else if(strcmp("xe", name) == 0)
443 {
444 device_type = DEVICE_TYPE_XE;
445 }
446 else
447 {
448 fprintf(stderr, "DRM_IOCTL_VERSION, unsupported drm device by media driver: %s\n", name);
449 }
450 return device_type;
451 }
452
453 drm_export int mos_bo_map_wc(struct mos_linux_bo *bo);
454 drm_export void mos_bo_clear_relocs(struct mos_linux_bo *bo, int start);
455 drm_export int mos_bo_wait(struct mos_linux_bo *bo, int64_t timeout_ns);
456
457 drm_export bool mos_bo_is_softpin(struct mos_linux_bo *bo);
458 drm_export bool mos_bo_is_exec_object_async(struct mos_linux_bo *bo);
459 #if defined(__cplusplus)
460 }
461 #endif
462
463 #define PLATFORM_INFORMATION_IS_SERVER 0x1
464
465 uint64_t mos_get_platform_information(struct mos_bufmgr *bufmgr);
466 void mos_set_platform_information(struct mos_bufmgr *bufmgr, uint64_t p);
467 bool mos_has_bsd2(struct mos_bufmgr *bufmgr);
468 int mos_get_ts_frequency(struct mos_bufmgr *bufmgr, uint32_t *ts_freq);
469 void mos_enable_turbo_boost(struct mos_bufmgr *bufmgr);
470
471 #endif /* MOS_BUFMGR_API_H */