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_64K (1ull << 16)
138 #define PAGE_SIZE_1M (1ull << 20)
139 #define PAGE_SIZE_2M (1ull << 21)
140 #define PAGE_SIZE_4G (1ull << 32)
141 #define ARRAY_INIT_SIZE 5
142
143 struct mos_linux_context {
144 unsigned int ctx_id;
145 struct mos_bufmgr *bufmgr;
146 struct _MOS_OS_CONTEXT *pOsContext;
147 __u32 vm_id;
148 };
149
150 struct mos_linux_bo {
151 /**
152 * Size in bytes of the buffer object.
153 *
154 * The size may be larger than the size originally requested for the
155 * allocation, such as being aligned to page size.
156 */
157 unsigned long size;
158
159 /**
160 * Alignment requirement for object
161 *
162 * Used for GTT mapping & pinning the object.
163 */
164 unsigned long align;
165
166 /**
167 * Deprecated field containing (possibly the low 32-bits of) the last
168 * seen virtual card address. Use offset64 instead.
169 */
170 unsigned long offset;
171
172 /**
173 * Virtual address for accessing the buffer data. Only valid while
174 * mapped.
175 */
176 #ifdef __cplusplus
177 void *virt;
178 #else
179 void *virtual;
180 #endif
181
182 /** Buffer manager context associated with this buffer object */
183 struct mos_bufmgr *bufmgr;
184
185 /**
186 * MM-specific handle for accessing object
187 */
188 int handle;
189
190 /**
191 * Last seen card virtual address (offset from the beginning of the
192 * aperture) for the object. This should be used to fill relocation
193 * entries when calling drm_intel_bo_emit_reloc()
194 */
195 uint64_t offset64;
196
197 /**
198 * indicate if the bo mapped into aux table
199 */
200 bool aux_mapped;
201
202 __u32 vm_id;
203 };
204
205 #define BO_ALLOC_FOR_RENDER (1<<0)
206
207 #define PAT_INDEX_INVALID ((uint16_t)-1)
208 struct mos_drm_bo_alloc_ext{
209 unsigned long flags = 0;
210 uint32_t tiling_mode = TILING_NONE;
211 int mem_type = 0;
212 uint16_t pat_index = PAT_INDEX_INVALID;
213 bool cpu_cacheable = true;
214 };
215
216 struct mos_drm_bo_alloc {
217 const char *name = "\0";
218 unsigned long size = 0;
219 unsigned int alignment = 0;
220 unsigned long stride = 0;
221
222 struct mos_drm_bo_alloc_ext ext;
223 };
224
225 struct mos_drm_bo_alloc_userptr {
226 const char *name = "\0";
227 void *addr = 0;
228 uint32_t tiling_mode = 0;
229 uint32_t stride = 0;
230 unsigned long size = 0;
231 unsigned long flags = 0;
232 uint16_t pat_index = 0;
233 };
234
235 struct mos_drm_bo_alloc_tiled {
236 const char *name = "\0";
237 int x = 0;
238 int y = 0;
239 int cpp = 0;
240 unsigned long pitch = 0;
241
242 struct mos_drm_bo_alloc_ext ext;
243 };
244
245 struct mos_drm_bo_alloc_prime {
246 const char *name = "\0";
247 int prime_fd;
248 int size;
249 uint16_t pat_index;
250 };
251
252 struct mos_drm_uc_version {
253 #define UC_TYPE_GUC_SUBMISSION 0
254 #define UC_TYPE_HUC 1
255 #define UC_TYPE_MAX 2
256 #define UC_TYPE_INVALID (uint16_t)-1
257 uint16_t uc_type;
258 uint32_t major_version;
259 uint32_t minor_version;
260 };
261
262 struct mos_linux_bo *mos_bo_alloc(struct mos_bufmgr *bufmgr,
263 struct mos_drm_bo_alloc *alloc);
264 struct mos_linux_bo *mos_bo_alloc_userptr(struct mos_bufmgr *bufmgr,
265 struct mos_drm_bo_alloc_userptr *alloc_uptr);
266 struct mos_linux_bo *mos_bo_alloc_tiled(struct mos_bufmgr *bufmgr,
267 struct mos_drm_bo_alloc_tiled *alloc_tiled);
268 void mos_bo_reference(struct mos_linux_bo *bo);
269 void mos_bo_unreference(struct mos_linux_bo *bo);
270 int mos_bo_map(struct mos_linux_bo *bo, int write_enable);
271 int mos_bo_unmap(struct mos_linux_bo *bo);
272
273 void mos_bo_wait_rendering(struct mos_linux_bo *bo);
274
275 void mos_bufmgr_set_debug(struct mos_bufmgr *bufmgr, int enable_debug);
276 void mos_bufmgr_destroy(struct mos_bufmgr *bufmgr);
277 int mos_bo_exec(struct mos_linux_bo *bo, int used,
278 struct drm_clip_rect *cliprects, int num_cliprects, int DR4);
279 int mos_bo_mrb_exec(struct mos_linux_bo *bo, int used,
280 struct drm_clip_rect *cliprects, int num_cliprects, int DR4,
281 unsigned int flags);
282 int mos_bufmgr_check_aperture_space(struct mos_linux_bo ** bo_array, int count);
283
284 int mos_bo_emit_reloc(struct mos_linux_bo *bo, uint32_t offset,
285 struct mos_linux_bo *target_bo, uint32_t target_offset,
286 uint32_t read_domains, uint32_t write_domain,
287 uint64_t presumed_offset);
288 int mos_bo_set_tiling(struct mos_linux_bo *bo, uint32_t * tiling_mode,
289 uint32_t stride);
290 int mos_bo_get_tiling(struct mos_linux_bo *bo, uint32_t * tiling_mode,
291 uint32_t * swizzle_mode);
292
293 int mos_bo_flink(struct mos_linux_bo *bo, uint32_t * name);
294 int mos_bo_busy(struct mos_linux_bo *bo);
295 int mos_bo_madvise(struct mos_linux_bo *bo, int madv);
296 int mos_bo_use_48b_address_range(struct mos_linux_bo *bo, uint32_t enable);
297 void mos_bo_set_object_async(struct mos_linux_bo *bo);
298 void mos_bo_set_exec_object_async(struct mos_linux_bo *bo, struct mos_linux_bo *target_bo);
299 void mos_bo_set_object_capture(struct mos_linux_bo *bo);
300 int mos_bo_set_softpin(struct mos_linux_bo *bo);
301 int mos_bo_add_softpin_target(struct mos_linux_bo *bo, struct mos_linux_bo *target_bo, bool write_flag);
302 mos_oca_exec_list_info* mos_bo_get_softpin_targets_info(struct mos_linux_bo *bo, int *count);
303
304 int mos_bo_disable_reuse(struct mos_linux_bo *bo);
305 int mos_bo_is_reusable(struct mos_linux_bo *bo);
306 int mos_bo_references(struct mos_linux_bo *bo, struct mos_linux_bo *target_bo);
307 int mos_bo_pad_to_size(struct mos_linux_bo *bo, uint64_t pad_to_size);
308
309 /* drm_intel_bufmgr_gem.c */
310 struct mos_bufmgr *mos_bufmgr_gem_init(int fd, int batch_size, int *device_type = nullptr);
311 struct mos_linux_bo *mos_bo_create_from_name(struct mos_bufmgr *bufmgr,
312 const char *name,
313 unsigned int handle);
314 void mos_bufmgr_enable_reuse(struct mos_bufmgr *bufmgr);
315 void mos_bufmgr_enable_softpin(struct mos_bufmgr *bufmgr, bool va1m_align);
316 void mos_bufmgr_enable_vmbind(struct mos_bufmgr *bufmgr);
317 void mos_bufmgr_disable_object_capture(struct mos_bufmgr *bufmgr);
318 int mos_bufmgr_get_memory_info(struct mos_bufmgr *bufmgr, char *info, uint32_t length);
319 int mos_bufmgr_get_devid(struct mos_bufmgr *bufmgr);
320 void mos_bufmgr_realloc_cache(struct mos_bufmgr *bufmgr, uint8_t alloc_mode);
321
322 int mos_bo_map_unsynchronized(struct mos_linux_bo *bo);
323 int mos_bo_map_gtt(struct mos_linux_bo *bo);
324 int mos_bo_unmap_gtt(struct mos_linux_bo *bo);
325 int mos_bo_unmap_wc(struct mos_linux_bo *bo);
326
327 void mos_bo_start_gtt_access(struct mos_linux_bo *bo, int write_enable);
328
329 struct mos_linux_context *mos_context_create(struct mos_bufmgr *bufmgr);
330 struct mos_linux_context *mos_context_create_ext(
331 struct mos_bufmgr *bufmgr,
332 __u32 flags,
333 bool bContextProtected);
334 struct mos_linux_context *mos_context_create_shared(
335 struct mos_bufmgr *bufmgr,
336 mos_linux_context* ctx,
337 __u32 flags,
338 bool bContextProtected,
339 void *engine_map,
340 uint8_t ctx_width,
341 uint8_t num_placements,
342 uint32_t ctx_type);
343
344 __u32 mos_vm_create(struct mos_bufmgr *bufmgr);
345 void mos_vm_destroy(struct mos_bufmgr *bufmgr, __u32 vm_id);
346
347 #define MAX_ENGINE_INSTANCE_NUM 8
348 #define MAX_PARALLEN_CMD_BO_NUM MAX_ENGINE_INSTANCE_NUM
349
350 int mos_query_engines_count(struct mos_bufmgr *bufmgr,
351 unsigned int *nengine);
352
353 int mos_query_engines(struct mos_bufmgr *bufmgr,
354 __u16 engine_class,
355 __u64 caps,
356 unsigned int *nengine,
357 void *ci);
358
359 size_t mos_get_engine_class_size(struct mos_bufmgr *bufmgr);
360
361 void mos_select_fixed_engine(struct mos_bufmgr *bufmgr,
362 void *engine_map,
363 uint32_t *nengine,
364 uint32_t fixed_instance_mask);
365
366 void mos_context_destroy(struct mos_linux_context *ctx);
367
368 int
369 mos_bo_context_exec2(struct mos_linux_bo *bo, int used, struct mos_linux_context *ctx,
370 struct drm_clip_rect *cliprects, int num_cliprects, int DR4,
371 unsigned int flags, int *fence);
372
373 int
374 mos_bo_context_exec3(struct mos_linux_bo **bo, int num_bo, struct mos_linux_context *ctx,
375 struct drm_clip_rect *cliprects, int num_cliprects, int DR4,
376 unsigned int flags, int *fence);
377
378 int mos_bo_export_to_prime(struct mos_linux_bo *bo, int *prime_fd);
379 struct mos_linux_bo *mos_bo_create_from_prime(struct mos_bufmgr *bufmgr,
380 struct mos_drm_bo_alloc_prime *alloc_prime);
381
382 int mos_reg_read(struct mos_bufmgr *bufmgr,
383 uint32_t offset,
384 uint64_t *result);
385
386 int mos_get_reset_stats(struct mos_linux_context *ctx,
387 uint32_t *reset_count,
388 uint32_t *active,
389 uint32_t *pending);
390
391 int mos_get_context_param(struct mos_linux_context *ctx,
392 uint32_t size,
393 uint64_t param,
394 uint64_t *value);
395
396 int mos_set_context_param(struct mos_linux_context *ctx,
397 uint32_t size,
398 uint64_t param,
399 uint64_t value);
400
401 uint8_t mos_switch_off_n_bits(struct mos_linux_context *ctx, uint8_t in_mask, int n);
402 unsigned int mos_hweight8(struct mos_linux_context *ctx, uint8_t w);
403
404 int mos_query_sys_engines(struct mos_bufmgr *bufmgr, MEDIA_SYSTEM_INFO* gfx_info);
405 int mos_query_device_blob(struct mos_bufmgr *bufmgr, MEDIA_SYSTEM_INFO* gfx_info);
406 int mos_query_hw_ip_version(struct mos_bufmgr *bufmgr, __u16 engine_class, void *ip_ver_info);
407 int mos_get_param(int fd, int32_t param, uint32_t *param_value);
408
409 struct LinuxDriverInfo;
410 int mos_get_driver_info(struct mos_bufmgr *bufmgr, struct LinuxDriverInfo *drvInfo);
411
412 int mos_get_device_id(int fd, uint32_t *deviceId);
413
414 #if defined(__cplusplus)
415 extern "C" {
416 #endif
417 extern drm_export int drmIoctl(int fd, unsigned long request, void *arg);
418
mos_query_device_type(int fd)419 static inline int mos_query_device_type(int fd)
420 {
421 int device_type = DEVICE_TYPE_COUNT;
422 drm_version_t version;
423 char name[5] = "";
424
425 memset(&version, 0, sizeof(version));
426 version.name_len = 4;
427 version.name = name;
428
429 if (drmIoctl(fd, DRM_IOCTL_VERSION, &version))
430 {
431 fprintf(stderr, "DRM_IOCTL_VERSION failed: %s\n", strerror(errno));
432 return device_type;
433 }
434
435 if(strcmp("i915", name) == 0)
436 {
437 device_type = DEVICE_TYPE_I915;
438 }
439 else if(strcmp("xe", name) == 0)
440 {
441 device_type = DEVICE_TYPE_XE;
442 }
443 else
444 {
445 fprintf(stderr, "DRM_IOCTL_VERSION, unsupported drm device by media driver: %s\n", name);
446 }
447 return device_type;
448 }
449
450 drm_export int mos_bo_map_wc(struct mos_linux_bo *bo);
451 drm_export void mos_bo_clear_relocs(struct mos_linux_bo *bo, int start);
452 drm_export int mos_bo_wait(struct mos_linux_bo *bo, int64_t timeout_ns);
453
454 drm_export bool mos_bo_is_softpin(struct mos_linux_bo *bo);
455 drm_export bool mos_bo_is_exec_object_async(struct mos_linux_bo *bo);
456 #if defined(__cplusplus)
457 }
458 #endif
459
460 #define PLATFORM_INFORMATION_IS_SERVER 0x1
461
462 uint64_t mos_get_platform_information(struct mos_bufmgr *bufmgr);
463 void mos_set_platform_information(struct mos_bufmgr *bufmgr, uint64_t p);
464 bool mos_has_bsd2(struct mos_bufmgr *bufmgr);
465 int mos_get_ts_frequency(struct mos_bufmgr *bufmgr, uint32_t *ts_freq);
466 void mos_enable_turbo_boost(struct mos_bufmgr *bufmgr);
467
468 #endif /* MOS_BUFMGR_API_H */
469