blob: ecb3d867b4260d9c9ada27438fb252dac1fc77e9 [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 */
43void vmw_clip_cliprects(struct drm_clip_rect *rects,
44 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);
78 drm_crtc_cleanup(&du->crtc);
79 drm_encoder_cleanup(&du->encoder);
80 drm_connector_cleanup(&du->connector);
81}
82
83/*
84 * Display Unit Cursor functions
85 */
86
87int vmw_cursor_update_image(struct vmw_private *dev_priv,
88 u32 *image, u32 width, u32 height,
89 u32 hotspotX, u32 hotspotY)
90{
91 struct {
92 u32 cmd;
93 SVGAFifoCmdDefineAlphaCursor cursor;
94 } *cmd;
95 u32 image_size = width * height * 4;
96 u32 cmd_size = sizeof(*cmd) + image_size;
97
98 if (!image)
99 return -EINVAL;
100
101 cmd = vmw_fifo_reserve(dev_priv, cmd_size);
102 if (unlikely(cmd == NULL)) {
103 DRM_ERROR("Fifo reserve failed.\n");
104 return -ENOMEM;
105 }
106
107 memset(cmd, 0, sizeof(*cmd));
108
109 memcpy(&cmd[1], image, image_size);
110
111 cmd->cmd = cpu_to_le32(SVGA_CMD_DEFINE_ALPHA_CURSOR);
112 cmd->cursor.id = cpu_to_le32(0);
113 cmd->cursor.width = cpu_to_le32(width);
114 cmd->cursor.height = cpu_to_le32(height);
115 cmd->cursor.hotspotX = cpu_to_le32(hotspotX);
116 cmd->cursor.hotspotY = cpu_to_le32(hotspotY);
117
118 vmw_fifo_commit(dev_priv, cmd_size);
119
120 return 0;
121}
122
Jakob Bornecrantz6a91d972011-11-28 13:19:10 +0100123int vmw_cursor_update_dmabuf(struct vmw_private *dev_priv,
124 struct vmw_dma_buffer *dmabuf,
125 u32 width, u32 height,
126 u32 hotspotX, u32 hotspotY)
127{
128 struct ttm_bo_kmap_obj map;
129 unsigned long kmap_offset;
130 unsigned long kmap_num;
131 void *virtual;
132 bool dummy;
133 int ret;
134
135 kmap_offset = 0;
136 kmap_num = (width*height*4 + PAGE_SIZE - 1) >> PAGE_SHIFT;
137
138 ret = ttm_bo_reserve(&dmabuf->base, true, false, false, 0);
139 if (unlikely(ret != 0)) {
140 DRM_ERROR("reserve failed\n");
141 return -EINVAL;
142 }
143
144 ret = ttm_bo_kmap(&dmabuf->base, kmap_offset, kmap_num, &map);
145 if (unlikely(ret != 0))
146 goto err_unreserve;
147
148 virtual = ttm_kmap_obj_virtual(&map, &dummy);
149 ret = vmw_cursor_update_image(dev_priv, virtual, width, height,
150 hotspotX, hotspotY);
151
152 ttm_bo_kunmap(&map);
153err_unreserve:
154 ttm_bo_unreserve(&dmabuf->base);
155
156 return ret;
157}
158
159
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000160void vmw_cursor_update_position(struct vmw_private *dev_priv,
161 bool show, int x, int y)
162{
163 __le32 __iomem *fifo_mem = dev_priv->mmio_virt;
164 uint32_t count;
165
166 iowrite32(show ? 1 : 0, fifo_mem + SVGA_FIFO_CURSOR_ON);
167 iowrite32(x, fifo_mem + SVGA_FIFO_CURSOR_X);
168 iowrite32(y, fifo_mem + SVGA_FIFO_CURSOR_Y);
169 count = ioread32(fifo_mem + SVGA_FIFO_CURSOR_COUNT);
170 iowrite32(++count, fifo_mem + SVGA_FIFO_CURSOR_COUNT);
171}
172
173int vmw_du_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
174 uint32_t handle, uint32_t width, uint32_t height)
175{
176 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000177 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
178 struct vmw_surface *surface = NULL;
179 struct vmw_dma_buffer *dmabuf = NULL;
180 int ret;
181
Daniel Vetterbfb89922012-12-02 13:48:21 +0100182 /*
183 * FIXME: Unclear whether there's any global state touched by the
184 * cursor_set function, especially vmw_cursor_update_position looks
185 * suspicious. For now take the easy route and reacquire all locks. We
186 * can do this since the caller in the drm core doesn't check anything
187 * which is protected by any looks.
188 */
189 mutex_unlock(&crtc->mutex);
190 drm_modeset_lock_all(dev_priv->dev);
191
Jakob Bornecrantzbaa91d642011-11-09 10:25:28 +0100192 /* A lot of the code assumes this */
Daniel Vetterbfb89922012-12-02 13:48:21 +0100193 if (handle && (width != 64 || height != 64)) {
194 ret = -EINVAL;
195 goto out;
196 }
Jakob Bornecrantzbaa91d642011-11-09 10:25:28 +0100197
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000198 if (handle) {
Ville Syrjäläa5d0f572013-06-03 16:10:41 +0300199 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
200
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100201 ret = vmw_user_lookup_handle(dev_priv, tfile,
202 handle, &surface, &dmabuf);
203 if (ret) {
204 DRM_ERROR("failed to find surface or dmabuf: %i\n", ret);
Daniel Vetterbfb89922012-12-02 13:48:21 +0100205 ret = -EINVAL;
206 goto out;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000207 }
208 }
209
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100210 /* need to do this before taking down old image */
211 if (surface && !surface->snooper.image) {
212 DRM_ERROR("surface not suitable for cursor\n");
213 vmw_surface_unreference(&surface);
Daniel Vetterbfb89922012-12-02 13:48:21 +0100214 ret = -EINVAL;
215 goto out;
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100216 }
217
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000218 /* takedown old cursor */
219 if (du->cursor_surface) {
220 du->cursor_surface->snooper.crtc = NULL;
221 vmw_surface_unreference(&du->cursor_surface);
222 }
223 if (du->cursor_dmabuf)
224 vmw_dmabuf_unreference(&du->cursor_dmabuf);
225
226 /* setup new image */
227 if (surface) {
228 /* vmw_user_surface_lookup takes one reference */
229 du->cursor_surface = surface;
230
231 du->cursor_surface->snooper.crtc = crtc;
232 du->cursor_age = du->cursor_surface->snooper.age;
233 vmw_cursor_update_image(dev_priv, surface->snooper.image,
234 64, 64, du->hotspot_x, du->hotspot_y);
235 } else if (dmabuf) {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000236 /* vmw_user_surface_lookup takes one reference */
237 du->cursor_dmabuf = dmabuf;
238
Jakob Bornecrantz6a91d972011-11-28 13:19:10 +0100239 ret = vmw_cursor_update_dmabuf(dev_priv, dmabuf, width, height,
240 du->hotspot_x, du->hotspot_y);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000241 } else {
242 vmw_cursor_update_position(dev_priv, false, 0, 0);
Daniel Vetterbfb89922012-12-02 13:48:21 +0100243 ret = 0;
244 goto out;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000245 }
246
Thomas Hellstromda7653d2011-11-02 09:43:12 +0100247 vmw_cursor_update_position(dev_priv, true,
248 du->cursor_x + du->hotspot_x,
249 du->cursor_y + du->hotspot_y);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000250
Daniel Vetterbfb89922012-12-02 13:48:21 +0100251 ret = 0;
252out:
253 drm_modeset_unlock_all(dev_priv->dev);
254 mutex_lock(&crtc->mutex);
255
256 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000257}
258
259int vmw_du_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
260{
261 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
262 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
263 bool shown = du->cursor_surface || du->cursor_dmabuf ? true : false;
264
265 du->cursor_x = x + crtc->x;
266 du->cursor_y = y + crtc->y;
267
Daniel Vetterdac35662012-12-02 15:24:10 +0100268 /*
269 * FIXME: Unclear whether there's any global state touched by the
270 * cursor_set function, especially vmw_cursor_update_position looks
271 * suspicious. For now take the easy route and reacquire all locks. We
272 * can do this since the caller in the drm core doesn't check anything
273 * which is protected by any looks.
274 */
275 mutex_unlock(&crtc->mutex);
276 drm_modeset_lock_all(dev_priv->dev);
277
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000278 vmw_cursor_update_position(dev_priv, shown,
Thomas Hellstromda7653d2011-11-02 09:43:12 +0100279 du->cursor_x + du->hotspot_x,
280 du->cursor_y + du->hotspot_y);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000281
Daniel Vetterdac35662012-12-02 15:24:10 +0100282 drm_modeset_unlock_all(dev_priv->dev);
283 mutex_lock(&crtc->mutex);
284
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000285 return 0;
286}
287
288void vmw_kms_cursor_snoop(struct vmw_surface *srf,
289 struct ttm_object_file *tfile,
290 struct ttm_buffer_object *bo,
291 SVGA3dCmdHeader *header)
292{
293 struct ttm_bo_kmap_obj map;
294 unsigned long kmap_offset;
295 unsigned long kmap_num;
296 SVGA3dCopyBox *box;
297 unsigned box_count;
298 void *virtual;
299 bool dummy;
300 struct vmw_dma_cmd {
301 SVGA3dCmdHeader header;
302 SVGA3dCmdSurfaceDMA dma;
303 } *cmd;
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100304 int i, ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000305
306 cmd = container_of(header, struct vmw_dma_cmd, header);
307
308 /* No snooper installed */
309 if (!srf->snooper.image)
310 return;
311
312 if (cmd->dma.host.face != 0 || cmd->dma.host.mipmap != 0) {
313 DRM_ERROR("face and mipmap for cursors should never != 0\n");
314 return;
315 }
316
317 if (cmd->header.size < 64) {
318 DRM_ERROR("at least one full copy box must be given\n");
319 return;
320 }
321
322 box = (SVGA3dCopyBox *)&cmd[1];
323 box_count = (cmd->header.size - sizeof(SVGA3dCmdSurfaceDMA)) /
324 sizeof(SVGA3dCopyBox);
325
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100326 if (cmd->dma.guest.ptr.offset % PAGE_SIZE ||
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000327 box->x != 0 || box->y != 0 || box->z != 0 ||
328 box->srcx != 0 || box->srcy != 0 || box->srcz != 0 ||
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100329 box->d != 1 || box_count != 1) {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000330 /* TODO handle none page aligned offsets */
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100331 /* TODO handle more dst & src != 0 */
332 /* TODO handle more then one copy */
333 DRM_ERROR("Cant snoop dma request for cursor!\n");
334 DRM_ERROR("(%u, %u, %u) (%u, %u, %u) (%ux%ux%u) %u %u\n",
335 box->srcx, box->srcy, box->srcz,
336 box->x, box->y, box->z,
337 box->w, box->h, box->d, box_count,
338 cmd->dma.guest.ptr.offset);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000339 return;
340 }
341
342 kmap_offset = cmd->dma.guest.ptr.offset >> PAGE_SHIFT;
343 kmap_num = (64*64*4) >> PAGE_SHIFT;
344
345 ret = ttm_bo_reserve(bo, true, false, false, 0);
346 if (unlikely(ret != 0)) {
347 DRM_ERROR("reserve failed\n");
348 return;
349 }
350
351 ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
352 if (unlikely(ret != 0))
353 goto err_unreserve;
354
355 virtual = ttm_kmap_obj_virtual(&map, &dummy);
356
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100357 if (box->w == 64 && cmd->dma.guest.pitch == 64*4) {
358 memcpy(srf->snooper.image, virtual, 64*64*4);
359 } else {
360 /* Image is unsigned pointer. */
361 for (i = 0; i < box->h; i++)
362 memcpy(srf->snooper.image + i * 64,
363 virtual + i * cmd->dma.guest.pitch,
364 box->w * 4);
365 }
366
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000367 srf->snooper.age++;
368
369 /* we can't call this function from this function since execbuf has
370 * reserved fifo space.
371 *
372 * if (srf->snooper.crtc)
373 * vmw_ldu_crtc_cursor_update_image(dev_priv,
374 * srf->snooper.image, 64, 64,
375 * du->hotspot_x, du->hotspot_y);
376 */
377
378 ttm_bo_kunmap(&map);
379err_unreserve:
380 ttm_bo_unreserve(bo);
381}
382
383void vmw_kms_cursor_post_execbuf(struct vmw_private *dev_priv)
384{
385 struct drm_device *dev = dev_priv->dev;
386 struct vmw_display_unit *du;
387 struct drm_crtc *crtc;
388
389 mutex_lock(&dev->mode_config.mutex);
390
391 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
392 du = vmw_crtc_to_du(crtc);
393 if (!du->cursor_surface ||
394 du->cursor_age == du->cursor_surface->snooper.age)
395 continue;
396
397 du->cursor_age = du->cursor_surface->snooper.age;
398 vmw_cursor_update_image(dev_priv,
399 du->cursor_surface->snooper.image,
400 64, 64, du->hotspot_x, du->hotspot_y);
401 }
402
403 mutex_unlock(&dev->mode_config.mutex);
404}
405
406/*
407 * Generic framebuffer code
408 */
409
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000410/*
411 * Surface framebuffer code
412 */
413
414#define vmw_framebuffer_to_vfbs(x) \
415 container_of(x, struct vmw_framebuffer_surface, base.base)
416
417struct vmw_framebuffer_surface {
418 struct vmw_framebuffer base;
419 struct vmw_surface *surface;
Thomas Hellstrom22ee8612010-05-28 11:22:00 +0200420 struct vmw_dma_buffer *buffer;
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200421 struct list_head head;
422 struct drm_master *master;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000423};
424
425void vmw_framebuffer_surface_destroy(struct drm_framebuffer *framebuffer)
426{
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200427 struct vmw_framebuffer_surface *vfbs =
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000428 vmw_framebuffer_to_vfbs(framebuffer);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200429 struct vmw_master *vmaster = vmw_master(vfbs->master);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000430
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200431
432 mutex_lock(&vmaster->fb_surf_mutex);
433 list_del(&vfbs->head);
434 mutex_unlock(&vmaster->fb_surf_mutex);
435
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200436 drm_master_put(&vfbs->master);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000437 drm_framebuffer_cleanup(framebuffer);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200438 vmw_surface_unreference(&vfbs->surface);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200439 ttm_base_object_unref(&vfbs->base.user_obj);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000440
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200441 kfree(vfbs);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000442}
443
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200444static int do_surface_dirty_sou(struct vmw_private *dev_priv,
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200445 struct drm_file *file_priv,
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200446 struct vmw_framebuffer *framebuffer,
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200447 unsigned flags, unsigned color,
448 struct drm_clip_rect *clips,
Jakob Bornecrantzbd49ae42012-02-09 16:56:44 +0100449 unsigned num_clips, int inc,
450 struct vmw_fence_obj **out_fence)
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200451{
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200452 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100453 struct drm_clip_rect *clips_ptr;
454 struct drm_clip_rect *tmp;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200455 struct drm_crtc *crtc;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200456 size_t fifo_size;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200457 int i, num_units;
458 int ret = 0; /* silence warning */
459 int left, right, top, bottom;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200460
461 struct {
462 SVGA3dCmdHeader header;
463 SVGA3dCmdBlitSurfaceToScreen body;
464 } *cmd;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200465 SVGASignedRect *blits;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200466
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200467 num_units = 0;
468 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list,
469 head) {
470 if (crtc->fb != &framebuffer->base)
471 continue;
472 units[num_units++] = vmw_crtc_to_du(crtc);
473 }
474
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200475 BUG_ON(!clips || !num_clips);
476
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100477 tmp = kzalloc(sizeof(*tmp) * num_clips, GFP_KERNEL);
478 if (unlikely(tmp == NULL)) {
479 DRM_ERROR("Temporary cliprect memory alloc failed.\n");
480 return -ENOMEM;
481 }
482
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200483 fifo_size = sizeof(*cmd) + sizeof(SVGASignedRect) * num_clips;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200484 cmd = kzalloc(fifo_size, GFP_KERNEL);
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200485 if (unlikely(cmd == NULL)) {
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200486 DRM_ERROR("Temporary fifo memory alloc failed.\n");
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100487 ret = -ENOMEM;
488 goto out_free_tmp;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200489 }
490
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100491 /* setup blits pointer */
492 blits = (SVGASignedRect *)&cmd[1];
493
494 /* initial clip region */
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200495 left = clips->x1;
496 right = clips->x2;
497 top = clips->y1;
498 bottom = clips->y2;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200499
Jakob Bornecrantzf0c8a652011-11-09 10:25:27 +0100500 /* skip the first clip rect */
501 for (i = 1, clips_ptr = clips + inc;
502 i < num_clips; i++, clips_ptr += inc) {
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200503 left = min_t(int, left, (int)clips_ptr->x1);
504 right = max_t(int, right, (int)clips_ptr->x2);
505 top = min_t(int, top, (int)clips_ptr->y1);
506 bottom = max_t(int, bottom, (int)clips_ptr->y2);
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200507 }
508
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200509 /* only need to do this once */
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200510 cmd->header.id = cpu_to_le32(SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN);
511 cmd->header.size = cpu_to_le32(fifo_size - sizeof(cmd->header));
512
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200513 cmd->body.srcRect.left = left;
514 cmd->body.srcRect.right = right;
515 cmd->body.srcRect.top = top;
516 cmd->body.srcRect.bottom = bottom;
517
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200518 clips_ptr = clips;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200519 for (i = 0; i < num_clips; i++, clips_ptr += inc) {
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100520 tmp[i].x1 = clips_ptr->x1 - left;
521 tmp[i].x2 = clips_ptr->x2 - left;
522 tmp[i].y1 = clips_ptr->y1 - top;
523 tmp[i].y2 = clips_ptr->y2 - top;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200524 }
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200525
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200526 /* do per unit writing, reuse fifo for each */
527 for (i = 0; i < num_units; i++) {
528 struct vmw_display_unit *unit = units[i];
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100529 struct vmw_clip_rect clip;
530 int num;
531
532 clip.x1 = left - unit->crtc.x;
533 clip.y1 = top - unit->crtc.y;
534 clip.x2 = right - unit->crtc.x;
535 clip.y2 = bottom - unit->crtc.y;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200536
537 /* skip any crtcs that misses the clip region */
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100538 if (clip.x1 >= unit->crtc.mode.hdisplay ||
539 clip.y1 >= unit->crtc.mode.vdisplay ||
540 clip.x2 <= 0 || clip.y2 <= 0)
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200541 continue;
542
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100543 /*
544 * In order for the clip rects to be correctly scaled
545 * the src and dest rects needs to be the same size.
546 */
547 cmd->body.destRect.left = clip.x1;
548 cmd->body.destRect.right = clip.x2;
549 cmd->body.destRect.top = clip.y1;
550 cmd->body.destRect.bottom = clip.y2;
551
552 /* create a clip rect of the crtc in dest coords */
553 clip.x2 = unit->crtc.mode.hdisplay - clip.x1;
554 clip.y2 = unit->crtc.mode.vdisplay - clip.y1;
555 clip.x1 = 0 - clip.x1;
556 clip.y1 = 0 - clip.y1;
557
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200558 /* need to reset sid as it is changed by execbuf */
559 cmd->body.srcImage.sid = cpu_to_le32(framebuffer->user_handle);
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200560 cmd->body.destScreenId = unit->unit;
561
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100562 /* clip and write blits to cmd stream */
563 vmw_clip_cliprects(tmp, num_clips, clip, blits, &num);
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200564
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100565 /* if no cliprects hit skip this */
566 if (num == 0)
567 continue;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200568
Jakob Bornecrantzbd49ae42012-02-09 16:56:44 +0100569 /* only return the last fence */
570 if (out_fence && *out_fence)
571 vmw_fence_obj_unreference(out_fence);
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200572
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100573 /* recalculate package length */
574 fifo_size = sizeof(*cmd) + sizeof(SVGASignedRect) * num;
575 cmd->header.size = cpu_to_le32(fifo_size - sizeof(cmd->header));
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200576 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd,
Jakob Bornecrantzbd49ae42012-02-09 16:56:44 +0100577 fifo_size, 0, NULL, out_fence);
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200578
579 if (unlikely(ret != 0))
580 break;
581 }
582
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100583
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200584 kfree(cmd);
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100585out_free_tmp:
586 kfree(tmp);
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200587
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200588 return ret;
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200589}
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000590
591int vmw_framebuffer_surface_dirty(struct drm_framebuffer *framebuffer,
Thomas Hellstrom02b00162010-10-05 12:43:02 +0200592 struct drm_file *file_priv,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000593 unsigned flags, unsigned color,
594 struct drm_clip_rect *clips,
595 unsigned num_clips)
596{
597 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200598 struct vmw_master *vmaster = vmw_master(file_priv->master);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000599 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
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200611 ret = ttm_read_lock(&vmaster->lock, true);
612 if (unlikely(ret != 0))
613 return ret;
614
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000615 if (!num_clips) {
616 num_clips = 1;
617 clips = &norect;
618 norect.x1 = norect.y1 = 0;
619 norect.x2 = framebuffer->width;
620 norect.y2 = framebuffer->height;
621 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
622 num_clips /= 2;
623 inc = 2; /* skip source rects */
624 }
625
Jakob Bornecrantzc5c42362011-10-22 10:29:34 +0200626 ret = do_surface_dirty_sou(dev_priv, file_priv, &vfbs->base,
Jakob Bornecrantz01e81412011-10-04 20:13:24 +0200627 flags, color,
Jakob Bornecrantzbd49ae42012-02-09 16:56:44 +0100628 clips, num_clips, inc, NULL);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000629
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200630 ttm_read_unlock(&vmaster->lock);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000631 return 0;
632}
633
634static struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs = {
635 .destroy = vmw_framebuffer_surface_destroy,
636 .dirty = vmw_framebuffer_surface_dirty,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000637};
638
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200639static int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv,
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200640 struct drm_file *file_priv,
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200641 struct vmw_surface *surface,
642 struct vmw_framebuffer **out,
643 const struct drm_mode_fb_cmd
644 *mode_cmd)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000645
646{
647 struct drm_device *dev = dev_priv->dev;
648 struct vmw_framebuffer_surface *vfbs;
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200649 enum SVGA3dSurfaceFormat format;
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200650 struct vmw_master *vmaster = vmw_master(file_priv->master);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000651 int ret;
652
Jakob Bornecrantz01e81412011-10-04 20:13:24 +0200653 /* 3D is only supported on HWv8 hosts which supports screen objects */
654 if (!dev_priv->sou_priv)
655 return -ENOSYS;
656
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200657 /*
658 * Sanity checks.
659 */
660
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100661 /* Surface must be marked as a scanout. */
662 if (unlikely(!surface->scanout))
663 return -EINVAL;
664
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200665 if (unlikely(surface->mip_levels[0] != 1 ||
666 surface->num_sizes != 1 ||
667 surface->sizes[0].width < mode_cmd->width ||
668 surface->sizes[0].height < mode_cmd->height ||
669 surface->sizes[0].depth != 1)) {
670 DRM_ERROR("Incompatible surface dimensions "
671 "for requested mode.\n");
672 return -EINVAL;
673 }
674
675 switch (mode_cmd->depth) {
676 case 32:
677 format = SVGA3D_A8R8G8B8;
678 break;
679 case 24:
680 format = SVGA3D_X8R8G8B8;
681 break;
682 case 16:
683 format = SVGA3D_R5G6B5;
684 break;
685 case 15:
686 format = SVGA3D_A1R5G5B5;
687 break;
Michel Dänzerf01b7ba2011-08-31 07:42:47 +0000688 case 8:
689 format = SVGA3D_LUMINANCE8;
690 break;
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200691 default:
692 DRM_ERROR("Invalid color depth: %d\n", mode_cmd->depth);
693 return -EINVAL;
694 }
695
696 if (unlikely(format != surface->format)) {
697 DRM_ERROR("Invalid surface format for requested mode.\n");
698 return -EINVAL;
699 }
700
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000701 vfbs = kzalloc(sizeof(*vfbs), GFP_KERNEL);
702 if (!vfbs) {
703 ret = -ENOMEM;
704 goto out_err1;
705 }
706
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000707 if (!vmw_surface_reference(surface)) {
708 DRM_ERROR("failed to reference surface %p\n", surface);
Daniel Vetter80f0b5a2012-12-13 23:39:01 +0100709 ret = -EINVAL;
710 goto out_err2;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000711 }
712
713 /* XXX get the first 3 from the surface info */
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200714 vfbs->base.base.bits_per_pixel = mode_cmd->bpp;
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200715 vfbs->base.base.pitches[0] = mode_cmd->pitch;
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200716 vfbs->base.base.depth = mode_cmd->depth;
717 vfbs->base.base.width = mode_cmd->width;
718 vfbs->base.base.height = mode_cmd->height;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000719 vfbs->surface = surface;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200720 vfbs->base.user_handle = mode_cmd->handle;
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200721 vfbs->master = drm_master_get(file_priv->master);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200722
723 mutex_lock(&vmaster->fb_surf_mutex);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200724 list_add_tail(&vfbs->head, &vmaster->fb_surf);
725 mutex_unlock(&vmaster->fb_surf_mutex);
726
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000727 *out = &vfbs->base;
728
Daniel Vetter80f0b5a2012-12-13 23:39:01 +0100729 ret = drm_framebuffer_init(dev, &vfbs->base.base,
730 &vmw_framebuffer_surface_funcs);
731 if (ret)
732 goto out_err3;
733
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000734 return 0;
735
736out_err3:
Daniel Vetter80f0b5a2012-12-13 23:39:01 +0100737 vmw_surface_unreference(&surface);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000738out_err2:
739 kfree(vfbs);
740out_err1:
741 return ret;
742}
743
744/*
745 * Dmabuf framebuffer code
746 */
747
748#define vmw_framebuffer_to_vfbd(x) \
749 container_of(x, struct vmw_framebuffer_dmabuf, base.base)
750
751struct vmw_framebuffer_dmabuf {
752 struct vmw_framebuffer base;
753 struct vmw_dma_buffer *buffer;
754};
755
756void vmw_framebuffer_dmabuf_destroy(struct drm_framebuffer *framebuffer)
757{
758 struct vmw_framebuffer_dmabuf *vfbd =
759 vmw_framebuffer_to_vfbd(framebuffer);
760
761 drm_framebuffer_cleanup(framebuffer);
762 vmw_dmabuf_unreference(&vfbd->buffer);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200763 ttm_base_object_unref(&vfbd->base.user_obj);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000764
765 kfree(vfbd);
766}
767
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200768static int do_dmabuf_dirty_ldu(struct vmw_private *dev_priv,
769 struct vmw_framebuffer *framebuffer,
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200770 unsigned flags, unsigned color,
771 struct drm_clip_rect *clips,
772 unsigned num_clips, int increment)
773{
774 size_t fifo_size;
775 int i;
776
777 struct {
778 uint32_t header;
779 SVGAFifoCmdUpdate body;
780 } *cmd;
781
782 fifo_size = sizeof(*cmd) * num_clips;
783 cmd = vmw_fifo_reserve(dev_priv, fifo_size);
784 if (unlikely(cmd == NULL)) {
785 DRM_ERROR("Fifo reserve failed.\n");
786 return -ENOMEM;
787 }
788
789 memset(cmd, 0, fifo_size);
790 for (i = 0; i < num_clips; i++, clips += increment) {
791 cmd[i].header = cpu_to_le32(SVGA_CMD_UPDATE);
792 cmd[i].body.x = cpu_to_le32(clips->x1);
793 cmd[i].body.y = cpu_to_le32(clips->y1);
794 cmd[i].body.width = cpu_to_le32(clips->x2 - clips->x1);
795 cmd[i].body.height = cpu_to_le32(clips->y2 - clips->y1);
796 }
797
798 vmw_fifo_commit(dev_priv, fifo_size);
799 return 0;
800}
801
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200802static int do_dmabuf_define_gmrfb(struct drm_file *file_priv,
803 struct vmw_private *dev_priv,
804 struct vmw_framebuffer *framebuffer)
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200805{
Jakob Bornecrantz64fc9942011-10-22 10:29:33 +0200806 int depth = framebuffer->base.depth;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200807 size_t fifo_size;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200808 int ret;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200809
810 struct {
811 uint32_t header;
812 SVGAFifoCmdDefineGMRFB body;
813 } *cmd;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200814
Jakob Bornecrantz64fc9942011-10-22 10:29:33 +0200815 /* Emulate RGBA support, contrary to svga_reg.h this is not
816 * supported by hosts. This is only a problem if we are reading
817 * this value later and expecting what we uploaded back.
818 */
819 if (depth == 32)
820 depth = 24;
821
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200822 fifo_size = sizeof(*cmd);
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200823 cmd = kmalloc(fifo_size, GFP_KERNEL);
824 if (unlikely(cmd == NULL)) {
825 DRM_ERROR("Failed to allocate temporary cmd buffer.\n");
826 return -ENOMEM;
827 }
828
829 memset(cmd, 0, fifo_size);
830 cmd->header = SVGA_CMD_DEFINE_GMRFB;
831 cmd->body.format.bitsPerPixel = framebuffer->base.bits_per_pixel;
Jakob Bornecrantz64fc9942011-10-22 10:29:33 +0200832 cmd->body.format.colorDepth = depth;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200833 cmd->body.format.reserved = 0;
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200834 cmd->body.bytesPerLine = framebuffer->base.pitches[0];
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200835 cmd->body.ptr.gmrId = framebuffer->user_handle;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200836 cmd->body.ptr.offset = 0;
837
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200838 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd,
Jakob Bornecrantzbb1bd2f2012-02-09 16:56:43 +0100839 fifo_size, 0, NULL, NULL);
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200840
841 kfree(cmd);
842
843 return ret;
844}
845
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200846static int do_dmabuf_dirty_sou(struct drm_file *file_priv,
847 struct vmw_private *dev_priv,
848 struct vmw_framebuffer *framebuffer,
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200849 unsigned flags, unsigned color,
850 struct drm_clip_rect *clips,
Jakob Bornecrantzbd49ae42012-02-09 16:56:44 +0100851 unsigned num_clips, int increment,
852 struct vmw_fence_obj **out_fence)
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200853{
854 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
855 struct drm_clip_rect *clips_ptr;
856 int i, k, num_units, ret;
857 struct drm_crtc *crtc;
858 size_t fifo_size;
859
860 struct {
861 uint32_t header;
862 SVGAFifoCmdBlitGMRFBToScreen body;
863 } *blits;
864
865 ret = do_dmabuf_define_gmrfb(file_priv, dev_priv, framebuffer);
866 if (unlikely(ret != 0))
867 return ret; /* define_gmrfb prints warnings */
868
869 fifo_size = sizeof(*blits) * num_clips;
870 blits = kmalloc(fifo_size, GFP_KERNEL);
871 if (unlikely(blits == NULL)) {
872 DRM_ERROR("Failed to allocate temporary cmd buffer.\n");
873 return -ENOMEM;
874 }
875
876 num_units = 0;
877 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
878 if (crtc->fb != &framebuffer->base)
879 continue;
880 units[num_units++] = vmw_crtc_to_du(crtc);
881 }
882
883 for (k = 0; k < num_units; k++) {
884 struct vmw_display_unit *unit = units[k];
885 int hit_num = 0;
886
887 clips_ptr = clips;
888 for (i = 0; i < num_clips; i++, clips_ptr += increment) {
889 int clip_x1 = clips_ptr->x1 - unit->crtc.x;
890 int clip_y1 = clips_ptr->y1 - unit->crtc.y;
891 int clip_x2 = clips_ptr->x2 - unit->crtc.x;
892 int clip_y2 = clips_ptr->y2 - unit->crtc.y;
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100893 int move_x, move_y;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200894
895 /* skip any crtcs that misses the clip region */
896 if (clip_x1 >= unit->crtc.mode.hdisplay ||
897 clip_y1 >= unit->crtc.mode.vdisplay ||
898 clip_x2 <= 0 || clip_y2 <= 0)
899 continue;
900
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100901 /* clip size to crtc size */
902 clip_x2 = min_t(int, clip_x2, unit->crtc.mode.hdisplay);
903 clip_y2 = min_t(int, clip_y2, unit->crtc.mode.vdisplay);
904
905 /* translate both src and dest to bring clip into screen */
906 move_x = min_t(int, clip_x1, 0);
907 move_y = min_t(int, clip_y1, 0);
908
909 /* actual translate done here */
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200910 blits[hit_num].header = SVGA_CMD_BLIT_GMRFB_TO_SCREEN;
911 blits[hit_num].body.destScreenId = unit->unit;
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100912 blits[hit_num].body.srcOrigin.x = clips_ptr->x1 - move_x;
913 blits[hit_num].body.srcOrigin.y = clips_ptr->y1 - move_y;
914 blits[hit_num].body.destRect.left = clip_x1 - move_x;
915 blits[hit_num].body.destRect.top = clip_y1 - move_y;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200916 blits[hit_num].body.destRect.right = clip_x2;
917 blits[hit_num].body.destRect.bottom = clip_y2;
918 hit_num++;
919 }
920
921 /* no clips hit the crtc */
922 if (hit_num == 0)
923 continue;
924
Jakob Bornecrantzbd49ae42012-02-09 16:56:44 +0100925 /* only return the last fence */
926 if (out_fence && *out_fence)
927 vmw_fence_obj_unreference(out_fence);
928
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200929 fifo_size = sizeof(*blits) * hit_num;
930 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, blits,
Jakob Bornecrantzbd49ae42012-02-09 16:56:44 +0100931 fifo_size, 0, NULL, out_fence);
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200932
933 if (unlikely(ret != 0))
934 break;
935 }
936
937 kfree(blits);
938
939 return ret;
940}
941
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000942int vmw_framebuffer_dmabuf_dirty(struct drm_framebuffer *framebuffer,
Thomas Hellstrom02b00162010-10-05 12:43:02 +0200943 struct drm_file *file_priv,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000944 unsigned flags, unsigned color,
945 struct drm_clip_rect *clips,
946 unsigned num_clips)
947{
948 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200949 struct vmw_master *vmaster = vmw_master(file_priv->master);
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200950 struct vmw_framebuffer_dmabuf *vfbd =
951 vmw_framebuffer_to_vfbd(framebuffer);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000952 struct drm_clip_rect norect;
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200953 int ret, increment = 1;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000954
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200955 ret = ttm_read_lock(&vmaster->lock, true);
956 if (unlikely(ret != 0))
957 return ret;
958
Thomas Hellstromdf1c93b2010-01-13 22:28:36 +0100959 if (!num_clips) {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000960 num_clips = 1;
961 clips = &norect;
962 norect.x1 = norect.y1 = 0;
963 norect.x2 = framebuffer->width;
964 norect.y2 = framebuffer->height;
965 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
966 num_clips /= 2;
967 increment = 2;
968 }
969
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200970 if (dev_priv->ldu_priv) {
Jakob Bornecrantzc5c42362011-10-22 10:29:34 +0200971 ret = do_dmabuf_dirty_ldu(dev_priv, &vfbd->base,
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200972 flags, color,
973 clips, num_clips, increment);
974 } else {
975 ret = do_dmabuf_dirty_sou(file_priv, dev_priv, &vfbd->base,
Jakob Bornecrantzc5c42362011-10-22 10:29:34 +0200976 flags, color,
Jakob Bornecrantzbd49ae42012-02-09 16:56:44 +0100977 clips, num_clips, increment, NULL);
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200978 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000979
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200980 ttm_read_unlock(&vmaster->lock);
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200981 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000982}
983
984static struct drm_framebuffer_funcs vmw_framebuffer_dmabuf_funcs = {
985 .destroy = vmw_framebuffer_dmabuf_destroy,
986 .dirty = vmw_framebuffer_dmabuf_dirty,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000987};
988
Jakob Bornecrantz497a3ff2011-10-04 20:13:14 +0200989/**
Jakob Bornecrantz497a3ff2011-10-04 20:13:14 +0200990 * Pin the dmabuffer to the start of vram.
991 */
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000992static int vmw_framebuffer_dmabuf_pin(struct vmw_framebuffer *vfb)
993{
994 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
995 struct vmw_framebuffer_dmabuf *vfbd =
996 vmw_framebuffer_to_vfbd(&vfb->base);
997 int ret;
998
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200999 /* This code should not be used with screen objects */
1000 BUG_ON(dev_priv->sou_priv);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001001
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001002 vmw_overlay_pause_all(dev_priv);
1003
Jakob Bornecrantzd991ef02011-10-04 20:13:21 +02001004 ret = vmw_dmabuf_to_start_of_vram(dev_priv, vfbd->buffer, true, false);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001005
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001006 vmw_overlay_resume_all(dev_priv);
1007
Jakob Bornecrantz316ab132010-05-28 11:22:05 +02001008 WARN_ON(ret != 0);
1009
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001010 return 0;
1011}
1012
1013static int vmw_framebuffer_dmabuf_unpin(struct vmw_framebuffer *vfb)
1014{
1015 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
1016 struct vmw_framebuffer_dmabuf *vfbd =
1017 vmw_framebuffer_to_vfbd(&vfb->base);
1018
1019 if (!vfbd->buffer) {
1020 WARN_ON(!vfbd->buffer);
1021 return 0;
1022 }
1023
Jakob Bornecrantzd991ef02011-10-04 20:13:21 +02001024 return vmw_dmabuf_unpin(dev_priv, vfbd->buffer, false);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001025}
1026
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001027static int vmw_kms_new_framebuffer_dmabuf(struct vmw_private *dev_priv,
1028 struct vmw_dma_buffer *dmabuf,
1029 struct vmw_framebuffer **out,
1030 const struct drm_mode_fb_cmd
1031 *mode_cmd)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001032
1033{
1034 struct drm_device *dev = dev_priv->dev;
1035 struct vmw_framebuffer_dmabuf *vfbd;
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001036 unsigned int requested_size;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001037 int ret;
1038
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001039 requested_size = mode_cmd->height * mode_cmd->pitch;
1040 if (unlikely(requested_size > dmabuf->base.num_pages * PAGE_SIZE)) {
1041 DRM_ERROR("Screen buffer object size is too small "
1042 "for requested mode.\n");
1043 return -EINVAL;
1044 }
1045
Jakob Bornecrantzc337ada2011-10-04 20:13:34 +02001046 /* Limited framebuffer color depth support for screen objects */
1047 if (dev_priv->sou_priv) {
1048 switch (mode_cmd->depth) {
1049 case 32:
1050 case 24:
1051 /* Only support 32 bpp for 32 and 24 depth fbs */
1052 if (mode_cmd->bpp == 32)
1053 break;
1054
1055 DRM_ERROR("Invalid color depth/bbp: %d %d\n",
1056 mode_cmd->depth, mode_cmd->bpp);
1057 return -EINVAL;
1058 case 16:
1059 case 15:
1060 /* Only support 16 bpp for 16 and 15 depth fbs */
1061 if (mode_cmd->bpp == 16)
1062 break;
1063
1064 DRM_ERROR("Invalid color depth/bbp: %d %d\n",
1065 mode_cmd->depth, mode_cmd->bpp);
1066 return -EINVAL;
1067 default:
1068 DRM_ERROR("Invalid color depth: %d\n", mode_cmd->depth);
1069 return -EINVAL;
1070 }
1071 }
1072
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001073 vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL);
1074 if (!vfbd) {
1075 ret = -ENOMEM;
1076 goto out_err1;
1077 }
1078
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001079 if (!vmw_dmabuf_reference(dmabuf)) {
1080 DRM_ERROR("failed to reference dmabuf %p\n", dmabuf);
Daniel Vetter80f0b5a2012-12-13 23:39:01 +01001081 ret = -EINVAL;
1082 goto out_err2;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001083 }
1084
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001085 vfbd->base.base.bits_per_pixel = mode_cmd->bpp;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02001086 vfbd->base.base.pitches[0] = mode_cmd->pitch;
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001087 vfbd->base.base.depth = mode_cmd->depth;
1088 vfbd->base.base.width = mode_cmd->width;
1089 vfbd->base.base.height = mode_cmd->height;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +02001090 if (!dev_priv->sou_priv) {
1091 vfbd->base.pin = vmw_framebuffer_dmabuf_pin;
1092 vfbd->base.unpin = vmw_framebuffer_dmabuf_unpin;
1093 }
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001094 vfbd->base.dmabuf = true;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001095 vfbd->buffer = dmabuf;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001096 vfbd->base.user_handle = mode_cmd->handle;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001097 *out = &vfbd->base;
1098
Daniel Vetter80f0b5a2012-12-13 23:39:01 +01001099 ret = drm_framebuffer_init(dev, &vfbd->base.base,
1100 &vmw_framebuffer_dmabuf_funcs);
1101 if (ret)
1102 goto out_err3;
1103
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001104 return 0;
1105
1106out_err3:
Daniel Vetter80f0b5a2012-12-13 23:39:01 +01001107 vmw_dmabuf_unreference(&dmabuf);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001108out_err2:
1109 kfree(vfbd);
1110out_err1:
1111 return ret;
1112}
1113
1114/*
1115 * Generic Kernel modesetting functions
1116 */
1117
1118static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev,
1119 struct drm_file *file_priv,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001120 struct drm_mode_fb_cmd2 *mode_cmd2)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001121{
1122 struct vmw_private *dev_priv = vmw_priv(dev);
1123 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
1124 struct vmw_framebuffer *vfb = NULL;
1125 struct vmw_surface *surface = NULL;
1126 struct vmw_dma_buffer *bo = NULL;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001127 struct ttm_base_object *user_obj;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001128 struct drm_mode_fb_cmd mode_cmd;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001129 int ret;
1130
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001131 mode_cmd.width = mode_cmd2->width;
1132 mode_cmd.height = mode_cmd2->height;
1133 mode_cmd.pitch = mode_cmd2->pitches[0];
1134 mode_cmd.handle = mode_cmd2->handles[0];
Dave Airlie248dbc22011-11-29 20:02:54 +00001135 drm_fb_get_bpp_depth(mode_cmd2->pixel_format, &mode_cmd.depth,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001136 &mode_cmd.bpp);
1137
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001138 /**
1139 * This code should be conditioned on Screen Objects not being used.
1140 * If screen objects are used, we can allocate a GMR to hold the
1141 * requested framebuffer.
1142 */
1143
Xi Wang8a783892011-12-21 05:18:33 -05001144 if (!vmw_kms_validate_mode_vram(dev_priv,
Linus Torvalds1a464cb2012-01-10 11:04:36 -08001145 mode_cmd.pitch,
1146 mode_cmd.height)) {
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001147 DRM_ERROR("VRAM size is too small for requested mode.\n");
Jakob Bornecrantzd9826402011-11-03 21:03:04 +01001148 return ERR_PTR(-ENOMEM);
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001149 }
1150
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001151 /*
1152 * Take a reference on the user object of the resource
1153 * backing the kms fb. This ensures that user-space handle
1154 * lookups on that resource will always work as long as
1155 * it's registered with a kms framebuffer. This is important,
1156 * since vmw_execbuf_process identifies resources in the
1157 * command stream using user-space handles.
1158 */
1159
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001160 user_obj = ttm_base_object_lookup(tfile, mode_cmd.handle);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001161 if (unlikely(user_obj == NULL)) {
1162 DRM_ERROR("Could not locate requested kms frame buffer.\n");
1163 return ERR_PTR(-ENOENT);
1164 }
1165
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001166 /**
1167 * End conditioned code.
1168 */
1169
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +01001170 /* returns either a dmabuf or surface */
1171 ret = vmw_user_lookup_handle(dev_priv, tfile,
Dave Airlie4cf73122011-12-21 09:50:56 +00001172 mode_cmd.handle,
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +01001173 &surface, &bo);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001174 if (ret)
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +01001175 goto err_out;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001176
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +01001177 /* Create the new framebuffer depending one what we got back */
1178 if (bo)
1179 ret = vmw_kms_new_framebuffer_dmabuf(dev_priv, bo, &vfb,
Dave Airlie4cf73122011-12-21 09:50:56 +00001180 &mode_cmd);
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +01001181 else if (surface)
1182 ret = vmw_kms_new_framebuffer_surface(dev_priv, file_priv,
Dave Airlie4cf73122011-12-21 09:50:56 +00001183 surface, &vfb, &mode_cmd);
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +01001184 else
1185 BUG();
Jakob Bornecrantz5ffdb652010-01-30 03:38:08 +00001186
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +01001187err_out:
1188 /* vmw_user_lookup_handle takes one ref so does new_fb */
1189 if (bo)
1190 vmw_dmabuf_unreference(&bo);
1191 if (surface)
1192 vmw_surface_unreference(&surface);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001193
1194 if (ret) {
1195 DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001196 ttm_base_object_unref(&user_obj);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01001197 return ERR_PTR(ret);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001198 } else
1199 vfb->user_obj = user_obj;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001200
1201 return &vfb->base;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001202}
1203
Laurent Pincharte6ecefa2012-05-17 13:27:23 +02001204static const struct drm_mode_config_funcs vmw_kms_funcs = {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001205 .fb_create = vmw_kms_fb_create,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001206};
1207
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001208int vmw_kms_present(struct vmw_private *dev_priv,
1209 struct drm_file *file_priv,
1210 struct vmw_framebuffer *vfb,
1211 struct vmw_surface *surface,
1212 uint32_t sid,
1213 int32_t destX, int32_t destY,
1214 struct drm_vmw_rect *clips,
1215 uint32_t num_clips)
1216{
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001217 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +01001218 struct drm_clip_rect *tmp;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001219 struct drm_crtc *crtc;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001220 size_t fifo_size;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001221 int i, k, num_units;
1222 int ret = 0; /* silence warning */
Jakob Bornecrantz203dc222011-11-28 13:19:13 +01001223 int left, right, top, bottom;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001224
1225 struct {
1226 SVGA3dCmdHeader header;
1227 SVGA3dCmdBlitSurfaceToScreen body;
1228 } *cmd;
1229 SVGASignedRect *blits;
1230
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001231 num_units = 0;
1232 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
1233 if (crtc->fb != &vfb->base)
1234 continue;
1235 units[num_units++] = vmw_crtc_to_du(crtc);
1236 }
1237
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001238 BUG_ON(surface == NULL);
1239 BUG_ON(!clips || !num_clips);
1240
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +01001241 tmp = kzalloc(sizeof(*tmp) * num_clips, GFP_KERNEL);
1242 if (unlikely(tmp == NULL)) {
1243 DRM_ERROR("Temporary cliprect memory alloc failed.\n");
1244 return -ENOMEM;
1245 }
1246
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001247 fifo_size = sizeof(*cmd) + sizeof(SVGASignedRect) * num_clips;
1248 cmd = kmalloc(fifo_size, GFP_KERNEL);
1249 if (unlikely(cmd == NULL)) {
1250 DRM_ERROR("Failed to allocate temporary fifo memory.\n");
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +01001251 ret = -ENOMEM;
1252 goto out_free_tmp;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001253 }
1254
Jakob Bornecrantz203dc222011-11-28 13:19:13 +01001255 left = clips->x;
1256 right = clips->x + clips->w;
1257 top = clips->y;
1258 bottom = clips->y + clips->h;
1259
1260 for (i = 1; i < num_clips; i++) {
1261 left = min_t(int, left, (int)clips[i].x);
1262 right = max_t(int, right, (int)clips[i].x + clips[i].w);
1263 top = min_t(int, top, (int)clips[i].y);
1264 bottom = max_t(int, bottom, (int)clips[i].y + clips[i].h);
1265 }
1266
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001267 /* only need to do this once */
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001268 memset(cmd, 0, fifo_size);
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001269 cmd->header.id = cpu_to_le32(SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN);
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +01001270
1271 blits = (SVGASignedRect *)&cmd[1];
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001272
Jakob Bornecrantz203dc222011-11-28 13:19:13 +01001273 cmd->body.srcRect.left = left;
1274 cmd->body.srcRect.right = right;
1275 cmd->body.srcRect.top = top;
1276 cmd->body.srcRect.bottom = bottom;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001277
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001278 for (i = 0; i < num_clips; i++) {
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +01001279 tmp[i].x1 = clips[i].x - left;
1280 tmp[i].x2 = clips[i].x + clips[i].w - left;
1281 tmp[i].y1 = clips[i].y - top;
1282 tmp[i].y2 = clips[i].y + clips[i].h - top;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001283 }
1284
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001285 for (k = 0; k < num_units; k++) {
1286 struct vmw_display_unit *unit = units[k];
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +01001287 struct vmw_clip_rect clip;
1288 int num;
1289
1290 clip.x1 = left + destX - unit->crtc.x;
1291 clip.y1 = top + destY - unit->crtc.y;
1292 clip.x2 = right + destX - unit->crtc.x;
1293 clip.y2 = bottom + destY - unit->crtc.y;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001294
1295 /* skip any crtcs that misses the clip region */
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +01001296 if (clip.x1 >= unit->crtc.mode.hdisplay ||
1297 clip.y1 >= unit->crtc.mode.vdisplay ||
1298 clip.x2 <= 0 || clip.y2 <= 0)
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001299 continue;
1300
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +01001301 /*
1302 * In order for the clip rects to be correctly scaled
1303 * the src and dest rects needs to be the same size.
1304 */
1305 cmd->body.destRect.left = clip.x1;
1306 cmd->body.destRect.right = clip.x2;
1307 cmd->body.destRect.top = clip.y1;
1308 cmd->body.destRect.bottom = clip.y2;
1309
1310 /* create a clip rect of the crtc in dest coords */
1311 clip.x2 = unit->crtc.mode.hdisplay - clip.x1;
1312 clip.y2 = unit->crtc.mode.vdisplay - clip.y1;
1313 clip.x1 = 0 - clip.x1;
1314 clip.y1 = 0 - clip.y1;
1315
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001316 /* need to reset sid as it is changed by execbuf */
1317 cmd->body.srcImage.sid = sid;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001318 cmd->body.destScreenId = unit->unit;
1319
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +01001320 /* clip and write blits to cmd stream */
1321 vmw_clip_cliprects(tmp, num_clips, clip, blits, &num);
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001322
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +01001323 /* if no cliprects hit skip this */
1324 if (num == 0)
1325 continue;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001326
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +01001327 /* recalculate package length */
1328 fifo_size = sizeof(*cmd) + sizeof(SVGASignedRect) * num;
1329 cmd->header.size = cpu_to_le32(fifo_size - sizeof(cmd->header));
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001330 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd,
Jakob Bornecrantzbb1bd2f2012-02-09 16:56:43 +01001331 fifo_size, 0, NULL, NULL);
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001332
1333 if (unlikely(ret != 0))
1334 break;
1335 }
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001336
1337 kfree(cmd);
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +01001338out_free_tmp:
1339 kfree(tmp);
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001340
1341 return ret;
1342}
1343
1344int vmw_kms_readback(struct vmw_private *dev_priv,
1345 struct drm_file *file_priv,
1346 struct vmw_framebuffer *vfb,
1347 struct drm_vmw_fence_rep __user *user_fence_rep,
1348 struct drm_vmw_rect *clips,
1349 uint32_t num_clips)
1350{
1351 struct vmw_framebuffer_dmabuf *vfbd =
1352 vmw_framebuffer_to_vfbd(&vfb->base);
1353 struct vmw_dma_buffer *dmabuf = vfbd->buffer;
1354 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
1355 struct drm_crtc *crtc;
1356 size_t fifo_size;
1357 int i, k, ret, num_units, blits_pos;
1358
1359 struct {
1360 uint32_t header;
1361 SVGAFifoCmdDefineGMRFB body;
1362 } *cmd;
1363 struct {
1364 uint32_t header;
1365 SVGAFifoCmdBlitScreenToGMRFB body;
1366 } *blits;
1367
1368 num_units = 0;
1369 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
1370 if (crtc->fb != &vfb->base)
1371 continue;
1372 units[num_units++] = vmw_crtc_to_du(crtc);
1373 }
1374
1375 BUG_ON(dmabuf == NULL);
1376 BUG_ON(!clips || !num_clips);
1377
1378 /* take a safe guess at fifo size */
1379 fifo_size = sizeof(*cmd) + sizeof(*blits) * num_clips * num_units;
1380 cmd = kmalloc(fifo_size, GFP_KERNEL);
1381 if (unlikely(cmd == NULL)) {
1382 DRM_ERROR("Failed to allocate temporary fifo memory.\n");
1383 return -ENOMEM;
1384 }
1385
1386 memset(cmd, 0, fifo_size);
1387 cmd->header = SVGA_CMD_DEFINE_GMRFB;
1388 cmd->body.format.bitsPerPixel = vfb->base.bits_per_pixel;
1389 cmd->body.format.colorDepth = vfb->base.depth;
1390 cmd->body.format.reserved = 0;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02001391 cmd->body.bytesPerLine = vfb->base.pitches[0];
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001392 cmd->body.ptr.gmrId = vfb->user_handle;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001393 cmd->body.ptr.offset = 0;
1394
1395 blits = (void *)&cmd[1];
1396 blits_pos = 0;
1397 for (i = 0; i < num_units; i++) {
1398 struct drm_vmw_rect *c = clips;
1399 for (k = 0; k < num_clips; k++, c++) {
1400 /* transform clip coords to crtc origin based coords */
1401 int clip_x1 = c->x - units[i]->crtc.x;
1402 int clip_x2 = c->x - units[i]->crtc.x + c->w;
1403 int clip_y1 = c->y - units[i]->crtc.y;
1404 int clip_y2 = c->y - units[i]->crtc.y + c->h;
1405 int dest_x = c->x;
1406 int dest_y = c->y;
1407
1408 /* compensate for clipping, we negate
1409 * a negative number and add that.
1410 */
1411 if (clip_x1 < 0)
1412 dest_x += -clip_x1;
1413 if (clip_y1 < 0)
1414 dest_y += -clip_y1;
1415
1416 /* clip */
1417 clip_x1 = max(clip_x1, 0);
1418 clip_y1 = max(clip_y1, 0);
1419 clip_x2 = min(clip_x2, units[i]->crtc.mode.hdisplay);
1420 clip_y2 = min(clip_y2, units[i]->crtc.mode.vdisplay);
1421
1422 /* and cull any rects that misses the crtc */
1423 if (clip_x1 >= units[i]->crtc.mode.hdisplay ||
1424 clip_y1 >= units[i]->crtc.mode.vdisplay ||
1425 clip_x2 <= 0 || clip_y2 <= 0)
1426 continue;
1427
1428 blits[blits_pos].header = SVGA_CMD_BLIT_SCREEN_TO_GMRFB;
1429 blits[blits_pos].body.srcScreenId = units[i]->unit;
1430 blits[blits_pos].body.destOrigin.x = dest_x;
1431 blits[blits_pos].body.destOrigin.y = dest_y;
1432
1433 blits[blits_pos].body.srcRect.left = clip_x1;
1434 blits[blits_pos].body.srcRect.top = clip_y1;
1435 blits[blits_pos].body.srcRect.right = clip_x2;
1436 blits[blits_pos].body.srcRect.bottom = clip_y2;
1437 blits_pos++;
1438 }
1439 }
1440 /* reset size here and use calculated exact size from loops */
1441 fifo_size = sizeof(*cmd) + sizeof(*blits) * blits_pos;
1442
1443 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd, fifo_size,
Jakob Bornecrantzbb1bd2f2012-02-09 16:56:43 +01001444 0, user_fence_rep, NULL);
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001445
1446 kfree(cmd);
1447
1448 return ret;
1449}
1450
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001451int vmw_kms_init(struct vmw_private *dev_priv)
1452{
1453 struct drm_device *dev = dev_priv->dev;
1454 int ret;
1455
1456 drm_mode_config_init(dev);
1457 dev->mode_config.funcs = &vmw_kms_funcs;
Jakob Bornecrantz3bef3572010-02-09 19:41:57 +00001458 dev->mode_config.min_width = 1;
1459 dev->mode_config.min_height = 1;
Jakob Bornecrantz7e71f8a2010-05-28 11:21:54 +02001460 /* assumed largest fb size */
1461 dev->mode_config.max_width = 8192;
1462 dev->mode_config.max_height = 8192;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001463
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +02001464 ret = vmw_kms_init_screen_object_display(dev_priv);
1465 if (ret) /* Fallback */
1466 (void)vmw_kms_init_legacy_display_system(dev_priv);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001467
1468 return 0;
1469}
1470
1471int vmw_kms_close(struct vmw_private *dev_priv)
1472{
1473 /*
1474 * Docs says we should take the lock before calling this function
1475 * but since it destroys encoders and our destructor calls
1476 * drm_encoder_cleanup which takes the lock we deadlock.
1477 */
1478 drm_mode_config_cleanup(dev_priv->dev);
Jakob Bornecrantzc0d18312011-11-09 10:25:26 +01001479 if (dev_priv->sou_priv)
1480 vmw_kms_close_screen_object_display(dev_priv);
1481 else
1482 vmw_kms_close_legacy_display_system(dev_priv);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001483 return 0;
1484}
1485
1486int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
1487 struct drm_file *file_priv)
1488{
1489 struct drm_vmw_cursor_bypass_arg *arg = data;
1490 struct vmw_display_unit *du;
1491 struct drm_mode_object *obj;
1492 struct drm_crtc *crtc;
1493 int ret = 0;
1494
1495
1496 mutex_lock(&dev->mode_config.mutex);
1497 if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
1498
1499 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1500 du = vmw_crtc_to_du(crtc);
1501 du->hotspot_x = arg->xhot;
1502 du->hotspot_y = arg->yhot;
1503 }
1504
1505 mutex_unlock(&dev->mode_config.mutex);
1506 return 0;
1507 }
1508
1509 obj = drm_mode_object_find(dev, arg->crtc_id, DRM_MODE_OBJECT_CRTC);
1510 if (!obj) {
Ville Syrjälä4ae87ff2013-10-17 13:35:05 +03001511 ret = -ENOENT;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001512 goto out;
1513 }
1514
1515 crtc = obj_to_crtc(obj);
1516 du = vmw_crtc_to_du(crtc);
1517
1518 du->hotspot_x = arg->xhot;
1519 du->hotspot_y = arg->yhot;
1520
1521out:
1522 mutex_unlock(&dev->mode_config.mutex);
1523
1524 return ret;
1525}
1526
Michel Dänzer0bef23f2011-08-31 07:42:50 +00001527int vmw_kms_write_svga(struct vmw_private *vmw_priv,
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001528 unsigned width, unsigned height, unsigned pitch,
Michel Dänzer6558429b2011-08-31 07:42:49 +00001529 unsigned bpp, unsigned depth)
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001530{
1531 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1532 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch);
1533 else if (vmw_fifo_have_pitchlock(vmw_priv))
1534 iowrite32(pitch, vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
1535 vmw_write(vmw_priv, SVGA_REG_WIDTH, width);
1536 vmw_write(vmw_priv, SVGA_REG_HEIGHT, height);
Michel Dänzer6558429b2011-08-31 07:42:49 +00001537 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp);
Michel Dänzer0bef23f2011-08-31 07:42:50 +00001538
1539 if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) {
1540 DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n",
1541 depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH));
1542 return -EINVAL;
1543 }
1544
1545 return 0;
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001546}
1547
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001548int vmw_kms_save_vga(struct vmw_private *vmw_priv)
1549{
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001550 struct vmw_vga_topology_state *save;
1551 uint32_t i;
1552
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001553 vmw_priv->vga_width = vmw_read(vmw_priv, SVGA_REG_WIDTH);
1554 vmw_priv->vga_height = vmw_read(vmw_priv, SVGA_REG_HEIGHT);
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001555 vmw_priv->vga_bpp = vmw_read(vmw_priv, SVGA_REG_BITS_PER_PIXEL);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001556 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1557 vmw_priv->vga_pitchlock =
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001558 vmw_read(vmw_priv, SVGA_REG_PITCHLOCK);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001559 else if (vmw_fifo_have_pitchlock(vmw_priv))
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001560 vmw_priv->vga_pitchlock = ioread32(vmw_priv->mmio_virt +
1561 SVGA_FIFO_PITCHLOCK);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001562
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001563 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1564 return 0;
1565
1566 vmw_priv->num_displays = vmw_read(vmw_priv,
1567 SVGA_REG_NUM_GUEST_DISPLAYS);
1568
Thomas Hellstrom029e50b2010-10-05 12:43:08 +02001569 if (vmw_priv->num_displays == 0)
1570 vmw_priv->num_displays = 1;
1571
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001572 for (i = 0; i < vmw_priv->num_displays; ++i) {
1573 save = &vmw_priv->vga_save[i];
1574 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1575 save->primary = vmw_read(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY);
1576 save->pos_x = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_X);
1577 save->pos_y = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y);
1578 save->width = vmw_read(vmw_priv, SVGA_REG_DISPLAY_WIDTH);
1579 save->height = vmw_read(vmw_priv, SVGA_REG_DISPLAY_HEIGHT);
1580 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
Thomas Hellstrom30c78bb2010-10-01 10:21:48 +02001581 if (i == 0 && vmw_priv->num_displays == 1 &&
1582 save->width == 0 && save->height == 0) {
1583
1584 /*
1585 * It should be fairly safe to assume that these
1586 * values are uninitialized.
1587 */
1588
1589 save->width = vmw_priv->vga_width - save->pos_x;
1590 save->height = vmw_priv->vga_height - save->pos_y;
1591 }
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001592 }
Thomas Hellstrom30c78bb2010-10-01 10:21:48 +02001593
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001594 return 0;
1595}
1596
1597int vmw_kms_restore_vga(struct vmw_private *vmw_priv)
1598{
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001599 struct vmw_vga_topology_state *save;
1600 uint32_t i;
1601
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001602 vmw_write(vmw_priv, SVGA_REG_WIDTH, vmw_priv->vga_width);
1603 vmw_write(vmw_priv, SVGA_REG_HEIGHT, vmw_priv->vga_height);
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001604 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, vmw_priv->vga_bpp);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001605 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1606 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK,
1607 vmw_priv->vga_pitchlock);
1608 else if (vmw_fifo_have_pitchlock(vmw_priv))
1609 iowrite32(vmw_priv->vga_pitchlock,
1610 vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001611
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001612 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1613 return 0;
1614
1615 for (i = 0; i < vmw_priv->num_displays; ++i) {
1616 save = &vmw_priv->vga_save[i];
1617 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1618 vmw_write(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY, save->primary);
1619 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_X, save->pos_x);
1620 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y, save->pos_y);
1621 vmw_write(vmw_priv, SVGA_REG_DISPLAY_WIDTH, save->width);
1622 vmw_write(vmw_priv, SVGA_REG_DISPLAY_HEIGHT, save->height);
1623 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
1624 }
1625
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001626 return 0;
1627}
Jakob Bornecrantzd8bd19d2010-06-01 11:54:20 +02001628
Thomas Hellstrome133e732010-10-05 12:43:04 +02001629bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
1630 uint32_t pitch,
1631 uint32_t height)
1632{
1633 return ((u64) pitch * (u64) height) < (u64) dev_priv->vram_size;
1634}
1635
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001636
1637/**
1638 * Function called by DRM code called with vbl_lock held.
1639 */
Thomas Hellstrom7a1c2f62010-10-01 10:21:49 +02001640u32 vmw_get_vblank_counter(struct drm_device *dev, int crtc)
1641{
1642 return 0;
1643}
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001644
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001645/**
1646 * Function called by DRM code called with vbl_lock held.
1647 */
1648int vmw_enable_vblank(struct drm_device *dev, int crtc)
1649{
1650 return -ENOSYS;
1651}
1652
1653/**
1654 * Function called by DRM code called with vbl_lock held.
1655 */
1656void vmw_disable_vblank(struct drm_device *dev, int crtc)
1657{
1658}
1659
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001660
1661/*
1662 * Small shared kms functions.
1663 */
1664
1665int vmw_du_update_layout(struct vmw_private *dev_priv, unsigned num,
1666 struct drm_vmw_rect *rects)
1667{
1668 struct drm_device *dev = dev_priv->dev;
1669 struct vmw_display_unit *du;
1670 struct drm_connector *con;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001671
1672 mutex_lock(&dev->mode_config.mutex);
1673
1674#if 0
Thomas Hellstrom6ea77d12011-10-04 20:13:36 +02001675 {
1676 unsigned int i;
1677
1678 DRM_INFO("%s: new layout ", __func__);
1679 for (i = 0; i < num; i++)
1680 DRM_INFO("(%i, %i %ux%u) ", rects[i].x, rects[i].y,
1681 rects[i].w, rects[i].h);
1682 DRM_INFO("\n");
1683 }
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001684#endif
1685
1686 list_for_each_entry(con, &dev->mode_config.connector_list, head) {
1687 du = vmw_connector_to_du(con);
1688 if (num > du->unit) {
1689 du->pref_width = rects[du->unit].w;
1690 du->pref_height = rects[du->unit].h;
1691 du->pref_active = true;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001692 du->gui_x = rects[du->unit].x;
1693 du->gui_y = rects[du->unit].y;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001694 } else {
1695 du->pref_width = 800;
1696 du->pref_height = 600;
1697 du->pref_active = false;
1698 }
1699 con->status = vmw_du_connector_detect(con, true);
1700 }
1701
1702 mutex_unlock(&dev->mode_config.mutex);
1703
1704 return 0;
1705}
1706
Jakob Bornecrantzb5ec4272012-02-09 16:56:45 +01001707int vmw_du_page_flip(struct drm_crtc *crtc,
1708 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07001709 struct drm_pending_vblank_event *event,
1710 uint32_t page_flip_flags)
Jakob Bornecrantzb5ec4272012-02-09 16:56:45 +01001711{
1712 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
1713 struct drm_framebuffer *old_fb = crtc->fb;
1714 struct vmw_framebuffer *vfb = vmw_framebuffer_to_vfb(fb);
Alan Coxf5869a82012-08-20 14:44:52 +00001715 struct drm_file *file_priv ;
Jakob Bornecrantzb5ec4272012-02-09 16:56:45 +01001716 struct vmw_fence_obj *fence = NULL;
1717 struct drm_clip_rect clips;
1718 int ret;
1719
Alan Coxf5869a82012-08-20 14:44:52 +00001720 if (event == NULL)
1721 return -EINVAL;
1722
Jakob Bornecrantzb5ec4272012-02-09 16:56:45 +01001723 /* require ScreenObject support for page flipping */
1724 if (!dev_priv->sou_priv)
1725 return -ENOSYS;
1726
Alan Coxf5869a82012-08-20 14:44:52 +00001727 file_priv = event->base.file_priv;
Jakob Bornecrantzb5ec4272012-02-09 16:56:45 +01001728 if (!vmw_kms_screen_object_flippable(dev_priv, crtc))
1729 return -EINVAL;
1730
1731 crtc->fb = fb;
1732
1733 /* do a full screen dirty update */
1734 clips.x1 = clips.y1 = 0;
1735 clips.x2 = fb->width;
1736 clips.y2 = fb->height;
1737
1738 if (vfb->dmabuf)
1739 ret = do_dmabuf_dirty_sou(file_priv, dev_priv, vfb,
1740 0, 0, &clips, 1, 1, &fence);
1741 else
1742 ret = do_surface_dirty_sou(dev_priv, file_priv, vfb,
1743 0, 0, &clips, 1, 1, &fence);
1744
1745
1746 if (ret != 0)
1747 goto out_no_fence;
1748 if (!fence) {
1749 ret = -EINVAL;
1750 goto out_no_fence;
1751 }
1752
1753 ret = vmw_event_fence_action_queue(file_priv, fence,
1754 &event->base,
1755 &event->event.tv_sec,
1756 &event->event.tv_usec,
1757 true);
1758
1759 /*
1760 * No need to hold on to this now. The only cleanup
1761 * we need to do if we fail is unref the fence.
1762 */
1763 vmw_fence_obj_unreference(&fence);
1764
1765 if (vmw_crtc_to_du(crtc)->is_implicit)
1766 vmw_kms_screen_object_update_implicit_fb(dev_priv, crtc);
1767
1768 return ret;
1769
1770out_no_fence:
1771 crtc->fb = old_fb;
1772 return ret;
1773}
1774
1775
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001776void vmw_du_crtc_save(struct drm_crtc *crtc)
1777{
1778}
1779
1780void vmw_du_crtc_restore(struct drm_crtc *crtc)
1781{
1782}
1783
1784void vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
1785 u16 *r, u16 *g, u16 *b,
1786 uint32_t start, uint32_t size)
1787{
1788 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
1789 int i;
1790
1791 for (i = 0; i < size; i++) {
1792 DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i,
1793 r[i], g[i], b[i]);
1794 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 0, r[i] >> 8);
1795 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 1, g[i] >> 8);
1796 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 2, b[i] >> 8);
1797 }
1798}
1799
1800void vmw_du_connector_dpms(struct drm_connector *connector, int mode)
1801{
1802}
1803
1804void vmw_du_connector_save(struct drm_connector *connector)
1805{
1806}
1807
1808void vmw_du_connector_restore(struct drm_connector *connector)
1809{
1810}
1811
1812enum drm_connector_status
1813vmw_du_connector_detect(struct drm_connector *connector, bool force)
1814{
1815 uint32_t num_displays;
1816 struct drm_device *dev = connector->dev;
1817 struct vmw_private *dev_priv = vmw_priv(dev);
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001818 struct vmw_display_unit *du = vmw_connector_to_du(connector);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001819
1820 mutex_lock(&dev_priv->hw_mutex);
1821 num_displays = vmw_read(dev_priv, SVGA_REG_NUM_DISPLAYS);
1822 mutex_unlock(&dev_priv->hw_mutex);
1823
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001824 return ((vmw_connector_to_du(connector)->unit < num_displays &&
1825 du->pref_active) ?
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001826 connector_status_connected : connector_status_disconnected);
1827}
1828
1829static struct drm_display_mode vmw_kms_connector_builtin[] = {
1830 /* 640x480@60Hz */
1831 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
1832 752, 800, 0, 480, 489, 492, 525, 0,
1833 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1834 /* 800x600@60Hz */
1835 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
1836 968, 1056, 0, 600, 601, 605, 628, 0,
1837 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1838 /* 1024x768@60Hz */
1839 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
1840 1184, 1344, 0, 768, 771, 777, 806, 0,
1841 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1842 /* 1152x864@75Hz */
1843 { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
1844 1344, 1600, 0, 864, 865, 868, 900, 0,
1845 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1846 /* 1280x768@60Hz */
1847 { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344,
1848 1472, 1664, 0, 768, 771, 778, 798, 0,
1849 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1850 /* 1280x800@60Hz */
1851 { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352,
1852 1480, 1680, 0, 800, 803, 809, 831, 0,
1853 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
1854 /* 1280x960@60Hz */
1855 { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376,
1856 1488, 1800, 0, 960, 961, 964, 1000, 0,
1857 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1858 /* 1280x1024@60Hz */
1859 { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328,
1860 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
1861 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1862 /* 1360x768@60Hz */
1863 { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424,
1864 1536, 1792, 0, 768, 771, 777, 795, 0,
1865 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1866 /* 1440x1050@60Hz */
1867 { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488,
1868 1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
1869 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1870 /* 1440x900@60Hz */
1871 { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520,
1872 1672, 1904, 0, 900, 903, 909, 934, 0,
1873 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1874 /* 1600x1200@60Hz */
1875 { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664,
1876 1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
1877 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1878 /* 1680x1050@60Hz */
1879 { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784,
1880 1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
1881 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1882 /* 1792x1344@60Hz */
1883 { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920,
1884 2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
1885 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1886 /* 1853x1392@60Hz */
1887 { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952,
1888 2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
1889 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1890 /* 1920x1200@60Hz */
1891 { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056,
1892 2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
1893 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1894 /* 1920x1440@60Hz */
1895 { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048,
1896 2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
1897 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1898 /* 2560x1600@60Hz */
1899 { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752,
1900 3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
1901 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1902 /* Terminate */
1903 { DRM_MODE("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) },
1904};
1905
Thomas Hellstrom1543b4d2011-11-02 09:43:10 +01001906/**
1907 * vmw_guess_mode_timing - Provide fake timings for a
1908 * 60Hz vrefresh mode.
1909 *
1910 * @mode - Pointer to a struct drm_display_mode with hdisplay and vdisplay
1911 * members filled in.
1912 */
1913static void vmw_guess_mode_timing(struct drm_display_mode *mode)
1914{
1915 mode->hsync_start = mode->hdisplay + 50;
1916 mode->hsync_end = mode->hsync_start + 50;
1917 mode->htotal = mode->hsync_end + 50;
1918
1919 mode->vsync_start = mode->vdisplay + 50;
1920 mode->vsync_end = mode->vsync_start + 50;
1921 mode->vtotal = mode->vsync_end + 50;
1922
1923 mode->clock = (u32)mode->htotal * (u32)mode->vtotal / 100 * 6;
1924 mode->vrefresh = drm_mode_vrefresh(mode);
1925}
1926
1927
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001928int vmw_du_connector_fill_modes(struct drm_connector *connector,
1929 uint32_t max_width, uint32_t max_height)
1930{
1931 struct vmw_display_unit *du = vmw_connector_to_du(connector);
1932 struct drm_device *dev = connector->dev;
1933 struct vmw_private *dev_priv = vmw_priv(dev);
1934 struct drm_display_mode *mode = NULL;
1935 struct drm_display_mode *bmode;
1936 struct drm_display_mode prefmode = { DRM_MODE("preferred",
1937 DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
1938 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1939 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
1940 };
1941 int i;
1942
1943 /* Add preferred mode */
1944 {
1945 mode = drm_mode_duplicate(dev, &prefmode);
1946 if (!mode)
1947 return 0;
1948 mode->hdisplay = du->pref_width;
1949 mode->vdisplay = du->pref_height;
Thomas Hellstrom1543b4d2011-11-02 09:43:10 +01001950 vmw_guess_mode_timing(mode);
Jakob Bornecrantz55bde5b2011-11-03 21:03:05 +01001951
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001952 if (vmw_kms_validate_mode_vram(dev_priv, mode->hdisplay * 2,
1953 mode->vdisplay)) {
1954 drm_mode_probed_add(connector, mode);
Jakob Bornecrantz55bde5b2011-11-03 21:03:05 +01001955 } else {
1956 drm_mode_destroy(dev, mode);
1957 mode = NULL;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001958 }
Jakob Bornecrantz55bde5b2011-11-03 21:03:05 +01001959
1960 if (du->pref_mode) {
1961 list_del_init(&du->pref_mode->head);
1962 drm_mode_destroy(dev, du->pref_mode);
1963 }
1964
1965 /* mode might be null here, this is intended */
1966 du->pref_mode = mode;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001967 }
1968
1969 for (i = 0; vmw_kms_connector_builtin[i].type != 0; i++) {
1970 bmode = &vmw_kms_connector_builtin[i];
1971 if (bmode->hdisplay > max_width ||
1972 bmode->vdisplay > max_height)
1973 continue;
1974
1975 if (!vmw_kms_validate_mode_vram(dev_priv, bmode->hdisplay * 2,
1976 bmode->vdisplay))
1977 continue;
1978
1979 mode = drm_mode_duplicate(dev, bmode);
1980 if (!mode)
1981 return 0;
1982 mode->vrefresh = drm_mode_vrefresh(mode);
1983
1984 drm_mode_probed_add(connector, mode);
1985 }
1986
Jakob Bornecrantzd41025c2011-11-03 21:03:07 +01001987 /* Move the prefered mode first, help apps pick the right mode. */
1988 if (du->pref_mode)
1989 list_move(&du->pref_mode->head, &connector->probed_modes);
1990
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001991 drm_mode_connector_list_update(connector);
1992
1993 return 1;
1994}
1995
1996int vmw_du_connector_set_property(struct drm_connector *connector,
1997 struct drm_property *property,
1998 uint64_t val)
1999{
2000 return 0;
2001}
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002002
2003
2004int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data,
2005 struct drm_file *file_priv)
2006{
2007 struct vmw_private *dev_priv = vmw_priv(dev);
2008 struct drm_vmw_update_layout_arg *arg =
2009 (struct drm_vmw_update_layout_arg *)data;
2010 struct vmw_master *vmaster = vmw_master(file_priv->master);
2011 void __user *user_rects;
2012 struct drm_vmw_rect *rects;
2013 unsigned rects_size;
2014 int ret;
2015 int i;
2016 struct drm_mode_config *mode_config = &dev->mode_config;
2017
2018 ret = ttm_read_lock(&vmaster->lock, true);
2019 if (unlikely(ret != 0))
2020 return ret;
2021
2022 if (!arg->num_outputs) {
2023 struct drm_vmw_rect def_rect = {0, 0, 800, 600};
2024 vmw_du_update_layout(dev_priv, 1, &def_rect);
2025 goto out_unlock;
2026 }
2027
2028 rects_size = arg->num_outputs * sizeof(struct drm_vmw_rect);
Xi Wangbab9efc2011-11-28 12:25:43 +01002029 rects = kcalloc(arg->num_outputs, sizeof(struct drm_vmw_rect),
2030 GFP_KERNEL);
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002031 if (unlikely(!rects)) {
2032 ret = -ENOMEM;
2033 goto out_unlock;
2034 }
2035
2036 user_rects = (void __user *)(unsigned long)arg->rects;
2037 ret = copy_from_user(rects, user_rects, rects_size);
2038 if (unlikely(ret != 0)) {
2039 DRM_ERROR("Failed to get rects.\n");
2040 ret = -EFAULT;
2041 goto out_free;
2042 }
2043
2044 for (i = 0; i < arg->num_outputs; ++i) {
Xi Wangbab9efc2011-11-28 12:25:43 +01002045 if (rects[i].x < 0 ||
2046 rects[i].y < 0 ||
2047 rects[i].x + rects[i].w > mode_config->max_width ||
2048 rects[i].y + rects[i].h > mode_config->max_height) {
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002049 DRM_ERROR("Invalid GUI layout.\n");
2050 ret = -EINVAL;
2051 goto out_free;
2052 }
2053 }
2054
2055 vmw_du_update_layout(dev_priv, arg->num_outputs, rects);
2056
2057out_free:
2058 kfree(rects);
2059out_unlock:
2060 ttm_read_unlock(&vmaster->lock);
2061 return ret;
2062}