blob: 159af7ee111fadd52ee1c005deb65b911a3e89aa [file] [log] [blame]
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001/**************************************************************************
2 *
3 * Copyright © 2009 VMware, Inc., Palo Alto, CA., USA
4 * 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
35struct vmw_clip_rect {
36 int x1, x2, y1, y2;
37};
38
39/**
40 * Clip @num_rects number of @rects against @clip storing the
41 * results in @out_rects and the number of passed rects in @out_num.
42 */
Rashika Kheria847c5962014-01-06 22:18:10 +053043static void vmw_clip_cliprects(struct drm_clip_rect *rects,
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +010044 int num_rects,
45 struct vmw_clip_rect clip,
46 SVGASignedRect *out_rects,
47 int *out_num)
48{
49 int i, k;
50
51 for (i = 0, k = 0; i < num_rects; i++) {
52 int x1 = max_t(int, clip.x1, rects[i].x1);
53 int y1 = max_t(int, clip.y1, rects[i].y1);
54 int x2 = min_t(int, clip.x2, rects[i].x2);
55 int y2 = min_t(int, clip.y2, rects[i].y2);
56
57 if (x1 >= x2)
58 continue;
59 if (y1 >= y2)
60 continue;
61
62 out_rects[k].left = x1;
63 out_rects[k].top = y1;
64 out_rects[k].right = x2;
65 out_rects[k].bottom = y2;
66 k++;
67 }
68
69 *out_num = k;
70}
71
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000072void vmw_display_unit_cleanup(struct vmw_display_unit *du)
73{
74 if (du->cursor_surface)
75 vmw_surface_unreference(&du->cursor_surface);
76 if (du->cursor_dmabuf)
77 vmw_dmabuf_unreference(&du->cursor_dmabuf);
Thomas Hellstrom6a0a7a92013-12-02 06:04:38 -080078 drm_sysfs_connector_remove(&du->connector);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000079 drm_crtc_cleanup(&du->crtc);
80 drm_encoder_cleanup(&du->encoder);
81 drm_connector_cleanup(&du->connector);
82}
83
84/*
85 * Display Unit Cursor functions
86 */
87
88int vmw_cursor_update_image(struct vmw_private *dev_priv,
89 u32 *image, u32 width, u32 height,
90 u32 hotspotX, u32 hotspotY)
91{
92 struct {
93 u32 cmd;
94 SVGAFifoCmdDefineAlphaCursor cursor;
95 } *cmd;
96 u32 image_size = width * height * 4;
97 u32 cmd_size = sizeof(*cmd) + image_size;
98
99 if (!image)
100 return -EINVAL;
101
102 cmd = vmw_fifo_reserve(dev_priv, cmd_size);
103 if (unlikely(cmd == NULL)) {
104 DRM_ERROR("Fifo reserve failed.\n");
105 return -ENOMEM;
106 }
107
108 memset(cmd, 0, sizeof(*cmd));
109
110 memcpy(&cmd[1], image, image_size);
111
112 cmd->cmd = cpu_to_le32(SVGA_CMD_DEFINE_ALPHA_CURSOR);
113 cmd->cursor.id = cpu_to_le32(0);
114 cmd->cursor.width = cpu_to_le32(width);
115 cmd->cursor.height = cpu_to_le32(height);
116 cmd->cursor.hotspotX = cpu_to_le32(hotspotX);
117 cmd->cursor.hotspotY = cpu_to_le32(hotspotY);
118
119 vmw_fifo_commit(dev_priv, cmd_size);
120
121 return 0;
122}
123
Jakob Bornecrantz6a91d972011-11-28 13:19:10 +0100124int vmw_cursor_update_dmabuf(struct vmw_private *dev_priv,
125 struct vmw_dma_buffer *dmabuf,
126 u32 width, u32 height,
127 u32 hotspotX, u32 hotspotY)
128{
129 struct ttm_bo_kmap_obj map;
130 unsigned long kmap_offset;
131 unsigned long kmap_num;
132 void *virtual;
133 bool dummy;
134 int ret;
135
136 kmap_offset = 0;
137 kmap_num = (width*height*4 + PAGE_SIZE - 1) >> PAGE_SHIFT;
138
139 ret = ttm_bo_reserve(&dmabuf->base, true, false, false, 0);
140 if (unlikely(ret != 0)) {
141 DRM_ERROR("reserve failed\n");
142 return -EINVAL;
143 }
144
145 ret = ttm_bo_kmap(&dmabuf->base, kmap_offset, kmap_num, &map);
146 if (unlikely(ret != 0))
147 goto err_unreserve;
148
149 virtual = ttm_kmap_obj_virtual(&map, &dummy);
150 ret = vmw_cursor_update_image(dev_priv, virtual, width, height,
151 hotspotX, hotspotY);
152
153 ttm_bo_kunmap(&map);
154err_unreserve:
155 ttm_bo_unreserve(&dmabuf->base);
156
157 return ret;
158}
159
160
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000161void vmw_cursor_update_position(struct vmw_private *dev_priv,
162 bool show, int x, int y)
163{
164 __le32 __iomem *fifo_mem = dev_priv->mmio_virt;
165 uint32_t count;
166
167 iowrite32(show ? 1 : 0, fifo_mem + SVGA_FIFO_CURSOR_ON);
168 iowrite32(x, fifo_mem + SVGA_FIFO_CURSOR_X);
169 iowrite32(y, fifo_mem + SVGA_FIFO_CURSOR_Y);
170 count = ioread32(fifo_mem + SVGA_FIFO_CURSOR_COUNT);
171 iowrite32(++count, fifo_mem + SVGA_FIFO_CURSOR_COUNT);
172}
173
174int vmw_du_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
175 uint32_t handle, uint32_t width, uint32_t height)
176{
177 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000178 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
179 struct vmw_surface *surface = NULL;
180 struct vmw_dma_buffer *dmabuf = NULL;
181 int ret;
182
Daniel Vetterbfb89922012-12-02 13:48:21 +0100183 /*
184 * FIXME: Unclear whether there's any global state touched by the
185 * cursor_set function, especially vmw_cursor_update_position looks
186 * suspicious. For now take the easy route and reacquire all locks. We
187 * can do this since the caller in the drm core doesn't check anything
188 * which is protected by any looks.
189 */
190 mutex_unlock(&crtc->mutex);
191 drm_modeset_lock_all(dev_priv->dev);
192
Jakob Bornecrantzbaa91d642011-11-09 10:25:28 +0100193 /* A lot of the code assumes this */
Daniel Vetterbfb89922012-12-02 13:48:21 +0100194 if (handle && (width != 64 || height != 64)) {
195 ret = -EINVAL;
196 goto out;
197 }
Jakob Bornecrantzbaa91d642011-11-09 10:25:28 +0100198
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000199 if (handle) {
Ville Syrjäläa5d0f572013-06-03 16:10:41 +0300200 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
201
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100202 ret = vmw_user_lookup_handle(dev_priv, tfile,
203 handle, &surface, &dmabuf);
204 if (ret) {
205 DRM_ERROR("failed to find surface or dmabuf: %i\n", ret);
Daniel Vetterbfb89922012-12-02 13:48:21 +0100206 ret = -EINVAL;
207 goto out;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000208 }
209 }
210
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100211 /* need to do this before taking down old image */
212 if (surface && !surface->snooper.image) {
213 DRM_ERROR("surface not suitable for cursor\n");
214 vmw_surface_unreference(&surface);
Daniel Vetterbfb89922012-12-02 13:48:21 +0100215 ret = -EINVAL;
216 goto out;
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100217 }
218
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000219 /* takedown old cursor */
220 if (du->cursor_surface) {
221 du->cursor_surface->snooper.crtc = NULL;
222 vmw_surface_unreference(&du->cursor_surface);
223 }
224 if (du->cursor_dmabuf)
225 vmw_dmabuf_unreference(&du->cursor_dmabuf);
226
227 /* setup new image */
228 if (surface) {
229 /* vmw_user_surface_lookup takes one reference */
230 du->cursor_surface = surface;
231
232 du->cursor_surface->snooper.crtc = crtc;
233 du->cursor_age = du->cursor_surface->snooper.age;
234 vmw_cursor_update_image(dev_priv, surface->snooper.image,
235 64, 64, du->hotspot_x, du->hotspot_y);
236 } else if (dmabuf) {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000237 /* vmw_user_surface_lookup takes one reference */
238 du->cursor_dmabuf = dmabuf;
239
Jakob Bornecrantz6a91d972011-11-28 13:19:10 +0100240 ret = vmw_cursor_update_dmabuf(dev_priv, dmabuf, width, height,
241 du->hotspot_x, du->hotspot_y);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000242 } else {
243 vmw_cursor_update_position(dev_priv, false, 0, 0);
Daniel Vetterbfb89922012-12-02 13:48:21 +0100244 ret = 0;
245 goto out;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000246 }
247
Thomas Hellstromda7653d2011-11-02 09:43:12 +0100248 vmw_cursor_update_position(dev_priv, true,
249 du->cursor_x + du->hotspot_x,
250 du->cursor_y + du->hotspot_y);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000251
Daniel Vetterbfb89922012-12-02 13:48:21 +0100252 ret = 0;
253out:
254 drm_modeset_unlock_all(dev_priv->dev);
255 mutex_lock(&crtc->mutex);
256
257 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000258}
259
260int vmw_du_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
261{
262 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
263 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
264 bool shown = du->cursor_surface || du->cursor_dmabuf ? true : false;
265
266 du->cursor_x = x + crtc->x;
267 du->cursor_y = y + crtc->y;
268
Daniel Vetterdac35662012-12-02 15:24:10 +0100269 /*
270 * FIXME: Unclear whether there's any global state touched by the
271 * cursor_set function, especially vmw_cursor_update_position looks
272 * suspicious. For now take the easy route and reacquire all locks. We
273 * can do this since the caller in the drm core doesn't check anything
274 * which is protected by any looks.
275 */
276 mutex_unlock(&crtc->mutex);
277 drm_modeset_lock_all(dev_priv->dev);
278
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000279 vmw_cursor_update_position(dev_priv, shown,
Thomas Hellstromda7653d2011-11-02 09:43:12 +0100280 du->cursor_x + du->hotspot_x,
281 du->cursor_y + du->hotspot_y);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000282
Daniel Vetterdac35662012-12-02 15:24:10 +0100283 drm_modeset_unlock_all(dev_priv->dev);
284 mutex_lock(&crtc->mutex);
285
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000286 return 0;
287}
288
289void vmw_kms_cursor_snoop(struct vmw_surface *srf,
290 struct ttm_object_file *tfile,
291 struct ttm_buffer_object *bo,
292 SVGA3dCmdHeader *header)
293{
294 struct ttm_bo_kmap_obj map;
295 unsigned long kmap_offset;
296 unsigned long kmap_num;
297 SVGA3dCopyBox *box;
298 unsigned box_count;
299 void *virtual;
300 bool dummy;
301 struct vmw_dma_cmd {
302 SVGA3dCmdHeader header;
303 SVGA3dCmdSurfaceDMA dma;
304 } *cmd;
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100305 int i, ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000306
307 cmd = container_of(header, struct vmw_dma_cmd, header);
308
309 /* No snooper installed */
310 if (!srf->snooper.image)
311 return;
312
313 if (cmd->dma.host.face != 0 || cmd->dma.host.mipmap != 0) {
314 DRM_ERROR("face and mipmap for cursors should never != 0\n");
315 return;
316 }
317
318 if (cmd->header.size < 64) {
319 DRM_ERROR("at least one full copy box must be given\n");
320 return;
321 }
322
323 box = (SVGA3dCopyBox *)&cmd[1];
324 box_count = (cmd->header.size - sizeof(SVGA3dCmdSurfaceDMA)) /
325 sizeof(SVGA3dCopyBox);
326
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100327 if (cmd->dma.guest.ptr.offset % PAGE_SIZE ||
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000328 box->x != 0 || box->y != 0 || box->z != 0 ||
329 box->srcx != 0 || box->srcy != 0 || box->srcz != 0 ||
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100330 box->d != 1 || box_count != 1) {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000331 /* TODO handle none page aligned offsets */
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100332 /* TODO handle more dst & src != 0 */
333 /* TODO handle more then one copy */
334 DRM_ERROR("Cant snoop dma request for cursor!\n");
335 DRM_ERROR("(%u, %u, %u) (%u, %u, %u) (%ux%ux%u) %u %u\n",
336 box->srcx, box->srcy, box->srcz,
337 box->x, box->y, box->z,
338 box->w, box->h, box->d, box_count,
339 cmd->dma.guest.ptr.offset);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000340 return;
341 }
342
343 kmap_offset = cmd->dma.guest.ptr.offset >> PAGE_SHIFT;
344 kmap_num = (64*64*4) >> PAGE_SHIFT;
345
346 ret = ttm_bo_reserve(bo, true, false, false, 0);
347 if (unlikely(ret != 0)) {
348 DRM_ERROR("reserve failed\n");
349 return;
350 }
351
352 ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
353 if (unlikely(ret != 0))
354 goto err_unreserve;
355
356 virtual = ttm_kmap_obj_virtual(&map, &dummy);
357
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100358 if (box->w == 64 && cmd->dma.guest.pitch == 64*4) {
359 memcpy(srf->snooper.image, virtual, 64*64*4);
360 } else {
361 /* Image is unsigned pointer. */
362 for (i = 0; i < box->h; i++)
363 memcpy(srf->snooper.image + i * 64,
364 virtual + i * cmd->dma.guest.pitch,
365 box->w * 4);
366 }
367
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000368 srf->snooper.age++;
369
370 /* we can't call this function from this function since execbuf has
371 * reserved fifo space.
372 *
373 * if (srf->snooper.crtc)
374 * vmw_ldu_crtc_cursor_update_image(dev_priv,
375 * srf->snooper.image, 64, 64,
376 * du->hotspot_x, du->hotspot_y);
377 */
378
379 ttm_bo_kunmap(&map);
380err_unreserve:
381 ttm_bo_unreserve(bo);
382}
383
384void vmw_kms_cursor_post_execbuf(struct vmw_private *dev_priv)
385{
386 struct drm_device *dev = dev_priv->dev;
387 struct vmw_display_unit *du;
388 struct drm_crtc *crtc;
389
390 mutex_lock(&dev->mode_config.mutex);
391
392 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
393 du = vmw_crtc_to_du(crtc);
394 if (!du->cursor_surface ||
395 du->cursor_age == du->cursor_surface->snooper.age)
396 continue;
397
398 du->cursor_age = du->cursor_surface->snooper.age;
399 vmw_cursor_update_image(dev_priv,
400 du->cursor_surface->snooper.image,
401 64, 64, du->hotspot_x, du->hotspot_y);
402 }
403
404 mutex_unlock(&dev->mode_config.mutex);
405}
406
407/*
408 * Generic framebuffer code
409 */
410
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000411/*
412 * Surface framebuffer code
413 */
414
415#define vmw_framebuffer_to_vfbs(x) \
416 container_of(x, struct vmw_framebuffer_surface, base.base)
417
418struct vmw_framebuffer_surface {
419 struct vmw_framebuffer base;
420 struct vmw_surface *surface;
Thomas Hellstrom22ee8612010-05-28 11:22:00 +0200421 struct vmw_dma_buffer *buffer;
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200422 struct list_head head;
423 struct drm_master *master;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000424};
425
Rashika Kheria847c5962014-01-06 22:18:10 +0530426static void vmw_framebuffer_surface_destroy(struct drm_framebuffer *framebuffer)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000427{
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200428 struct vmw_framebuffer_surface *vfbs =
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000429 vmw_framebuffer_to_vfbs(framebuffer);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200430 struct vmw_master *vmaster = vmw_master(vfbs->master);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000431
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200432
433 mutex_lock(&vmaster->fb_surf_mutex);
434 list_del(&vfbs->head);
435 mutex_unlock(&vmaster->fb_surf_mutex);
436
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200437 drm_master_put(&vfbs->master);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000438 drm_framebuffer_cleanup(framebuffer);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200439 vmw_surface_unreference(&vfbs->surface);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200440 ttm_base_object_unref(&vfbs->base.user_obj);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000441
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200442 kfree(vfbs);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000443}
444
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200445static int do_surface_dirty_sou(struct vmw_private *dev_priv,
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200446 struct drm_file *file_priv,
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200447 struct vmw_framebuffer *framebuffer,
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200448 unsigned flags, unsigned color,
449 struct drm_clip_rect *clips,
Jakob Bornecrantzbd49ae42012-02-09 16:56:44 +0100450 unsigned num_clips, int inc,
451 struct vmw_fence_obj **out_fence)
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200452{
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200453 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100454 struct drm_clip_rect *clips_ptr;
455 struct drm_clip_rect *tmp;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200456 struct drm_crtc *crtc;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200457 size_t fifo_size;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200458 int i, num_units;
459 int ret = 0; /* silence warning */
460 int left, right, top, bottom;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200461
462 struct {
463 SVGA3dCmdHeader header;
464 SVGA3dCmdBlitSurfaceToScreen body;
465 } *cmd;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200466 SVGASignedRect *blits;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200467
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200468 num_units = 0;
469 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list,
470 head) {
471 if (crtc->fb != &framebuffer->base)
472 continue;
473 units[num_units++] = vmw_crtc_to_du(crtc);
474 }
475
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200476 BUG_ON(!clips || !num_clips);
477
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100478 tmp = kzalloc(sizeof(*tmp) * num_clips, GFP_KERNEL);
479 if (unlikely(tmp == NULL)) {
480 DRM_ERROR("Temporary cliprect memory alloc failed.\n");
481 return -ENOMEM;
482 }
483
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200484 fifo_size = sizeof(*cmd) + sizeof(SVGASignedRect) * num_clips;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200485 cmd = kzalloc(fifo_size, GFP_KERNEL);
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200486 if (unlikely(cmd == NULL)) {
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200487 DRM_ERROR("Temporary fifo memory alloc failed.\n");
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100488 ret = -ENOMEM;
489 goto out_free_tmp;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200490 }
491
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100492 /* setup blits pointer */
493 blits = (SVGASignedRect *)&cmd[1];
494
495 /* initial clip region */
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200496 left = clips->x1;
497 right = clips->x2;
498 top = clips->y1;
499 bottom = clips->y2;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200500
Jakob Bornecrantzf0c8a652011-11-09 10:25:27 +0100501 /* skip the first clip rect */
502 for (i = 1, clips_ptr = clips + inc;
503 i < num_clips; i++, clips_ptr += inc) {
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200504 left = min_t(int, left, (int)clips_ptr->x1);
505 right = max_t(int, right, (int)clips_ptr->x2);
506 top = min_t(int, top, (int)clips_ptr->y1);
507 bottom = max_t(int, bottom, (int)clips_ptr->y2);
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200508 }
509
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200510 /* only need to do this once */
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200511 cmd->header.id = cpu_to_le32(SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN);
512 cmd->header.size = cpu_to_le32(fifo_size - sizeof(cmd->header));
513
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200514 cmd->body.srcRect.left = left;
515 cmd->body.srcRect.right = right;
516 cmd->body.srcRect.top = top;
517 cmd->body.srcRect.bottom = bottom;
518
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200519 clips_ptr = clips;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200520 for (i = 0; i < num_clips; i++, clips_ptr += inc) {
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100521 tmp[i].x1 = clips_ptr->x1 - left;
522 tmp[i].x2 = clips_ptr->x2 - left;
523 tmp[i].y1 = clips_ptr->y1 - top;
524 tmp[i].y2 = clips_ptr->y2 - top;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200525 }
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200526
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200527 /* do per unit writing, reuse fifo for each */
528 for (i = 0; i < num_units; i++) {
529 struct vmw_display_unit *unit = units[i];
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100530 struct vmw_clip_rect clip;
531 int num;
532
533 clip.x1 = left - unit->crtc.x;
534 clip.y1 = top - unit->crtc.y;
535 clip.x2 = right - unit->crtc.x;
536 clip.y2 = bottom - unit->crtc.y;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200537
538 /* skip any crtcs that misses the clip region */
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100539 if (clip.x1 >= unit->crtc.mode.hdisplay ||
540 clip.y1 >= unit->crtc.mode.vdisplay ||
541 clip.x2 <= 0 || clip.y2 <= 0)
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200542 continue;
543
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100544 /*
545 * In order for the clip rects to be correctly scaled
546 * the src and dest rects needs to be the same size.
547 */
548 cmd->body.destRect.left = clip.x1;
549 cmd->body.destRect.right = clip.x2;
550 cmd->body.destRect.top = clip.y1;
551 cmd->body.destRect.bottom = clip.y2;
552
553 /* create a clip rect of the crtc in dest coords */
554 clip.x2 = unit->crtc.mode.hdisplay - clip.x1;
555 clip.y2 = unit->crtc.mode.vdisplay - clip.y1;
556 clip.x1 = 0 - clip.x1;
557 clip.y1 = 0 - clip.y1;
558
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200559 /* need to reset sid as it is changed by execbuf */
560 cmd->body.srcImage.sid = cpu_to_le32(framebuffer->user_handle);
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200561 cmd->body.destScreenId = unit->unit;
562
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100563 /* clip and write blits to cmd stream */
564 vmw_clip_cliprects(tmp, num_clips, clip, blits, &num);
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200565
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100566 /* if no cliprects hit skip this */
567 if (num == 0)
568 continue;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200569
Jakob Bornecrantzbd49ae42012-02-09 16:56:44 +0100570 /* only return the last fence */
571 if (out_fence && *out_fence)
572 vmw_fence_obj_unreference(out_fence);
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200573
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100574 /* recalculate package length */
575 fifo_size = sizeof(*cmd) + sizeof(SVGASignedRect) * num;
576 cmd->header.size = cpu_to_le32(fifo_size - sizeof(cmd->header));
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200577 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd,
Jakob Bornecrantzbd49ae42012-02-09 16:56:44 +0100578 fifo_size, 0, NULL, out_fence);
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200579
580 if (unlikely(ret != 0))
581 break;
582 }
583
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100584
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200585 kfree(cmd);
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100586out_free_tmp:
587 kfree(tmp);
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200588
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200589 return ret;
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200590}
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000591
Rashika Kheria847c5962014-01-06 22:18:10 +0530592static int vmw_framebuffer_surface_dirty(struct drm_framebuffer *framebuffer,
Thomas Hellstrom02b00162010-10-05 12:43:02 +0200593 struct drm_file *file_priv,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000594 unsigned flags, unsigned color,
595 struct drm_clip_rect *clips,
596 unsigned num_clips)
597{
598 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
599 struct vmw_framebuffer_surface *vfbs =
600 vmw_framebuffer_to_vfbs(framebuffer);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000601 struct drm_clip_rect norect;
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200602 int ret, inc = 1;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000603
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200604 if (unlikely(vfbs->master != file_priv->master))
605 return -EINVAL;
606
Jakob Bornecrantz01e81412011-10-04 20:13:24 +0200607 /* Require ScreenObject support for 3D */
608 if (!dev_priv->sou_priv)
609 return -EINVAL;
610
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200611 drm_modeset_lock_all(dev_priv->dev);
612
Thomas Hellstrom294adf72014-02-27 12:34:51 +0100613 ret = ttm_read_lock(&dev_priv->reservation_sem, true);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200614 if (unlikely(ret != 0)) {
615 drm_modeset_unlock_all(dev_priv->dev);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200616 return ret;
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200617 }
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200618
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000619 if (!num_clips) {
620 num_clips = 1;
621 clips = &norect;
622 norect.x1 = norect.y1 = 0;
623 norect.x2 = framebuffer->width;
624 norect.y2 = framebuffer->height;
625 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
626 num_clips /= 2;
627 inc = 2; /* skip source rects */
628 }
629
Jakob Bornecrantzc5c42362011-10-22 10:29:34 +0200630 ret = do_surface_dirty_sou(dev_priv, file_priv, &vfbs->base,
Jakob Bornecrantz01e81412011-10-04 20:13:24 +0200631 flags, color,
Jakob Bornecrantzbd49ae42012-02-09 16:56:44 +0100632 clips, num_clips, inc, NULL);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000633
Thomas Hellstrom294adf72014-02-27 12:34:51 +0100634 ttm_read_unlock(&dev_priv->reservation_sem);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200635
636 drm_modeset_unlock_all(dev_priv->dev);
637
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000638 return 0;
639}
640
641static struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs = {
642 .destroy = vmw_framebuffer_surface_destroy,
643 .dirty = vmw_framebuffer_surface_dirty,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000644};
645
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200646static int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv,
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200647 struct drm_file *file_priv,
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200648 struct vmw_surface *surface,
649 struct vmw_framebuffer **out,
650 const struct drm_mode_fb_cmd
651 *mode_cmd)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000652
653{
654 struct drm_device *dev = dev_priv->dev;
655 struct vmw_framebuffer_surface *vfbs;
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200656 enum SVGA3dSurfaceFormat format;
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200657 struct vmw_master *vmaster = vmw_master(file_priv->master);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000658 int ret;
659
Jakob Bornecrantz01e81412011-10-04 20:13:24 +0200660 /* 3D is only supported on HWv8 hosts which supports screen objects */
661 if (!dev_priv->sou_priv)
662 return -ENOSYS;
663
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200664 /*
665 * Sanity checks.
666 */
667
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100668 /* Surface must be marked as a scanout. */
669 if (unlikely(!surface->scanout))
670 return -EINVAL;
671
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200672 if (unlikely(surface->mip_levels[0] != 1 ||
673 surface->num_sizes != 1 ||
Thomas Hellstromb360a3c2014-01-15 08:51:36 +0100674 surface->base_size.width < mode_cmd->width ||
675 surface->base_size.height < mode_cmd->height ||
676 surface->base_size.depth != 1)) {
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200677 DRM_ERROR("Incompatible surface dimensions "
678 "for requested mode.\n");
679 return -EINVAL;
680 }
681
682 switch (mode_cmd->depth) {
683 case 32:
684 format = SVGA3D_A8R8G8B8;
685 break;
686 case 24:
687 format = SVGA3D_X8R8G8B8;
688 break;
689 case 16:
690 format = SVGA3D_R5G6B5;
691 break;
692 case 15:
693 format = SVGA3D_A1R5G5B5;
694 break;
Michel Dänzerf01b7ba2011-08-31 07:42:47 +0000695 case 8:
696 format = SVGA3D_LUMINANCE8;
697 break;
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200698 default:
699 DRM_ERROR("Invalid color depth: %d\n", mode_cmd->depth);
700 return -EINVAL;
701 }
702
703 if (unlikely(format != surface->format)) {
704 DRM_ERROR("Invalid surface format for requested mode.\n");
705 return -EINVAL;
706 }
707
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000708 vfbs = kzalloc(sizeof(*vfbs), GFP_KERNEL);
709 if (!vfbs) {
710 ret = -ENOMEM;
711 goto out_err1;
712 }
713
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000714 if (!vmw_surface_reference(surface)) {
715 DRM_ERROR("failed to reference surface %p\n", surface);
Daniel Vetter80f0b5a2012-12-13 23:39:01 +0100716 ret = -EINVAL;
717 goto out_err2;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000718 }
719
720 /* XXX get the first 3 from the surface info */
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200721 vfbs->base.base.bits_per_pixel = mode_cmd->bpp;
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200722 vfbs->base.base.pitches[0] = mode_cmd->pitch;
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200723 vfbs->base.base.depth = mode_cmd->depth;
724 vfbs->base.base.width = mode_cmd->width;
725 vfbs->base.base.height = mode_cmd->height;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000726 vfbs->surface = surface;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200727 vfbs->base.user_handle = mode_cmd->handle;
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200728 vfbs->master = drm_master_get(file_priv->master);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200729
730 mutex_lock(&vmaster->fb_surf_mutex);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200731 list_add_tail(&vfbs->head, &vmaster->fb_surf);
732 mutex_unlock(&vmaster->fb_surf_mutex);
733
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000734 *out = &vfbs->base;
735
Daniel Vetter80f0b5a2012-12-13 23:39:01 +0100736 ret = drm_framebuffer_init(dev, &vfbs->base.base,
737 &vmw_framebuffer_surface_funcs);
738 if (ret)
739 goto out_err3;
740
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000741 return 0;
742
743out_err3:
Daniel Vetter80f0b5a2012-12-13 23:39:01 +0100744 vmw_surface_unreference(&surface);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000745out_err2:
746 kfree(vfbs);
747out_err1:
748 return ret;
749}
750
751/*
752 * Dmabuf framebuffer code
753 */
754
755#define vmw_framebuffer_to_vfbd(x) \
756 container_of(x, struct vmw_framebuffer_dmabuf, base.base)
757
758struct vmw_framebuffer_dmabuf {
759 struct vmw_framebuffer base;
760 struct vmw_dma_buffer *buffer;
761};
762
Rashika Kheria847c5962014-01-06 22:18:10 +0530763static void vmw_framebuffer_dmabuf_destroy(struct drm_framebuffer *framebuffer)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000764{
765 struct vmw_framebuffer_dmabuf *vfbd =
766 vmw_framebuffer_to_vfbd(framebuffer);
767
768 drm_framebuffer_cleanup(framebuffer);
769 vmw_dmabuf_unreference(&vfbd->buffer);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200770 ttm_base_object_unref(&vfbd->base.user_obj);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000771
772 kfree(vfbd);
773}
774
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200775static int do_dmabuf_dirty_ldu(struct vmw_private *dev_priv,
776 struct vmw_framebuffer *framebuffer,
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200777 unsigned flags, unsigned color,
778 struct drm_clip_rect *clips,
779 unsigned num_clips, int increment)
780{
781 size_t fifo_size;
782 int i;
783
784 struct {
785 uint32_t header;
786 SVGAFifoCmdUpdate body;
787 } *cmd;
788
789 fifo_size = sizeof(*cmd) * num_clips;
790 cmd = vmw_fifo_reserve(dev_priv, fifo_size);
791 if (unlikely(cmd == NULL)) {
792 DRM_ERROR("Fifo reserve failed.\n");
793 return -ENOMEM;
794 }
795
796 memset(cmd, 0, fifo_size);
797 for (i = 0; i < num_clips; i++, clips += increment) {
798 cmd[i].header = cpu_to_le32(SVGA_CMD_UPDATE);
799 cmd[i].body.x = cpu_to_le32(clips->x1);
800 cmd[i].body.y = cpu_to_le32(clips->y1);
801 cmd[i].body.width = cpu_to_le32(clips->x2 - clips->x1);
802 cmd[i].body.height = cpu_to_le32(clips->y2 - clips->y1);
803 }
804
805 vmw_fifo_commit(dev_priv, fifo_size);
806 return 0;
807}
808
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200809static int do_dmabuf_define_gmrfb(struct drm_file *file_priv,
810 struct vmw_private *dev_priv,
811 struct vmw_framebuffer *framebuffer)
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200812{
Jakob Bornecrantz64fc9942011-10-22 10:29:33 +0200813 int depth = framebuffer->base.depth;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200814 size_t fifo_size;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200815 int ret;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200816
817 struct {
818 uint32_t header;
819 SVGAFifoCmdDefineGMRFB body;
820 } *cmd;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200821
Jakob Bornecrantz64fc9942011-10-22 10:29:33 +0200822 /* Emulate RGBA support, contrary to svga_reg.h this is not
823 * supported by hosts. This is only a problem if we are reading
824 * this value later and expecting what we uploaded back.
825 */
826 if (depth == 32)
827 depth = 24;
828
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200829 fifo_size = sizeof(*cmd);
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200830 cmd = kmalloc(fifo_size, GFP_KERNEL);
831 if (unlikely(cmd == NULL)) {
832 DRM_ERROR("Failed to allocate temporary cmd buffer.\n");
833 return -ENOMEM;
834 }
835
836 memset(cmd, 0, fifo_size);
837 cmd->header = SVGA_CMD_DEFINE_GMRFB;
838 cmd->body.format.bitsPerPixel = framebuffer->base.bits_per_pixel;
Jakob Bornecrantz64fc9942011-10-22 10:29:33 +0200839 cmd->body.format.colorDepth = depth;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200840 cmd->body.format.reserved = 0;
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200841 cmd->body.bytesPerLine = framebuffer->base.pitches[0];
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200842 cmd->body.ptr.gmrId = framebuffer->user_handle;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200843 cmd->body.ptr.offset = 0;
844
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200845 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd,
Jakob Bornecrantzbb1bd2f2012-02-09 16:56:43 +0100846 fifo_size, 0, NULL, NULL);
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200847
848 kfree(cmd);
849
850 return ret;
851}
852
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200853static int do_dmabuf_dirty_sou(struct drm_file *file_priv,
854 struct vmw_private *dev_priv,
855 struct vmw_framebuffer *framebuffer,
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200856 unsigned flags, unsigned color,
857 struct drm_clip_rect *clips,
Jakob Bornecrantzbd49ae42012-02-09 16:56:44 +0100858 unsigned num_clips, int increment,
859 struct vmw_fence_obj **out_fence)
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200860{
861 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
862 struct drm_clip_rect *clips_ptr;
863 int i, k, num_units, ret;
864 struct drm_crtc *crtc;
865 size_t fifo_size;
866
867 struct {
868 uint32_t header;
869 SVGAFifoCmdBlitGMRFBToScreen body;
870 } *blits;
871
872 ret = do_dmabuf_define_gmrfb(file_priv, dev_priv, framebuffer);
873 if (unlikely(ret != 0))
874 return ret; /* define_gmrfb prints warnings */
875
876 fifo_size = sizeof(*blits) * num_clips;
877 blits = kmalloc(fifo_size, GFP_KERNEL);
878 if (unlikely(blits == NULL)) {
879 DRM_ERROR("Failed to allocate temporary cmd buffer.\n");
880 return -ENOMEM;
881 }
882
883 num_units = 0;
884 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
885 if (crtc->fb != &framebuffer->base)
886 continue;
887 units[num_units++] = vmw_crtc_to_du(crtc);
888 }
889
890 for (k = 0; k < num_units; k++) {
891 struct vmw_display_unit *unit = units[k];
892 int hit_num = 0;
893
894 clips_ptr = clips;
895 for (i = 0; i < num_clips; i++, clips_ptr += increment) {
896 int clip_x1 = clips_ptr->x1 - unit->crtc.x;
897 int clip_y1 = clips_ptr->y1 - unit->crtc.y;
898 int clip_x2 = clips_ptr->x2 - unit->crtc.x;
899 int clip_y2 = clips_ptr->y2 - unit->crtc.y;
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100900 int move_x, move_y;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200901
902 /* skip any crtcs that misses the clip region */
903 if (clip_x1 >= unit->crtc.mode.hdisplay ||
904 clip_y1 >= unit->crtc.mode.vdisplay ||
905 clip_x2 <= 0 || clip_y2 <= 0)
906 continue;
907
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100908 /* clip size to crtc size */
909 clip_x2 = min_t(int, clip_x2, unit->crtc.mode.hdisplay);
910 clip_y2 = min_t(int, clip_y2, unit->crtc.mode.vdisplay);
911
912 /* translate both src and dest to bring clip into screen */
913 move_x = min_t(int, clip_x1, 0);
914 move_y = min_t(int, clip_y1, 0);
915
916 /* actual translate done here */
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200917 blits[hit_num].header = SVGA_CMD_BLIT_GMRFB_TO_SCREEN;
918 blits[hit_num].body.destScreenId = unit->unit;
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100919 blits[hit_num].body.srcOrigin.x = clips_ptr->x1 - move_x;
920 blits[hit_num].body.srcOrigin.y = clips_ptr->y1 - move_y;
921 blits[hit_num].body.destRect.left = clip_x1 - move_x;
922 blits[hit_num].body.destRect.top = clip_y1 - move_y;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200923 blits[hit_num].body.destRect.right = clip_x2;
924 blits[hit_num].body.destRect.bottom = clip_y2;
925 hit_num++;
926 }
927
928 /* no clips hit the crtc */
929 if (hit_num == 0)
930 continue;
931
Jakob Bornecrantzbd49ae42012-02-09 16:56:44 +0100932 /* only return the last fence */
933 if (out_fence && *out_fence)
934 vmw_fence_obj_unreference(out_fence);
935
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200936 fifo_size = sizeof(*blits) * hit_num;
937 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, blits,
Jakob Bornecrantzbd49ae42012-02-09 16:56:44 +0100938 fifo_size, 0, NULL, out_fence);
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200939
940 if (unlikely(ret != 0))
941 break;
942 }
943
944 kfree(blits);
945
946 return ret;
947}
948
Rashika Kheria847c5962014-01-06 22:18:10 +0530949static int vmw_framebuffer_dmabuf_dirty(struct drm_framebuffer *framebuffer,
Thomas Hellstrom02b00162010-10-05 12:43:02 +0200950 struct drm_file *file_priv,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000951 unsigned flags, unsigned color,
952 struct drm_clip_rect *clips,
953 unsigned num_clips)
954{
955 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200956 struct vmw_framebuffer_dmabuf *vfbd =
957 vmw_framebuffer_to_vfbd(framebuffer);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000958 struct drm_clip_rect norect;
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200959 int ret, increment = 1;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000960
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200961 drm_modeset_lock_all(dev_priv->dev);
962
Thomas Hellstrom294adf72014-02-27 12:34:51 +0100963 ret = ttm_read_lock(&dev_priv->reservation_sem, true);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200964 if (unlikely(ret != 0)) {
965 drm_modeset_unlock_all(dev_priv->dev);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200966 return ret;
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200967 }
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200968
Thomas Hellstromdf1c93b2010-01-13 22:28:36 +0100969 if (!num_clips) {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000970 num_clips = 1;
971 clips = &norect;
972 norect.x1 = norect.y1 = 0;
973 norect.x2 = framebuffer->width;
974 norect.y2 = framebuffer->height;
975 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
976 num_clips /= 2;
977 increment = 2;
978 }
979
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200980 if (dev_priv->ldu_priv) {
Jakob Bornecrantzc5c42362011-10-22 10:29:34 +0200981 ret = do_dmabuf_dirty_ldu(dev_priv, &vfbd->base,
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200982 flags, color,
983 clips, num_clips, increment);
984 } else {
985 ret = do_dmabuf_dirty_sou(file_priv, dev_priv, &vfbd->base,
Jakob Bornecrantzc5c42362011-10-22 10:29:34 +0200986 flags, color,
Jakob Bornecrantzbd49ae42012-02-09 16:56:44 +0100987 clips, num_clips, increment, NULL);
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200988 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000989
Thomas Hellstrom294adf72014-02-27 12:34:51 +0100990 ttm_read_unlock(&dev_priv->reservation_sem);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200991
992 drm_modeset_unlock_all(dev_priv->dev);
993
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200994 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000995}
996
997static struct drm_framebuffer_funcs vmw_framebuffer_dmabuf_funcs = {
998 .destroy = vmw_framebuffer_dmabuf_destroy,
999 .dirty = vmw_framebuffer_dmabuf_dirty,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001000};
1001
Jakob Bornecrantz497a3ff2011-10-04 20:13:14 +02001002/**
Jakob Bornecrantz497a3ff2011-10-04 20:13:14 +02001003 * Pin the dmabuffer to the start of vram.
1004 */
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001005static int vmw_framebuffer_dmabuf_pin(struct vmw_framebuffer *vfb)
1006{
1007 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
1008 struct vmw_framebuffer_dmabuf *vfbd =
1009 vmw_framebuffer_to_vfbd(&vfb->base);
1010 int ret;
1011
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +02001012 /* This code should not be used with screen objects */
1013 BUG_ON(dev_priv->sou_priv);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001014
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001015 vmw_overlay_pause_all(dev_priv);
1016
Jakob Bornecrantzd991ef02011-10-04 20:13:21 +02001017 ret = vmw_dmabuf_to_start_of_vram(dev_priv, vfbd->buffer, true, false);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001018
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001019 vmw_overlay_resume_all(dev_priv);
1020
Jakob Bornecrantz316ab132010-05-28 11:22:05 +02001021 WARN_ON(ret != 0);
1022
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001023 return 0;
1024}
1025
1026static int vmw_framebuffer_dmabuf_unpin(struct vmw_framebuffer *vfb)
1027{
1028 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
1029 struct vmw_framebuffer_dmabuf *vfbd =
1030 vmw_framebuffer_to_vfbd(&vfb->base);
1031
1032 if (!vfbd->buffer) {
1033 WARN_ON(!vfbd->buffer);
1034 return 0;
1035 }
1036
Jakob Bornecrantzd991ef02011-10-04 20:13:21 +02001037 return vmw_dmabuf_unpin(dev_priv, vfbd->buffer, false);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001038}
1039
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001040static int vmw_kms_new_framebuffer_dmabuf(struct vmw_private *dev_priv,
1041 struct vmw_dma_buffer *dmabuf,
1042 struct vmw_framebuffer **out,
1043 const struct drm_mode_fb_cmd
1044 *mode_cmd)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001045
1046{
1047 struct drm_device *dev = dev_priv->dev;
1048 struct vmw_framebuffer_dmabuf *vfbd;
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001049 unsigned int requested_size;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001050 int ret;
1051
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001052 requested_size = mode_cmd->height * mode_cmd->pitch;
1053 if (unlikely(requested_size > dmabuf->base.num_pages * PAGE_SIZE)) {
1054 DRM_ERROR("Screen buffer object size is too small "
1055 "for requested mode.\n");
1056 return -EINVAL;
1057 }
1058
Jakob Bornecrantzc337ada2011-10-04 20:13:34 +02001059 /* Limited framebuffer color depth support for screen objects */
1060 if (dev_priv->sou_priv) {
1061 switch (mode_cmd->depth) {
1062 case 32:
1063 case 24:
1064 /* Only support 32 bpp for 32 and 24 depth fbs */
1065 if (mode_cmd->bpp == 32)
1066 break;
1067
1068 DRM_ERROR("Invalid color depth/bbp: %d %d\n",
1069 mode_cmd->depth, mode_cmd->bpp);
1070 return -EINVAL;
1071 case 16:
1072 case 15:
1073 /* Only support 16 bpp for 16 and 15 depth fbs */
1074 if (mode_cmd->bpp == 16)
1075 break;
1076
1077 DRM_ERROR("Invalid color depth/bbp: %d %d\n",
1078 mode_cmd->depth, mode_cmd->bpp);
1079 return -EINVAL;
1080 default:
1081 DRM_ERROR("Invalid color depth: %d\n", mode_cmd->depth);
1082 return -EINVAL;
1083 }
1084 }
1085
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001086 vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL);
1087 if (!vfbd) {
1088 ret = -ENOMEM;
1089 goto out_err1;
1090 }
1091
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001092 if (!vmw_dmabuf_reference(dmabuf)) {
1093 DRM_ERROR("failed to reference dmabuf %p\n", dmabuf);
Daniel Vetter80f0b5a2012-12-13 23:39:01 +01001094 ret = -EINVAL;
1095 goto out_err2;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001096 }
1097
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001098 vfbd->base.base.bits_per_pixel = mode_cmd->bpp;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02001099 vfbd->base.base.pitches[0] = mode_cmd->pitch;
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001100 vfbd->base.base.depth = mode_cmd->depth;
1101 vfbd->base.base.width = mode_cmd->width;
1102 vfbd->base.base.height = mode_cmd->height;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +02001103 if (!dev_priv->sou_priv) {
1104 vfbd->base.pin = vmw_framebuffer_dmabuf_pin;
1105 vfbd->base.unpin = vmw_framebuffer_dmabuf_unpin;
1106 }
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001107 vfbd->base.dmabuf = true;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001108 vfbd->buffer = dmabuf;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001109 vfbd->base.user_handle = mode_cmd->handle;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001110 *out = &vfbd->base;
1111
Daniel Vetter80f0b5a2012-12-13 23:39:01 +01001112 ret = drm_framebuffer_init(dev, &vfbd->base.base,
1113 &vmw_framebuffer_dmabuf_funcs);
1114 if (ret)
1115 goto out_err3;
1116
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001117 return 0;
1118
1119out_err3:
Daniel Vetter80f0b5a2012-12-13 23:39:01 +01001120 vmw_dmabuf_unreference(&dmabuf);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001121out_err2:
1122 kfree(vfbd);
1123out_err1:
1124 return ret;
1125}
1126
1127/*
1128 * Generic Kernel modesetting functions
1129 */
1130
1131static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev,
1132 struct drm_file *file_priv,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001133 struct drm_mode_fb_cmd2 *mode_cmd2)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001134{
1135 struct vmw_private *dev_priv = vmw_priv(dev);
1136 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
1137 struct vmw_framebuffer *vfb = NULL;
1138 struct vmw_surface *surface = NULL;
1139 struct vmw_dma_buffer *bo = NULL;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001140 struct ttm_base_object *user_obj;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001141 struct drm_mode_fb_cmd mode_cmd;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001142 int ret;
1143
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001144 mode_cmd.width = mode_cmd2->width;
1145 mode_cmd.height = mode_cmd2->height;
1146 mode_cmd.pitch = mode_cmd2->pitches[0];
1147 mode_cmd.handle = mode_cmd2->handles[0];
Dave Airlie248dbc22011-11-29 20:02:54 +00001148 drm_fb_get_bpp_depth(mode_cmd2->pixel_format, &mode_cmd.depth,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001149 &mode_cmd.bpp);
1150
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001151 /**
1152 * This code should be conditioned on Screen Objects not being used.
1153 * If screen objects are used, we can allocate a GMR to hold the
1154 * requested framebuffer.
1155 */
1156
Xi Wang8a783892011-12-21 05:18:33 -05001157 if (!vmw_kms_validate_mode_vram(dev_priv,
Linus Torvalds1a464cb2012-01-10 11:04:36 -08001158 mode_cmd.pitch,
1159 mode_cmd.height)) {
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001160 DRM_ERROR("VRAM size is too small for requested mode.\n");
Jakob Bornecrantzd9826402011-11-03 21:03:04 +01001161 return ERR_PTR(-ENOMEM);
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001162 }
1163
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001164 /*
1165 * Take a reference on the user object of the resource
1166 * backing the kms fb. This ensures that user-space handle
1167 * lookups on that resource will always work as long as
1168 * it's registered with a kms framebuffer. This is important,
1169 * since vmw_execbuf_process identifies resources in the
1170 * command stream using user-space handles.
1171 */
1172
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001173 user_obj = ttm_base_object_lookup(tfile, mode_cmd.handle);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001174 if (unlikely(user_obj == NULL)) {
1175 DRM_ERROR("Could not locate requested kms frame buffer.\n");
1176 return ERR_PTR(-ENOENT);
1177 }
1178
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001179 /**
1180 * End conditioned code.
1181 */
1182
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +01001183 /* returns either a dmabuf or surface */
1184 ret = vmw_user_lookup_handle(dev_priv, tfile,
Dave Airlie4cf73122011-12-21 09:50:56 +00001185 mode_cmd.handle,
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +01001186 &surface, &bo);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001187 if (ret)
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +01001188 goto err_out;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001189
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +01001190 /* Create the new framebuffer depending one what we got back */
1191 if (bo)
1192 ret = vmw_kms_new_framebuffer_dmabuf(dev_priv, bo, &vfb,
Dave Airlie4cf73122011-12-21 09:50:56 +00001193 &mode_cmd);
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +01001194 else if (surface)
1195 ret = vmw_kms_new_framebuffer_surface(dev_priv, file_priv,
Dave Airlie4cf73122011-12-21 09:50:56 +00001196 surface, &vfb, &mode_cmd);
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +01001197 else
1198 BUG();
Jakob Bornecrantz5ffdb652010-01-30 03:38:08 +00001199
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +01001200err_out:
1201 /* vmw_user_lookup_handle takes one ref so does new_fb */
1202 if (bo)
1203 vmw_dmabuf_unreference(&bo);
1204 if (surface)
1205 vmw_surface_unreference(&surface);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001206
1207 if (ret) {
1208 DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001209 ttm_base_object_unref(&user_obj);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01001210 return ERR_PTR(ret);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001211 } else
1212 vfb->user_obj = user_obj;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001213
1214 return &vfb->base;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001215}
1216
Laurent Pincharte6ecefa2012-05-17 13:27:23 +02001217static const struct drm_mode_config_funcs vmw_kms_funcs = {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001218 .fb_create = vmw_kms_fb_create,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001219};
1220
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001221int vmw_kms_present(struct vmw_private *dev_priv,
1222 struct drm_file *file_priv,
1223 struct vmw_framebuffer *vfb,
1224 struct vmw_surface *surface,
1225 uint32_t sid,
1226 int32_t destX, int32_t destY,
1227 struct drm_vmw_rect *clips,
1228 uint32_t num_clips)
1229{
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001230 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +01001231 struct drm_clip_rect *tmp;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001232 struct drm_crtc *crtc;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001233 size_t fifo_size;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001234 int i, k, num_units;
1235 int ret = 0; /* silence warning */
Jakob Bornecrantz203dc222011-11-28 13:19:13 +01001236 int left, right, top, bottom;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001237
1238 struct {
1239 SVGA3dCmdHeader header;
1240 SVGA3dCmdBlitSurfaceToScreen body;
1241 } *cmd;
1242 SVGASignedRect *blits;
1243
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001244 num_units = 0;
1245 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
1246 if (crtc->fb != &vfb->base)
1247 continue;
1248 units[num_units++] = vmw_crtc_to_du(crtc);
1249 }
1250
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001251 BUG_ON(surface == NULL);
1252 BUG_ON(!clips || !num_clips);
1253
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +01001254 tmp = kzalloc(sizeof(*tmp) * num_clips, GFP_KERNEL);
1255 if (unlikely(tmp == NULL)) {
1256 DRM_ERROR("Temporary cliprect memory alloc failed.\n");
1257 return -ENOMEM;
1258 }
1259
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001260 fifo_size = sizeof(*cmd) + sizeof(SVGASignedRect) * num_clips;
1261 cmd = kmalloc(fifo_size, GFP_KERNEL);
1262 if (unlikely(cmd == NULL)) {
1263 DRM_ERROR("Failed to allocate temporary fifo memory.\n");
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +01001264 ret = -ENOMEM;
1265 goto out_free_tmp;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001266 }
1267
Jakob Bornecrantz203dc222011-11-28 13:19:13 +01001268 left = clips->x;
1269 right = clips->x + clips->w;
1270 top = clips->y;
1271 bottom = clips->y + clips->h;
1272
1273 for (i = 1; i < num_clips; i++) {
1274 left = min_t(int, left, (int)clips[i].x);
1275 right = max_t(int, right, (int)clips[i].x + clips[i].w);
1276 top = min_t(int, top, (int)clips[i].y);
1277 bottom = max_t(int, bottom, (int)clips[i].y + clips[i].h);
1278 }
1279
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001280 /* only need to do this once */
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001281 memset(cmd, 0, fifo_size);
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001282 cmd->header.id = cpu_to_le32(SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN);
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +01001283
1284 blits = (SVGASignedRect *)&cmd[1];
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001285
Jakob Bornecrantz203dc222011-11-28 13:19:13 +01001286 cmd->body.srcRect.left = left;
1287 cmd->body.srcRect.right = right;
1288 cmd->body.srcRect.top = top;
1289 cmd->body.srcRect.bottom = bottom;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001290
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001291 for (i = 0; i < num_clips; i++) {
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +01001292 tmp[i].x1 = clips[i].x - left;
1293 tmp[i].x2 = clips[i].x + clips[i].w - left;
1294 tmp[i].y1 = clips[i].y - top;
1295 tmp[i].y2 = clips[i].y + clips[i].h - top;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001296 }
1297
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001298 for (k = 0; k < num_units; k++) {
1299 struct vmw_display_unit *unit = units[k];
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +01001300 struct vmw_clip_rect clip;
1301 int num;
1302
1303 clip.x1 = left + destX - unit->crtc.x;
1304 clip.y1 = top + destY - unit->crtc.y;
1305 clip.x2 = right + destX - unit->crtc.x;
1306 clip.y2 = bottom + destY - unit->crtc.y;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001307
1308 /* skip any crtcs that misses the clip region */
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +01001309 if (clip.x1 >= unit->crtc.mode.hdisplay ||
1310 clip.y1 >= unit->crtc.mode.vdisplay ||
1311 clip.x2 <= 0 || clip.y2 <= 0)
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001312 continue;
1313
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +01001314 /*
1315 * In order for the clip rects to be correctly scaled
1316 * the src and dest rects needs to be the same size.
1317 */
1318 cmd->body.destRect.left = clip.x1;
1319 cmd->body.destRect.right = clip.x2;
1320 cmd->body.destRect.top = clip.y1;
1321 cmd->body.destRect.bottom = clip.y2;
1322
1323 /* create a clip rect of the crtc in dest coords */
1324 clip.x2 = unit->crtc.mode.hdisplay - clip.x1;
1325 clip.y2 = unit->crtc.mode.vdisplay - clip.y1;
1326 clip.x1 = 0 - clip.x1;
1327 clip.y1 = 0 - clip.y1;
1328
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001329 /* need to reset sid as it is changed by execbuf */
1330 cmd->body.srcImage.sid = sid;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001331 cmd->body.destScreenId = unit->unit;
1332
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +01001333 /* clip and write blits to cmd stream */
1334 vmw_clip_cliprects(tmp, num_clips, clip, blits, &num);
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001335
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +01001336 /* if no cliprects hit skip this */
1337 if (num == 0)
1338 continue;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001339
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +01001340 /* recalculate package length */
1341 fifo_size = sizeof(*cmd) + sizeof(SVGASignedRect) * num;
1342 cmd->header.size = cpu_to_le32(fifo_size - sizeof(cmd->header));
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001343 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd,
Jakob Bornecrantzbb1bd2f2012-02-09 16:56:43 +01001344 fifo_size, 0, NULL, NULL);
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001345
1346 if (unlikely(ret != 0))
1347 break;
1348 }
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001349
1350 kfree(cmd);
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +01001351out_free_tmp:
1352 kfree(tmp);
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001353
1354 return ret;
1355}
1356
1357int vmw_kms_readback(struct vmw_private *dev_priv,
1358 struct drm_file *file_priv,
1359 struct vmw_framebuffer *vfb,
1360 struct drm_vmw_fence_rep __user *user_fence_rep,
1361 struct drm_vmw_rect *clips,
1362 uint32_t num_clips)
1363{
1364 struct vmw_framebuffer_dmabuf *vfbd =
1365 vmw_framebuffer_to_vfbd(&vfb->base);
1366 struct vmw_dma_buffer *dmabuf = vfbd->buffer;
1367 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
1368 struct drm_crtc *crtc;
1369 size_t fifo_size;
1370 int i, k, ret, num_units, blits_pos;
1371
1372 struct {
1373 uint32_t header;
1374 SVGAFifoCmdDefineGMRFB body;
1375 } *cmd;
1376 struct {
1377 uint32_t header;
1378 SVGAFifoCmdBlitScreenToGMRFB body;
1379 } *blits;
1380
1381 num_units = 0;
1382 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
1383 if (crtc->fb != &vfb->base)
1384 continue;
1385 units[num_units++] = vmw_crtc_to_du(crtc);
1386 }
1387
1388 BUG_ON(dmabuf == NULL);
1389 BUG_ON(!clips || !num_clips);
1390
1391 /* take a safe guess at fifo size */
1392 fifo_size = sizeof(*cmd) + sizeof(*blits) * num_clips * num_units;
1393 cmd = kmalloc(fifo_size, GFP_KERNEL);
1394 if (unlikely(cmd == NULL)) {
1395 DRM_ERROR("Failed to allocate temporary fifo memory.\n");
1396 return -ENOMEM;
1397 }
1398
1399 memset(cmd, 0, fifo_size);
1400 cmd->header = SVGA_CMD_DEFINE_GMRFB;
1401 cmd->body.format.bitsPerPixel = vfb->base.bits_per_pixel;
1402 cmd->body.format.colorDepth = vfb->base.depth;
1403 cmd->body.format.reserved = 0;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02001404 cmd->body.bytesPerLine = vfb->base.pitches[0];
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001405 cmd->body.ptr.gmrId = vfb->user_handle;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001406 cmd->body.ptr.offset = 0;
1407
1408 blits = (void *)&cmd[1];
1409 blits_pos = 0;
1410 for (i = 0; i < num_units; i++) {
1411 struct drm_vmw_rect *c = clips;
1412 for (k = 0; k < num_clips; k++, c++) {
1413 /* transform clip coords to crtc origin based coords */
1414 int clip_x1 = c->x - units[i]->crtc.x;
1415 int clip_x2 = c->x - units[i]->crtc.x + c->w;
1416 int clip_y1 = c->y - units[i]->crtc.y;
1417 int clip_y2 = c->y - units[i]->crtc.y + c->h;
1418 int dest_x = c->x;
1419 int dest_y = c->y;
1420
1421 /* compensate for clipping, we negate
1422 * a negative number and add that.
1423 */
1424 if (clip_x1 < 0)
1425 dest_x += -clip_x1;
1426 if (clip_y1 < 0)
1427 dest_y += -clip_y1;
1428
1429 /* clip */
1430 clip_x1 = max(clip_x1, 0);
1431 clip_y1 = max(clip_y1, 0);
1432 clip_x2 = min(clip_x2, units[i]->crtc.mode.hdisplay);
1433 clip_y2 = min(clip_y2, units[i]->crtc.mode.vdisplay);
1434
1435 /* and cull any rects that misses the crtc */
1436 if (clip_x1 >= units[i]->crtc.mode.hdisplay ||
1437 clip_y1 >= units[i]->crtc.mode.vdisplay ||
1438 clip_x2 <= 0 || clip_y2 <= 0)
1439 continue;
1440
1441 blits[blits_pos].header = SVGA_CMD_BLIT_SCREEN_TO_GMRFB;
1442 blits[blits_pos].body.srcScreenId = units[i]->unit;
1443 blits[blits_pos].body.destOrigin.x = dest_x;
1444 blits[blits_pos].body.destOrigin.y = dest_y;
1445
1446 blits[blits_pos].body.srcRect.left = clip_x1;
1447 blits[blits_pos].body.srcRect.top = clip_y1;
1448 blits[blits_pos].body.srcRect.right = clip_x2;
1449 blits[blits_pos].body.srcRect.bottom = clip_y2;
1450 blits_pos++;
1451 }
1452 }
1453 /* reset size here and use calculated exact size from loops */
1454 fifo_size = sizeof(*cmd) + sizeof(*blits) * blits_pos;
1455
1456 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd, fifo_size,
Jakob Bornecrantzbb1bd2f2012-02-09 16:56:43 +01001457 0, user_fence_rep, NULL);
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001458
1459 kfree(cmd);
1460
1461 return ret;
1462}
1463
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001464int vmw_kms_init(struct vmw_private *dev_priv)
1465{
1466 struct drm_device *dev = dev_priv->dev;
1467 int ret;
1468
1469 drm_mode_config_init(dev);
1470 dev->mode_config.funcs = &vmw_kms_funcs;
Jakob Bornecrantz3bef3572010-02-09 19:41:57 +00001471 dev->mode_config.min_width = 1;
1472 dev->mode_config.min_height = 1;
Jakob Bornecrantz7e71f8a2010-05-28 11:21:54 +02001473 /* assumed largest fb size */
1474 dev->mode_config.max_width = 8192;
1475 dev->mode_config.max_height = 8192;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001476
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +02001477 ret = vmw_kms_init_screen_object_display(dev_priv);
1478 if (ret) /* Fallback */
1479 (void)vmw_kms_init_legacy_display_system(dev_priv);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001480
1481 return 0;
1482}
1483
1484int vmw_kms_close(struct vmw_private *dev_priv)
1485{
1486 /*
1487 * Docs says we should take the lock before calling this function
1488 * but since it destroys encoders and our destructor calls
1489 * drm_encoder_cleanup which takes the lock we deadlock.
1490 */
1491 drm_mode_config_cleanup(dev_priv->dev);
Jakob Bornecrantzc0d18312011-11-09 10:25:26 +01001492 if (dev_priv->sou_priv)
1493 vmw_kms_close_screen_object_display(dev_priv);
1494 else
1495 vmw_kms_close_legacy_display_system(dev_priv);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001496 return 0;
1497}
1498
1499int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
1500 struct drm_file *file_priv)
1501{
1502 struct drm_vmw_cursor_bypass_arg *arg = data;
1503 struct vmw_display_unit *du;
1504 struct drm_mode_object *obj;
1505 struct drm_crtc *crtc;
1506 int ret = 0;
1507
1508
1509 mutex_lock(&dev->mode_config.mutex);
1510 if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
1511
1512 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1513 du = vmw_crtc_to_du(crtc);
1514 du->hotspot_x = arg->xhot;
1515 du->hotspot_y = arg->yhot;
1516 }
1517
1518 mutex_unlock(&dev->mode_config.mutex);
1519 return 0;
1520 }
1521
1522 obj = drm_mode_object_find(dev, arg->crtc_id, DRM_MODE_OBJECT_CRTC);
1523 if (!obj) {
Ville Syrjälä4ae87ff2013-10-17 13:35:05 +03001524 ret = -ENOENT;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001525 goto out;
1526 }
1527
1528 crtc = obj_to_crtc(obj);
1529 du = vmw_crtc_to_du(crtc);
1530
1531 du->hotspot_x = arg->xhot;
1532 du->hotspot_y = arg->yhot;
1533
1534out:
1535 mutex_unlock(&dev->mode_config.mutex);
1536
1537 return ret;
1538}
1539
Michel Dänzer0bef23f2011-08-31 07:42:50 +00001540int vmw_kms_write_svga(struct vmw_private *vmw_priv,
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001541 unsigned width, unsigned height, unsigned pitch,
Michel Dänzer6558429b2011-08-31 07:42:49 +00001542 unsigned bpp, unsigned depth)
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001543{
1544 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1545 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch);
1546 else if (vmw_fifo_have_pitchlock(vmw_priv))
1547 iowrite32(pitch, vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
1548 vmw_write(vmw_priv, SVGA_REG_WIDTH, width);
1549 vmw_write(vmw_priv, SVGA_REG_HEIGHT, height);
Michel Dänzer6558429b2011-08-31 07:42:49 +00001550 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp);
Michel Dänzer0bef23f2011-08-31 07:42:50 +00001551
1552 if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) {
1553 DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n",
1554 depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH));
1555 return -EINVAL;
1556 }
1557
1558 return 0;
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001559}
1560
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001561int vmw_kms_save_vga(struct vmw_private *vmw_priv)
1562{
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001563 struct vmw_vga_topology_state *save;
1564 uint32_t i;
1565
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001566 vmw_priv->vga_width = vmw_read(vmw_priv, SVGA_REG_WIDTH);
1567 vmw_priv->vga_height = vmw_read(vmw_priv, SVGA_REG_HEIGHT);
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001568 vmw_priv->vga_bpp = vmw_read(vmw_priv, SVGA_REG_BITS_PER_PIXEL);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001569 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1570 vmw_priv->vga_pitchlock =
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001571 vmw_read(vmw_priv, SVGA_REG_PITCHLOCK);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001572 else if (vmw_fifo_have_pitchlock(vmw_priv))
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001573 vmw_priv->vga_pitchlock = ioread32(vmw_priv->mmio_virt +
1574 SVGA_FIFO_PITCHLOCK);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001575
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001576 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1577 return 0;
1578
1579 vmw_priv->num_displays = vmw_read(vmw_priv,
1580 SVGA_REG_NUM_GUEST_DISPLAYS);
1581
Thomas Hellstrom029e50b2010-10-05 12:43:08 +02001582 if (vmw_priv->num_displays == 0)
1583 vmw_priv->num_displays = 1;
1584
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001585 for (i = 0; i < vmw_priv->num_displays; ++i) {
1586 save = &vmw_priv->vga_save[i];
1587 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1588 save->primary = vmw_read(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY);
1589 save->pos_x = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_X);
1590 save->pos_y = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y);
1591 save->width = vmw_read(vmw_priv, SVGA_REG_DISPLAY_WIDTH);
1592 save->height = vmw_read(vmw_priv, SVGA_REG_DISPLAY_HEIGHT);
1593 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
Thomas Hellstrom30c78bb2010-10-01 10:21:48 +02001594 if (i == 0 && vmw_priv->num_displays == 1 &&
1595 save->width == 0 && save->height == 0) {
1596
1597 /*
1598 * It should be fairly safe to assume that these
1599 * values are uninitialized.
1600 */
1601
1602 save->width = vmw_priv->vga_width - save->pos_x;
1603 save->height = vmw_priv->vga_height - save->pos_y;
1604 }
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001605 }
Thomas Hellstrom30c78bb2010-10-01 10:21:48 +02001606
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001607 return 0;
1608}
1609
1610int vmw_kms_restore_vga(struct vmw_private *vmw_priv)
1611{
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001612 struct vmw_vga_topology_state *save;
1613 uint32_t i;
1614
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001615 vmw_write(vmw_priv, SVGA_REG_WIDTH, vmw_priv->vga_width);
1616 vmw_write(vmw_priv, SVGA_REG_HEIGHT, vmw_priv->vga_height);
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001617 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, vmw_priv->vga_bpp);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001618 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1619 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK,
1620 vmw_priv->vga_pitchlock);
1621 else if (vmw_fifo_have_pitchlock(vmw_priv))
1622 iowrite32(vmw_priv->vga_pitchlock,
1623 vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001624
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001625 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1626 return 0;
1627
1628 for (i = 0; i < vmw_priv->num_displays; ++i) {
1629 save = &vmw_priv->vga_save[i];
1630 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1631 vmw_write(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY, save->primary);
1632 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_X, save->pos_x);
1633 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y, save->pos_y);
1634 vmw_write(vmw_priv, SVGA_REG_DISPLAY_WIDTH, save->width);
1635 vmw_write(vmw_priv, SVGA_REG_DISPLAY_HEIGHT, save->height);
1636 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
1637 }
1638
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001639 return 0;
1640}
Jakob Bornecrantzd8bd19d2010-06-01 11:54:20 +02001641
Thomas Hellstrome133e7372010-10-05 12:43:04 +02001642bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
1643 uint32_t pitch,
1644 uint32_t height)
1645{
Thomas Hellstrombc2d6502012-11-21 10:32:36 +01001646 return ((u64) pitch * (u64) height) < (u64) dev_priv->prim_bb_mem;
Thomas Hellstrome133e7372010-10-05 12:43:04 +02001647}
1648
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001649
1650/**
1651 * Function called by DRM code called with vbl_lock held.
1652 */
Thomas Hellstrom7a1c2f62010-10-01 10:21:49 +02001653u32 vmw_get_vblank_counter(struct drm_device *dev, int crtc)
1654{
1655 return 0;
1656}
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001657
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001658/**
1659 * Function called by DRM code called with vbl_lock held.
1660 */
1661int vmw_enable_vblank(struct drm_device *dev, int crtc)
1662{
1663 return -ENOSYS;
1664}
1665
1666/**
1667 * Function called by DRM code called with vbl_lock held.
1668 */
1669void vmw_disable_vblank(struct drm_device *dev, int crtc)
1670{
1671}
1672
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001673
1674/*
1675 * Small shared kms functions.
1676 */
1677
Rashika Kheria847c5962014-01-06 22:18:10 +05301678static int vmw_du_update_layout(struct vmw_private *dev_priv, unsigned num,
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001679 struct drm_vmw_rect *rects)
1680{
1681 struct drm_device *dev = dev_priv->dev;
1682 struct vmw_display_unit *du;
1683 struct drm_connector *con;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001684
1685 mutex_lock(&dev->mode_config.mutex);
1686
1687#if 0
Thomas Hellstrom6ea77d12011-10-04 20:13:36 +02001688 {
1689 unsigned int i;
1690
1691 DRM_INFO("%s: new layout ", __func__);
1692 for (i = 0; i < num; i++)
1693 DRM_INFO("(%i, %i %ux%u) ", rects[i].x, rects[i].y,
1694 rects[i].w, rects[i].h);
1695 DRM_INFO("\n");
1696 }
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001697#endif
1698
1699 list_for_each_entry(con, &dev->mode_config.connector_list, head) {
1700 du = vmw_connector_to_du(con);
1701 if (num > du->unit) {
1702 du->pref_width = rects[du->unit].w;
1703 du->pref_height = rects[du->unit].h;
1704 du->pref_active = true;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001705 du->gui_x = rects[du->unit].x;
1706 du->gui_y = rects[du->unit].y;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001707 } else {
1708 du->pref_width = 800;
1709 du->pref_height = 600;
1710 du->pref_active = false;
1711 }
1712 con->status = vmw_du_connector_detect(con, true);
1713 }
1714
1715 mutex_unlock(&dev->mode_config.mutex);
1716
1717 return 0;
1718}
1719
Jakob Bornecrantzb5ec4272012-02-09 16:56:45 +01001720int vmw_du_page_flip(struct drm_crtc *crtc,
1721 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07001722 struct drm_pending_vblank_event *event,
1723 uint32_t page_flip_flags)
Jakob Bornecrantzb5ec4272012-02-09 16:56:45 +01001724{
1725 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
1726 struct drm_framebuffer *old_fb = crtc->fb;
1727 struct vmw_framebuffer *vfb = vmw_framebuffer_to_vfb(fb);
Alan Coxf5869a82012-08-20 14:44:52 +00001728 struct drm_file *file_priv ;
Jakob Bornecrantzb5ec4272012-02-09 16:56:45 +01001729 struct vmw_fence_obj *fence = NULL;
1730 struct drm_clip_rect clips;
1731 int ret;
1732
Alan Coxf5869a82012-08-20 14:44:52 +00001733 if (event == NULL)
1734 return -EINVAL;
1735
Jakob Bornecrantzb5ec4272012-02-09 16:56:45 +01001736 /* require ScreenObject support for page flipping */
1737 if (!dev_priv->sou_priv)
1738 return -ENOSYS;
1739
Alan Coxf5869a82012-08-20 14:44:52 +00001740 file_priv = event->base.file_priv;
Jakob Bornecrantzb5ec4272012-02-09 16:56:45 +01001741 if (!vmw_kms_screen_object_flippable(dev_priv, crtc))
1742 return -EINVAL;
1743
1744 crtc->fb = fb;
1745
1746 /* do a full screen dirty update */
1747 clips.x1 = clips.y1 = 0;
1748 clips.x2 = fb->width;
1749 clips.y2 = fb->height;
1750
1751 if (vfb->dmabuf)
1752 ret = do_dmabuf_dirty_sou(file_priv, dev_priv, vfb,
1753 0, 0, &clips, 1, 1, &fence);
1754 else
1755 ret = do_surface_dirty_sou(dev_priv, file_priv, vfb,
1756 0, 0, &clips, 1, 1, &fence);
1757
1758
1759 if (ret != 0)
1760 goto out_no_fence;
1761 if (!fence) {
1762 ret = -EINVAL;
1763 goto out_no_fence;
1764 }
1765
1766 ret = vmw_event_fence_action_queue(file_priv, fence,
1767 &event->base,
1768 &event->event.tv_sec,
1769 &event->event.tv_usec,
1770 true);
1771
1772 /*
1773 * No need to hold on to this now. The only cleanup
1774 * we need to do if we fail is unref the fence.
1775 */
1776 vmw_fence_obj_unreference(&fence);
1777
1778 if (vmw_crtc_to_du(crtc)->is_implicit)
1779 vmw_kms_screen_object_update_implicit_fb(dev_priv, crtc);
1780
1781 return ret;
1782
1783out_no_fence:
1784 crtc->fb = old_fb;
1785 return ret;
1786}
1787
1788
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001789void vmw_du_crtc_save(struct drm_crtc *crtc)
1790{
1791}
1792
1793void vmw_du_crtc_restore(struct drm_crtc *crtc)
1794{
1795}
1796
1797void vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
1798 u16 *r, u16 *g, u16 *b,
1799 uint32_t start, uint32_t size)
1800{
1801 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
1802 int i;
1803
1804 for (i = 0; i < size; i++) {
1805 DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i,
1806 r[i], g[i], b[i]);
1807 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 0, r[i] >> 8);
1808 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 1, g[i] >> 8);
1809 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 2, b[i] >> 8);
1810 }
1811}
1812
1813void vmw_du_connector_dpms(struct drm_connector *connector, int mode)
1814{
1815}
1816
1817void vmw_du_connector_save(struct drm_connector *connector)
1818{
1819}
1820
1821void vmw_du_connector_restore(struct drm_connector *connector)
1822{
1823}
1824
1825enum drm_connector_status
1826vmw_du_connector_detect(struct drm_connector *connector, bool force)
1827{
1828 uint32_t num_displays;
1829 struct drm_device *dev = connector->dev;
1830 struct vmw_private *dev_priv = vmw_priv(dev);
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001831 struct vmw_display_unit *du = vmw_connector_to_du(connector);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001832
1833 mutex_lock(&dev_priv->hw_mutex);
1834 num_displays = vmw_read(dev_priv, SVGA_REG_NUM_DISPLAYS);
1835 mutex_unlock(&dev_priv->hw_mutex);
1836
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001837 return ((vmw_connector_to_du(connector)->unit < num_displays &&
1838 du->pref_active) ?
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001839 connector_status_connected : connector_status_disconnected);
1840}
1841
1842static struct drm_display_mode vmw_kms_connector_builtin[] = {
1843 /* 640x480@60Hz */
1844 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
1845 752, 800, 0, 480, 489, 492, 525, 0,
1846 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1847 /* 800x600@60Hz */
1848 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
1849 968, 1056, 0, 600, 601, 605, 628, 0,
1850 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1851 /* 1024x768@60Hz */
1852 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
1853 1184, 1344, 0, 768, 771, 777, 806, 0,
1854 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1855 /* 1152x864@75Hz */
1856 { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
1857 1344, 1600, 0, 864, 865, 868, 900, 0,
1858 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1859 /* 1280x768@60Hz */
1860 { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344,
1861 1472, 1664, 0, 768, 771, 778, 798, 0,
1862 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1863 /* 1280x800@60Hz */
1864 { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352,
1865 1480, 1680, 0, 800, 803, 809, 831, 0,
1866 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
1867 /* 1280x960@60Hz */
1868 { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376,
1869 1488, 1800, 0, 960, 961, 964, 1000, 0,
1870 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1871 /* 1280x1024@60Hz */
1872 { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328,
1873 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
1874 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1875 /* 1360x768@60Hz */
1876 { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424,
1877 1536, 1792, 0, 768, 771, 777, 795, 0,
1878 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1879 /* 1440x1050@60Hz */
1880 { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488,
1881 1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
1882 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1883 /* 1440x900@60Hz */
1884 { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520,
1885 1672, 1904, 0, 900, 903, 909, 934, 0,
1886 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1887 /* 1600x1200@60Hz */
1888 { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664,
1889 1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
1890 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1891 /* 1680x1050@60Hz */
1892 { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784,
1893 1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
1894 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1895 /* 1792x1344@60Hz */
1896 { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920,
1897 2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
1898 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1899 /* 1853x1392@60Hz */
1900 { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952,
1901 2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
1902 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1903 /* 1920x1200@60Hz */
1904 { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056,
1905 2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
1906 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1907 /* 1920x1440@60Hz */
1908 { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048,
1909 2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
1910 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1911 /* 2560x1600@60Hz */
1912 { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752,
1913 3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
1914 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1915 /* Terminate */
1916 { DRM_MODE("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) },
1917};
1918
Thomas Hellstrom1543b4dd2011-11-02 09:43:10 +01001919/**
1920 * vmw_guess_mode_timing - Provide fake timings for a
1921 * 60Hz vrefresh mode.
1922 *
1923 * @mode - Pointer to a struct drm_display_mode with hdisplay and vdisplay
1924 * members filled in.
1925 */
1926static void vmw_guess_mode_timing(struct drm_display_mode *mode)
1927{
1928 mode->hsync_start = mode->hdisplay + 50;
1929 mode->hsync_end = mode->hsync_start + 50;
1930 mode->htotal = mode->hsync_end + 50;
1931
1932 mode->vsync_start = mode->vdisplay + 50;
1933 mode->vsync_end = mode->vsync_start + 50;
1934 mode->vtotal = mode->vsync_end + 50;
1935
1936 mode->clock = (u32)mode->htotal * (u32)mode->vtotal / 100 * 6;
1937 mode->vrefresh = drm_mode_vrefresh(mode);
1938}
1939
1940
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001941int vmw_du_connector_fill_modes(struct drm_connector *connector,
1942 uint32_t max_width, uint32_t max_height)
1943{
1944 struct vmw_display_unit *du = vmw_connector_to_du(connector);
1945 struct drm_device *dev = connector->dev;
1946 struct vmw_private *dev_priv = vmw_priv(dev);
1947 struct drm_display_mode *mode = NULL;
1948 struct drm_display_mode *bmode;
1949 struct drm_display_mode prefmode = { DRM_MODE("preferred",
1950 DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
1951 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1952 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
1953 };
1954 int i;
1955
1956 /* Add preferred mode */
1957 {
1958 mode = drm_mode_duplicate(dev, &prefmode);
1959 if (!mode)
1960 return 0;
1961 mode->hdisplay = du->pref_width;
1962 mode->vdisplay = du->pref_height;
Thomas Hellstrom1543b4dd2011-11-02 09:43:10 +01001963 vmw_guess_mode_timing(mode);
Jakob Bornecrantz55bde5b2011-11-03 21:03:05 +01001964
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001965 if (vmw_kms_validate_mode_vram(dev_priv, mode->hdisplay * 2,
1966 mode->vdisplay)) {
1967 drm_mode_probed_add(connector, mode);
Jakob Bornecrantz55bde5b2011-11-03 21:03:05 +01001968 } else {
1969 drm_mode_destroy(dev, mode);
1970 mode = NULL;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001971 }
Jakob Bornecrantz55bde5b2011-11-03 21:03:05 +01001972
1973 if (du->pref_mode) {
1974 list_del_init(&du->pref_mode->head);
1975 drm_mode_destroy(dev, du->pref_mode);
1976 }
1977
1978 /* mode might be null here, this is intended */
1979 du->pref_mode = mode;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001980 }
1981
1982 for (i = 0; vmw_kms_connector_builtin[i].type != 0; i++) {
1983 bmode = &vmw_kms_connector_builtin[i];
1984 if (bmode->hdisplay > max_width ||
1985 bmode->vdisplay > max_height)
1986 continue;
1987
1988 if (!vmw_kms_validate_mode_vram(dev_priv, bmode->hdisplay * 2,
1989 bmode->vdisplay))
1990 continue;
1991
1992 mode = drm_mode_duplicate(dev, bmode);
1993 if (!mode)
1994 return 0;
1995 mode->vrefresh = drm_mode_vrefresh(mode);
1996
1997 drm_mode_probed_add(connector, mode);
1998 }
1999
Jakob Bornecrantzd41025c2011-11-03 21:03:07 +01002000 /* Move the prefered mode first, help apps pick the right mode. */
2001 if (du->pref_mode)
2002 list_move(&du->pref_mode->head, &connector->probed_modes);
2003
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002004 drm_mode_connector_list_update(connector);
2005
2006 return 1;
2007}
2008
2009int vmw_du_connector_set_property(struct drm_connector *connector,
2010 struct drm_property *property,
2011 uint64_t val)
2012{
2013 return 0;
2014}
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002015
2016
2017int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data,
2018 struct drm_file *file_priv)
2019{
2020 struct vmw_private *dev_priv = vmw_priv(dev);
2021 struct drm_vmw_update_layout_arg *arg =
2022 (struct drm_vmw_update_layout_arg *)data;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002023 void __user *user_rects;
2024 struct drm_vmw_rect *rects;
2025 unsigned rects_size;
2026 int ret;
2027 int i;
2028 struct drm_mode_config *mode_config = &dev->mode_config;
2029
Thomas Hellstrom294adf72014-02-27 12:34:51 +01002030 ret = ttm_read_lock(&dev_priv->reservation_sem, true);
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002031 if (unlikely(ret != 0))
2032 return ret;
2033
2034 if (!arg->num_outputs) {
2035 struct drm_vmw_rect def_rect = {0, 0, 800, 600};
2036 vmw_du_update_layout(dev_priv, 1, &def_rect);
2037 goto out_unlock;
2038 }
2039
2040 rects_size = arg->num_outputs * sizeof(struct drm_vmw_rect);
Xi Wangbab9efc2011-11-28 12:25:43 +01002041 rects = kcalloc(arg->num_outputs, sizeof(struct drm_vmw_rect),
2042 GFP_KERNEL);
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002043 if (unlikely(!rects)) {
2044 ret = -ENOMEM;
2045 goto out_unlock;
2046 }
2047
2048 user_rects = (void __user *)(unsigned long)arg->rects;
2049 ret = copy_from_user(rects, user_rects, rects_size);
2050 if (unlikely(ret != 0)) {
2051 DRM_ERROR("Failed to get rects.\n");
2052 ret = -EFAULT;
2053 goto out_free;
2054 }
2055
2056 for (i = 0; i < arg->num_outputs; ++i) {
Xi Wangbab9efc2011-11-28 12:25:43 +01002057 if (rects[i].x < 0 ||
2058 rects[i].y < 0 ||
2059 rects[i].x + rects[i].w > mode_config->max_width ||
2060 rects[i].y + rects[i].h > mode_config->max_height) {
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002061 DRM_ERROR("Invalid GUI layout.\n");
2062 ret = -EINVAL;
2063 goto out_free;
2064 }
2065 }
2066
2067 vmw_du_update_layout(dev_priv, arg->num_outputs, rects);
2068
2069out_free:
2070 kfree(rects);
2071out_unlock:
Thomas Hellstrom294adf72014-02-27 12:34:51 +01002072 ttm_read_unlock(&dev_priv->reservation_sem);
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002073 return ret;
2074}