blob: a840a9dfb527bc8ee4fd4a2f123d6c31bfa09ce7 [file] [log] [blame]
Chia-I Wu214dac62014-08-05 11:07:40 +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.
Chia-I Wu44e42362014-09-02 08:32:09 +080023 *
24 * Authors:
25 * Chia-I Wu <olv@lunarg.com>
Chia-I Wu214dac62014-08-05 11:07:40 +080026 */
27
28#include <stdio.h>
29#include <sys/types.h>
30#include <sys/stat.h>
31#include <fcntl.h>
32#include <unistd.h>
33
34#include "genhw/genhw.h"
Chia-I Wude2bb862014-08-19 14:32:47 +080035#include "dispatch.h"
Chia-I Wuec841722014-08-25 22:36:01 +080036#include "queue.h"
Chia-I Wu214dac62014-08-05 11:07:40 +080037#include "gpu.h"
38
39static const char *gpu_get_name(const struct intel_gpu *gpu)
40{
41 const char *name = NULL;
42
43 if (gen_is_hsw(gpu->devid)) {
44 if (gen_is_desktop(gpu->devid))
45 name = "Intel(R) Haswell Desktop";
46 else if (gen_is_mobile(gpu->devid))
47 name = "Intel(R) Haswell Mobile";
48 else if (gen_is_server(gpu->devid))
49 name = "Intel(R) Haswell Server";
50 }
51 else if (gen_is_ivb(gpu->devid)) {
52 if (gen_is_desktop(gpu->devid))
53 name = "Intel(R) Ivybridge Desktop";
54 else if (gen_is_mobile(gpu->devid))
55 name = "Intel(R) Ivybridge Mobile";
56 else if (gen_is_server(gpu->devid))
57 name = "Intel(R) Ivybridge Server";
58 }
59 else if (gen_is_snb(gpu->devid)) {
60 if (gen_is_desktop(gpu->devid))
61 name = "Intel(R) Sandybridge Desktop";
62 else if (gen_is_mobile(gpu->devid))
63 name = "Intel(R) Sandybridge Mobile";
64 else if (gen_is_server(gpu->devid))
65 name = "Intel(R) Sandybridge Server";
66 }
67
68 if (!name)
69 name = "Unknown Intel Chipset";
70
71 return name;
72}
73
74static int gpu_open_internal(struct intel_gpu *gpu)
75{
76 if (gpu->fd_internal < 0) {
77 gpu->fd_internal = open(gpu->path, O_RDWR);
78 if (gpu->fd_internal < 0) {
79 icd_log(XGL_DBG_MSG_ERROR, XGL_VALIDATION_LEVEL_0, NULL, 0,
80 0, "failed to open %s", gpu->path);
81 }
82 }
83
84 return gpu->fd_internal;
85}
86
87static void gpu_close_internal(struct intel_gpu *gpu)
88{
89 if (gpu->fd_internal >= 0) {
90 close(gpu->fd_internal);
91 gpu->fd_internal = -1;
92 }
93}
94
95static struct intel_gpu *gpu_create(int gen, int devid, const char *path)
96{
97 struct intel_gpu *gpu;
98 size_t path_len;
99
100 gpu = icd_alloc(sizeof(*gpu), 0, XGL_SYSTEM_ALLOC_API_OBJECT);
101 if (!gpu)
102 return NULL;
103
104 memset(gpu, 0, sizeof(*gpu));
105
106 /* debug layer is always enabled for intel_gpu */
Chia-I Wu6a42c2a2014-08-19 14:36:47 +0800107 gpu->dispatch = intel_dispatch_get(true);
Chia-I Wu214dac62014-08-05 11:07:40 +0800108
109 gpu->devid = devid;
110
111 path_len = strlen(path);
112 gpu->path = icd_alloc(path_len + 1, 0, XGL_SYSTEM_ALLOC_INTERNAL);
113 if (!gpu->path) {
114 icd_free(gpu);
115 return NULL;
116 }
117 memcpy(gpu->path, path, path_len + 1);
118
119 gpu->gen_opaque = gen;
120
Chia-I Wu960f1952014-08-28 23:27:10 +0800121 switch (intel_gpu_gen(gpu)) {
122 case INTEL_GEN(7.5):
123 gpu->gt = gen_get_hsw_gt(devid);
124 break;
125 case INTEL_GEN(7):
126 gpu->gt = gen_get_ivb_gt(devid);
127 break;
128 case INTEL_GEN(6):
129 gpu->gt = gen_get_snb_gt(devid);
130 break;
131 }
132
Chia-I Wu214dac62014-08-05 11:07:40 +0800133 /* 8192 dwords */
Chia-I Wud6109bb2014-08-21 09:12:19 +0800134 gpu->max_batch_buffer_size = sizeof(uint32_t) * 8192;
135
136 /* the winsys is prepared for one reloc every two dwords, then minus 2 */
137 gpu->batch_buffer_reloc_count =
138 gpu->max_batch_buffer_size / sizeof(uint32_t) / 2 - 2;
Chia-I Wu214dac62014-08-05 11:07:40 +0800139
140 gpu->fd_internal = -1;
141 gpu->fd = -1;
142
143 return gpu;
144}
145
146static void gpu_destroy(struct intel_gpu *gpu)
147{
148 gpu_close_internal(gpu);
149 icd_free(gpu->path);
150 icd_free(gpu);
151}
152
153static struct intel_gpu *intel_gpus;
154
155/**
156 * Return true if \p gpu is a valid intel_gpu.
157 */
158bool intel_gpu_is_valid(const struct intel_gpu *gpu)
159{
160 const struct intel_gpu *iter = intel_gpus;
161
162 while (iter) {
163 if (iter == gpu)
164 return true;
165 iter = iter->next;
166 }
167
168 return false;
169}
170
171static int devid_to_gen(int devid)
172{
173 int gen;
174
175 if (gen_is_hsw(devid))
176 gen = INTEL_GEN(7.5);
177 else if (gen_is_ivb(devid))
178 gen = INTEL_GEN(7);
179 else if (gen_is_snb(devid))
180 gen = INTEL_GEN(6);
181 else
182 gen = -1;
183
Chia-I Wubfce58e2014-08-28 23:23:33 +0800184#ifdef INTEL_GEN_SPECIALIZED
185 if (gen != INTEL_GEN(INTEL_GEN_SPECIALIZED))
186 gen = -1;
187#endif
188
Chia-I Wu214dac62014-08-05 11:07:40 +0800189 return gen;
190}
191
192XGL_RESULT intel_gpu_add(int devid, const char *path,
193 struct intel_gpu **gpu_ret)
194{
195 const int gen = devid_to_gen(devid);
196 struct intel_gpu *gpu;
197
198 if (gen < 0) {
199 icd_log(XGL_DBG_MSG_WARNING, XGL_VALIDATION_LEVEL_0, XGL_NULL_HANDLE,
200 0, 0, "unsupported device id 0x%04x", devid);
201 return XGL_ERROR_INITIALIZATION_FAILED;
202 }
203
204 gpu = gpu_create(gen, devid, path);
205 if (!gpu)
206 return XGL_ERROR_OUT_OF_MEMORY;
207
208 gpu->next = intel_gpus;
209 intel_gpus = gpu;
210
211 *gpu_ret = gpu;
212
213 return XGL_SUCCESS;
214}
215
216void intel_gpu_remove_all(void)
217{
218 struct intel_gpu *gpu = intel_gpus;
219
220 while (gpu) {
221 struct intel_gpu *next = gpu->next;
222
223 gpu_destroy(gpu);
224 gpu = next;
225 }
226
227 intel_gpus = NULL;
228}
229
230struct intel_gpu *intel_gpu_get_list(void)
231{
232 return intel_gpus;
233}
234
235void intel_gpu_get_props(const struct intel_gpu *gpu,
236 XGL_PHYSICAL_GPU_PROPERTIES *props)
237{
238 const char *name;
239 size_t name_len;
240
241 props->structSize = sizeof(*props);
242
243 props->apiVersion = INTEL_API_VERSION;
244 props->driverVersion = INTEL_DRIVER_VERSION;
245
246 props->vendorId = 0x8086;
247 props->deviceId = gpu->devid;
248
249 props->gpuType = XGL_GPU_TYPE_INTEGRATED;
250
251 /* copy GPU name */
252 name = gpu_get_name(gpu);
253 name_len = strlen(name);
254 if (name_len > sizeof(props->gpuName) - 1)
255 name_len = sizeof(props->gpuName) - 1;
256 memcpy(props->gpuName, name, name_len);
257 props->gpuName[name_len] = '\0';
258
Chia-I Wud6109bb2014-08-21 09:12:19 +0800259 props->maxMemRefsPerSubmission = gpu->batch_buffer_reloc_count;
Chia-I Wu214dac62014-08-05 11:07:40 +0800260
261 props->virtualMemPageSize = 4096;
262
263 /* no size limit, but no bounded buffer could exceed 2GB */
264 props->maxInlineMemoryUpdateSize = 2u << 30;
265
266 props->maxBoundDescriptorSets = 1;
267 props->maxThreadGroupSize = 512;
268
269 /* incremented every 80ns */
270 props->timestampFrequency = 1000 * 1000 * 1000 / 80;
271
272 props->multiColorAttachmentClears = false;
273}
274
275void intel_gpu_get_perf(const struct intel_gpu *gpu,
276 XGL_PHYSICAL_GPU_PERFORMANCE *perf)
277{
278 /* TODO */
279 perf->maxGpuClock = 1.0f;
280 perf->aluPerClock = 1.0f;
281 perf->texPerClock = 1.0f;
282 perf->primsPerClock = 1.0f;
283 perf->pixelsPerClock = 1.0f;
284}
285
286void intel_gpu_get_queue_props(const struct intel_gpu *gpu,
287 enum intel_gpu_engine_type engine,
288 XGL_PHYSICAL_GPU_QUEUE_PROPERTIES *props)
289{
290 props->structSize = sizeof(*props);
291
292 switch (engine) {
293 case INTEL_GPU_ENGINE_3D:
294 props->queueFlags = XGL_QUEUE_GRAPHICS_BIT | XGL_QUEUE_COMPUTE_BIT;
295 props->queueCount = 1;
Chia-I Wuec841722014-08-25 22:36:01 +0800296 props->maxAtomicCounters = INTEL_QUEUE_ATOMIC_COUNTER_COUNT;
Chia-I Wu214dac62014-08-05 11:07:40 +0800297 props->supportsTimestamps = true;
298 break;
299 default:
300 assert(!"unknown engine type");
301 return;
302 }
303}
304
305void intel_gpu_get_memory_props(const struct intel_gpu *gpu,
306 XGL_PHYSICAL_GPU_MEMORY_PROPERTIES *props)
307{
308 props->structSize = sizeof(*props);
309
310 props->supportsMigration = false;
311
312 /* no kernel support yet */
313 props->supportsVirtualMemoryRemapping = false;
314
Chia-I Wu54c0c4b2014-08-06 13:48:25 +0800315 /* no winsys support for DRM_I915_GEM_USERPTR yet */
316 props->supportsPinning = false;
Chia-I Wu214dac62014-08-05 11:07:40 +0800317}
318
319XGL_RESULT intel_gpu_open(struct intel_gpu *gpu)
320{
321 gpu->fd = gpu_open_internal(gpu);
322
323 return (gpu->fd >= 0) ? XGL_SUCCESS : XGL_ERROR_UNKNOWN;
324}
325
326void intel_gpu_close(struct intel_gpu *gpu)
327{
328 gpu->fd = -1;
329 gpu_close_internal(gpu);
330}
331
332bool intel_gpu_has_extension(const struct intel_gpu *gpu, const char *ext)
333{
334 return false;
335}
Chia-I Wubec90a02014-08-06 12:33:03 +0800336
337XGL_RESULT XGLAPI intelGetGpuInfo(
338 XGL_PHYSICAL_GPU gpu_,
339 XGL_PHYSICAL_GPU_INFO_TYPE infoType,
340 XGL_SIZE* pDataSize,
341 XGL_VOID* pData)
342{
343 const struct intel_gpu *gpu = intel_gpu(gpu_);
344 XGL_RESULT ret = XGL_SUCCESS;
345
346 switch (infoType) {
347 case XGL_INFO_TYPE_PHYSICAL_GPU_PROPERTIES:
348 if (pData == NULL) {
349 return XGL_ERROR_INVALID_POINTER;
350 }
351 *pDataSize = sizeof(XGL_PHYSICAL_GPU_PROPERTIES);
352 intel_gpu_get_props(gpu, pData);
353 break;
354
355 case XGL_INFO_TYPE_PHYSICAL_GPU_PERFORMANCE:
356 if (pData == NULL) {
357 return XGL_ERROR_INVALID_POINTER;
358 }
359 *pDataSize = sizeof(XGL_PHYSICAL_GPU_PERFORMANCE);
360 intel_gpu_get_perf(gpu, pData);
361 break;
362
363 case XGL_INFO_TYPE_PHYSICAL_GPU_QUEUE_PROPERTIES:
364 /*
365 * XGL Programmers guide, page 33:
366 * to determine the data size an application calls
367 * xglGetGpuInfo() with a NULL data pointer. The
368 * expected data size for all queue property structures
369 * is returned in pDataSize
370 */
371 *pDataSize = sizeof(XGL_PHYSICAL_GPU_QUEUE_PROPERTIES) *
372 INTEL_GPU_ENGINE_COUNT;
373 if (pData != NULL) {
374 XGL_PHYSICAL_GPU_QUEUE_PROPERTIES *dst = pData;
375 int engine;
376
377 for (engine = 0; engine < INTEL_GPU_ENGINE_COUNT; engine++) {
378 intel_gpu_get_queue_props(gpu, engine, dst);
379 dst++;
380 }
381 }
382 break;
383
384 case XGL_INFO_TYPE_PHYSICAL_GPU_MEMORY_PROPERTIES:
385 if (pData == NULL) {
386 return XGL_ERROR_INVALID_POINTER;
387 }
388 *pDataSize = sizeof(XGL_PHYSICAL_GPU_MEMORY_PROPERTIES);
389 intel_gpu_get_memory_props(gpu, pData);
390 break;
391
392 default:
393 ret = XGL_ERROR_INVALID_VALUE;
394 }
395
396 return ret;
397}
398
399XGL_RESULT XGLAPI intelGetExtensionSupport(
400 XGL_PHYSICAL_GPU gpu_,
401 const XGL_CHAR* pExtName)
402{
403 struct intel_gpu *gpu = intel_gpu(gpu_);
404
405 return (intel_gpu_has_extension(gpu, (const char *) pExtName)) ?
406 XGL_SUCCESS : XGL_ERROR_INVALID_EXTENSION;
407}
Chia-I Wu251e7d92014-08-19 13:35:42 +0800408
409XGL_RESULT XGLAPI intelGetMultiGpuCompatibility(
Chia-I Wu452f5e82014-08-31 12:39:05 +0800410 XGL_PHYSICAL_GPU gpu0_,
411 XGL_PHYSICAL_GPU gpu1_,
Chia-I Wu251e7d92014-08-19 13:35:42 +0800412 XGL_GPU_COMPATIBILITY_INFO* pInfo)
413{
Chia-I Wu452f5e82014-08-31 12:39:05 +0800414 const struct intel_gpu *gpu0 = intel_gpu(gpu0_);
415 const struct intel_gpu *gpu1 = intel_gpu(gpu1_);
416 XGL_FLAGS compat = XGL_GPU_COMPAT_IQ_MATCH_BIT |
417 XGL_GPU_COMPAT_PEER_TRANSFER_BIT |
418 XGL_GPU_COMPAT_SHARED_MEMORY_BIT |
419 XGL_GPU_COMPAT_SHARED_GPU0_DISPLAY_BIT |
420 XGL_GPU_COMPAT_SHARED_GPU1_DISPLAY_BIT;
421
422 if (intel_gpu_gen(gpu0) == intel_gpu_gen(gpu1))
423 compat |= XGL_GPU_COMPAT_ASIC_FEATURES_BIT;
424
425 pInfo->compatibilityFlags = compat;
426
427 return XGL_SUCCESS;
Chia-I Wu251e7d92014-08-19 13:35:42 +0800428}