1 /**********************************************************
2 * Copyright 2009-2015 VMware, Inc. All rights reserved.
3 *
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:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
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
22 * SOFTWARE.
23 *
24 **********************************************************/
25
26
27 #include "svga_cmd.h"
28 #include "util/u_debug.h"
29 #include "util/u_memory.h"
30 #include "pipe/p_defines.h"
31 #include "vmw_surface.h"
32 #include "vmw_screen.h"
33 #include "vmw_buffer.h"
34 #include "vmw_context.h"
35 #include "pipebuffer/pb_bufmgr.h"
36
37
38 void *
vmw_svga_winsys_surface_map(struct svga_winsys_context * swc,struct svga_winsys_surface * srf,unsigned flags,boolean * retry)39 vmw_svga_winsys_surface_map(struct svga_winsys_context *swc,
40 struct svga_winsys_surface *srf,
41 unsigned flags, boolean *retry)
42 {
43 struct vmw_svga_winsys_surface *vsrf = vmw_svga_winsys_surface(srf);
44 void *data = NULL;
45 struct pb_buffer *pb_buf;
46 uint32_t pb_flags;
47 struct vmw_winsys_screen *vws = vsrf->screen;
48
49 *retry = FALSE;
50 assert((flags & (PIPE_TRANSFER_READ | PIPE_TRANSFER_WRITE)) != 0);
51 mtx_lock(&vsrf->mutex);
52
53 if (vsrf->mapcount) {
54 /*
55 * Only allow multiple readers to map.
56 */
57 if ((flags & PIPE_TRANSFER_WRITE) ||
58 (vsrf->map_mode & PIPE_TRANSFER_WRITE))
59 goto out_unlock;
60
61 data = vsrf->data;
62 goto out_mapped;
63 }
64
65 vsrf->rebind = FALSE;
66
67 /*
68 * If we intend to read, there's no point discarding the
69 * data if busy.
70 */
71 if (flags & PIPE_TRANSFER_READ || vsrf->shared)
72 flags &= ~PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE;
73
74 /*
75 * Discard is a hint to a synchronized map.
76 */
77 if (flags & PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE)
78 flags &= ~PIPE_TRANSFER_UNSYNCHRONIZED;
79
80 /*
81 * The surface is allowed to be referenced on the command stream iff
82 * we're mapping unsynchronized or discard. This is an early check.
83 * We need to recheck after a failing discard map.
84 */
85 if (!(flags & (PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE |
86 PIPE_TRANSFER_UNSYNCHRONIZED)) &&
87 p_atomic_read(&vsrf->validated)) {
88 *retry = TRUE;
89 goto out_unlock;
90 }
91
92 pb_flags = flags & (PIPE_TRANSFER_READ_WRITE | PIPE_TRANSFER_UNSYNCHRONIZED);
93
94 if (flags & PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE) {
95 struct pb_manager *provider;
96 struct pb_desc desc;
97
98 /*
99 * First, if possible, try to map existing storage with DONTBLOCK.
100 */
101 if (!p_atomic_read(&vsrf->validated)) {
102 data = vmw_svga_winsys_buffer_map(&vws->base, vsrf->buf,
103 PIPE_TRANSFER_DONTBLOCK | pb_flags);
104 if (data)
105 goto out_mapped;
106 }
107
108 /*
109 * Attempt to get a new buffer.
110 */
111 provider = vws->pools.mob_fenced;
112 memset(&desc, 0, sizeof(desc));
113 desc.alignment = 4096;
114 pb_buf = provider->create_buffer(provider, vsrf->size, &desc);
115 if (pb_buf != NULL) {
116 struct svga_winsys_buffer *vbuf =
117 vmw_svga_winsys_buffer_wrap(pb_buf);
118
119 data = vmw_svga_winsys_buffer_map(&vws->base, vbuf, pb_flags);
120 if (data) {
121 vsrf->rebind = TRUE;
122 /*
123 * We've discarded data on this surface and thus
124 * it's data is no longer consider referenced.
125 */
126 vmw_swc_surface_clear_reference(swc, vsrf);
127 if (vsrf->buf)
128 vmw_svga_winsys_buffer_destroy(&vws->base, vsrf->buf);
129 vsrf->buf = vbuf;
130 goto out_mapped;
131 } else
132 vmw_svga_winsys_buffer_destroy(&vws->base, vbuf);
133 }
134 /*
135 * We couldn't get and map a new buffer for some reason.
136 * Fall through to an ordinary map.
137 * But tell pipe driver to flush now if already on validate list,
138 * Otherwise we'll overwrite previous contents.
139 */
140 if (!(flags & PIPE_TRANSFER_UNSYNCHRONIZED) &&
141 p_atomic_read(&vsrf->validated)) {
142 *retry = TRUE;
143 goto out_unlock;
144 }
145 }
146
147 pb_flags |= (flags & PIPE_TRANSFER_DONTBLOCK);
148 data = vmw_svga_winsys_buffer_map(&vws->base, vsrf->buf, pb_flags);
149 if (data == NULL)
150 goto out_unlock;
151
152 out_mapped:
153 ++vsrf->mapcount;
154 vsrf->data = data;
155 vsrf->map_mode = flags & (PIPE_TRANSFER_READ | PIPE_TRANSFER_WRITE);
156 out_unlock:
157 mtx_unlock(&vsrf->mutex);
158 return data;
159 }
160
161
162 void
vmw_svga_winsys_surface_unmap(struct svga_winsys_context * swc,struct svga_winsys_surface * srf,boolean * rebind)163 vmw_svga_winsys_surface_unmap(struct svga_winsys_context *swc,
164 struct svga_winsys_surface *srf,
165 boolean *rebind)
166 {
167 struct vmw_svga_winsys_surface *vsrf = vmw_svga_winsys_surface(srf);
168 mtx_lock(&vsrf->mutex);
169 if (--vsrf->mapcount == 0) {
170 *rebind = vsrf->rebind;
171 vsrf->rebind = FALSE;
172 vmw_svga_winsys_buffer_unmap(&vsrf->screen->base, vsrf->buf);
173 } else {
174 *rebind = FALSE;
175 }
176 mtx_unlock(&vsrf->mutex);
177 }
178
179 enum pipe_error
vmw_svga_winsys_surface_invalidate(struct svga_winsys_context * swc,struct svga_winsys_surface * surf)180 vmw_svga_winsys_surface_invalidate(struct svga_winsys_context *swc,
181 struct svga_winsys_surface *surf)
182 {
183 /* this is a noop since surface invalidation is not needed for DMA path.
184 * DMA is enabled when guest-backed surface is not enabled or
185 * guest-backed dma is enabled. Since guest-backed dma is enabled
186 * when guest-backed surface is enabled, that implies DMA is always enabled;
187 * hence, surface invalidation is not needed.
188 */
189 return PIPE_OK;
190 }
191
192 void
vmw_svga_winsys_surface_reference(struct vmw_svga_winsys_surface ** pdst,struct vmw_svga_winsys_surface * src)193 vmw_svga_winsys_surface_reference(struct vmw_svga_winsys_surface **pdst,
194 struct vmw_svga_winsys_surface *src)
195 {
196 struct pipe_reference *src_ref;
197 struct pipe_reference *dst_ref;
198 struct vmw_svga_winsys_surface *dst;
199
200 if(pdst == NULL || *pdst == src)
201 return;
202
203 dst = *pdst;
204
205 src_ref = src ? &src->refcnt : NULL;
206 dst_ref = dst ? &dst->refcnt : NULL;
207
208 if (pipe_reference(dst_ref, src_ref)) {
209 if (dst->buf)
210 vmw_svga_winsys_buffer_destroy(&dst->screen->base, dst->buf);
211 vmw_ioctl_surface_destroy(dst->screen, dst->sid);
212 #ifdef DEBUG
213 /* to detect dangling pointers */
214 assert(p_atomic_read(&dst->validated) == 0);
215 dst->sid = SVGA3D_INVALID_ID;
216 #endif
217 mtx_destroy(&dst->mutex);
218 FREE(dst);
219 }
220
221 *pdst = src;
222 }
223