intel-gem: Always build GEM execbuffer code.
[mesa.git] / src / mesa / drivers / dri / intel / intel_ioctl.c
1 /**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28
29 #include <stdio.h>
30 #include <unistd.h>
31 #include <errno.h>
32 #include <sched.h>
33 #include <sys/types.h>
34 #include <sys/ioctl.h>
35
36 #include "mtypes.h"
37 #include "context.h"
38 #include "swrast/swrast.h"
39
40 #include "intel_context.h"
41 #include "intel_ioctl.h"
42 #include "intel_batchbuffer.h"
43 #include "intel_blit.h"
44 #include "intel_regions.h"
45 #include "drm.h"
46 #include "i915_drm.h"
47
48 #include "intel_bufmgr.h"
49
50 #define FILE_DEBUG_FLAG DEBUG_IOCTL
51
52 int
53 intelEmitIrqLocked(struct intel_context *intel)
54 {
55 struct drm_i915_irq_emit ie;
56 int ret, seq = 1;
57
58 if (intel->no_hw)
59 return 1;
60
61 /*
62 assert(((*(int *)intel->driHwLock) & ~DRM_LOCK_CONT) ==
63 (DRM_LOCK_HELD|intel->hHWContext));
64 */
65
66 ie.irq_seq = &seq;
67
68 ret = drmCommandWriteRead(intel->driFd, DRM_I915_IRQ_EMIT, &ie, sizeof(ie));
69 if (ret) {
70 fprintf(stderr, "%s: drm_i915_irq_emit: %d\n", __FUNCTION__, ret);
71 exit(1);
72 }
73
74 DBG("%s --> %d\n", __FUNCTION__, seq);
75
76 return seq;
77 }
78
79 void
80 intelWaitIrq(struct intel_context *intel, int seq)
81 {
82 struct drm_i915_irq_wait iw;
83 int ret, lastdispatch;
84 volatile struct drm_i915_sarea *sarea = intel->sarea;
85
86 if (intel->no_hw)
87 return;
88
89 DBG("%s %d\n", __FUNCTION__, seq);
90
91 iw.irq_seq = seq;
92
93 do {
94 lastdispatch = sarea->last_dispatch;
95 ret = drmCommandWrite(intel->driFd, DRM_I915_IRQ_WAIT, &iw, sizeof(iw));
96 } while (ret == -EAGAIN ||
97 ret == -EINTR ||
98 (ret == -EBUSY && lastdispatch != sarea->last_dispatch) ||
99 (ret == 0 && seq > sarea->last_dispatch) ||
100 (ret == 0 && sarea->last_dispatch - seq >= (1 << 24)));
101
102 if (ret) {
103 fprintf(stderr, "%s: drm_i915_irq_wait: %d\n", __FUNCTION__, ret);
104 exit(1);
105 }
106 }
107
108
109 int
110 intel_batch_ioctl(struct intel_context *intel,
111 GLuint start_offset,
112 GLuint used,
113 GLboolean ignore_cliprects, GLboolean allow_unlock)
114 {
115 struct drm_i915_batchbuffer batch;
116
117 if (intel->no_hw)
118 return 0;
119
120 assert(intel->locked);
121 assert(used);
122
123 DBG("%s used %d offset %x..%x ignore_cliprects %d\n",
124 __FUNCTION__,
125 used, start_offset, start_offset + used, ignore_cliprects);
126
127 /* Throw away non-effective packets. Won't work once we have
128 * hardware contexts which would preserve statechanges beyond a
129 * single buffer.
130 */
131 batch.start = start_offset;
132 batch.used = used;
133 batch.cliprects = intel->pClipRects;
134 batch.num_cliprects = ignore_cliprects ? 0 : intel->numClipRects;
135 batch.DR1 = 0;
136 batch.DR4 = ((((GLuint) intel->drawX) & 0xffff) |
137 (((GLuint) intel->drawY) << 16));
138
139 DBG("%s: 0x%x..0x%x DR4: %x cliprects: %d\n",
140 __FUNCTION__,
141 batch.start,
142 batch.start + batch.used * 4, batch.DR4, batch.num_cliprects);
143
144 if (drmCommandWrite(intel->driFd, DRM_I915_BATCHBUFFER, &batch,
145 sizeof(batch))) {
146 fprintf(stderr, "DRM_I915_BATCHBUFFER: %d\n", -errno);
147 return -errno;
148 }
149
150 return 0;
151 }
152
153 int
154 intel_exec_ioctl(struct intel_context *intel,
155 GLuint used,
156 GLboolean ignore_cliprects, GLboolean allow_unlock,
157 struct drm_i915_gem_execbuffer *execbuf)
158 {
159 int ret;
160
161 assert(intel->locked);
162 assert(used);
163
164 if (intel->no_hw)
165 return 0;
166
167 execbuf->batch_start_offset = 0;
168 execbuf->batch_len = used;
169 execbuf->cliprects_ptr = (uintptr_t)intel->pClipRects;
170 execbuf->num_cliprects = ignore_cliprects ? 0 : intel->numClipRects;
171 execbuf->DR1 = 0;
172 execbuf->DR4 = ((((GLuint) intel->drawX) & 0xffff) |
173 (((GLuint) intel->drawY) << 16));
174
175 do {
176 ret = ioctl(intel->driFd, DRM_IOCTL_I915_GEM_EXECBUFFER, execbuf);
177 } while (ret == -EAGAIN);
178
179 if (ret != 0) {
180 fprintf(stderr, "DRM_I915_GEM_EXECBUFFER: %d\n", -errno);
181 return -errno;
182 }
183
184 return 0;
185 }