blob: edc97929c9a342806bfe2544eb79649ac335722a [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);
177 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
178 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
Jakob Bornecrantzbaa91d642011-11-09 10:25:28 +0100183 /* A lot of the code assumes this */
184 if (handle && (width != 64 || height != 64))
185 return -EINVAL;
186
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000187 if (handle) {
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100188 ret = vmw_user_lookup_handle(dev_priv, tfile,
189 handle, &surface, &dmabuf);
190 if (ret) {
191 DRM_ERROR("failed to find surface or dmabuf: %i\n", ret);
192 return -EINVAL;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000193 }
194 }
195
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100196 /* need to do this before taking down old image */
197 if (surface && !surface->snooper.image) {
198 DRM_ERROR("surface not suitable for cursor\n");
199 vmw_surface_unreference(&surface);
200 return -EINVAL;
201 }
202
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000203 /* takedown old cursor */
204 if (du->cursor_surface) {
205 du->cursor_surface->snooper.crtc = NULL;
206 vmw_surface_unreference(&du->cursor_surface);
207 }
208 if (du->cursor_dmabuf)
209 vmw_dmabuf_unreference(&du->cursor_dmabuf);
210
211 /* setup new image */
212 if (surface) {
213 /* vmw_user_surface_lookup takes one reference */
214 du->cursor_surface = surface;
215
216 du->cursor_surface->snooper.crtc = crtc;
217 du->cursor_age = du->cursor_surface->snooper.age;
218 vmw_cursor_update_image(dev_priv, surface->snooper.image,
219 64, 64, du->hotspot_x, du->hotspot_y);
220 } else if (dmabuf) {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000221 /* vmw_user_surface_lookup takes one reference */
222 du->cursor_dmabuf = dmabuf;
223
Jakob Bornecrantz6a91d972011-11-28 13:19:10 +0100224 ret = vmw_cursor_update_dmabuf(dev_priv, dmabuf, width, height,
225 du->hotspot_x, du->hotspot_y);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000226 } else {
227 vmw_cursor_update_position(dev_priv, false, 0, 0);
228 return 0;
229 }
230
Thomas Hellstromda7653d2011-11-02 09:43:12 +0100231 vmw_cursor_update_position(dev_priv, true,
232 du->cursor_x + du->hotspot_x,
233 du->cursor_y + du->hotspot_y);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000234
235 return 0;
236}
237
238int vmw_du_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
239{
240 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
241 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
242 bool shown = du->cursor_surface || du->cursor_dmabuf ? true : false;
243
244 du->cursor_x = x + crtc->x;
245 du->cursor_y = y + crtc->y;
246
247 vmw_cursor_update_position(dev_priv, shown,
Thomas Hellstromda7653d2011-11-02 09:43:12 +0100248 du->cursor_x + du->hotspot_x,
249 du->cursor_y + du->hotspot_y);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000250
251 return 0;
252}
253
254void vmw_kms_cursor_snoop(struct vmw_surface *srf,
255 struct ttm_object_file *tfile,
256 struct ttm_buffer_object *bo,
257 SVGA3dCmdHeader *header)
258{
259 struct ttm_bo_kmap_obj map;
260 unsigned long kmap_offset;
261 unsigned long kmap_num;
262 SVGA3dCopyBox *box;
263 unsigned box_count;
264 void *virtual;
265 bool dummy;
266 struct vmw_dma_cmd {
267 SVGA3dCmdHeader header;
268 SVGA3dCmdSurfaceDMA dma;
269 } *cmd;
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100270 int i, ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000271
272 cmd = container_of(header, struct vmw_dma_cmd, header);
273
274 /* No snooper installed */
275 if (!srf->snooper.image)
276 return;
277
278 if (cmd->dma.host.face != 0 || cmd->dma.host.mipmap != 0) {
279 DRM_ERROR("face and mipmap for cursors should never != 0\n");
280 return;
281 }
282
283 if (cmd->header.size < 64) {
284 DRM_ERROR("at least one full copy box must be given\n");
285 return;
286 }
287
288 box = (SVGA3dCopyBox *)&cmd[1];
289 box_count = (cmd->header.size - sizeof(SVGA3dCmdSurfaceDMA)) /
290 sizeof(SVGA3dCopyBox);
291
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100292 if (cmd->dma.guest.ptr.offset % PAGE_SIZE ||
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000293 box->x != 0 || box->y != 0 || box->z != 0 ||
294 box->srcx != 0 || box->srcy != 0 || box->srcz != 0 ||
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100295 box->d != 1 || box_count != 1) {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000296 /* TODO handle none page aligned offsets */
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100297 /* TODO handle more dst & src != 0 */
298 /* TODO handle more then one copy */
299 DRM_ERROR("Cant snoop dma request for cursor!\n");
300 DRM_ERROR("(%u, %u, %u) (%u, %u, %u) (%ux%ux%u) %u %u\n",
301 box->srcx, box->srcy, box->srcz,
302 box->x, box->y, box->z,
303 box->w, box->h, box->d, box_count,
304 cmd->dma.guest.ptr.offset);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000305 return;
306 }
307
308 kmap_offset = cmd->dma.guest.ptr.offset >> PAGE_SHIFT;
309 kmap_num = (64*64*4) >> PAGE_SHIFT;
310
311 ret = ttm_bo_reserve(bo, true, false, false, 0);
312 if (unlikely(ret != 0)) {
313 DRM_ERROR("reserve failed\n");
314 return;
315 }
316
317 ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
318 if (unlikely(ret != 0))
319 goto err_unreserve;
320
321 virtual = ttm_kmap_obj_virtual(&map, &dummy);
322
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100323 if (box->w == 64 && cmd->dma.guest.pitch == 64*4) {
324 memcpy(srf->snooper.image, virtual, 64*64*4);
325 } else {
326 /* Image is unsigned pointer. */
327 for (i = 0; i < box->h; i++)
328 memcpy(srf->snooper.image + i * 64,
329 virtual + i * cmd->dma.guest.pitch,
330 box->w * 4);
331 }
332
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000333 srf->snooper.age++;
334
335 /* we can't call this function from this function since execbuf has
336 * reserved fifo space.
337 *
338 * if (srf->snooper.crtc)
339 * vmw_ldu_crtc_cursor_update_image(dev_priv,
340 * srf->snooper.image, 64, 64,
341 * du->hotspot_x, du->hotspot_y);
342 */
343
344 ttm_bo_kunmap(&map);
345err_unreserve:
346 ttm_bo_unreserve(bo);
347}
348
349void vmw_kms_cursor_post_execbuf(struct vmw_private *dev_priv)
350{
351 struct drm_device *dev = dev_priv->dev;
352 struct vmw_display_unit *du;
353 struct drm_crtc *crtc;
354
355 mutex_lock(&dev->mode_config.mutex);
356
357 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
358 du = vmw_crtc_to_du(crtc);
359 if (!du->cursor_surface ||
360 du->cursor_age == du->cursor_surface->snooper.age)
361 continue;
362
363 du->cursor_age = du->cursor_surface->snooper.age;
364 vmw_cursor_update_image(dev_priv,
365 du->cursor_surface->snooper.image,
366 64, 64, du->hotspot_x, du->hotspot_y);
367 }
368
369 mutex_unlock(&dev->mode_config.mutex);
370}
371
372/*
373 * Generic framebuffer code
374 */
375
376int vmw_framebuffer_create_handle(struct drm_framebuffer *fb,
377 struct drm_file *file_priv,
378 unsigned int *handle)
379{
380 if (handle)
Ryan Mallonbf9c05d2012-01-28 08:51:40 +1100381 *handle = 0;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000382
383 return 0;
384}
385
386/*
387 * Surface framebuffer code
388 */
389
390#define vmw_framebuffer_to_vfbs(x) \
391 container_of(x, struct vmw_framebuffer_surface, base.base)
392
393struct vmw_framebuffer_surface {
394 struct vmw_framebuffer base;
395 struct vmw_surface *surface;
Thomas Hellstrom22ee8612010-05-28 11:22:00 +0200396 struct vmw_dma_buffer *buffer;
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200397 struct list_head head;
398 struct drm_master *master;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000399};
400
401void vmw_framebuffer_surface_destroy(struct drm_framebuffer *framebuffer)
402{
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200403 struct vmw_framebuffer_surface *vfbs =
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000404 vmw_framebuffer_to_vfbs(framebuffer);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200405 struct vmw_master *vmaster = vmw_master(vfbs->master);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000406
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200407
408 mutex_lock(&vmaster->fb_surf_mutex);
409 list_del(&vfbs->head);
410 mutex_unlock(&vmaster->fb_surf_mutex);
411
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200412 drm_master_put(&vfbs->master);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000413 drm_framebuffer_cleanup(framebuffer);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200414 vmw_surface_unreference(&vfbs->surface);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200415 ttm_base_object_unref(&vfbs->base.user_obj);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000416
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200417 kfree(vfbs);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000418}
419
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200420static int do_surface_dirty_sou(struct vmw_private *dev_priv,
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200421 struct drm_file *file_priv,
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200422 struct vmw_framebuffer *framebuffer,
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200423 unsigned flags, unsigned color,
424 struct drm_clip_rect *clips,
Jakob Bornecrantzbd49ae42012-02-09 16:56:44 +0100425 unsigned num_clips, int inc,
426 struct vmw_fence_obj **out_fence)
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200427{
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200428 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100429 struct drm_clip_rect *clips_ptr;
430 struct drm_clip_rect *tmp;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200431 struct drm_crtc *crtc;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200432 size_t fifo_size;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200433 int i, num_units;
434 int ret = 0; /* silence warning */
435 int left, right, top, bottom;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200436
437 struct {
438 SVGA3dCmdHeader header;
439 SVGA3dCmdBlitSurfaceToScreen body;
440 } *cmd;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200441 SVGASignedRect *blits;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200442
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200443 num_units = 0;
444 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list,
445 head) {
446 if (crtc->fb != &framebuffer->base)
447 continue;
448 units[num_units++] = vmw_crtc_to_du(crtc);
449 }
450
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200451 BUG_ON(!clips || !num_clips);
452
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100453 tmp = kzalloc(sizeof(*tmp) * num_clips, GFP_KERNEL);
454 if (unlikely(tmp == NULL)) {
455 DRM_ERROR("Temporary cliprect memory alloc failed.\n");
456 return -ENOMEM;
457 }
458
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200459 fifo_size = sizeof(*cmd) + sizeof(SVGASignedRect) * num_clips;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200460 cmd = kzalloc(fifo_size, GFP_KERNEL);
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200461 if (unlikely(cmd == NULL)) {
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200462 DRM_ERROR("Temporary fifo memory alloc failed.\n");
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100463 ret = -ENOMEM;
464 goto out_free_tmp;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200465 }
466
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100467 /* setup blits pointer */
468 blits = (SVGASignedRect *)&cmd[1];
469
470 /* initial clip region */
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200471 left = clips->x1;
472 right = clips->x2;
473 top = clips->y1;
474 bottom = clips->y2;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200475
Jakob Bornecrantzf0c8a652011-11-09 10:25:27 +0100476 /* skip the first clip rect */
477 for (i = 1, clips_ptr = clips + inc;
478 i < num_clips; i++, clips_ptr += inc) {
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200479 left = min_t(int, left, (int)clips_ptr->x1);
480 right = max_t(int, right, (int)clips_ptr->x2);
481 top = min_t(int, top, (int)clips_ptr->y1);
482 bottom = max_t(int, bottom, (int)clips_ptr->y2);
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200483 }
484
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200485 /* only need to do this once */
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200486 cmd->header.id = cpu_to_le32(SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN);
487 cmd->header.size = cpu_to_le32(fifo_size - sizeof(cmd->header));
488
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200489 cmd->body.srcRect.left = left;
490 cmd->body.srcRect.right = right;
491 cmd->body.srcRect.top = top;
492 cmd->body.srcRect.bottom = bottom;
493
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200494 clips_ptr = clips;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200495 for (i = 0; i < num_clips; i++, clips_ptr += inc) {
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100496 tmp[i].x1 = clips_ptr->x1 - left;
497 tmp[i].x2 = clips_ptr->x2 - left;
498 tmp[i].y1 = clips_ptr->y1 - top;
499 tmp[i].y2 = clips_ptr->y2 - top;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200500 }
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200501
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200502 /* do per unit writing, reuse fifo for each */
503 for (i = 0; i < num_units; i++) {
504 struct vmw_display_unit *unit = units[i];
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100505 struct vmw_clip_rect clip;
506 int num;
507
508 clip.x1 = left - unit->crtc.x;
509 clip.y1 = top - unit->crtc.y;
510 clip.x2 = right - unit->crtc.x;
511 clip.y2 = bottom - unit->crtc.y;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200512
513 /* skip any crtcs that misses the clip region */
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100514 if (clip.x1 >= unit->crtc.mode.hdisplay ||
515 clip.y1 >= unit->crtc.mode.vdisplay ||
516 clip.x2 <= 0 || clip.y2 <= 0)
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200517 continue;
518
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100519 /*
520 * In order for the clip rects to be correctly scaled
521 * the src and dest rects needs to be the same size.
522 */
523 cmd->body.destRect.left = clip.x1;
524 cmd->body.destRect.right = clip.x2;
525 cmd->body.destRect.top = clip.y1;
526 cmd->body.destRect.bottom = clip.y2;
527
528 /* create a clip rect of the crtc in dest coords */
529 clip.x2 = unit->crtc.mode.hdisplay - clip.x1;
530 clip.y2 = unit->crtc.mode.vdisplay - clip.y1;
531 clip.x1 = 0 - clip.x1;
532 clip.y1 = 0 - clip.y1;
533
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200534 /* need to reset sid as it is changed by execbuf */
535 cmd->body.srcImage.sid = cpu_to_le32(framebuffer->user_handle);
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200536 cmd->body.destScreenId = unit->unit;
537
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100538 /* clip and write blits to cmd stream */
539 vmw_clip_cliprects(tmp, num_clips, clip, blits, &num);
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200540
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100541 /* if no cliprects hit skip this */
542 if (num == 0)
543 continue;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200544
Jakob Bornecrantzbd49ae42012-02-09 16:56:44 +0100545 /* only return the last fence */
546 if (out_fence && *out_fence)
547 vmw_fence_obj_unreference(out_fence);
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200548
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100549 /* recalculate package length */
550 fifo_size = sizeof(*cmd) + sizeof(SVGASignedRect) * num;
551 cmd->header.size = cpu_to_le32(fifo_size - sizeof(cmd->header));
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200552 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd,
Jakob Bornecrantzbd49ae42012-02-09 16:56:44 +0100553 fifo_size, 0, NULL, out_fence);
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200554
555 if (unlikely(ret != 0))
556 break;
557 }
558
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100559
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200560 kfree(cmd);
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100561out_free_tmp:
562 kfree(tmp);
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200563
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200564 return ret;
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200565}
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000566
567int vmw_framebuffer_surface_dirty(struct drm_framebuffer *framebuffer,
Thomas Hellstrom02b00162010-10-05 12:43:02 +0200568 struct drm_file *file_priv,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000569 unsigned flags, unsigned color,
570 struct drm_clip_rect *clips,
571 unsigned num_clips)
572{
573 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200574 struct vmw_master *vmaster = vmw_master(file_priv->master);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000575 struct vmw_framebuffer_surface *vfbs =
576 vmw_framebuffer_to_vfbs(framebuffer);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000577 struct drm_clip_rect norect;
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200578 int ret, inc = 1;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000579
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200580 if (unlikely(vfbs->master != file_priv->master))
581 return -EINVAL;
582
Jakob Bornecrantz01e81412011-10-04 20:13:24 +0200583 /* Require ScreenObject support for 3D */
584 if (!dev_priv->sou_priv)
585 return -EINVAL;
586
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200587 ret = ttm_read_lock(&vmaster->lock, true);
588 if (unlikely(ret != 0))
589 return ret;
590
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000591 if (!num_clips) {
592 num_clips = 1;
593 clips = &norect;
594 norect.x1 = norect.y1 = 0;
595 norect.x2 = framebuffer->width;
596 norect.y2 = framebuffer->height;
597 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
598 num_clips /= 2;
599 inc = 2; /* skip source rects */
600 }
601
Jakob Bornecrantzc5c42362011-10-22 10:29:34 +0200602 ret = do_surface_dirty_sou(dev_priv, file_priv, &vfbs->base,
Jakob Bornecrantz01e81412011-10-04 20:13:24 +0200603 flags, color,
Jakob Bornecrantzbd49ae42012-02-09 16:56:44 +0100604 clips, num_clips, inc, NULL);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000605
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200606 ttm_read_unlock(&vmaster->lock);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000607 return 0;
608}
609
610static struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs = {
611 .destroy = vmw_framebuffer_surface_destroy,
612 .dirty = vmw_framebuffer_surface_dirty,
613 .create_handle = vmw_framebuffer_create_handle,
614};
615
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200616static int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv,
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200617 struct drm_file *file_priv,
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200618 struct vmw_surface *surface,
619 struct vmw_framebuffer **out,
620 const struct drm_mode_fb_cmd
621 *mode_cmd)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000622
623{
624 struct drm_device *dev = dev_priv->dev;
625 struct vmw_framebuffer_surface *vfbs;
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200626 enum SVGA3dSurfaceFormat format;
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200627 struct vmw_master *vmaster = vmw_master(file_priv->master);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000628 int ret;
629
Jakob Bornecrantz01e81412011-10-04 20:13:24 +0200630 /* 3D is only supported on HWv8 hosts which supports screen objects */
631 if (!dev_priv->sou_priv)
632 return -ENOSYS;
633
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200634 /*
635 * Sanity checks.
636 */
637
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100638 /* Surface must be marked as a scanout. */
639 if (unlikely(!surface->scanout))
640 return -EINVAL;
641
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200642 if (unlikely(surface->mip_levels[0] != 1 ||
643 surface->num_sizes != 1 ||
644 surface->sizes[0].width < mode_cmd->width ||
645 surface->sizes[0].height < mode_cmd->height ||
646 surface->sizes[0].depth != 1)) {
647 DRM_ERROR("Incompatible surface dimensions "
648 "for requested mode.\n");
649 return -EINVAL;
650 }
651
652 switch (mode_cmd->depth) {
653 case 32:
654 format = SVGA3D_A8R8G8B8;
655 break;
656 case 24:
657 format = SVGA3D_X8R8G8B8;
658 break;
659 case 16:
660 format = SVGA3D_R5G6B5;
661 break;
662 case 15:
663 format = SVGA3D_A1R5G5B5;
664 break;
Michel Dänzerf01b7ba2011-08-31 07:42:47 +0000665 case 8:
666 format = SVGA3D_LUMINANCE8;
667 break;
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200668 default:
669 DRM_ERROR("Invalid color depth: %d\n", mode_cmd->depth);
670 return -EINVAL;
671 }
672
673 if (unlikely(format != surface->format)) {
674 DRM_ERROR("Invalid surface format for requested mode.\n");
675 return -EINVAL;
676 }
677
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000678 vfbs = kzalloc(sizeof(*vfbs), GFP_KERNEL);
679 if (!vfbs) {
680 ret = -ENOMEM;
681 goto out_err1;
682 }
683
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000684 if (!vmw_surface_reference(surface)) {
685 DRM_ERROR("failed to reference surface %p\n", surface);
Daniel Vetter80f0b5a2012-12-13 23:39:01 +0100686 ret = -EINVAL;
687 goto out_err2;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000688 }
689
690 /* XXX get the first 3 from the surface info */
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200691 vfbs->base.base.bits_per_pixel = mode_cmd->bpp;
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200692 vfbs->base.base.pitches[0] = mode_cmd->pitch;
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200693 vfbs->base.base.depth = mode_cmd->depth;
694 vfbs->base.base.width = mode_cmd->width;
695 vfbs->base.base.height = mode_cmd->height;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000696 vfbs->surface = surface;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200697 vfbs->base.user_handle = mode_cmd->handle;
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200698 vfbs->master = drm_master_get(file_priv->master);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200699
700 mutex_lock(&vmaster->fb_surf_mutex);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200701 list_add_tail(&vfbs->head, &vmaster->fb_surf);
702 mutex_unlock(&vmaster->fb_surf_mutex);
703
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000704 *out = &vfbs->base;
705
Daniel Vetter80f0b5a2012-12-13 23:39:01 +0100706 ret = drm_framebuffer_init(dev, &vfbs->base.base,
707 &vmw_framebuffer_surface_funcs);
708 if (ret)
709 goto out_err3;
710
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000711 return 0;
712
713out_err3:
Daniel Vetter80f0b5a2012-12-13 23:39:01 +0100714 vmw_surface_unreference(&surface);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000715out_err2:
716 kfree(vfbs);
717out_err1:
718 return ret;
719}
720
721/*
722 * Dmabuf framebuffer code
723 */
724
725#define vmw_framebuffer_to_vfbd(x) \
726 container_of(x, struct vmw_framebuffer_dmabuf, base.base)
727
728struct vmw_framebuffer_dmabuf {
729 struct vmw_framebuffer base;
730 struct vmw_dma_buffer *buffer;
731};
732
733void vmw_framebuffer_dmabuf_destroy(struct drm_framebuffer *framebuffer)
734{
735 struct vmw_framebuffer_dmabuf *vfbd =
736 vmw_framebuffer_to_vfbd(framebuffer);
737
738 drm_framebuffer_cleanup(framebuffer);
739 vmw_dmabuf_unreference(&vfbd->buffer);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200740 ttm_base_object_unref(&vfbd->base.user_obj);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000741
742 kfree(vfbd);
743}
744
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200745static int do_dmabuf_dirty_ldu(struct vmw_private *dev_priv,
746 struct vmw_framebuffer *framebuffer,
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200747 unsigned flags, unsigned color,
748 struct drm_clip_rect *clips,
749 unsigned num_clips, int increment)
750{
751 size_t fifo_size;
752 int i;
753
754 struct {
755 uint32_t header;
756 SVGAFifoCmdUpdate body;
757 } *cmd;
758
759 fifo_size = sizeof(*cmd) * num_clips;
760 cmd = vmw_fifo_reserve(dev_priv, fifo_size);
761 if (unlikely(cmd == NULL)) {
762 DRM_ERROR("Fifo reserve failed.\n");
763 return -ENOMEM;
764 }
765
766 memset(cmd, 0, fifo_size);
767 for (i = 0; i < num_clips; i++, clips += increment) {
768 cmd[i].header = cpu_to_le32(SVGA_CMD_UPDATE);
769 cmd[i].body.x = cpu_to_le32(clips->x1);
770 cmd[i].body.y = cpu_to_le32(clips->y1);
771 cmd[i].body.width = cpu_to_le32(clips->x2 - clips->x1);
772 cmd[i].body.height = cpu_to_le32(clips->y2 - clips->y1);
773 }
774
775 vmw_fifo_commit(dev_priv, fifo_size);
776 return 0;
777}
778
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200779static int do_dmabuf_define_gmrfb(struct drm_file *file_priv,
780 struct vmw_private *dev_priv,
781 struct vmw_framebuffer *framebuffer)
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200782{
Jakob Bornecrantz64fc9942011-10-22 10:29:33 +0200783 int depth = framebuffer->base.depth;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200784 size_t fifo_size;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200785 int ret;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200786
787 struct {
788 uint32_t header;
789 SVGAFifoCmdDefineGMRFB body;
790 } *cmd;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200791
Jakob Bornecrantz64fc9942011-10-22 10:29:33 +0200792 /* Emulate RGBA support, contrary to svga_reg.h this is not
793 * supported by hosts. This is only a problem if we are reading
794 * this value later and expecting what we uploaded back.
795 */
796 if (depth == 32)
797 depth = 24;
798
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200799 fifo_size = sizeof(*cmd);
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200800 cmd = kmalloc(fifo_size, GFP_KERNEL);
801 if (unlikely(cmd == NULL)) {
802 DRM_ERROR("Failed to allocate temporary cmd buffer.\n");
803 return -ENOMEM;
804 }
805
806 memset(cmd, 0, fifo_size);
807 cmd->header = SVGA_CMD_DEFINE_GMRFB;
808 cmd->body.format.bitsPerPixel = framebuffer->base.bits_per_pixel;
Jakob Bornecrantz64fc9942011-10-22 10:29:33 +0200809 cmd->body.format.colorDepth = depth;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200810 cmd->body.format.reserved = 0;
Ville Syrjälä01f2c772011-12-20 00:06:49 +0200811 cmd->body.bytesPerLine = framebuffer->base.pitches[0];
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200812 cmd->body.ptr.gmrId = framebuffer->user_handle;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200813 cmd->body.ptr.offset = 0;
814
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200815 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd,
Jakob Bornecrantzbb1bd2f2012-02-09 16:56:43 +0100816 fifo_size, 0, NULL, NULL);
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200817
818 kfree(cmd);
819
820 return ret;
821}
822
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200823static int do_dmabuf_dirty_sou(struct drm_file *file_priv,
824 struct vmw_private *dev_priv,
825 struct vmw_framebuffer *framebuffer,
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200826 unsigned flags, unsigned color,
827 struct drm_clip_rect *clips,
Jakob Bornecrantzbd49ae42012-02-09 16:56:44 +0100828 unsigned num_clips, int increment,
829 struct vmw_fence_obj **out_fence)
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200830{
831 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
832 struct drm_clip_rect *clips_ptr;
833 int i, k, num_units, ret;
834 struct drm_crtc *crtc;
835 size_t fifo_size;
836
837 struct {
838 uint32_t header;
839 SVGAFifoCmdBlitGMRFBToScreen body;
840 } *blits;
841
842 ret = do_dmabuf_define_gmrfb(file_priv, dev_priv, framebuffer);
843 if (unlikely(ret != 0))
844 return ret; /* define_gmrfb prints warnings */
845
846 fifo_size = sizeof(*blits) * num_clips;
847 blits = kmalloc(fifo_size, GFP_KERNEL);
848 if (unlikely(blits == NULL)) {
849 DRM_ERROR("Failed to allocate temporary cmd buffer.\n");
850 return -ENOMEM;
851 }
852
853 num_units = 0;
854 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
855 if (crtc->fb != &framebuffer->base)
856 continue;
857 units[num_units++] = vmw_crtc_to_du(crtc);
858 }
859
860 for (k = 0; k < num_units; k++) {
861 struct vmw_display_unit *unit = units[k];
862 int hit_num = 0;
863
864 clips_ptr = clips;
865 for (i = 0; i < num_clips; i++, clips_ptr += increment) {
866 int clip_x1 = clips_ptr->x1 - unit->crtc.x;
867 int clip_y1 = clips_ptr->y1 - unit->crtc.y;
868 int clip_x2 = clips_ptr->x2 - unit->crtc.x;
869 int clip_y2 = clips_ptr->y2 - unit->crtc.y;
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100870 int move_x, move_y;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200871
872 /* skip any crtcs that misses the clip region */
873 if (clip_x1 >= unit->crtc.mode.hdisplay ||
874 clip_y1 >= unit->crtc.mode.vdisplay ||
875 clip_x2 <= 0 || clip_y2 <= 0)
876 continue;
877
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100878 /* clip size to crtc size */
879 clip_x2 = min_t(int, clip_x2, unit->crtc.mode.hdisplay);
880 clip_y2 = min_t(int, clip_y2, unit->crtc.mode.vdisplay);
881
882 /* translate both src and dest to bring clip into screen */
883 move_x = min_t(int, clip_x1, 0);
884 move_y = min_t(int, clip_y1, 0);
885
886 /* actual translate done here */
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200887 blits[hit_num].header = SVGA_CMD_BLIT_GMRFB_TO_SCREEN;
888 blits[hit_num].body.destScreenId = unit->unit;
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +0100889 blits[hit_num].body.srcOrigin.x = clips_ptr->x1 - move_x;
890 blits[hit_num].body.srcOrigin.y = clips_ptr->y1 - move_y;
891 blits[hit_num].body.destRect.left = clip_x1 - move_x;
892 blits[hit_num].body.destRect.top = clip_y1 - move_y;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200893 blits[hit_num].body.destRect.right = clip_x2;
894 blits[hit_num].body.destRect.bottom = clip_y2;
895 hit_num++;
896 }
897
898 /* no clips hit the crtc */
899 if (hit_num == 0)
900 continue;
901
Jakob Bornecrantzbd49ae42012-02-09 16:56:44 +0100902 /* only return the last fence */
903 if (out_fence && *out_fence)
904 vmw_fence_obj_unreference(out_fence);
905
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200906 fifo_size = sizeof(*blits) * hit_num;
907 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, blits,
Jakob Bornecrantzbd49ae42012-02-09 16:56:44 +0100908 fifo_size, 0, NULL, out_fence);
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +0200909
910 if (unlikely(ret != 0))
911 break;
912 }
913
914 kfree(blits);
915
916 return ret;
917}
918
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000919int vmw_framebuffer_dmabuf_dirty(struct drm_framebuffer *framebuffer,
Thomas Hellstrom02b00162010-10-05 12:43:02 +0200920 struct drm_file *file_priv,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000921 unsigned flags, unsigned color,
922 struct drm_clip_rect *clips,
923 unsigned num_clips)
924{
925 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200926 struct vmw_master *vmaster = vmw_master(file_priv->master);
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200927 struct vmw_framebuffer_dmabuf *vfbd =
928 vmw_framebuffer_to_vfbd(framebuffer);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000929 struct drm_clip_rect norect;
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200930 int ret, increment = 1;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000931
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200932 ret = ttm_read_lock(&vmaster->lock, true);
933 if (unlikely(ret != 0))
934 return ret;
935
Thomas Hellstromdf1c93b2010-01-13 22:28:36 +0100936 if (!num_clips) {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000937 num_clips = 1;
938 clips = &norect;
939 norect.x1 = norect.y1 = 0;
940 norect.x2 = framebuffer->width;
941 norect.y2 = framebuffer->height;
942 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
943 num_clips /= 2;
944 increment = 2;
945 }
946
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200947 if (dev_priv->ldu_priv) {
Jakob Bornecrantzc5c42362011-10-22 10:29:34 +0200948 ret = do_dmabuf_dirty_ldu(dev_priv, &vfbd->base,
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200949 flags, color,
950 clips, num_clips, increment);
951 } else {
952 ret = do_dmabuf_dirty_sou(file_priv, dev_priv, &vfbd->base,
Jakob Bornecrantzc5c42362011-10-22 10:29:34 +0200953 flags, color,
Jakob Bornecrantzbd49ae42012-02-09 16:56:44 +0100954 clips, num_clips, increment, NULL);
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200955 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000956
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200957 ttm_read_unlock(&vmaster->lock);
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200958 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000959}
960
961static struct drm_framebuffer_funcs vmw_framebuffer_dmabuf_funcs = {
962 .destroy = vmw_framebuffer_dmabuf_destroy,
963 .dirty = vmw_framebuffer_dmabuf_dirty,
964 .create_handle = vmw_framebuffer_create_handle,
965};
966
Jakob Bornecrantz497a3ff2011-10-04 20:13:14 +0200967/**
Jakob Bornecrantz497a3ff2011-10-04 20:13:14 +0200968 * Pin the dmabuffer to the start of vram.
969 */
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000970static int vmw_framebuffer_dmabuf_pin(struct vmw_framebuffer *vfb)
971{
972 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
973 struct vmw_framebuffer_dmabuf *vfbd =
974 vmw_framebuffer_to_vfbd(&vfb->base);
975 int ret;
976
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200977 /* This code should not be used with screen objects */
978 BUG_ON(dev_priv->sou_priv);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +0200979
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000980 vmw_overlay_pause_all(dev_priv);
981
Jakob Bornecrantzd991ef02011-10-04 20:13:21 +0200982 ret = vmw_dmabuf_to_start_of_vram(dev_priv, vfbd->buffer, true, false);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000983
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000984 vmw_overlay_resume_all(dev_priv);
985
Jakob Bornecrantz316ab132010-05-28 11:22:05 +0200986 WARN_ON(ret != 0);
987
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000988 return 0;
989}
990
991static int vmw_framebuffer_dmabuf_unpin(struct vmw_framebuffer *vfb)
992{
993 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
994 struct vmw_framebuffer_dmabuf *vfbd =
995 vmw_framebuffer_to_vfbd(&vfb->base);
996
997 if (!vfbd->buffer) {
998 WARN_ON(!vfbd->buffer);
999 return 0;
1000 }
1001
Jakob Bornecrantzd991ef02011-10-04 20:13:21 +02001002 return vmw_dmabuf_unpin(dev_priv, vfbd->buffer, false);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001003}
1004
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001005static int vmw_kms_new_framebuffer_dmabuf(struct vmw_private *dev_priv,
1006 struct vmw_dma_buffer *dmabuf,
1007 struct vmw_framebuffer **out,
1008 const struct drm_mode_fb_cmd
1009 *mode_cmd)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001010
1011{
1012 struct drm_device *dev = dev_priv->dev;
1013 struct vmw_framebuffer_dmabuf *vfbd;
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001014 unsigned int requested_size;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001015 int ret;
1016
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001017 requested_size = mode_cmd->height * mode_cmd->pitch;
1018 if (unlikely(requested_size > dmabuf->base.num_pages * PAGE_SIZE)) {
1019 DRM_ERROR("Screen buffer object size is too small "
1020 "for requested mode.\n");
1021 return -EINVAL;
1022 }
1023
Jakob Bornecrantzc337ada2011-10-04 20:13:34 +02001024 /* Limited framebuffer color depth support for screen objects */
1025 if (dev_priv->sou_priv) {
1026 switch (mode_cmd->depth) {
1027 case 32:
1028 case 24:
1029 /* Only support 32 bpp for 32 and 24 depth fbs */
1030 if (mode_cmd->bpp == 32)
1031 break;
1032
1033 DRM_ERROR("Invalid color depth/bbp: %d %d\n",
1034 mode_cmd->depth, mode_cmd->bpp);
1035 return -EINVAL;
1036 case 16:
1037 case 15:
1038 /* Only support 16 bpp for 16 and 15 depth fbs */
1039 if (mode_cmd->bpp == 16)
1040 break;
1041
1042 DRM_ERROR("Invalid color depth/bbp: %d %d\n",
1043 mode_cmd->depth, mode_cmd->bpp);
1044 return -EINVAL;
1045 default:
1046 DRM_ERROR("Invalid color depth: %d\n", mode_cmd->depth);
1047 return -EINVAL;
1048 }
1049 }
1050
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001051 vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL);
1052 if (!vfbd) {
1053 ret = -ENOMEM;
1054 goto out_err1;
1055 }
1056
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001057 if (!vmw_dmabuf_reference(dmabuf)) {
1058 DRM_ERROR("failed to reference dmabuf %p\n", dmabuf);
Daniel Vetter80f0b5a2012-12-13 23:39:01 +01001059 ret = -EINVAL;
1060 goto out_err2;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001061 }
1062
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001063 vfbd->base.base.bits_per_pixel = mode_cmd->bpp;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02001064 vfbd->base.base.pitches[0] = mode_cmd->pitch;
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001065 vfbd->base.base.depth = mode_cmd->depth;
1066 vfbd->base.base.width = mode_cmd->width;
1067 vfbd->base.base.height = mode_cmd->height;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +02001068 if (!dev_priv->sou_priv) {
1069 vfbd->base.pin = vmw_framebuffer_dmabuf_pin;
1070 vfbd->base.unpin = vmw_framebuffer_dmabuf_unpin;
1071 }
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001072 vfbd->base.dmabuf = true;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001073 vfbd->buffer = dmabuf;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001074 vfbd->base.user_handle = mode_cmd->handle;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001075 *out = &vfbd->base;
1076
Daniel Vetter80f0b5a2012-12-13 23:39:01 +01001077 ret = drm_framebuffer_init(dev, &vfbd->base.base,
1078 &vmw_framebuffer_dmabuf_funcs);
1079 if (ret)
1080 goto out_err3;
1081
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001082 return 0;
1083
1084out_err3:
Daniel Vetter80f0b5a2012-12-13 23:39:01 +01001085 vmw_dmabuf_unreference(&dmabuf);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001086out_err2:
1087 kfree(vfbd);
1088out_err1:
1089 return ret;
1090}
1091
1092/*
1093 * Generic Kernel modesetting functions
1094 */
1095
1096static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev,
1097 struct drm_file *file_priv,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001098 struct drm_mode_fb_cmd2 *mode_cmd2)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001099{
1100 struct vmw_private *dev_priv = vmw_priv(dev);
1101 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
1102 struct vmw_framebuffer *vfb = NULL;
1103 struct vmw_surface *surface = NULL;
1104 struct vmw_dma_buffer *bo = NULL;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001105 struct ttm_base_object *user_obj;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001106 struct drm_mode_fb_cmd mode_cmd;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001107 int ret;
1108
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001109 mode_cmd.width = mode_cmd2->width;
1110 mode_cmd.height = mode_cmd2->height;
1111 mode_cmd.pitch = mode_cmd2->pitches[0];
1112 mode_cmd.handle = mode_cmd2->handles[0];
Dave Airlie248dbc22011-11-29 20:02:54 +00001113 drm_fb_get_bpp_depth(mode_cmd2->pixel_format, &mode_cmd.depth,
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001114 &mode_cmd.bpp);
1115
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001116 /**
1117 * This code should be conditioned on Screen Objects not being used.
1118 * If screen objects are used, we can allocate a GMR to hold the
1119 * requested framebuffer.
1120 */
1121
Xi Wang8a783892011-12-21 05:18:33 -05001122 if (!vmw_kms_validate_mode_vram(dev_priv,
Linus Torvalds1a464cb2012-01-10 11:04:36 -08001123 mode_cmd.pitch,
1124 mode_cmd.height)) {
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001125 DRM_ERROR("VRAM size is too small for requested mode.\n");
Jakob Bornecrantzd9826402011-11-03 21:03:04 +01001126 return ERR_PTR(-ENOMEM);
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001127 }
1128
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001129 /*
1130 * Take a reference on the user object of the resource
1131 * backing the kms fb. This ensures that user-space handle
1132 * lookups on that resource will always work as long as
1133 * it's registered with a kms framebuffer. This is important,
1134 * since vmw_execbuf_process identifies resources in the
1135 * command stream using user-space handles.
1136 */
1137
Jesse Barnes308e5bc2011-11-14 14:51:28 -08001138 user_obj = ttm_base_object_lookup(tfile, mode_cmd.handle);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001139 if (unlikely(user_obj == NULL)) {
1140 DRM_ERROR("Could not locate requested kms frame buffer.\n");
1141 return ERR_PTR(-ENOENT);
1142 }
1143
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001144 /**
1145 * End conditioned code.
1146 */
1147
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +01001148 /* returns either a dmabuf or surface */
1149 ret = vmw_user_lookup_handle(dev_priv, tfile,
Dave Airlie4cf73122011-12-21 09:50:56 +00001150 mode_cmd.handle,
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +01001151 &surface, &bo);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001152 if (ret)
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +01001153 goto err_out;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001154
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +01001155 /* Create the new framebuffer depending one what we got back */
1156 if (bo)
1157 ret = vmw_kms_new_framebuffer_dmabuf(dev_priv, bo, &vfb,
Dave Airlie4cf73122011-12-21 09:50:56 +00001158 &mode_cmd);
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +01001159 else if (surface)
1160 ret = vmw_kms_new_framebuffer_surface(dev_priv, file_priv,
Dave Airlie4cf73122011-12-21 09:50:56 +00001161 surface, &vfb, &mode_cmd);
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +01001162 else
1163 BUG();
Jakob Bornecrantz5ffdb652010-01-30 03:38:08 +00001164
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +01001165err_out:
1166 /* vmw_user_lookup_handle takes one ref so does new_fb */
1167 if (bo)
1168 vmw_dmabuf_unreference(&bo);
1169 if (surface)
1170 vmw_surface_unreference(&surface);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001171
1172 if (ret) {
1173 DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001174 ttm_base_object_unref(&user_obj);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01001175 return ERR_PTR(ret);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001176 } else
1177 vfb->user_obj = user_obj;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001178
1179 return &vfb->base;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001180}
1181
Laurent Pincharte6ecefa2012-05-17 13:27:23 +02001182static const struct drm_mode_config_funcs vmw_kms_funcs = {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001183 .fb_create = vmw_kms_fb_create,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001184};
1185
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001186int vmw_kms_present(struct vmw_private *dev_priv,
1187 struct drm_file *file_priv,
1188 struct vmw_framebuffer *vfb,
1189 struct vmw_surface *surface,
1190 uint32_t sid,
1191 int32_t destX, int32_t destY,
1192 struct drm_vmw_rect *clips,
1193 uint32_t num_clips)
1194{
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001195 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +01001196 struct drm_clip_rect *tmp;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001197 struct drm_crtc *crtc;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001198 size_t fifo_size;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001199 int i, k, num_units;
1200 int ret = 0; /* silence warning */
Jakob Bornecrantz203dc222011-11-28 13:19:13 +01001201 int left, right, top, bottom;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001202
1203 struct {
1204 SVGA3dCmdHeader header;
1205 SVGA3dCmdBlitSurfaceToScreen body;
1206 } *cmd;
1207 SVGASignedRect *blits;
1208
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001209 num_units = 0;
1210 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
1211 if (crtc->fb != &vfb->base)
1212 continue;
1213 units[num_units++] = vmw_crtc_to_du(crtc);
1214 }
1215
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001216 BUG_ON(surface == NULL);
1217 BUG_ON(!clips || !num_clips);
1218
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +01001219 tmp = kzalloc(sizeof(*tmp) * num_clips, GFP_KERNEL);
1220 if (unlikely(tmp == NULL)) {
1221 DRM_ERROR("Temporary cliprect memory alloc failed.\n");
1222 return -ENOMEM;
1223 }
1224
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001225 fifo_size = sizeof(*cmd) + sizeof(SVGASignedRect) * num_clips;
1226 cmd = kmalloc(fifo_size, GFP_KERNEL);
1227 if (unlikely(cmd == NULL)) {
1228 DRM_ERROR("Failed to allocate temporary fifo memory.\n");
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +01001229 ret = -ENOMEM;
1230 goto out_free_tmp;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001231 }
1232
Jakob Bornecrantz203dc222011-11-28 13:19:13 +01001233 left = clips->x;
1234 right = clips->x + clips->w;
1235 top = clips->y;
1236 bottom = clips->y + clips->h;
1237
1238 for (i = 1; i < num_clips; i++) {
1239 left = min_t(int, left, (int)clips[i].x);
1240 right = max_t(int, right, (int)clips[i].x + clips[i].w);
1241 top = min_t(int, top, (int)clips[i].y);
1242 bottom = max_t(int, bottom, (int)clips[i].y + clips[i].h);
1243 }
1244
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001245 /* only need to do this once */
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001246 memset(cmd, 0, fifo_size);
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001247 cmd->header.id = cpu_to_le32(SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN);
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +01001248
1249 blits = (SVGASignedRect *)&cmd[1];
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001250
Jakob Bornecrantz203dc222011-11-28 13:19:13 +01001251 cmd->body.srcRect.left = left;
1252 cmd->body.srcRect.right = right;
1253 cmd->body.srcRect.top = top;
1254 cmd->body.srcRect.bottom = bottom;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001255
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001256 for (i = 0; i < num_clips; i++) {
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +01001257 tmp[i].x1 = clips[i].x - left;
1258 tmp[i].x2 = clips[i].x + clips[i].w - left;
1259 tmp[i].y1 = clips[i].y - top;
1260 tmp[i].y2 = clips[i].y + clips[i].h - top;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001261 }
1262
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001263 for (k = 0; k < num_units; k++) {
1264 struct vmw_display_unit *unit = units[k];
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +01001265 struct vmw_clip_rect clip;
1266 int num;
1267
1268 clip.x1 = left + destX - unit->crtc.x;
1269 clip.y1 = top + destY - unit->crtc.y;
1270 clip.x2 = right + destX - unit->crtc.x;
1271 clip.y2 = bottom + destY - unit->crtc.y;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001272
1273 /* skip any crtcs that misses the clip region */
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +01001274 if (clip.x1 >= unit->crtc.mode.hdisplay ||
1275 clip.y1 >= unit->crtc.mode.vdisplay ||
1276 clip.x2 <= 0 || clip.y2 <= 0)
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001277 continue;
1278
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +01001279 /*
1280 * In order for the clip rects to be correctly scaled
1281 * the src and dest rects needs to be the same size.
1282 */
1283 cmd->body.destRect.left = clip.x1;
1284 cmd->body.destRect.right = clip.x2;
1285 cmd->body.destRect.top = clip.y1;
1286 cmd->body.destRect.bottom = clip.y2;
1287
1288 /* create a clip rect of the crtc in dest coords */
1289 clip.x2 = unit->crtc.mode.hdisplay - clip.x1;
1290 clip.y2 = unit->crtc.mode.vdisplay - clip.y1;
1291 clip.x1 = 0 - clip.x1;
1292 clip.y1 = 0 - clip.y1;
1293
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001294 /* need to reset sid as it is changed by execbuf */
1295 cmd->body.srcImage.sid = sid;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001296 cmd->body.destScreenId = unit->unit;
1297
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +01001298 /* clip and write blits to cmd stream */
1299 vmw_clip_cliprects(tmp, num_clips, clip, blits, &num);
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001300
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +01001301 /* if no cliprects hit skip this */
1302 if (num == 0)
1303 continue;
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001304
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +01001305 /* recalculate package length */
1306 fifo_size = sizeof(*cmd) + sizeof(SVGASignedRect) * num;
1307 cmd->header.size = cpu_to_le32(fifo_size - sizeof(cmd->header));
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001308 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd,
Jakob Bornecrantzbb1bd2f2012-02-09 16:56:43 +01001309 fifo_size, 0, NULL, NULL);
Jakob Bornecrantzc6ca8392011-10-07 15:23:07 +02001310
1311 if (unlikely(ret != 0))
1312 break;
1313 }
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001314
1315 kfree(cmd);
Jakob Bornecrantz6abff3c2011-11-28 13:19:15 +01001316out_free_tmp:
1317 kfree(tmp);
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001318
1319 return ret;
1320}
1321
1322int vmw_kms_readback(struct vmw_private *dev_priv,
1323 struct drm_file *file_priv,
1324 struct vmw_framebuffer *vfb,
1325 struct drm_vmw_fence_rep __user *user_fence_rep,
1326 struct drm_vmw_rect *clips,
1327 uint32_t num_clips)
1328{
1329 struct vmw_framebuffer_dmabuf *vfbd =
1330 vmw_framebuffer_to_vfbd(&vfb->base);
1331 struct vmw_dma_buffer *dmabuf = vfbd->buffer;
1332 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
1333 struct drm_crtc *crtc;
1334 size_t fifo_size;
1335 int i, k, ret, num_units, blits_pos;
1336
1337 struct {
1338 uint32_t header;
1339 SVGAFifoCmdDefineGMRFB body;
1340 } *cmd;
1341 struct {
1342 uint32_t header;
1343 SVGAFifoCmdBlitScreenToGMRFB body;
1344 } *blits;
1345
1346 num_units = 0;
1347 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
1348 if (crtc->fb != &vfb->base)
1349 continue;
1350 units[num_units++] = vmw_crtc_to_du(crtc);
1351 }
1352
1353 BUG_ON(dmabuf == NULL);
1354 BUG_ON(!clips || !num_clips);
1355
1356 /* take a safe guess at fifo size */
1357 fifo_size = sizeof(*cmd) + sizeof(*blits) * num_clips * num_units;
1358 cmd = kmalloc(fifo_size, GFP_KERNEL);
1359 if (unlikely(cmd == NULL)) {
1360 DRM_ERROR("Failed to allocate temporary fifo memory.\n");
1361 return -ENOMEM;
1362 }
1363
1364 memset(cmd, 0, fifo_size);
1365 cmd->header = SVGA_CMD_DEFINE_GMRFB;
1366 cmd->body.format.bitsPerPixel = vfb->base.bits_per_pixel;
1367 cmd->body.format.colorDepth = vfb->base.depth;
1368 cmd->body.format.reserved = 0;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02001369 cmd->body.bytesPerLine = vfb->base.pitches[0];
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001370 cmd->body.ptr.gmrId = vfb->user_handle;
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001371 cmd->body.ptr.offset = 0;
1372
1373 blits = (void *)&cmd[1];
1374 blits_pos = 0;
1375 for (i = 0; i < num_units; i++) {
1376 struct drm_vmw_rect *c = clips;
1377 for (k = 0; k < num_clips; k++, c++) {
1378 /* transform clip coords to crtc origin based coords */
1379 int clip_x1 = c->x - units[i]->crtc.x;
1380 int clip_x2 = c->x - units[i]->crtc.x + c->w;
1381 int clip_y1 = c->y - units[i]->crtc.y;
1382 int clip_y2 = c->y - units[i]->crtc.y + c->h;
1383 int dest_x = c->x;
1384 int dest_y = c->y;
1385
1386 /* compensate for clipping, we negate
1387 * a negative number and add that.
1388 */
1389 if (clip_x1 < 0)
1390 dest_x += -clip_x1;
1391 if (clip_y1 < 0)
1392 dest_y += -clip_y1;
1393
1394 /* clip */
1395 clip_x1 = max(clip_x1, 0);
1396 clip_y1 = max(clip_y1, 0);
1397 clip_x2 = min(clip_x2, units[i]->crtc.mode.hdisplay);
1398 clip_y2 = min(clip_y2, units[i]->crtc.mode.vdisplay);
1399
1400 /* and cull any rects that misses the crtc */
1401 if (clip_x1 >= units[i]->crtc.mode.hdisplay ||
1402 clip_y1 >= units[i]->crtc.mode.vdisplay ||
1403 clip_x2 <= 0 || clip_y2 <= 0)
1404 continue;
1405
1406 blits[blits_pos].header = SVGA_CMD_BLIT_SCREEN_TO_GMRFB;
1407 blits[blits_pos].body.srcScreenId = units[i]->unit;
1408 blits[blits_pos].body.destOrigin.x = dest_x;
1409 blits[blits_pos].body.destOrigin.y = dest_y;
1410
1411 blits[blits_pos].body.srcRect.left = clip_x1;
1412 blits[blits_pos].body.srcRect.top = clip_y1;
1413 blits[blits_pos].body.srcRect.right = clip_x2;
1414 blits[blits_pos].body.srcRect.bottom = clip_y2;
1415 blits_pos++;
1416 }
1417 }
1418 /* reset size here and use calculated exact size from loops */
1419 fifo_size = sizeof(*cmd) + sizeof(*blits) * blits_pos;
1420
1421 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd, fifo_size,
Jakob Bornecrantzbb1bd2f2012-02-09 16:56:43 +01001422 0, user_fence_rep, NULL);
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001423
1424 kfree(cmd);
1425
1426 return ret;
1427}
1428
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001429int vmw_kms_init(struct vmw_private *dev_priv)
1430{
1431 struct drm_device *dev = dev_priv->dev;
1432 int ret;
1433
1434 drm_mode_config_init(dev);
1435 dev->mode_config.funcs = &vmw_kms_funcs;
Jakob Bornecrantz3bef3572010-02-09 19:41:57 +00001436 dev->mode_config.min_width = 1;
1437 dev->mode_config.min_height = 1;
Jakob Bornecrantz7e71f8a2010-05-28 11:21:54 +02001438 /* assumed largest fb size */
1439 dev->mode_config.max_width = 8192;
1440 dev->mode_config.max_height = 8192;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001441
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +02001442 ret = vmw_kms_init_screen_object_display(dev_priv);
1443 if (ret) /* Fallback */
1444 (void)vmw_kms_init_legacy_display_system(dev_priv);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001445
1446 return 0;
1447}
1448
1449int vmw_kms_close(struct vmw_private *dev_priv)
1450{
1451 /*
1452 * Docs says we should take the lock before calling this function
1453 * but since it destroys encoders and our destructor calls
1454 * drm_encoder_cleanup which takes the lock we deadlock.
1455 */
1456 drm_mode_config_cleanup(dev_priv->dev);
Jakob Bornecrantzc0d18312011-11-09 10:25:26 +01001457 if (dev_priv->sou_priv)
1458 vmw_kms_close_screen_object_display(dev_priv);
1459 else
1460 vmw_kms_close_legacy_display_system(dev_priv);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001461 return 0;
1462}
1463
1464int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
1465 struct drm_file *file_priv)
1466{
1467 struct drm_vmw_cursor_bypass_arg *arg = data;
1468 struct vmw_display_unit *du;
1469 struct drm_mode_object *obj;
1470 struct drm_crtc *crtc;
1471 int ret = 0;
1472
1473
1474 mutex_lock(&dev->mode_config.mutex);
1475 if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
1476
1477 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1478 du = vmw_crtc_to_du(crtc);
1479 du->hotspot_x = arg->xhot;
1480 du->hotspot_y = arg->yhot;
1481 }
1482
1483 mutex_unlock(&dev->mode_config.mutex);
1484 return 0;
1485 }
1486
1487 obj = drm_mode_object_find(dev, arg->crtc_id, DRM_MODE_OBJECT_CRTC);
1488 if (!obj) {
1489 ret = -EINVAL;
1490 goto out;
1491 }
1492
1493 crtc = obj_to_crtc(obj);
1494 du = vmw_crtc_to_du(crtc);
1495
1496 du->hotspot_x = arg->xhot;
1497 du->hotspot_y = arg->yhot;
1498
1499out:
1500 mutex_unlock(&dev->mode_config.mutex);
1501
1502 return ret;
1503}
1504
Michel Dänzer0bef23f2011-08-31 07:42:50 +00001505int vmw_kms_write_svga(struct vmw_private *vmw_priv,
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001506 unsigned width, unsigned height, unsigned pitch,
Michel Dänzer6558429b2011-08-31 07:42:49 +00001507 unsigned bpp, unsigned depth)
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001508{
1509 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1510 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch);
1511 else if (vmw_fifo_have_pitchlock(vmw_priv))
1512 iowrite32(pitch, vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
1513 vmw_write(vmw_priv, SVGA_REG_WIDTH, width);
1514 vmw_write(vmw_priv, SVGA_REG_HEIGHT, height);
Michel Dänzer6558429b2011-08-31 07:42:49 +00001515 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp);
Michel Dänzer0bef23f2011-08-31 07:42:50 +00001516
1517 if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) {
1518 DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n",
1519 depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH));
1520 return -EINVAL;
1521 }
1522
1523 return 0;
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001524}
1525
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001526int vmw_kms_save_vga(struct vmw_private *vmw_priv)
1527{
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001528 struct vmw_vga_topology_state *save;
1529 uint32_t i;
1530
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001531 vmw_priv->vga_width = vmw_read(vmw_priv, SVGA_REG_WIDTH);
1532 vmw_priv->vga_height = vmw_read(vmw_priv, SVGA_REG_HEIGHT);
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001533 vmw_priv->vga_bpp = vmw_read(vmw_priv, SVGA_REG_BITS_PER_PIXEL);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001534 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1535 vmw_priv->vga_pitchlock =
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001536 vmw_read(vmw_priv, SVGA_REG_PITCHLOCK);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001537 else if (vmw_fifo_have_pitchlock(vmw_priv))
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001538 vmw_priv->vga_pitchlock = ioread32(vmw_priv->mmio_virt +
1539 SVGA_FIFO_PITCHLOCK);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001540
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001541 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1542 return 0;
1543
1544 vmw_priv->num_displays = vmw_read(vmw_priv,
1545 SVGA_REG_NUM_GUEST_DISPLAYS);
1546
Thomas Hellstrom029e50b2010-10-05 12:43:08 +02001547 if (vmw_priv->num_displays == 0)
1548 vmw_priv->num_displays = 1;
1549
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001550 for (i = 0; i < vmw_priv->num_displays; ++i) {
1551 save = &vmw_priv->vga_save[i];
1552 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1553 save->primary = vmw_read(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY);
1554 save->pos_x = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_X);
1555 save->pos_y = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y);
1556 save->width = vmw_read(vmw_priv, SVGA_REG_DISPLAY_WIDTH);
1557 save->height = vmw_read(vmw_priv, SVGA_REG_DISPLAY_HEIGHT);
1558 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
Thomas Hellstrom30c78bb2010-10-01 10:21:48 +02001559 if (i == 0 && vmw_priv->num_displays == 1 &&
1560 save->width == 0 && save->height == 0) {
1561
1562 /*
1563 * It should be fairly safe to assume that these
1564 * values are uninitialized.
1565 */
1566
1567 save->width = vmw_priv->vga_width - save->pos_x;
1568 save->height = vmw_priv->vga_height - save->pos_y;
1569 }
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001570 }
Thomas Hellstrom30c78bb2010-10-01 10:21:48 +02001571
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001572 return 0;
1573}
1574
1575int vmw_kms_restore_vga(struct vmw_private *vmw_priv)
1576{
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001577 struct vmw_vga_topology_state *save;
1578 uint32_t i;
1579
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001580 vmw_write(vmw_priv, SVGA_REG_WIDTH, vmw_priv->vga_width);
1581 vmw_write(vmw_priv, SVGA_REG_HEIGHT, vmw_priv->vga_height);
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001582 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, vmw_priv->vga_bpp);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001583 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1584 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK,
1585 vmw_priv->vga_pitchlock);
1586 else if (vmw_fifo_have_pitchlock(vmw_priv))
1587 iowrite32(vmw_priv->vga_pitchlock,
1588 vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001589
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001590 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1591 return 0;
1592
1593 for (i = 0; i < vmw_priv->num_displays; ++i) {
1594 save = &vmw_priv->vga_save[i];
1595 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1596 vmw_write(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY, save->primary);
1597 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_X, save->pos_x);
1598 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y, save->pos_y);
1599 vmw_write(vmw_priv, SVGA_REG_DISPLAY_WIDTH, save->width);
1600 vmw_write(vmw_priv, SVGA_REG_DISPLAY_HEIGHT, save->height);
1601 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
1602 }
1603
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001604 return 0;
1605}
Jakob Bornecrantzd8bd19d2010-06-01 11:54:20 +02001606
Thomas Hellstrome133e7372010-10-05 12:43:04 +02001607bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
1608 uint32_t pitch,
1609 uint32_t height)
1610{
1611 return ((u64) pitch * (u64) height) < (u64) dev_priv->vram_size;
1612}
1613
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001614
1615/**
1616 * Function called by DRM code called with vbl_lock held.
1617 */
Thomas Hellstrom7a1c2f62010-10-01 10:21:49 +02001618u32 vmw_get_vblank_counter(struct drm_device *dev, int crtc)
1619{
1620 return 0;
1621}
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001622
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001623/**
1624 * Function called by DRM code called with vbl_lock held.
1625 */
1626int vmw_enable_vblank(struct drm_device *dev, int crtc)
1627{
1628 return -ENOSYS;
1629}
1630
1631/**
1632 * Function called by DRM code called with vbl_lock held.
1633 */
1634void vmw_disable_vblank(struct drm_device *dev, int crtc)
1635{
1636}
1637
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001638
1639/*
1640 * Small shared kms functions.
1641 */
1642
1643int vmw_du_update_layout(struct vmw_private *dev_priv, unsigned num,
1644 struct drm_vmw_rect *rects)
1645{
1646 struct drm_device *dev = dev_priv->dev;
1647 struct vmw_display_unit *du;
1648 struct drm_connector *con;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001649
1650 mutex_lock(&dev->mode_config.mutex);
1651
1652#if 0
Thomas Hellstrom6ea77d12011-10-04 20:13:36 +02001653 {
1654 unsigned int i;
1655
1656 DRM_INFO("%s: new layout ", __func__);
1657 for (i = 0; i < num; i++)
1658 DRM_INFO("(%i, %i %ux%u) ", rects[i].x, rects[i].y,
1659 rects[i].w, rects[i].h);
1660 DRM_INFO("\n");
1661 }
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001662#endif
1663
1664 list_for_each_entry(con, &dev->mode_config.connector_list, head) {
1665 du = vmw_connector_to_du(con);
1666 if (num > du->unit) {
1667 du->pref_width = rects[du->unit].w;
1668 du->pref_height = rects[du->unit].h;
1669 du->pref_active = true;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001670 du->gui_x = rects[du->unit].x;
1671 du->gui_y = rects[du->unit].y;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001672 } else {
1673 du->pref_width = 800;
1674 du->pref_height = 600;
1675 du->pref_active = false;
1676 }
1677 con->status = vmw_du_connector_detect(con, true);
1678 }
1679
1680 mutex_unlock(&dev->mode_config.mutex);
1681
1682 return 0;
1683}
1684
Jakob Bornecrantzb5ec4272012-02-09 16:56:45 +01001685int vmw_du_page_flip(struct drm_crtc *crtc,
1686 struct drm_framebuffer *fb,
1687 struct drm_pending_vblank_event *event)
1688{
1689 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
1690 struct drm_framebuffer *old_fb = crtc->fb;
1691 struct vmw_framebuffer *vfb = vmw_framebuffer_to_vfb(fb);
Alan Coxf5869a82012-08-20 14:44:52 +00001692 struct drm_file *file_priv ;
Jakob Bornecrantzb5ec4272012-02-09 16:56:45 +01001693 struct vmw_fence_obj *fence = NULL;
1694 struct drm_clip_rect clips;
1695 int ret;
1696
Alan Coxf5869a82012-08-20 14:44:52 +00001697 if (event == NULL)
1698 return -EINVAL;
1699
Jakob Bornecrantzb5ec4272012-02-09 16:56:45 +01001700 /* require ScreenObject support for page flipping */
1701 if (!dev_priv->sou_priv)
1702 return -ENOSYS;
1703
Alan Coxf5869a82012-08-20 14:44:52 +00001704 file_priv = event->base.file_priv;
Jakob Bornecrantzb5ec4272012-02-09 16:56:45 +01001705 if (!vmw_kms_screen_object_flippable(dev_priv, crtc))
1706 return -EINVAL;
1707
1708 crtc->fb = fb;
1709
1710 /* do a full screen dirty update */
1711 clips.x1 = clips.y1 = 0;
1712 clips.x2 = fb->width;
1713 clips.y2 = fb->height;
1714
1715 if (vfb->dmabuf)
1716 ret = do_dmabuf_dirty_sou(file_priv, dev_priv, vfb,
1717 0, 0, &clips, 1, 1, &fence);
1718 else
1719 ret = do_surface_dirty_sou(dev_priv, file_priv, vfb,
1720 0, 0, &clips, 1, 1, &fence);
1721
1722
1723 if (ret != 0)
1724 goto out_no_fence;
1725 if (!fence) {
1726 ret = -EINVAL;
1727 goto out_no_fence;
1728 }
1729
1730 ret = vmw_event_fence_action_queue(file_priv, fence,
1731 &event->base,
1732 &event->event.tv_sec,
1733 &event->event.tv_usec,
1734 true);
1735
1736 /*
1737 * No need to hold on to this now. The only cleanup
1738 * we need to do if we fail is unref the fence.
1739 */
1740 vmw_fence_obj_unreference(&fence);
1741
1742 if (vmw_crtc_to_du(crtc)->is_implicit)
1743 vmw_kms_screen_object_update_implicit_fb(dev_priv, crtc);
1744
1745 return ret;
1746
1747out_no_fence:
1748 crtc->fb = old_fb;
1749 return ret;
1750}
1751
1752
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001753void vmw_du_crtc_save(struct drm_crtc *crtc)
1754{
1755}
1756
1757void vmw_du_crtc_restore(struct drm_crtc *crtc)
1758{
1759}
1760
1761void vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
1762 u16 *r, u16 *g, u16 *b,
1763 uint32_t start, uint32_t size)
1764{
1765 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
1766 int i;
1767
1768 for (i = 0; i < size; i++) {
1769 DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i,
1770 r[i], g[i], b[i]);
1771 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 0, r[i] >> 8);
1772 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 1, g[i] >> 8);
1773 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 2, b[i] >> 8);
1774 }
1775}
1776
1777void vmw_du_connector_dpms(struct drm_connector *connector, int mode)
1778{
1779}
1780
1781void vmw_du_connector_save(struct drm_connector *connector)
1782{
1783}
1784
1785void vmw_du_connector_restore(struct drm_connector *connector)
1786{
1787}
1788
1789enum drm_connector_status
1790vmw_du_connector_detect(struct drm_connector *connector, bool force)
1791{
1792 uint32_t num_displays;
1793 struct drm_device *dev = connector->dev;
1794 struct vmw_private *dev_priv = vmw_priv(dev);
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001795 struct vmw_display_unit *du = vmw_connector_to_du(connector);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001796
1797 mutex_lock(&dev_priv->hw_mutex);
1798 num_displays = vmw_read(dev_priv, SVGA_REG_NUM_DISPLAYS);
1799 mutex_unlock(&dev_priv->hw_mutex);
1800
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001801 return ((vmw_connector_to_du(connector)->unit < num_displays &&
1802 du->pref_active) ?
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001803 connector_status_connected : connector_status_disconnected);
1804}
1805
1806static struct drm_display_mode vmw_kms_connector_builtin[] = {
1807 /* 640x480@60Hz */
1808 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
1809 752, 800, 0, 480, 489, 492, 525, 0,
1810 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1811 /* 800x600@60Hz */
1812 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
1813 968, 1056, 0, 600, 601, 605, 628, 0,
1814 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1815 /* 1024x768@60Hz */
1816 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
1817 1184, 1344, 0, 768, 771, 777, 806, 0,
1818 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1819 /* 1152x864@75Hz */
1820 { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
1821 1344, 1600, 0, 864, 865, 868, 900, 0,
1822 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1823 /* 1280x768@60Hz */
1824 { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344,
1825 1472, 1664, 0, 768, 771, 778, 798, 0,
1826 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1827 /* 1280x800@60Hz */
1828 { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352,
1829 1480, 1680, 0, 800, 803, 809, 831, 0,
1830 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
1831 /* 1280x960@60Hz */
1832 { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376,
1833 1488, 1800, 0, 960, 961, 964, 1000, 0,
1834 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1835 /* 1280x1024@60Hz */
1836 { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328,
1837 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
1838 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1839 /* 1360x768@60Hz */
1840 { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424,
1841 1536, 1792, 0, 768, 771, 777, 795, 0,
1842 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1843 /* 1440x1050@60Hz */
1844 { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488,
1845 1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
1846 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1847 /* 1440x900@60Hz */
1848 { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520,
1849 1672, 1904, 0, 900, 903, 909, 934, 0,
1850 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1851 /* 1600x1200@60Hz */
1852 { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664,
1853 1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
1854 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1855 /* 1680x1050@60Hz */
1856 { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784,
1857 1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
1858 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1859 /* 1792x1344@60Hz */
1860 { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920,
1861 2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
1862 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1863 /* 1853x1392@60Hz */
1864 { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952,
1865 2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
1866 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1867 /* 1920x1200@60Hz */
1868 { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056,
1869 2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
1870 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1871 /* 1920x1440@60Hz */
1872 { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048,
1873 2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
1874 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1875 /* 2560x1600@60Hz */
1876 { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752,
1877 3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
1878 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1879 /* Terminate */
1880 { DRM_MODE("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) },
1881};
1882
Thomas Hellstrom1543b4dd2011-11-02 09:43:10 +01001883/**
1884 * vmw_guess_mode_timing - Provide fake timings for a
1885 * 60Hz vrefresh mode.
1886 *
1887 * @mode - Pointer to a struct drm_display_mode with hdisplay and vdisplay
1888 * members filled in.
1889 */
1890static void vmw_guess_mode_timing(struct drm_display_mode *mode)
1891{
1892 mode->hsync_start = mode->hdisplay + 50;
1893 mode->hsync_end = mode->hsync_start + 50;
1894 mode->htotal = mode->hsync_end + 50;
1895
1896 mode->vsync_start = mode->vdisplay + 50;
1897 mode->vsync_end = mode->vsync_start + 50;
1898 mode->vtotal = mode->vsync_end + 50;
1899
1900 mode->clock = (u32)mode->htotal * (u32)mode->vtotal / 100 * 6;
1901 mode->vrefresh = drm_mode_vrefresh(mode);
1902}
1903
1904
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001905int vmw_du_connector_fill_modes(struct drm_connector *connector,
1906 uint32_t max_width, uint32_t max_height)
1907{
1908 struct vmw_display_unit *du = vmw_connector_to_du(connector);
1909 struct drm_device *dev = connector->dev;
1910 struct vmw_private *dev_priv = vmw_priv(dev);
1911 struct drm_display_mode *mode = NULL;
1912 struct drm_display_mode *bmode;
1913 struct drm_display_mode prefmode = { DRM_MODE("preferred",
1914 DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
1915 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1916 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
1917 };
1918 int i;
1919
1920 /* Add preferred mode */
1921 {
1922 mode = drm_mode_duplicate(dev, &prefmode);
1923 if (!mode)
1924 return 0;
1925 mode->hdisplay = du->pref_width;
1926 mode->vdisplay = du->pref_height;
Thomas Hellstrom1543b4dd2011-11-02 09:43:10 +01001927 vmw_guess_mode_timing(mode);
Jakob Bornecrantz55bde5b2011-11-03 21:03:05 +01001928
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001929 if (vmw_kms_validate_mode_vram(dev_priv, mode->hdisplay * 2,
1930 mode->vdisplay)) {
1931 drm_mode_probed_add(connector, mode);
Jakob Bornecrantz55bde5b2011-11-03 21:03:05 +01001932 } else {
1933 drm_mode_destroy(dev, mode);
1934 mode = NULL;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001935 }
Jakob Bornecrantz55bde5b2011-11-03 21:03:05 +01001936
1937 if (du->pref_mode) {
1938 list_del_init(&du->pref_mode->head);
1939 drm_mode_destroy(dev, du->pref_mode);
1940 }
1941
1942 /* mode might be null here, this is intended */
1943 du->pref_mode = mode;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001944 }
1945
1946 for (i = 0; vmw_kms_connector_builtin[i].type != 0; i++) {
1947 bmode = &vmw_kms_connector_builtin[i];
1948 if (bmode->hdisplay > max_width ||
1949 bmode->vdisplay > max_height)
1950 continue;
1951
1952 if (!vmw_kms_validate_mode_vram(dev_priv, bmode->hdisplay * 2,
1953 bmode->vdisplay))
1954 continue;
1955
1956 mode = drm_mode_duplicate(dev, bmode);
1957 if (!mode)
1958 return 0;
1959 mode->vrefresh = drm_mode_vrefresh(mode);
1960
1961 drm_mode_probed_add(connector, mode);
1962 }
1963
Jakob Bornecrantzd41025c2011-11-03 21:03:07 +01001964 /* Move the prefered mode first, help apps pick the right mode. */
1965 if (du->pref_mode)
1966 list_move(&du->pref_mode->head, &connector->probed_modes);
1967
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001968 drm_mode_connector_list_update(connector);
1969
1970 return 1;
1971}
1972
1973int vmw_du_connector_set_property(struct drm_connector *connector,
1974 struct drm_property *property,
1975 uint64_t val)
1976{
1977 return 0;
1978}
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001979
1980
1981int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data,
1982 struct drm_file *file_priv)
1983{
1984 struct vmw_private *dev_priv = vmw_priv(dev);
1985 struct drm_vmw_update_layout_arg *arg =
1986 (struct drm_vmw_update_layout_arg *)data;
1987 struct vmw_master *vmaster = vmw_master(file_priv->master);
1988 void __user *user_rects;
1989 struct drm_vmw_rect *rects;
1990 unsigned rects_size;
1991 int ret;
1992 int i;
1993 struct drm_mode_config *mode_config = &dev->mode_config;
1994
1995 ret = ttm_read_lock(&vmaster->lock, true);
1996 if (unlikely(ret != 0))
1997 return ret;
1998
1999 if (!arg->num_outputs) {
2000 struct drm_vmw_rect def_rect = {0, 0, 800, 600};
2001 vmw_du_update_layout(dev_priv, 1, &def_rect);
2002 goto out_unlock;
2003 }
2004
2005 rects_size = arg->num_outputs * sizeof(struct drm_vmw_rect);
Xi Wangbab9efc2011-11-28 12:25:43 +01002006 rects = kcalloc(arg->num_outputs, sizeof(struct drm_vmw_rect),
2007 GFP_KERNEL);
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002008 if (unlikely(!rects)) {
2009 ret = -ENOMEM;
2010 goto out_unlock;
2011 }
2012
2013 user_rects = (void __user *)(unsigned long)arg->rects;
2014 ret = copy_from_user(rects, user_rects, rects_size);
2015 if (unlikely(ret != 0)) {
2016 DRM_ERROR("Failed to get rects.\n");
2017 ret = -EFAULT;
2018 goto out_free;
2019 }
2020
2021 for (i = 0; i < arg->num_outputs; ++i) {
Xi Wangbab9efc2011-11-28 12:25:43 +01002022 if (rects[i].x < 0 ||
2023 rects[i].y < 0 ||
2024 rects[i].x + rects[i].w > mode_config->max_width ||
2025 rects[i].y + rects[i].h > mode_config->max_height) {
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002026 DRM_ERROR("Invalid GUI layout.\n");
2027 ret = -EINVAL;
2028 goto out_free;
2029 }
2030 }
2031
2032 vmw_du_update_layout(dev_priv, arg->num_outputs, rects);
2033
2034out_free:
2035 kfree(rects);
2036out_unlock:
2037 ttm_read_unlock(&vmaster->lock);
2038 return ret;
2039}