blob: f5dd1952152426c99eee363a4761c30cbc1d77ff [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
Chia-I Wude2bb862014-08-19 14:32:47 +080025#include "dispatch.h"
Chia-I Wu82f50aa2014-08-05 10:43:03 +080026#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{
Chia-I Wu6a42c2a2014-08-19 14:36:47 +080038 if (base->dispatch != intel_dispatch_get(true) &&
39 base->dispatch != intel_dispatch_get(false))
Chia-I Wu82f50aa2014-08-05 10:43:03 +080040 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
Chia-I Wub1076d72014-08-18 16:10:20 +080080 switch (dbg->type) {
81 case XGL_DBG_OBJECT_DEVICE:
82 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_DEVICE_CREATE_INFO);
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +080083 break;
Chia-I Wub1076d72014-08-18 16:10:20 +080084 case XGL_DBG_OBJECT_GPU_MEMORY:
85 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO);
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +080086 shallow_copy = sizeof(XGL_MEMORY_ALLOC_INFO);
87 break;
Chia-I Wub1076d72014-08-18 16:10:20 +080088 case XGL_DBG_OBJECT_EVENT:
89 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_EVENT_CREATE_INFO);
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +080090 shallow_copy = sizeof(XGL_EVENT_CREATE_INFO);
91 break;
Chia-I Wub1076d72014-08-18 16:10:20 +080092 case XGL_DBG_OBJECT_FENCE:
93 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_FENCE_CREATE_INFO);
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +080094 shallow_copy = sizeof(XGL_FENCE_CREATE_INFO);
95 break;
Chia-I Wub1076d72014-08-18 16:10:20 +080096 case XGL_DBG_OBJECT_QUERY_POOL:
97 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO);
Courtney Goeltzenleuchter850d12c2014-08-07 18:13:10 -060098 shallow_copy = sizeof(XGL_QUERY_POOL_CREATE_INFO);
99 break;
Chia-I Wub1076d72014-08-18 16:10:20 +0800100 case XGL_DBG_OBJECT_IMAGE:
101 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_IMAGE_CREATE_INFO);
Chia-I Wufeb441f2014-08-08 21:27:38 +0800102 shallow_copy = sizeof(XGL_IMAGE_CREATE_INFO);
103 break;
Chia-I Wub1076d72014-08-18 16:10:20 +0800104 case XGL_DBG_OBJECT_IMAGE_VIEW:
105 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO);
Chia-I Wu5a323262014-08-11 10:31:53 +0800106 shallow_copy = sizeof(XGL_IMAGE_VIEW_CREATE_INFO);
107 break;
Chia-I Wub1076d72014-08-18 16:10:20 +0800108 case XGL_DBG_OBJECT_COLOR_TARGET_VIEW:
109 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_COLOR_ATTACHMENT_VIEW_CREATE_INFO);
Chia-I Wu5a323262014-08-11 10:31:53 +0800110 shallow_copy = sizeof(XGL_COLOR_ATTACHMENT_VIEW_CREATE_INFO);
111 break;
Chia-I Wub1076d72014-08-18 16:10:20 +0800112 case XGL_DBG_OBJECT_DEPTH_STENCIL_VIEW:
113 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_DEPTH_STENCIL_VIEW_CREATE_INFO);
Chia-I Wu5a323262014-08-11 10:31:53 +0800114 shallow_copy = sizeof(XGL_DEPTH_STENCIL_VIEW_CREATE_INFO);
115 break;
Chia-I Wub1076d72014-08-18 16:10:20 +0800116 case XGL_DBG_OBJECT_SAMPLER:
117 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_SAMPLER_CREATE_INFO);
Chia-I Wu28b89962014-08-18 14:40:49 +0800118 shallow_copy = sizeof(XGL_SAMPLER_CREATE_INFO);
119 break;
Chia-I Wub1076d72014-08-18 16:10:20 +0800120 case XGL_DBG_OBJECT_DESCRIPTOR_SET:
121 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_DESCRIPTOR_SET_CREATE_INFO);
Chia-I Wub8d04c82014-08-18 15:51:10 +0800122 shallow_copy = sizeof(XGL_DESCRIPTOR_SET_CREATE_INFO);
123 break;
Chia-I Wua5714e82014-08-11 15:33:42 +0800124 case XGL_DBG_OBJECT_VIEWPORT_STATE:
125 /* no struct header! */
126 shallow_copy = sizeof(XGL_VIEWPORT_STATE_CREATE_INFO);
127 break;
128 case XGL_DBG_OBJECT_RASTER_STATE:
129 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_RASTER_STATE_CREATE_INFO);
130 shallow_copy = sizeof(XGL_RASTER_STATE_CREATE_INFO);
131 break;
132 case XGL_DBG_OBJECT_MSAA_STATE:
133 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_MSAA_STATE_CREATE_INFO);
134 shallow_copy = sizeof(XGL_MSAA_STATE_CREATE_INFO);
135 break;
136 case XGL_DBG_OBJECT_COLOR_BLEND_STATE:
137 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_COLOR_BLEND_STATE_CREATE_INFO);
138 shallow_copy = sizeof(XGL_COLOR_BLEND_STATE_CREATE_INFO);
139 break;
140 case XGL_DBG_OBJECT_DEPTH_STENCIL_STATE:
141 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_DEPTH_STENCIL_STATE_CREATE_INFO);
142 shallow_copy = sizeof(XGL_DEPTH_STENCIL_STATE_CREATE_INFO);
143 break;
Chia-I Wu730e5362014-08-19 12:15:09 +0800144 case XGL_DBG_OBJECT_CMD_BUFFER:
145 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_CMD_BUFFER_CREATE_INFO);
146 shallow_copy = sizeof(XGL_CMD_BUFFER_CREATE_INFO);
147 break;
Courtney Goeltzenleuchter05a60542014-08-15 14:54:34 -0600148 case XGL_DBG_OBJECT_GRAPHICS_PIPELINE:
149 assert(info.header->struct_type == XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO);
150 break;
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +0800151 default:
Courtney Goeltzenleuchterfb4fb532014-08-14 09:35:21 -0600152 // log debug message regarding invalid struct_type?
153 intel_dev_log(dbg->dev, XGL_DBG_MSG_ERROR,
154 XGL_VALIDATION_LEVEL_0, XGL_NULL_HANDLE, 0, 0,
155 "Invalid Create Info type: 0x%x", info.header->struct_type);
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +0800156 return false;
157 break;
158 }
159
160 if (shallow_copy) {
161 assert(!info.header->next);
162
163 dbg->create_info = icd_alloc(shallow_copy, 0, XGL_SYSTEM_ALLOC_DEBUG);
164 if (!dbg->create_info)
165 return false;
166
167 memcpy(dbg->create_info, create_info, shallow_copy);
Chia-I Wue2934f92014-08-16 13:17:22 +0800168 dbg->create_info_size = shallow_copy;
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +0800169 } else if (info.header->struct_type ==
170 XGL_STRUCTURE_TYPE_DEVICE_CREATE_INFO) {
171 const XGL_DEVICE_CREATE_INFO *src = info.ptr;
172 XGL_DEVICE_CREATE_INFO *dst;
173 uint8_t *d;
174 XGL_SIZE size;
175 XGL_UINT i;
176
177 size = sizeof(*src);
Chia-I Wue2934f92014-08-16 13:17:22 +0800178 dbg->create_info_size = size;
179
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +0800180 size += sizeof(src->pRequestedQueues[0]) * src->queueRecordCount;
181 size += sizeof(src->ppEnabledExtensionNames[0]) * src->extensionCount;
182 for (i = 0; i < src->extensionCount; i++) {
183 size += 1 +
184 strlen((const char *) src->ppEnabledExtensionNames[i]);
185 }
186
Chia-I Wu98dcfab2014-08-07 12:07:52 +0800187 dst = icd_alloc(size, 0, XGL_SYSTEM_ALLOC_DEBUG);
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +0800188 if (!dst)
189 return false;
190
191 memcpy(dst, src, sizeof(*src));
192
193 d = (uint8_t *) dst;
194 d += sizeof(*src);
195
196 size = sizeof(src->pRequestedQueues[0]) * src->queueRecordCount;
197 memcpy(d, src->pRequestedQueues, size);
198 dst->pRequestedQueues = (const XGL_DEVICE_QUEUE_CREATE_INFO *) d;
199 d += size;
200
201 size = sizeof(src->ppEnabledExtensionNames[0]) * src->extensionCount;
202 dst->ppEnabledExtensionNames = (const XGL_CHAR * const *) d;
203
204 for (i = 0; i < src->extensionCount; i++) {
205 const XGL_SIZE len =
206 strlen((const char *) src->ppEnabledExtensionNames[i]);
207
208 memcpy(d + size, src->ppEnabledExtensionNames[i], len + 1);
209 ((const XGL_CHAR **) d)[i] = (const XGL_CHAR *) (d + size);
210
211 size += len + 1;
212 }
Courtney Goeltzenleuchter05a60542014-08-15 14:54:34 -0600213 } else if (info.header->struct_type == XGL_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO) {
214 // TODO: What do we want to copy here?
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +0800215 }
216
217 return true;
218}
219
Chia-I Wu82f50aa2014-08-05 10:43:03 +0800220/**
Chia-I Wubbf2c932014-08-07 12:20:08 +0800221 * Create an intel_base_dbg. When dbg_size is non-zero, a buffer of that
Chia-I Wu660caf82014-08-07 10:54:26 +0800222 * size is allocated and zeroed.
Chia-I Wu82f50aa2014-08-05 10:43:03 +0800223 */
Courtney Goeltzenleuchterfb4fb532014-08-14 09:35:21 -0600224struct intel_base_dbg *intel_base_dbg_create(struct intel_dev *dev,
225 XGL_DBG_OBJECT_TYPE type,
Chia-I Wu660caf82014-08-07 10:54:26 +0800226 const void *create_info,
Chia-I Wubbf2c932014-08-07 12:20:08 +0800227 XGL_SIZE dbg_size)
Chia-I Wu82f50aa2014-08-05 10:43:03 +0800228{
Chia-I Wu660caf82014-08-07 10:54:26 +0800229 struct intel_base_dbg *dbg;
230
Chia-I Wubbf2c932014-08-07 12:20:08 +0800231 if (!dbg_size)
232 dbg_size = sizeof(*dbg);
Chia-I Wu660caf82014-08-07 10:54:26 +0800233
Chia-I Wubbf2c932014-08-07 12:20:08 +0800234 assert(dbg_size >= sizeof(*dbg));
Chia-I Wu660caf82014-08-07 10:54:26 +0800235
Chia-I Wubbf2c932014-08-07 12:20:08 +0800236 dbg = icd_alloc(dbg_size, 0, XGL_SYSTEM_ALLOC_DEBUG);
Chia-I Wu660caf82014-08-07 10:54:26 +0800237 if (!dbg)
238 return NULL;
239
Chia-I Wubbf2c932014-08-07 12:20:08 +0800240 memset(dbg, 0, dbg_size);
Chia-I Wu660caf82014-08-07 10:54:26 +0800241
Chia-I Wu82f50aa2014-08-05 10:43:03 +0800242 dbg->alloc_id = icd_get_allocator_id();
243 dbg->type = type;
Courtney Goeltzenleuchterfb4fb532014-08-14 09:35:21 -0600244 dbg->dev = dev;
Chia-I Wu82f50aa2014-08-05 10:43:03 +0800245
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +0800246 if (!base_dbg_copy_create_info(dbg, create_info)) {
247 icd_free(dbg);
248 return NULL;
Chia-I Wu82f50aa2014-08-05 10:43:03 +0800249 }
250
Chia-I Wu82f50aa2014-08-05 10:43:03 +0800251 return dbg;
252}
253
254void intel_base_dbg_destroy(struct intel_base_dbg *dbg)
255{
Chia-I Wu660caf82014-08-07 10:54:26 +0800256 if (dbg->tag)
257 icd_free(dbg->tag);
258
259 if (dbg->create_info)
260 icd_free(dbg->create_info);
261
Chia-I Wu82f50aa2014-08-05 10:43:03 +0800262 icd_free(dbg);
263}
Chia-I Wu53fc6aa2014-08-06 14:22:51 +0800264
Chia-I Wubbf2c932014-08-07 12:20:08 +0800265/**
266 * Create an intel_base. obj_size and dbg_size specify the real sizes of the
267 * object and the debug metadata. Memories are zeroed.
268 */
Courtney Goeltzenleuchterfb4fb532014-08-14 09:35:21 -0600269struct intel_base *intel_base_create(struct intel_dev *dev,
270 XGL_SIZE obj_size, bool debug,
Chia-I Wubbf2c932014-08-07 12:20:08 +0800271 XGL_DBG_OBJECT_TYPE type,
272 const void *create_info,
273 XGL_SIZE dbg_size)
274{
275 struct intel_base *base;
276
277 if (!obj_size)
278 obj_size = sizeof(*base);
279
280 assert(obj_size >= sizeof(*base));
281
282 base = icd_alloc(obj_size, 0, XGL_SYSTEM_ALLOC_API_OBJECT);
283 if (!base)
284 return NULL;
285
Courtney Goeltzenleuchterfb4fb532014-08-14 09:35:21 -0600286 if (dev == NULL) {
287 /*
288 * dev is NULL when we are creating the base device object
289 * Set dev now so that debug setup happens correctly
290 */
291 dev = (struct intel_dev *) base;
292 }
293
Chia-I Wubbf2c932014-08-07 12:20:08 +0800294 memset(base, 0, obj_size);
295
Chia-I Wu6a42c2a2014-08-19 14:36:47 +0800296 base->dispatch = intel_dispatch_get(debug);
Chia-I Wubbf2c932014-08-07 12:20:08 +0800297 if (debug) {
Courtney Goeltzenleuchterfb4fb532014-08-14 09:35:21 -0600298 base->dbg = intel_base_dbg_create(dev, type, create_info, dbg_size);
Chia-I Wubbf2c932014-08-07 12:20:08 +0800299 if (!base->dbg) {
300 icd_free(base);
301 return NULL;
302 }
303 }
Chia-I Wu6a42c2a2014-08-19 14:36:47 +0800304
Chia-I Wubbf2c932014-08-07 12:20:08 +0800305 base->get_info = intel_base_get_info;
306
307 return base;
308}
309
310void intel_base_destroy(struct intel_base *base)
311{
312 if (base->dbg)
313 intel_base_dbg_destroy(base->dbg);
314 icd_free(base);
315}
316
Chia-I Wu53fc6aa2014-08-06 14:22:51 +0800317XGL_RESULT XGLAPI intelDestroyObject(
318 XGL_OBJECT object)
319{
320 struct intel_obj *obj = intel_obj(object);
321
322 obj->destroy(obj);
323
324 return XGL_SUCCESS;
325}
326
327XGL_RESULT XGLAPI intelGetObjectInfo(
328 XGL_BASE_OBJECT object,
329 XGL_OBJECT_INFO_TYPE infoType,
330 XGL_SIZE* pDataSize,
331 XGL_VOID* pData)
332{
333 struct intel_base *base = intel_base(object);
Chia-I Wu53fc6aa2014-08-06 14:22:51 +0800334
Chia-I Wu26f0bd02014-08-07 10:38:40 +0800335 return base->get_info(base, infoType, pDataSize, pData);
Chia-I Wu53fc6aa2014-08-06 14:22:51 +0800336}
337
338XGL_RESULT XGLAPI intelBindObjectMemory(
339 XGL_OBJECT object,
340 XGL_GPU_MEMORY mem,
341 XGL_GPU_SIZE offset)
342{
343 struct intel_obj *obj = intel_obj(object);
344
345 obj->mem = mem;
346 obj->offset = offset;
347
348 return XGL_SUCCESS;
349}
Chia-I Wu7ec9f342014-08-19 10:47:53 +0800350
351XGL_RESULT XGLAPI intelDbgSetObjectTag(
352 XGL_BASE_OBJECT object,
353 XGL_SIZE tagSize,
354 const XGL_VOID* pTag)
355{
356 struct intel_base *base = intel_base(object);
357 struct intel_base_dbg *dbg = base->dbg;
358 void *tag;
359
360 if (!dbg)
361 return XGL_SUCCESS;
362
363 tag = icd_alloc(tagSize, 0, XGL_SYSTEM_ALLOC_DEBUG);
364 if (!tag)
365 return XGL_ERROR_OUT_OF_MEMORY;
366
367 memcpy(tag, pTag, tagSize);
368
369 if (dbg->tag)
370 icd_free(dbg->tag);
371
372 dbg->tag = tag;
373 dbg->tag_size = tagSize;
374
375 return XGL_SUCCESS;
376}