svga, winsys/svga: Fix persistent memory discard maps
[mesa.git] / src / gallium / winsys / svga / drm / vmw_surface.c
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 *
39 vmw_svga_winsys_surface_map(struct svga_winsys_context *swc,
40 struct svga_winsys_surface *srf,
41 unsigned flags, boolean *retry,
42 boolean *rebind)
43 {
44 struct vmw_svga_winsys_surface *vsrf = vmw_svga_winsys_surface(srf);
45 void *data = NULL;
46 struct pb_buffer *pb_buf;
47 uint32_t pb_flags;
48 struct vmw_winsys_screen *vws = vsrf->screen;
49
50 *retry = FALSE;
51 *rebind = FALSE;
52 assert((flags & (PIPE_TRANSFER_READ | PIPE_TRANSFER_WRITE)) != 0);
53 mtx_lock(&vsrf->mutex);
54
55 if (vsrf->mapcount) {
56 /* Other mappers will get confused if we discard. */
57 flags &= ~PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE;
58 }
59
60 vsrf->rebind = FALSE;
61
62 /*
63 * If we intend to read, there's no point discarding the
64 * data if busy.
65 */
66 if (flags & PIPE_TRANSFER_READ || vsrf->shared)
67 flags &= ~PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE;
68
69 /*
70 * Discard is a hint to a synchronized map.
71 */
72 if (flags & PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE)
73 flags &= ~PIPE_TRANSFER_UNSYNCHRONIZED;
74
75 /*
76 * The surface is allowed to be referenced on the command stream iff
77 * we're mapping unsynchronized or discard. This is an early check.
78 * We need to recheck after a failing discard map.
79 */
80 if (!(flags & (PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE |
81 PIPE_TRANSFER_UNSYNCHRONIZED)) &&
82 p_atomic_read(&vsrf->validated)) {
83 *retry = TRUE;
84 goto out_unlock;
85 }
86
87 pb_flags = flags & (PIPE_TRANSFER_READ_WRITE | PIPE_TRANSFER_UNSYNCHRONIZED |
88 PIPE_TRANSFER_PERSISTENT);
89
90 if (flags & PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE) {
91 struct pb_manager *provider;
92 struct pb_desc desc;
93
94 /*
95 * First, if possible, try to map existing storage with DONTBLOCK.
96 */
97 if (!p_atomic_read(&vsrf->validated)) {
98 data = vmw_svga_winsys_buffer_map(&vws->base, vsrf->buf,
99 PIPE_TRANSFER_DONTBLOCK | pb_flags);
100 if (data)
101 goto out_mapped;
102 }
103
104 /*
105 * Attempt to get a new buffer.
106 */
107 provider = vws->pools.mob_fenced;
108 memset(&desc, 0, sizeof(desc));
109 desc.alignment = 4096;
110 pb_buf = provider->create_buffer(provider, vsrf->size, &desc);
111 if (pb_buf != NULL) {
112 struct svga_winsys_buffer *vbuf =
113 vmw_svga_winsys_buffer_wrap(pb_buf);
114
115 data = vmw_svga_winsys_buffer_map(&vws->base, vbuf, pb_flags);
116 if (data) {
117 vsrf->rebind = TRUE;
118 /*
119 * We've discarded data on this surface and thus
120 * it's data is no longer consider referenced.
121 */
122 vmw_swc_surface_clear_reference(swc, vsrf);
123 if (vsrf->buf)
124 vmw_svga_winsys_buffer_destroy(&vws->base, vsrf->buf);
125 vsrf->buf = vbuf;
126
127 /* Rebind persistent maps immediately */
128 if (flags & PIPE_TRANSFER_PERSISTENT) {
129 *rebind = TRUE;
130 vsrf->rebind = FALSE;
131 }
132 goto out_mapped;
133 } else
134 vmw_svga_winsys_buffer_destroy(&vws->base, vbuf);
135 }
136 /*
137 * We couldn't get and map a new buffer for some reason.
138 * Fall through to an ordinary map.
139 * But tell pipe driver to flush now if already on validate list,
140 * Otherwise we'll overwrite previous contents.
141 */
142 if (!(flags & PIPE_TRANSFER_UNSYNCHRONIZED) &&
143 p_atomic_read(&vsrf->validated)) {
144 *retry = TRUE;
145 goto out_unlock;
146 }
147 }
148
149 pb_flags |= (flags & PIPE_TRANSFER_DONTBLOCK);
150 data = vmw_svga_winsys_buffer_map(&vws->base, vsrf->buf, pb_flags);
151 if (data == NULL)
152 goto out_unlock;
153
154 out_mapped:
155 ++vsrf->mapcount;
156 vsrf->data = data;
157 vsrf->map_mode = flags & (PIPE_TRANSFER_READ | PIPE_TRANSFER_WRITE);
158 out_unlock:
159 mtx_unlock(&vsrf->mutex);
160 return data;
161 }
162
163
164 void
165 vmw_svga_winsys_surface_unmap(struct svga_winsys_context *swc,
166 struct svga_winsys_surface *srf,
167 boolean *rebind)
168 {
169 struct vmw_svga_winsys_surface *vsrf = vmw_svga_winsys_surface(srf);
170 mtx_lock(&vsrf->mutex);
171 if (--vsrf->mapcount == 0) {
172 *rebind = vsrf->rebind;
173 vsrf->rebind = FALSE;
174 } else {
175 *rebind = FALSE;
176 }
177 vmw_svga_winsys_buffer_unmap(&vsrf->screen->base, vsrf->buf);
178 mtx_unlock(&vsrf->mutex);
179 }
180
181 void
182 vmw_svga_winsys_surface_reference(struct vmw_svga_winsys_surface **pdst,
183 struct vmw_svga_winsys_surface *src)
184 {
185 struct pipe_reference *src_ref;
186 struct pipe_reference *dst_ref;
187 struct vmw_svga_winsys_surface *dst;
188
189 if(pdst == NULL || *pdst == src)
190 return;
191
192 dst = *pdst;
193
194 src_ref = src ? &src->refcnt : NULL;
195 dst_ref = dst ? &dst->refcnt : NULL;
196
197 if (pipe_reference(dst_ref, src_ref)) {
198 if (dst->buf)
199 vmw_svga_winsys_buffer_destroy(&dst->screen->base, dst->buf);
200 vmw_ioctl_surface_destroy(dst->screen, dst->sid);
201 #ifdef DEBUG
202 /* to detect dangling pointers */
203 assert(p_atomic_read(&dst->validated) == 0);
204 dst->sid = SVGA3D_INVALID_ID;
205 #endif
206 mtx_destroy(&dst->mutex);
207 FREE(dst);
208 }
209
210 *pdst = src;
211 }