xref: /aosp_15_r20/external/mesa3d/src/intel/common/i915/intel_gem.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2022 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 
24 #pragma once
25 
26 #include <stdbool.h>
27 #include <stdint.h>
28 
29 #include "common/intel_gem.h"
30 
31 #include "drm-uapi/i915_drm.h"
32 
33 bool i915_gem_create_context(int fd, uint32_t *context_id);
34 bool i915_gem_destroy_context(int fd, uint32_t context_id);
35 bool i915_gem_create_context_engines(int fd,
36                                      enum intel_gem_create_context_flags flags,
37                                      const struct intel_query_engine_info *info,
38                                      int num_engines, enum intel_engine_class *engine_classes,
39                                      uint32_t vm_id,
40                                      uint32_t *context_id);
41 bool i915_gem_set_context_param(int fd, uint32_t context, uint32_t param,
42                                 uint64_t value);
43 bool i915_gem_get_context_param(int fd, uint32_t context, uint32_t param,
44                                 uint64_t *value);
45 bool i915_gem_read_render_timestamp(int fd, uint64_t *value);
46 bool
47 i915_gem_create_context_ext(int fd,
48                             enum intel_gem_create_context_flags flags,
49                             uint32_t *ctx_id);
50 bool i915_gem_supports_protected_context(int fd);
51 bool i915_gem_get_param(int fd, uint32_t param, int *value);
52 bool i915_gem_can_render_on_fd(int fd);
53 
54 /**
55  * A wrapper around DRM_IOCTL_I915_QUERY
56  *
57  * Unfortunately, the error semantics of this ioctl are rather annoying so
58  * it's better to have a common helper.
59  */
60 static inline int
intel_i915_query_flags(int fd,uint64_t query_id,uint32_t flags,void * buffer,int32_t * buffer_len)61 intel_i915_query_flags(int fd, uint64_t query_id, uint32_t flags,
62                        void *buffer, int32_t *buffer_len)
63 {
64    struct drm_i915_query_item item = {
65       .query_id = query_id,
66       .length = *buffer_len,
67       .flags = flags,
68       .data_ptr = (uintptr_t)buffer,
69    };
70 
71    struct drm_i915_query args = {
72       .num_items = 1,
73       .flags = 0,
74       .items_ptr = (uintptr_t)&item,
75    };
76 
77    int ret = intel_ioctl(fd, DRM_IOCTL_I915_QUERY, &args);
78    if (ret != 0)
79       return -errno;
80    else if (item.length < 0)
81       return item.length;
82 
83    *buffer_len = item.length;
84    return 0;
85 }
86 
87 static inline int
intel_i915_query(int fd,uint64_t query_id,void * buffer,int32_t * buffer_len)88 intel_i915_query(int fd, uint64_t query_id, void *buffer,
89                  int32_t *buffer_len)
90 {
91    return intel_i915_query_flags(fd, query_id, 0, buffer, buffer_len);
92 }
93 
94 /**
95  * Query for the given data, allocating as needed
96  *
97  * The caller is responsible for freeing the returned pointer.
98  */
99 static inline void *
intel_i915_query_alloc(int fd,uint64_t query_id,int32_t * query_length)100 intel_i915_query_alloc(int fd, uint64_t query_id, int32_t *query_length)
101 {
102    if (query_length)
103       *query_length = 0;
104 
105    int32_t length = 0;
106    int ret = intel_i915_query(fd, query_id, NULL, &length);
107    if (ret < 0)
108       return NULL;
109 
110    void *data = calloc(1, length);
111    assert(data != NULL); /* This shouldn't happen in practice */
112    if (data == NULL)
113       return NULL;
114 
115    ret = intel_i915_query(fd, query_id, data, &length);
116    assert(ret == 0); /* We should have caught the error above */
117    if (ret < 0) {
118       free(data);
119       return NULL;
120    }
121 
122    if (query_length)
123       *query_length = length;
124 
125    return data;
126 }
127 
128 static inline void
intel_i915_gem_add_ext(__u64 * ptr,uint32_t ext_name,struct i915_user_extension * ext)129 intel_i915_gem_add_ext(__u64 *ptr, uint32_t ext_name,
130                   struct i915_user_extension *ext)
131 {
132    __u64 *iter = ptr;
133 
134    while (*iter != 0) {
135       iter = (__u64 *) &((struct i915_user_extension *)(uintptr_t)*iter)->next_extension;
136    }
137 
138    ext->name = ext_name;
139 
140    *iter = (uintptr_t) ext;
141 }
142