blob: cefc1f9d35c3e6a2e42ed0ceb0ed831bef36484e [file] [log] [blame]
Chia-I Wu82f50aa2014-08-05 10:43:03 +08001/*
2 * XGL
3 *
4 * Copyright (C) 2014 LunarG, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25#include "dispatch_tables.h"
26#include "gpu.h"
Courtney Goeltzenleuchterfb4fb532014-08-14 09:35:21 -060027#include "dev.h"
Chia-I Wu82f50aa2014-08-05 10:43:03 +080028#include "obj.h"
29
30/**
31 * Return true if an (not so) arbitrary pointer casted to intel_base points to
32 * a valid intel_base. This assumes at least the first sizeof(void*) bytes of
33 * the address are accessible, and they does not happen to be our magic
34 * values.
35 */
36bool intel_base_is_valid(const struct intel_base *base)
37{
38 if (base->dispatch != &intel_normal_dispatch_table &&
39 base->dispatch != &intel_debug_dispatch_table)
40 return false;
41
42 return !intel_gpu_is_valid((const struct intel_gpu *) base);
43}
44
Chia-I Wu26f0bd02014-08-07 10:38:40 +080045XGL_RESULT intel_base_get_info(struct intel_base *base, int type,
46 XGL_SIZE *size, XGL_VOID *data)
47{
48 XGL_RESULT ret = XGL_SUCCESS;
49 XGL_SIZE s;
50
51 switch (type) {
52 case XGL_INFO_TYPE_MEMORY_REQUIREMENTS:
53 s = sizeof(XGL_MEMORY_REQUIREMENTS);
54 memset(data, 0, s);
55 *size = s;
56 break;
57 default:
58 ret = XGL_ERROR_INVALID_VALUE;
59 break;
60 }
61
62 return ret;
63}
64
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +080065static bool base_dbg_copy_create_info(struct intel_base_dbg *dbg,
66 const void *create_info)
67{
68 const union {
69 const void *ptr;
70 const struct {
71 XGL_STRUCTURE_TYPE struct_type;
72 XGL_VOID *next;
73 } *header;
74 } info = { .ptr = create_info };
75 XGL_SIZE shallow_copy = 0;
76
77 if (!create_info)
78 return true;
79
80 switch (info.header->struct_type) {
81 case XGL_STRUCTURE_TYPE_DEVICE_CREATE_INFO:
82 assert(dbg->type == XGL_DBG_OBJECT_DEVICE);
83 break;
84 case XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO:
85 assert(dbg->type == XGL_DBG_OBJECT_GPU_MEMORY);
86 shallow_copy = sizeof(XGL_MEMORY_ALLOC_INFO);
87 break;
88 case XGL_STRUCTURE_TYPE_EVENT_CREATE_INFO:
89 assert(dbg->type == XGL_DBG_OBJECT_EVENT);
90 shallow_copy = sizeof(XGL_EVENT_CREATE_INFO);
91 break;
92 case XGL_STRUCTURE_TYPE_FENCE_CREATE_INFO:
93 assert(dbg->type == XGL_DBG_OBJECT_FENCE);
94 shallow_copy = sizeof(XGL_FENCE_CREATE_INFO);
95 break;
Courtney Goeltzenleuchter850d12c2014-08-07 18:13:10 -060096 case XGL_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO:
97 assert(dbg->type == XGL_DBG_OBJECT_QUERY_POOL);
98 shallow_copy = sizeof(XGL_QUERY_POOL_CREATE_INFO);
99 break;
Chia-I Wufeb441f2014-08-08 21:27:38 +0800100 case XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO:
101 assert(dbg->type == XGL_DBG_OBJECT_IMAGE);
102 shallow_copy = sizeof(XGL_IMAGE_CREATE_INFO);
103 break;
Chia-I Wu5a323262014-08-11 10:31:53 +0800104 case XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO:
105 assert(dbg->type == XGL_DBG_OBJECT_IMAGE_VIEW);
106 shallow_copy = sizeof(XGL_IMAGE_VIEW_CREATE_INFO);
107 break;
108 case XGL_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO:
109 assert(dbg->type == XGL_DBG_OBJECT_COLOR_TARGET_VIEW);
110 shallow_copy = sizeof(XGL_COLOR_ATTACHMENT_VIEW_CREATE_INFO);
111 break;
112 case XGL_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO:
113 assert(dbg->type == XGL_DBG_OBJECT_DEPTH_STENCIL_VIEW);
114 shallow_copy = sizeof(XGL_DEPTH_STENCIL_VIEW_CREATE_INFO);
115 break;
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +0800116 default:
Courtney Goeltzenleuchterfb4fb532014-08-14 09:35:21 -0600117 // log debug message regarding invalid struct_type?
118 intel_dev_log(dbg->dev, XGL_DBG_MSG_ERROR,
119 XGL_VALIDATION_LEVEL_0, XGL_NULL_HANDLE, 0, 0,
120 "Invalid Create Info type: 0x%x", info.header->struct_type);
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +0800121 return false;
122 break;
123 }
124
125 if (shallow_copy) {
126 assert(!info.header->next);
127
128 dbg->create_info = icd_alloc(shallow_copy, 0, XGL_SYSTEM_ALLOC_DEBUG);
129 if (!dbg->create_info)
130 return false;
131
132 memcpy(dbg->create_info, create_info, shallow_copy);
Chia-I Wue2934f92014-08-16 13:17:22 +0800133 dbg->create_info_size = shallow_copy;
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +0800134 } else if (info.header->struct_type ==
135 XGL_STRUCTURE_TYPE_DEVICE_CREATE_INFO) {
136 const XGL_DEVICE_CREATE_INFO *src = info.ptr;
137 XGL_DEVICE_CREATE_INFO *dst;
138 uint8_t *d;
139 XGL_SIZE size;
140 XGL_UINT i;
141
142 size = sizeof(*src);
Chia-I Wue2934f92014-08-16 13:17:22 +0800143 dbg->create_info_size = size;
144
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +0800145 size += sizeof(src->pRequestedQueues[0]) * src->queueRecordCount;
146 size += sizeof(src->ppEnabledExtensionNames[0]) * src->extensionCount;
147 for (i = 0; i < src->extensionCount; i++) {
148 size += 1 +
149 strlen((const char *) src->ppEnabledExtensionNames[i]);
150 }
151
Chia-I Wu98dcfab2014-08-07 12:07:52 +0800152 dst = icd_alloc(size, 0, XGL_SYSTEM_ALLOC_DEBUG);
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +0800153 if (!dst)
154 return false;
155
156 memcpy(dst, src, sizeof(*src));
157
158 d = (uint8_t *) dst;
159 d += sizeof(*src);
160
161 size = sizeof(src->pRequestedQueues[0]) * src->queueRecordCount;
162 memcpy(d, src->pRequestedQueues, size);
163 dst->pRequestedQueues = (const XGL_DEVICE_QUEUE_CREATE_INFO *) d;
164 d += size;
165
166 size = sizeof(src->ppEnabledExtensionNames[0]) * src->extensionCount;
167 dst->ppEnabledExtensionNames = (const XGL_CHAR * const *) d;
168
169 for (i = 0; i < src->extensionCount; i++) {
170 const XGL_SIZE len =
171 strlen((const char *) src->ppEnabledExtensionNames[i]);
172
173 memcpy(d + size, src->ppEnabledExtensionNames[i], len + 1);
174 ((const XGL_CHAR **) d)[i] = (const XGL_CHAR *) (d + size);
175
176 size += len + 1;
177 }
178 }
179
180 return true;
181}
182
Chia-I Wu82f50aa2014-08-05 10:43:03 +0800183/**
Chia-I Wubbf2c932014-08-07 12:20:08 +0800184 * Create an intel_base_dbg. When dbg_size is non-zero, a buffer of that
Chia-I Wu660caf82014-08-07 10:54:26 +0800185 * size is allocated and zeroed.
Chia-I Wu82f50aa2014-08-05 10:43:03 +0800186 */
Courtney Goeltzenleuchterfb4fb532014-08-14 09:35:21 -0600187struct intel_base_dbg *intel_base_dbg_create(struct intel_dev *dev,
188 XGL_DBG_OBJECT_TYPE type,
Chia-I Wu660caf82014-08-07 10:54:26 +0800189 const void *create_info,
Chia-I Wubbf2c932014-08-07 12:20:08 +0800190 XGL_SIZE dbg_size)
Chia-I Wu82f50aa2014-08-05 10:43:03 +0800191{
Chia-I Wu660caf82014-08-07 10:54:26 +0800192 struct intel_base_dbg *dbg;
193
Chia-I Wubbf2c932014-08-07 12:20:08 +0800194 if (!dbg_size)
195 dbg_size = sizeof(*dbg);
Chia-I Wu660caf82014-08-07 10:54:26 +0800196
Chia-I Wubbf2c932014-08-07 12:20:08 +0800197 assert(dbg_size >= sizeof(*dbg));
Chia-I Wu660caf82014-08-07 10:54:26 +0800198
Chia-I Wubbf2c932014-08-07 12:20:08 +0800199 dbg = icd_alloc(dbg_size, 0, XGL_SYSTEM_ALLOC_DEBUG);
Chia-I Wu660caf82014-08-07 10:54:26 +0800200 if (!dbg)
201 return NULL;
202
Chia-I Wubbf2c932014-08-07 12:20:08 +0800203 memset(dbg, 0, dbg_size);
Chia-I Wu660caf82014-08-07 10:54:26 +0800204
Chia-I Wu82f50aa2014-08-05 10:43:03 +0800205 dbg->alloc_id = icd_get_allocator_id();
206 dbg->type = type;
Courtney Goeltzenleuchterfb4fb532014-08-14 09:35:21 -0600207 dbg->dev = dev;
Chia-I Wu82f50aa2014-08-05 10:43:03 +0800208
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +0800209 if (!base_dbg_copy_create_info(dbg, create_info)) {
210 icd_free(dbg);
211 return NULL;
Chia-I Wu82f50aa2014-08-05 10:43:03 +0800212 }
213
Chia-I Wu82f50aa2014-08-05 10:43:03 +0800214 return dbg;
215}
216
217void intel_base_dbg_destroy(struct intel_base_dbg *dbg)
218{
Chia-I Wu660caf82014-08-07 10:54:26 +0800219 if (dbg->tag)
220 icd_free(dbg->tag);
221
222 if (dbg->create_info)
223 icd_free(dbg->create_info);
224
Chia-I Wu82f50aa2014-08-05 10:43:03 +0800225 icd_free(dbg);
226}
Chia-I Wu53fc6aa2014-08-06 14:22:51 +0800227
Chia-I Wubbf2c932014-08-07 12:20:08 +0800228/**
229 * Create an intel_base. obj_size and dbg_size specify the real sizes of the
230 * object and the debug metadata. Memories are zeroed.
231 */
Courtney Goeltzenleuchterfb4fb532014-08-14 09:35:21 -0600232struct intel_base *intel_base_create(struct intel_dev *dev,
233 XGL_SIZE obj_size, bool debug,
Chia-I Wubbf2c932014-08-07 12:20:08 +0800234 XGL_DBG_OBJECT_TYPE type,
235 const void *create_info,
236 XGL_SIZE dbg_size)
237{
238 struct intel_base *base;
239
240 if (!obj_size)
241 obj_size = sizeof(*base);
242
243 assert(obj_size >= sizeof(*base));
244
245 base = icd_alloc(obj_size, 0, XGL_SYSTEM_ALLOC_API_OBJECT);
246 if (!base)
247 return NULL;
248
Courtney Goeltzenleuchterfb4fb532014-08-14 09:35:21 -0600249 if (dev == NULL) {
250 /*
251 * dev is NULL when we are creating the base device object
252 * Set dev now so that debug setup happens correctly
253 */
254 dev = (struct intel_dev *) base;
255 }
256
Chia-I Wubbf2c932014-08-07 12:20:08 +0800257 memset(base, 0, obj_size);
258
259 if (debug) {
260 base->dispatch = &intel_debug_dispatch_table;
Courtney Goeltzenleuchterfb4fb532014-08-14 09:35:21 -0600261 base->dbg = intel_base_dbg_create(dev, type, create_info, dbg_size);
Chia-I Wubbf2c932014-08-07 12:20:08 +0800262 if (!base->dbg) {
263 icd_free(base);
264 return NULL;
265 }
266 }
267 else {
268 base->dispatch = &intel_normal_dispatch_table;
269 }
270 base->get_info = intel_base_get_info;
271
272 return base;
273}
274
275void intel_base_destroy(struct intel_base *base)
276{
277 if (base->dbg)
278 intel_base_dbg_destroy(base->dbg);
279 icd_free(base);
280}
281
Chia-I Wu53fc6aa2014-08-06 14:22:51 +0800282XGL_RESULT XGLAPI intelDestroyObject(
283 XGL_OBJECT object)
284{
285 struct intel_obj *obj = intel_obj(object);
286
287 obj->destroy(obj);
288
289 return XGL_SUCCESS;
290}
291
292XGL_RESULT XGLAPI intelGetObjectInfo(
293 XGL_BASE_OBJECT object,
294 XGL_OBJECT_INFO_TYPE infoType,
295 XGL_SIZE* pDataSize,
296 XGL_VOID* pData)
297{
298 struct intel_base *base = intel_base(object);
Chia-I Wu53fc6aa2014-08-06 14:22:51 +0800299
Chia-I Wu26f0bd02014-08-07 10:38:40 +0800300 return base->get_info(base, infoType, pDataSize, pData);
Chia-I Wu53fc6aa2014-08-06 14:22:51 +0800301}
302
303XGL_RESULT XGLAPI intelBindObjectMemory(
304 XGL_OBJECT object,
305 XGL_GPU_MEMORY mem,
306 XGL_GPU_SIZE offset)
307{
308 struct intel_obj *obj = intel_obj(object);
309
310 obj->mem = mem;
311 obj->offset = offset;
312
313 return XGL_SUCCESS;
314}