blob: 1481b630fe03269e0ce9bae264d5fab1ee02cf74 [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 Wu1f8fc7c2014-08-07 11:09:11 +0800104 default:
Courtney Goeltzenleuchterfb4fb532014-08-14 09:35:21 -0600105 // log debug message regarding invalid struct_type?
106 intel_dev_log(dbg->dev, XGL_DBG_MSG_ERROR,
107 XGL_VALIDATION_LEVEL_0, XGL_NULL_HANDLE, 0, 0,
108 "Invalid Create Info type: 0x%x", info.header->struct_type);
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +0800109 return false;
110 break;
111 }
112
113 if (shallow_copy) {
114 assert(!info.header->next);
115
116 dbg->create_info = icd_alloc(shallow_copy, 0, XGL_SYSTEM_ALLOC_DEBUG);
117 if (!dbg->create_info)
118 return false;
119
120 memcpy(dbg->create_info, create_info, shallow_copy);
121 } else if (info.header->struct_type ==
122 XGL_STRUCTURE_TYPE_DEVICE_CREATE_INFO) {
123 const XGL_DEVICE_CREATE_INFO *src = info.ptr;
124 XGL_DEVICE_CREATE_INFO *dst;
125 uint8_t *d;
126 XGL_SIZE size;
127 XGL_UINT i;
128
129 size = sizeof(*src);
130 size += sizeof(src->pRequestedQueues[0]) * src->queueRecordCount;
131 size += sizeof(src->ppEnabledExtensionNames[0]) * src->extensionCount;
132 for (i = 0; i < src->extensionCount; i++) {
133 size += 1 +
134 strlen((const char *) src->ppEnabledExtensionNames[i]);
135 }
136
Chia-I Wu98dcfab2014-08-07 12:07:52 +0800137 dst = icd_alloc(size, 0, XGL_SYSTEM_ALLOC_DEBUG);
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +0800138 if (!dst)
139 return false;
140
141 memcpy(dst, src, sizeof(*src));
142
143 d = (uint8_t *) dst;
144 d += sizeof(*src);
145
146 size = sizeof(src->pRequestedQueues[0]) * src->queueRecordCount;
147 memcpy(d, src->pRequestedQueues, size);
148 dst->pRequestedQueues = (const XGL_DEVICE_QUEUE_CREATE_INFO *) d;
149 d += size;
150
151 size = sizeof(src->ppEnabledExtensionNames[0]) * src->extensionCount;
152 dst->ppEnabledExtensionNames = (const XGL_CHAR * const *) d;
153
154 for (i = 0; i < src->extensionCount; i++) {
155 const XGL_SIZE len =
156 strlen((const char *) src->ppEnabledExtensionNames[i]);
157
158 memcpy(d + size, src->ppEnabledExtensionNames[i], len + 1);
159 ((const XGL_CHAR **) d)[i] = (const XGL_CHAR *) (d + size);
160
161 size += len + 1;
162 }
163 }
164
165 return true;
166}
167
Chia-I Wu82f50aa2014-08-05 10:43:03 +0800168/**
Chia-I Wubbf2c932014-08-07 12:20:08 +0800169 * Create an intel_base_dbg. When dbg_size is non-zero, a buffer of that
Chia-I Wu660caf82014-08-07 10:54:26 +0800170 * size is allocated and zeroed.
Chia-I Wu82f50aa2014-08-05 10:43:03 +0800171 */
Courtney Goeltzenleuchterfb4fb532014-08-14 09:35:21 -0600172struct intel_base_dbg *intel_base_dbg_create(struct intel_dev *dev,
173 XGL_DBG_OBJECT_TYPE type,
Chia-I Wu660caf82014-08-07 10:54:26 +0800174 const void *create_info,
Chia-I Wubbf2c932014-08-07 12:20:08 +0800175 XGL_SIZE dbg_size)
Chia-I Wu82f50aa2014-08-05 10:43:03 +0800176{
Chia-I Wu660caf82014-08-07 10:54:26 +0800177 struct intel_base_dbg *dbg;
178
Chia-I Wubbf2c932014-08-07 12:20:08 +0800179 if (!dbg_size)
180 dbg_size = sizeof(*dbg);
Chia-I Wu660caf82014-08-07 10:54:26 +0800181
Chia-I Wubbf2c932014-08-07 12:20:08 +0800182 assert(dbg_size >= sizeof(*dbg));
Chia-I Wu660caf82014-08-07 10:54:26 +0800183
Chia-I Wubbf2c932014-08-07 12:20:08 +0800184 dbg = icd_alloc(dbg_size, 0, XGL_SYSTEM_ALLOC_DEBUG);
Chia-I Wu660caf82014-08-07 10:54:26 +0800185 if (!dbg)
186 return NULL;
187
Chia-I Wubbf2c932014-08-07 12:20:08 +0800188 memset(dbg, 0, dbg_size);
Chia-I Wu660caf82014-08-07 10:54:26 +0800189
Chia-I Wu82f50aa2014-08-05 10:43:03 +0800190 dbg->alloc_id = icd_get_allocator_id();
191 dbg->type = type;
Courtney Goeltzenleuchterfb4fb532014-08-14 09:35:21 -0600192 dbg->dev = dev;
Chia-I Wu82f50aa2014-08-05 10:43:03 +0800193
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +0800194 if (!base_dbg_copy_create_info(dbg, create_info)) {
195 icd_free(dbg);
196 return NULL;
Chia-I Wu82f50aa2014-08-05 10:43:03 +0800197 }
198
Chia-I Wu82f50aa2014-08-05 10:43:03 +0800199 return dbg;
200}
201
202void intel_base_dbg_destroy(struct intel_base_dbg *dbg)
203{
Chia-I Wu660caf82014-08-07 10:54:26 +0800204 if (dbg->tag)
205 icd_free(dbg->tag);
206
207 if (dbg->create_info)
208 icd_free(dbg->create_info);
209
Chia-I Wu82f50aa2014-08-05 10:43:03 +0800210 icd_free(dbg);
211}
Chia-I Wu53fc6aa2014-08-06 14:22:51 +0800212
Chia-I Wubbf2c932014-08-07 12:20:08 +0800213/**
214 * Create an intel_base. obj_size and dbg_size specify the real sizes of the
215 * object and the debug metadata. Memories are zeroed.
216 */
Courtney Goeltzenleuchterfb4fb532014-08-14 09:35:21 -0600217struct intel_base *intel_base_create(struct intel_dev *dev,
218 XGL_SIZE obj_size, bool debug,
Chia-I Wubbf2c932014-08-07 12:20:08 +0800219 XGL_DBG_OBJECT_TYPE type,
220 const void *create_info,
221 XGL_SIZE dbg_size)
222{
223 struct intel_base *base;
224
225 if (!obj_size)
226 obj_size = sizeof(*base);
227
228 assert(obj_size >= sizeof(*base));
229
230 base = icd_alloc(obj_size, 0, XGL_SYSTEM_ALLOC_API_OBJECT);
231 if (!base)
232 return NULL;
233
Courtney Goeltzenleuchterfb4fb532014-08-14 09:35:21 -0600234 if (dev == NULL) {
235 /*
236 * dev is NULL when we are creating the base device object
237 * Set dev now so that debug setup happens correctly
238 */
239 dev = (struct intel_dev *) base;
240 }
241
Chia-I Wubbf2c932014-08-07 12:20:08 +0800242 memset(base, 0, obj_size);
243
244 if (debug) {
245 base->dispatch = &intel_debug_dispatch_table;
Courtney Goeltzenleuchterfb4fb532014-08-14 09:35:21 -0600246 base->dbg = intel_base_dbg_create(dev, type, create_info, dbg_size);
Chia-I Wubbf2c932014-08-07 12:20:08 +0800247 if (!base->dbg) {
248 icd_free(base);
249 return NULL;
250 }
251 }
252 else {
253 base->dispatch = &intel_normal_dispatch_table;
254 }
255 base->get_info = intel_base_get_info;
256
257 return base;
258}
259
260void intel_base_destroy(struct intel_base *base)
261{
262 if (base->dbg)
263 intel_base_dbg_destroy(base->dbg);
264 icd_free(base);
265}
266
Chia-I Wu53fc6aa2014-08-06 14:22:51 +0800267XGL_RESULT XGLAPI intelDestroyObject(
268 XGL_OBJECT object)
269{
270 struct intel_obj *obj = intel_obj(object);
271
272 obj->destroy(obj);
273
274 return XGL_SUCCESS;
275}
276
277XGL_RESULT XGLAPI intelGetObjectInfo(
278 XGL_BASE_OBJECT object,
279 XGL_OBJECT_INFO_TYPE infoType,
280 XGL_SIZE* pDataSize,
281 XGL_VOID* pData)
282{
283 struct intel_base *base = intel_base(object);
Chia-I Wu53fc6aa2014-08-06 14:22:51 +0800284
Chia-I Wu26f0bd02014-08-07 10:38:40 +0800285 return base->get_info(base, infoType, pDataSize, pData);
Chia-I Wu53fc6aa2014-08-06 14:22:51 +0800286}
287
288XGL_RESULT XGLAPI intelBindObjectMemory(
289 XGL_OBJECT object,
290 XGL_GPU_MEMORY mem,
291 XGL_GPU_SIZE offset)
292{
293 struct intel_obj *obj = intel_obj(object);
294
295 obj->mem = mem;
296 obj->offset = offset;
297
298 return XGL_SUCCESS;
299}