blob: ae87e7ec06efb5af3fe52d8ac349e437bf35da92 [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);
372
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000373 drm_framebuffer_cleanup(framebuffer);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200374 vmw_surface_unreference(&vfbs->surface);
Thomas Hellstroma2787242015-06-29 12:55:07 -0700375 if (vfbs->base.user_obj)
376 ttm_base_object_unref(&vfbs->base.user_obj);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000377
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200378 kfree(vfbs);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000379}
380
Rashika Kheria847c5962014-01-06 22:18:10 +0530381static int vmw_framebuffer_surface_dirty(struct drm_framebuffer *framebuffer,
Thomas Hellstrom02b00162010-10-05 12:43:02 +0200382 struct drm_file *file_priv,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000383 unsigned flags, unsigned color,
384 struct drm_clip_rect *clips,
385 unsigned num_clips)
386{
387 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
388 struct vmw_framebuffer_surface *vfbs =
389 vmw_framebuffer_to_vfbs(framebuffer);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000390 struct drm_clip_rect norect;
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200391 int ret, inc = 1;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000392
Sinclair Yehc8261a92015-06-26 01:23:42 -0700393 /* Legacy Display Unit does not support 3D */
394 if (dev_priv->active_display_unit == vmw_du_legacy)
Jakob Bornecrantz01e81412011-10-04 20:13:24 +0200395 return -EINVAL;
396
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200397 drm_modeset_lock_all(dev_priv->dev);
398
Thomas Hellstrom294adf72014-02-27 12:34:51 +0100399 ret = ttm_read_lock(&dev_priv->reservation_sem, true);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200400 if (unlikely(ret != 0)) {
401 drm_modeset_unlock_all(dev_priv->dev);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200402 return ret;
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200403 }
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200404
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000405 if (!num_clips) {
406 num_clips = 1;
407 clips = &norect;
408 norect.x1 = norect.y1 = 0;
409 norect.x2 = framebuffer->width;
410 norect.y2 = framebuffer->height;
411 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
412 num_clips /= 2;
413 inc = 2; /* skip source rects */
414 }
415
Sinclair Yehc8261a92015-06-26 01:23:42 -0700416 if (dev_priv->active_display_unit == vmw_du_screen_object)
Thomas Hellstrom10b1e0c2015-06-26 02:14:27 -0700417 ret = vmw_kms_sou_do_surface_dirty(dev_priv, &vfbs->base,
418 clips, NULL, NULL, 0, 0,
419 num_clips, inc, NULL);
Sinclair Yeh35c05122015-06-26 01:42:06 -0700420 else
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700421 ret = vmw_kms_stdu_surface_dirty(dev_priv, &vfbs->base,
422 clips, NULL, NULL, 0, 0,
423 num_clips, inc, NULL);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000424
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700425 vmw_fifo_flush(dev_priv, false);
Thomas Hellstrom294adf72014-02-27 12:34:51 +0100426 ttm_read_unlock(&dev_priv->reservation_sem);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200427
428 drm_modeset_unlock_all(dev_priv->dev);
429
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000430 return 0;
431}
432
Thomas Hellstrom10b1e0c2015-06-26 02:14:27 -0700433/**
434 * vmw_kms_readback - Perform a readback from the screen system to
435 * a dma-buffer backed framebuffer.
436 *
437 * @dev_priv: Pointer to the device private structure.
438 * @file_priv: Pointer to a struct drm_file identifying the caller.
439 * Must be set to NULL if @user_fence_rep is NULL.
440 * @vfb: Pointer to the dma-buffer backed framebuffer.
441 * @user_fence_rep: User-space provided structure for fence information.
442 * Must be set to non-NULL if @file_priv is non-NULL.
443 * @vclips: Array of clip rects.
444 * @num_clips: Number of clip rects in @vclips.
445 *
446 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
447 * interrupted.
448 */
449int vmw_kms_readback(struct vmw_private *dev_priv,
450 struct drm_file *file_priv,
451 struct vmw_framebuffer *vfb,
452 struct drm_vmw_fence_rep __user *user_fence_rep,
453 struct drm_vmw_rect *vclips,
454 uint32_t num_clips)
455{
456 switch (dev_priv->active_display_unit) {
457 case vmw_du_screen_object:
458 return vmw_kms_sou_readback(dev_priv, file_priv, vfb,
459 user_fence_rep, vclips, num_clips);
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700460 case vmw_du_screen_target:
461 return vmw_kms_stdu_dma(dev_priv, file_priv, vfb,
462 user_fence_rep, NULL, vclips, num_clips,
463 1, false, true);
Thomas Hellstrom10b1e0c2015-06-26 02:14:27 -0700464 default:
465 WARN_ONCE(true,
466 "Readback called with invalid display system.\n");
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700467}
Thomas Hellstrom10b1e0c2015-06-26 02:14:27 -0700468
469 return -ENOSYS;
470}
471
472
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000473static struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs = {
474 .destroy = vmw_framebuffer_surface_destroy,
475 .dirty = vmw_framebuffer_surface_dirty,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000476};
477
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200478static int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv,
479 struct vmw_surface *surface,
480 struct vmw_framebuffer **out,
481 const struct drm_mode_fb_cmd
Sinclair Yehf89c6c32015-06-26 01:54:28 -0700482 *mode_cmd,
483 bool is_dmabuf_proxy)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000484
485{
486 struct drm_device *dev = dev_priv->dev;
487 struct vmw_framebuffer_surface *vfbs;
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200488 enum SVGA3dSurfaceFormat format;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000489 int ret;
490
Sinclair Yehc8261a92015-06-26 01:23:42 -0700491 /* 3D is only supported on HWv8 and newer hosts */
492 if (dev_priv->active_display_unit == vmw_du_legacy)
Jakob Bornecrantz01e81412011-10-04 20:13:24 +0200493 return -ENOSYS;
494
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200495 /*
496 * Sanity checks.
497 */
498
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100499 /* Surface must be marked as a scanout. */
500 if (unlikely(!surface->scanout))
501 return -EINVAL;
502
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200503 if (unlikely(surface->mip_levels[0] != 1 ||
504 surface->num_sizes != 1 ||
Thomas Hellstromb360a3c2014-01-15 08:51:36 +0100505 surface->base_size.width < mode_cmd->width ||
506 surface->base_size.height < mode_cmd->height ||
507 surface->base_size.depth != 1)) {
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200508 DRM_ERROR("Incompatible surface dimensions "
509 "for requested mode.\n");
510 return -EINVAL;
511 }
512
513 switch (mode_cmd->depth) {
514 case 32:
515 format = SVGA3D_A8R8G8B8;
516 break;
517 case 24:
518 format = SVGA3D_X8R8G8B8;
519 break;
520 case 16:
521 format = SVGA3D_R5G6B5;
522 break;
523 case 15:
524 format = SVGA3D_A1R5G5B5;
525 break;
526 default:
527 DRM_ERROR("Invalid color depth: %d\n", mode_cmd->depth);
528 return -EINVAL;
529 }
530
531 if (unlikely(format != surface->format)) {
532 DRM_ERROR("Invalid surface format for requested mode.\n");
533 return -EINVAL;
534 }
535
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000536 vfbs = kzalloc(sizeof(*vfbs), GFP_KERNEL);
537 if (!vfbs) {
538 ret = -ENOMEM;
539 goto out_err1;
540 }
541
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000542 if (!vmw_surface_reference(surface)) {
543 DRM_ERROR("failed to reference surface %p\n", surface);
Daniel Vetter80f0b5a2012-12-13 23:39:01 +0100544 ret = -EINVAL;
545 goto out_err2;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000546 }
547
548 /* XXX get the first 3 from the surface info */
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200549 vfbs->base.base.bits_per_pixel = mode_cmd->bpp;
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200550 vfbs->base.base.pitches[0] = mode_cmd->pitch;
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200551 vfbs->base.base.depth = mode_cmd->depth;
552 vfbs->base.base.width = mode_cmd->width;
553 vfbs->base.base.height = mode_cmd->height;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000554 vfbs->surface = surface;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200555 vfbs->base.user_handle = mode_cmd->handle;
Sinclair Yehf89c6c32015-06-26 01:54:28 -0700556 vfbs->is_dmabuf_proxy = is_dmabuf_proxy;
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200557
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000558 *out = &vfbs->base;
559
Daniel Vetter80f0b5a2012-12-13 23:39:01 +0100560 ret = drm_framebuffer_init(dev, &vfbs->base.base,
561 &vmw_framebuffer_surface_funcs);
562 if (ret)
563 goto out_err3;
564
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000565 return 0;
566
567out_err3:
Daniel Vetter80f0b5a2012-12-13 23:39:01 +0100568 vmw_surface_unreference(&surface);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000569out_err2:
570 kfree(vfbs);
571out_err1:
572 return ret;
573}
574
575/*
576 * Dmabuf framebuffer code
577 */
578
Rashika Kheria847c5962014-01-06 22:18:10 +0530579static void vmw_framebuffer_dmabuf_destroy(struct drm_framebuffer *framebuffer)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000580{
581 struct vmw_framebuffer_dmabuf *vfbd =
582 vmw_framebuffer_to_vfbd(framebuffer);
583
584 drm_framebuffer_cleanup(framebuffer);
585 vmw_dmabuf_unreference(&vfbd->buffer);
Thomas Hellstroma2787242015-06-29 12:55:07 -0700586 if (vfbd->base.user_obj)
587 ttm_base_object_unref(&vfbd->base.user_obj);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000588
589 kfree(vfbd);
590}
591
Rashika Kheria847c5962014-01-06 22:18:10 +0530592static int vmw_framebuffer_dmabuf_dirty(struct drm_framebuffer *framebuffer,
Thomas Hellstrom02b00162010-10-05 12:43:02 +0200593 struct drm_file *file_priv,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000594 unsigned flags, unsigned color,
595 struct drm_clip_rect *clips,
596 unsigned num_clips)
597{
598 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200599 struct vmw_framebuffer_dmabuf *vfbd =
600 vmw_framebuffer_to_vfbd(framebuffer);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000601 struct drm_clip_rect norect;
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200602 int ret, increment = 1;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000603
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200604 drm_modeset_lock_all(dev_priv->dev);
605
Thomas Hellstrom294adf72014-02-27 12:34:51 +0100606 ret = ttm_read_lock(&dev_priv->reservation_sem, true);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200607 if (unlikely(ret != 0)) {
608 drm_modeset_unlock_all(dev_priv->dev);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200609 return ret;
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200610 }
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200611
Thomas Hellstromdf1c93b2010-01-13 22:28:36 +0100612 if (!num_clips) {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000613 num_clips = 1;
614 clips = &norect;
615 norect.x1 = norect.y1 = 0;
616 norect.x2 = framebuffer->width;
617 norect.y2 = framebuffer->height;
618 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
619 num_clips /= 2;
620 increment = 2;
621 }
622
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700623 switch (dev_priv->active_display_unit) {
624 case vmw_du_screen_target:
625 ret = vmw_kms_stdu_dma(dev_priv, NULL, &vfbd->base, NULL,
626 clips, NULL, num_clips, increment,
627 true, true);
628 break;
629 case vmw_du_screen_object:
Thomas Hellstrom10b1e0c2015-06-26 02:14:27 -0700630 ret = vmw_kms_sou_do_dmabuf_dirty(dev_priv, &vfbd->base,
Sinclair Yehc8261a92015-06-26 01:23:42 -0700631 clips, num_clips, increment,
Thomas Hellstrom10b1e0c2015-06-26 02:14:27 -0700632 true,
Sinclair Yehc8261a92015-06-26 01:23:42 -0700633 NULL);
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700634 break;
Thomas Hellstrom352b20d2015-06-29 12:57:37 -0700635 case vmw_du_legacy:
636 ret = vmw_kms_ldu_do_dmabuf_dirty(dev_priv, &vfbd->base, 0, 0,
637 clips, num_clips, increment);
638 break;
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700639 default:
Thomas Hellstrom352b20d2015-06-29 12:57:37 -0700640 ret = -EINVAL;
641 WARN_ONCE(true, "Dirty called with invalid display system.\n");
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700642 break;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200643 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000644
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700645 vmw_fifo_flush(dev_priv, false);
Thomas Hellstrom294adf72014-02-27 12:34:51 +0100646 ttm_read_unlock(&dev_priv->reservation_sem);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200647
648 drm_modeset_unlock_all(dev_priv->dev);
649
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200650 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000651}
652
653static struct drm_framebuffer_funcs vmw_framebuffer_dmabuf_funcs = {
654 .destroy = vmw_framebuffer_dmabuf_destroy,
655 .dirty = vmw_framebuffer_dmabuf_dirty,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000656};
657
Jakob Bornecrantz497a3ff2011-10-04 20:13:14 +0200658/**
Jakob Bornecrantz497a3ff2011-10-04 20:13:14 +0200659 * Pin the dmabuffer to the start of vram.
660 */
Thomas Hellstromfd006a42015-06-28 02:50:56 -0700661static int vmw_framebuffer_pin(struct vmw_framebuffer *vfb)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000662{
663 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
Thomas Hellstromfd006a42015-06-28 02:50:56 -0700664 struct vmw_dma_buffer *buf;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000665 int ret;
666
Thomas Hellstromfd006a42015-06-28 02:50:56 -0700667 buf = vfb->dmabuf ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
668 vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +0200669
Thomas Hellstromfd006a42015-06-28 02:50:56 -0700670 if (!buf)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000671 return 0;
Thomas Hellstromfd006a42015-06-28 02:50:56 -0700672
673 switch (dev_priv->active_display_unit) {
674 case vmw_du_legacy:
675 vmw_overlay_pause_all(dev_priv);
676 ret = vmw_dmabuf_pin_in_start_of_vram(dev_priv, buf, false);
677 vmw_overlay_resume_all(dev_priv);
678 break;
679 case vmw_du_screen_object:
680 case vmw_du_screen_target:
681 if (vfb->dmabuf)
682 return vmw_dmabuf_pin_in_vram_or_gmr(dev_priv, buf,
683 false);
684
685 return vmw_dmabuf_pin_in_placement(dev_priv, buf,
686 &vmw_mob_placement, false);
687 default:
688 return -EINVAL;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000689 }
690
Thomas Hellstromfd006a42015-06-28 02:50:56 -0700691 return ret;
692}
693
694static int vmw_framebuffer_unpin(struct vmw_framebuffer *vfb)
695{
696 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
697 struct vmw_dma_buffer *buf;
698
699 buf = vfb->dmabuf ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
700 vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
701
702 if (WARN_ON(!buf))
703 return 0;
704
705 return vmw_dmabuf_unpin(dev_priv, buf, false);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000706}
707
Sinclair Yehf89c6c32015-06-26 01:54:28 -0700708/**
709 * vmw_create_dmabuf_proxy - create a proxy surface for the DMA buf
710 *
711 * @dev: DRM device
712 * @mode_cmd: parameters for the new surface
713 * @dmabuf_mob: MOB backing the DMA buf
714 * @srf_out: newly created surface
715 *
716 * When the content FB is a DMA buf, we create a surface as a proxy to the
717 * same buffer. This way we can do a surface copy rather than a surface DMA.
718 * This is a more efficient approach
719 *
720 * RETURNS:
721 * 0 on success, error code otherwise
722 */
723static int vmw_create_dmabuf_proxy(struct drm_device *dev,
Thomas Hellstromfd006a42015-06-28 02:50:56 -0700724 const struct drm_mode_fb_cmd *mode_cmd,
Sinclair Yehf89c6c32015-06-26 01:54:28 -0700725 struct vmw_dma_buffer *dmabuf_mob,
726 struct vmw_surface **srf_out)
727{
728 uint32_t format;
729 struct drm_vmw_size content_base_size;
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700730 struct vmw_resource *res;
Sinclair Yehf89c6c32015-06-26 01:54:28 -0700731 int ret;
732
Sinclair Yehf89c6c32015-06-26 01:54:28 -0700733 switch (mode_cmd->depth) {
734 case 32:
735 case 24:
736 format = SVGA3D_X8R8G8B8;
737 break;
738
739 case 16:
740 case 15:
741 format = SVGA3D_R5G6B5;
742 break;
743
744 case 8:
745 format = SVGA3D_P8;
746 break;
747
748 default:
749 DRM_ERROR("Invalid framebuffer format %d\n", mode_cmd->depth);
750 return -EINVAL;
751 }
752
753 content_base_size.width = mode_cmd->width;
754 content_base_size.height = mode_cmd->height;
755 content_base_size.depth = 1;
756
757 ret = vmw_surface_gb_priv_define(dev,
758 0, /* kernel visible only */
759 0, /* flags */
760 format,
761 true, /* can be a scanout buffer */
762 1, /* num of mip levels */
763 0,
764 content_base_size,
765 srf_out);
766 if (ret) {
767 DRM_ERROR("Failed to allocate proxy content buffer\n");
768 return ret;
769 }
770
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700771 res = &(*srf_out)->res;
Sinclair Yehf89c6c32015-06-26 01:54:28 -0700772
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700773 /* Reserve and switch the backing mob. */
774 mutex_lock(&res->dev_priv->cmdbuf_mutex);
775 (void) vmw_resource_reserve(res, false, true);
776 vmw_dmabuf_unreference(&res->backup);
777 res->backup = vmw_dmabuf_reference(dmabuf_mob);
778 res->backup_offset = 0;
779 vmw_resource_unreserve(res, NULL, 0);
780 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
Sinclair Yehf89c6c32015-06-26 01:54:28 -0700781
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700782 return 0;
Sinclair Yehf89c6c32015-06-26 01:54:28 -0700783}
784
785
786
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200787static int vmw_kms_new_framebuffer_dmabuf(struct vmw_private *dev_priv,
788 struct vmw_dma_buffer *dmabuf,
789 struct vmw_framebuffer **out,
790 const struct drm_mode_fb_cmd
791 *mode_cmd)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000792
793{
794 struct drm_device *dev = dev_priv->dev;
795 struct vmw_framebuffer_dmabuf *vfbd;
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200796 unsigned int requested_size;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000797 int ret;
798
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200799 requested_size = mode_cmd->height * mode_cmd->pitch;
800 if (unlikely(requested_size > dmabuf->base.num_pages * PAGE_SIZE)) {
801 DRM_ERROR("Screen buffer object size is too small "
802 "for requested mode.\n");
803 return -EINVAL;
804 }
805
Jakob Bornecrantzc337ada2011-10-04 20:13:34 +0200806 /* Limited framebuffer color depth support for screen objects */
Sinclair Yehc8261a92015-06-26 01:23:42 -0700807 if (dev_priv->active_display_unit == vmw_du_screen_object) {
Jakob Bornecrantzc337ada2011-10-04 20:13:34 +0200808 switch (mode_cmd->depth) {
809 case 32:
810 case 24:
811 /* Only support 32 bpp for 32 and 24 depth fbs */
812 if (mode_cmd->bpp == 32)
813 break;
814
815 DRM_ERROR("Invalid color depth/bbp: %d %d\n",
816 mode_cmd->depth, mode_cmd->bpp);
817 return -EINVAL;
818 case 16:
819 case 15:
820 /* Only support 16 bpp for 16 and 15 depth fbs */
821 if (mode_cmd->bpp == 16)
822 break;
823
824 DRM_ERROR("Invalid color depth/bbp: %d %d\n",
825 mode_cmd->depth, mode_cmd->bpp);
826 return -EINVAL;
827 default:
828 DRM_ERROR("Invalid color depth: %d\n", mode_cmd->depth);
829 return -EINVAL;
830 }
831 }
832
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000833 vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL);
834 if (!vfbd) {
835 ret = -ENOMEM;
836 goto out_err1;
837 }
838
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000839 if (!vmw_dmabuf_reference(dmabuf)) {
840 DRM_ERROR("failed to reference dmabuf %p\n", dmabuf);
Daniel Vetter80f0b5a2012-12-13 23:39:01 +0100841 ret = -EINVAL;
842 goto out_err2;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000843 }
844
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200845 vfbd->base.base.bits_per_pixel = mode_cmd->bpp;
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200846 vfbd->base.base.pitches[0] = mode_cmd->pitch;
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200847 vfbd->base.base.depth = mode_cmd->depth;
848 vfbd->base.base.width = mode_cmd->width;
849 vfbd->base.base.height = mode_cmd->height;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200850 vfbd->base.dmabuf = true;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000851 vfbd->buffer = dmabuf;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200852 vfbd->base.user_handle = mode_cmd->handle;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000853 *out = &vfbd->base;
854
Daniel Vetter80f0b5a2012-12-13 23:39:01 +0100855 ret = drm_framebuffer_init(dev, &vfbd->base.base,
856 &vmw_framebuffer_dmabuf_funcs);
857 if (ret)
858 goto out_err3;
859
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000860 return 0;
861
862out_err3:
Daniel Vetter80f0b5a2012-12-13 23:39:01 +0100863 vmw_dmabuf_unreference(&dmabuf);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000864out_err2:
865 kfree(vfbd);
866out_err1:
867 return ret;
868}
869
Thomas Hellstromfd006a42015-06-28 02:50:56 -0700870/**
871 * vmw_kms_new_framebuffer - Create a new framebuffer.
872 *
873 * @dev_priv: Pointer to device private struct.
874 * @dmabuf: Pointer to dma buffer to wrap the kms framebuffer around.
875 * Either @dmabuf or @surface must be NULL.
876 * @surface: Pointer to a surface to wrap the kms framebuffer around.
877 * Either @dmabuf or @surface must be NULL.
878 * @only_2d: No presents will occur to this dma buffer based framebuffer. This
879 * Helps the code to do some important optimizations.
880 * @mode_cmd: Frame-buffer metadata.
881 */
882struct vmw_framebuffer *
883vmw_kms_new_framebuffer(struct vmw_private *dev_priv,
884 struct vmw_dma_buffer *dmabuf,
885 struct vmw_surface *surface,
886 bool only_2d,
887 const struct drm_mode_fb_cmd *mode_cmd)
888{
889 struct vmw_framebuffer *vfb;
890 bool is_dmabuf_proxy = false;
891 int ret;
892
893 /*
894 * We cannot use the SurfaceDMA command in an non-accelerated VM,
895 * therefore, wrap the DMA buf in a surface so we can use the
896 * SurfaceCopy command.
897 */
898 if (dmabuf && only_2d &&
899 dev_priv->active_display_unit == vmw_du_screen_target) {
900 ret = vmw_create_dmabuf_proxy(dev_priv->dev, mode_cmd,
901 dmabuf, &surface);
902 if (ret)
903 return ERR_PTR(ret);
904
905 is_dmabuf_proxy = true;
906 }
907
908 /* Create the new framebuffer depending one what we have */
909 if (surface)
910 ret = vmw_kms_new_framebuffer_surface(dev_priv, surface, &vfb,
911 mode_cmd,
912 is_dmabuf_proxy);
913 else if (dmabuf)
914 ret = vmw_kms_new_framebuffer_dmabuf(dev_priv, dmabuf, &vfb,
915 mode_cmd);
916 else
917 BUG();
918
919 if (ret)
920 return ERR_PTR(ret);
921
922 vfb->pin = vmw_framebuffer_pin;
923 vfb->unpin = vmw_framebuffer_unpin;
924
925 return vfb;
926}
927
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000928/*
929 * Generic Kernel modesetting functions
930 */
931
932static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev,
933 struct drm_file *file_priv,
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800934 struct drm_mode_fb_cmd2 *mode_cmd2)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000935{
936 struct vmw_private *dev_priv = vmw_priv(dev);
937 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
938 struct vmw_framebuffer *vfb = NULL;
939 struct vmw_surface *surface = NULL;
940 struct vmw_dma_buffer *bo = NULL;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200941 struct ttm_base_object *user_obj;
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800942 struct drm_mode_fb_cmd mode_cmd;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000943 int ret;
944
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800945 mode_cmd.width = mode_cmd2->width;
946 mode_cmd.height = mode_cmd2->height;
947 mode_cmd.pitch = mode_cmd2->pitches[0];
948 mode_cmd.handle = mode_cmd2->handles[0];
Dave Airlie248dbc22011-11-29 20:02:54 +0000949 drm_fb_get_bpp_depth(mode_cmd2->pixel_format, &mode_cmd.depth,
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800950 &mode_cmd.bpp);
951
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200952 /**
953 * This code should be conditioned on Screen Objects not being used.
954 * If screen objects are used, we can allocate a GMR to hold the
955 * requested framebuffer.
956 */
957
Xi Wang8a783892011-12-21 05:18:33 -0500958 if (!vmw_kms_validate_mode_vram(dev_priv,
Linus Torvalds1a464cb2012-01-10 11:04:36 -0800959 mode_cmd.pitch,
960 mode_cmd.height)) {
Sinclair Yehc8261a92015-06-26 01:23:42 -0700961 DRM_ERROR("Requested mode exceed bounding box limit.\n");
Jakob Bornecrantzd9826402011-11-03 21:03:04 +0100962 return ERR_PTR(-ENOMEM);
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200963 }
964
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200965 /*
966 * Take a reference on the user object of the resource
967 * backing the kms fb. This ensures that user-space handle
968 * lookups on that resource will always work as long as
969 * it's registered with a kms framebuffer. This is important,
970 * since vmw_execbuf_process identifies resources in the
971 * command stream using user-space handles.
972 */
973
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800974 user_obj = ttm_base_object_lookup(tfile, mode_cmd.handle);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200975 if (unlikely(user_obj == NULL)) {
976 DRM_ERROR("Could not locate requested kms frame buffer.\n");
977 return ERR_PTR(-ENOENT);
978 }
979
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200980 /**
981 * End conditioned code.
982 */
983
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100984 /* returns either a dmabuf or surface */
985 ret = vmw_user_lookup_handle(dev_priv, tfile,
Dave Airlie4cf73122011-12-21 09:50:56 +0000986 mode_cmd.handle,
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100987 &surface, &bo);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000988 if (ret)
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100989 goto err_out;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000990
Thomas Hellstromfd006a42015-06-28 02:50:56 -0700991 vfb = vmw_kms_new_framebuffer(dev_priv, bo, surface,
992 !(dev_priv->capabilities & SVGA_CAP_3D),
993 &mode_cmd);
994 if (IS_ERR(vfb)) {
995 ret = PTR_ERR(vfb);
996 goto err_out;
997 }
Jakob Bornecrantz5ffdb652010-01-30 03:38:08 +0000998
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100999err_out:
1000 /* vmw_user_lookup_handle takes one ref so does new_fb */
1001 if (bo)
1002 vmw_dmabuf_unreference(&bo);
1003 if (surface)
1004 vmw_surface_unreference(&surface);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001005
1006 if (ret) {
1007 DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001008 ttm_base_object_unref(&user_obj);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01001009 return ERR_PTR(ret);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001010 } else
1011 vfb->user_obj = user_obj;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001012
1013 return &vfb->base;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001014}
1015
Laurent Pincharte6ecefa2012-05-17 13:27:23 +02001016static const struct drm_mode_config_funcs vmw_kms_funcs = {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001017 .fb_create = vmw_kms_fb_create,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001018};
1019
Sinclair Yehc8261a92015-06-26 01:23:42 -07001020int vmw_kms_generic_present(struct vmw_private *dev_priv,
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001021 struct drm_file *file_priv,
1022 struct vmw_framebuffer *vfb,
1023 struct vmw_surface *surface,
1024 uint32_t sid,
1025 int32_t destX, int32_t destY,
1026 struct drm_vmw_rect *clips,
1027 uint32_t num_clips)
1028{
Thomas Hellstrom10b1e0c2015-06-26 02:14:27 -07001029 return vmw_kms_sou_do_surface_dirty(dev_priv, vfb, NULL, clips,
1030 &surface->res, destX, destY,
1031 num_clips, 1, NULL);
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001032}
1033
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001034
Sinclair Yehc8261a92015-06-26 01:23:42 -07001035int vmw_kms_present(struct vmw_private *dev_priv,
1036 struct drm_file *file_priv,
1037 struct vmw_framebuffer *vfb,
1038 struct vmw_surface *surface,
1039 uint32_t sid,
1040 int32_t destX, int32_t destY,
1041 struct drm_vmw_rect *clips,
1042 uint32_t num_clips)
1043{
Sinclair Yeh35c05122015-06-26 01:42:06 -07001044 int ret;
1045
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001046 switch (dev_priv->active_display_unit) {
1047 case vmw_du_screen_target:
1048 ret = vmw_kms_stdu_surface_dirty(dev_priv, vfb, NULL, clips,
1049 &surface->res, destX, destY,
1050 num_clips, 1, NULL);
1051 break;
1052 case vmw_du_screen_object:
1053 ret = vmw_kms_generic_present(dev_priv, file_priv, vfb, surface,
1054 sid, destX, destY, clips,
1055 num_clips);
1056 break;
1057 default:
1058 WARN_ONCE(true,
1059 "Present called with invalid display system.\n");
1060 ret = -ENOSYS;
1061 break;
1062 }
Sinclair Yeh35c05122015-06-26 01:42:06 -07001063 if (ret)
1064 return ret;
1065
1066 vmw_fifo_flush(dev_priv, false);
1067
1068 return 0;
Sinclair Yehc8261a92015-06-26 01:23:42 -07001069}
1070
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001071int vmw_kms_init(struct vmw_private *dev_priv)
1072{
1073 struct drm_device *dev = dev_priv->dev;
1074 int ret;
1075
1076 drm_mode_config_init(dev);
1077 dev->mode_config.funcs = &vmw_kms_funcs;
Jakob Bornecrantz3bef3572010-02-09 19:41:57 +00001078 dev->mode_config.min_width = 1;
1079 dev->mode_config.min_height = 1;
Jakob Bornecrantz7e71f8a2010-05-28 11:21:54 +02001080 /* assumed largest fb size */
1081 dev->mode_config.max_width = 8192;
1082 dev->mode_config.max_height = 8192;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001083
Sinclair Yeh35c05122015-06-26 01:42:06 -07001084 ret = vmw_kms_stdu_init_display(dev_priv);
1085 if (ret) {
1086 ret = vmw_kms_sou_init_display(dev_priv);
1087 if (ret) /* Fallback */
1088 ret = vmw_kms_ldu_init_display(dev_priv);
1089 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001090
Sinclair Yehc8261a92015-06-26 01:23:42 -07001091 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001092}
1093
1094int vmw_kms_close(struct vmw_private *dev_priv)
1095{
Sinclair Yehc8261a92015-06-26 01:23:42 -07001096 int ret;
1097
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001098 /*
1099 * Docs says we should take the lock before calling this function
1100 * but since it destroys encoders and our destructor calls
1101 * drm_encoder_cleanup which takes the lock we deadlock.
1102 */
1103 drm_mode_config_cleanup(dev_priv->dev);
Sinclair Yehc8261a92015-06-26 01:23:42 -07001104 if (dev_priv->active_display_unit == vmw_du_screen_object)
1105 ret = vmw_kms_sou_close_display(dev_priv);
Sinclair Yeh35c05122015-06-26 01:42:06 -07001106 else if (dev_priv->active_display_unit == vmw_du_screen_target)
1107 ret = vmw_kms_stdu_close_display(dev_priv);
Jakob Bornecrantzc0d18312011-11-09 10:25:26 +01001108 else
Sinclair Yehc8261a92015-06-26 01:23:42 -07001109 ret = vmw_kms_ldu_close_display(dev_priv);
1110
1111 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001112}
1113
1114int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
1115 struct drm_file *file_priv)
1116{
1117 struct drm_vmw_cursor_bypass_arg *arg = data;
1118 struct vmw_display_unit *du;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001119 struct drm_crtc *crtc;
1120 int ret = 0;
1121
1122
1123 mutex_lock(&dev->mode_config.mutex);
1124 if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
1125
1126 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1127 du = vmw_crtc_to_du(crtc);
1128 du->hotspot_x = arg->xhot;
1129 du->hotspot_y = arg->yhot;
1130 }
1131
1132 mutex_unlock(&dev->mode_config.mutex);
1133 return 0;
1134 }
1135
Rob Clarka4cd5d62014-07-17 23:30:02 -04001136 crtc = drm_crtc_find(dev, arg->crtc_id);
1137 if (!crtc) {
Ville Syrjälä4ae87ff2013-10-17 13:35:05 +03001138 ret = -ENOENT;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001139 goto out;
1140 }
1141
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001142 du = vmw_crtc_to_du(crtc);
1143
1144 du->hotspot_x = arg->xhot;
1145 du->hotspot_y = arg->yhot;
1146
1147out:
1148 mutex_unlock(&dev->mode_config.mutex);
1149
1150 return ret;
1151}
1152
Michel Dänzer0bef23f2011-08-31 07:42:50 +00001153int vmw_kms_write_svga(struct vmw_private *vmw_priv,
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001154 unsigned width, unsigned height, unsigned pitch,
Michel Dänzer6558429b2011-08-31 07:42:49 +00001155 unsigned bpp, unsigned depth)
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001156{
1157 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1158 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch);
1159 else if (vmw_fifo_have_pitchlock(vmw_priv))
1160 iowrite32(pitch, vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
1161 vmw_write(vmw_priv, SVGA_REG_WIDTH, width);
1162 vmw_write(vmw_priv, SVGA_REG_HEIGHT, height);
Michel Dänzer6558429b2011-08-31 07:42:49 +00001163 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp);
Michel Dänzer0bef23f2011-08-31 07:42:50 +00001164
1165 if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) {
1166 DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n",
1167 depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH));
1168 return -EINVAL;
1169 }
1170
1171 return 0;
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001172}
1173
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001174int vmw_kms_save_vga(struct vmw_private *vmw_priv)
1175{
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001176 struct vmw_vga_topology_state *save;
1177 uint32_t i;
1178
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001179 vmw_priv->vga_width = vmw_read(vmw_priv, SVGA_REG_WIDTH);
1180 vmw_priv->vga_height = vmw_read(vmw_priv, SVGA_REG_HEIGHT);
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001181 vmw_priv->vga_bpp = vmw_read(vmw_priv, SVGA_REG_BITS_PER_PIXEL);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001182 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1183 vmw_priv->vga_pitchlock =
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001184 vmw_read(vmw_priv, SVGA_REG_PITCHLOCK);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001185 else if (vmw_fifo_have_pitchlock(vmw_priv))
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001186 vmw_priv->vga_pitchlock = ioread32(vmw_priv->mmio_virt +
Sinclair Yehc8261a92015-06-26 01:23:42 -07001187 SVGA_FIFO_PITCHLOCK);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001188
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001189 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1190 return 0;
1191
1192 vmw_priv->num_displays = vmw_read(vmw_priv,
1193 SVGA_REG_NUM_GUEST_DISPLAYS);
1194
Thomas Hellstrom029e50b2010-10-05 12:43:08 +02001195 if (vmw_priv->num_displays == 0)
1196 vmw_priv->num_displays = 1;
1197
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001198 for (i = 0; i < vmw_priv->num_displays; ++i) {
1199 save = &vmw_priv->vga_save[i];
1200 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1201 save->primary = vmw_read(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY);
1202 save->pos_x = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_X);
1203 save->pos_y = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y);
1204 save->width = vmw_read(vmw_priv, SVGA_REG_DISPLAY_WIDTH);
1205 save->height = vmw_read(vmw_priv, SVGA_REG_DISPLAY_HEIGHT);
1206 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
Thomas Hellstrom30c78bb2010-10-01 10:21:48 +02001207 if (i == 0 && vmw_priv->num_displays == 1 &&
1208 save->width == 0 && save->height == 0) {
1209
1210 /*
1211 * It should be fairly safe to assume that these
1212 * values are uninitialized.
1213 */
1214
1215 save->width = vmw_priv->vga_width - save->pos_x;
1216 save->height = vmw_priv->vga_height - save->pos_y;
1217 }
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001218 }
Thomas Hellstrom30c78bb2010-10-01 10:21:48 +02001219
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001220 return 0;
1221}
1222
1223int vmw_kms_restore_vga(struct vmw_private *vmw_priv)
1224{
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001225 struct vmw_vga_topology_state *save;
1226 uint32_t i;
1227
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001228 vmw_write(vmw_priv, SVGA_REG_WIDTH, vmw_priv->vga_width);
1229 vmw_write(vmw_priv, SVGA_REG_HEIGHT, vmw_priv->vga_height);
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001230 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, vmw_priv->vga_bpp);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001231 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1232 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK,
1233 vmw_priv->vga_pitchlock);
1234 else if (vmw_fifo_have_pitchlock(vmw_priv))
1235 iowrite32(vmw_priv->vga_pitchlock,
1236 vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001237
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001238 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1239 return 0;
1240
1241 for (i = 0; i < vmw_priv->num_displays; ++i) {
1242 save = &vmw_priv->vga_save[i];
1243 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1244 vmw_write(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY, save->primary);
1245 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_X, save->pos_x);
1246 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y, save->pos_y);
1247 vmw_write(vmw_priv, SVGA_REG_DISPLAY_WIDTH, save->width);
1248 vmw_write(vmw_priv, SVGA_REG_DISPLAY_HEIGHT, save->height);
1249 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
1250 }
1251
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001252 return 0;
1253}
Jakob Bornecrantzd8bd19d2010-06-01 11:54:20 +02001254
Thomas Hellstrome133e732010-10-05 12:43:04 +02001255bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
1256 uint32_t pitch,
1257 uint32_t height)
1258{
Sinclair Yeh35c05122015-06-26 01:42:06 -07001259 return ((u64) pitch * (u64) height) < (u64)
1260 ((dev_priv->active_display_unit == vmw_du_screen_target) ?
1261 dev_priv->prim_bb_mem : dev_priv->vram_size);
Thomas Hellstrome133e732010-10-05 12:43:04 +02001262}
1263
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001264
1265/**
1266 * Function called by DRM code called with vbl_lock held.
1267 */
Thomas Hellstrom7a1c2f62010-10-01 10:21:49 +02001268u32 vmw_get_vblank_counter(struct drm_device *dev, int crtc)
1269{
1270 return 0;
1271}
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001272
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001273/**
1274 * Function called by DRM code called with vbl_lock held.
1275 */
1276int vmw_enable_vblank(struct drm_device *dev, int crtc)
1277{
1278 return -ENOSYS;
1279}
1280
1281/**
1282 * Function called by DRM code called with vbl_lock held.
1283 */
1284void vmw_disable_vblank(struct drm_device *dev, int crtc)
1285{
1286}
1287
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001288
1289/*
1290 * Small shared kms functions.
1291 */
1292
Rashika Kheria847c5962014-01-06 22:18:10 +05301293static int vmw_du_update_layout(struct vmw_private *dev_priv, unsigned num,
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001294 struct drm_vmw_rect *rects)
1295{
1296 struct drm_device *dev = dev_priv->dev;
1297 struct vmw_display_unit *du;
1298 struct drm_connector *con;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001299
1300 mutex_lock(&dev->mode_config.mutex);
1301
1302#if 0
Thomas Hellstrom6ea77d12011-10-04 20:13:36 +02001303 {
1304 unsigned int i;
1305
1306 DRM_INFO("%s: new layout ", __func__);
1307 for (i = 0; i < num; i++)
1308 DRM_INFO("(%i, %i %ux%u) ", rects[i].x, rects[i].y,
1309 rects[i].w, rects[i].h);
1310 DRM_INFO("\n");
1311 }
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001312#endif
1313
1314 list_for_each_entry(con, &dev->mode_config.connector_list, head) {
1315 du = vmw_connector_to_du(con);
1316 if (num > du->unit) {
1317 du->pref_width = rects[du->unit].w;
1318 du->pref_height = rects[du->unit].h;
1319 du->pref_active = true;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001320 du->gui_x = rects[du->unit].x;
1321 du->gui_y = rects[du->unit].y;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001322 } else {
1323 du->pref_width = 800;
1324 du->pref_height = 600;
1325 du->pref_active = false;
1326 }
1327 con->status = vmw_du_connector_detect(con, true);
1328 }
1329
1330 mutex_unlock(&dev->mode_config.mutex);
1331
1332 return 0;
1333}
1334
1335void vmw_du_crtc_save(struct drm_crtc *crtc)
1336{
1337}
1338
1339void vmw_du_crtc_restore(struct drm_crtc *crtc)
1340{
1341}
1342
1343void vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
1344 u16 *r, u16 *g, u16 *b,
1345 uint32_t start, uint32_t size)
1346{
1347 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
1348 int i;
1349
1350 for (i = 0; i < size; i++) {
1351 DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i,
1352 r[i], g[i], b[i]);
1353 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 0, r[i] >> 8);
1354 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 1, g[i] >> 8);
1355 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 2, b[i] >> 8);
1356 }
1357}
1358
1359void vmw_du_connector_dpms(struct drm_connector *connector, int mode)
1360{
1361}
1362
1363void vmw_du_connector_save(struct drm_connector *connector)
1364{
1365}
1366
1367void vmw_du_connector_restore(struct drm_connector *connector)
1368{
1369}
1370
1371enum drm_connector_status
1372vmw_du_connector_detect(struct drm_connector *connector, bool force)
1373{
1374 uint32_t num_displays;
1375 struct drm_device *dev = connector->dev;
1376 struct vmw_private *dev_priv = vmw_priv(dev);
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001377 struct vmw_display_unit *du = vmw_connector_to_du(connector);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001378
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001379 num_displays = vmw_read(dev_priv, SVGA_REG_NUM_DISPLAYS);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001380
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001381 return ((vmw_connector_to_du(connector)->unit < num_displays &&
1382 du->pref_active) ?
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001383 connector_status_connected : connector_status_disconnected);
1384}
1385
1386static struct drm_display_mode vmw_kms_connector_builtin[] = {
1387 /* 640x480@60Hz */
1388 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
1389 752, 800, 0, 480, 489, 492, 525, 0,
1390 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1391 /* 800x600@60Hz */
1392 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
1393 968, 1056, 0, 600, 601, 605, 628, 0,
1394 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1395 /* 1024x768@60Hz */
1396 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
1397 1184, 1344, 0, 768, 771, 777, 806, 0,
1398 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1399 /* 1152x864@75Hz */
1400 { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
1401 1344, 1600, 0, 864, 865, 868, 900, 0,
1402 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1403 /* 1280x768@60Hz */
1404 { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344,
1405 1472, 1664, 0, 768, 771, 778, 798, 0,
1406 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1407 /* 1280x800@60Hz */
1408 { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352,
1409 1480, 1680, 0, 800, 803, 809, 831, 0,
1410 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
1411 /* 1280x960@60Hz */
1412 { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376,
1413 1488, 1800, 0, 960, 961, 964, 1000, 0,
1414 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1415 /* 1280x1024@60Hz */
1416 { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328,
1417 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
1418 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1419 /* 1360x768@60Hz */
1420 { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424,
1421 1536, 1792, 0, 768, 771, 777, 795, 0,
1422 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1423 /* 1440x1050@60Hz */
1424 { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488,
1425 1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
1426 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1427 /* 1440x900@60Hz */
1428 { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520,
1429 1672, 1904, 0, 900, 903, 909, 934, 0,
1430 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1431 /* 1600x1200@60Hz */
1432 { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664,
1433 1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
1434 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1435 /* 1680x1050@60Hz */
1436 { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784,
1437 1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
1438 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1439 /* 1792x1344@60Hz */
1440 { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920,
1441 2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
1442 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1443 /* 1853x1392@60Hz */
1444 { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952,
1445 2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
1446 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1447 /* 1920x1200@60Hz */
1448 { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056,
1449 2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
1450 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1451 /* 1920x1440@60Hz */
1452 { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048,
1453 2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
1454 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1455 /* 2560x1600@60Hz */
1456 { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752,
1457 3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
1458 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1459 /* Terminate */
1460 { DRM_MODE("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) },
1461};
1462
Thomas Hellstrom1543b4d2011-11-02 09:43:10 +01001463/**
1464 * vmw_guess_mode_timing - Provide fake timings for a
1465 * 60Hz vrefresh mode.
1466 *
1467 * @mode - Pointer to a struct drm_display_mode with hdisplay and vdisplay
1468 * members filled in.
1469 */
Thomas Hellstroma2787242015-06-29 12:55:07 -07001470void vmw_guess_mode_timing(struct drm_display_mode *mode)
Thomas Hellstrom1543b4d2011-11-02 09:43:10 +01001471{
1472 mode->hsync_start = mode->hdisplay + 50;
1473 mode->hsync_end = mode->hsync_start + 50;
1474 mode->htotal = mode->hsync_end + 50;
1475
1476 mode->vsync_start = mode->vdisplay + 50;
1477 mode->vsync_end = mode->vsync_start + 50;
1478 mode->vtotal = mode->vsync_end + 50;
1479
1480 mode->clock = (u32)mode->htotal * (u32)mode->vtotal / 100 * 6;
1481 mode->vrefresh = drm_mode_vrefresh(mode);
1482}
1483
1484
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001485int vmw_du_connector_fill_modes(struct drm_connector *connector,
1486 uint32_t max_width, uint32_t max_height)
1487{
1488 struct vmw_display_unit *du = vmw_connector_to_du(connector);
1489 struct drm_device *dev = connector->dev;
1490 struct vmw_private *dev_priv = vmw_priv(dev);
1491 struct drm_display_mode *mode = NULL;
1492 struct drm_display_mode *bmode;
1493 struct drm_display_mode prefmode = { DRM_MODE("preferred",
1494 DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
1495 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1496 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
1497 };
1498 int i;
Sinclair Yeh9a723842014-10-31 09:58:06 +01001499 u32 assumed_bpp = 2;
1500
1501 /*
1502 * If using screen objects, then assume 32-bpp because that's what the
1503 * SVGA device is assuming
1504 */
Sinclair Yehc8261a92015-06-26 01:23:42 -07001505 if (dev_priv->active_display_unit == vmw_du_screen_object)
Sinclair Yeh9a723842014-10-31 09:58:06 +01001506 assumed_bpp = 4;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001507
Sinclair Yeh35c05122015-06-26 01:42:06 -07001508 if (dev_priv->active_display_unit == vmw_du_screen_target) {
1509 max_width = min(max_width, dev_priv->stdu_max_width);
1510 max_height = min(max_height, dev_priv->stdu_max_height);
1511 }
1512
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001513 /* Add preferred mode */
Sinclair Yehc8261a92015-06-26 01:23:42 -07001514 mode = drm_mode_duplicate(dev, &prefmode);
1515 if (!mode)
1516 return 0;
1517 mode->hdisplay = du->pref_width;
1518 mode->vdisplay = du->pref_height;
1519 vmw_guess_mode_timing(mode);
Jakob Bornecrantz55bde5b2011-11-03 21:03:05 +01001520
Sinclair Yehc8261a92015-06-26 01:23:42 -07001521 if (vmw_kms_validate_mode_vram(dev_priv,
1522 mode->hdisplay * assumed_bpp,
1523 mode->vdisplay)) {
1524 drm_mode_probed_add(connector, mode);
1525 } else {
1526 drm_mode_destroy(dev, mode);
1527 mode = NULL;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001528 }
1529
Sinclair Yehc8261a92015-06-26 01:23:42 -07001530 if (du->pref_mode) {
1531 list_del_init(&du->pref_mode->head);
1532 drm_mode_destroy(dev, du->pref_mode);
1533 }
1534
1535 /* mode might be null here, this is intended */
1536 du->pref_mode = mode;
1537
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001538 for (i = 0; vmw_kms_connector_builtin[i].type != 0; i++) {
1539 bmode = &vmw_kms_connector_builtin[i];
1540 if (bmode->hdisplay > max_width ||
1541 bmode->vdisplay > max_height)
1542 continue;
1543
Sinclair Yeh9a723842014-10-31 09:58:06 +01001544 if (!vmw_kms_validate_mode_vram(dev_priv,
1545 bmode->hdisplay * assumed_bpp,
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001546 bmode->vdisplay))
1547 continue;
1548
1549 mode = drm_mode_duplicate(dev, bmode);
1550 if (!mode)
1551 return 0;
1552 mode->vrefresh = drm_mode_vrefresh(mode);
1553
1554 drm_mode_probed_add(connector, mode);
1555 }
1556
Jakob Bornecrantzd41025c2011-11-03 21:03:07 +01001557 /* Move the prefered mode first, help apps pick the right mode. */
1558 if (du->pref_mode)
1559 list_move(&du->pref_mode->head, &connector->probed_modes);
1560
Dave Airlieb87577b2014-05-01 09:26:53 +10001561 drm_mode_connector_list_update(connector, true);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001562
1563 return 1;
1564}
1565
1566int vmw_du_connector_set_property(struct drm_connector *connector,
1567 struct drm_property *property,
1568 uint64_t val)
1569{
1570 return 0;
1571}
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001572
1573
1574int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data,
1575 struct drm_file *file_priv)
1576{
1577 struct vmw_private *dev_priv = vmw_priv(dev);
1578 struct drm_vmw_update_layout_arg *arg =
1579 (struct drm_vmw_update_layout_arg *)data;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001580 void __user *user_rects;
1581 struct drm_vmw_rect *rects;
1582 unsigned rects_size;
1583 int ret;
1584 int i;
1585 struct drm_mode_config *mode_config = &dev->mode_config;
Sinclair Yehc8261a92015-06-26 01:23:42 -07001586 struct drm_vmw_rect bounding_box = {0};
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001587
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001588 if (!arg->num_outputs) {
1589 struct drm_vmw_rect def_rect = {0, 0, 800, 600};
1590 vmw_du_update_layout(dev_priv, 1, &def_rect);
Thomas Hellstrom5151adb2015-03-09 01:56:21 -07001591 return 0;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001592 }
1593
1594 rects_size = arg->num_outputs * sizeof(struct drm_vmw_rect);
Xi Wangbab9efc2011-11-28 12:25:43 +01001595 rects = kcalloc(arg->num_outputs, sizeof(struct drm_vmw_rect),
1596 GFP_KERNEL);
Thomas Hellstrom5151adb2015-03-09 01:56:21 -07001597 if (unlikely(!rects))
1598 return -ENOMEM;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001599
1600 user_rects = (void __user *)(unsigned long)arg->rects;
1601 ret = copy_from_user(rects, user_rects, rects_size);
1602 if (unlikely(ret != 0)) {
1603 DRM_ERROR("Failed to get rects.\n");
1604 ret = -EFAULT;
1605 goto out_free;
1606 }
1607
1608 for (i = 0; i < arg->num_outputs; ++i) {
Xi Wangbab9efc2011-11-28 12:25:43 +01001609 if (rects[i].x < 0 ||
1610 rects[i].y < 0 ||
1611 rects[i].x + rects[i].w > mode_config->max_width ||
1612 rects[i].y + rects[i].h > mode_config->max_height) {
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001613 DRM_ERROR("Invalid GUI layout.\n");
1614 ret = -EINVAL;
1615 goto out_free;
1616 }
Sinclair Yehc8261a92015-06-26 01:23:42 -07001617
1618 /*
1619 * bounding_box.w and bunding_box.h are used as
1620 * lower-right coordinates
1621 */
1622 if (rects[i].x + rects[i].w > bounding_box.w)
1623 bounding_box.w = rects[i].x + rects[i].w;
1624
1625 if (rects[i].y + rects[i].h > bounding_box.h)
1626 bounding_box.h = rects[i].y + rects[i].h;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001627 }
1628
Sinclair Yeh35c05122015-06-26 01:42:06 -07001629 /*
1630 * For Screen Target Display Unit, all the displays must fit
1631 * inside of maximum texture size.
1632 */
1633 if (dev_priv->active_display_unit == vmw_du_screen_target)
1634 if (bounding_box.w > dev_priv->texture_max_width ||
1635 bounding_box.h > dev_priv->texture_max_height) {
1636 DRM_ERROR("Layout exceeds maximum texture size\n");
1637 ret = -EINVAL;
1638 goto out_free;
1639 }
1640
1641
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001642 vmw_du_update_layout(dev_priv, arg->num_outputs, rects);
1643
1644out_free:
1645 kfree(rects);
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001646 return ret;
1647}
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07001648
1649/**
1650 * vmw_kms_helper_dirty - Helper to build commands and perform actions based
1651 * on a set of cliprects and a set of display units.
1652 *
1653 * @dev_priv: Pointer to a device private structure.
1654 * @framebuffer: Pointer to the framebuffer on which to perform the actions.
1655 * @clips: A set of struct drm_clip_rect. Either this os @vclips must be NULL.
1656 * Cliprects are given in framebuffer coordinates.
1657 * @vclips: A set of struct drm_vmw_rect cliprects. Either this or @clips must
1658 * be NULL. Cliprects are given in source coordinates.
1659 * @dest_x: X coordinate offset for the crtc / destination clip rects.
1660 * @dest_y: Y coordinate offset for the crtc / destination clip rects.
1661 * @num_clips: Number of cliprects in the @clips or @vclips array.
1662 * @increment: Integer with which to increment the clip counter when looping.
1663 * Used to skip a predetermined number of clip rects.
1664 * @dirty: Closure structure. See the description of struct vmw_kms_dirty.
1665 */
1666int vmw_kms_helper_dirty(struct vmw_private *dev_priv,
1667 struct vmw_framebuffer *framebuffer,
1668 const struct drm_clip_rect *clips,
1669 const struct drm_vmw_rect *vclips,
1670 s32 dest_x, s32 dest_y,
1671 int num_clips,
1672 int increment,
1673 struct vmw_kms_dirty *dirty)
1674{
1675 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
1676 struct drm_crtc *crtc;
1677 u32 num_units = 0;
1678 u32 i, k;
1679 int ret;
1680
1681 dirty->dev_priv = dev_priv;
1682
1683 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
1684 if (crtc->primary->fb != &framebuffer->base)
1685 continue;
1686 units[num_units++] = vmw_crtc_to_du(crtc);
1687 }
1688
1689 for (k = 0; k < num_units; k++) {
1690 struct vmw_display_unit *unit = units[k];
1691 s32 crtc_x = unit->crtc.x;
1692 s32 crtc_y = unit->crtc.y;
1693 s32 crtc_width = unit->crtc.mode.hdisplay;
1694 s32 crtc_height = unit->crtc.mode.vdisplay;
1695 const struct drm_clip_rect *clips_ptr = clips;
1696 const struct drm_vmw_rect *vclips_ptr = vclips;
1697
1698 dirty->unit = unit;
1699 if (dirty->fifo_reserve_size > 0) {
1700 dirty->cmd = vmw_fifo_reserve(dev_priv,
1701 dirty->fifo_reserve_size);
1702 if (!dirty->cmd) {
1703 DRM_ERROR("Couldn't reserve fifo space "
1704 "for dirty blits.\n");
1705 return ret;
1706 }
1707 memset(dirty->cmd, 0, dirty->fifo_reserve_size);
1708 }
1709 dirty->num_hits = 0;
1710 for (i = 0; i < num_clips; i++, clips_ptr += increment,
1711 vclips_ptr += increment) {
1712 s32 clip_left;
1713 s32 clip_top;
1714
1715 /*
1716 * Select clip array type. Note that integer type
1717 * in @clips is unsigned short, whereas in @vclips
1718 * it's 32-bit.
1719 */
1720 if (clips) {
1721 dirty->fb_x = (s32) clips_ptr->x1;
1722 dirty->fb_y = (s32) clips_ptr->y1;
1723 dirty->unit_x2 = (s32) clips_ptr->x2 + dest_x -
1724 crtc_x;
1725 dirty->unit_y2 = (s32) clips_ptr->y2 + dest_y -
1726 crtc_y;
1727 } else {
1728 dirty->fb_x = vclips_ptr->x;
1729 dirty->fb_y = vclips_ptr->y;
1730 dirty->unit_x2 = dirty->fb_x + vclips_ptr->w +
1731 dest_x - crtc_x;
1732 dirty->unit_y2 = dirty->fb_y + vclips_ptr->h +
1733 dest_y - crtc_y;
1734 }
1735
1736 dirty->unit_x1 = dirty->fb_x + dest_x - crtc_x;
1737 dirty->unit_y1 = dirty->fb_y + dest_y - crtc_y;
1738
1739 /* Skip this clip if it's outside the crtc region */
1740 if (dirty->unit_x1 >= crtc_width ||
1741 dirty->unit_y1 >= crtc_height ||
1742 dirty->unit_x2 <= 0 || dirty->unit_y2 <= 0)
1743 continue;
1744
1745 /* Clip right and bottom to crtc limits */
1746 dirty->unit_x2 = min_t(s32, dirty->unit_x2,
1747 crtc_width);
1748 dirty->unit_y2 = min_t(s32, dirty->unit_y2,
1749 crtc_height);
1750
1751 /* Clip left and top to crtc limits */
1752 clip_left = min_t(s32, dirty->unit_x1, 0);
1753 clip_top = min_t(s32, dirty->unit_y1, 0);
1754 dirty->unit_x1 -= clip_left;
1755 dirty->unit_y1 -= clip_top;
1756 dirty->fb_x -= clip_left;
1757 dirty->fb_y -= clip_top;
1758
1759 dirty->clip(dirty);
1760 }
1761
1762 dirty->fifo_commit(dirty);
1763 }
1764
1765 return 0;
1766}
1767
1768/**
1769 * vmw_kms_helper_buffer_prepare - Reserve and validate a buffer object before
1770 * command submission.
1771 *
1772 * @dev_priv. Pointer to a device private structure.
1773 * @buf: The buffer object
1774 * @interruptible: Whether to perform waits as interruptible.
1775 * @validate_as_mob: Whether the buffer should be validated as a MOB. If false,
1776 * The buffer will be validated as a GMR. Already pinned buffers will not be
1777 * validated.
1778 *
1779 * Returns 0 on success, negative error code on failure, -ERESTARTSYS if
1780 * interrupted by a signal.
1781 */
1782int vmw_kms_helper_buffer_prepare(struct vmw_private *dev_priv,
1783 struct vmw_dma_buffer *buf,
1784 bool interruptible,
1785 bool validate_as_mob)
1786{
1787 struct ttm_buffer_object *bo = &buf->base;
1788 int ret;
1789
1790 ttm_bo_reserve(bo, false, false, interruptible, 0);
1791 ret = vmw_validate_single_buffer(dev_priv, bo, interruptible,
1792 validate_as_mob);
1793 if (ret)
1794 ttm_bo_unreserve(bo);
1795
1796 return ret;
1797}
1798
1799/**
1800 * vmw_kms_helper_buffer_revert - Undo the actions of
1801 * vmw_kms_helper_buffer_prepare.
1802 *
1803 * @res: Pointer to the buffer object.
1804 *
1805 * Helper to be used if an error forces the caller to undo the actions of
1806 * vmw_kms_helper_buffer_prepare.
1807 */
1808void vmw_kms_helper_buffer_revert(struct vmw_dma_buffer *buf)
1809{
1810 if (buf)
1811 ttm_bo_unreserve(&buf->base);
1812}
1813
1814/**
1815 * vmw_kms_helper_buffer_finish - Unreserve and fence a buffer object after
1816 * kms command submission.
1817 *
1818 * @dev_priv: Pointer to a device private structure.
1819 * @file_priv: Pointer to a struct drm_file representing the caller's
1820 * connection. Must be set to NULL if @user_fence_rep is NULL, and conversely
1821 * if non-NULL, @user_fence_rep must be non-NULL.
1822 * @buf: The buffer object.
1823 * @out_fence: Optional pointer to a fence pointer. If non-NULL, a
1824 * ref-counted fence pointer is returned here.
1825 * @user_fence_rep: Optional pointer to a user-space provided struct
1826 * drm_vmw_fence_rep. If provided, @file_priv must also be provided and the
1827 * function copies fence data to user-space in a fail-safe manner.
1828 */
1829void vmw_kms_helper_buffer_finish(struct vmw_private *dev_priv,
1830 struct drm_file *file_priv,
1831 struct vmw_dma_buffer *buf,
1832 struct vmw_fence_obj **out_fence,
1833 struct drm_vmw_fence_rep __user *
1834 user_fence_rep)
1835{
1836 struct vmw_fence_obj *fence;
1837 uint32_t handle;
1838 int ret;
1839
1840 ret = vmw_execbuf_fence_commands(file_priv, dev_priv, &fence,
1841 file_priv ? &handle : NULL);
1842 if (buf)
1843 vmw_fence_single_bo(&buf->base, fence);
1844 if (file_priv)
1845 vmw_execbuf_copy_fence_user(dev_priv, vmw_fpriv(file_priv),
1846 ret, user_fence_rep, fence,
1847 handle);
1848 if (out_fence)
1849 *out_fence = fence;
1850 else
1851 vmw_fence_obj_unreference(&fence);
1852
1853 vmw_kms_helper_buffer_revert(buf);
1854}
1855
1856
1857/**
1858 * vmw_kms_helper_resource_revert - Undo the actions of
1859 * vmw_kms_helper_resource_prepare.
1860 *
1861 * @res: Pointer to the resource. Typically a surface.
1862 *
1863 * Helper to be used if an error forces the caller to undo the actions of
1864 * vmw_kms_helper_resource_prepare.
1865 */
1866void vmw_kms_helper_resource_revert(struct vmw_resource *res)
1867{
1868 vmw_kms_helper_buffer_revert(res->backup);
1869 vmw_resource_unreserve(res, NULL, 0);
1870 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
1871}
1872
1873/**
1874 * vmw_kms_helper_resource_prepare - Reserve and validate a resource before
1875 * command submission.
1876 *
1877 * @res: Pointer to the resource. Typically a surface.
1878 * @interruptible: Whether to perform waits as interruptible.
1879 *
1880 * Reserves and validates also the backup buffer if a guest-backed resource.
1881 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
1882 * interrupted by a signal.
1883 */
1884int vmw_kms_helper_resource_prepare(struct vmw_resource *res,
1885 bool interruptible)
1886{
1887 int ret = 0;
1888
1889 if (interruptible)
1890 ret = mutex_lock_interruptible(&res->dev_priv->cmdbuf_mutex);
1891 else
1892 mutex_lock(&res->dev_priv->cmdbuf_mutex);
1893
1894 if (unlikely(ret != 0))
1895 return -ERESTARTSYS;
1896
1897 ret = vmw_resource_reserve(res, interruptible, false);
1898 if (ret)
1899 goto out_unlock;
1900
1901 if (res->backup) {
1902 ret = vmw_kms_helper_buffer_prepare(res->dev_priv, res->backup,
1903 interruptible,
1904 res->dev_priv->has_mob);
1905 if (ret)
1906 goto out_unreserve;
1907 }
1908 ret = vmw_resource_validate(res);
1909 if (ret)
1910 goto out_revert;
1911 return 0;
1912
1913out_revert:
1914 vmw_kms_helper_buffer_revert(res->backup);
1915out_unreserve:
1916 vmw_resource_unreserve(res, NULL, 0);
1917out_unlock:
1918 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
1919 return ret;
1920}
1921
1922/**
1923 * vmw_kms_helper_resource_finish - Unreserve and fence a resource after
1924 * kms command submission.
1925 *
1926 * @res: Pointer to the resource. Typically a surface.
1927 * @out_fence: Optional pointer to a fence pointer. If non-NULL, a
1928 * ref-counted fence pointer is returned here.
1929 */
1930void vmw_kms_helper_resource_finish(struct vmw_resource *res,
1931 struct vmw_fence_obj **out_fence)
1932{
1933 if (res->backup || out_fence)
1934 vmw_kms_helper_buffer_finish(res->dev_priv, NULL, res->backup,
1935 out_fence, NULL);
1936
1937 vmw_resource_unreserve(res, NULL, 0);
1938 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
1939}
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001940
1941/**
1942 * vmw_kms_update_proxy - Helper function to update a proxy surface from
1943 * its backing MOB.
1944 *
1945 * @res: Pointer to the surface resource
1946 * @clips: Clip rects in framebuffer (surface) space.
1947 * @num_clips: Number of clips in @clips.
1948 * @increment: Integer with which to increment the clip counter when looping.
1949 * Used to skip a predetermined number of clip rects.
1950 *
1951 * This function makes sure the proxy surface is updated from its backing MOB
1952 * using the region given by @clips. The surface resource @res and its backing
1953 * MOB needs to be reserved and validated on call.
1954 */
1955int vmw_kms_update_proxy(struct vmw_resource *res,
1956 const struct drm_clip_rect *clips,
1957 unsigned num_clips,
1958 int increment)
1959{
1960 struct vmw_private *dev_priv = res->dev_priv;
1961 struct drm_vmw_size *size = &vmw_res_to_srf(res)->base_size;
1962 struct {
1963 SVGA3dCmdHeader header;
1964 SVGA3dCmdUpdateGBImage body;
1965 } *cmd;
1966 SVGA3dBox *box;
1967 size_t copy_size = 0;
1968 int i;
1969
1970 if (!clips)
1971 return 0;
1972
1973 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd) * num_clips);
1974 if (!cmd) {
1975 DRM_ERROR("Couldn't reserve fifo space for proxy surface "
1976 "update.\n");
1977 return -ENOMEM;
1978 }
1979
1980 for (i = 0; i < num_clips; ++i, clips += increment, ++cmd) {
1981 box = &cmd->body.box;
1982
1983 cmd->header.id = SVGA_3D_CMD_UPDATE_GB_IMAGE;
1984 cmd->header.size = sizeof(cmd->body);
1985 cmd->body.image.sid = res->id;
1986 cmd->body.image.face = 0;
1987 cmd->body.image.mipmap = 0;
1988
1989 if (clips->x1 > size->width || clips->x2 > size->width ||
1990 clips->y1 > size->height || clips->y2 > size->height) {
1991 DRM_ERROR("Invalid clips outsize of framebuffer.\n");
1992 return -EINVAL;
1993 }
1994
1995 box->x = clips->x1;
1996 box->y = clips->y1;
1997 box->z = 0;
1998 box->w = clips->x2 - clips->x1;
1999 box->h = clips->y2 - clips->y1;
2000 box->d = 1;
2001
2002 copy_size += sizeof(*cmd);
2003 }
2004
2005 vmw_fifo_commit(dev_priv, copy_size);
2006
2007 return 0;
2008}
Thomas Hellstroma2787242015-06-29 12:55:07 -07002009
2010int vmw_kms_fbdev_init_data(struct vmw_private *dev_priv,
2011 unsigned unit,
2012 u32 max_width,
2013 u32 max_height,
2014 struct drm_connector **p_con,
2015 struct drm_crtc **p_crtc,
2016 struct drm_display_mode **p_mode)
2017{
2018 struct drm_connector *con;
2019 struct vmw_display_unit *du;
2020 struct drm_display_mode *mode;
2021 int i = 0;
2022
2023 list_for_each_entry(con, &dev_priv->dev->mode_config.connector_list,
2024 head) {
2025 if (i == unit)
2026 break;
2027
2028 ++i;
2029 }
2030
2031 if (i != unit) {
2032 DRM_ERROR("Could not find initial display unit.\n");
2033 return -EINVAL;
2034 }
2035
2036 if (list_empty(&con->modes))
2037 (void) vmw_du_connector_fill_modes(con, max_width, max_height);
2038
2039 if (list_empty(&con->modes)) {
2040 DRM_ERROR("Could not find initial display mode.\n");
2041 return -EINVAL;
2042 }
2043
2044 du = vmw_connector_to_du(con);
2045 *p_con = con;
2046 *p_crtc = &du->crtc;
2047
2048 list_for_each_entry(mode, &con->modes, head) {
2049 if (mode->type & DRM_MODE_TYPE_PREFERRED)
2050 break;
2051 }
2052
2053 if (mode->type & DRM_MODE_TYPE_PREFERRED)
2054 *p_mode = mode;
2055 else {
2056 WARN_ONCE(true, "Could not find initial preferred mode.\n");
2057 *p_mode = list_first_entry(&con->modes,
2058 struct drm_display_mode,
2059 head);
2060 }
2061
2062 return 0;
2063}