blob: 7bbd0541b420ca0826ae37e7ae551b19d7271490 [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"
27#include "obj.h"
28
29/**
30 * Return true if an (not so) arbitrary pointer casted to intel_base points to
31 * a valid intel_base. This assumes at least the first sizeof(void*) bytes of
32 * the address are accessible, and they does not happen to be our magic
33 * values.
34 */
35bool intel_base_is_valid(const struct intel_base *base)
36{
37 if (base->dispatch != &intel_normal_dispatch_table &&
38 base->dispatch != &intel_debug_dispatch_table)
39 return false;
40
41 return !intel_gpu_is_valid((const struct intel_gpu *) base);
42}
43
Chia-I Wu26f0bd02014-08-07 10:38:40 +080044XGL_RESULT intel_base_get_info(struct intel_base *base, int type,
45 XGL_SIZE *size, XGL_VOID *data)
46{
47 XGL_RESULT ret = XGL_SUCCESS;
48 XGL_SIZE s;
49
50 switch (type) {
51 case XGL_INFO_TYPE_MEMORY_REQUIREMENTS:
52 s = sizeof(XGL_MEMORY_REQUIREMENTS);
53 memset(data, 0, s);
54 *size = s;
55 break;
56 default:
57 ret = XGL_ERROR_INVALID_VALUE;
58 break;
59 }
60
61 return ret;
62}
63
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +080064static bool base_dbg_copy_create_info(struct intel_base_dbg *dbg,
65 const void *create_info)
66{
67 const union {
68 const void *ptr;
69 const struct {
70 XGL_STRUCTURE_TYPE struct_type;
71 XGL_VOID *next;
72 } *header;
73 } info = { .ptr = create_info };
74 XGL_SIZE shallow_copy = 0;
75
76 if (!create_info)
77 return true;
78
79 switch (info.header->struct_type) {
80 case XGL_STRUCTURE_TYPE_DEVICE_CREATE_INFO:
81 assert(dbg->type == XGL_DBG_OBJECT_DEVICE);
82 break;
83 case XGL_STRUCTURE_TYPE_MEMORY_ALLOC_INFO:
84 assert(dbg->type == XGL_DBG_OBJECT_GPU_MEMORY);
85 shallow_copy = sizeof(XGL_MEMORY_ALLOC_INFO);
86 break;
87 case XGL_STRUCTURE_TYPE_EVENT_CREATE_INFO:
88 assert(dbg->type == XGL_DBG_OBJECT_EVENT);
89 shallow_copy = sizeof(XGL_EVENT_CREATE_INFO);
90 break;
91 case XGL_STRUCTURE_TYPE_FENCE_CREATE_INFO:
92 assert(dbg->type == XGL_DBG_OBJECT_FENCE);
93 shallow_copy = sizeof(XGL_FENCE_CREATE_INFO);
94 break;
Courtney Goeltzenleuchter850d12c2014-08-07 18:13:10 -060095 case XGL_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO:
96 assert(dbg->type == XGL_DBG_OBJECT_QUERY_POOL);
97 shallow_copy = sizeof(XGL_QUERY_POOL_CREATE_INFO);
98 break;
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +080099 default:
100 return false;
101 break;
102 }
103
104 if (shallow_copy) {
105 assert(!info.header->next);
106
107 dbg->create_info = icd_alloc(shallow_copy, 0, XGL_SYSTEM_ALLOC_DEBUG);
108 if (!dbg->create_info)
109 return false;
110
111 memcpy(dbg->create_info, create_info, shallow_copy);
112 } else if (info.header->struct_type ==
113 XGL_STRUCTURE_TYPE_DEVICE_CREATE_INFO) {
114 const XGL_DEVICE_CREATE_INFO *src = info.ptr;
115 XGL_DEVICE_CREATE_INFO *dst;
116 uint8_t *d;
117 XGL_SIZE size;
118 XGL_UINT i;
119
120 size = sizeof(*src);
121 size += sizeof(src->pRequestedQueues[0]) * src->queueRecordCount;
122 size += sizeof(src->ppEnabledExtensionNames[0]) * src->extensionCount;
123 for (i = 0; i < src->extensionCount; i++) {
124 size += 1 +
125 strlen((const char *) src->ppEnabledExtensionNames[i]);
126 }
127
Chia-I Wu98dcfab2014-08-07 12:07:52 +0800128 dst = icd_alloc(size, 0, XGL_SYSTEM_ALLOC_DEBUG);
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +0800129 if (!dst)
130 return false;
131
132 memcpy(dst, src, sizeof(*src));
133
134 d = (uint8_t *) dst;
135 d += sizeof(*src);
136
137 size = sizeof(src->pRequestedQueues[0]) * src->queueRecordCount;
138 memcpy(d, src->pRequestedQueues, size);
139 dst->pRequestedQueues = (const XGL_DEVICE_QUEUE_CREATE_INFO *) d;
140 d += size;
141
142 size = sizeof(src->ppEnabledExtensionNames[0]) * src->extensionCount;
143 dst->ppEnabledExtensionNames = (const XGL_CHAR * const *) d;
144
145 for (i = 0; i < src->extensionCount; i++) {
146 const XGL_SIZE len =
147 strlen((const char *) src->ppEnabledExtensionNames[i]);
148
149 memcpy(d + size, src->ppEnabledExtensionNames[i], len + 1);
150 ((const XGL_CHAR **) d)[i] = (const XGL_CHAR *) (d + size);
151
152 size += len + 1;
153 }
154 }
155
156 return true;
157}
158
Chia-I Wu82f50aa2014-08-05 10:43:03 +0800159/**
Chia-I Wubbf2c932014-08-07 12:20:08 +0800160 * Create an intel_base_dbg. When dbg_size is non-zero, a buffer of that
Chia-I Wu660caf82014-08-07 10:54:26 +0800161 * size is allocated and zeroed.
Chia-I Wu82f50aa2014-08-05 10:43:03 +0800162 */
Chia-I Wu660caf82014-08-07 10:54:26 +0800163struct intel_base_dbg *intel_base_dbg_create(XGL_DBG_OBJECT_TYPE type,
164 const void *create_info,
Chia-I Wubbf2c932014-08-07 12:20:08 +0800165 XGL_SIZE dbg_size)
Chia-I Wu82f50aa2014-08-05 10:43:03 +0800166{
Chia-I Wu660caf82014-08-07 10:54:26 +0800167 struct intel_base_dbg *dbg;
168
Chia-I Wubbf2c932014-08-07 12:20:08 +0800169 if (!dbg_size)
170 dbg_size = sizeof(*dbg);
Chia-I Wu660caf82014-08-07 10:54:26 +0800171
Chia-I Wubbf2c932014-08-07 12:20:08 +0800172 assert(dbg_size >= sizeof(*dbg));
Chia-I Wu660caf82014-08-07 10:54:26 +0800173
Chia-I Wubbf2c932014-08-07 12:20:08 +0800174 dbg = icd_alloc(dbg_size, 0, XGL_SYSTEM_ALLOC_DEBUG);
Chia-I Wu660caf82014-08-07 10:54:26 +0800175 if (!dbg)
176 return NULL;
177
Chia-I Wubbf2c932014-08-07 12:20:08 +0800178 memset(dbg, 0, dbg_size);
Chia-I Wu660caf82014-08-07 10:54:26 +0800179
Chia-I Wu82f50aa2014-08-05 10:43:03 +0800180 dbg->alloc_id = icd_get_allocator_id();
181 dbg->type = type;
182
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +0800183 if (!base_dbg_copy_create_info(dbg, create_info)) {
184 icd_free(dbg);
185 return NULL;
Chia-I Wu82f50aa2014-08-05 10:43:03 +0800186 }
187
Chia-I Wu82f50aa2014-08-05 10:43:03 +0800188 return dbg;
189}
190
191void intel_base_dbg_destroy(struct intel_base_dbg *dbg)
192{
Chia-I Wu660caf82014-08-07 10:54:26 +0800193 if (dbg->tag)
194 icd_free(dbg->tag);
195
196 if (dbg->create_info)
197 icd_free(dbg->create_info);
198
Chia-I Wu82f50aa2014-08-05 10:43:03 +0800199 icd_free(dbg);
200}
Chia-I Wu53fc6aa2014-08-06 14:22:51 +0800201
Chia-I Wubbf2c932014-08-07 12:20:08 +0800202/**
203 * Create an intel_base. obj_size and dbg_size specify the real sizes of the
204 * object and the debug metadata. Memories are zeroed.
205 */
206struct intel_base *intel_base_create(XGL_SIZE obj_size, bool debug,
207 XGL_DBG_OBJECT_TYPE type,
208 const void *create_info,
209 XGL_SIZE dbg_size)
210{
211 struct intel_base *base;
212
213 if (!obj_size)
214 obj_size = sizeof(*base);
215
216 assert(obj_size >= sizeof(*base));
217
218 base = icd_alloc(obj_size, 0, XGL_SYSTEM_ALLOC_API_OBJECT);
219 if (!base)
220 return NULL;
221
222 memset(base, 0, obj_size);
223
224 if (debug) {
225 base->dispatch = &intel_debug_dispatch_table;
226 base->dbg = intel_base_dbg_create(type, create_info, dbg_size);
227 if (!base->dbg) {
228 icd_free(base);
229 return NULL;
230 }
231 }
232 else {
233 base->dispatch = &intel_normal_dispatch_table;
234 }
235 base->get_info = intel_base_get_info;
236
237 return base;
238}
239
240void intel_base_destroy(struct intel_base *base)
241{
242 if (base->dbg)
243 intel_base_dbg_destroy(base->dbg);
244 icd_free(base);
245}
246
Chia-I Wu53fc6aa2014-08-06 14:22:51 +0800247XGL_RESULT XGLAPI intelDestroyObject(
248 XGL_OBJECT object)
249{
250 struct intel_obj *obj = intel_obj(object);
251
252 obj->destroy(obj);
253
254 return XGL_SUCCESS;
255}
256
257XGL_RESULT XGLAPI intelGetObjectInfo(
258 XGL_BASE_OBJECT object,
259 XGL_OBJECT_INFO_TYPE infoType,
260 XGL_SIZE* pDataSize,
261 XGL_VOID* pData)
262{
263 struct intel_base *base = intel_base(object);
Chia-I Wu53fc6aa2014-08-06 14:22:51 +0800264
Chia-I Wu26f0bd02014-08-07 10:38:40 +0800265 return base->get_info(base, infoType, pDataSize, pData);
Chia-I Wu53fc6aa2014-08-06 14:22:51 +0800266}
267
268XGL_RESULT XGLAPI intelBindObjectMemory(
269 XGL_OBJECT object,
270 XGL_GPU_MEMORY mem,
271 XGL_GPU_SIZE offset)
272{
273 struct intel_obj *obj = intel_obj(object);
274
275 obj->mem = mem;
276 obj->offset = offset;
277
278 return XGL_SUCCESS;
279}