blob: 5901d32d02736c3fb9a4a9ee2aa14f6daa35f7ce [file] [log] [blame]
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001/**************************************************************************
2 *
Sinclair Yehc8261a92015-06-26 01:23:42 -07003 * Copyright © 2009-2014 VMware, Inc., Palo Alto, CA., USA
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00004 * 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 OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28#include "vmwgfx_kms.h"
29
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +020030
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000031/* Might need a hrtimer here? */
32#define VMWGFX_PRESENT_RATE ((HZ / 60 > 0) ? HZ / 60 : 1)
33
Sinclair Yehc8261a92015-06-26 01:23:42 -070034void vmw_du_cleanup(struct vmw_display_unit *du)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000035{
36 if (du->cursor_surface)
37 vmw_surface_unreference(&du->cursor_surface);
38 if (du->cursor_dmabuf)
39 vmw_dmabuf_unreference(&du->cursor_dmabuf);
Thomas Wood34ea3d32014-05-29 16:57:41 +010040 drm_connector_unregister(&du->connector);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000041 drm_crtc_cleanup(&du->crtc);
42 drm_encoder_cleanup(&du->encoder);
43 drm_connector_cleanup(&du->connector);
44}
45
46/*
47 * Display Unit Cursor functions
48 */
49
50int vmw_cursor_update_image(struct vmw_private *dev_priv,
51 u32 *image, u32 width, u32 height,
52 u32 hotspotX, u32 hotspotY)
53{
54 struct {
55 u32 cmd;
56 SVGAFifoCmdDefineAlphaCursor cursor;
57 } *cmd;
58 u32 image_size = width * height * 4;
59 u32 cmd_size = sizeof(*cmd) + image_size;
60
61 if (!image)
62 return -EINVAL;
63
64 cmd = vmw_fifo_reserve(dev_priv, cmd_size);
65 if (unlikely(cmd == NULL)) {
66 DRM_ERROR("Fifo reserve failed.\n");
67 return -ENOMEM;
68 }
69
70 memset(cmd, 0, sizeof(*cmd));
71
72 memcpy(&cmd[1], image, image_size);
73
74 cmd->cmd = cpu_to_le32(SVGA_CMD_DEFINE_ALPHA_CURSOR);
75 cmd->cursor.id = cpu_to_le32(0);
76 cmd->cursor.width = cpu_to_le32(width);
77 cmd->cursor.height = cpu_to_le32(height);
78 cmd->cursor.hotspotX = cpu_to_le32(hotspotX);
79 cmd->cursor.hotspotY = cpu_to_le32(hotspotY);
80
81 vmw_fifo_commit(dev_priv, cmd_size);
82
83 return 0;
84}
85
Jakob Bornecrantz6a91d972011-11-28 13:19:10 +010086int vmw_cursor_update_dmabuf(struct vmw_private *dev_priv,
87 struct vmw_dma_buffer *dmabuf,
88 u32 width, u32 height,
89 u32 hotspotX, u32 hotspotY)
90{
91 struct ttm_bo_kmap_obj map;
92 unsigned long kmap_offset;
93 unsigned long kmap_num;
94 void *virtual;
95 bool dummy;
96 int ret;
97
98 kmap_offset = 0;
99 kmap_num = (width*height*4 + PAGE_SIZE - 1) >> PAGE_SHIFT;
100
Thierry Redingee3939e2014-07-21 13:15:51 +0200101 ret = ttm_bo_reserve(&dmabuf->base, true, false, false, NULL);
Jakob Bornecrantz6a91d972011-11-28 13:19:10 +0100102 if (unlikely(ret != 0)) {
103 DRM_ERROR("reserve failed\n");
104 return -EINVAL;
105 }
106
107 ret = ttm_bo_kmap(&dmabuf->base, kmap_offset, kmap_num, &map);
108 if (unlikely(ret != 0))
109 goto err_unreserve;
110
111 virtual = ttm_kmap_obj_virtual(&map, &dummy);
112 ret = vmw_cursor_update_image(dev_priv, virtual, width, height,
113 hotspotX, hotspotY);
114
115 ttm_bo_kunmap(&map);
116err_unreserve:
117 ttm_bo_unreserve(&dmabuf->base);
118
119 return ret;
120}
121
122
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000123void vmw_cursor_update_position(struct vmw_private *dev_priv,
124 bool show, int x, int y)
125{
126 __le32 __iomem *fifo_mem = dev_priv->mmio_virt;
127 uint32_t count;
128
129 iowrite32(show ? 1 : 0, fifo_mem + SVGA_FIFO_CURSOR_ON);
130 iowrite32(x, fifo_mem + SVGA_FIFO_CURSOR_X);
131 iowrite32(y, fifo_mem + SVGA_FIFO_CURSOR_Y);
132 count = ioread32(fifo_mem + SVGA_FIFO_CURSOR_COUNT);
133 iowrite32(++count, fifo_mem + SVGA_FIFO_CURSOR_COUNT);
134}
135
136int vmw_du_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
137 uint32_t handle, uint32_t width, uint32_t height)
138{
139 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000140 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
141 struct vmw_surface *surface = NULL;
142 struct vmw_dma_buffer *dmabuf = NULL;
143 int ret;
144
Daniel Vetterbfb89922012-12-02 13:48:21 +0100145 /*
146 * FIXME: Unclear whether there's any global state touched by the
147 * cursor_set function, especially vmw_cursor_update_position looks
148 * suspicious. For now take the easy route and reacquire all locks. We
149 * can do this since the caller in the drm core doesn't check anything
150 * which is protected by any looks.
151 */
Rob Clark21e88622014-10-30 13:39:04 -0400152 drm_modeset_unlock_crtc(crtc);
Daniel Vetterbfb89922012-12-02 13:48:21 +0100153 drm_modeset_lock_all(dev_priv->dev);
154
Jakob Bornecrantzbaa91d642011-11-09 10:25:28 +0100155 /* A lot of the code assumes this */
Daniel Vetterbfb89922012-12-02 13:48:21 +0100156 if (handle && (width != 64 || height != 64)) {
157 ret = -EINVAL;
158 goto out;
159 }
Jakob Bornecrantzbaa91d642011-11-09 10:25:28 +0100160
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000161 if (handle) {
Ville Syrjäläa5d0f572013-06-03 16:10:41 +0300162 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
163
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100164 ret = vmw_user_lookup_handle(dev_priv, tfile,
165 handle, &surface, &dmabuf);
166 if (ret) {
167 DRM_ERROR("failed to find surface or dmabuf: %i\n", ret);
Daniel Vetterbfb89922012-12-02 13:48:21 +0100168 ret = -EINVAL;
169 goto out;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000170 }
171 }
172
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100173 /* need to do this before taking down old image */
174 if (surface && !surface->snooper.image) {
175 DRM_ERROR("surface not suitable for cursor\n");
176 vmw_surface_unreference(&surface);
Daniel Vetterbfb89922012-12-02 13:48:21 +0100177 ret = -EINVAL;
178 goto out;
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100179 }
180
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000181 /* takedown old cursor */
182 if (du->cursor_surface) {
183 du->cursor_surface->snooper.crtc = NULL;
184 vmw_surface_unreference(&du->cursor_surface);
185 }
186 if (du->cursor_dmabuf)
187 vmw_dmabuf_unreference(&du->cursor_dmabuf);
188
189 /* setup new image */
190 if (surface) {
191 /* vmw_user_surface_lookup takes one reference */
192 du->cursor_surface = surface;
193
194 du->cursor_surface->snooper.crtc = crtc;
195 du->cursor_age = du->cursor_surface->snooper.age;
196 vmw_cursor_update_image(dev_priv, surface->snooper.image,
197 64, 64, du->hotspot_x, du->hotspot_y);
198 } else if (dmabuf) {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000199 /* vmw_user_surface_lookup takes one reference */
200 du->cursor_dmabuf = dmabuf;
201
Jakob Bornecrantz6a91d972011-11-28 13:19:10 +0100202 ret = vmw_cursor_update_dmabuf(dev_priv, dmabuf, width, height,
203 du->hotspot_x, du->hotspot_y);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000204 } else {
205 vmw_cursor_update_position(dev_priv, false, 0, 0);
Daniel Vetterbfb89922012-12-02 13:48:21 +0100206 ret = 0;
207 goto out;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000208 }
209
Thomas Hellstromda7653d2011-11-02 09:43:12 +0100210 vmw_cursor_update_position(dev_priv, true,
211 du->cursor_x + du->hotspot_x,
212 du->cursor_y + du->hotspot_y);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000213
Daniel Vetterbfb89922012-12-02 13:48:21 +0100214 ret = 0;
215out:
216 drm_modeset_unlock_all(dev_priv->dev);
Daniel Vetter4d02e2d2014-11-11 10:12:00 +0100217 drm_modeset_lock_crtc(crtc, crtc->cursor);
Daniel Vetterbfb89922012-12-02 13:48:21 +0100218
219 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000220}
221
222int vmw_du_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
223{
224 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
225 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
226 bool shown = du->cursor_surface || du->cursor_dmabuf ? true : false;
227
228 du->cursor_x = x + crtc->x;
229 du->cursor_y = y + crtc->y;
230
Daniel Vetterdac35662012-12-02 15:24:10 +0100231 /*
232 * FIXME: Unclear whether there's any global state touched by the
233 * cursor_set function, especially vmw_cursor_update_position looks
234 * suspicious. For now take the easy route and reacquire all locks. We
235 * can do this since the caller in the drm core doesn't check anything
236 * which is protected by any looks.
237 */
Rob Clark21e88622014-10-30 13:39:04 -0400238 drm_modeset_unlock_crtc(crtc);
Daniel Vetterdac35662012-12-02 15:24:10 +0100239 drm_modeset_lock_all(dev_priv->dev);
240
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000241 vmw_cursor_update_position(dev_priv, shown,
Thomas Hellstromda7653d2011-11-02 09:43:12 +0100242 du->cursor_x + du->hotspot_x,
243 du->cursor_y + du->hotspot_y);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000244
Daniel Vetterdac35662012-12-02 15:24:10 +0100245 drm_modeset_unlock_all(dev_priv->dev);
Daniel Vetter4d02e2d2014-11-11 10:12:00 +0100246 drm_modeset_lock_crtc(crtc, crtc->cursor);
Daniel Vetterdac35662012-12-02 15:24:10 +0100247
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000248 return 0;
249}
250
251void vmw_kms_cursor_snoop(struct vmw_surface *srf,
252 struct ttm_object_file *tfile,
253 struct ttm_buffer_object *bo,
254 SVGA3dCmdHeader *header)
255{
256 struct ttm_bo_kmap_obj map;
257 unsigned long kmap_offset;
258 unsigned long kmap_num;
259 SVGA3dCopyBox *box;
260 unsigned box_count;
261 void *virtual;
262 bool dummy;
263 struct vmw_dma_cmd {
264 SVGA3dCmdHeader header;
265 SVGA3dCmdSurfaceDMA dma;
266 } *cmd;
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100267 int i, ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000268
269 cmd = container_of(header, struct vmw_dma_cmd, header);
270
271 /* No snooper installed */
272 if (!srf->snooper.image)
273 return;
274
275 if (cmd->dma.host.face != 0 || cmd->dma.host.mipmap != 0) {
276 DRM_ERROR("face and mipmap for cursors should never != 0\n");
277 return;
278 }
279
280 if (cmd->header.size < 64) {
281 DRM_ERROR("at least one full copy box must be given\n");
282 return;
283 }
284
285 box = (SVGA3dCopyBox *)&cmd[1];
286 box_count = (cmd->header.size - sizeof(SVGA3dCmdSurfaceDMA)) /
287 sizeof(SVGA3dCopyBox);
288
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100289 if (cmd->dma.guest.ptr.offset % PAGE_SIZE ||
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000290 box->x != 0 || box->y != 0 || box->z != 0 ||
291 box->srcx != 0 || box->srcy != 0 || box->srcz != 0 ||
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100292 box->d != 1 || box_count != 1) {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000293 /* TODO handle none page aligned offsets */
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100294 /* TODO handle more dst & src != 0 */
295 /* TODO handle more then one copy */
296 DRM_ERROR("Cant snoop dma request for cursor!\n");
297 DRM_ERROR("(%u, %u, %u) (%u, %u, %u) (%ux%ux%u) %u %u\n",
298 box->srcx, box->srcy, box->srcz,
299 box->x, box->y, box->z,
300 box->w, box->h, box->d, box_count,
301 cmd->dma.guest.ptr.offset);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000302 return;
303 }
304
305 kmap_offset = cmd->dma.guest.ptr.offset >> PAGE_SHIFT;
306 kmap_num = (64*64*4) >> PAGE_SHIFT;
307
Thierry Redingee3939e2014-07-21 13:15:51 +0200308 ret = ttm_bo_reserve(bo, true, false, false, NULL);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000309 if (unlikely(ret != 0)) {
310 DRM_ERROR("reserve failed\n");
311 return;
312 }
313
314 ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
315 if (unlikely(ret != 0))
316 goto err_unreserve;
317
318 virtual = ttm_kmap_obj_virtual(&map, &dummy);
319
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100320 if (box->w == 64 && cmd->dma.guest.pitch == 64*4) {
321 memcpy(srf->snooper.image, virtual, 64*64*4);
322 } else {
323 /* Image is unsigned pointer. */
324 for (i = 0; i < box->h; i++)
325 memcpy(srf->snooper.image + i * 64,
326 virtual + i * cmd->dma.guest.pitch,
327 box->w * 4);
328 }
329
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000330 srf->snooper.age++;
331
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000332 ttm_bo_kunmap(&map);
333err_unreserve:
334 ttm_bo_unreserve(bo);
335}
336
337void vmw_kms_cursor_post_execbuf(struct vmw_private *dev_priv)
338{
339 struct drm_device *dev = dev_priv->dev;
340 struct vmw_display_unit *du;
341 struct drm_crtc *crtc;
342
343 mutex_lock(&dev->mode_config.mutex);
344
345 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
346 du = vmw_crtc_to_du(crtc);
347 if (!du->cursor_surface ||
348 du->cursor_age == du->cursor_surface->snooper.age)
349 continue;
350
351 du->cursor_age = du->cursor_surface->snooper.age;
352 vmw_cursor_update_image(dev_priv,
353 du->cursor_surface->snooper.image,
354 64, 64, du->hotspot_x, du->hotspot_y);
355 }
356
357 mutex_unlock(&dev->mode_config.mutex);
358}
359
360/*
361 * Generic framebuffer code
362 */
363
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000364/*
365 * Surface framebuffer code
366 */
367
Rashika Kheria847c5962014-01-06 22:18:10 +0530368static void vmw_framebuffer_surface_destroy(struct drm_framebuffer *framebuffer)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000369{
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200370 struct vmw_framebuffer_surface *vfbs =
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000371 vmw_framebuffer_to_vfbs(framebuffer);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200372 struct vmw_master *vmaster = vmw_master(vfbs->master);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000373
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200374
375 mutex_lock(&vmaster->fb_surf_mutex);
376 list_del(&vfbs->head);
377 mutex_unlock(&vmaster->fb_surf_mutex);
378
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200379 drm_master_put(&vfbs->master);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000380 drm_framebuffer_cleanup(framebuffer);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200381 vmw_surface_unreference(&vfbs->surface);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200382 ttm_base_object_unref(&vfbs->base.user_obj);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000383
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200384 kfree(vfbs);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000385}
386
Rashika Kheria847c5962014-01-06 22:18:10 +0530387static int vmw_framebuffer_surface_dirty(struct drm_framebuffer *framebuffer,
Thomas Hellstrom02b00162010-10-05 12:43:02 +0200388 struct drm_file *file_priv,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000389 unsigned flags, unsigned color,
390 struct drm_clip_rect *clips,
391 unsigned num_clips)
392{
393 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
394 struct vmw_framebuffer_surface *vfbs =
395 vmw_framebuffer_to_vfbs(framebuffer);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000396 struct drm_clip_rect norect;
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200397 int ret, inc = 1;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000398
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200399 if (unlikely(vfbs->master != file_priv->master))
400 return -EINVAL;
401
Sinclair Yehc8261a92015-06-26 01:23:42 -0700402 /* Legacy Display Unit does not support 3D */
403 if (dev_priv->active_display_unit == vmw_du_legacy)
Jakob Bornecrantz01e81412011-10-04 20:13:24 +0200404 return -EINVAL;
405
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200406 drm_modeset_lock_all(dev_priv->dev);
407
Thomas Hellstrom294adf72014-02-27 12:34:51 +0100408 ret = ttm_read_lock(&dev_priv->reservation_sem, true);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200409 if (unlikely(ret != 0)) {
410 drm_modeset_unlock_all(dev_priv->dev);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200411 return ret;
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200412 }
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200413
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000414 if (!num_clips) {
415 num_clips = 1;
416 clips = &norect;
417 norect.x1 = norect.y1 = 0;
418 norect.x2 = framebuffer->width;
419 norect.y2 = framebuffer->height;
420 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
421 num_clips /= 2;
422 inc = 2; /* skip source rects */
423 }
424
Sinclair Yehc8261a92015-06-26 01:23:42 -0700425 if (dev_priv->active_display_unit == vmw_du_screen_object)
Thomas Hellstrom10b1e0c2015-06-26 02:14:27 -0700426 ret = vmw_kms_sou_do_surface_dirty(dev_priv, &vfbs->base,
427 clips, NULL, NULL, 0, 0,
428 num_clips, inc, NULL);
Sinclair Yeh35c05122015-06-26 01:42:06 -0700429 else
430 ret = vmw_kms_stdu_do_surface_dirty(dev_priv, file_priv,
431 &vfbs->base,
432 clips, num_clips,
433 inc);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000434
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700435 vmw_fifo_flush(dev_priv, false);
Thomas Hellstrom294adf72014-02-27 12:34:51 +0100436 ttm_read_unlock(&dev_priv->reservation_sem);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200437
438 drm_modeset_unlock_all(dev_priv->dev);
439
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000440 return 0;
441}
442
Thomas Hellstrom10b1e0c2015-06-26 02:14:27 -0700443/**
444 * vmw_kms_readback - Perform a readback from the screen system to
445 * a dma-buffer backed framebuffer.
446 *
447 * @dev_priv: Pointer to the device private structure.
448 * @file_priv: Pointer to a struct drm_file identifying the caller.
449 * Must be set to NULL if @user_fence_rep is NULL.
450 * @vfb: Pointer to the dma-buffer backed framebuffer.
451 * @user_fence_rep: User-space provided structure for fence information.
452 * Must be set to non-NULL if @file_priv is non-NULL.
453 * @vclips: Array of clip rects.
454 * @num_clips: Number of clip rects in @vclips.
455 *
456 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
457 * interrupted.
458 */
459int vmw_kms_readback(struct vmw_private *dev_priv,
460 struct drm_file *file_priv,
461 struct vmw_framebuffer *vfb,
462 struct drm_vmw_fence_rep __user *user_fence_rep,
463 struct drm_vmw_rect *vclips,
464 uint32_t num_clips)
465{
466 switch (dev_priv->active_display_unit) {
467 case vmw_du_screen_object:
468 return vmw_kms_sou_readback(dev_priv, file_priv, vfb,
469 user_fence_rep, vclips, num_clips);
470 default:
471 WARN_ONCE(true,
472 "Readback called with invalid display system.\n");
473 }
474
475 return -ENOSYS;
476}
477
478
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000479static struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs = {
480 .destroy = vmw_framebuffer_surface_destroy,
481 .dirty = vmw_framebuffer_surface_dirty,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000482};
483
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200484static int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv,
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200485 struct drm_file *file_priv,
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200486 struct vmw_surface *surface,
487 struct vmw_framebuffer **out,
488 const struct drm_mode_fb_cmd
Sinclair Yehf89c6c32015-06-26 01:54:28 -0700489 *mode_cmd,
490 bool is_dmabuf_proxy)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000491
492{
493 struct drm_device *dev = dev_priv->dev;
494 struct vmw_framebuffer_surface *vfbs;
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200495 enum SVGA3dSurfaceFormat format;
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200496 struct vmw_master *vmaster = vmw_master(file_priv->master);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000497 int ret;
498
Sinclair Yehc8261a92015-06-26 01:23:42 -0700499 /* 3D is only supported on HWv8 and newer hosts */
500 if (dev_priv->active_display_unit == vmw_du_legacy)
Jakob Bornecrantz01e81412011-10-04 20:13:24 +0200501 return -ENOSYS;
502
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200503 /*
504 * Sanity checks.
505 */
506
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100507 /* Surface must be marked as a scanout. */
508 if (unlikely(!surface->scanout))
509 return -EINVAL;
510
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200511 if (unlikely(surface->mip_levels[0] != 1 ||
512 surface->num_sizes != 1 ||
Thomas Hellstromb360a3c2014-01-15 08:51:36 +0100513 surface->base_size.width < mode_cmd->width ||
514 surface->base_size.height < mode_cmd->height ||
515 surface->base_size.depth != 1)) {
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200516 DRM_ERROR("Incompatible surface dimensions "
517 "for requested mode.\n");
518 return -EINVAL;
519 }
520
521 switch (mode_cmd->depth) {
522 case 32:
523 format = SVGA3D_A8R8G8B8;
524 break;
525 case 24:
526 format = SVGA3D_X8R8G8B8;
527 break;
528 case 16:
529 format = SVGA3D_R5G6B5;
530 break;
531 case 15:
532 format = SVGA3D_A1R5G5B5;
533 break;
534 default:
535 DRM_ERROR("Invalid color depth: %d\n", mode_cmd->depth);
536 return -EINVAL;
537 }
538
539 if (unlikely(format != surface->format)) {
540 DRM_ERROR("Invalid surface format for requested mode.\n");
541 return -EINVAL;
542 }
543
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000544 vfbs = kzalloc(sizeof(*vfbs), GFP_KERNEL);
545 if (!vfbs) {
546 ret = -ENOMEM;
547 goto out_err1;
548 }
549
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000550 if (!vmw_surface_reference(surface)) {
551 DRM_ERROR("failed to reference surface %p\n", surface);
Daniel Vetter80f0b5a2012-12-13 23:39:01 +0100552 ret = -EINVAL;
553 goto out_err2;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000554 }
555
556 /* XXX get the first 3 from the surface info */
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200557 vfbs->base.base.bits_per_pixel = mode_cmd->bpp;
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200558 vfbs->base.base.pitches[0] = mode_cmd->pitch;
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200559 vfbs->base.base.depth = mode_cmd->depth;
560 vfbs->base.base.width = mode_cmd->width;
561 vfbs->base.base.height = mode_cmd->height;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000562 vfbs->surface = surface;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200563 vfbs->base.user_handle = mode_cmd->handle;
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200564 vfbs->master = drm_master_get(file_priv->master);
Sinclair Yehf89c6c32015-06-26 01:54:28 -0700565 vfbs->is_dmabuf_proxy = is_dmabuf_proxy;
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200566
567 mutex_lock(&vmaster->fb_surf_mutex);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200568 list_add_tail(&vfbs->head, &vmaster->fb_surf);
569 mutex_unlock(&vmaster->fb_surf_mutex);
570
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000571 *out = &vfbs->base;
572
Daniel Vetter80f0b5a2012-12-13 23:39:01 +0100573 ret = drm_framebuffer_init(dev, &vfbs->base.base,
574 &vmw_framebuffer_surface_funcs);
575 if (ret)
576 goto out_err3;
577
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000578 return 0;
579
580out_err3:
Daniel Vetter80f0b5a2012-12-13 23:39:01 +0100581 vmw_surface_unreference(&surface);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000582out_err2:
583 kfree(vfbs);
584out_err1:
585 return ret;
586}
587
588/*
589 * Dmabuf framebuffer code
590 */
591
Rashika Kheria847c5962014-01-06 22:18:10 +0530592static void vmw_framebuffer_dmabuf_destroy(struct drm_framebuffer *framebuffer)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000593{
594 struct vmw_framebuffer_dmabuf *vfbd =
595 vmw_framebuffer_to_vfbd(framebuffer);
596
597 drm_framebuffer_cleanup(framebuffer);
598 vmw_dmabuf_unreference(&vfbd->buffer);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200599 ttm_base_object_unref(&vfbd->base.user_obj);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000600
601 kfree(vfbd);
602}
603
Rashika Kheria847c5962014-01-06 22:18:10 +0530604static int vmw_framebuffer_dmabuf_dirty(struct drm_framebuffer *framebuffer,
Thomas Hellstrom02b00162010-10-05 12:43:02 +0200605 struct drm_file *file_priv,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000606 unsigned flags, unsigned color,
607 struct drm_clip_rect *clips,
608 unsigned num_clips)
609{
610 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200611 struct vmw_framebuffer_dmabuf *vfbd =
612 vmw_framebuffer_to_vfbd(framebuffer);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000613 struct drm_clip_rect norect;
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200614 int ret, increment = 1;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000615
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200616 drm_modeset_lock_all(dev_priv->dev);
617
Thomas Hellstrom294adf72014-02-27 12:34:51 +0100618 ret = ttm_read_lock(&dev_priv->reservation_sem, true);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200619 if (unlikely(ret != 0)) {
620 drm_modeset_unlock_all(dev_priv->dev);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200621 return ret;
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200622 }
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200623
Thomas Hellstromdf1c93b2010-01-13 22:28:36 +0100624 if (!num_clips) {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000625 num_clips = 1;
626 clips = &norect;
627 norect.x1 = norect.y1 = 0;
628 norect.x2 = framebuffer->width;
629 norect.y2 = framebuffer->height;
630 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
631 num_clips /= 2;
632 increment = 2;
633 }
634
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200635 if (dev_priv->ldu_priv) {
Sinclair Yehc8261a92015-06-26 01:23:42 -0700636 ret = vmw_kms_ldu_do_dmabuf_dirty(dev_priv, &vfbd->base,
637 flags, color,
638 clips, num_clips, increment);
639 } else if (dev_priv->active_display_unit == vmw_du_screen_object) {
Thomas Hellstrom10b1e0c2015-06-26 02:14:27 -0700640 ret = vmw_kms_sou_do_dmabuf_dirty(dev_priv, &vfbd->base,
Sinclair Yehc8261a92015-06-26 01:23:42 -0700641 clips, num_clips, increment,
Thomas Hellstrom10b1e0c2015-06-26 02:14:27 -0700642 true,
Sinclair Yehc8261a92015-06-26 01:23:42 -0700643 NULL);
Sinclair Yeh35c05122015-06-26 01:42:06 -0700644 } else {
645 ret = vmw_kms_stdu_do_surface_dirty(dev_priv, file_priv,
646 &vfbd->base,
647 clips, num_clips,
648 increment);
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200649 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000650
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700651 vmw_fifo_flush(dev_priv, false);
Thomas Hellstrom294adf72014-02-27 12:34:51 +0100652 ttm_read_unlock(&dev_priv->reservation_sem);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200653
654 drm_modeset_unlock_all(dev_priv->dev);
655
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200656 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000657}
658
659static struct drm_framebuffer_funcs vmw_framebuffer_dmabuf_funcs = {
660 .destroy = vmw_framebuffer_dmabuf_destroy,
661 .dirty = vmw_framebuffer_dmabuf_dirty,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000662};
663
Jakob Bornecrantz497a3ff2011-10-04 20:13:14 +0200664/**
Jakob Bornecrantz497a3ff2011-10-04 20:13:14 +0200665 * Pin the dmabuffer to the start of vram.
666 */
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000667static int vmw_framebuffer_dmabuf_pin(struct vmw_framebuffer *vfb)
668{
669 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
670 struct vmw_framebuffer_dmabuf *vfbd =
671 vmw_framebuffer_to_vfbd(&vfb->base);
672 int ret;
673
Sinclair Yehc8261a92015-06-26 01:23:42 -0700674 /* This code should only be used with Legacy Display Unit */
675 BUG_ON(dev_priv->active_display_unit != vmw_du_legacy);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +0200676
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000677 vmw_overlay_pause_all(dev_priv);
678
Thomas Hellstrom459d0fa2015-06-26 00:25:37 -0700679 ret = vmw_dmabuf_pin_in_start_of_vram(dev_priv, vfbd->buffer, false);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000680
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000681 vmw_overlay_resume_all(dev_priv);
682
Jakob Bornecrantz316ab132010-05-28 11:22:05 +0200683 WARN_ON(ret != 0);
684
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000685 return 0;
686}
687
688static int vmw_framebuffer_dmabuf_unpin(struct vmw_framebuffer *vfb)
689{
690 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
691 struct vmw_framebuffer_dmabuf *vfbd =
692 vmw_framebuffer_to_vfbd(&vfb->base);
693
694 if (!vfbd->buffer) {
695 WARN_ON(!vfbd->buffer);
696 return 0;
697 }
698
Jakob Bornecrantzd991ef02011-10-04 20:13:21 +0200699 return vmw_dmabuf_unpin(dev_priv, vfbd->buffer, false);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000700}
701
Sinclair Yehf89c6c32015-06-26 01:54:28 -0700702/**
703 * vmw_create_dmabuf_proxy - create a proxy surface for the DMA buf
704 *
705 * @dev: DRM device
706 * @mode_cmd: parameters for the new surface
707 * @dmabuf_mob: MOB backing the DMA buf
708 * @srf_out: newly created surface
709 *
710 * When the content FB is a DMA buf, we create a surface as a proxy to the
711 * same buffer. This way we can do a surface copy rather than a surface DMA.
712 * This is a more efficient approach
713 *
714 * RETURNS:
715 * 0 on success, error code otherwise
716 */
717static int vmw_create_dmabuf_proxy(struct drm_device *dev,
718 struct drm_mode_fb_cmd *mode_cmd,
719 struct vmw_dma_buffer *dmabuf_mob,
720 struct vmw_surface **srf_out)
721{
722 uint32_t format;
723 struct drm_vmw_size content_base_size;
724 int ret;
725
726
727 switch (mode_cmd->depth) {
728 case 32:
729 case 24:
730 format = SVGA3D_X8R8G8B8;
731 break;
732
733 case 16:
734 case 15:
735 format = SVGA3D_R5G6B5;
736 break;
737
738 case 8:
739 format = SVGA3D_P8;
740 break;
741
742 default:
743 DRM_ERROR("Invalid framebuffer format %d\n", mode_cmd->depth);
744 return -EINVAL;
745 }
746
747 content_base_size.width = mode_cmd->width;
748 content_base_size.height = mode_cmd->height;
749 content_base_size.depth = 1;
750
751 ret = vmw_surface_gb_priv_define(dev,
752 0, /* kernel visible only */
753 0, /* flags */
754 format,
755 true, /* can be a scanout buffer */
756 1, /* num of mip levels */
757 0,
758 content_base_size,
759 srf_out);
760 if (ret) {
761 DRM_ERROR("Failed to allocate proxy content buffer\n");
762 return ret;
763 }
764
765 /* Use the same MOB backing for surface */
766 vmw_dmabuf_reference(dmabuf_mob);
767
768 (*srf_out)->res.backup = dmabuf_mob;
769
770 /* FIXME: Waiting for fbdev rework to do a proper reserve/pin */
771 ret = vmw_resource_validate(&(*srf_out)->res);
772
773 return ret;
774}
775
776
777
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200778static int vmw_kms_new_framebuffer_dmabuf(struct vmw_private *dev_priv,
779 struct vmw_dma_buffer *dmabuf,
780 struct vmw_framebuffer **out,
781 const struct drm_mode_fb_cmd
782 *mode_cmd)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000783
784{
785 struct drm_device *dev = dev_priv->dev;
786 struct vmw_framebuffer_dmabuf *vfbd;
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200787 unsigned int requested_size;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000788 int ret;
789
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200790 requested_size = mode_cmd->height * mode_cmd->pitch;
791 if (unlikely(requested_size > dmabuf->base.num_pages * PAGE_SIZE)) {
792 DRM_ERROR("Screen buffer object size is too small "
793 "for requested mode.\n");
794 return -EINVAL;
795 }
796
Jakob Bornecrantzc337ada2011-10-04 20:13:34 +0200797 /* Limited framebuffer color depth support for screen objects */
Sinclair Yehc8261a92015-06-26 01:23:42 -0700798 if (dev_priv->active_display_unit == vmw_du_screen_object) {
Jakob Bornecrantzc337ada2011-10-04 20:13:34 +0200799 switch (mode_cmd->depth) {
800 case 32:
801 case 24:
802 /* Only support 32 bpp for 32 and 24 depth fbs */
803 if (mode_cmd->bpp == 32)
804 break;
805
806 DRM_ERROR("Invalid color depth/bbp: %d %d\n",
807 mode_cmd->depth, mode_cmd->bpp);
808 return -EINVAL;
809 case 16:
810 case 15:
811 /* Only support 16 bpp for 16 and 15 depth fbs */
812 if (mode_cmd->bpp == 16)
813 break;
814
815 DRM_ERROR("Invalid color depth/bbp: %d %d\n",
816 mode_cmd->depth, mode_cmd->bpp);
817 return -EINVAL;
818 default:
819 DRM_ERROR("Invalid color depth: %d\n", mode_cmd->depth);
820 return -EINVAL;
821 }
822 }
823
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000824 vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL);
825 if (!vfbd) {
826 ret = -ENOMEM;
827 goto out_err1;
828 }
829
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000830 if (!vmw_dmabuf_reference(dmabuf)) {
831 DRM_ERROR("failed to reference dmabuf %p\n", dmabuf);
Daniel Vetter80f0b5a2012-12-13 23:39:01 +0100832 ret = -EINVAL;
833 goto out_err2;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000834 }
835
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200836 vfbd->base.base.bits_per_pixel = mode_cmd->bpp;
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200837 vfbd->base.base.pitches[0] = mode_cmd->pitch;
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200838 vfbd->base.base.depth = mode_cmd->depth;
839 vfbd->base.base.width = mode_cmd->width;
840 vfbd->base.base.height = mode_cmd->height;
Sinclair Yehc8261a92015-06-26 01:23:42 -0700841 if (dev_priv->active_display_unit == vmw_du_legacy) {
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200842 vfbd->base.pin = vmw_framebuffer_dmabuf_pin;
843 vfbd->base.unpin = vmw_framebuffer_dmabuf_unpin;
844 }
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200845 vfbd->base.dmabuf = true;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000846 vfbd->buffer = dmabuf;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200847 vfbd->base.user_handle = mode_cmd->handle;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000848 *out = &vfbd->base;
849
Daniel Vetter80f0b5a2012-12-13 23:39:01 +0100850 ret = drm_framebuffer_init(dev, &vfbd->base.base,
851 &vmw_framebuffer_dmabuf_funcs);
852 if (ret)
853 goto out_err3;
854
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000855 return 0;
856
857out_err3:
Daniel Vetter80f0b5a2012-12-13 23:39:01 +0100858 vmw_dmabuf_unreference(&dmabuf);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000859out_err2:
860 kfree(vfbd);
861out_err1:
862 return ret;
863}
864
865/*
866 * Generic Kernel modesetting functions
867 */
868
869static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev,
870 struct drm_file *file_priv,
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800871 struct drm_mode_fb_cmd2 *mode_cmd2)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000872{
873 struct vmw_private *dev_priv = vmw_priv(dev);
874 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
875 struct vmw_framebuffer *vfb = NULL;
876 struct vmw_surface *surface = NULL;
877 struct vmw_dma_buffer *bo = NULL;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200878 struct ttm_base_object *user_obj;
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800879 struct drm_mode_fb_cmd mode_cmd;
Sinclair Yehf89c6c32015-06-26 01:54:28 -0700880 bool is_dmabuf_proxy = false;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000881 int ret;
882
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800883 mode_cmd.width = mode_cmd2->width;
884 mode_cmd.height = mode_cmd2->height;
885 mode_cmd.pitch = mode_cmd2->pitches[0];
886 mode_cmd.handle = mode_cmd2->handles[0];
Dave Airlie248dbc22011-11-29 20:02:54 +0000887 drm_fb_get_bpp_depth(mode_cmd2->pixel_format, &mode_cmd.depth,
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800888 &mode_cmd.bpp);
889
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200890 /**
891 * This code should be conditioned on Screen Objects not being used.
892 * If screen objects are used, we can allocate a GMR to hold the
893 * requested framebuffer.
894 */
895
Xi Wang8a783892011-12-21 05:18:33 -0500896 if (!vmw_kms_validate_mode_vram(dev_priv,
Linus Torvalds1a464cb2012-01-10 11:04:36 -0800897 mode_cmd.pitch,
898 mode_cmd.height)) {
Sinclair Yehc8261a92015-06-26 01:23:42 -0700899 DRM_ERROR("Requested mode exceed bounding box limit.\n");
Jakob Bornecrantzd9826402011-11-03 21:03:04 +0100900 return ERR_PTR(-ENOMEM);
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200901 }
902
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200903 /*
904 * Take a reference on the user object of the resource
905 * backing the kms fb. This ensures that user-space handle
906 * lookups on that resource will always work as long as
907 * it's registered with a kms framebuffer. This is important,
908 * since vmw_execbuf_process identifies resources in the
909 * command stream using user-space handles.
910 */
911
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800912 user_obj = ttm_base_object_lookup(tfile, mode_cmd.handle);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200913 if (unlikely(user_obj == NULL)) {
914 DRM_ERROR("Could not locate requested kms frame buffer.\n");
915 return ERR_PTR(-ENOENT);
916 }
917
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200918 /**
919 * End conditioned code.
920 */
921
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100922 /* returns either a dmabuf or surface */
923 ret = vmw_user_lookup_handle(dev_priv, tfile,
Dave Airlie4cf73122011-12-21 09:50:56 +0000924 mode_cmd.handle,
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100925 &surface, &bo);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000926 if (ret)
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100927 goto err_out;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000928
Sinclair Yehf89c6c32015-06-26 01:54:28 -0700929 /*
930 * We cannot use the SurfaceDMA command in an non-accelerated VM,
931 * therefore, wrap the DMA buf in a surface so we can use the
932 * SurfaceCopy command.
933 */
934 if (bo && !(dev_priv->capabilities & SVGA_CAP_3D) &&
935 dev_priv->active_display_unit == vmw_du_screen_target) {
936 ret = vmw_create_dmabuf_proxy(dev_priv->dev, &mode_cmd, bo,
937 &surface);
938 if (ret)
939 goto err_out;
940
941 is_dmabuf_proxy = true;
942 }
943
944 /* Create the new framebuffer depending one what we have */
945 if (surface)
946 ret = vmw_kms_new_framebuffer_surface(dev_priv, file_priv,
947 surface, &vfb, &mode_cmd,
948 is_dmabuf_proxy);
949 else if (bo)
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100950 ret = vmw_kms_new_framebuffer_dmabuf(dev_priv, bo, &vfb,
Dave Airlie4cf73122011-12-21 09:50:56 +0000951 &mode_cmd);
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100952 else
953 BUG();
Jakob Bornecrantz5ffdb652010-01-30 03:38:08 +0000954
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100955err_out:
956 /* vmw_user_lookup_handle takes one ref so does new_fb */
957 if (bo)
958 vmw_dmabuf_unreference(&bo);
959 if (surface)
960 vmw_surface_unreference(&surface);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000961
962 if (ret) {
963 DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200964 ttm_base_object_unref(&user_obj);
Chris Wilsoncce13ff2010-08-08 13:36:38 +0100965 return ERR_PTR(ret);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200966 } else
967 vfb->user_obj = user_obj;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000968
969 return &vfb->base;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000970}
971
Laurent Pincharte6ecefa2012-05-17 13:27:23 +0200972static const struct drm_mode_config_funcs vmw_kms_funcs = {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000973 .fb_create = vmw_kms_fb_create,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000974};
975
Sinclair Yehc8261a92015-06-26 01:23:42 -0700976int vmw_kms_generic_present(struct vmw_private *dev_priv,
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200977 struct drm_file *file_priv,
978 struct vmw_framebuffer *vfb,
979 struct vmw_surface *surface,
980 uint32_t sid,
981 int32_t destX, int32_t destY,
982 struct drm_vmw_rect *clips,
983 uint32_t num_clips)
984{
Thomas Hellstrom10b1e0c2015-06-26 02:14:27 -0700985 return vmw_kms_sou_do_surface_dirty(dev_priv, vfb, NULL, clips,
986 &surface->res, destX, destY,
987 num_clips, 1, NULL);
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200988}
989
Sinclair Yehc8261a92015-06-26 01:23:42 -0700990int vmw_kms_present(struct vmw_private *dev_priv,
991 struct drm_file *file_priv,
992 struct vmw_framebuffer *vfb,
993 struct vmw_surface *surface,
994 uint32_t sid,
995 int32_t destX, int32_t destY,
996 struct drm_vmw_rect *clips,
997 uint32_t num_clips)
998{
Sinclair Yeh35c05122015-06-26 01:42:06 -0700999 int ret;
1000
1001 if (dev_priv->active_display_unit == vmw_du_screen_target)
1002 ret = vmw_kms_stdu_present(dev_priv, file_priv, vfb, sid,
1003 destX, destY, clips, num_clips);
1004 else
1005 ret = vmw_kms_generic_present(dev_priv, file_priv, vfb,
1006 surface, sid, destX, destY,
1007 clips, num_clips);
1008 if (ret)
1009 return ret;
1010
1011 vmw_fifo_flush(dev_priv, false);
1012
1013 return 0;
Sinclair Yehc8261a92015-06-26 01:23:42 -07001014}
1015
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001016int vmw_kms_init(struct vmw_private *dev_priv)
1017{
1018 struct drm_device *dev = dev_priv->dev;
1019 int ret;
1020
1021 drm_mode_config_init(dev);
1022 dev->mode_config.funcs = &vmw_kms_funcs;
Jakob Bornecrantz3bef3572010-02-09 19:41:57 +00001023 dev->mode_config.min_width = 1;
1024 dev->mode_config.min_height = 1;
Jakob Bornecrantz7e71f8a2010-05-28 11:21:54 +02001025 /* assumed largest fb size */
1026 dev->mode_config.max_width = 8192;
1027 dev->mode_config.max_height = 8192;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001028
Sinclair Yeh35c05122015-06-26 01:42:06 -07001029 ret = vmw_kms_stdu_init_display(dev_priv);
1030 if (ret) {
1031 ret = vmw_kms_sou_init_display(dev_priv);
1032 if (ret) /* Fallback */
1033 ret = vmw_kms_ldu_init_display(dev_priv);
1034 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001035
Sinclair Yehc8261a92015-06-26 01:23:42 -07001036 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001037}
1038
1039int vmw_kms_close(struct vmw_private *dev_priv)
1040{
Sinclair Yehc8261a92015-06-26 01:23:42 -07001041 int ret;
1042
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001043 /*
1044 * Docs says we should take the lock before calling this function
1045 * but since it destroys encoders and our destructor calls
1046 * drm_encoder_cleanup which takes the lock we deadlock.
1047 */
1048 drm_mode_config_cleanup(dev_priv->dev);
Sinclair Yehc8261a92015-06-26 01:23:42 -07001049 if (dev_priv->active_display_unit == vmw_du_screen_object)
1050 ret = vmw_kms_sou_close_display(dev_priv);
Sinclair Yeh35c05122015-06-26 01:42:06 -07001051 else if (dev_priv->active_display_unit == vmw_du_screen_target)
1052 ret = vmw_kms_stdu_close_display(dev_priv);
Jakob Bornecrantzc0d18312011-11-09 10:25:26 +01001053 else
Sinclair Yehc8261a92015-06-26 01:23:42 -07001054 ret = vmw_kms_ldu_close_display(dev_priv);
1055
1056 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001057}
1058
1059int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
1060 struct drm_file *file_priv)
1061{
1062 struct drm_vmw_cursor_bypass_arg *arg = data;
1063 struct vmw_display_unit *du;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001064 struct drm_crtc *crtc;
1065 int ret = 0;
1066
1067
1068 mutex_lock(&dev->mode_config.mutex);
1069 if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
1070
1071 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1072 du = vmw_crtc_to_du(crtc);
1073 du->hotspot_x = arg->xhot;
1074 du->hotspot_y = arg->yhot;
1075 }
1076
1077 mutex_unlock(&dev->mode_config.mutex);
1078 return 0;
1079 }
1080
Rob Clarka4cd5d62014-07-17 23:30:02 -04001081 crtc = drm_crtc_find(dev, arg->crtc_id);
1082 if (!crtc) {
Ville Syrjälä4ae87ff2013-10-17 13:35:05 +03001083 ret = -ENOENT;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001084 goto out;
1085 }
1086
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001087 du = vmw_crtc_to_du(crtc);
1088
1089 du->hotspot_x = arg->xhot;
1090 du->hotspot_y = arg->yhot;
1091
1092out:
1093 mutex_unlock(&dev->mode_config.mutex);
1094
1095 return ret;
1096}
1097
Michel Dänzer0bef23f2011-08-31 07:42:50 +00001098int vmw_kms_write_svga(struct vmw_private *vmw_priv,
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001099 unsigned width, unsigned height, unsigned pitch,
Michel Dänzer6558429b2011-08-31 07:42:49 +00001100 unsigned bpp, unsigned depth)
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001101{
1102 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1103 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch);
1104 else if (vmw_fifo_have_pitchlock(vmw_priv))
1105 iowrite32(pitch, vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
1106 vmw_write(vmw_priv, SVGA_REG_WIDTH, width);
1107 vmw_write(vmw_priv, SVGA_REG_HEIGHT, height);
Michel Dänzer6558429b2011-08-31 07:42:49 +00001108 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp);
Michel Dänzer0bef23f2011-08-31 07:42:50 +00001109
1110 if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) {
1111 DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n",
1112 depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH));
1113 return -EINVAL;
1114 }
1115
1116 return 0;
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001117}
1118
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001119int vmw_kms_save_vga(struct vmw_private *vmw_priv)
1120{
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001121 struct vmw_vga_topology_state *save;
1122 uint32_t i;
1123
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001124 vmw_priv->vga_width = vmw_read(vmw_priv, SVGA_REG_WIDTH);
1125 vmw_priv->vga_height = vmw_read(vmw_priv, SVGA_REG_HEIGHT);
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001126 vmw_priv->vga_bpp = vmw_read(vmw_priv, SVGA_REG_BITS_PER_PIXEL);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001127 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1128 vmw_priv->vga_pitchlock =
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001129 vmw_read(vmw_priv, SVGA_REG_PITCHLOCK);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001130 else if (vmw_fifo_have_pitchlock(vmw_priv))
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001131 vmw_priv->vga_pitchlock = ioread32(vmw_priv->mmio_virt +
Sinclair Yehc8261a92015-06-26 01:23:42 -07001132 SVGA_FIFO_PITCHLOCK);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001133
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001134 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1135 return 0;
1136
1137 vmw_priv->num_displays = vmw_read(vmw_priv,
1138 SVGA_REG_NUM_GUEST_DISPLAYS);
1139
Thomas Hellstrom029e50b2010-10-05 12:43:08 +02001140 if (vmw_priv->num_displays == 0)
1141 vmw_priv->num_displays = 1;
1142
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001143 for (i = 0; i < vmw_priv->num_displays; ++i) {
1144 save = &vmw_priv->vga_save[i];
1145 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1146 save->primary = vmw_read(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY);
1147 save->pos_x = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_X);
1148 save->pos_y = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y);
1149 save->width = vmw_read(vmw_priv, SVGA_REG_DISPLAY_WIDTH);
1150 save->height = vmw_read(vmw_priv, SVGA_REG_DISPLAY_HEIGHT);
1151 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
Thomas Hellstrom30c78bb2010-10-01 10:21:48 +02001152 if (i == 0 && vmw_priv->num_displays == 1 &&
1153 save->width == 0 && save->height == 0) {
1154
1155 /*
1156 * It should be fairly safe to assume that these
1157 * values are uninitialized.
1158 */
1159
1160 save->width = vmw_priv->vga_width - save->pos_x;
1161 save->height = vmw_priv->vga_height - save->pos_y;
1162 }
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001163 }
Thomas Hellstrom30c78bb2010-10-01 10:21:48 +02001164
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001165 return 0;
1166}
1167
1168int vmw_kms_restore_vga(struct vmw_private *vmw_priv)
1169{
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001170 struct vmw_vga_topology_state *save;
1171 uint32_t i;
1172
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001173 vmw_write(vmw_priv, SVGA_REG_WIDTH, vmw_priv->vga_width);
1174 vmw_write(vmw_priv, SVGA_REG_HEIGHT, vmw_priv->vga_height);
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001175 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, vmw_priv->vga_bpp);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001176 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1177 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK,
1178 vmw_priv->vga_pitchlock);
1179 else if (vmw_fifo_have_pitchlock(vmw_priv))
1180 iowrite32(vmw_priv->vga_pitchlock,
1181 vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001182
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001183 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1184 return 0;
1185
1186 for (i = 0; i < vmw_priv->num_displays; ++i) {
1187 save = &vmw_priv->vga_save[i];
1188 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1189 vmw_write(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY, save->primary);
1190 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_X, save->pos_x);
1191 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y, save->pos_y);
1192 vmw_write(vmw_priv, SVGA_REG_DISPLAY_WIDTH, save->width);
1193 vmw_write(vmw_priv, SVGA_REG_DISPLAY_HEIGHT, save->height);
1194 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
1195 }
1196
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001197 return 0;
1198}
Jakob Bornecrantzd8bd19d2010-06-01 11:54:20 +02001199
Thomas Hellstrome133e732010-10-05 12:43:04 +02001200bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
1201 uint32_t pitch,
1202 uint32_t height)
1203{
Sinclair Yeh35c05122015-06-26 01:42:06 -07001204 return ((u64) pitch * (u64) height) < (u64)
1205 ((dev_priv->active_display_unit == vmw_du_screen_target) ?
1206 dev_priv->prim_bb_mem : dev_priv->vram_size);
Thomas Hellstrome133e732010-10-05 12:43:04 +02001207}
1208
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001209
1210/**
1211 * Function called by DRM code called with vbl_lock held.
1212 */
Thomas Hellstrom7a1c2f62010-10-01 10:21:49 +02001213u32 vmw_get_vblank_counter(struct drm_device *dev, int crtc)
1214{
1215 return 0;
1216}
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001217
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001218/**
1219 * Function called by DRM code called with vbl_lock held.
1220 */
1221int vmw_enable_vblank(struct drm_device *dev, int crtc)
1222{
1223 return -ENOSYS;
1224}
1225
1226/**
1227 * Function called by DRM code called with vbl_lock held.
1228 */
1229void vmw_disable_vblank(struct drm_device *dev, int crtc)
1230{
1231}
1232
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001233
1234/*
1235 * Small shared kms functions.
1236 */
1237
Rashika Kheria847c5962014-01-06 22:18:10 +05301238static int vmw_du_update_layout(struct vmw_private *dev_priv, unsigned num,
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001239 struct drm_vmw_rect *rects)
1240{
1241 struct drm_device *dev = dev_priv->dev;
1242 struct vmw_display_unit *du;
1243 struct drm_connector *con;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001244
1245 mutex_lock(&dev->mode_config.mutex);
1246
1247#if 0
Thomas Hellstrom6ea77d12011-10-04 20:13:36 +02001248 {
1249 unsigned int i;
1250
1251 DRM_INFO("%s: new layout ", __func__);
1252 for (i = 0; i < num; i++)
1253 DRM_INFO("(%i, %i %ux%u) ", rects[i].x, rects[i].y,
1254 rects[i].w, rects[i].h);
1255 DRM_INFO("\n");
1256 }
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001257#endif
1258
1259 list_for_each_entry(con, &dev->mode_config.connector_list, head) {
1260 du = vmw_connector_to_du(con);
1261 if (num > du->unit) {
1262 du->pref_width = rects[du->unit].w;
1263 du->pref_height = rects[du->unit].h;
1264 du->pref_active = true;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001265 du->gui_x = rects[du->unit].x;
1266 du->gui_y = rects[du->unit].y;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001267 } else {
1268 du->pref_width = 800;
1269 du->pref_height = 600;
1270 du->pref_active = false;
1271 }
1272 con->status = vmw_du_connector_detect(con, true);
1273 }
1274
1275 mutex_unlock(&dev->mode_config.mutex);
1276
1277 return 0;
1278}
1279
1280void vmw_du_crtc_save(struct drm_crtc *crtc)
1281{
1282}
1283
1284void vmw_du_crtc_restore(struct drm_crtc *crtc)
1285{
1286}
1287
1288void vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
1289 u16 *r, u16 *g, u16 *b,
1290 uint32_t start, uint32_t size)
1291{
1292 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
1293 int i;
1294
1295 for (i = 0; i < size; i++) {
1296 DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i,
1297 r[i], g[i], b[i]);
1298 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 0, r[i] >> 8);
1299 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 1, g[i] >> 8);
1300 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 2, b[i] >> 8);
1301 }
1302}
1303
1304void vmw_du_connector_dpms(struct drm_connector *connector, int mode)
1305{
1306}
1307
1308void vmw_du_connector_save(struct drm_connector *connector)
1309{
1310}
1311
1312void vmw_du_connector_restore(struct drm_connector *connector)
1313{
1314}
1315
1316enum drm_connector_status
1317vmw_du_connector_detect(struct drm_connector *connector, bool force)
1318{
1319 uint32_t num_displays;
1320 struct drm_device *dev = connector->dev;
1321 struct vmw_private *dev_priv = vmw_priv(dev);
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001322 struct vmw_display_unit *du = vmw_connector_to_du(connector);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001323
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001324 num_displays = vmw_read(dev_priv, SVGA_REG_NUM_DISPLAYS);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001325
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001326 return ((vmw_connector_to_du(connector)->unit < num_displays &&
1327 du->pref_active) ?
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001328 connector_status_connected : connector_status_disconnected);
1329}
1330
1331static struct drm_display_mode vmw_kms_connector_builtin[] = {
1332 /* 640x480@60Hz */
1333 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
1334 752, 800, 0, 480, 489, 492, 525, 0,
1335 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1336 /* 800x600@60Hz */
1337 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
1338 968, 1056, 0, 600, 601, 605, 628, 0,
1339 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1340 /* 1024x768@60Hz */
1341 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
1342 1184, 1344, 0, 768, 771, 777, 806, 0,
1343 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1344 /* 1152x864@75Hz */
1345 { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
1346 1344, 1600, 0, 864, 865, 868, 900, 0,
1347 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1348 /* 1280x768@60Hz */
1349 { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344,
1350 1472, 1664, 0, 768, 771, 778, 798, 0,
1351 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1352 /* 1280x800@60Hz */
1353 { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352,
1354 1480, 1680, 0, 800, 803, 809, 831, 0,
1355 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
1356 /* 1280x960@60Hz */
1357 { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376,
1358 1488, 1800, 0, 960, 961, 964, 1000, 0,
1359 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1360 /* 1280x1024@60Hz */
1361 { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328,
1362 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
1363 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1364 /* 1360x768@60Hz */
1365 { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424,
1366 1536, 1792, 0, 768, 771, 777, 795, 0,
1367 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1368 /* 1440x1050@60Hz */
1369 { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488,
1370 1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
1371 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1372 /* 1440x900@60Hz */
1373 { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520,
1374 1672, 1904, 0, 900, 903, 909, 934, 0,
1375 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1376 /* 1600x1200@60Hz */
1377 { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664,
1378 1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
1379 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1380 /* 1680x1050@60Hz */
1381 { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784,
1382 1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
1383 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1384 /* 1792x1344@60Hz */
1385 { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920,
1386 2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
1387 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1388 /* 1853x1392@60Hz */
1389 { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952,
1390 2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
1391 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1392 /* 1920x1200@60Hz */
1393 { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056,
1394 2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
1395 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1396 /* 1920x1440@60Hz */
1397 { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048,
1398 2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
1399 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1400 /* 2560x1600@60Hz */
1401 { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752,
1402 3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
1403 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1404 /* Terminate */
1405 { DRM_MODE("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) },
1406};
1407
Thomas Hellstrom1543b4d2011-11-02 09:43:10 +01001408/**
1409 * vmw_guess_mode_timing - Provide fake timings for a
1410 * 60Hz vrefresh mode.
1411 *
1412 * @mode - Pointer to a struct drm_display_mode with hdisplay and vdisplay
1413 * members filled in.
1414 */
1415static void vmw_guess_mode_timing(struct drm_display_mode *mode)
1416{
1417 mode->hsync_start = mode->hdisplay + 50;
1418 mode->hsync_end = mode->hsync_start + 50;
1419 mode->htotal = mode->hsync_end + 50;
1420
1421 mode->vsync_start = mode->vdisplay + 50;
1422 mode->vsync_end = mode->vsync_start + 50;
1423 mode->vtotal = mode->vsync_end + 50;
1424
1425 mode->clock = (u32)mode->htotal * (u32)mode->vtotal / 100 * 6;
1426 mode->vrefresh = drm_mode_vrefresh(mode);
1427}
1428
1429
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001430int vmw_du_connector_fill_modes(struct drm_connector *connector,
1431 uint32_t max_width, uint32_t max_height)
1432{
1433 struct vmw_display_unit *du = vmw_connector_to_du(connector);
1434 struct drm_device *dev = connector->dev;
1435 struct vmw_private *dev_priv = vmw_priv(dev);
1436 struct drm_display_mode *mode = NULL;
1437 struct drm_display_mode *bmode;
1438 struct drm_display_mode prefmode = { DRM_MODE("preferred",
1439 DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
1440 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1441 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
1442 };
1443 int i;
Sinclair Yeh9a723842014-10-31 09:58:06 +01001444 u32 assumed_bpp = 2;
1445
1446 /*
1447 * If using screen objects, then assume 32-bpp because that's what the
1448 * SVGA device is assuming
1449 */
Sinclair Yehc8261a92015-06-26 01:23:42 -07001450 if (dev_priv->active_display_unit == vmw_du_screen_object)
Sinclair Yeh9a723842014-10-31 09:58:06 +01001451 assumed_bpp = 4;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001452
Sinclair Yeh35c05122015-06-26 01:42:06 -07001453 if (dev_priv->active_display_unit == vmw_du_screen_target) {
1454 max_width = min(max_width, dev_priv->stdu_max_width);
1455 max_height = min(max_height, dev_priv->stdu_max_height);
1456 }
1457
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001458 /* Add preferred mode */
Sinclair Yehc8261a92015-06-26 01:23:42 -07001459 mode = drm_mode_duplicate(dev, &prefmode);
1460 if (!mode)
1461 return 0;
1462 mode->hdisplay = du->pref_width;
1463 mode->vdisplay = du->pref_height;
1464 vmw_guess_mode_timing(mode);
Jakob Bornecrantz55bde5b2011-11-03 21:03:05 +01001465
Sinclair Yehc8261a92015-06-26 01:23:42 -07001466 if (vmw_kms_validate_mode_vram(dev_priv,
1467 mode->hdisplay * assumed_bpp,
1468 mode->vdisplay)) {
1469 drm_mode_probed_add(connector, mode);
1470 } else {
1471 drm_mode_destroy(dev, mode);
1472 mode = NULL;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001473 }
1474
Sinclair Yehc8261a92015-06-26 01:23:42 -07001475 if (du->pref_mode) {
1476 list_del_init(&du->pref_mode->head);
1477 drm_mode_destroy(dev, du->pref_mode);
1478 }
1479
1480 /* mode might be null here, this is intended */
1481 du->pref_mode = mode;
1482
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001483 for (i = 0; vmw_kms_connector_builtin[i].type != 0; i++) {
1484 bmode = &vmw_kms_connector_builtin[i];
1485 if (bmode->hdisplay > max_width ||
1486 bmode->vdisplay > max_height)
1487 continue;
1488
Sinclair Yeh9a723842014-10-31 09:58:06 +01001489 if (!vmw_kms_validate_mode_vram(dev_priv,
1490 bmode->hdisplay * assumed_bpp,
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001491 bmode->vdisplay))
1492 continue;
1493
1494 mode = drm_mode_duplicate(dev, bmode);
1495 if (!mode)
1496 return 0;
1497 mode->vrefresh = drm_mode_vrefresh(mode);
1498
1499 drm_mode_probed_add(connector, mode);
1500 }
1501
Jakob Bornecrantzd41025c2011-11-03 21:03:07 +01001502 /* Move the prefered mode first, help apps pick the right mode. */
1503 if (du->pref_mode)
1504 list_move(&du->pref_mode->head, &connector->probed_modes);
1505
Dave Airlieb87577b2014-05-01 09:26:53 +10001506 drm_mode_connector_list_update(connector, true);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001507
1508 return 1;
1509}
1510
1511int vmw_du_connector_set_property(struct drm_connector *connector,
1512 struct drm_property *property,
1513 uint64_t val)
1514{
1515 return 0;
1516}
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001517
1518
1519int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data,
1520 struct drm_file *file_priv)
1521{
1522 struct vmw_private *dev_priv = vmw_priv(dev);
1523 struct drm_vmw_update_layout_arg *arg =
1524 (struct drm_vmw_update_layout_arg *)data;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001525 void __user *user_rects;
1526 struct drm_vmw_rect *rects;
1527 unsigned rects_size;
1528 int ret;
1529 int i;
1530 struct drm_mode_config *mode_config = &dev->mode_config;
Sinclair Yehc8261a92015-06-26 01:23:42 -07001531 struct drm_vmw_rect bounding_box = {0};
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001532
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001533 if (!arg->num_outputs) {
1534 struct drm_vmw_rect def_rect = {0, 0, 800, 600};
1535 vmw_du_update_layout(dev_priv, 1, &def_rect);
Thomas Hellstrom5151adb2015-03-09 01:56:21 -07001536 return 0;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001537 }
1538
1539 rects_size = arg->num_outputs * sizeof(struct drm_vmw_rect);
Xi Wangbab9efc2011-11-28 12:25:43 +01001540 rects = kcalloc(arg->num_outputs, sizeof(struct drm_vmw_rect),
1541 GFP_KERNEL);
Thomas Hellstrom5151adb2015-03-09 01:56:21 -07001542 if (unlikely(!rects))
1543 return -ENOMEM;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001544
1545 user_rects = (void __user *)(unsigned long)arg->rects;
1546 ret = copy_from_user(rects, user_rects, rects_size);
1547 if (unlikely(ret != 0)) {
1548 DRM_ERROR("Failed to get rects.\n");
1549 ret = -EFAULT;
1550 goto out_free;
1551 }
1552
1553 for (i = 0; i < arg->num_outputs; ++i) {
Xi Wangbab9efc2011-11-28 12:25:43 +01001554 if (rects[i].x < 0 ||
1555 rects[i].y < 0 ||
1556 rects[i].x + rects[i].w > mode_config->max_width ||
1557 rects[i].y + rects[i].h > mode_config->max_height) {
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001558 DRM_ERROR("Invalid GUI layout.\n");
1559 ret = -EINVAL;
1560 goto out_free;
1561 }
Sinclair Yehc8261a92015-06-26 01:23:42 -07001562
1563 /*
1564 * bounding_box.w and bunding_box.h are used as
1565 * lower-right coordinates
1566 */
1567 if (rects[i].x + rects[i].w > bounding_box.w)
1568 bounding_box.w = rects[i].x + rects[i].w;
1569
1570 if (rects[i].y + rects[i].h > bounding_box.h)
1571 bounding_box.h = rects[i].y + rects[i].h;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001572 }
1573
Sinclair Yeh35c05122015-06-26 01:42:06 -07001574 /*
1575 * For Screen Target Display Unit, all the displays must fit
1576 * inside of maximum texture size.
1577 */
1578 if (dev_priv->active_display_unit == vmw_du_screen_target)
1579 if (bounding_box.w > dev_priv->texture_max_width ||
1580 bounding_box.h > dev_priv->texture_max_height) {
1581 DRM_ERROR("Layout exceeds maximum texture size\n");
1582 ret = -EINVAL;
1583 goto out_free;
1584 }
1585
1586
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001587 vmw_du_update_layout(dev_priv, arg->num_outputs, rects);
1588
1589out_free:
1590 kfree(rects);
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001591 return ret;
1592}
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07001593
1594/**
1595 * vmw_kms_helper_dirty - Helper to build commands and perform actions based
1596 * on a set of cliprects and a set of display units.
1597 *
1598 * @dev_priv: Pointer to a device private structure.
1599 * @framebuffer: Pointer to the framebuffer on which to perform the actions.
1600 * @clips: A set of struct drm_clip_rect. Either this os @vclips must be NULL.
1601 * Cliprects are given in framebuffer coordinates.
1602 * @vclips: A set of struct drm_vmw_rect cliprects. Either this or @clips must
1603 * be NULL. Cliprects are given in source coordinates.
1604 * @dest_x: X coordinate offset for the crtc / destination clip rects.
1605 * @dest_y: Y coordinate offset for the crtc / destination clip rects.
1606 * @num_clips: Number of cliprects in the @clips or @vclips array.
1607 * @increment: Integer with which to increment the clip counter when looping.
1608 * Used to skip a predetermined number of clip rects.
1609 * @dirty: Closure structure. See the description of struct vmw_kms_dirty.
1610 */
1611int vmw_kms_helper_dirty(struct vmw_private *dev_priv,
1612 struct vmw_framebuffer *framebuffer,
1613 const struct drm_clip_rect *clips,
1614 const struct drm_vmw_rect *vclips,
1615 s32 dest_x, s32 dest_y,
1616 int num_clips,
1617 int increment,
1618 struct vmw_kms_dirty *dirty)
1619{
1620 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
1621 struct drm_crtc *crtc;
1622 u32 num_units = 0;
1623 u32 i, k;
1624 int ret;
1625
1626 dirty->dev_priv = dev_priv;
1627
1628 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
1629 if (crtc->primary->fb != &framebuffer->base)
1630 continue;
1631 units[num_units++] = vmw_crtc_to_du(crtc);
1632 }
1633
1634 for (k = 0; k < num_units; k++) {
1635 struct vmw_display_unit *unit = units[k];
1636 s32 crtc_x = unit->crtc.x;
1637 s32 crtc_y = unit->crtc.y;
1638 s32 crtc_width = unit->crtc.mode.hdisplay;
1639 s32 crtc_height = unit->crtc.mode.vdisplay;
1640 const struct drm_clip_rect *clips_ptr = clips;
1641 const struct drm_vmw_rect *vclips_ptr = vclips;
1642
1643 dirty->unit = unit;
1644 if (dirty->fifo_reserve_size > 0) {
1645 dirty->cmd = vmw_fifo_reserve(dev_priv,
1646 dirty->fifo_reserve_size);
1647 if (!dirty->cmd) {
1648 DRM_ERROR("Couldn't reserve fifo space "
1649 "for dirty blits.\n");
1650 return ret;
1651 }
1652 memset(dirty->cmd, 0, dirty->fifo_reserve_size);
1653 }
1654 dirty->num_hits = 0;
1655 for (i = 0; i < num_clips; i++, clips_ptr += increment,
1656 vclips_ptr += increment) {
1657 s32 clip_left;
1658 s32 clip_top;
1659
1660 /*
1661 * Select clip array type. Note that integer type
1662 * in @clips is unsigned short, whereas in @vclips
1663 * it's 32-bit.
1664 */
1665 if (clips) {
1666 dirty->fb_x = (s32) clips_ptr->x1;
1667 dirty->fb_y = (s32) clips_ptr->y1;
1668 dirty->unit_x2 = (s32) clips_ptr->x2 + dest_x -
1669 crtc_x;
1670 dirty->unit_y2 = (s32) clips_ptr->y2 + dest_y -
1671 crtc_y;
1672 } else {
1673 dirty->fb_x = vclips_ptr->x;
1674 dirty->fb_y = vclips_ptr->y;
1675 dirty->unit_x2 = dirty->fb_x + vclips_ptr->w +
1676 dest_x - crtc_x;
1677 dirty->unit_y2 = dirty->fb_y + vclips_ptr->h +
1678 dest_y - crtc_y;
1679 }
1680
1681 dirty->unit_x1 = dirty->fb_x + dest_x - crtc_x;
1682 dirty->unit_y1 = dirty->fb_y + dest_y - crtc_y;
1683
1684 /* Skip this clip if it's outside the crtc region */
1685 if (dirty->unit_x1 >= crtc_width ||
1686 dirty->unit_y1 >= crtc_height ||
1687 dirty->unit_x2 <= 0 || dirty->unit_y2 <= 0)
1688 continue;
1689
1690 /* Clip right and bottom to crtc limits */
1691 dirty->unit_x2 = min_t(s32, dirty->unit_x2,
1692 crtc_width);
1693 dirty->unit_y2 = min_t(s32, dirty->unit_y2,
1694 crtc_height);
1695
1696 /* Clip left and top to crtc limits */
1697 clip_left = min_t(s32, dirty->unit_x1, 0);
1698 clip_top = min_t(s32, dirty->unit_y1, 0);
1699 dirty->unit_x1 -= clip_left;
1700 dirty->unit_y1 -= clip_top;
1701 dirty->fb_x -= clip_left;
1702 dirty->fb_y -= clip_top;
1703
1704 dirty->clip(dirty);
1705 }
1706
1707 dirty->fifo_commit(dirty);
1708 }
1709
1710 return 0;
1711}
1712
1713/**
1714 * vmw_kms_helper_buffer_prepare - Reserve and validate a buffer object before
1715 * command submission.
1716 *
1717 * @dev_priv. Pointer to a device private structure.
1718 * @buf: The buffer object
1719 * @interruptible: Whether to perform waits as interruptible.
1720 * @validate_as_mob: Whether the buffer should be validated as a MOB. If false,
1721 * The buffer will be validated as a GMR. Already pinned buffers will not be
1722 * validated.
1723 *
1724 * Returns 0 on success, negative error code on failure, -ERESTARTSYS if
1725 * interrupted by a signal.
1726 */
1727int vmw_kms_helper_buffer_prepare(struct vmw_private *dev_priv,
1728 struct vmw_dma_buffer *buf,
1729 bool interruptible,
1730 bool validate_as_mob)
1731{
1732 struct ttm_buffer_object *bo = &buf->base;
1733 int ret;
1734
1735 ttm_bo_reserve(bo, false, false, interruptible, 0);
1736 ret = vmw_validate_single_buffer(dev_priv, bo, interruptible,
1737 validate_as_mob);
1738 if (ret)
1739 ttm_bo_unreserve(bo);
1740
1741 return ret;
1742}
1743
1744/**
1745 * vmw_kms_helper_buffer_revert - Undo the actions of
1746 * vmw_kms_helper_buffer_prepare.
1747 *
1748 * @res: Pointer to the buffer object.
1749 *
1750 * Helper to be used if an error forces the caller to undo the actions of
1751 * vmw_kms_helper_buffer_prepare.
1752 */
1753void vmw_kms_helper_buffer_revert(struct vmw_dma_buffer *buf)
1754{
1755 if (buf)
1756 ttm_bo_unreserve(&buf->base);
1757}
1758
1759/**
1760 * vmw_kms_helper_buffer_finish - Unreserve and fence a buffer object after
1761 * kms command submission.
1762 *
1763 * @dev_priv: Pointer to a device private structure.
1764 * @file_priv: Pointer to a struct drm_file representing the caller's
1765 * connection. Must be set to NULL if @user_fence_rep is NULL, and conversely
1766 * if non-NULL, @user_fence_rep must be non-NULL.
1767 * @buf: The buffer object.
1768 * @out_fence: Optional pointer to a fence pointer. If non-NULL, a
1769 * ref-counted fence pointer is returned here.
1770 * @user_fence_rep: Optional pointer to a user-space provided struct
1771 * drm_vmw_fence_rep. If provided, @file_priv must also be provided and the
1772 * function copies fence data to user-space in a fail-safe manner.
1773 */
1774void vmw_kms_helper_buffer_finish(struct vmw_private *dev_priv,
1775 struct drm_file *file_priv,
1776 struct vmw_dma_buffer *buf,
1777 struct vmw_fence_obj **out_fence,
1778 struct drm_vmw_fence_rep __user *
1779 user_fence_rep)
1780{
1781 struct vmw_fence_obj *fence;
1782 uint32_t handle;
1783 int ret;
1784
1785 ret = vmw_execbuf_fence_commands(file_priv, dev_priv, &fence,
1786 file_priv ? &handle : NULL);
1787 if (buf)
1788 vmw_fence_single_bo(&buf->base, fence);
1789 if (file_priv)
1790 vmw_execbuf_copy_fence_user(dev_priv, vmw_fpriv(file_priv),
1791 ret, user_fence_rep, fence,
1792 handle);
1793 if (out_fence)
1794 *out_fence = fence;
1795 else
1796 vmw_fence_obj_unreference(&fence);
1797
1798 vmw_kms_helper_buffer_revert(buf);
1799}
1800
1801
1802/**
1803 * vmw_kms_helper_resource_revert - Undo the actions of
1804 * vmw_kms_helper_resource_prepare.
1805 *
1806 * @res: Pointer to the resource. Typically a surface.
1807 *
1808 * Helper to be used if an error forces the caller to undo the actions of
1809 * vmw_kms_helper_resource_prepare.
1810 */
1811void vmw_kms_helper_resource_revert(struct vmw_resource *res)
1812{
1813 vmw_kms_helper_buffer_revert(res->backup);
1814 vmw_resource_unreserve(res, NULL, 0);
1815 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
1816}
1817
1818/**
1819 * vmw_kms_helper_resource_prepare - Reserve and validate a resource before
1820 * command submission.
1821 *
1822 * @res: Pointer to the resource. Typically a surface.
1823 * @interruptible: Whether to perform waits as interruptible.
1824 *
1825 * Reserves and validates also the backup buffer if a guest-backed resource.
1826 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
1827 * interrupted by a signal.
1828 */
1829int vmw_kms_helper_resource_prepare(struct vmw_resource *res,
1830 bool interruptible)
1831{
1832 int ret = 0;
1833
1834 if (interruptible)
1835 ret = mutex_lock_interruptible(&res->dev_priv->cmdbuf_mutex);
1836 else
1837 mutex_lock(&res->dev_priv->cmdbuf_mutex);
1838
1839 if (unlikely(ret != 0))
1840 return -ERESTARTSYS;
1841
1842 ret = vmw_resource_reserve(res, interruptible, false);
1843 if (ret)
1844 goto out_unlock;
1845
1846 if (res->backup) {
1847 ret = vmw_kms_helper_buffer_prepare(res->dev_priv, res->backup,
1848 interruptible,
1849 res->dev_priv->has_mob);
1850 if (ret)
1851 goto out_unreserve;
1852 }
1853 ret = vmw_resource_validate(res);
1854 if (ret)
1855 goto out_revert;
1856 return 0;
1857
1858out_revert:
1859 vmw_kms_helper_buffer_revert(res->backup);
1860out_unreserve:
1861 vmw_resource_unreserve(res, NULL, 0);
1862out_unlock:
1863 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
1864 return ret;
1865}
1866
1867/**
1868 * vmw_kms_helper_resource_finish - Unreserve and fence a resource after
1869 * kms command submission.
1870 *
1871 * @res: Pointer to the resource. Typically a surface.
1872 * @out_fence: Optional pointer to a fence pointer. If non-NULL, a
1873 * ref-counted fence pointer is returned here.
1874 */
1875void vmw_kms_helper_resource_finish(struct vmw_resource *res,
1876 struct vmw_fence_obj **out_fence)
1877{
1878 if (res->backup || out_fence)
1879 vmw_kms_helper_buffer_finish(res->dev_priv, NULL, res->backup,
1880 out_fence, NULL);
1881
1882 vmw_resource_unreserve(res, NULL, 0);
1883 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
1884}