1 /**********************************************************
2 * Copyright 2009 VMware, Inc. All rights reserved.
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 **********************************************************/
29 * Wrappers for DRM ioctl functionlaity used by the rest of the vmw
32 * Based on svgaicd_escape.c
37 #include "util/u_memory.h"
38 #include "util/u_math.h"
39 #include "svgadump/svga_dump.h"
40 #include "vmw_screen.h"
41 #include "vmw_context.h"
43 #include "vmwgfx_drm.h"
60 /* XXX: This isn't a real hardware flag, but just a hack for kernel to
61 * know about primary surfaces. In newer versions of the kernel
62 * interface the driver uses a special field.
64 #define SVGA3D_SURFACE_HINT_SCANOUT (1 << 9)
67 vmw_check_last_cmd(struct vmw_winsys_screen *vws)
69 static uint32_t buffer[16384];
70 struct drm_vmw_fifo_debug_arg arg;
74 memset(&arg, 0, sizeof(arg));
75 arg.debug_buffer = (unsigned long)buffer;
76 arg.debug_buffer_size = 65536;
78 ret = drmCommandWriteRead(vws->ioctl.drm_fd, DRM_VMW_FIFO_DEBUG,
82 debug_printf("%s Ioctl error: \"%s\".\n", __FUNCTION__, strerror(-ret));
86 if (arg.did_not_fit) {
87 debug_printf("%s Command did not fit completely.\n", __FUNCTION__);
90 svga_dump_commands(buffer, arg.used_size);
94 vmw_ioctl_fifo_unmap(struct vmw_winsys_screen *vws, void *mapping)
97 (void)munmap(mapping, getpagesize());
102 vmw_ioctl_fifo_map(struct vmw_winsys_screen *vws,
103 uint32_t fifo_offset )
109 map = mmap(NULL, getpagesize(), PROT_READ, MAP_SHARED,
110 vws->ioctl.drm_fd, fifo_offset);
112 if (map == MAP_FAILED) {
113 debug_printf("Map failed %s\n", strerror(errno));
117 vmw_printf("Fifo (min) is 0x%08x\n", ((uint32_t *) map)[SVGA_FIFO_MIN]);
123 vmw_ioctl_context_create(struct vmw_winsys_screen *vws)
125 struct drm_vmw_context_arg c_arg;
130 ret = drmCommandRead(vws->ioctl.drm_fd, DRM_VMW_CREATE_CONTEXT,
131 &c_arg, sizeof(c_arg));
136 vmw_check_last_cmd(vws);
137 vmw_printf("Context id is %d\n", c_arg.cid);
143 vmw_ioctl_context_destroy(struct vmw_winsys_screen *vws, uint32 cid)
145 struct drm_vmw_context_arg c_arg;
149 memset(&c_arg, 0, sizeof(c_arg));
152 (void)drmCommandWrite(vws->ioctl.drm_fd, DRM_VMW_UNREF_CONTEXT,
153 &c_arg, sizeof(c_arg));
155 vmw_check_last_cmd(vws);
159 vmw_ioctl_surface_create(struct vmw_winsys_screen *vws,
160 SVGA3dSurfaceFlags flags,
161 SVGA3dSurfaceFormat format,
163 uint32_t numFaces, uint32_t numMipLevels)
165 union drm_vmw_surface_create_arg s_arg;
166 struct drm_vmw_surface_create_req *req = &s_arg.req;
167 struct drm_vmw_surface_arg *rep = &s_arg.rep;
168 struct drm_vmw_size sizes[DRM_VMW_MAX_SURFACE_FACES*
169 DRM_VMW_MAX_MIP_LEVELS];
170 struct drm_vmw_size *cur_size;
175 vmw_printf("%s flags %d format %d\n", __FUNCTION__, flags, format);
177 memset(&s_arg, 0, sizeof(s_arg));
178 if (vws->use_old_scanout_flag &&
179 (flags & SVGA3D_SURFACE_HINT_SCANOUT)) {
180 req->flags = (uint32_t) flags;
181 req->scanout = false;
182 } else if (flags & SVGA3D_SURFACE_HINT_SCANOUT) {
183 req->flags = (uint32_t) (flags & ~SVGA3D_SURFACE_HINT_SCANOUT);
186 req->flags = (uint32_t) flags;
187 req->scanout = false;
189 req->format = (uint32_t) format;
192 assert(numFaces * numMipLevels < DRM_VMW_MAX_SURFACE_FACES*
193 DRM_VMW_MAX_MIP_LEVELS);
195 for (iFace = 0; iFace < numFaces; ++iFace) {
196 SVGA3dSize mipSize = size;
198 req->mip_levels[iFace] = numMipLevels;
199 for (iMipLevel = 0; iMipLevel < numMipLevels; ++iMipLevel) {
200 cur_size->width = mipSize.width;
201 cur_size->height = mipSize.height;
202 cur_size->depth = mipSize.depth;
203 mipSize.width = MAX2(mipSize.width >> 1, 1);
204 mipSize.height = MAX2(mipSize.height >> 1, 1);
205 mipSize.depth = MAX2(mipSize.depth >> 1, 1);
209 for (iFace = numFaces; iFace < SVGA3D_MAX_SURFACE_FACES; ++iFace) {
210 req->mip_levels[iFace] = 0;
213 req->size_addr = (unsigned long)&sizes;
215 ret = drmCommandWriteRead(vws->ioctl.drm_fd, DRM_VMW_CREATE_SURFACE,
216 &s_arg, sizeof(s_arg));
221 vmw_printf("Surface id is %d\n", rep->sid);
222 vmw_check_last_cmd(vws);
228 vmw_ioctl_surface_destroy(struct vmw_winsys_screen *vws, uint32 sid)
230 struct drm_vmw_surface_arg s_arg;
234 memset(&s_arg, 0, sizeof(s_arg));
237 (void)drmCommandWrite(vws->ioctl.drm_fd, DRM_VMW_UNREF_SURFACE,
238 &s_arg, sizeof(s_arg));
239 vmw_check_last_cmd(vws);
244 vmw_ioctl_command(struct vmw_winsys_screen *vws, int32_t cid,
245 uint32_t throttle_us, void *commands, uint32_t size,
248 struct drm_vmw_execbuf_arg arg;
249 struct drm_vmw_fence_rep rep;
254 static boolean firsttime = TRUE;
255 static boolean debug = FALSE;
256 static boolean skip = FALSE;
258 debug = debug_get_bool_option("SVGA_DUMP_CMD", FALSE);
259 skip = debug_get_bool_option("SVGA_SKIP_CMD", FALSE);
263 svga_dump_commands(commands, size);
272 memset(&arg, 0, sizeof(arg));
273 memset(&rep, 0, sizeof(rep));
276 arg.fence_rep = (unsigned long)&rep;
277 arg.commands = (unsigned long)commands;
278 arg.command_size = size;
279 arg.throttle_us = throttle_us;
282 ret = drmCommandWrite(vws->ioctl.drm_fd, DRM_VMW_EXECBUF, &arg, sizeof(arg));
283 } while(ret == -ERESTART);
285 debug_printf("%s error %s.\n", __FUNCTION__, strerror(-ret));
290 * Kernel has synced and put the last fence sequence in the FIFO
294 if (rep.error == -EFAULT)
295 rep.fence_seq = vws->ioctl.fifo_map[SVGA_FIFO_FENCE];
297 debug_printf("%s Fence error %s.\n", __FUNCTION__,
298 strerror(-rep.error));
301 vws->ioctl.last_fence = rep.fence_seq;
304 *pfence = rep.fence_seq;
305 vmw_check_last_cmd(vws);
311 vmw_ioctl_region_create(struct vmw_winsys_screen *vws, uint32_t size)
313 struct vmw_region *region;
314 union drm_vmw_alloc_dmabuf_arg arg;
315 struct drm_vmw_alloc_dmabuf_req *req = &arg.req;
316 struct drm_vmw_dmabuf_rep *rep = &arg.rep;
319 vmw_printf("%s: size = %u\n", __FUNCTION__, size);
321 region = CALLOC_STRUCT(vmw_region);
325 memset(&arg, 0, sizeof(arg));
328 ret = drmCommandWriteRead(vws->ioctl.drm_fd, DRM_VMW_ALLOC_DMABUF, &arg,
330 } while (ret == -ERESTART);
333 debug_printf("IOCTL failed %d: %s\n", ret, strerror(-ret));
337 region->ptr.gmrId = rep->cur_gmr_id;
338 region->ptr.offset = rep->cur_gmr_offset;
340 region->handle = rep->handle;
341 region->map_handle = rep->map_handle;
342 region->map_count = 0;
344 region->drm_fd = vws->ioctl.drm_fd;
346 vmw_printf(" gmrId = %u, offset = %u\n",
347 region->ptr.gmrId, region->ptr.offset);
357 vmw_ioctl_region_destroy(struct vmw_region *region)
359 struct drm_vmw_unref_dmabuf_arg arg;
361 vmw_printf("%s: gmrId = %u, offset = %u\n", __FUNCTION__,
362 region->ptr.gmrId, region->ptr.offset);
365 munmap(region->data, region->size);
369 memset(&arg, 0, sizeof(arg));
370 arg.handle = region->handle;
371 drmCommandWrite(region->drm_fd, DRM_VMW_UNREF_DMABUF, &arg, sizeof(arg));
377 vmw_ioctl_region_ptr(struct vmw_region *region)
383 vmw_ioctl_region_map(struct vmw_region *region)
387 vmw_printf("%s: gmrId = %u, offset = %u\n", __FUNCTION__,
388 region->ptr.gmrId, region->ptr.offset);
390 if (region->data == NULL) {
391 map = mmap(NULL, region->size, PROT_READ | PROT_WRITE, MAP_SHARED,
392 region->drm_fd, region->map_handle);
393 if (map == MAP_FAILED) {
394 debug_printf("%s: Map failed.\n", __FUNCTION__);
407 vmw_ioctl_region_unmap(struct vmw_region *region)
409 vmw_printf("%s: gmrId = %u, offset = %u\n", __FUNCTION__,
410 region->ptr.gmrId, region->ptr.offset);
416 vmw_ioctl_fence_signalled(struct vmw_winsys_screen *vws,
427 current = vws->ioctl.fifo_map[SVGA_FIFO_FENCE];
429 if ((int32)(current - expected) >= 0)
430 return 0; /* fence passed */
437 vmw_ioctl_sync(struct vmw_winsys_screen *vws,
441 struct drm_vmw_fence_wait_arg arg;
444 vmw_printf("%s: fence = %lu\n", __FUNCTION__,
445 (unsigned long)fence);
447 cur_fence = vws->ioctl.fifo_map[SVGA_FIFO_FENCE];
448 vmw_printf("%s: Fence id read is 0x%08x\n", __FUNCTION__,
449 (unsigned int)cur_fence);
451 if ((cur_fence - fence) < (1 << 24))
454 memset(&arg, 0, sizeof(arg));
455 arg.sequence = fence;
458 ret = drmCommandWriteRead(vws->ioctl.drm_fd, DRM_VMW_FENCE_WAIT, &arg,
460 } while (ret == -ERESTART);
465 vmw_ioctl_fence_finish(struct vmw_winsys_screen *vws,
471 if(vmw_ioctl_fence_signalled(vws, fence) != 0) {
472 vmw_ioctl_sync(vws, fence);
481 vmw_ioctl_init(struct vmw_winsys_screen *vws)
483 struct drm_vmw_getparam_arg gp_arg;
488 memset(&gp_arg, 0, sizeof(gp_arg));
489 gp_arg.param = DRM_VMW_PARAM_3D;
490 ret = drmCommandWriteRead(vws->ioctl.drm_fd, DRM_VMW_GET_PARAM,
491 &gp_arg, sizeof(gp_arg));
492 if (ret || gp_arg.value == 0) {
493 debug_printf("No 3D enabled (%i, %s)\n", ret, strerror(-ret));
497 memset(&gp_arg, 0, sizeof(gp_arg));
498 gp_arg.param = DRM_VMW_PARAM_FIFO_OFFSET;
499 ret = drmCommandWriteRead(vws->ioctl.drm_fd, DRM_VMW_GET_PARAM,
500 &gp_arg, sizeof(gp_arg));
503 debug_printf("GET_PARAM on %d returned %d: %s\n",
504 vws->ioctl.drm_fd, ret, strerror(-ret));
508 vmw_printf("Offset to map is 0x%08llx\n",
509 (unsigned long long)gp_arg.value);
511 vws->ioctl.fifo_map = vmw_ioctl_fifo_map(vws, gp_arg.value);
512 if (vws->ioctl.fifo_map == NULL)
515 vmw_printf("%s OK\n", __FUNCTION__);
519 debug_printf("%s Failed\n", __FUNCTION__);
526 vmw_ioctl_cleanup(struct vmw_winsys_screen *vws)
530 vmw_ioctl_fifo_unmap(vws, (void *)vws->ioctl.fifo_map);