blob: 7566a5a14004435255408d7b9d635fff928a1192 [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);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000466
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700467 vmw_fifo_flush(dev_priv, false);
Thomas Hellstrom294adf72014-02-27 12:34:51 +0100468 ttm_read_unlock(&dev_priv->reservation_sem);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200469
470 drm_modeset_unlock_all(dev_priv->dev);
471
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000472 return 0;
473}
474
475static struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs = {
476 .destroy = vmw_framebuffer_surface_destroy,
477 .dirty = vmw_framebuffer_surface_dirty,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000478};
479
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200480static int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv,
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200481 struct drm_file *file_priv,
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200482 struct vmw_surface *surface,
483 struct vmw_framebuffer **out,
484 const struct drm_mode_fb_cmd
485 *mode_cmd)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000486
487{
488 struct drm_device *dev = dev_priv->dev;
489 struct vmw_framebuffer_surface *vfbs;
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200490 enum SVGA3dSurfaceFormat format;
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200491 struct vmw_master *vmaster = vmw_master(file_priv->master);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000492 int ret;
493
Sinclair Yehc8261a92015-06-26 01:23:42 -0700494 /* 3D is only supported on HWv8 and newer hosts */
495 if (dev_priv->active_display_unit == vmw_du_legacy)
Jakob Bornecrantz01e81412011-10-04 20:13:24 +0200496 return -ENOSYS;
497
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200498 /*
499 * Sanity checks.
500 */
501
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100502 /* Surface must be marked as a scanout. */
503 if (unlikely(!surface->scanout))
504 return -EINVAL;
505
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200506 if (unlikely(surface->mip_levels[0] != 1 ||
507 surface->num_sizes != 1 ||
Thomas Hellstromb360a3c2014-01-15 08:51:36 +0100508 surface->base_size.width < mode_cmd->width ||
509 surface->base_size.height < mode_cmd->height ||
510 surface->base_size.depth != 1)) {
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200511 DRM_ERROR("Incompatible surface dimensions "
512 "for requested mode.\n");
513 return -EINVAL;
514 }
515
516 switch (mode_cmd->depth) {
517 case 32:
518 format = SVGA3D_A8R8G8B8;
519 break;
520 case 24:
521 format = SVGA3D_X8R8G8B8;
522 break;
523 case 16:
524 format = SVGA3D_R5G6B5;
525 break;
526 case 15:
527 format = SVGA3D_A1R5G5B5;
528 break;
529 default:
530 DRM_ERROR("Invalid color depth: %d\n", mode_cmd->depth);
531 return -EINVAL;
532 }
533
534 if (unlikely(format != surface->format)) {
535 DRM_ERROR("Invalid surface format for requested mode.\n");
536 return -EINVAL;
537 }
538
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000539 vfbs = kzalloc(sizeof(*vfbs), GFP_KERNEL);
540 if (!vfbs) {
541 ret = -ENOMEM;
542 goto out_err1;
543 }
544
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000545 if (!vmw_surface_reference(surface)) {
546 DRM_ERROR("failed to reference surface %p\n", surface);
Daniel Vetter80f0b5a2012-12-13 23:39:01 +0100547 ret = -EINVAL;
548 goto out_err2;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000549 }
550
551 /* XXX get the first 3 from the surface info */
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200552 vfbs->base.base.bits_per_pixel = mode_cmd->bpp;
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200553 vfbs->base.base.pitches[0] = mode_cmd->pitch;
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200554 vfbs->base.base.depth = mode_cmd->depth;
555 vfbs->base.base.width = mode_cmd->width;
556 vfbs->base.base.height = mode_cmd->height;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000557 vfbs->surface = surface;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200558 vfbs->base.user_handle = mode_cmd->handle;
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200559 vfbs->master = drm_master_get(file_priv->master);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200560
561 mutex_lock(&vmaster->fb_surf_mutex);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200562 list_add_tail(&vfbs->head, &vmaster->fb_surf);
563 mutex_unlock(&vmaster->fb_surf_mutex);
564
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000565 *out = &vfbs->base;
566
Daniel Vetter80f0b5a2012-12-13 23:39:01 +0100567 ret = drm_framebuffer_init(dev, &vfbs->base.base,
568 &vmw_framebuffer_surface_funcs);
569 if (ret)
570 goto out_err3;
571
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000572 return 0;
573
574out_err3:
Daniel Vetter80f0b5a2012-12-13 23:39:01 +0100575 vmw_surface_unreference(&surface);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000576out_err2:
577 kfree(vfbs);
578out_err1:
579 return ret;
580}
581
582/*
583 * Dmabuf framebuffer code
584 */
585
Rashika Kheria847c5962014-01-06 22:18:10 +0530586static void vmw_framebuffer_dmabuf_destroy(struct drm_framebuffer *framebuffer)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000587{
588 struct vmw_framebuffer_dmabuf *vfbd =
589 vmw_framebuffer_to_vfbd(framebuffer);
590
591 drm_framebuffer_cleanup(framebuffer);
592 vmw_dmabuf_unreference(&vfbd->buffer);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200593 ttm_base_object_unref(&vfbd->base.user_obj);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000594
595 kfree(vfbd);
596}
597
Rashika Kheria847c5962014-01-06 22:18:10 +0530598static int vmw_framebuffer_dmabuf_dirty(struct drm_framebuffer *framebuffer,
Thomas Hellstrom02b00162010-10-05 12:43:02 +0200599 struct drm_file *file_priv,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000600 unsigned flags, unsigned color,
601 struct drm_clip_rect *clips,
602 unsigned num_clips)
603{
604 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200605 struct vmw_framebuffer_dmabuf *vfbd =
606 vmw_framebuffer_to_vfbd(framebuffer);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000607 struct drm_clip_rect norect;
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200608 int ret, increment = 1;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000609
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200610 drm_modeset_lock_all(dev_priv->dev);
611
Thomas Hellstrom294adf72014-02-27 12:34:51 +0100612 ret = ttm_read_lock(&dev_priv->reservation_sem, true);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200613 if (unlikely(ret != 0)) {
614 drm_modeset_unlock_all(dev_priv->dev);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200615 return ret;
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200616 }
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200617
Thomas Hellstromdf1c93b2010-01-13 22:28:36 +0100618 if (!num_clips) {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000619 num_clips = 1;
620 clips = &norect;
621 norect.x1 = norect.y1 = 0;
622 norect.x2 = framebuffer->width;
623 norect.y2 = framebuffer->height;
624 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
625 num_clips /= 2;
626 increment = 2;
627 }
628
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200629 if (dev_priv->ldu_priv) {
Sinclair Yehc8261a92015-06-26 01:23:42 -0700630 ret = vmw_kms_ldu_do_dmabuf_dirty(dev_priv, &vfbd->base,
631 flags, color,
632 clips, num_clips, increment);
633 } else if (dev_priv->active_display_unit == vmw_du_screen_object) {
634 ret = vmw_kms_sou_do_dmabuf_dirty(file_priv, dev_priv,
635 &vfbd->base,
636 flags, color,
637 clips, num_clips, increment,
638 NULL);
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200639 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000640
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700641 vmw_fifo_flush(dev_priv, false);
Thomas Hellstrom294adf72014-02-27 12:34:51 +0100642 ttm_read_unlock(&dev_priv->reservation_sem);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200643
644 drm_modeset_unlock_all(dev_priv->dev);
645
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200646 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000647}
648
649static struct drm_framebuffer_funcs vmw_framebuffer_dmabuf_funcs = {
650 .destroy = vmw_framebuffer_dmabuf_destroy,
651 .dirty = vmw_framebuffer_dmabuf_dirty,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000652};
653
Jakob Bornecrantz497a3ff2011-10-04 20:13:14 +0200654/**
Jakob Bornecrantz497a3ff2011-10-04 20:13:14 +0200655 * Pin the dmabuffer to the start of vram.
656 */
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000657static int vmw_framebuffer_dmabuf_pin(struct vmw_framebuffer *vfb)
658{
659 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
660 struct vmw_framebuffer_dmabuf *vfbd =
661 vmw_framebuffer_to_vfbd(&vfb->base);
662 int ret;
663
Sinclair Yehc8261a92015-06-26 01:23:42 -0700664 /* This code should only be used with Legacy Display Unit */
665 BUG_ON(dev_priv->active_display_unit != vmw_du_legacy);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +0200666
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000667 vmw_overlay_pause_all(dev_priv);
668
Jakob Bornecrantzd991ef02011-10-04 20:13:21 +0200669 ret = vmw_dmabuf_to_start_of_vram(dev_priv, vfbd->buffer, true, false);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000670
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000671 vmw_overlay_resume_all(dev_priv);
672
Jakob Bornecrantz316ab132010-05-28 11:22:05 +0200673 WARN_ON(ret != 0);
674
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000675 return 0;
676}
677
678static int vmw_framebuffer_dmabuf_unpin(struct vmw_framebuffer *vfb)
679{
680 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
681 struct vmw_framebuffer_dmabuf *vfbd =
682 vmw_framebuffer_to_vfbd(&vfb->base);
683
684 if (!vfbd->buffer) {
685 WARN_ON(!vfbd->buffer);
686 return 0;
687 }
688
Jakob Bornecrantzd991ef02011-10-04 20:13:21 +0200689 return vmw_dmabuf_unpin(dev_priv, vfbd->buffer, false);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000690}
691
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200692static int vmw_kms_new_framebuffer_dmabuf(struct vmw_private *dev_priv,
693 struct vmw_dma_buffer *dmabuf,
694 struct vmw_framebuffer **out,
695 const struct drm_mode_fb_cmd
696 *mode_cmd)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000697
698{
699 struct drm_device *dev = dev_priv->dev;
700 struct vmw_framebuffer_dmabuf *vfbd;
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200701 unsigned int requested_size;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000702 int ret;
703
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200704 requested_size = mode_cmd->height * mode_cmd->pitch;
705 if (unlikely(requested_size > dmabuf->base.num_pages * PAGE_SIZE)) {
706 DRM_ERROR("Screen buffer object size is too small "
707 "for requested mode.\n");
708 return -EINVAL;
709 }
710
Jakob Bornecrantzc337ada2011-10-04 20:13:34 +0200711 /* Limited framebuffer color depth support for screen objects */
Sinclair Yehc8261a92015-06-26 01:23:42 -0700712 if (dev_priv->active_display_unit == vmw_du_screen_object) {
Jakob Bornecrantzc337ada2011-10-04 20:13:34 +0200713 switch (mode_cmd->depth) {
714 case 32:
715 case 24:
716 /* Only support 32 bpp for 32 and 24 depth fbs */
717 if (mode_cmd->bpp == 32)
718 break;
719
720 DRM_ERROR("Invalid color depth/bbp: %d %d\n",
721 mode_cmd->depth, mode_cmd->bpp);
722 return -EINVAL;
723 case 16:
724 case 15:
725 /* Only support 16 bpp for 16 and 15 depth fbs */
726 if (mode_cmd->bpp == 16)
727 break;
728
729 DRM_ERROR("Invalid color depth/bbp: %d %d\n",
730 mode_cmd->depth, mode_cmd->bpp);
731 return -EINVAL;
732 default:
733 DRM_ERROR("Invalid color depth: %d\n", mode_cmd->depth);
734 return -EINVAL;
735 }
736 }
737
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000738 vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL);
739 if (!vfbd) {
740 ret = -ENOMEM;
741 goto out_err1;
742 }
743
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000744 if (!vmw_dmabuf_reference(dmabuf)) {
745 DRM_ERROR("failed to reference dmabuf %p\n", dmabuf);
Daniel Vetter80f0b5a2012-12-13 23:39:01 +0100746 ret = -EINVAL;
747 goto out_err2;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000748 }
749
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200750 vfbd->base.base.bits_per_pixel = mode_cmd->bpp;
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200751 vfbd->base.base.pitches[0] = mode_cmd->pitch;
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200752 vfbd->base.base.depth = mode_cmd->depth;
753 vfbd->base.base.width = mode_cmd->width;
754 vfbd->base.base.height = mode_cmd->height;
Sinclair Yehc8261a92015-06-26 01:23:42 -0700755 if (dev_priv->active_display_unit == vmw_du_legacy) {
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200756 vfbd->base.pin = vmw_framebuffer_dmabuf_pin;
757 vfbd->base.unpin = vmw_framebuffer_dmabuf_unpin;
758 }
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200759 vfbd->base.dmabuf = true;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000760 vfbd->buffer = dmabuf;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200761 vfbd->base.user_handle = mode_cmd->handle;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000762 *out = &vfbd->base;
763
Daniel Vetter80f0b5a2012-12-13 23:39:01 +0100764 ret = drm_framebuffer_init(dev, &vfbd->base.base,
765 &vmw_framebuffer_dmabuf_funcs);
766 if (ret)
767 goto out_err3;
768
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000769 return 0;
770
771out_err3:
Daniel Vetter80f0b5a2012-12-13 23:39:01 +0100772 vmw_dmabuf_unreference(&dmabuf);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000773out_err2:
774 kfree(vfbd);
775out_err1:
776 return ret;
777}
778
779/*
780 * Generic Kernel modesetting functions
781 */
782
783static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev,
784 struct drm_file *file_priv,
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800785 struct drm_mode_fb_cmd2 *mode_cmd2)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000786{
787 struct vmw_private *dev_priv = vmw_priv(dev);
788 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
789 struct vmw_framebuffer *vfb = NULL;
790 struct vmw_surface *surface = NULL;
791 struct vmw_dma_buffer *bo = NULL;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200792 struct ttm_base_object *user_obj;
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800793 struct drm_mode_fb_cmd mode_cmd;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000794 int ret;
795
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800796 mode_cmd.width = mode_cmd2->width;
797 mode_cmd.height = mode_cmd2->height;
798 mode_cmd.pitch = mode_cmd2->pitches[0];
799 mode_cmd.handle = mode_cmd2->handles[0];
Dave Airlie248dbc22011-11-29 20:02:54 +0000800 drm_fb_get_bpp_depth(mode_cmd2->pixel_format, &mode_cmd.depth,
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800801 &mode_cmd.bpp);
802
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200803 /**
804 * This code should be conditioned on Screen Objects not being used.
805 * If screen objects are used, we can allocate a GMR to hold the
806 * requested framebuffer.
807 */
808
Xi Wang8a783892011-12-21 05:18:33 -0500809 if (!vmw_kms_validate_mode_vram(dev_priv,
Linus Torvalds1a464cb2012-01-10 11:04:36 -0800810 mode_cmd.pitch,
811 mode_cmd.height)) {
Sinclair Yehc8261a92015-06-26 01:23:42 -0700812 DRM_ERROR("Requested mode exceed bounding box limit.\n");
Jakob Bornecrantzd9826402011-11-03 21:03:04 +0100813 return ERR_PTR(-ENOMEM);
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200814 }
815
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200816 /*
817 * Take a reference on the user object of the resource
818 * backing the kms fb. This ensures that user-space handle
819 * lookups on that resource will always work as long as
820 * it's registered with a kms framebuffer. This is important,
821 * since vmw_execbuf_process identifies resources in the
822 * command stream using user-space handles.
823 */
824
Jesse Barnes308e5bc2011-11-14 14:51:28 -0800825 user_obj = ttm_base_object_lookup(tfile, mode_cmd.handle);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200826 if (unlikely(user_obj == NULL)) {
827 DRM_ERROR("Could not locate requested kms frame buffer.\n");
828 return ERR_PTR(-ENOENT);
829 }
830
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200831 /**
832 * End conditioned code.
833 */
834
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100835 /* returns either a dmabuf or surface */
836 ret = vmw_user_lookup_handle(dev_priv, tfile,
Dave Airlie4cf73122011-12-21 09:50:56 +0000837 mode_cmd.handle,
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100838 &surface, &bo);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000839 if (ret)
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100840 goto err_out;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000841
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100842 /* Create the new framebuffer depending one what we got back */
843 if (bo)
844 ret = vmw_kms_new_framebuffer_dmabuf(dev_priv, bo, &vfb,
Dave Airlie4cf73122011-12-21 09:50:56 +0000845 &mode_cmd);
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100846 else if (surface)
847 ret = vmw_kms_new_framebuffer_surface(dev_priv, file_priv,
Dave Airlie4cf73122011-12-21 09:50:56 +0000848 surface, &vfb, &mode_cmd);
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100849 else
850 BUG();
Jakob Bornecrantz5ffdb652010-01-30 03:38:08 +0000851
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100852err_out:
853 /* vmw_user_lookup_handle takes one ref so does new_fb */
854 if (bo)
855 vmw_dmabuf_unreference(&bo);
856 if (surface)
857 vmw_surface_unreference(&surface);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000858
859 if (ret) {
860 DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200861 ttm_base_object_unref(&user_obj);
Chris Wilsoncce13ff2010-08-08 13:36:38 +0100862 return ERR_PTR(ret);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200863 } else
864 vfb->user_obj = user_obj;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000865
866 return &vfb->base;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000867}
868
Laurent Pincharte6ecefa2012-05-17 13:27:23 +0200869static const struct drm_mode_config_funcs vmw_kms_funcs = {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000870 .fb_create = vmw_kms_fb_create,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000871};
872
Sinclair Yehc8261a92015-06-26 01:23:42 -0700873int vmw_kms_generic_present(struct vmw_private *dev_priv,
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200874 struct drm_file *file_priv,
875 struct vmw_framebuffer *vfb,
876 struct vmw_surface *surface,
877 uint32_t sid,
878 int32_t destX, int32_t destY,
879 struct drm_vmw_rect *clips,
880 uint32_t num_clips)
881{
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200882 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100883 struct drm_clip_rect *tmp;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200884 struct drm_crtc *crtc;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200885 size_t fifo_size;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200886 int i, k, num_units;
887 int ret = 0; /* silence warning */
Jakob Bornecrantz203dc222011-11-28 13:19:13 +0100888 int left, right, top, bottom;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200889
890 struct {
891 SVGA3dCmdHeader header;
892 SVGA3dCmdBlitSurfaceToScreen body;
893 } *cmd;
894 SVGASignedRect *blits;
895
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200896 num_units = 0;
897 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
Matt Roperf4510a22014-04-01 15:22:40 -0700898 if (crtc->primary->fb != &vfb->base)
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200899 continue;
900 units[num_units++] = vmw_crtc_to_du(crtc);
901 }
902
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200903 BUG_ON(surface == NULL);
904 BUG_ON(!clips || !num_clips);
905
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100906 tmp = kzalloc(sizeof(*tmp) * num_clips, GFP_KERNEL);
907 if (unlikely(tmp == NULL)) {
908 DRM_ERROR("Temporary cliprect memory alloc failed.\n");
909 return -ENOMEM;
910 }
911
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200912 fifo_size = sizeof(*cmd) + sizeof(SVGASignedRect) * num_clips;
913 cmd = kmalloc(fifo_size, GFP_KERNEL);
914 if (unlikely(cmd == NULL)) {
915 DRM_ERROR("Failed to allocate temporary fifo memory.\n");
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100916 ret = -ENOMEM;
917 goto out_free_tmp;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200918 }
919
Jakob Bornecrantz203dc222011-11-28 13:19:13 +0100920 left = clips->x;
921 right = clips->x + clips->w;
922 top = clips->y;
923 bottom = clips->y + clips->h;
924
925 for (i = 1; i < num_clips; i++) {
926 left = min_t(int, left, (int)clips[i].x);
927 right = max_t(int, right, (int)clips[i].x + clips[i].w);
928 top = min_t(int, top, (int)clips[i].y);
929 bottom = max_t(int, bottom, (int)clips[i].y + clips[i].h);
930 }
931
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200932 /* only need to do this once */
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200933 memset(cmd, 0, fifo_size);
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200934 cmd->header.id = cpu_to_le32(SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN);
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100935
936 blits = (SVGASignedRect *)&cmd[1];
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200937
Jakob Bornecrantz203dc222011-11-28 13:19:13 +0100938 cmd->body.srcRect.left = left;
939 cmd->body.srcRect.right = right;
940 cmd->body.srcRect.top = top;
941 cmd->body.srcRect.bottom = bottom;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200942
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200943 for (i = 0; i < num_clips; i++) {
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100944 tmp[i].x1 = clips[i].x - left;
945 tmp[i].x2 = clips[i].x + clips[i].w - left;
946 tmp[i].y1 = clips[i].y - top;
947 tmp[i].y2 = clips[i].y + clips[i].h - top;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200948 }
949
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200950 for (k = 0; k < num_units; k++) {
951 struct vmw_display_unit *unit = units[k];
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100952 struct vmw_clip_rect clip;
953 int num;
954
955 clip.x1 = left + destX - unit->crtc.x;
956 clip.y1 = top + destY - unit->crtc.y;
957 clip.x2 = right + destX - unit->crtc.x;
958 clip.y2 = bottom + destY - unit->crtc.y;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200959
960 /* skip any crtcs that misses the clip region */
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100961 if (clip.x1 >= unit->crtc.mode.hdisplay ||
962 clip.y1 >= unit->crtc.mode.vdisplay ||
963 clip.x2 <= 0 || clip.y2 <= 0)
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200964 continue;
965
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100966 /*
967 * In order for the clip rects to be correctly scaled
968 * the src and dest rects needs to be the same size.
969 */
970 cmd->body.destRect.left = clip.x1;
971 cmd->body.destRect.right = clip.x2;
972 cmd->body.destRect.top = clip.y1;
973 cmd->body.destRect.bottom = clip.y2;
974
975 /* create a clip rect of the crtc in dest coords */
976 clip.x2 = unit->crtc.mode.hdisplay - clip.x1;
977 clip.y2 = unit->crtc.mode.vdisplay - clip.y1;
978 clip.x1 = 0 - clip.x1;
979 clip.y1 = 0 - clip.y1;
980
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200981 /* need to reset sid as it is changed by execbuf */
982 cmd->body.srcImage.sid = sid;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200983 cmd->body.destScreenId = unit->unit;
984
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100985 /* clip and write blits to cmd stream */
986 vmw_clip_cliprects(tmp, num_clips, clip, blits, &num);
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200987
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100988 /* if no cliprects hit skip this */
989 if (num == 0)
990 continue;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200991
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100992 /* recalculate package length */
993 fifo_size = sizeof(*cmd) + sizeof(SVGASignedRect) * num;
994 cmd->header.size = cpu_to_le32(fifo_size - sizeof(cmd->header));
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200995 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd,
Thomas Hellstromc9146cd2015-03-02 23:45:04 -0800996 fifo_size, 0, 0, NULL, NULL);
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200997
998 if (unlikely(ret != 0))
999 break;
1000 }
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001001
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07001002 vmw_fifo_flush(dev_priv, false);
1003
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001004 kfree(cmd);
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +01001005out_free_tmp:
1006 kfree(tmp);
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001007
1008 return ret;
1009}
1010
Sinclair Yehc8261a92015-06-26 01:23:42 -07001011int vmw_kms_present(struct vmw_private *dev_priv,
1012 struct drm_file *file_priv,
1013 struct vmw_framebuffer *vfb,
1014 struct vmw_surface *surface,
1015 uint32_t sid,
1016 int32_t destX, int32_t destY,
1017 struct drm_vmw_rect *clips,
1018 uint32_t num_clips)
1019{
1020 return vmw_kms_generic_present(dev_priv, file_priv, vfb, surface, sid,
1021 destX, destY, clips, num_clips);
1022}
1023
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001024int vmw_kms_readback(struct vmw_private *dev_priv,
1025 struct drm_file *file_priv,
1026 struct vmw_framebuffer *vfb,
1027 struct drm_vmw_fence_rep __user *user_fence_rep,
1028 struct drm_vmw_rect *clips,
1029 uint32_t num_clips)
1030{
1031 struct vmw_framebuffer_dmabuf *vfbd =
1032 vmw_framebuffer_to_vfbd(&vfb->base);
1033 struct vmw_dma_buffer *dmabuf = vfbd->buffer;
1034 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
1035 struct drm_crtc *crtc;
1036 size_t fifo_size;
1037 int i, k, ret, num_units, blits_pos;
1038
1039 struct {
1040 uint32_t header;
1041 SVGAFifoCmdDefineGMRFB body;
1042 } *cmd;
1043 struct {
1044 uint32_t header;
1045 SVGAFifoCmdBlitScreenToGMRFB body;
1046 } *blits;
1047
1048 num_units = 0;
1049 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
Matt Roperf4510a22014-04-01 15:22:40 -07001050 if (crtc->primary->fb != &vfb->base)
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001051 continue;
1052 units[num_units++] = vmw_crtc_to_du(crtc);
1053 }
1054
1055 BUG_ON(dmabuf == NULL);
1056 BUG_ON(!clips || !num_clips);
1057
1058 /* take a safe guess at fifo size */
1059 fifo_size = sizeof(*cmd) + sizeof(*blits) * num_clips * num_units;
1060 cmd = kmalloc(fifo_size, GFP_KERNEL);
1061 if (unlikely(cmd == NULL)) {
1062 DRM_ERROR("Failed to allocate temporary fifo memory.\n");
1063 return -ENOMEM;
1064 }
1065
1066 memset(cmd, 0, fifo_size);
1067 cmd->header = SVGA_CMD_DEFINE_GMRFB;
1068 cmd->body.format.bitsPerPixel = vfb->base.bits_per_pixel;
1069 cmd->body.format.colorDepth = vfb->base.depth;
1070 cmd->body.format.reserved = 0;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02001071 cmd->body.bytesPerLine = vfb->base.pitches[0];
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001072 cmd->body.ptr.gmrId = vfb->user_handle;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001073 cmd->body.ptr.offset = 0;
1074
1075 blits = (void *)&cmd[1];
1076 blits_pos = 0;
1077 for (i = 0; i < num_units; i++) {
1078 struct drm_vmw_rect *c = clips;
1079 for (k = 0; k < num_clips; k++, c++) {
1080 /* transform clip coords to crtc origin based coords */
1081 int clip_x1 = c->x - units[i]->crtc.x;
1082 int clip_x2 = c->x - units[i]->crtc.x + c->w;
1083 int clip_y1 = c->y - units[i]->crtc.y;
1084 int clip_y2 = c->y - units[i]->crtc.y + c->h;
1085 int dest_x = c->x;
1086 int dest_y = c->y;
1087
1088 /* compensate for clipping, we negate
1089 * a negative number and add that.
1090 */
1091 if (clip_x1 < 0)
1092 dest_x += -clip_x1;
1093 if (clip_y1 < 0)
1094 dest_y += -clip_y1;
1095
1096 /* clip */
1097 clip_x1 = max(clip_x1, 0);
1098 clip_y1 = max(clip_y1, 0);
1099 clip_x2 = min(clip_x2, units[i]->crtc.mode.hdisplay);
1100 clip_y2 = min(clip_y2, units[i]->crtc.mode.vdisplay);
1101
1102 /* and cull any rects that misses the crtc */
1103 if (clip_x1 >= units[i]->crtc.mode.hdisplay ||
1104 clip_y1 >= units[i]->crtc.mode.vdisplay ||
1105 clip_x2 <= 0 || clip_y2 <= 0)
1106 continue;
1107
1108 blits[blits_pos].header = SVGA_CMD_BLIT_SCREEN_TO_GMRFB;
1109 blits[blits_pos].body.srcScreenId = units[i]->unit;
1110 blits[blits_pos].body.destOrigin.x = dest_x;
1111 blits[blits_pos].body.destOrigin.y = dest_y;
1112
1113 blits[blits_pos].body.srcRect.left = clip_x1;
1114 blits[blits_pos].body.srcRect.top = clip_y1;
1115 blits[blits_pos].body.srcRect.right = clip_x2;
1116 blits[blits_pos].body.srcRect.bottom = clip_y2;
1117 blits_pos++;
1118 }
1119 }
1120 /* reset size here and use calculated exact size from loops */
1121 fifo_size = sizeof(*cmd) + sizeof(*blits) * blits_pos;
1122
1123 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd, fifo_size,
Thomas Hellstromc9146cd2015-03-02 23:45:04 -08001124 0, 0, user_fence_rep, NULL);
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001125
1126 kfree(cmd);
1127
1128 return ret;
1129}
1130
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001131int vmw_kms_init(struct vmw_private *dev_priv)
1132{
1133 struct drm_device *dev = dev_priv->dev;
1134 int ret;
1135
1136 drm_mode_config_init(dev);
1137 dev->mode_config.funcs = &vmw_kms_funcs;
Jakob Bornecrantz3bef3572010-02-09 19:41:57 +00001138 dev->mode_config.min_width = 1;
1139 dev->mode_config.min_height = 1;
Jakob Bornecrantz7e71f8a2010-05-28 11:21:54 +02001140 /* assumed largest fb size */
1141 dev->mode_config.max_width = 8192;
1142 dev->mode_config.max_height = 8192;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001143
Sinclair Yehc8261a92015-06-26 01:23:42 -07001144 ret = vmw_kms_sou_init_display(dev_priv);
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +02001145 if (ret) /* Fallback */
Sinclair Yehc8261a92015-06-26 01:23:42 -07001146 ret = vmw_kms_ldu_init_display(dev_priv);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001147
Sinclair Yehc8261a92015-06-26 01:23:42 -07001148 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001149}
1150
1151int vmw_kms_close(struct vmw_private *dev_priv)
1152{
Sinclair Yehc8261a92015-06-26 01:23:42 -07001153 int ret;
1154
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001155 /*
1156 * Docs says we should take the lock before calling this function
1157 * but since it destroys encoders and our destructor calls
1158 * drm_encoder_cleanup which takes the lock we deadlock.
1159 */
1160 drm_mode_config_cleanup(dev_priv->dev);
Sinclair Yehc8261a92015-06-26 01:23:42 -07001161 if (dev_priv->active_display_unit == vmw_du_screen_object)
1162 ret = vmw_kms_sou_close_display(dev_priv);
Jakob Bornecrantzc0d18312011-11-09 10:25:26 +01001163 else
Sinclair Yehc8261a92015-06-26 01:23:42 -07001164 ret = vmw_kms_ldu_close_display(dev_priv);
1165
1166 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001167}
1168
1169int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
1170 struct drm_file *file_priv)
1171{
1172 struct drm_vmw_cursor_bypass_arg *arg = data;
1173 struct vmw_display_unit *du;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001174 struct drm_crtc *crtc;
1175 int ret = 0;
1176
1177
1178 mutex_lock(&dev->mode_config.mutex);
1179 if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
1180
1181 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1182 du = vmw_crtc_to_du(crtc);
1183 du->hotspot_x = arg->xhot;
1184 du->hotspot_y = arg->yhot;
1185 }
1186
1187 mutex_unlock(&dev->mode_config.mutex);
1188 return 0;
1189 }
1190
Rob Clarka4cd5d62014-07-17 23:30:02 -04001191 crtc = drm_crtc_find(dev, arg->crtc_id);
1192 if (!crtc) {
Ville Syrjälä4ae87ff2013-10-17 13:35:05 +03001193 ret = -ENOENT;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001194 goto out;
1195 }
1196
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001197 du = vmw_crtc_to_du(crtc);
1198
1199 du->hotspot_x = arg->xhot;
1200 du->hotspot_y = arg->yhot;
1201
1202out:
1203 mutex_unlock(&dev->mode_config.mutex);
1204
1205 return ret;
1206}
1207
Michel Dänzer0bef23f2011-08-31 07:42:50 +00001208int vmw_kms_write_svga(struct vmw_private *vmw_priv,
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001209 unsigned width, unsigned height, unsigned pitch,
Michel Dänzer6558429b2011-08-31 07:42:49 +00001210 unsigned bpp, unsigned depth)
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001211{
1212 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1213 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch);
1214 else if (vmw_fifo_have_pitchlock(vmw_priv))
1215 iowrite32(pitch, vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
1216 vmw_write(vmw_priv, SVGA_REG_WIDTH, width);
1217 vmw_write(vmw_priv, SVGA_REG_HEIGHT, height);
Michel Dänzer6558429b2011-08-31 07:42:49 +00001218 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp);
Michel Dänzer0bef23f2011-08-31 07:42:50 +00001219
1220 if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) {
1221 DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n",
1222 depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH));
1223 return -EINVAL;
1224 }
1225
1226 return 0;
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001227}
1228
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001229int vmw_kms_save_vga(struct vmw_private *vmw_priv)
1230{
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001231 struct vmw_vga_topology_state *save;
1232 uint32_t i;
1233
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001234 vmw_priv->vga_width = vmw_read(vmw_priv, SVGA_REG_WIDTH);
1235 vmw_priv->vga_height = vmw_read(vmw_priv, SVGA_REG_HEIGHT);
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001236 vmw_priv->vga_bpp = vmw_read(vmw_priv, SVGA_REG_BITS_PER_PIXEL);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001237 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1238 vmw_priv->vga_pitchlock =
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001239 vmw_read(vmw_priv, SVGA_REG_PITCHLOCK);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001240 else if (vmw_fifo_have_pitchlock(vmw_priv))
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001241 vmw_priv->vga_pitchlock = ioread32(vmw_priv->mmio_virt +
Sinclair Yehc8261a92015-06-26 01:23:42 -07001242 SVGA_FIFO_PITCHLOCK);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001243
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001244 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1245 return 0;
1246
1247 vmw_priv->num_displays = vmw_read(vmw_priv,
1248 SVGA_REG_NUM_GUEST_DISPLAYS);
1249
Thomas Hellstrom029e50b2010-10-05 12:43:08 +02001250 if (vmw_priv->num_displays == 0)
1251 vmw_priv->num_displays = 1;
1252
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001253 for (i = 0; i < vmw_priv->num_displays; ++i) {
1254 save = &vmw_priv->vga_save[i];
1255 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1256 save->primary = vmw_read(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY);
1257 save->pos_x = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_X);
1258 save->pos_y = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y);
1259 save->width = vmw_read(vmw_priv, SVGA_REG_DISPLAY_WIDTH);
1260 save->height = vmw_read(vmw_priv, SVGA_REG_DISPLAY_HEIGHT);
1261 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
Thomas Hellstrom30c78bb2010-10-01 10:21:48 +02001262 if (i == 0 && vmw_priv->num_displays == 1 &&
1263 save->width == 0 && save->height == 0) {
1264
1265 /*
1266 * It should be fairly safe to assume that these
1267 * values are uninitialized.
1268 */
1269
1270 save->width = vmw_priv->vga_width - save->pos_x;
1271 save->height = vmw_priv->vga_height - save->pos_y;
1272 }
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001273 }
Thomas Hellstrom30c78bb2010-10-01 10:21:48 +02001274
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001275 return 0;
1276}
1277
1278int vmw_kms_restore_vga(struct vmw_private *vmw_priv)
1279{
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001280 struct vmw_vga_topology_state *save;
1281 uint32_t i;
1282
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001283 vmw_write(vmw_priv, SVGA_REG_WIDTH, vmw_priv->vga_width);
1284 vmw_write(vmw_priv, SVGA_REG_HEIGHT, vmw_priv->vga_height);
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001285 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, vmw_priv->vga_bpp);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001286 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1287 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK,
1288 vmw_priv->vga_pitchlock);
1289 else if (vmw_fifo_have_pitchlock(vmw_priv))
1290 iowrite32(vmw_priv->vga_pitchlock,
1291 vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001292
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001293 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1294 return 0;
1295
1296 for (i = 0; i < vmw_priv->num_displays; ++i) {
1297 save = &vmw_priv->vga_save[i];
1298 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1299 vmw_write(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY, save->primary);
1300 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_X, save->pos_x);
1301 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y, save->pos_y);
1302 vmw_write(vmw_priv, SVGA_REG_DISPLAY_WIDTH, save->width);
1303 vmw_write(vmw_priv, SVGA_REG_DISPLAY_HEIGHT, save->height);
1304 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
1305 }
1306
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001307 return 0;
1308}
Jakob Bornecrantzd8bd19d2010-06-01 11:54:20 +02001309
Thomas Hellstrome133e732010-10-05 12:43:04 +02001310bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
1311 uint32_t pitch,
1312 uint32_t height)
1313{
Thomas Hellstrombc2d6502012-11-21 10:32:36 +01001314 return ((u64) pitch * (u64) height) < (u64) dev_priv->prim_bb_mem;
Thomas Hellstrome133e732010-10-05 12:43:04 +02001315}
1316
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001317
1318/**
1319 * Function called by DRM code called with vbl_lock held.
1320 */
Thomas Hellstrom7a1c2f62010-10-01 10:21:49 +02001321u32 vmw_get_vblank_counter(struct drm_device *dev, int crtc)
1322{
1323 return 0;
1324}
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001325
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001326/**
1327 * Function called by DRM code called with vbl_lock held.
1328 */
1329int vmw_enable_vblank(struct drm_device *dev, int crtc)
1330{
1331 return -ENOSYS;
1332}
1333
1334/**
1335 * Function called by DRM code called with vbl_lock held.
1336 */
1337void vmw_disable_vblank(struct drm_device *dev, int crtc)
1338{
1339}
1340
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001341
1342/*
1343 * Small shared kms functions.
1344 */
1345
Rashika Kheria847c5962014-01-06 22:18:10 +05301346static int vmw_du_update_layout(struct vmw_private *dev_priv, unsigned num,
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001347 struct drm_vmw_rect *rects)
1348{
1349 struct drm_device *dev = dev_priv->dev;
1350 struct vmw_display_unit *du;
1351 struct drm_connector *con;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001352
1353 mutex_lock(&dev->mode_config.mutex);
1354
1355#if 0
Thomas Hellstrom6ea77d12011-10-04 20:13:36 +02001356 {
1357 unsigned int i;
1358
1359 DRM_INFO("%s: new layout ", __func__);
1360 for (i = 0; i < num; i++)
1361 DRM_INFO("(%i, %i %ux%u) ", rects[i].x, rects[i].y,
1362 rects[i].w, rects[i].h);
1363 DRM_INFO("\n");
1364 }
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001365#endif
1366
1367 list_for_each_entry(con, &dev->mode_config.connector_list, head) {
1368 du = vmw_connector_to_du(con);
1369 if (num > du->unit) {
1370 du->pref_width = rects[du->unit].w;
1371 du->pref_height = rects[du->unit].h;
1372 du->pref_active = true;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001373 du->gui_x = rects[du->unit].x;
1374 du->gui_y = rects[du->unit].y;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001375 } else {
1376 du->pref_width = 800;
1377 du->pref_height = 600;
1378 du->pref_active = false;
1379 }
1380 con->status = vmw_du_connector_detect(con, true);
1381 }
1382
1383 mutex_unlock(&dev->mode_config.mutex);
1384
1385 return 0;
1386}
1387
1388void vmw_du_crtc_save(struct drm_crtc *crtc)
1389{
1390}
1391
1392void vmw_du_crtc_restore(struct drm_crtc *crtc)
1393{
1394}
1395
1396void vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
1397 u16 *r, u16 *g, u16 *b,
1398 uint32_t start, uint32_t size)
1399{
1400 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
1401 int i;
1402
1403 for (i = 0; i < size; i++) {
1404 DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i,
1405 r[i], g[i], b[i]);
1406 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 0, r[i] >> 8);
1407 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 1, g[i] >> 8);
1408 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 2, b[i] >> 8);
1409 }
1410}
1411
1412void vmw_du_connector_dpms(struct drm_connector *connector, int mode)
1413{
1414}
1415
1416void vmw_du_connector_save(struct drm_connector *connector)
1417{
1418}
1419
1420void vmw_du_connector_restore(struct drm_connector *connector)
1421{
1422}
1423
1424enum drm_connector_status
1425vmw_du_connector_detect(struct drm_connector *connector, bool force)
1426{
1427 uint32_t num_displays;
1428 struct drm_device *dev = connector->dev;
1429 struct vmw_private *dev_priv = vmw_priv(dev);
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001430 struct vmw_display_unit *du = vmw_connector_to_du(connector);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001431
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001432 num_displays = vmw_read(dev_priv, SVGA_REG_NUM_DISPLAYS);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001433
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001434 return ((vmw_connector_to_du(connector)->unit < num_displays &&
1435 du->pref_active) ?
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001436 connector_status_connected : connector_status_disconnected);
1437}
1438
1439static struct drm_display_mode vmw_kms_connector_builtin[] = {
1440 /* 640x480@60Hz */
1441 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
1442 752, 800, 0, 480, 489, 492, 525, 0,
1443 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1444 /* 800x600@60Hz */
1445 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
1446 968, 1056, 0, 600, 601, 605, 628, 0,
1447 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1448 /* 1024x768@60Hz */
1449 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
1450 1184, 1344, 0, 768, 771, 777, 806, 0,
1451 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1452 /* 1152x864@75Hz */
1453 { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
1454 1344, 1600, 0, 864, 865, 868, 900, 0,
1455 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1456 /* 1280x768@60Hz */
1457 { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344,
1458 1472, 1664, 0, 768, 771, 778, 798, 0,
1459 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1460 /* 1280x800@60Hz */
1461 { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352,
1462 1480, 1680, 0, 800, 803, 809, 831, 0,
1463 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
1464 /* 1280x960@60Hz */
1465 { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376,
1466 1488, 1800, 0, 960, 961, 964, 1000, 0,
1467 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1468 /* 1280x1024@60Hz */
1469 { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328,
1470 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
1471 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1472 /* 1360x768@60Hz */
1473 { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424,
1474 1536, 1792, 0, 768, 771, 777, 795, 0,
1475 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1476 /* 1440x1050@60Hz */
1477 { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488,
1478 1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
1479 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1480 /* 1440x900@60Hz */
1481 { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520,
1482 1672, 1904, 0, 900, 903, 909, 934, 0,
1483 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1484 /* 1600x1200@60Hz */
1485 { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664,
1486 1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
1487 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1488 /* 1680x1050@60Hz */
1489 { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784,
1490 1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
1491 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1492 /* 1792x1344@60Hz */
1493 { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920,
1494 2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
1495 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1496 /* 1853x1392@60Hz */
1497 { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952,
1498 2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
1499 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1500 /* 1920x1200@60Hz */
1501 { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056,
1502 2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
1503 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1504 /* 1920x1440@60Hz */
1505 { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048,
1506 2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
1507 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1508 /* 2560x1600@60Hz */
1509 { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752,
1510 3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
1511 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1512 /* Terminate */
1513 { DRM_MODE("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) },
1514};
1515
Thomas Hellstrom1543b4d2011-11-02 09:43:10 +01001516/**
1517 * vmw_guess_mode_timing - Provide fake timings for a
1518 * 60Hz vrefresh mode.
1519 *
1520 * @mode - Pointer to a struct drm_display_mode with hdisplay and vdisplay
1521 * members filled in.
1522 */
1523static void vmw_guess_mode_timing(struct drm_display_mode *mode)
1524{
1525 mode->hsync_start = mode->hdisplay + 50;
1526 mode->hsync_end = mode->hsync_start + 50;
1527 mode->htotal = mode->hsync_end + 50;
1528
1529 mode->vsync_start = mode->vdisplay + 50;
1530 mode->vsync_end = mode->vsync_start + 50;
1531 mode->vtotal = mode->vsync_end + 50;
1532
1533 mode->clock = (u32)mode->htotal * (u32)mode->vtotal / 100 * 6;
1534 mode->vrefresh = drm_mode_vrefresh(mode);
1535}
1536
1537
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001538int vmw_du_connector_fill_modes(struct drm_connector *connector,
1539 uint32_t max_width, uint32_t max_height)
1540{
1541 struct vmw_display_unit *du = vmw_connector_to_du(connector);
1542 struct drm_device *dev = connector->dev;
1543 struct vmw_private *dev_priv = vmw_priv(dev);
1544 struct drm_display_mode *mode = NULL;
1545 struct drm_display_mode *bmode;
1546 struct drm_display_mode prefmode = { DRM_MODE("preferred",
1547 DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
1548 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1549 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
1550 };
1551 int i;
Sinclair Yeh9a723842014-10-31 09:58:06 +01001552 u32 assumed_bpp = 2;
1553
1554 /*
1555 * If using screen objects, then assume 32-bpp because that's what the
1556 * SVGA device is assuming
1557 */
Sinclair Yehc8261a92015-06-26 01:23:42 -07001558 if (dev_priv->active_display_unit == vmw_du_screen_object)
Sinclair Yeh9a723842014-10-31 09:58:06 +01001559 assumed_bpp = 4;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001560
1561 /* Add preferred mode */
Sinclair Yehc8261a92015-06-26 01:23:42 -07001562 mode = drm_mode_duplicate(dev, &prefmode);
1563 if (!mode)
1564 return 0;
1565 mode->hdisplay = du->pref_width;
1566 mode->vdisplay = du->pref_height;
1567 vmw_guess_mode_timing(mode);
Jakob Bornecrantz55bde5b2011-11-03 21:03:05 +01001568
Sinclair Yehc8261a92015-06-26 01:23:42 -07001569 if (vmw_kms_validate_mode_vram(dev_priv,
1570 mode->hdisplay * assumed_bpp,
1571 mode->vdisplay)) {
1572 drm_mode_probed_add(connector, mode);
1573 } else {
1574 drm_mode_destroy(dev, mode);
1575 mode = NULL;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001576 }
1577
Sinclair Yehc8261a92015-06-26 01:23:42 -07001578 if (du->pref_mode) {
1579 list_del_init(&du->pref_mode->head);
1580 drm_mode_destroy(dev, du->pref_mode);
1581 }
1582
1583 /* mode might be null here, this is intended */
1584 du->pref_mode = mode;
1585
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001586 for (i = 0; vmw_kms_connector_builtin[i].type != 0; i++) {
1587 bmode = &vmw_kms_connector_builtin[i];
1588 if (bmode->hdisplay > max_width ||
1589 bmode->vdisplay > max_height)
1590 continue;
1591
Sinclair Yeh9a723842014-10-31 09:58:06 +01001592 if (!vmw_kms_validate_mode_vram(dev_priv,
1593 bmode->hdisplay * assumed_bpp,
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001594 bmode->vdisplay))
1595 continue;
1596
1597 mode = drm_mode_duplicate(dev, bmode);
1598 if (!mode)
1599 return 0;
1600 mode->vrefresh = drm_mode_vrefresh(mode);
1601
1602 drm_mode_probed_add(connector, mode);
1603 }
1604
Jakob Bornecrantzd41025c2011-11-03 21:03:07 +01001605 /* Move the prefered mode first, help apps pick the right mode. */
1606 if (du->pref_mode)
1607 list_move(&du->pref_mode->head, &connector->probed_modes);
1608
Dave Airlieb87577b2014-05-01 09:26:53 +10001609 drm_mode_connector_list_update(connector, true);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001610
1611 return 1;
1612}
1613
1614int vmw_du_connector_set_property(struct drm_connector *connector,
1615 struct drm_property *property,
1616 uint64_t val)
1617{
1618 return 0;
1619}
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001620
1621
1622int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data,
1623 struct drm_file *file_priv)
1624{
1625 struct vmw_private *dev_priv = vmw_priv(dev);
1626 struct drm_vmw_update_layout_arg *arg =
1627 (struct drm_vmw_update_layout_arg *)data;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001628 void __user *user_rects;
1629 struct drm_vmw_rect *rects;
1630 unsigned rects_size;
1631 int ret;
1632 int i;
1633 struct drm_mode_config *mode_config = &dev->mode_config;
Sinclair Yehc8261a92015-06-26 01:23:42 -07001634 struct drm_vmw_rect bounding_box = {0};
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001635
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001636 if (!arg->num_outputs) {
1637 struct drm_vmw_rect def_rect = {0, 0, 800, 600};
1638 vmw_du_update_layout(dev_priv, 1, &def_rect);
Thomas Hellstrom5151adb2015-03-09 01:56:21 -07001639 return 0;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001640 }
1641
1642 rects_size = arg->num_outputs * sizeof(struct drm_vmw_rect);
Xi Wangbab9efc2011-11-28 12:25:43 +01001643 rects = kcalloc(arg->num_outputs, sizeof(struct drm_vmw_rect),
1644 GFP_KERNEL);
Thomas Hellstrom5151adb2015-03-09 01:56:21 -07001645 if (unlikely(!rects))
1646 return -ENOMEM;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001647
1648 user_rects = (void __user *)(unsigned long)arg->rects;
1649 ret = copy_from_user(rects, user_rects, rects_size);
1650 if (unlikely(ret != 0)) {
1651 DRM_ERROR("Failed to get rects.\n");
1652 ret = -EFAULT;
1653 goto out_free;
1654 }
1655
1656 for (i = 0; i < arg->num_outputs; ++i) {
Xi Wangbab9efc2011-11-28 12:25:43 +01001657 if (rects[i].x < 0 ||
1658 rects[i].y < 0 ||
1659 rects[i].x + rects[i].w > mode_config->max_width ||
1660 rects[i].y + rects[i].h > mode_config->max_height) {
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001661 DRM_ERROR("Invalid GUI layout.\n");
1662 ret = -EINVAL;
1663 goto out_free;
1664 }
Sinclair Yehc8261a92015-06-26 01:23:42 -07001665
1666 /*
1667 * bounding_box.w and bunding_box.h are used as
1668 * lower-right coordinates
1669 */
1670 if (rects[i].x + rects[i].w > bounding_box.w)
1671 bounding_box.w = rects[i].x + rects[i].w;
1672
1673 if (rects[i].y + rects[i].h > bounding_box.h)
1674 bounding_box.h = rects[i].y + rects[i].h;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001675 }
1676
1677 vmw_du_update_layout(dev_priv, arg->num_outputs, rects);
1678
1679out_free:
1680 kfree(rects);
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001681 return ret;
1682}