blob: c46c68846f0e9a4364b250cbabb17247100cc01e [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
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +010034
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +010035
36/**
37 * Clip @num_rects number of @rects against @clip storing the
38 * results in @out_rects and the number of passed rects in @out_num.
39 */
Sinclair Yehc8261a92015-06-26 01:23:42 -070040void vmw_clip_cliprects(struct drm_clip_rect *rects,
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +010041 int num_rects,
42 struct vmw_clip_rect clip,
43 SVGASignedRect *out_rects,
44 int *out_num)
45{
46 int i, k;
47
48 for (i = 0, k = 0; i < num_rects; i++) {
49 int x1 = max_t(int, clip.x1, rects[i].x1);
50 int y1 = max_t(int, clip.y1, rects[i].y1);
51 int x2 = min_t(int, clip.x2, rects[i].x2);
52 int y2 = min_t(int, clip.y2, rects[i].y2);
53
54 if (x1 >= x2)
55 continue;
56 if (y1 >= y2)
57 continue;
58
59 out_rects[k].left = x1;
60 out_rects[k].top = y1;
61 out_rects[k].right = x2;
62 out_rects[k].bottom = y2;
63 k++;
64 }
65
66 *out_num = k;
67}
68
Sinclair Yehc8261a92015-06-26 01:23:42 -070069void vmw_du_cleanup(struct vmw_display_unit *du)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000070{
71 if (du->cursor_surface)
72 vmw_surface_unreference(&du->cursor_surface);
73 if (du->cursor_dmabuf)
74 vmw_dmabuf_unreference(&du->cursor_dmabuf);
Thomas Wood34ea3d32014-05-29 16:57:41 +010075 drm_connector_unregister(&du->connector);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000076 drm_crtc_cleanup(&du->crtc);
77 drm_encoder_cleanup(&du->encoder);
78 drm_connector_cleanup(&du->connector);
79}
80
81/*
82 * Display Unit Cursor functions
83 */
84
85int vmw_cursor_update_image(struct vmw_private *dev_priv,
86 u32 *image, u32 width, u32 height,
87 u32 hotspotX, u32 hotspotY)
88{
89 struct {
90 u32 cmd;
91 SVGAFifoCmdDefineAlphaCursor cursor;
92 } *cmd;
93 u32 image_size = width * height * 4;
94 u32 cmd_size = sizeof(*cmd) + image_size;
95
96 if (!image)
97 return -EINVAL;
98
99 cmd = vmw_fifo_reserve(dev_priv, cmd_size);
100 if (unlikely(cmd == NULL)) {
101 DRM_ERROR("Fifo reserve failed.\n");
102 return -ENOMEM;
103 }
104
105 memset(cmd, 0, sizeof(*cmd));
106
107 memcpy(&cmd[1], image, image_size);
108
109 cmd->cmd = cpu_to_le32(SVGA_CMD_DEFINE_ALPHA_CURSOR);
110 cmd->cursor.id = cpu_to_le32(0);
111 cmd->cursor.width = cpu_to_le32(width);
112 cmd->cursor.height = cpu_to_le32(height);
113 cmd->cursor.hotspotX = cpu_to_le32(hotspotX);
114 cmd->cursor.hotspotY = cpu_to_le32(hotspotY);
115
116 vmw_fifo_commit(dev_priv, cmd_size);
117
118 return 0;
119}
120
Jakob Bornecrantz6a91d972011-11-28 13:19:10 +0100121int vmw_cursor_update_dmabuf(struct vmw_private *dev_priv,
122 struct vmw_dma_buffer *dmabuf,
123 u32 width, u32 height,
124 u32 hotspotX, u32 hotspotY)
125{
126 struct ttm_bo_kmap_obj map;
127 unsigned long kmap_offset;
128 unsigned long kmap_num;
129 void *virtual;
130 bool dummy;
131 int ret;
132
133 kmap_offset = 0;
134 kmap_num = (width*height*4 + PAGE_SIZE - 1) >> PAGE_SHIFT;
135
Thierry Redingee3939e2014-07-21 13:15:51 +0200136 ret = ttm_bo_reserve(&dmabuf->base, true, false, false, NULL);
Jakob Bornecrantz6a91d972011-11-28 13:19:10 +0100137 if (unlikely(ret != 0)) {
138 DRM_ERROR("reserve failed\n");
139 return -EINVAL;
140 }
141
142 ret = ttm_bo_kmap(&dmabuf->base, kmap_offset, kmap_num, &map);
143 if (unlikely(ret != 0))
144 goto err_unreserve;
145
146 virtual = ttm_kmap_obj_virtual(&map, &dummy);
147 ret = vmw_cursor_update_image(dev_priv, virtual, width, height,
148 hotspotX, hotspotY);
149
150 ttm_bo_kunmap(&map);
151err_unreserve:
152 ttm_bo_unreserve(&dmabuf->base);
153
154 return ret;
155}
156
157
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000158void vmw_cursor_update_position(struct vmw_private *dev_priv,
159 bool show, int x, int y)
160{
161 __le32 __iomem *fifo_mem = dev_priv->mmio_virt;
162 uint32_t count;
163
164 iowrite32(show ? 1 : 0, fifo_mem + SVGA_FIFO_CURSOR_ON);
165 iowrite32(x, fifo_mem + SVGA_FIFO_CURSOR_X);
166 iowrite32(y, fifo_mem + SVGA_FIFO_CURSOR_Y);
167 count = ioread32(fifo_mem + SVGA_FIFO_CURSOR_COUNT);
168 iowrite32(++count, fifo_mem + SVGA_FIFO_CURSOR_COUNT);
169}
170
171int vmw_du_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
172 uint32_t handle, uint32_t width, uint32_t height)
173{
174 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000175 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
176 struct vmw_surface *surface = NULL;
177 struct vmw_dma_buffer *dmabuf = NULL;
178 int ret;
179
Daniel Vetterbfb89922012-12-02 13:48:21 +0100180 /*
181 * FIXME: Unclear whether there's any global state touched by the
182 * cursor_set function, especially vmw_cursor_update_position looks
183 * suspicious. For now take the easy route and reacquire all locks. We
184 * can do this since the caller in the drm core doesn't check anything
185 * which is protected by any looks.
186 */
Rob Clark21e88622014-10-30 13:39:04 -0400187 drm_modeset_unlock_crtc(crtc);
Daniel Vetterbfb89922012-12-02 13:48:21 +0100188 drm_modeset_lock_all(dev_priv->dev);
189
Jakob Bornecrantzbaa91d642011-11-09 10:25:28 +0100190 /* A lot of the code assumes this */
Daniel Vetterbfb89922012-12-02 13:48:21 +0100191 if (handle && (width != 64 || height != 64)) {
192 ret = -EINVAL;
193 goto out;
194 }
Jakob Bornecrantzbaa91d642011-11-09 10:25:28 +0100195
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000196 if (handle) {
Ville Syrjäläa5d0f572013-06-03 16:10:41 +0300197 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
198
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100199 ret = vmw_user_lookup_handle(dev_priv, tfile,
200 handle, &surface, &dmabuf);
201 if (ret) {
202 DRM_ERROR("failed to find surface or dmabuf: %i\n", ret);
Daniel Vetterbfb89922012-12-02 13:48:21 +0100203 ret = -EINVAL;
204 goto out;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000205 }
206 }
207
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100208 /* need to do this before taking down old image */
209 if (surface && !surface->snooper.image) {
210 DRM_ERROR("surface not suitable for cursor\n");
211 vmw_surface_unreference(&surface);
Daniel Vetterbfb89922012-12-02 13:48:21 +0100212 ret = -EINVAL;
213 goto out;
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100214 }
215
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000216 /* takedown old cursor */
217 if (du->cursor_surface) {
218 du->cursor_surface->snooper.crtc = NULL;
219 vmw_surface_unreference(&du->cursor_surface);
220 }
221 if (du->cursor_dmabuf)
222 vmw_dmabuf_unreference(&du->cursor_dmabuf);
223
224 /* setup new image */
225 if (surface) {
226 /* vmw_user_surface_lookup takes one reference */
227 du->cursor_surface = surface;
228
229 du->cursor_surface->snooper.crtc = crtc;
230 du->cursor_age = du->cursor_surface->snooper.age;
231 vmw_cursor_update_image(dev_priv, surface->snooper.image,
232 64, 64, du->hotspot_x, du->hotspot_y);
233 } else if (dmabuf) {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000234 /* vmw_user_surface_lookup takes one reference */
235 du->cursor_dmabuf = dmabuf;
236
Jakob Bornecrantz6a91d972011-11-28 13:19:10 +0100237 ret = vmw_cursor_update_dmabuf(dev_priv, dmabuf, width, height,
238 du->hotspot_x, du->hotspot_y);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000239 } else {
240 vmw_cursor_update_position(dev_priv, false, 0, 0);
Daniel Vetterbfb89922012-12-02 13:48:21 +0100241 ret = 0;
242 goto out;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000243 }
244
Thomas Hellstromda7653d2011-11-02 09:43:12 +0100245 vmw_cursor_update_position(dev_priv, true,
246 du->cursor_x + du->hotspot_x,
247 du->cursor_y + du->hotspot_y);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000248
Daniel Vetterbfb89922012-12-02 13:48:21 +0100249 ret = 0;
250out:
251 drm_modeset_unlock_all(dev_priv->dev);
Daniel Vetter4d02e2d2014-11-11 10:12:00 +0100252 drm_modeset_lock_crtc(crtc, crtc->cursor);
Daniel Vetterbfb89922012-12-02 13:48:21 +0100253
254 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000255}
256
257int vmw_du_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
258{
259 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
260 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
261 bool shown = du->cursor_surface || du->cursor_dmabuf ? true : false;
262
263 du->cursor_x = x + crtc->x;
264 du->cursor_y = y + crtc->y;
265
Daniel Vetterdac35662012-12-02 15:24:10 +0100266 /*
267 * FIXME: Unclear whether there's any global state touched by the
268 * cursor_set function, especially vmw_cursor_update_position looks
269 * suspicious. For now take the easy route and reacquire all locks. We
270 * can do this since the caller in the drm core doesn't check anything
271 * which is protected by any looks.
272 */
Rob Clark21e88622014-10-30 13:39:04 -0400273 drm_modeset_unlock_crtc(crtc);
Daniel Vetterdac35662012-12-02 15:24:10 +0100274 drm_modeset_lock_all(dev_priv->dev);
275
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000276 vmw_cursor_update_position(dev_priv, shown,
Thomas Hellstromda7653d2011-11-02 09:43:12 +0100277 du->cursor_x + du->hotspot_x,
278 du->cursor_y + du->hotspot_y);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000279
Daniel Vetterdac35662012-12-02 15:24:10 +0100280 drm_modeset_unlock_all(dev_priv->dev);
Daniel Vetter4d02e2d2014-11-11 10:12:00 +0100281 drm_modeset_lock_crtc(crtc, crtc->cursor);
Daniel Vetterdac35662012-12-02 15:24:10 +0100282
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000283 return 0;
284}
285
286void vmw_kms_cursor_snoop(struct vmw_surface *srf,
287 struct ttm_object_file *tfile,
288 struct ttm_buffer_object *bo,
289 SVGA3dCmdHeader *header)
290{
291 struct ttm_bo_kmap_obj map;
292 unsigned long kmap_offset;
293 unsigned long kmap_num;
294 SVGA3dCopyBox *box;
295 unsigned box_count;
296 void *virtual;
297 bool dummy;
298 struct vmw_dma_cmd {
299 SVGA3dCmdHeader header;
300 SVGA3dCmdSurfaceDMA dma;
301 } *cmd;
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100302 int i, ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000303
304 cmd = container_of(header, struct vmw_dma_cmd, header);
305
306 /* No snooper installed */
307 if (!srf->snooper.image)
308 return;
309
310 if (cmd->dma.host.face != 0 || cmd->dma.host.mipmap != 0) {
311 DRM_ERROR("face and mipmap for cursors should never != 0\n");
312 return;
313 }
314
315 if (cmd->header.size < 64) {
316 DRM_ERROR("at least one full copy box must be given\n");
317 return;
318 }
319
320 box = (SVGA3dCopyBox *)&cmd[1];
321 box_count = (cmd->header.size - sizeof(SVGA3dCmdSurfaceDMA)) /
322 sizeof(SVGA3dCopyBox);
323
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100324 if (cmd->dma.guest.ptr.offset % PAGE_SIZE ||
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000325 box->x != 0 || box->y != 0 || box->z != 0 ||
326 box->srcx != 0 || box->srcy != 0 || box->srcz != 0 ||
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100327 box->d != 1 || box_count != 1) {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000328 /* TODO handle none page aligned offsets */
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100329 /* TODO handle more dst & src != 0 */
330 /* TODO handle more then one copy */
331 DRM_ERROR("Cant snoop dma request for cursor!\n");
332 DRM_ERROR("(%u, %u, %u) (%u, %u, %u) (%ux%ux%u) %u %u\n",
333 box->srcx, box->srcy, box->srcz,
334 box->x, box->y, box->z,
335 box->w, box->h, box->d, box_count,
336 cmd->dma.guest.ptr.offset);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000337 return;
338 }
339
340 kmap_offset = cmd->dma.guest.ptr.offset >> PAGE_SHIFT;
341 kmap_num = (64*64*4) >> PAGE_SHIFT;
342
Thierry Redingee3939e2014-07-21 13:15:51 +0200343 ret = ttm_bo_reserve(bo, true, false, false, NULL);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000344 if (unlikely(ret != 0)) {
345 DRM_ERROR("reserve failed\n");
346 return;
347 }
348
349 ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
350 if (unlikely(ret != 0))
351 goto err_unreserve;
352
353 virtual = ttm_kmap_obj_virtual(&map, &dummy);
354
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100355 if (box->w == 64 && cmd->dma.guest.pitch == 64*4) {
356 memcpy(srf->snooper.image, virtual, 64*64*4);
357 } else {
358 /* Image is unsigned pointer. */
359 for (i = 0; i < box->h; i++)
360 memcpy(srf->snooper.image + i * 64,
361 virtual + i * cmd->dma.guest.pitch,
362 box->w * 4);
363 }
364
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000365 srf->snooper.age++;
366
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000367 ttm_bo_kunmap(&map);
368err_unreserve:
369 ttm_bo_unreserve(bo);
370}
371
372void vmw_kms_cursor_post_execbuf(struct vmw_private *dev_priv)
373{
374 struct drm_device *dev = dev_priv->dev;
375 struct vmw_display_unit *du;
376 struct drm_crtc *crtc;
377
378 mutex_lock(&dev->mode_config.mutex);
379
380 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
381 du = vmw_crtc_to_du(crtc);
382 if (!du->cursor_surface ||
383 du->cursor_age == du->cursor_surface->snooper.age)
384 continue;
385
386 du->cursor_age = du->cursor_surface->snooper.age;
387 vmw_cursor_update_image(dev_priv,
388 du->cursor_surface->snooper.image,
389 64, 64, du->hotspot_x, du->hotspot_y);
390 }
391
392 mutex_unlock(&dev->mode_config.mutex);
393}
394
395/*
396 * Generic framebuffer code
397 */
398
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000399/*
400 * Surface framebuffer code
401 */
402
Rashika Kheria847c5962014-01-06 22:18:10 +0530403static void vmw_framebuffer_surface_destroy(struct drm_framebuffer *framebuffer)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000404{
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200405 struct vmw_framebuffer_surface *vfbs =
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000406 vmw_framebuffer_to_vfbs(framebuffer);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200407 struct vmw_master *vmaster = vmw_master(vfbs->master);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000408
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200409
410 mutex_lock(&vmaster->fb_surf_mutex);
411 list_del(&vfbs->head);
412 mutex_unlock(&vmaster->fb_surf_mutex);
413
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200414 drm_master_put(&vfbs->master);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000415 drm_framebuffer_cleanup(framebuffer);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200416 vmw_surface_unreference(&vfbs->surface);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200417 ttm_base_object_unref(&vfbs->base.user_obj);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000418
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200419 kfree(vfbs);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000420}
421
Rashika Kheria847c5962014-01-06 22:18:10 +0530422static int vmw_framebuffer_surface_dirty(struct drm_framebuffer *framebuffer,
Thomas Hellstrom02b00162010-10-05 12:43:02 +0200423 struct drm_file *file_priv,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000424 unsigned flags, unsigned color,
425 struct drm_clip_rect *clips,
426 unsigned num_clips)
427{
428 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
429 struct vmw_framebuffer_surface *vfbs =
430 vmw_framebuffer_to_vfbs(framebuffer);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000431 struct drm_clip_rect norect;
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200432 int ret, inc = 1;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000433
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200434 if (unlikely(vfbs->master != file_priv->master))
435 return -EINVAL;
436
Sinclair Yehc8261a92015-06-26 01:23:42 -0700437 /* Legacy Display Unit does not support 3D */
438 if (dev_priv->active_display_unit == vmw_du_legacy)
Jakob Bornecrantz01e81412011-10-04 20:13:24 +0200439 return -EINVAL;
440
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200441 drm_modeset_lock_all(dev_priv->dev);
442
Thomas Hellstrom294adf72014-02-27 12:34:51 +0100443 ret = ttm_read_lock(&dev_priv->reservation_sem, true);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200444 if (unlikely(ret != 0)) {
445 drm_modeset_unlock_all(dev_priv->dev);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200446 return ret;
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200447 }
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200448
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000449 if (!num_clips) {
450 num_clips = 1;
451 clips = &norect;
452 norect.x1 = norect.y1 = 0;
453 norect.x2 = framebuffer->width;
454 norect.y2 = framebuffer->height;
455 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
456 num_clips /= 2;
457 inc = 2; /* skip source rects */
458 }
459
Sinclair Yehc8261a92015-06-26 01:23:42 -0700460 if (dev_priv->active_display_unit == vmw_du_screen_object)
461 ret = vmw_kms_sou_do_surface_dirty(dev_priv, file_priv,
462 &vfbs->base,
463 flags, color,
464 clips, num_clips,
465 inc, NULL);
Sinclair Yeh35c05122015-06-26 01:42:06 -0700466 else
467 ret = vmw_kms_stdu_do_surface_dirty(dev_priv, file_priv,
468 &vfbs->base,
469 clips, num_clips,
470 inc);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000471
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700472 vmw_fifo_flush(dev_priv, false);
Thomas Hellstrom294adf72014-02-27 12:34:51 +0100473 ttm_read_unlock(&dev_priv->reservation_sem);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200474
475 drm_modeset_unlock_all(dev_priv->dev);
476
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000477 return 0;
478}
479
480static struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs = {
481 .destroy = vmw_framebuffer_surface_destroy,
482 .dirty = vmw_framebuffer_surface_dirty,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000483};
484
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200485static int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv,
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200486 struct drm_file *file_priv,
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200487 struct vmw_surface *surface,
488 struct vmw_framebuffer **out,
489 const struct drm_mode_fb_cmd
Sinclair Yehf89c6c32015-06-26 01:54:28 -0700490 *mode_cmd,
491 bool is_dmabuf_proxy)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000492
493{
494 struct drm_device *dev = dev_priv->dev;
495 struct vmw_framebuffer_surface *vfbs;
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200496 enum SVGA3dSurfaceFormat format;
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200497 struct vmw_master *vmaster = vmw_master(file_priv->master);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000498 int ret;
499
Sinclair Yehc8261a92015-06-26 01:23:42 -0700500 /* 3D is only supported on HWv8 and newer hosts */
501 if (dev_priv->active_display_unit == vmw_du_legacy)
Jakob Bornecrantz01e81412011-10-04 20:13:24 +0200502 return -ENOSYS;
503
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200504 /*
505 * Sanity checks.
506 */
507
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100508 /* Surface must be marked as a scanout. */
509 if (unlikely(!surface->scanout))
510 return -EINVAL;
511
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200512 if (unlikely(surface->mip_levels[0] != 1 ||
513 surface->num_sizes != 1 ||
Thomas Hellstromb360a3c2014-01-15 08:51:36 +0100514 surface->base_size.width < mode_cmd->width ||
515 surface->base_size.height < mode_cmd->height ||
516 surface->base_size.depth != 1)) {
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200517 DRM_ERROR("Incompatible surface dimensions "
518 "for requested mode.\n");
519 return -EINVAL;
520 }
521
522 switch (mode_cmd->depth) {
523 case 32:
524 format = SVGA3D_A8R8G8B8;
525 break;
526 case 24:
527 format = SVGA3D_X8R8G8B8;
528 break;
529 case 16:
530 format = SVGA3D_R5G6B5;
531 break;
532 case 15:
533 format = SVGA3D_A1R5G5B5;
534 break;
535 default:
536 DRM_ERROR("Invalid color depth: %d\n", mode_cmd->depth);
537 return -EINVAL;
538 }
539
540 if (unlikely(format != surface->format)) {
541 DRM_ERROR("Invalid surface format for requested mode.\n");
542 return -EINVAL;
543 }
544
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000545 vfbs = kzalloc(sizeof(*vfbs), GFP_KERNEL);
546 if (!vfbs) {
547 ret = -ENOMEM;
548 goto out_err1;
549 }
550
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000551 if (!vmw_surface_reference(surface)) {
552 DRM_ERROR("failed to reference surface %p\n", surface);
Daniel Vetter80f0b5a2012-12-13 23:39:01 +0100553 ret = -EINVAL;
554 goto out_err2;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000555 }
556
557 /* XXX get the first 3 from the surface info */
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200558 vfbs->base.base.bits_per_pixel = mode_cmd->bpp;
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200559 vfbs->base.base.pitches[0] = mode_cmd->pitch;
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200560 vfbs->base.base.depth = mode_cmd->depth;
561 vfbs->base.base.width = mode_cmd->width;
562 vfbs->base.base.height = mode_cmd->height;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000563 vfbs->surface = surface;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200564 vfbs->base.user_handle = mode_cmd->handle;
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200565 vfbs->master = drm_master_get(file_priv->master);
Sinclair Yehf89c6c32015-06-26 01:54:28 -0700566 vfbs->is_dmabuf_proxy = is_dmabuf_proxy;
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200567
568 mutex_lock(&vmaster->fb_surf_mutex);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200569 list_add_tail(&vfbs->head, &vmaster->fb_surf);
570 mutex_unlock(&vmaster->fb_surf_mutex);
571
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000572 *out = &vfbs->base;
573
Daniel Vetter80f0b5a2012-12-13 23:39:01 +0100574 ret = drm_framebuffer_init(dev, &vfbs->base.base,
575 &vmw_framebuffer_surface_funcs);
576 if (ret)
577 goto out_err3;
578
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000579 return 0;
580
581out_err3:
Daniel Vetter80f0b5a2012-12-13 23:39:01 +0100582 vmw_surface_unreference(&surface);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000583out_err2:
584 kfree(vfbs);
585out_err1:
586 return ret;
587}
588
589/*
590 * Dmabuf framebuffer code
591 */
592
Rashika Kheria847c5962014-01-06 22:18:10 +0530593static void vmw_framebuffer_dmabuf_destroy(struct drm_framebuffer *framebuffer)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000594{
595 struct vmw_framebuffer_dmabuf *vfbd =
596 vmw_framebuffer_to_vfbd(framebuffer);
597
598 drm_framebuffer_cleanup(framebuffer);
599 vmw_dmabuf_unreference(&vfbd->buffer);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200600 ttm_base_object_unref(&vfbd->base.user_obj);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000601
602 kfree(vfbd);
603}
604
Rashika Kheria847c5962014-01-06 22:18:10 +0530605static int vmw_framebuffer_dmabuf_dirty(struct drm_framebuffer *framebuffer,
Thomas Hellstrom02b00162010-10-05 12:43:02 +0200606 struct drm_file *file_priv,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000607 unsigned flags, unsigned color,
608 struct drm_clip_rect *clips,
609 unsigned num_clips)
610{
611 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200612 struct vmw_framebuffer_dmabuf *vfbd =
613 vmw_framebuffer_to_vfbd(framebuffer);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000614 struct drm_clip_rect norect;
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200615 int ret, increment = 1;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000616
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200617 drm_modeset_lock_all(dev_priv->dev);
618
Thomas Hellstrom294adf72014-02-27 12:34:51 +0100619 ret = ttm_read_lock(&dev_priv->reservation_sem, true);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200620 if (unlikely(ret != 0)) {
621 drm_modeset_unlock_all(dev_priv->dev);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200622 return ret;
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200623 }
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200624
Thomas Hellstromdf1c93b2010-01-13 22:28:36 +0100625 if (!num_clips) {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000626 num_clips = 1;
627 clips = &norect;
628 norect.x1 = norect.y1 = 0;
629 norect.x2 = framebuffer->width;
630 norect.y2 = framebuffer->height;
631 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
632 num_clips /= 2;
633 increment = 2;
634 }
635
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200636 if (dev_priv->ldu_priv) {
Sinclair Yehc8261a92015-06-26 01:23:42 -0700637 ret = vmw_kms_ldu_do_dmabuf_dirty(dev_priv, &vfbd->base,
638 flags, color,
639 clips, num_clips, increment);
640 } else if (dev_priv->active_display_unit == vmw_du_screen_object) {
641 ret = vmw_kms_sou_do_dmabuf_dirty(file_priv, dev_priv,
642 &vfbd->base,
643 flags, color,
644 clips, num_clips, increment,
645 NULL);
Sinclair Yeh35c05122015-06-26 01:42:06 -0700646 } else {
647 ret = vmw_kms_stdu_do_surface_dirty(dev_priv, file_priv,
648 &vfbd->base,
649 clips, num_clips,
650 increment);
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200651 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000652
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700653 vmw_fifo_flush(dev_priv, false);
Thomas Hellstrom294adf72014-02-27 12:34:51 +0100654 ttm_read_unlock(&dev_priv->reservation_sem);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200655
656 drm_modeset_unlock_all(dev_priv->dev);
657
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200658 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000659}
660
661static struct drm_framebuffer_funcs vmw_framebuffer_dmabuf_funcs = {
662 .destroy = vmw_framebuffer_dmabuf_destroy,
663 .dirty = vmw_framebuffer_dmabuf_dirty,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000664};
665
Jakob Bornecrantz497a3ff2011-10-04 20:13:14 +0200666/**
Jakob Bornecrantz497a3ff2011-10-04 20:13:14 +0200667 * Pin the dmabuffer to the start of vram.
668 */
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000669static int vmw_framebuffer_dmabuf_pin(struct vmw_framebuffer *vfb)
670{
671 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
672 struct vmw_framebuffer_dmabuf *vfbd =
673 vmw_framebuffer_to_vfbd(&vfb->base);
674 int ret;
675
Sinclair Yehc8261a92015-06-26 01:23:42 -0700676 /* This code should only be used with Legacy Display Unit */
677 BUG_ON(dev_priv->active_display_unit != vmw_du_legacy);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +0200678
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000679 vmw_overlay_pause_all(dev_priv);
680
Thomas Hellstrom459d0fa2015-06-26 00:25:37 -0700681 ret = vmw_dmabuf_pin_in_start_of_vram(dev_priv, vfbd->buffer, false);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000682
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000683 vmw_overlay_resume_all(dev_priv);
684
Jakob Bornecrantz316ab132010-05-28 11:22:05 +0200685 WARN_ON(ret != 0);
686
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000687 return 0;
688}
689
690static int vmw_framebuffer_dmabuf_unpin(struct vmw_framebuffer *vfb)
691{
692 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
693 struct vmw_framebuffer_dmabuf *vfbd =
694 vmw_framebuffer_to_vfbd(&vfb->base);
695
696 if (!vfbd->buffer) {
697 WARN_ON(!vfbd->buffer);
698 return 0;
699 }
700
Jakob Bornecrantzd991ef02011-10-04 20:13:21 +0200701 return vmw_dmabuf_unpin(dev_priv, vfbd->buffer, false);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000702}
703
Sinclair Yehf89c6c32015-06-26 01:54:28 -0700704/**
705 * vmw_create_dmabuf_proxy - create a proxy surface for the DMA buf
706 *
707 * @dev: DRM device
708 * @mode_cmd: parameters for the new surface
709 * @dmabuf_mob: MOB backing the DMA buf
710 * @srf_out: newly created surface
711 *
712 * When the content FB is a DMA buf, we create a surface as a proxy to the
713 * same buffer. This way we can do a surface copy rather than a surface DMA.
714 * This is a more efficient approach
715 *
716 * RETURNS:
717 * 0 on success, error code otherwise
718 */
719static int vmw_create_dmabuf_proxy(struct drm_device *dev,
720 struct drm_mode_fb_cmd *mode_cmd,
721 struct vmw_dma_buffer *dmabuf_mob,
722 struct vmw_surface **srf_out)
723{
724 uint32_t format;
725 struct drm_vmw_size content_base_size;
726 int ret;
727
728
729 switch (mode_cmd->depth) {
730 case 32:
731 case 24:
732 format = SVGA3D_X8R8G8B8;
733 break;
734
735 case 16:
736 case 15:
737 format = SVGA3D_R5G6B5;
738 break;
739
740 case 8:
741 format = SVGA3D_P8;
742 break;
743
744 default:
745 DRM_ERROR("Invalid framebuffer format %d\n", mode_cmd->depth);
746 return -EINVAL;
747 }
748
749 content_base_size.width = mode_cmd->width;
750 content_base_size.height = mode_cmd->height;
751 content_base_size.depth = 1;
752
753 ret = vmw_surface_gb_priv_define(dev,
754 0, /* kernel visible only */
755 0, /* flags */
756 format,
757 true, /* can be a scanout buffer */
758 1, /* num of mip levels */
759 0,
760 content_base_size,
761 srf_out);
762 if (ret) {
763 DRM_ERROR("Failed to allocate proxy content buffer\n");
764 return ret;
765 }
766
767 /* Use the same MOB backing for surface */
768 vmw_dmabuf_reference(dmabuf_mob);
769
770 (*srf_out)->res.backup = dmabuf_mob;
771
772 /* FIXME: Waiting for fbdev rework to do a proper reserve/pin */
773 ret = vmw_resource_validate(&(*srf_out)->res);
774
775 return ret;
776}
777
778
779
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200780static int vmw_kms_new_framebuffer_dmabuf(struct vmw_private *dev_priv,
781 struct vmw_dma_buffer *dmabuf,
782 struct vmw_framebuffer **out,
783 const struct drm_mode_fb_cmd
784 *mode_cmd)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000785
786{
787 struct drm_device *dev = dev_priv->dev;
788 struct vmw_framebuffer_dmabuf *vfbd;
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200789 unsigned int requested_size;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000790 int ret;
791
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200792 requested_size = mode_cmd->height * mode_cmd->pitch;
793 if (unlikely(requested_size > dmabuf->base.num_pages * PAGE_SIZE)) {
794 DRM_ERROR("Screen buffer object size is too small "
795 "for requested mode.\n");
796 return -EINVAL;
797 }
798
Jakob Bornecrantzc337ada2011-10-04 20:13:34 +0200799 /* Limited framebuffer color depth support for screen objects */
Sinclair Yehc8261a92015-06-26 01:23:42 -0700800 if (dev_priv->active_display_unit == vmw_du_screen_object) {
Jakob Bornecrantzc337ada2011-10-04 20:13:34 +0200801 switch (mode_cmd->depth) {
802 case 32:
803 case 24:
804 /* Only support 32 bpp for 32 and 24 depth fbs */
805 if (mode_cmd->bpp == 32)
806 break;
807
808 DRM_ERROR("Invalid color depth/bbp: %d %d\n",
809 mode_cmd->depth, mode_cmd->bpp);
810 return -EINVAL;
811 case 16:
812 case 15:
813 /* Only support 16 bpp for 16 and 15 depth fbs */
814 if (mode_cmd->bpp == 16)
815 break;
816
817 DRM_ERROR("Invalid color depth/bbp: %d %d\n",
818 mode_cmd->depth, mode_cmd->bpp);
819 return -EINVAL;
820 default:
821 DRM_ERROR("Invalid color depth: %d\n", mode_cmd->depth);
822 return -EINVAL;
823 }
824 }
825
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000826 vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL);
827 if (!vfbd) {
828 ret = -ENOMEM;
829 goto out_err1;
830 }
831
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000832 if (!vmw_dmabuf_reference(dmabuf)) {
833 DRM_ERROR("failed to reference dmabuf %p\n", dmabuf);
Daniel Vetter80f0b5a2012-12-13 23:39:01 +0100834 ret = -EINVAL;
835 goto out_err2;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000836 }
837
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200838 vfbd->base.base.bits_per_pixel = mode_cmd->bpp;
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200839 vfbd->base.base.pitches[0] = mode_cmd->pitch;
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200840 vfbd->base.base.depth = mode_cmd->depth;
841 vfbd->base.base.width = mode_cmd->width;
842 vfbd->base.base.height = mode_cmd->height;
Sinclair Yehc8261a92015-06-26 01:23:42 -0700843 if (dev_priv->active_display_unit == vmw_du_legacy) {
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200844 vfbd->base.pin = vmw_framebuffer_dmabuf_pin;
845 vfbd->base.unpin = vmw_framebuffer_dmabuf_unpin;
846 }
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200847 vfbd->base.dmabuf = true;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000848 vfbd->buffer = dmabuf;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200849 vfbd->base.user_handle = mode_cmd->handle;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000850 *out = &vfbd->base;
851
Daniel Vetter80f0b5a2012-12-13 23:39:01 +0100852 ret = drm_framebuffer_init(dev, &vfbd->base.base,
853 &vmw_framebuffer_dmabuf_funcs);
854 if (ret)
855 goto out_err3;
856
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000857 return 0;
858
859out_err3:
Daniel Vetter80f0b5a2012-12-13 23:39:01 +0100860 vmw_dmabuf_unreference(&dmabuf);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000861out_err2:
862 kfree(vfbd);
863out_err1:
864 return ret;
865}
866
867/*
868 * Generic Kernel modesetting functions
869 */
870
871static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev,
872 struct drm_file *file_priv,
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800873 struct drm_mode_fb_cmd2 *mode_cmd2)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000874{
875 struct vmw_private *dev_priv = vmw_priv(dev);
876 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
877 struct vmw_framebuffer *vfb = NULL;
878 struct vmw_surface *surface = NULL;
879 struct vmw_dma_buffer *bo = NULL;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200880 struct ttm_base_object *user_obj;
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800881 struct drm_mode_fb_cmd mode_cmd;
Sinclair Yehf89c6c32015-06-26 01:54:28 -0700882 bool is_dmabuf_proxy = false;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000883 int ret;
884
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800885 mode_cmd.width = mode_cmd2->width;
886 mode_cmd.height = mode_cmd2->height;
887 mode_cmd.pitch = mode_cmd2->pitches[0];
888 mode_cmd.handle = mode_cmd2->handles[0];
Dave Airlie248dbc22011-11-29 20:02:54 +0000889 drm_fb_get_bpp_depth(mode_cmd2->pixel_format, &mode_cmd.depth,
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800890 &mode_cmd.bpp);
891
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200892 /**
893 * This code should be conditioned on Screen Objects not being used.
894 * If screen objects are used, we can allocate a GMR to hold the
895 * requested framebuffer.
896 */
897
Xi Wang8a783892011-12-21 05:18:33 -0500898 if (!vmw_kms_validate_mode_vram(dev_priv,
Linus Torvalds1a464cb2012-01-10 11:04:36 -0800899 mode_cmd.pitch,
900 mode_cmd.height)) {
Sinclair Yehc8261a92015-06-26 01:23:42 -0700901 DRM_ERROR("Requested mode exceed bounding box limit.\n");
Jakob Bornecrantzd9826402011-11-03 21:03:04 +0100902 return ERR_PTR(-ENOMEM);
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200903 }
904
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200905 /*
906 * Take a reference on the user object of the resource
907 * backing the kms fb. This ensures that user-space handle
908 * lookups on that resource will always work as long as
909 * it's registered with a kms framebuffer. This is important,
910 * since vmw_execbuf_process identifies resources in the
911 * command stream using user-space handles.
912 */
913
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800914 user_obj = ttm_base_object_lookup(tfile, mode_cmd.handle);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200915 if (unlikely(user_obj == NULL)) {
916 DRM_ERROR("Could not locate requested kms frame buffer.\n");
917 return ERR_PTR(-ENOENT);
918 }
919
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200920 /**
921 * End conditioned code.
922 */
923
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100924 /* returns either a dmabuf or surface */
925 ret = vmw_user_lookup_handle(dev_priv, tfile,
Dave Airlie4cf73122011-12-21 09:50:56 +0000926 mode_cmd.handle,
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100927 &surface, &bo);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000928 if (ret)
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100929 goto err_out;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000930
Sinclair Yehf89c6c32015-06-26 01:54:28 -0700931 /*
932 * We cannot use the SurfaceDMA command in an non-accelerated VM,
933 * therefore, wrap the DMA buf in a surface so we can use the
934 * SurfaceCopy command.
935 */
936 if (bo && !(dev_priv->capabilities & SVGA_CAP_3D) &&
937 dev_priv->active_display_unit == vmw_du_screen_target) {
938 ret = vmw_create_dmabuf_proxy(dev_priv->dev, &mode_cmd, bo,
939 &surface);
940 if (ret)
941 goto err_out;
942
943 is_dmabuf_proxy = true;
944 }
945
946 /* Create the new framebuffer depending one what we have */
947 if (surface)
948 ret = vmw_kms_new_framebuffer_surface(dev_priv, file_priv,
949 surface, &vfb, &mode_cmd,
950 is_dmabuf_proxy);
951 else if (bo)
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100952 ret = vmw_kms_new_framebuffer_dmabuf(dev_priv, bo, &vfb,
Dave Airlie4cf73122011-12-21 09:50:56 +0000953 &mode_cmd);
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100954 else
955 BUG();
Jakob Bornecrantz5ffdb652010-01-30 03:38:08 +0000956
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100957err_out:
958 /* vmw_user_lookup_handle takes one ref so does new_fb */
959 if (bo)
960 vmw_dmabuf_unreference(&bo);
961 if (surface)
962 vmw_surface_unreference(&surface);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000963
964 if (ret) {
965 DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200966 ttm_base_object_unref(&user_obj);
Chris Wilsoncce13ff2010-08-08 13:36:38 +0100967 return ERR_PTR(ret);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200968 } else
969 vfb->user_obj = user_obj;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000970
971 return &vfb->base;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000972}
973
Laurent Pincharte6ecefa2012-05-17 13:27:23 +0200974static const struct drm_mode_config_funcs vmw_kms_funcs = {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000975 .fb_create = vmw_kms_fb_create,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000976};
977
Sinclair Yehc8261a92015-06-26 01:23:42 -0700978int vmw_kms_generic_present(struct vmw_private *dev_priv,
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200979 struct drm_file *file_priv,
980 struct vmw_framebuffer *vfb,
981 struct vmw_surface *surface,
982 uint32_t sid,
983 int32_t destX, int32_t destY,
984 struct drm_vmw_rect *clips,
985 uint32_t num_clips)
986{
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200987 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100988 struct drm_clip_rect *tmp;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200989 struct drm_crtc *crtc;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200990 size_t fifo_size;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200991 int i, k, num_units;
992 int ret = 0; /* silence warning */
Jakob Bornecrantz203dc222011-11-28 13:19:13 +0100993 int left, right, top, bottom;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200994
995 struct {
996 SVGA3dCmdHeader header;
997 SVGA3dCmdBlitSurfaceToScreen body;
998 } *cmd;
999 SVGASignedRect *blits;
1000
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001001 num_units = 0;
1002 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
Matt Roperf4510a22014-04-01 15:22:40 -07001003 if (crtc->primary->fb != &vfb->base)
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001004 continue;
1005 units[num_units++] = vmw_crtc_to_du(crtc);
1006 }
1007
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001008 BUG_ON(surface == NULL);
1009 BUG_ON(!clips || !num_clips);
1010
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +01001011 tmp = kzalloc(sizeof(*tmp) * num_clips, GFP_KERNEL);
1012 if (unlikely(tmp == NULL)) {
1013 DRM_ERROR("Temporary cliprect memory alloc failed.\n");
1014 return -ENOMEM;
1015 }
1016
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001017 fifo_size = sizeof(*cmd) + sizeof(SVGASignedRect) * num_clips;
1018 cmd = kmalloc(fifo_size, GFP_KERNEL);
1019 if (unlikely(cmd == NULL)) {
1020 DRM_ERROR("Failed to allocate temporary fifo memory.\n");
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +01001021 ret = -ENOMEM;
1022 goto out_free_tmp;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001023 }
1024
Jakob Bornecrantz203dc222011-11-28 13:19:13 +01001025 left = clips->x;
1026 right = clips->x + clips->w;
1027 top = clips->y;
1028 bottom = clips->y + clips->h;
1029
1030 for (i = 1; i < num_clips; i++) {
1031 left = min_t(int, left, (int)clips[i].x);
1032 right = max_t(int, right, (int)clips[i].x + clips[i].w);
1033 top = min_t(int, top, (int)clips[i].y);
1034 bottom = max_t(int, bottom, (int)clips[i].y + clips[i].h);
1035 }
1036
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001037 /* only need to do this once */
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001038 memset(cmd, 0, fifo_size);
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001039 cmd->header.id = cpu_to_le32(SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN);
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +01001040
1041 blits = (SVGASignedRect *)&cmd[1];
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001042
Jakob Bornecrantz203dc222011-11-28 13:19:13 +01001043 cmd->body.srcRect.left = left;
1044 cmd->body.srcRect.right = right;
1045 cmd->body.srcRect.top = top;
1046 cmd->body.srcRect.bottom = bottom;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001047
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001048 for (i = 0; i < num_clips; i++) {
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +01001049 tmp[i].x1 = clips[i].x - left;
1050 tmp[i].x2 = clips[i].x + clips[i].w - left;
1051 tmp[i].y1 = clips[i].y - top;
1052 tmp[i].y2 = clips[i].y + clips[i].h - top;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001053 }
1054
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001055 for (k = 0; k < num_units; k++) {
1056 struct vmw_display_unit *unit = units[k];
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +01001057 struct vmw_clip_rect clip;
1058 int num;
1059
1060 clip.x1 = left + destX - unit->crtc.x;
1061 clip.y1 = top + destY - unit->crtc.y;
1062 clip.x2 = right + destX - unit->crtc.x;
1063 clip.y2 = bottom + destY - unit->crtc.y;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001064
1065 /* skip any crtcs that misses the clip region */
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +01001066 if (clip.x1 >= unit->crtc.mode.hdisplay ||
1067 clip.y1 >= unit->crtc.mode.vdisplay ||
1068 clip.x2 <= 0 || clip.y2 <= 0)
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001069 continue;
1070
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +01001071 /*
1072 * In order for the clip rects to be correctly scaled
1073 * the src and dest rects needs to be the same size.
1074 */
1075 cmd->body.destRect.left = clip.x1;
1076 cmd->body.destRect.right = clip.x2;
1077 cmd->body.destRect.top = clip.y1;
1078 cmd->body.destRect.bottom = clip.y2;
1079
1080 /* create a clip rect of the crtc in dest coords */
1081 clip.x2 = unit->crtc.mode.hdisplay - clip.x1;
1082 clip.y2 = unit->crtc.mode.vdisplay - clip.y1;
1083 clip.x1 = 0 - clip.x1;
1084 clip.y1 = 0 - clip.y1;
1085
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001086 /* need to reset sid as it is changed by execbuf */
1087 cmd->body.srcImage.sid = sid;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001088 cmd->body.destScreenId = unit->unit;
1089
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +01001090 /* clip and write blits to cmd stream */
1091 vmw_clip_cliprects(tmp, num_clips, clip, blits, &num);
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001092
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +01001093 /* if no cliprects hit skip this */
1094 if (num == 0)
1095 continue;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001096
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +01001097 /* recalculate package length */
1098 fifo_size = sizeof(*cmd) + sizeof(SVGASignedRect) * num;
1099 cmd->header.size = cpu_to_le32(fifo_size - sizeof(cmd->header));
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001100 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd,
Thomas Hellstromc9146cd2015-03-02 23:45:04 -08001101 fifo_size, 0, 0, NULL, NULL);
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001102
1103 if (unlikely(ret != 0))
1104 break;
1105 }
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001106
1107 kfree(cmd);
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +01001108out_free_tmp:
1109 kfree(tmp);
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001110
1111 return ret;
1112}
1113
Sinclair Yehc8261a92015-06-26 01:23:42 -07001114int vmw_kms_present(struct vmw_private *dev_priv,
1115 struct drm_file *file_priv,
1116 struct vmw_framebuffer *vfb,
1117 struct vmw_surface *surface,
1118 uint32_t sid,
1119 int32_t destX, int32_t destY,
1120 struct drm_vmw_rect *clips,
1121 uint32_t num_clips)
1122{
Sinclair Yeh35c05122015-06-26 01:42:06 -07001123 int ret;
1124
1125 if (dev_priv->active_display_unit == vmw_du_screen_target)
1126 ret = vmw_kms_stdu_present(dev_priv, file_priv, vfb, sid,
1127 destX, destY, clips, num_clips);
1128 else
1129 ret = vmw_kms_generic_present(dev_priv, file_priv, vfb,
1130 surface, sid, destX, destY,
1131 clips, num_clips);
1132 if (ret)
1133 return ret;
1134
1135 vmw_fifo_flush(dev_priv, false);
1136
1137 return 0;
Sinclair Yehc8261a92015-06-26 01:23:42 -07001138}
1139
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001140int vmw_kms_readback(struct vmw_private *dev_priv,
1141 struct drm_file *file_priv,
1142 struct vmw_framebuffer *vfb,
1143 struct drm_vmw_fence_rep __user *user_fence_rep,
1144 struct drm_vmw_rect *clips,
1145 uint32_t num_clips)
1146{
1147 struct vmw_framebuffer_dmabuf *vfbd =
1148 vmw_framebuffer_to_vfbd(&vfb->base);
1149 struct vmw_dma_buffer *dmabuf = vfbd->buffer;
1150 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
1151 struct drm_crtc *crtc;
1152 size_t fifo_size;
1153 int i, k, ret, num_units, blits_pos;
1154
1155 struct {
1156 uint32_t header;
1157 SVGAFifoCmdDefineGMRFB body;
1158 } *cmd;
1159 struct {
1160 uint32_t header;
1161 SVGAFifoCmdBlitScreenToGMRFB body;
1162 } *blits;
1163
1164 num_units = 0;
1165 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
Matt Roperf4510a22014-04-01 15:22:40 -07001166 if (crtc->primary->fb != &vfb->base)
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001167 continue;
1168 units[num_units++] = vmw_crtc_to_du(crtc);
1169 }
1170
1171 BUG_ON(dmabuf == NULL);
1172 BUG_ON(!clips || !num_clips);
1173
1174 /* take a safe guess at fifo size */
1175 fifo_size = sizeof(*cmd) + sizeof(*blits) * num_clips * num_units;
1176 cmd = kmalloc(fifo_size, GFP_KERNEL);
1177 if (unlikely(cmd == NULL)) {
1178 DRM_ERROR("Failed to allocate temporary fifo memory.\n");
1179 return -ENOMEM;
1180 }
1181
1182 memset(cmd, 0, fifo_size);
1183 cmd->header = SVGA_CMD_DEFINE_GMRFB;
1184 cmd->body.format.bitsPerPixel = vfb->base.bits_per_pixel;
1185 cmd->body.format.colorDepth = vfb->base.depth;
1186 cmd->body.format.reserved = 0;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02001187 cmd->body.bytesPerLine = vfb->base.pitches[0];
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001188 cmd->body.ptr.gmrId = vfb->user_handle;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001189 cmd->body.ptr.offset = 0;
1190
1191 blits = (void *)&cmd[1];
1192 blits_pos = 0;
1193 for (i = 0; i < num_units; i++) {
1194 struct drm_vmw_rect *c = clips;
1195 for (k = 0; k < num_clips; k++, c++) {
1196 /* transform clip coords to crtc origin based coords */
1197 int clip_x1 = c->x - units[i]->crtc.x;
1198 int clip_x2 = c->x - units[i]->crtc.x + c->w;
1199 int clip_y1 = c->y - units[i]->crtc.y;
1200 int clip_y2 = c->y - units[i]->crtc.y + c->h;
1201 int dest_x = c->x;
1202 int dest_y = c->y;
1203
1204 /* compensate for clipping, we negate
1205 * a negative number and add that.
1206 */
1207 if (clip_x1 < 0)
1208 dest_x += -clip_x1;
1209 if (clip_y1 < 0)
1210 dest_y += -clip_y1;
1211
1212 /* clip */
1213 clip_x1 = max(clip_x1, 0);
1214 clip_y1 = max(clip_y1, 0);
1215 clip_x2 = min(clip_x2, units[i]->crtc.mode.hdisplay);
1216 clip_y2 = min(clip_y2, units[i]->crtc.mode.vdisplay);
1217
1218 /* and cull any rects that misses the crtc */
1219 if (clip_x1 >= units[i]->crtc.mode.hdisplay ||
1220 clip_y1 >= units[i]->crtc.mode.vdisplay ||
1221 clip_x2 <= 0 || clip_y2 <= 0)
1222 continue;
1223
1224 blits[blits_pos].header = SVGA_CMD_BLIT_SCREEN_TO_GMRFB;
1225 blits[blits_pos].body.srcScreenId = units[i]->unit;
1226 blits[blits_pos].body.destOrigin.x = dest_x;
1227 blits[blits_pos].body.destOrigin.y = dest_y;
1228
1229 blits[blits_pos].body.srcRect.left = clip_x1;
1230 blits[blits_pos].body.srcRect.top = clip_y1;
1231 blits[blits_pos].body.srcRect.right = clip_x2;
1232 blits[blits_pos].body.srcRect.bottom = clip_y2;
1233 blits_pos++;
1234 }
1235 }
1236 /* reset size here and use calculated exact size from loops */
1237 fifo_size = sizeof(*cmd) + sizeof(*blits) * blits_pos;
1238
1239 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd, fifo_size,
Thomas Hellstromc9146cd2015-03-02 23:45:04 -08001240 0, 0, user_fence_rep, NULL);
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001241
1242 kfree(cmd);
1243
1244 return ret;
1245}
1246
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001247int vmw_kms_init(struct vmw_private *dev_priv)
1248{
1249 struct drm_device *dev = dev_priv->dev;
1250 int ret;
1251
1252 drm_mode_config_init(dev);
1253 dev->mode_config.funcs = &vmw_kms_funcs;
Jakob Bornecrantz3bef3572010-02-09 19:41:57 +00001254 dev->mode_config.min_width = 1;
1255 dev->mode_config.min_height = 1;
Jakob Bornecrantz7e71f8a2010-05-28 11:21:54 +02001256 /* assumed largest fb size */
1257 dev->mode_config.max_width = 8192;
1258 dev->mode_config.max_height = 8192;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001259
Sinclair Yeh35c05122015-06-26 01:42:06 -07001260 ret = vmw_kms_stdu_init_display(dev_priv);
1261 if (ret) {
1262 ret = vmw_kms_sou_init_display(dev_priv);
1263 if (ret) /* Fallback */
1264 ret = vmw_kms_ldu_init_display(dev_priv);
1265 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001266
Sinclair Yehc8261a92015-06-26 01:23:42 -07001267 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001268}
1269
1270int vmw_kms_close(struct vmw_private *dev_priv)
1271{
Sinclair Yehc8261a92015-06-26 01:23:42 -07001272 int ret;
1273
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001274 /*
1275 * Docs says we should take the lock before calling this function
1276 * but since it destroys encoders and our destructor calls
1277 * drm_encoder_cleanup which takes the lock we deadlock.
1278 */
1279 drm_mode_config_cleanup(dev_priv->dev);
Sinclair Yehc8261a92015-06-26 01:23:42 -07001280 if (dev_priv->active_display_unit == vmw_du_screen_object)
1281 ret = vmw_kms_sou_close_display(dev_priv);
Sinclair Yeh35c05122015-06-26 01:42:06 -07001282 else if (dev_priv->active_display_unit == vmw_du_screen_target)
1283 ret = vmw_kms_stdu_close_display(dev_priv);
Jakob Bornecrantzc0d18312011-11-09 10:25:26 +01001284 else
Sinclair Yehc8261a92015-06-26 01:23:42 -07001285 ret = vmw_kms_ldu_close_display(dev_priv);
1286
1287 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001288}
1289
1290int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
1291 struct drm_file *file_priv)
1292{
1293 struct drm_vmw_cursor_bypass_arg *arg = data;
1294 struct vmw_display_unit *du;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001295 struct drm_crtc *crtc;
1296 int ret = 0;
1297
1298
1299 mutex_lock(&dev->mode_config.mutex);
1300 if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
1301
1302 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1303 du = vmw_crtc_to_du(crtc);
1304 du->hotspot_x = arg->xhot;
1305 du->hotspot_y = arg->yhot;
1306 }
1307
1308 mutex_unlock(&dev->mode_config.mutex);
1309 return 0;
1310 }
1311
Rob Clarka4cd5d62014-07-17 23:30:02 -04001312 crtc = drm_crtc_find(dev, arg->crtc_id);
1313 if (!crtc) {
Ville Syrjälä4ae87ff2013-10-17 13:35:05 +03001314 ret = -ENOENT;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001315 goto out;
1316 }
1317
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001318 du = vmw_crtc_to_du(crtc);
1319
1320 du->hotspot_x = arg->xhot;
1321 du->hotspot_y = arg->yhot;
1322
1323out:
1324 mutex_unlock(&dev->mode_config.mutex);
1325
1326 return ret;
1327}
1328
Michel Dänzer0bef23f2011-08-31 07:42:50 +00001329int vmw_kms_write_svga(struct vmw_private *vmw_priv,
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001330 unsigned width, unsigned height, unsigned pitch,
Michel Dänzer6558429b2011-08-31 07:42:49 +00001331 unsigned bpp, unsigned depth)
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001332{
1333 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1334 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch);
1335 else if (vmw_fifo_have_pitchlock(vmw_priv))
1336 iowrite32(pitch, vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
1337 vmw_write(vmw_priv, SVGA_REG_WIDTH, width);
1338 vmw_write(vmw_priv, SVGA_REG_HEIGHT, height);
Michel Dänzer6558429b2011-08-31 07:42:49 +00001339 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp);
Michel Dänzer0bef23f2011-08-31 07:42:50 +00001340
1341 if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) {
1342 DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n",
1343 depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH));
1344 return -EINVAL;
1345 }
1346
1347 return 0;
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001348}
1349
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001350int vmw_kms_save_vga(struct vmw_private *vmw_priv)
1351{
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001352 struct vmw_vga_topology_state *save;
1353 uint32_t i;
1354
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001355 vmw_priv->vga_width = vmw_read(vmw_priv, SVGA_REG_WIDTH);
1356 vmw_priv->vga_height = vmw_read(vmw_priv, SVGA_REG_HEIGHT);
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001357 vmw_priv->vga_bpp = vmw_read(vmw_priv, SVGA_REG_BITS_PER_PIXEL);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001358 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1359 vmw_priv->vga_pitchlock =
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001360 vmw_read(vmw_priv, SVGA_REG_PITCHLOCK);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001361 else if (vmw_fifo_have_pitchlock(vmw_priv))
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001362 vmw_priv->vga_pitchlock = ioread32(vmw_priv->mmio_virt +
Sinclair Yehc8261a92015-06-26 01:23:42 -07001363 SVGA_FIFO_PITCHLOCK);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001364
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001365 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1366 return 0;
1367
1368 vmw_priv->num_displays = vmw_read(vmw_priv,
1369 SVGA_REG_NUM_GUEST_DISPLAYS);
1370
Thomas Hellstrom029e50b2010-10-05 12:43:08 +02001371 if (vmw_priv->num_displays == 0)
1372 vmw_priv->num_displays = 1;
1373
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001374 for (i = 0; i < vmw_priv->num_displays; ++i) {
1375 save = &vmw_priv->vga_save[i];
1376 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1377 save->primary = vmw_read(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY);
1378 save->pos_x = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_X);
1379 save->pos_y = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y);
1380 save->width = vmw_read(vmw_priv, SVGA_REG_DISPLAY_WIDTH);
1381 save->height = vmw_read(vmw_priv, SVGA_REG_DISPLAY_HEIGHT);
1382 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
Thomas Hellstrom30c78bb2010-10-01 10:21:48 +02001383 if (i == 0 && vmw_priv->num_displays == 1 &&
1384 save->width == 0 && save->height == 0) {
1385
1386 /*
1387 * It should be fairly safe to assume that these
1388 * values are uninitialized.
1389 */
1390
1391 save->width = vmw_priv->vga_width - save->pos_x;
1392 save->height = vmw_priv->vga_height - save->pos_y;
1393 }
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001394 }
Thomas Hellstrom30c78bb2010-10-01 10:21:48 +02001395
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001396 return 0;
1397}
1398
1399int vmw_kms_restore_vga(struct vmw_private *vmw_priv)
1400{
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001401 struct vmw_vga_topology_state *save;
1402 uint32_t i;
1403
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001404 vmw_write(vmw_priv, SVGA_REG_WIDTH, vmw_priv->vga_width);
1405 vmw_write(vmw_priv, SVGA_REG_HEIGHT, vmw_priv->vga_height);
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001406 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, vmw_priv->vga_bpp);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001407 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1408 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK,
1409 vmw_priv->vga_pitchlock);
1410 else if (vmw_fifo_have_pitchlock(vmw_priv))
1411 iowrite32(vmw_priv->vga_pitchlock,
1412 vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001413
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001414 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1415 return 0;
1416
1417 for (i = 0; i < vmw_priv->num_displays; ++i) {
1418 save = &vmw_priv->vga_save[i];
1419 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1420 vmw_write(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY, save->primary);
1421 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_X, save->pos_x);
1422 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y, save->pos_y);
1423 vmw_write(vmw_priv, SVGA_REG_DISPLAY_WIDTH, save->width);
1424 vmw_write(vmw_priv, SVGA_REG_DISPLAY_HEIGHT, save->height);
1425 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
1426 }
1427
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001428 return 0;
1429}
Jakob Bornecrantzd8bd19d2010-06-01 11:54:20 +02001430
Thomas Hellstrome133e732010-10-05 12:43:04 +02001431bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
1432 uint32_t pitch,
1433 uint32_t height)
1434{
Sinclair Yeh35c05122015-06-26 01:42:06 -07001435 return ((u64) pitch * (u64) height) < (u64)
1436 ((dev_priv->active_display_unit == vmw_du_screen_target) ?
1437 dev_priv->prim_bb_mem : dev_priv->vram_size);
Thomas Hellstrome133e732010-10-05 12:43:04 +02001438}
1439
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001440
1441/**
1442 * Function called by DRM code called with vbl_lock held.
1443 */
Thomas Hellstrom7a1c2f62010-10-01 10:21:49 +02001444u32 vmw_get_vblank_counter(struct drm_device *dev, int crtc)
1445{
1446 return 0;
1447}
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001448
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001449/**
1450 * Function called by DRM code called with vbl_lock held.
1451 */
1452int vmw_enable_vblank(struct drm_device *dev, int crtc)
1453{
1454 return -ENOSYS;
1455}
1456
1457/**
1458 * Function called by DRM code called with vbl_lock held.
1459 */
1460void vmw_disable_vblank(struct drm_device *dev, int crtc)
1461{
1462}
1463
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001464
1465/*
1466 * Small shared kms functions.
1467 */
1468
Rashika Kheria847c5962014-01-06 22:18:10 +05301469static int vmw_du_update_layout(struct vmw_private *dev_priv, unsigned num,
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001470 struct drm_vmw_rect *rects)
1471{
1472 struct drm_device *dev = dev_priv->dev;
1473 struct vmw_display_unit *du;
1474 struct drm_connector *con;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001475
1476 mutex_lock(&dev->mode_config.mutex);
1477
1478#if 0
Thomas Hellstrom6ea77d12011-10-04 20:13:36 +02001479 {
1480 unsigned int i;
1481
1482 DRM_INFO("%s: new layout ", __func__);
1483 for (i = 0; i < num; i++)
1484 DRM_INFO("(%i, %i %ux%u) ", rects[i].x, rects[i].y,
1485 rects[i].w, rects[i].h);
1486 DRM_INFO("\n");
1487 }
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001488#endif
1489
1490 list_for_each_entry(con, &dev->mode_config.connector_list, head) {
1491 du = vmw_connector_to_du(con);
1492 if (num > du->unit) {
1493 du->pref_width = rects[du->unit].w;
1494 du->pref_height = rects[du->unit].h;
1495 du->pref_active = true;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001496 du->gui_x = rects[du->unit].x;
1497 du->gui_y = rects[du->unit].y;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001498 } else {
1499 du->pref_width = 800;
1500 du->pref_height = 600;
1501 du->pref_active = false;
1502 }
1503 con->status = vmw_du_connector_detect(con, true);
1504 }
1505
1506 mutex_unlock(&dev->mode_config.mutex);
1507
1508 return 0;
1509}
1510
1511void vmw_du_crtc_save(struct drm_crtc *crtc)
1512{
1513}
1514
1515void vmw_du_crtc_restore(struct drm_crtc *crtc)
1516{
1517}
1518
1519void vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
1520 u16 *r, u16 *g, u16 *b,
1521 uint32_t start, uint32_t size)
1522{
1523 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
1524 int i;
1525
1526 for (i = 0; i < size; i++) {
1527 DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i,
1528 r[i], g[i], b[i]);
1529 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 0, r[i] >> 8);
1530 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 1, g[i] >> 8);
1531 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 2, b[i] >> 8);
1532 }
1533}
1534
1535void vmw_du_connector_dpms(struct drm_connector *connector, int mode)
1536{
1537}
1538
1539void vmw_du_connector_save(struct drm_connector *connector)
1540{
1541}
1542
1543void vmw_du_connector_restore(struct drm_connector *connector)
1544{
1545}
1546
1547enum drm_connector_status
1548vmw_du_connector_detect(struct drm_connector *connector, bool force)
1549{
1550 uint32_t num_displays;
1551 struct drm_device *dev = connector->dev;
1552 struct vmw_private *dev_priv = vmw_priv(dev);
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001553 struct vmw_display_unit *du = vmw_connector_to_du(connector);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001554
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001555 num_displays = vmw_read(dev_priv, SVGA_REG_NUM_DISPLAYS);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001556
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001557 return ((vmw_connector_to_du(connector)->unit < num_displays &&
1558 du->pref_active) ?
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001559 connector_status_connected : connector_status_disconnected);
1560}
1561
1562static struct drm_display_mode vmw_kms_connector_builtin[] = {
1563 /* 640x480@60Hz */
1564 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
1565 752, 800, 0, 480, 489, 492, 525, 0,
1566 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1567 /* 800x600@60Hz */
1568 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
1569 968, 1056, 0, 600, 601, 605, 628, 0,
1570 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1571 /* 1024x768@60Hz */
1572 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
1573 1184, 1344, 0, 768, 771, 777, 806, 0,
1574 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1575 /* 1152x864@75Hz */
1576 { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
1577 1344, 1600, 0, 864, 865, 868, 900, 0,
1578 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1579 /* 1280x768@60Hz */
1580 { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344,
1581 1472, 1664, 0, 768, 771, 778, 798, 0,
1582 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1583 /* 1280x800@60Hz */
1584 { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352,
1585 1480, 1680, 0, 800, 803, 809, 831, 0,
1586 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
1587 /* 1280x960@60Hz */
1588 { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376,
1589 1488, 1800, 0, 960, 961, 964, 1000, 0,
1590 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1591 /* 1280x1024@60Hz */
1592 { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328,
1593 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
1594 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1595 /* 1360x768@60Hz */
1596 { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424,
1597 1536, 1792, 0, 768, 771, 777, 795, 0,
1598 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1599 /* 1440x1050@60Hz */
1600 { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488,
1601 1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
1602 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1603 /* 1440x900@60Hz */
1604 { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520,
1605 1672, 1904, 0, 900, 903, 909, 934, 0,
1606 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1607 /* 1600x1200@60Hz */
1608 { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664,
1609 1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
1610 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1611 /* 1680x1050@60Hz */
1612 { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784,
1613 1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
1614 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1615 /* 1792x1344@60Hz */
1616 { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920,
1617 2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
1618 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1619 /* 1853x1392@60Hz */
1620 { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952,
1621 2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
1622 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1623 /* 1920x1200@60Hz */
1624 { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056,
1625 2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
1626 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1627 /* 1920x1440@60Hz */
1628 { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048,
1629 2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
1630 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1631 /* 2560x1600@60Hz */
1632 { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752,
1633 3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
1634 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1635 /* Terminate */
1636 { DRM_MODE("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) },
1637};
1638
Thomas Hellstrom1543b4d2011-11-02 09:43:10 +01001639/**
1640 * vmw_guess_mode_timing - Provide fake timings for a
1641 * 60Hz vrefresh mode.
1642 *
1643 * @mode - Pointer to a struct drm_display_mode with hdisplay and vdisplay
1644 * members filled in.
1645 */
1646static void vmw_guess_mode_timing(struct drm_display_mode *mode)
1647{
1648 mode->hsync_start = mode->hdisplay + 50;
1649 mode->hsync_end = mode->hsync_start + 50;
1650 mode->htotal = mode->hsync_end + 50;
1651
1652 mode->vsync_start = mode->vdisplay + 50;
1653 mode->vsync_end = mode->vsync_start + 50;
1654 mode->vtotal = mode->vsync_end + 50;
1655
1656 mode->clock = (u32)mode->htotal * (u32)mode->vtotal / 100 * 6;
1657 mode->vrefresh = drm_mode_vrefresh(mode);
1658}
1659
1660
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001661int vmw_du_connector_fill_modes(struct drm_connector *connector,
1662 uint32_t max_width, uint32_t max_height)
1663{
1664 struct vmw_display_unit *du = vmw_connector_to_du(connector);
1665 struct drm_device *dev = connector->dev;
1666 struct vmw_private *dev_priv = vmw_priv(dev);
1667 struct drm_display_mode *mode = NULL;
1668 struct drm_display_mode *bmode;
1669 struct drm_display_mode prefmode = { DRM_MODE("preferred",
1670 DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
1671 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1672 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
1673 };
1674 int i;
Sinclair Yeh9a723842014-10-31 09:58:06 +01001675 u32 assumed_bpp = 2;
1676
1677 /*
1678 * If using screen objects, then assume 32-bpp because that's what the
1679 * SVGA device is assuming
1680 */
Sinclair Yehc8261a92015-06-26 01:23:42 -07001681 if (dev_priv->active_display_unit == vmw_du_screen_object)
Sinclair Yeh9a723842014-10-31 09:58:06 +01001682 assumed_bpp = 4;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001683
Sinclair Yeh35c05122015-06-26 01:42:06 -07001684 if (dev_priv->active_display_unit == vmw_du_screen_target) {
1685 max_width = min(max_width, dev_priv->stdu_max_width);
1686 max_height = min(max_height, dev_priv->stdu_max_height);
1687 }
1688
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001689 /* Add preferred mode */
Sinclair Yehc8261a92015-06-26 01:23:42 -07001690 mode = drm_mode_duplicate(dev, &prefmode);
1691 if (!mode)
1692 return 0;
1693 mode->hdisplay = du->pref_width;
1694 mode->vdisplay = du->pref_height;
1695 vmw_guess_mode_timing(mode);
Jakob Bornecrantz55bde5b2011-11-03 21:03:05 +01001696
Sinclair Yehc8261a92015-06-26 01:23:42 -07001697 if (vmw_kms_validate_mode_vram(dev_priv,
1698 mode->hdisplay * assumed_bpp,
1699 mode->vdisplay)) {
1700 drm_mode_probed_add(connector, mode);
1701 } else {
1702 drm_mode_destroy(dev, mode);
1703 mode = NULL;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001704 }
1705
Sinclair Yehc8261a92015-06-26 01:23:42 -07001706 if (du->pref_mode) {
1707 list_del_init(&du->pref_mode->head);
1708 drm_mode_destroy(dev, du->pref_mode);
1709 }
1710
1711 /* mode might be null here, this is intended */
1712 du->pref_mode = mode;
1713
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001714 for (i = 0; vmw_kms_connector_builtin[i].type != 0; i++) {
1715 bmode = &vmw_kms_connector_builtin[i];
1716 if (bmode->hdisplay > max_width ||
1717 bmode->vdisplay > max_height)
1718 continue;
1719
Sinclair Yeh9a723842014-10-31 09:58:06 +01001720 if (!vmw_kms_validate_mode_vram(dev_priv,
1721 bmode->hdisplay * assumed_bpp,
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001722 bmode->vdisplay))
1723 continue;
1724
1725 mode = drm_mode_duplicate(dev, bmode);
1726 if (!mode)
1727 return 0;
1728 mode->vrefresh = drm_mode_vrefresh(mode);
1729
1730 drm_mode_probed_add(connector, mode);
1731 }
1732
Jakob Bornecrantzd41025c2011-11-03 21:03:07 +01001733 /* Move the prefered mode first, help apps pick the right mode. */
1734 if (du->pref_mode)
1735 list_move(&du->pref_mode->head, &connector->probed_modes);
1736
Dave Airlieb87577b2014-05-01 09:26:53 +10001737 drm_mode_connector_list_update(connector, true);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001738
1739 return 1;
1740}
1741
1742int vmw_du_connector_set_property(struct drm_connector *connector,
1743 struct drm_property *property,
1744 uint64_t val)
1745{
1746 return 0;
1747}
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001748
1749
1750int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data,
1751 struct drm_file *file_priv)
1752{
1753 struct vmw_private *dev_priv = vmw_priv(dev);
1754 struct drm_vmw_update_layout_arg *arg =
1755 (struct drm_vmw_update_layout_arg *)data;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001756 void __user *user_rects;
1757 struct drm_vmw_rect *rects;
1758 unsigned rects_size;
1759 int ret;
1760 int i;
1761 struct drm_mode_config *mode_config = &dev->mode_config;
Sinclair Yehc8261a92015-06-26 01:23:42 -07001762 struct drm_vmw_rect bounding_box = {0};
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001763
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001764 if (!arg->num_outputs) {
1765 struct drm_vmw_rect def_rect = {0, 0, 800, 600};
1766 vmw_du_update_layout(dev_priv, 1, &def_rect);
Thomas Hellstrom5151adb2015-03-09 01:56:21 -07001767 return 0;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001768 }
1769
1770 rects_size = arg->num_outputs * sizeof(struct drm_vmw_rect);
Xi Wangbab9efc2011-11-28 12:25:43 +01001771 rects = kcalloc(arg->num_outputs, sizeof(struct drm_vmw_rect),
1772 GFP_KERNEL);
Thomas Hellstrom5151adb2015-03-09 01:56:21 -07001773 if (unlikely(!rects))
1774 return -ENOMEM;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001775
1776 user_rects = (void __user *)(unsigned long)arg->rects;
1777 ret = copy_from_user(rects, user_rects, rects_size);
1778 if (unlikely(ret != 0)) {
1779 DRM_ERROR("Failed to get rects.\n");
1780 ret = -EFAULT;
1781 goto out_free;
1782 }
1783
1784 for (i = 0; i < arg->num_outputs; ++i) {
Xi Wangbab9efc2011-11-28 12:25:43 +01001785 if (rects[i].x < 0 ||
1786 rects[i].y < 0 ||
1787 rects[i].x + rects[i].w > mode_config->max_width ||
1788 rects[i].y + rects[i].h > mode_config->max_height) {
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001789 DRM_ERROR("Invalid GUI layout.\n");
1790 ret = -EINVAL;
1791 goto out_free;
1792 }
Sinclair Yehc8261a92015-06-26 01:23:42 -07001793
1794 /*
1795 * bounding_box.w and bunding_box.h are used as
1796 * lower-right coordinates
1797 */
1798 if (rects[i].x + rects[i].w > bounding_box.w)
1799 bounding_box.w = rects[i].x + rects[i].w;
1800
1801 if (rects[i].y + rects[i].h > bounding_box.h)
1802 bounding_box.h = rects[i].y + rects[i].h;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001803 }
1804
Sinclair Yeh35c05122015-06-26 01:42:06 -07001805 /*
1806 * For Screen Target Display Unit, all the displays must fit
1807 * inside of maximum texture size.
1808 */
1809 if (dev_priv->active_display_unit == vmw_du_screen_target)
1810 if (bounding_box.w > dev_priv->texture_max_width ||
1811 bounding_box.h > dev_priv->texture_max_height) {
1812 DRM_ERROR("Layout exceeds maximum texture size\n");
1813 ret = -EINVAL;
1814 goto out_free;
1815 }
1816
1817
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001818 vmw_du_update_layout(dev_priv, arg->num_outputs, rects);
1819
1820out_free:
1821 kfree(rects);
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001822 return ret;
1823}
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07001824
1825/**
1826 * vmw_kms_helper_dirty - Helper to build commands and perform actions based
1827 * on a set of cliprects and a set of display units.
1828 *
1829 * @dev_priv: Pointer to a device private structure.
1830 * @framebuffer: Pointer to the framebuffer on which to perform the actions.
1831 * @clips: A set of struct drm_clip_rect. Either this os @vclips must be NULL.
1832 * Cliprects are given in framebuffer coordinates.
1833 * @vclips: A set of struct drm_vmw_rect cliprects. Either this or @clips must
1834 * be NULL. Cliprects are given in source coordinates.
1835 * @dest_x: X coordinate offset for the crtc / destination clip rects.
1836 * @dest_y: Y coordinate offset for the crtc / destination clip rects.
1837 * @num_clips: Number of cliprects in the @clips or @vclips array.
1838 * @increment: Integer with which to increment the clip counter when looping.
1839 * Used to skip a predetermined number of clip rects.
1840 * @dirty: Closure structure. See the description of struct vmw_kms_dirty.
1841 */
1842int vmw_kms_helper_dirty(struct vmw_private *dev_priv,
1843 struct vmw_framebuffer *framebuffer,
1844 const struct drm_clip_rect *clips,
1845 const struct drm_vmw_rect *vclips,
1846 s32 dest_x, s32 dest_y,
1847 int num_clips,
1848 int increment,
1849 struct vmw_kms_dirty *dirty)
1850{
1851 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
1852 struct drm_crtc *crtc;
1853 u32 num_units = 0;
1854 u32 i, k;
1855 int ret;
1856
1857 dirty->dev_priv = dev_priv;
1858
1859 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
1860 if (crtc->primary->fb != &framebuffer->base)
1861 continue;
1862 units[num_units++] = vmw_crtc_to_du(crtc);
1863 }
1864
1865 for (k = 0; k < num_units; k++) {
1866 struct vmw_display_unit *unit = units[k];
1867 s32 crtc_x = unit->crtc.x;
1868 s32 crtc_y = unit->crtc.y;
1869 s32 crtc_width = unit->crtc.mode.hdisplay;
1870 s32 crtc_height = unit->crtc.mode.vdisplay;
1871 const struct drm_clip_rect *clips_ptr = clips;
1872 const struct drm_vmw_rect *vclips_ptr = vclips;
1873
1874 dirty->unit = unit;
1875 if (dirty->fifo_reserve_size > 0) {
1876 dirty->cmd = vmw_fifo_reserve(dev_priv,
1877 dirty->fifo_reserve_size);
1878 if (!dirty->cmd) {
1879 DRM_ERROR("Couldn't reserve fifo space "
1880 "for dirty blits.\n");
1881 return ret;
1882 }
1883 memset(dirty->cmd, 0, dirty->fifo_reserve_size);
1884 }
1885 dirty->num_hits = 0;
1886 for (i = 0; i < num_clips; i++, clips_ptr += increment,
1887 vclips_ptr += increment) {
1888 s32 clip_left;
1889 s32 clip_top;
1890
1891 /*
1892 * Select clip array type. Note that integer type
1893 * in @clips is unsigned short, whereas in @vclips
1894 * it's 32-bit.
1895 */
1896 if (clips) {
1897 dirty->fb_x = (s32) clips_ptr->x1;
1898 dirty->fb_y = (s32) clips_ptr->y1;
1899 dirty->unit_x2 = (s32) clips_ptr->x2 + dest_x -
1900 crtc_x;
1901 dirty->unit_y2 = (s32) clips_ptr->y2 + dest_y -
1902 crtc_y;
1903 } else {
1904 dirty->fb_x = vclips_ptr->x;
1905 dirty->fb_y = vclips_ptr->y;
1906 dirty->unit_x2 = dirty->fb_x + vclips_ptr->w +
1907 dest_x - crtc_x;
1908 dirty->unit_y2 = dirty->fb_y + vclips_ptr->h +
1909 dest_y - crtc_y;
1910 }
1911
1912 dirty->unit_x1 = dirty->fb_x + dest_x - crtc_x;
1913 dirty->unit_y1 = dirty->fb_y + dest_y - crtc_y;
1914
1915 /* Skip this clip if it's outside the crtc region */
1916 if (dirty->unit_x1 >= crtc_width ||
1917 dirty->unit_y1 >= crtc_height ||
1918 dirty->unit_x2 <= 0 || dirty->unit_y2 <= 0)
1919 continue;
1920
1921 /* Clip right and bottom to crtc limits */
1922 dirty->unit_x2 = min_t(s32, dirty->unit_x2,
1923 crtc_width);
1924 dirty->unit_y2 = min_t(s32, dirty->unit_y2,
1925 crtc_height);
1926
1927 /* Clip left and top to crtc limits */
1928 clip_left = min_t(s32, dirty->unit_x1, 0);
1929 clip_top = min_t(s32, dirty->unit_y1, 0);
1930 dirty->unit_x1 -= clip_left;
1931 dirty->unit_y1 -= clip_top;
1932 dirty->fb_x -= clip_left;
1933 dirty->fb_y -= clip_top;
1934
1935 dirty->clip(dirty);
1936 }
1937
1938 dirty->fifo_commit(dirty);
1939 }
1940
1941 return 0;
1942}
1943
1944/**
1945 * vmw_kms_helper_buffer_prepare - Reserve and validate a buffer object before
1946 * command submission.
1947 *
1948 * @dev_priv. Pointer to a device private structure.
1949 * @buf: The buffer object
1950 * @interruptible: Whether to perform waits as interruptible.
1951 * @validate_as_mob: Whether the buffer should be validated as a MOB. If false,
1952 * The buffer will be validated as a GMR. Already pinned buffers will not be
1953 * validated.
1954 *
1955 * Returns 0 on success, negative error code on failure, -ERESTARTSYS if
1956 * interrupted by a signal.
1957 */
1958int vmw_kms_helper_buffer_prepare(struct vmw_private *dev_priv,
1959 struct vmw_dma_buffer *buf,
1960 bool interruptible,
1961 bool validate_as_mob)
1962{
1963 struct ttm_buffer_object *bo = &buf->base;
1964 int ret;
1965
1966 ttm_bo_reserve(bo, false, false, interruptible, 0);
1967 ret = vmw_validate_single_buffer(dev_priv, bo, interruptible,
1968 validate_as_mob);
1969 if (ret)
1970 ttm_bo_unreserve(bo);
1971
1972 return ret;
1973}
1974
1975/**
1976 * vmw_kms_helper_buffer_revert - Undo the actions of
1977 * vmw_kms_helper_buffer_prepare.
1978 *
1979 * @res: Pointer to the buffer object.
1980 *
1981 * Helper to be used if an error forces the caller to undo the actions of
1982 * vmw_kms_helper_buffer_prepare.
1983 */
1984void vmw_kms_helper_buffer_revert(struct vmw_dma_buffer *buf)
1985{
1986 if (buf)
1987 ttm_bo_unreserve(&buf->base);
1988}
1989
1990/**
1991 * vmw_kms_helper_buffer_finish - Unreserve and fence a buffer object after
1992 * kms command submission.
1993 *
1994 * @dev_priv: Pointer to a device private structure.
1995 * @file_priv: Pointer to a struct drm_file representing the caller's
1996 * connection. Must be set to NULL if @user_fence_rep is NULL, and conversely
1997 * if non-NULL, @user_fence_rep must be non-NULL.
1998 * @buf: The buffer object.
1999 * @out_fence: Optional pointer to a fence pointer. If non-NULL, a
2000 * ref-counted fence pointer is returned here.
2001 * @user_fence_rep: Optional pointer to a user-space provided struct
2002 * drm_vmw_fence_rep. If provided, @file_priv must also be provided and the
2003 * function copies fence data to user-space in a fail-safe manner.
2004 */
2005void vmw_kms_helper_buffer_finish(struct vmw_private *dev_priv,
2006 struct drm_file *file_priv,
2007 struct vmw_dma_buffer *buf,
2008 struct vmw_fence_obj **out_fence,
2009 struct drm_vmw_fence_rep __user *
2010 user_fence_rep)
2011{
2012 struct vmw_fence_obj *fence;
2013 uint32_t handle;
2014 int ret;
2015
2016 ret = vmw_execbuf_fence_commands(file_priv, dev_priv, &fence,
2017 file_priv ? &handle : NULL);
2018 if (buf)
2019 vmw_fence_single_bo(&buf->base, fence);
2020 if (file_priv)
2021 vmw_execbuf_copy_fence_user(dev_priv, vmw_fpriv(file_priv),
2022 ret, user_fence_rep, fence,
2023 handle);
2024 if (out_fence)
2025 *out_fence = fence;
2026 else
2027 vmw_fence_obj_unreference(&fence);
2028
2029 vmw_kms_helper_buffer_revert(buf);
2030}
2031
2032
2033/**
2034 * vmw_kms_helper_resource_revert - Undo the actions of
2035 * vmw_kms_helper_resource_prepare.
2036 *
2037 * @res: Pointer to the resource. Typically a surface.
2038 *
2039 * Helper to be used if an error forces the caller to undo the actions of
2040 * vmw_kms_helper_resource_prepare.
2041 */
2042void vmw_kms_helper_resource_revert(struct vmw_resource *res)
2043{
2044 vmw_kms_helper_buffer_revert(res->backup);
2045 vmw_resource_unreserve(res, NULL, 0);
2046 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
2047}
2048
2049/**
2050 * vmw_kms_helper_resource_prepare - Reserve and validate a resource before
2051 * command submission.
2052 *
2053 * @res: Pointer to the resource. Typically a surface.
2054 * @interruptible: Whether to perform waits as interruptible.
2055 *
2056 * Reserves and validates also the backup buffer if a guest-backed resource.
2057 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
2058 * interrupted by a signal.
2059 */
2060int vmw_kms_helper_resource_prepare(struct vmw_resource *res,
2061 bool interruptible)
2062{
2063 int ret = 0;
2064
2065 if (interruptible)
2066 ret = mutex_lock_interruptible(&res->dev_priv->cmdbuf_mutex);
2067 else
2068 mutex_lock(&res->dev_priv->cmdbuf_mutex);
2069
2070 if (unlikely(ret != 0))
2071 return -ERESTARTSYS;
2072
2073 ret = vmw_resource_reserve(res, interruptible, false);
2074 if (ret)
2075 goto out_unlock;
2076
2077 if (res->backup) {
2078 ret = vmw_kms_helper_buffer_prepare(res->dev_priv, res->backup,
2079 interruptible,
2080 res->dev_priv->has_mob);
2081 if (ret)
2082 goto out_unreserve;
2083 }
2084 ret = vmw_resource_validate(res);
2085 if (ret)
2086 goto out_revert;
2087 return 0;
2088
2089out_revert:
2090 vmw_kms_helper_buffer_revert(res->backup);
2091out_unreserve:
2092 vmw_resource_unreserve(res, NULL, 0);
2093out_unlock:
2094 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
2095 return ret;
2096}
2097
2098/**
2099 * vmw_kms_helper_resource_finish - Unreserve and fence a resource after
2100 * kms command submission.
2101 *
2102 * @res: Pointer to the resource. Typically a surface.
2103 * @out_fence: Optional pointer to a fence pointer. If non-NULL, a
2104 * ref-counted fence pointer is returned here.
2105 */
2106void vmw_kms_helper_resource_finish(struct vmw_resource *res,
2107 struct vmw_fence_obj **out_fence)
2108{
2109 if (res->backup || out_fence)
2110 vmw_kms_helper_buffer_finish(res->dev_priv, NULL, res->backup,
2111 out_fence, NULL);
2112
2113 vmw_resource_unreserve(res, NULL, 0);
2114 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
2115}