]> git.ipfire.org Git - people/ms/linux.git/blame - drivers/gpu/drm/vmwgfx/vmwgfx_kms.c
vmwgfx: Close screen object system
[people/ms/linux.git] / drivers / gpu / drm / vmwgfx / vmwgfx_kms.c
CommitLineData
fb1d9738
JB
1/**************************************************************************
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
56d1c78d 30
fb1d9738
JB
31/* Might need a hrtimer here? */
32#define VMWGFX_PRESENT_RATE ((HZ / 60 > 0) ? HZ / 60 : 1)
33
fb1d9738
JB
34void vmw_display_unit_cleanup(struct vmw_display_unit *du)
35{
36 if (du->cursor_surface)
37 vmw_surface_unreference(&du->cursor_surface);
38 if (du->cursor_dmabuf)
39 vmw_dmabuf_unreference(&du->cursor_dmabuf);
40 drm_crtc_cleanup(&du->crtc);
41 drm_encoder_cleanup(&du->encoder);
42 drm_connector_cleanup(&du->connector);
43}
44
45/*
46 * Display Unit Cursor functions
47 */
48
49int vmw_cursor_update_image(struct vmw_private *dev_priv,
50 u32 *image, u32 width, u32 height,
51 u32 hotspotX, u32 hotspotY)
52{
53 struct {
54 u32 cmd;
55 SVGAFifoCmdDefineAlphaCursor cursor;
56 } *cmd;
57 u32 image_size = width * height * 4;
58 u32 cmd_size = sizeof(*cmd) + image_size;
59
60 if (!image)
61 return -EINVAL;
62
63 cmd = vmw_fifo_reserve(dev_priv, cmd_size);
64 if (unlikely(cmd == NULL)) {
65 DRM_ERROR("Fifo reserve failed.\n");
66 return -ENOMEM;
67 }
68
69 memset(cmd, 0, sizeof(*cmd));
70
71 memcpy(&cmd[1], image, image_size);
72
73 cmd->cmd = cpu_to_le32(SVGA_CMD_DEFINE_ALPHA_CURSOR);
74 cmd->cursor.id = cpu_to_le32(0);
75 cmd->cursor.width = cpu_to_le32(width);
76 cmd->cursor.height = cpu_to_le32(height);
77 cmd->cursor.hotspotX = cpu_to_le32(hotspotX);
78 cmd->cursor.hotspotY = cpu_to_le32(hotspotY);
79
80 vmw_fifo_commit(dev_priv, cmd_size);
81
82 return 0;
83}
84
85void vmw_cursor_update_position(struct vmw_private *dev_priv,
86 bool show, int x, int y)
87{
88 __le32 __iomem *fifo_mem = dev_priv->mmio_virt;
89 uint32_t count;
90
91 iowrite32(show ? 1 : 0, fifo_mem + SVGA_FIFO_CURSOR_ON);
92 iowrite32(x, fifo_mem + SVGA_FIFO_CURSOR_X);
93 iowrite32(y, fifo_mem + SVGA_FIFO_CURSOR_Y);
94 count = ioread32(fifo_mem + SVGA_FIFO_CURSOR_COUNT);
95 iowrite32(++count, fifo_mem + SVGA_FIFO_CURSOR_COUNT);
96}
97
98int vmw_du_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
99 uint32_t handle, uint32_t width, uint32_t height)
100{
101 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
102 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
103 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
104 struct vmw_surface *surface = NULL;
105 struct vmw_dma_buffer *dmabuf = NULL;
106 int ret;
107
108 if (handle) {
7a73ba74
TH
109 ret = vmw_user_surface_lookup_handle(dev_priv, tfile,
110 handle, &surface);
fb1d9738
JB
111 if (!ret) {
112 if (!surface->snooper.image) {
113 DRM_ERROR("surface not suitable for cursor\n");
e5c8dbb8 114 vmw_surface_unreference(&surface);
fb1d9738
JB
115 return -EINVAL;
116 }
117 } else {
118 ret = vmw_user_dmabuf_lookup(tfile,
119 handle, &dmabuf);
120 if (ret) {
121 DRM_ERROR("failed to find surface or dmabuf: %i\n", ret);
122 return -EINVAL;
123 }
124 }
125 }
126
127 /* takedown old cursor */
128 if (du->cursor_surface) {
129 du->cursor_surface->snooper.crtc = NULL;
130 vmw_surface_unreference(&du->cursor_surface);
131 }
132 if (du->cursor_dmabuf)
133 vmw_dmabuf_unreference(&du->cursor_dmabuf);
134
135 /* setup new image */
136 if (surface) {
137 /* vmw_user_surface_lookup takes one reference */
138 du->cursor_surface = surface;
139
140 du->cursor_surface->snooper.crtc = crtc;
141 du->cursor_age = du->cursor_surface->snooper.age;
142 vmw_cursor_update_image(dev_priv, surface->snooper.image,
143 64, 64, du->hotspot_x, du->hotspot_y);
144 } else if (dmabuf) {
145 struct ttm_bo_kmap_obj map;
146 unsigned long kmap_offset;
147 unsigned long kmap_num;
148 void *virtual;
149 bool dummy;
150
151 /* vmw_user_surface_lookup takes one reference */
152 du->cursor_dmabuf = dmabuf;
153
154 kmap_offset = 0;
155 kmap_num = (64*64*4) >> PAGE_SHIFT;
156
157 ret = ttm_bo_reserve(&dmabuf->base, true, false, false, 0);
158 if (unlikely(ret != 0)) {
159 DRM_ERROR("reserve failed\n");
160 return -EINVAL;
161 }
162
163 ret = ttm_bo_kmap(&dmabuf->base, kmap_offset, kmap_num, &map);
164 if (unlikely(ret != 0))
165 goto err_unreserve;
166
167 virtual = ttm_kmap_obj_virtual(&map, &dummy);
168 vmw_cursor_update_image(dev_priv, virtual, 64, 64,
169 du->hotspot_x, du->hotspot_y);
170
171 ttm_bo_kunmap(&map);
172err_unreserve:
173 ttm_bo_unreserve(&dmabuf->base);
174
175 } else {
176 vmw_cursor_update_position(dev_priv, false, 0, 0);
177 return 0;
178 }
179
da7653d6
TH
180 vmw_cursor_update_position(dev_priv, true,
181 du->cursor_x + du->hotspot_x,
182 du->cursor_y + du->hotspot_y);
fb1d9738
JB
183
184 return 0;
185}
186
187int vmw_du_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
188{
189 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
190 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
191 bool shown = du->cursor_surface || du->cursor_dmabuf ? true : false;
192
193 du->cursor_x = x + crtc->x;
194 du->cursor_y = y + crtc->y;
195
196 vmw_cursor_update_position(dev_priv, shown,
da7653d6
TH
197 du->cursor_x + du->hotspot_x,
198 du->cursor_y + du->hotspot_y);
fb1d9738
JB
199
200 return 0;
201}
202
203void vmw_kms_cursor_snoop(struct vmw_surface *srf,
204 struct ttm_object_file *tfile,
205 struct ttm_buffer_object *bo,
206 SVGA3dCmdHeader *header)
207{
208 struct ttm_bo_kmap_obj map;
209 unsigned long kmap_offset;
210 unsigned long kmap_num;
211 SVGA3dCopyBox *box;
212 unsigned box_count;
213 void *virtual;
214 bool dummy;
215 struct vmw_dma_cmd {
216 SVGA3dCmdHeader header;
217 SVGA3dCmdSurfaceDMA dma;
218 } *cmd;
2ac86371 219 int i, ret;
fb1d9738
JB
220
221 cmd = container_of(header, struct vmw_dma_cmd, header);
222
223 /* No snooper installed */
224 if (!srf->snooper.image)
225 return;
226
227 if (cmd->dma.host.face != 0 || cmd->dma.host.mipmap != 0) {
228 DRM_ERROR("face and mipmap for cursors should never != 0\n");
229 return;
230 }
231
232 if (cmd->header.size < 64) {
233 DRM_ERROR("at least one full copy box must be given\n");
234 return;
235 }
236
237 box = (SVGA3dCopyBox *)&cmd[1];
238 box_count = (cmd->header.size - sizeof(SVGA3dCmdSurfaceDMA)) /
239 sizeof(SVGA3dCopyBox);
240
2ac86371 241 if (cmd->dma.guest.ptr.offset % PAGE_SIZE ||
fb1d9738
JB
242 box->x != 0 || box->y != 0 || box->z != 0 ||
243 box->srcx != 0 || box->srcy != 0 || box->srcz != 0 ||
2ac86371 244 box->d != 1 || box_count != 1) {
fb1d9738 245 /* TODO handle none page aligned offsets */
2ac86371
JB
246 /* TODO handle more dst & src != 0 */
247 /* TODO handle more then one copy */
248 DRM_ERROR("Cant snoop dma request for cursor!\n");
249 DRM_ERROR("(%u, %u, %u) (%u, %u, %u) (%ux%ux%u) %u %u\n",
250 box->srcx, box->srcy, box->srcz,
251 box->x, box->y, box->z,
252 box->w, box->h, box->d, box_count,
253 cmd->dma.guest.ptr.offset);
fb1d9738
JB
254 return;
255 }
256
257 kmap_offset = cmd->dma.guest.ptr.offset >> PAGE_SHIFT;
258 kmap_num = (64*64*4) >> PAGE_SHIFT;
259
260 ret = ttm_bo_reserve(bo, true, false, false, 0);
261 if (unlikely(ret != 0)) {
262 DRM_ERROR("reserve failed\n");
263 return;
264 }
265
266 ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
267 if (unlikely(ret != 0))
268 goto err_unreserve;
269
270 virtual = ttm_kmap_obj_virtual(&map, &dummy);
271
2ac86371
JB
272 if (box->w == 64 && cmd->dma.guest.pitch == 64*4) {
273 memcpy(srf->snooper.image, virtual, 64*64*4);
274 } else {
275 /* Image is unsigned pointer. */
276 for (i = 0; i < box->h; i++)
277 memcpy(srf->snooper.image + i * 64,
278 virtual + i * cmd->dma.guest.pitch,
279 box->w * 4);
280 }
281
fb1d9738
JB
282 srf->snooper.age++;
283
284 /* we can't call this function from this function since execbuf has
285 * reserved fifo space.
286 *
287 * if (srf->snooper.crtc)
288 * vmw_ldu_crtc_cursor_update_image(dev_priv,
289 * srf->snooper.image, 64, 64,
290 * du->hotspot_x, du->hotspot_y);
291 */
292
293 ttm_bo_kunmap(&map);
294err_unreserve:
295 ttm_bo_unreserve(bo);
296}
297
298void vmw_kms_cursor_post_execbuf(struct vmw_private *dev_priv)
299{
300 struct drm_device *dev = dev_priv->dev;
301 struct vmw_display_unit *du;
302 struct drm_crtc *crtc;
303
304 mutex_lock(&dev->mode_config.mutex);
305
306 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
307 du = vmw_crtc_to_du(crtc);
308 if (!du->cursor_surface ||
309 du->cursor_age == du->cursor_surface->snooper.age)
310 continue;
311
312 du->cursor_age = du->cursor_surface->snooper.age;
313 vmw_cursor_update_image(dev_priv,
314 du->cursor_surface->snooper.image,
315 64, 64, du->hotspot_x, du->hotspot_y);
316 }
317
318 mutex_unlock(&dev->mode_config.mutex);
319}
320
321/*
322 * Generic framebuffer code
323 */
324
325int vmw_framebuffer_create_handle(struct drm_framebuffer *fb,
326 struct drm_file *file_priv,
327 unsigned int *handle)
328{
329 if (handle)
330 handle = 0;
331
332 return 0;
333}
334
335/*
336 * Surface framebuffer code
337 */
338
339#define vmw_framebuffer_to_vfbs(x) \
340 container_of(x, struct vmw_framebuffer_surface, base.base)
341
342struct vmw_framebuffer_surface {
343 struct vmw_framebuffer base;
344 struct vmw_surface *surface;
22ee861c 345 struct vmw_dma_buffer *buffer;
3a939a5e
TH
346 struct list_head head;
347 struct drm_master *master;
fb1d9738
JB
348};
349
350void vmw_framebuffer_surface_destroy(struct drm_framebuffer *framebuffer)
351{
3a939a5e 352 struct vmw_framebuffer_surface *vfbs =
fb1d9738 353 vmw_framebuffer_to_vfbs(framebuffer);
3a939a5e
TH
354 struct vmw_master *vmaster = vmw_master(vfbs->master);
355
356
357 mutex_lock(&vmaster->fb_surf_mutex);
358 list_del(&vfbs->head);
359 mutex_unlock(&vmaster->fb_surf_mutex);
fb1d9738 360
3a939a5e 361 drm_master_put(&vfbs->master);
fb1d9738 362 drm_framebuffer_cleanup(framebuffer);
3a939a5e 363 vmw_surface_unreference(&vfbs->surface);
90ff18bc 364 ttm_base_object_unref(&vfbs->base.user_obj);
fb1d9738 365
3a939a5e 366 kfree(vfbs);
fb1d9738
JB
367}
368
56d1c78d 369static int do_surface_dirty_sou(struct vmw_private *dev_priv,
90ff18bc 370 struct drm_file *file_priv,
56d1c78d 371 struct vmw_framebuffer *framebuffer,
56d1c78d
JB
372 unsigned flags, unsigned color,
373 struct drm_clip_rect *clips,
374 unsigned num_clips, int inc)
375{
c6ca8391
JB
376 struct drm_clip_rect *clips_ptr;
377 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
378 struct drm_crtc *crtc;
56d1c78d 379 size_t fifo_size;
c6ca8391
JB
380 int i, num_units;
381 int ret = 0; /* silence warning */
382 int left, right, top, bottom;
56d1c78d
JB
383
384 struct {
385 SVGA3dCmdHeader header;
386 SVGA3dCmdBlitSurfaceToScreen body;
387 } *cmd;
c6ca8391 388 SVGASignedRect *blits;
56d1c78d
JB
389
390
c6ca8391
JB
391 num_units = 0;
392 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list,
393 head) {
394 if (crtc->fb != &framebuffer->base)
395 continue;
396 units[num_units++] = vmw_crtc_to_du(crtc);
397 }
398
c6ca8391
JB
399 BUG_ON(!clips || !num_clips);
400
401 fifo_size = sizeof(*cmd) + sizeof(SVGASignedRect) * num_clips;
90ff18bc 402 cmd = kzalloc(fifo_size, GFP_KERNEL);
56d1c78d 403 if (unlikely(cmd == NULL)) {
90ff18bc 404 DRM_ERROR("Temporary fifo memory alloc failed.\n");
56d1c78d
JB
405 return -ENOMEM;
406 }
407
c6ca8391
JB
408 left = clips->x1;
409 right = clips->x2;
410 top = clips->y1;
411 bottom = clips->y2;
412
413 clips_ptr = clips;
414 for (i = 1; i < num_clips; i++, clips_ptr += inc) {
415 left = min_t(int, left, (int)clips_ptr->x1);
416 right = max_t(int, right, (int)clips_ptr->x2);
417 top = min_t(int, top, (int)clips_ptr->y1);
418 bottom = max_t(int, bottom, (int)clips_ptr->y2);
56d1c78d
JB
419 }
420
c6ca8391
JB
421 /* only need to do this once */
422 memset(cmd, 0, fifo_size);
423 cmd->header.id = cpu_to_le32(SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN);
424 cmd->header.size = cpu_to_le32(fifo_size - sizeof(cmd->header));
425
56d1c78d
JB
426 cmd->body.srcRect.left = left;
427 cmd->body.srcRect.right = right;
428 cmd->body.srcRect.top = top;
429 cmd->body.srcRect.bottom = bottom;
430
c6ca8391
JB
431 clips_ptr = clips;
432 blits = (SVGASignedRect *)&cmd[1];
433 for (i = 0; i < num_clips; i++, clips_ptr += inc) {
434 blits[i].left = clips_ptr->x1 - left;
435 blits[i].right = clips_ptr->x2 - left;
436 blits[i].top = clips_ptr->y1 - top;
437 blits[i].bottom = clips_ptr->y2 - top;
438 }
439
440 /* do per unit writing, reuse fifo for each */
441 for (i = 0; i < num_units; i++) {
442 struct vmw_display_unit *unit = units[i];
443 int clip_x1 = left - unit->crtc.x;
444 int clip_y1 = top - unit->crtc.y;
445 int clip_x2 = right - unit->crtc.x;
446 int clip_y2 = bottom - unit->crtc.y;
447
448 /* skip any crtcs that misses the clip region */
449 if (clip_x1 >= unit->crtc.mode.hdisplay ||
450 clip_y1 >= unit->crtc.mode.vdisplay ||
451 clip_x2 <= 0 || clip_y2 <= 0)
452 continue;
453
454 /* need to reset sid as it is changed by execbuf */
455 cmd->body.srcImage.sid = cpu_to_le32(framebuffer->user_handle);
456
457 cmd->body.destScreenId = unit->unit;
458
459 /*
460 * The blit command is a lot more resilient then the
461 * readback command when it comes to clip rects. So its
462 * okay to go out of bounds.
463 */
464
465 cmd->body.destRect.left = clip_x1;
466 cmd->body.destRect.right = clip_x2;
467 cmd->body.destRect.top = clip_y1;
468 cmd->body.destRect.bottom = clip_y2;
469
470
471 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd,
472 fifo_size, 0, NULL);
473
474 if (unlikely(ret != 0))
475 break;
476 }
56d1c78d 477
90ff18bc 478 kfree(cmd);
56d1c78d 479
90ff18bc 480 return ret;
5deb65cf 481}
fb1d9738
JB
482
483int vmw_framebuffer_surface_dirty(struct drm_framebuffer *framebuffer,
02b00162 484 struct drm_file *file_priv,
fb1d9738
JB
485 unsigned flags, unsigned color,
486 struct drm_clip_rect *clips,
487 unsigned num_clips)
488{
489 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
3a939a5e 490 struct vmw_master *vmaster = vmw_master(file_priv->master);
fb1d9738
JB
491 struct vmw_framebuffer_surface *vfbs =
492 vmw_framebuffer_to_vfbs(framebuffer);
fb1d9738 493 struct drm_clip_rect norect;
5deb65cf 494 int ret, inc = 1;
fb1d9738 495
3a939a5e
TH
496 if (unlikely(vfbs->master != file_priv->master))
497 return -EINVAL;
498
01e81419
JB
499 /* Require ScreenObject support for 3D */
500 if (!dev_priv->sou_priv)
501 return -EINVAL;
502
3a939a5e
TH
503 ret = ttm_read_lock(&vmaster->lock, true);
504 if (unlikely(ret != 0))
505 return ret;
506
fb1d9738
JB
507 if (!num_clips) {
508 num_clips = 1;
509 clips = &norect;
510 norect.x1 = norect.y1 = 0;
511 norect.x2 = framebuffer->width;
512 norect.y2 = framebuffer->height;
513 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
514 num_clips /= 2;
515 inc = 2; /* skip source rects */
516 }
517
c5c42360 518 ret = do_surface_dirty_sou(dev_priv, file_priv, &vfbs->base,
01e81419
JB
519 flags, color,
520 clips, num_clips, inc);
fb1d9738 521
3a939a5e 522 ttm_read_unlock(&vmaster->lock);
fb1d9738
JB
523 return 0;
524}
525
526static struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs = {
527 .destroy = vmw_framebuffer_surface_destroy,
528 .dirty = vmw_framebuffer_surface_dirty,
529 .create_handle = vmw_framebuffer_create_handle,
530};
531
d3216a0c 532static int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv,
3a939a5e 533 struct drm_file *file_priv,
d3216a0c
TH
534 struct vmw_surface *surface,
535 struct vmw_framebuffer **out,
536 const struct drm_mode_fb_cmd
537 *mode_cmd)
fb1d9738
JB
538
539{
540 struct drm_device *dev = dev_priv->dev;
541 struct vmw_framebuffer_surface *vfbs;
d3216a0c 542 enum SVGA3dSurfaceFormat format;
3a939a5e 543 struct vmw_master *vmaster = vmw_master(file_priv->master);
fb1d9738
JB
544 int ret;
545
01e81419
JB
546 /* 3D is only supported on HWv8 hosts which supports screen objects */
547 if (!dev_priv->sou_priv)
548 return -ENOSYS;
549
d3216a0c
TH
550 /*
551 * Sanity checks.
552 */
553
554 if (unlikely(surface->mip_levels[0] != 1 ||
555 surface->num_sizes != 1 ||
556 surface->sizes[0].width < mode_cmd->width ||
557 surface->sizes[0].height < mode_cmd->height ||
558 surface->sizes[0].depth != 1)) {
559 DRM_ERROR("Incompatible surface dimensions "
560 "for requested mode.\n");
561 return -EINVAL;
562 }
563
564 switch (mode_cmd->depth) {
565 case 32:
566 format = SVGA3D_A8R8G8B8;
567 break;
568 case 24:
569 format = SVGA3D_X8R8G8B8;
570 break;
571 case 16:
572 format = SVGA3D_R5G6B5;
573 break;
574 case 15:
575 format = SVGA3D_A1R5G5B5;
576 break;
f01b7ba0
MD
577 case 8:
578 format = SVGA3D_LUMINANCE8;
579 break;
d3216a0c
TH
580 default:
581 DRM_ERROR("Invalid color depth: %d\n", mode_cmd->depth);
582 return -EINVAL;
583 }
584
585 if (unlikely(format != surface->format)) {
586 DRM_ERROR("Invalid surface format for requested mode.\n");
587 return -EINVAL;
588 }
589
fb1d9738
JB
590 vfbs = kzalloc(sizeof(*vfbs), GFP_KERNEL);
591 if (!vfbs) {
592 ret = -ENOMEM;
593 goto out_err1;
594 }
595
596 ret = drm_framebuffer_init(dev, &vfbs->base.base,
597 &vmw_framebuffer_surface_funcs);
598 if (ret)
599 goto out_err2;
600
601 if (!vmw_surface_reference(surface)) {
602 DRM_ERROR("failed to reference surface %p\n", surface);
603 goto out_err3;
604 }
605
606 /* XXX get the first 3 from the surface info */
d3216a0c
TH
607 vfbs->base.base.bits_per_pixel = mode_cmd->bpp;
608 vfbs->base.base.pitch = mode_cmd->pitch;
609 vfbs->base.base.depth = mode_cmd->depth;
610 vfbs->base.base.width = mode_cmd->width;
611 vfbs->base.base.height = mode_cmd->height;
fb1d9738 612 vfbs->surface = surface;
90ff18bc 613 vfbs->base.user_handle = mode_cmd->handle;
3a939a5e 614 vfbs->master = drm_master_get(file_priv->master);
3a939a5e
TH
615
616 mutex_lock(&vmaster->fb_surf_mutex);
3a939a5e
TH
617 list_add_tail(&vfbs->head, &vmaster->fb_surf);
618 mutex_unlock(&vmaster->fb_surf_mutex);
619
fb1d9738
JB
620 *out = &vfbs->base;
621
622 return 0;
623
624out_err3:
625 drm_framebuffer_cleanup(&vfbs->base.base);
626out_err2:
627 kfree(vfbs);
628out_err1:
629 return ret;
630}
631
632/*
633 * Dmabuf framebuffer code
634 */
635
636#define vmw_framebuffer_to_vfbd(x) \
637 container_of(x, struct vmw_framebuffer_dmabuf, base.base)
638
639struct vmw_framebuffer_dmabuf {
640 struct vmw_framebuffer base;
641 struct vmw_dma_buffer *buffer;
642};
643
644void vmw_framebuffer_dmabuf_destroy(struct drm_framebuffer *framebuffer)
645{
646 struct vmw_framebuffer_dmabuf *vfbd =
647 vmw_framebuffer_to_vfbd(framebuffer);
648
649 drm_framebuffer_cleanup(framebuffer);
650 vmw_dmabuf_unreference(&vfbd->buffer);
90ff18bc 651 ttm_base_object_unref(&vfbd->base.user_obj);
fb1d9738
JB
652
653 kfree(vfbd);
654}
655
5deb65cf
JB
656static int do_dmabuf_dirty_ldu(struct vmw_private *dev_priv,
657 struct vmw_framebuffer *framebuffer,
5deb65cf
JB
658 unsigned flags, unsigned color,
659 struct drm_clip_rect *clips,
660 unsigned num_clips, int increment)
661{
662 size_t fifo_size;
663 int i;
664
665 struct {
666 uint32_t header;
667 SVGAFifoCmdUpdate body;
668 } *cmd;
669
670 fifo_size = sizeof(*cmd) * num_clips;
671 cmd = vmw_fifo_reserve(dev_priv, fifo_size);
672 if (unlikely(cmd == NULL)) {
673 DRM_ERROR("Fifo reserve failed.\n");
674 return -ENOMEM;
675 }
676
677 memset(cmd, 0, fifo_size);
678 for (i = 0; i < num_clips; i++, clips += increment) {
679 cmd[i].header = cpu_to_le32(SVGA_CMD_UPDATE);
680 cmd[i].body.x = cpu_to_le32(clips->x1);
681 cmd[i].body.y = cpu_to_le32(clips->y1);
682 cmd[i].body.width = cpu_to_le32(clips->x2 - clips->x1);
683 cmd[i].body.height = cpu_to_le32(clips->y2 - clips->y1);
684 }
685
686 vmw_fifo_commit(dev_priv, fifo_size);
687 return 0;
688}
689
c6ca8391
JB
690static int do_dmabuf_define_gmrfb(struct drm_file *file_priv,
691 struct vmw_private *dev_priv,
692 struct vmw_framebuffer *framebuffer)
56d1c78d 693{
64fc9944 694 int depth = framebuffer->base.depth;
56d1c78d 695 size_t fifo_size;
c6ca8391 696 int ret;
56d1c78d
JB
697
698 struct {
699 uint32_t header;
700 SVGAFifoCmdDefineGMRFB body;
701 } *cmd;
56d1c78d 702
64fc9944
JB
703 /* Emulate RGBA support, contrary to svga_reg.h this is not
704 * supported by hosts. This is only a problem if we are reading
705 * this value later and expecting what we uploaded back.
706 */
707 if (depth == 32)
708 depth = 24;
709
c6ca8391 710 fifo_size = sizeof(*cmd);
56d1c78d
JB
711 cmd = kmalloc(fifo_size, GFP_KERNEL);
712 if (unlikely(cmd == NULL)) {
713 DRM_ERROR("Failed to allocate temporary cmd buffer.\n");
714 return -ENOMEM;
715 }
716
717 memset(cmd, 0, fifo_size);
718 cmd->header = SVGA_CMD_DEFINE_GMRFB;
719 cmd->body.format.bitsPerPixel = framebuffer->base.bits_per_pixel;
64fc9944 720 cmd->body.format.colorDepth = depth;
56d1c78d
JB
721 cmd->body.format.reserved = 0;
722 cmd->body.bytesPerLine = framebuffer->base.pitch;
90ff18bc 723 cmd->body.ptr.gmrId = framebuffer->user_handle;
56d1c78d
JB
724 cmd->body.ptr.offset = 0;
725
56d1c78d
JB
726 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd,
727 fifo_size, 0, NULL);
728
729 kfree(cmd);
730
731 return ret;
732}
733
c6ca8391
JB
734static int do_dmabuf_dirty_sou(struct drm_file *file_priv,
735 struct vmw_private *dev_priv,
736 struct vmw_framebuffer *framebuffer,
c6ca8391
JB
737 unsigned flags, unsigned color,
738 struct drm_clip_rect *clips,
739 unsigned num_clips, int increment)
740{
741 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
742 struct drm_clip_rect *clips_ptr;
743 int i, k, num_units, ret;
744 struct drm_crtc *crtc;
745 size_t fifo_size;
746
747 struct {
748 uint32_t header;
749 SVGAFifoCmdBlitGMRFBToScreen body;
750 } *blits;
751
752 ret = do_dmabuf_define_gmrfb(file_priv, dev_priv, framebuffer);
753 if (unlikely(ret != 0))
754 return ret; /* define_gmrfb prints warnings */
755
756 fifo_size = sizeof(*blits) * num_clips;
757 blits = kmalloc(fifo_size, GFP_KERNEL);
758 if (unlikely(blits == NULL)) {
759 DRM_ERROR("Failed to allocate temporary cmd buffer.\n");
760 return -ENOMEM;
761 }
762
763 num_units = 0;
764 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
765 if (crtc->fb != &framebuffer->base)
766 continue;
767 units[num_units++] = vmw_crtc_to_du(crtc);
768 }
769
770 for (k = 0; k < num_units; k++) {
771 struct vmw_display_unit *unit = units[k];
772 int hit_num = 0;
773
774 clips_ptr = clips;
775 for (i = 0; i < num_clips; i++, clips_ptr += increment) {
776 int clip_x1 = clips_ptr->x1 - unit->crtc.x;
777 int clip_y1 = clips_ptr->y1 - unit->crtc.y;
778 int clip_x2 = clips_ptr->x2 - unit->crtc.x;
779 int clip_y2 = clips_ptr->y2 - unit->crtc.y;
780
781 /* skip any crtcs that misses the clip region */
782 if (clip_x1 >= unit->crtc.mode.hdisplay ||
783 clip_y1 >= unit->crtc.mode.vdisplay ||
784 clip_x2 <= 0 || clip_y2 <= 0)
785 continue;
786
787 blits[hit_num].header = SVGA_CMD_BLIT_GMRFB_TO_SCREEN;
788 blits[hit_num].body.destScreenId = unit->unit;
789 blits[hit_num].body.srcOrigin.x = clips_ptr->x1;
790 blits[hit_num].body.srcOrigin.y = clips_ptr->y1;
791 blits[hit_num].body.destRect.left = clip_x1;
792 blits[hit_num].body.destRect.top = clip_y1;
793 blits[hit_num].body.destRect.right = clip_x2;
794 blits[hit_num].body.destRect.bottom = clip_y2;
795 hit_num++;
796 }
797
798 /* no clips hit the crtc */
799 if (hit_num == 0)
800 continue;
801
802 fifo_size = sizeof(*blits) * hit_num;
803 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, blits,
804 fifo_size, 0, NULL);
805
806 if (unlikely(ret != 0))
807 break;
808 }
809
810 kfree(blits);
811
812 return ret;
813}
814
fb1d9738 815int vmw_framebuffer_dmabuf_dirty(struct drm_framebuffer *framebuffer,
02b00162 816 struct drm_file *file_priv,
fb1d9738
JB
817 unsigned flags, unsigned color,
818 struct drm_clip_rect *clips,
819 unsigned num_clips)
820{
821 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
3a939a5e 822 struct vmw_master *vmaster = vmw_master(file_priv->master);
5deb65cf
JB
823 struct vmw_framebuffer_dmabuf *vfbd =
824 vmw_framebuffer_to_vfbd(framebuffer);
fb1d9738 825 struct drm_clip_rect norect;
5deb65cf 826 int ret, increment = 1;
fb1d9738 827
3a939a5e
TH
828 ret = ttm_read_lock(&vmaster->lock, true);
829 if (unlikely(ret != 0))
830 return ret;
831
df1c93ba 832 if (!num_clips) {
fb1d9738
JB
833 num_clips = 1;
834 clips = &norect;
835 norect.x1 = norect.y1 = 0;
836 norect.x2 = framebuffer->width;
837 norect.y2 = framebuffer->height;
838 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
839 num_clips /= 2;
840 increment = 2;
841 }
842
56d1c78d 843 if (dev_priv->ldu_priv) {
c5c42360 844 ret = do_dmabuf_dirty_ldu(dev_priv, &vfbd->base,
56d1c78d
JB
845 flags, color,
846 clips, num_clips, increment);
847 } else {
848 ret = do_dmabuf_dirty_sou(file_priv, dev_priv, &vfbd->base,
c5c42360 849 flags, color,
56d1c78d
JB
850 clips, num_clips, increment);
851 }
fb1d9738 852
3a939a5e 853 ttm_read_unlock(&vmaster->lock);
5deb65cf 854 return ret;
fb1d9738
JB
855}
856
857static struct drm_framebuffer_funcs vmw_framebuffer_dmabuf_funcs = {
858 .destroy = vmw_framebuffer_dmabuf_destroy,
859 .dirty = vmw_framebuffer_dmabuf_dirty,
860 .create_handle = vmw_framebuffer_create_handle,
861};
862
497a3ff9
JB
863/**
864 * Pin the dmabuffer to the start of vram.
865 */
fb1d9738
JB
866static int vmw_framebuffer_dmabuf_pin(struct vmw_framebuffer *vfb)
867{
868 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
869 struct vmw_framebuffer_dmabuf *vfbd =
870 vmw_framebuffer_to_vfbd(&vfb->base);
871 int ret;
872
56d1c78d
JB
873 /* This code should not be used with screen objects */
874 BUG_ON(dev_priv->sou_priv);
d7e1958d 875
fb1d9738
JB
876 vmw_overlay_pause_all(dev_priv);
877
d991ef03 878 ret = vmw_dmabuf_to_start_of_vram(dev_priv, vfbd->buffer, true, false);
fb1d9738 879
fb1d9738
JB
880 vmw_overlay_resume_all(dev_priv);
881
316ab13a
JB
882 WARN_ON(ret != 0);
883
fb1d9738
JB
884 return 0;
885}
886
887static int vmw_framebuffer_dmabuf_unpin(struct vmw_framebuffer *vfb)
888{
889 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
890 struct vmw_framebuffer_dmabuf *vfbd =
891 vmw_framebuffer_to_vfbd(&vfb->base);
892
893 if (!vfbd->buffer) {
894 WARN_ON(!vfbd->buffer);
895 return 0;
896 }
897
d991ef03 898 return vmw_dmabuf_unpin(dev_priv, vfbd->buffer, false);
fb1d9738
JB
899}
900
d3216a0c
TH
901static int vmw_kms_new_framebuffer_dmabuf(struct vmw_private *dev_priv,
902 struct vmw_dma_buffer *dmabuf,
903 struct vmw_framebuffer **out,
904 const struct drm_mode_fb_cmd
905 *mode_cmd)
fb1d9738
JB
906
907{
908 struct drm_device *dev = dev_priv->dev;
909 struct vmw_framebuffer_dmabuf *vfbd;
d3216a0c 910 unsigned int requested_size;
fb1d9738
JB
911 int ret;
912
d3216a0c
TH
913 requested_size = mode_cmd->height * mode_cmd->pitch;
914 if (unlikely(requested_size > dmabuf->base.num_pages * PAGE_SIZE)) {
915 DRM_ERROR("Screen buffer object size is too small "
916 "for requested mode.\n");
917 return -EINVAL;
918 }
919
c337ada7
JB
920 /* Limited framebuffer color depth support for screen objects */
921 if (dev_priv->sou_priv) {
922 switch (mode_cmd->depth) {
923 case 32:
924 case 24:
925 /* Only support 32 bpp for 32 and 24 depth fbs */
926 if (mode_cmd->bpp == 32)
927 break;
928
929 DRM_ERROR("Invalid color depth/bbp: %d %d\n",
930 mode_cmd->depth, mode_cmd->bpp);
931 return -EINVAL;
932 case 16:
933 case 15:
934 /* Only support 16 bpp for 16 and 15 depth fbs */
935 if (mode_cmd->bpp == 16)
936 break;
937
938 DRM_ERROR("Invalid color depth/bbp: %d %d\n",
939 mode_cmd->depth, mode_cmd->bpp);
940 return -EINVAL;
941 default:
942 DRM_ERROR("Invalid color depth: %d\n", mode_cmd->depth);
943 return -EINVAL;
944 }
945 }
946
fb1d9738
JB
947 vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL);
948 if (!vfbd) {
949 ret = -ENOMEM;
950 goto out_err1;
951 }
952
953 ret = drm_framebuffer_init(dev, &vfbd->base.base,
954 &vmw_framebuffer_dmabuf_funcs);
955 if (ret)
956 goto out_err2;
957
958 if (!vmw_dmabuf_reference(dmabuf)) {
959 DRM_ERROR("failed to reference dmabuf %p\n", dmabuf);
960 goto out_err3;
961 }
962
d3216a0c
TH
963 vfbd->base.base.bits_per_pixel = mode_cmd->bpp;
964 vfbd->base.base.pitch = mode_cmd->pitch;
965 vfbd->base.base.depth = mode_cmd->depth;
966 vfbd->base.base.width = mode_cmd->width;
967 vfbd->base.base.height = mode_cmd->height;
56d1c78d
JB
968 if (!dev_priv->sou_priv) {
969 vfbd->base.pin = vmw_framebuffer_dmabuf_pin;
970 vfbd->base.unpin = vmw_framebuffer_dmabuf_unpin;
971 }
2fcd5a73 972 vfbd->base.dmabuf = true;
fb1d9738 973 vfbd->buffer = dmabuf;
90ff18bc 974 vfbd->base.user_handle = mode_cmd->handle;
fb1d9738
JB
975 *out = &vfbd->base;
976
977 return 0;
978
979out_err3:
980 drm_framebuffer_cleanup(&vfbd->base.base);
981out_err2:
982 kfree(vfbd);
983out_err1:
984 return ret;
985}
986
987/*
988 * Generic Kernel modesetting functions
989 */
990
991static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev,
992 struct drm_file *file_priv,
993 struct drm_mode_fb_cmd *mode_cmd)
994{
995 struct vmw_private *dev_priv = vmw_priv(dev);
996 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
997 struct vmw_framebuffer *vfb = NULL;
998 struct vmw_surface *surface = NULL;
999 struct vmw_dma_buffer *bo = NULL;
90ff18bc 1000 struct ttm_base_object *user_obj;
e133e737 1001 u64 required_size;
fb1d9738
JB
1002 int ret;
1003
d3216a0c
TH
1004 /**
1005 * This code should be conditioned on Screen Objects not being used.
1006 * If screen objects are used, we can allocate a GMR to hold the
1007 * requested framebuffer.
1008 */
1009
1010 required_size = mode_cmd->pitch * mode_cmd->height;
e133e737 1011 if (unlikely(required_size > (u64) dev_priv->vram_size)) {
d3216a0c 1012 DRM_ERROR("VRAM size is too small for requested mode.\n");
d9826409 1013 return ERR_PTR(-ENOMEM);
d3216a0c
TH
1014 }
1015
90ff18bc
TH
1016 /*
1017 * Take a reference on the user object of the resource
1018 * backing the kms fb. This ensures that user-space handle
1019 * lookups on that resource will always work as long as
1020 * it's registered with a kms framebuffer. This is important,
1021 * since vmw_execbuf_process identifies resources in the
1022 * command stream using user-space handles.
1023 */
1024
1025 user_obj = ttm_base_object_lookup(tfile, mode_cmd->handle);
1026 if (unlikely(user_obj == NULL)) {
1027 DRM_ERROR("Could not locate requested kms frame buffer.\n");
1028 return ERR_PTR(-ENOENT);
1029 }
1030
d3216a0c
TH
1031 /**
1032 * End conditioned code.
1033 */
1034
7a73ba74
TH
1035 ret = vmw_user_surface_lookup_handle(dev_priv, tfile,
1036 mode_cmd->handle, &surface);
fb1d9738
JB
1037 if (ret)
1038 goto try_dmabuf;
1039
5ffdb658
JB
1040 if (!surface->scanout)
1041 goto err_not_scanout;
1042
3a939a5e
TH
1043 ret = vmw_kms_new_framebuffer_surface(dev_priv, file_priv, surface,
1044 &vfb, mode_cmd);
fb1d9738
JB
1045
1046 /* vmw_user_surface_lookup takes one ref so does new_fb */
1047 vmw_surface_unreference(&surface);
1048
1049 if (ret) {
1050 DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
90ff18bc 1051 ttm_base_object_unref(&user_obj);
cce13ff7 1052 return ERR_PTR(ret);
90ff18bc
TH
1053 } else
1054 vfb->user_obj = user_obj;
fb1d9738
JB
1055 return &vfb->base;
1056
1057try_dmabuf:
1058 DRM_INFO("%s: trying buffer\n", __func__);
1059
1060 ret = vmw_user_dmabuf_lookup(tfile, mode_cmd->handle, &bo);
1061 if (ret) {
1062 DRM_ERROR("failed to find buffer: %i\n", ret);
cce13ff7 1063 return ERR_PTR(-ENOENT);
fb1d9738
JB
1064 }
1065
1066 ret = vmw_kms_new_framebuffer_dmabuf(dev_priv, bo, &vfb,
d3216a0c 1067 mode_cmd);
fb1d9738
JB
1068
1069 /* vmw_user_dmabuf_lookup takes one ref so does new_fb */
1070 vmw_dmabuf_unreference(&bo);
1071
1072 if (ret) {
1073 DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
90ff18bc 1074 ttm_base_object_unref(&user_obj);
cce13ff7 1075 return ERR_PTR(ret);
90ff18bc
TH
1076 } else
1077 vfb->user_obj = user_obj;
fb1d9738
JB
1078
1079 return &vfb->base;
5ffdb658
JB
1080
1081err_not_scanout:
1082 DRM_ERROR("surface not marked as scanout\n");
1083 /* vmw_user_surface_lookup takes one ref */
1084 vmw_surface_unreference(&surface);
90ff18bc 1085 ttm_base_object_unref(&user_obj);
5ffdb658 1086
cce13ff7 1087 return ERR_PTR(-EINVAL);
fb1d9738
JB
1088}
1089
fb1d9738
JB
1090static struct drm_mode_config_funcs vmw_kms_funcs = {
1091 .fb_create = vmw_kms_fb_create,
fb1d9738
JB
1092};
1093
2fcd5a73
JB
1094int vmw_kms_present(struct vmw_private *dev_priv,
1095 struct drm_file *file_priv,
1096 struct vmw_framebuffer *vfb,
1097 struct vmw_surface *surface,
1098 uint32_t sid,
1099 int32_t destX, int32_t destY,
1100 struct drm_vmw_rect *clips,
1101 uint32_t num_clips)
1102{
c6ca8391
JB
1103 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
1104 struct drm_crtc *crtc;
2fcd5a73 1105 size_t fifo_size;
c6ca8391
JB
1106 int i, k, num_units;
1107 int ret = 0; /* silence warning */
2fcd5a73
JB
1108
1109 struct {
1110 SVGA3dCmdHeader header;
1111 SVGA3dCmdBlitSurfaceToScreen body;
1112 } *cmd;
1113 SVGASignedRect *blits;
1114
c6ca8391
JB
1115 num_units = 0;
1116 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
1117 if (crtc->fb != &vfb->base)
1118 continue;
1119 units[num_units++] = vmw_crtc_to_du(crtc);
1120 }
1121
2fcd5a73
JB
1122 BUG_ON(surface == NULL);
1123 BUG_ON(!clips || !num_clips);
1124
1125 fifo_size = sizeof(*cmd) + sizeof(SVGASignedRect) * num_clips;
1126 cmd = kmalloc(fifo_size, GFP_KERNEL);
1127 if (unlikely(cmd == NULL)) {
1128 DRM_ERROR("Failed to allocate temporary fifo memory.\n");
1129 return -ENOMEM;
1130 }
1131
c6ca8391 1132 /* only need to do this once */
2fcd5a73 1133 memset(cmd, 0, fifo_size);
2fcd5a73
JB
1134 cmd->header.id = cpu_to_le32(SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN);
1135 cmd->header.size = cpu_to_le32(fifo_size - sizeof(cmd->header));
1136
2fcd5a73
JB
1137 cmd->body.srcRect.left = 0;
1138 cmd->body.srcRect.right = surface->sizes[0].width;
1139 cmd->body.srcRect.top = 0;
1140 cmd->body.srcRect.bottom = surface->sizes[0].height;
1141
2fcd5a73
JB
1142 blits = (SVGASignedRect *)&cmd[1];
1143 for (i = 0; i < num_clips; i++) {
1144 blits[i].left = clips[i].x;
1145 blits[i].right = clips[i].x + clips[i].w;
1146 blits[i].top = clips[i].y;
1147 blits[i].bottom = clips[i].y + clips[i].h;
1148 }
1149
c6ca8391
JB
1150 for (k = 0; k < num_units; k++) {
1151 struct vmw_display_unit *unit = units[k];
1152 int clip_x1 = destX - unit->crtc.x;
1153 int clip_y1 = destY - unit->crtc.y;
1154 int clip_x2 = clip_x1 + surface->sizes[0].width;
1155 int clip_y2 = clip_y1 + surface->sizes[0].height;
1156
1157 /* skip any crtcs that misses the clip region */
1158 if (clip_x1 >= unit->crtc.mode.hdisplay ||
1159 clip_y1 >= unit->crtc.mode.vdisplay ||
1160 clip_x2 <= 0 || clip_y2 <= 0)
1161 continue;
1162
1163 /* need to reset sid as it is changed by execbuf */
1164 cmd->body.srcImage.sid = sid;
1165
1166 cmd->body.destScreenId = unit->unit;
1167
1168 /*
1169 * The blit command is a lot more resilient then the
1170 * readback command when it comes to clip rects. So its
1171 * okay to go out of bounds.
1172 */
1173
1174 cmd->body.destRect.left = clip_x1;
1175 cmd->body.destRect.right = clip_x2;
1176 cmd->body.destRect.top = clip_y1;
1177 cmd->body.destRect.bottom = clip_y2;
1178
1179 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd,
1180 fifo_size, 0, NULL);
1181
1182 if (unlikely(ret != 0))
1183 break;
1184 }
2fcd5a73
JB
1185
1186 kfree(cmd);
1187
1188 return ret;
1189}
1190
1191int vmw_kms_readback(struct vmw_private *dev_priv,
1192 struct drm_file *file_priv,
1193 struct vmw_framebuffer *vfb,
1194 struct drm_vmw_fence_rep __user *user_fence_rep,
1195 struct drm_vmw_rect *clips,
1196 uint32_t num_clips)
1197{
1198 struct vmw_framebuffer_dmabuf *vfbd =
1199 vmw_framebuffer_to_vfbd(&vfb->base);
1200 struct vmw_dma_buffer *dmabuf = vfbd->buffer;
1201 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
1202 struct drm_crtc *crtc;
1203 size_t fifo_size;
1204 int i, k, ret, num_units, blits_pos;
1205
1206 struct {
1207 uint32_t header;
1208 SVGAFifoCmdDefineGMRFB body;
1209 } *cmd;
1210 struct {
1211 uint32_t header;
1212 SVGAFifoCmdBlitScreenToGMRFB body;
1213 } *blits;
1214
1215 num_units = 0;
1216 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
1217 if (crtc->fb != &vfb->base)
1218 continue;
1219 units[num_units++] = vmw_crtc_to_du(crtc);
1220 }
1221
1222 BUG_ON(dmabuf == NULL);
1223 BUG_ON(!clips || !num_clips);
1224
1225 /* take a safe guess at fifo size */
1226 fifo_size = sizeof(*cmd) + sizeof(*blits) * num_clips * num_units;
1227 cmd = kmalloc(fifo_size, GFP_KERNEL);
1228 if (unlikely(cmd == NULL)) {
1229 DRM_ERROR("Failed to allocate temporary fifo memory.\n");
1230 return -ENOMEM;
1231 }
1232
1233 memset(cmd, 0, fifo_size);
1234 cmd->header = SVGA_CMD_DEFINE_GMRFB;
1235 cmd->body.format.bitsPerPixel = vfb->base.bits_per_pixel;
1236 cmd->body.format.colorDepth = vfb->base.depth;
1237 cmd->body.format.reserved = 0;
1238 cmd->body.bytesPerLine = vfb->base.pitch;
90ff18bc 1239 cmd->body.ptr.gmrId = vfb->user_handle;
2fcd5a73
JB
1240 cmd->body.ptr.offset = 0;
1241
1242 blits = (void *)&cmd[1];
1243 blits_pos = 0;
1244 for (i = 0; i < num_units; i++) {
1245 struct drm_vmw_rect *c = clips;
1246 for (k = 0; k < num_clips; k++, c++) {
1247 /* transform clip coords to crtc origin based coords */
1248 int clip_x1 = c->x - units[i]->crtc.x;
1249 int clip_x2 = c->x - units[i]->crtc.x + c->w;
1250 int clip_y1 = c->y - units[i]->crtc.y;
1251 int clip_y2 = c->y - units[i]->crtc.y + c->h;
1252 int dest_x = c->x;
1253 int dest_y = c->y;
1254
1255 /* compensate for clipping, we negate
1256 * a negative number and add that.
1257 */
1258 if (clip_x1 < 0)
1259 dest_x += -clip_x1;
1260 if (clip_y1 < 0)
1261 dest_y += -clip_y1;
1262
1263 /* clip */
1264 clip_x1 = max(clip_x1, 0);
1265 clip_y1 = max(clip_y1, 0);
1266 clip_x2 = min(clip_x2, units[i]->crtc.mode.hdisplay);
1267 clip_y2 = min(clip_y2, units[i]->crtc.mode.vdisplay);
1268
1269 /* and cull any rects that misses the crtc */
1270 if (clip_x1 >= units[i]->crtc.mode.hdisplay ||
1271 clip_y1 >= units[i]->crtc.mode.vdisplay ||
1272 clip_x2 <= 0 || clip_y2 <= 0)
1273 continue;
1274
1275 blits[blits_pos].header = SVGA_CMD_BLIT_SCREEN_TO_GMRFB;
1276 blits[blits_pos].body.srcScreenId = units[i]->unit;
1277 blits[blits_pos].body.destOrigin.x = dest_x;
1278 blits[blits_pos].body.destOrigin.y = dest_y;
1279
1280 blits[blits_pos].body.srcRect.left = clip_x1;
1281 blits[blits_pos].body.srcRect.top = clip_y1;
1282 blits[blits_pos].body.srcRect.right = clip_x2;
1283 blits[blits_pos].body.srcRect.bottom = clip_y2;
1284 blits_pos++;
1285 }
1286 }
1287 /* reset size here and use calculated exact size from loops */
1288 fifo_size = sizeof(*cmd) + sizeof(*blits) * blits_pos;
1289
1290 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd, fifo_size,
1291 0, user_fence_rep);
1292
1293 kfree(cmd);
1294
1295 return ret;
1296}
1297
fb1d9738
JB
1298int vmw_kms_init(struct vmw_private *dev_priv)
1299{
1300 struct drm_device *dev = dev_priv->dev;
1301 int ret;
1302
1303 drm_mode_config_init(dev);
1304 dev->mode_config.funcs = &vmw_kms_funcs;
3bef3572
JB
1305 dev->mode_config.min_width = 1;
1306 dev->mode_config.min_height = 1;
7e71f8a5
JB
1307 /* assumed largest fb size */
1308 dev->mode_config.max_width = 8192;
1309 dev->mode_config.max_height = 8192;
fb1d9738 1310
56d1c78d
JB
1311 ret = vmw_kms_init_screen_object_display(dev_priv);
1312 if (ret) /* Fallback */
1313 (void)vmw_kms_init_legacy_display_system(dev_priv);
fb1d9738
JB
1314
1315 return 0;
1316}
1317
1318int vmw_kms_close(struct vmw_private *dev_priv)
1319{
1320 /*
1321 * Docs says we should take the lock before calling this function
1322 * but since it destroys encoders and our destructor calls
1323 * drm_encoder_cleanup which takes the lock we deadlock.
1324 */
1325 drm_mode_config_cleanup(dev_priv->dev);
c0d18316
JB
1326 if (dev_priv->sou_priv)
1327 vmw_kms_close_screen_object_display(dev_priv);
1328 else
1329 vmw_kms_close_legacy_display_system(dev_priv);
fb1d9738
JB
1330 return 0;
1331}
1332
1333int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
1334 struct drm_file *file_priv)
1335{
1336 struct drm_vmw_cursor_bypass_arg *arg = data;
1337 struct vmw_display_unit *du;
1338 struct drm_mode_object *obj;
1339 struct drm_crtc *crtc;
1340 int ret = 0;
1341
1342
1343 mutex_lock(&dev->mode_config.mutex);
1344 if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
1345
1346 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1347 du = vmw_crtc_to_du(crtc);
1348 du->hotspot_x = arg->xhot;
1349 du->hotspot_y = arg->yhot;
1350 }
1351
1352 mutex_unlock(&dev->mode_config.mutex);
1353 return 0;
1354 }
1355
1356 obj = drm_mode_object_find(dev, arg->crtc_id, DRM_MODE_OBJECT_CRTC);
1357 if (!obj) {
1358 ret = -EINVAL;
1359 goto out;
1360 }
1361
1362 crtc = obj_to_crtc(obj);
1363 du = vmw_crtc_to_du(crtc);
1364
1365 du->hotspot_x = arg->xhot;
1366 du->hotspot_y = arg->yhot;
1367
1368out:
1369 mutex_unlock(&dev->mode_config.mutex);
1370
1371 return ret;
1372}
1373
0bef23f9 1374int vmw_kms_write_svga(struct vmw_private *vmw_priv,
d7e1958d 1375 unsigned width, unsigned height, unsigned pitch,
6558429b 1376 unsigned bpp, unsigned depth)
fb1d9738 1377{
d7e1958d
JB
1378 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1379 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch);
1380 else if (vmw_fifo_have_pitchlock(vmw_priv))
1381 iowrite32(pitch, vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
1382 vmw_write(vmw_priv, SVGA_REG_WIDTH, width);
1383 vmw_write(vmw_priv, SVGA_REG_HEIGHT, height);
6558429b 1384 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp);
0bef23f9
MD
1385
1386 if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) {
1387 DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n",
1388 depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH));
1389 return -EINVAL;
1390 }
1391
1392 return 0;
d7e1958d 1393}
fb1d9738 1394
d7e1958d
JB
1395int vmw_kms_save_vga(struct vmw_private *vmw_priv)
1396{
7c4f7780
TH
1397 struct vmw_vga_topology_state *save;
1398 uint32_t i;
1399
fb1d9738
JB
1400 vmw_priv->vga_width = vmw_read(vmw_priv, SVGA_REG_WIDTH);
1401 vmw_priv->vga_height = vmw_read(vmw_priv, SVGA_REG_HEIGHT);
7c4f7780 1402 vmw_priv->vga_bpp = vmw_read(vmw_priv, SVGA_REG_BITS_PER_PIXEL);
d7e1958d
JB
1403 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1404 vmw_priv->vga_pitchlock =
7c4f7780 1405 vmw_read(vmw_priv, SVGA_REG_PITCHLOCK);
d7e1958d 1406 else if (vmw_fifo_have_pitchlock(vmw_priv))
7c4f7780
TH
1407 vmw_priv->vga_pitchlock = ioread32(vmw_priv->mmio_virt +
1408 SVGA_FIFO_PITCHLOCK);
1409
1410 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1411 return 0;
fb1d9738 1412
7c4f7780
TH
1413 vmw_priv->num_displays = vmw_read(vmw_priv,
1414 SVGA_REG_NUM_GUEST_DISPLAYS);
1415
029e50bf
TH
1416 if (vmw_priv->num_displays == 0)
1417 vmw_priv->num_displays = 1;
1418
7c4f7780
TH
1419 for (i = 0; i < vmw_priv->num_displays; ++i) {
1420 save = &vmw_priv->vga_save[i];
1421 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1422 save->primary = vmw_read(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY);
1423 save->pos_x = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_X);
1424 save->pos_y = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y);
1425 save->width = vmw_read(vmw_priv, SVGA_REG_DISPLAY_WIDTH);
1426 save->height = vmw_read(vmw_priv, SVGA_REG_DISPLAY_HEIGHT);
1427 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
30c78bb8
TH
1428 if (i == 0 && vmw_priv->num_displays == 1 &&
1429 save->width == 0 && save->height == 0) {
1430
1431 /*
1432 * It should be fairly safe to assume that these
1433 * values are uninitialized.
1434 */
1435
1436 save->width = vmw_priv->vga_width - save->pos_x;
1437 save->height = vmw_priv->vga_height - save->pos_y;
1438 }
7c4f7780 1439 }
30c78bb8 1440
fb1d9738
JB
1441 return 0;
1442}
1443
1444int vmw_kms_restore_vga(struct vmw_private *vmw_priv)
1445{
7c4f7780
TH
1446 struct vmw_vga_topology_state *save;
1447 uint32_t i;
1448
fb1d9738
JB
1449 vmw_write(vmw_priv, SVGA_REG_WIDTH, vmw_priv->vga_width);
1450 vmw_write(vmw_priv, SVGA_REG_HEIGHT, vmw_priv->vga_height);
7c4f7780 1451 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, vmw_priv->vga_bpp);
d7e1958d
JB
1452 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1453 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK,
1454 vmw_priv->vga_pitchlock);
1455 else if (vmw_fifo_have_pitchlock(vmw_priv))
1456 iowrite32(vmw_priv->vga_pitchlock,
1457 vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
fb1d9738 1458
7c4f7780
TH
1459 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1460 return 0;
1461
1462 for (i = 0; i < vmw_priv->num_displays; ++i) {
1463 save = &vmw_priv->vga_save[i];
1464 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1465 vmw_write(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY, save->primary);
1466 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_X, save->pos_x);
1467 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y, save->pos_y);
1468 vmw_write(vmw_priv, SVGA_REG_DISPLAY_WIDTH, save->width);
1469 vmw_write(vmw_priv, SVGA_REG_DISPLAY_HEIGHT, save->height);
1470 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
1471 }
1472
fb1d9738
JB
1473 return 0;
1474}
d8bd19d2 1475
e133e737
TH
1476bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
1477 uint32_t pitch,
1478 uint32_t height)
1479{
1480 return ((u64) pitch * (u64) height) < (u64) dev_priv->vram_size;
1481}
1482
1c482ab3
JB
1483
1484/**
1485 * Function called by DRM code called with vbl_lock held.
1486 */
7a1c2f6c
TH
1487u32 vmw_get_vblank_counter(struct drm_device *dev, int crtc)
1488{
1489 return 0;
1490}
626ab771 1491
1c482ab3
JB
1492/**
1493 * Function called by DRM code called with vbl_lock held.
1494 */
1495int vmw_enable_vblank(struct drm_device *dev, int crtc)
1496{
1497 return -ENOSYS;
1498}
1499
1500/**
1501 * Function called by DRM code called with vbl_lock held.
1502 */
1503void vmw_disable_vblank(struct drm_device *dev, int crtc)
1504{
1505}
1506
626ab771
JB
1507
1508/*
1509 * Small shared kms functions.
1510 */
1511
1512int vmw_du_update_layout(struct vmw_private *dev_priv, unsigned num,
1513 struct drm_vmw_rect *rects)
1514{
1515 struct drm_device *dev = dev_priv->dev;
1516 struct vmw_display_unit *du;
1517 struct drm_connector *con;
626ab771
JB
1518
1519 mutex_lock(&dev->mode_config.mutex);
1520
1521#if 0
6ea77d13
TH
1522 {
1523 unsigned int i;
1524
1525 DRM_INFO("%s: new layout ", __func__);
1526 for (i = 0; i < num; i++)
1527 DRM_INFO("(%i, %i %ux%u) ", rects[i].x, rects[i].y,
1528 rects[i].w, rects[i].h);
1529 DRM_INFO("\n");
1530 }
626ab771
JB
1531#endif
1532
1533 list_for_each_entry(con, &dev->mode_config.connector_list, head) {
1534 du = vmw_connector_to_du(con);
1535 if (num > du->unit) {
1536 du->pref_width = rects[du->unit].w;
1537 du->pref_height = rects[du->unit].h;
1538 du->pref_active = true;
cd2b89e7
TH
1539 du->gui_x = rects[du->unit].x;
1540 du->gui_y = rects[du->unit].y;
626ab771
JB
1541 } else {
1542 du->pref_width = 800;
1543 du->pref_height = 600;
1544 du->pref_active = false;
1545 }
1546 con->status = vmw_du_connector_detect(con, true);
1547 }
1548
1549 mutex_unlock(&dev->mode_config.mutex);
1550
1551 return 0;
1552}
1553
1554void vmw_du_crtc_save(struct drm_crtc *crtc)
1555{
1556}
1557
1558void vmw_du_crtc_restore(struct drm_crtc *crtc)
1559{
1560}
1561
1562void vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
1563 u16 *r, u16 *g, u16 *b,
1564 uint32_t start, uint32_t size)
1565{
1566 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
1567 int i;
1568
1569 for (i = 0; i < size; i++) {
1570 DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i,
1571 r[i], g[i], b[i]);
1572 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 0, r[i] >> 8);
1573 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 1, g[i] >> 8);
1574 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 2, b[i] >> 8);
1575 }
1576}
1577
1578void vmw_du_connector_dpms(struct drm_connector *connector, int mode)
1579{
1580}
1581
1582void vmw_du_connector_save(struct drm_connector *connector)
1583{
1584}
1585
1586void vmw_du_connector_restore(struct drm_connector *connector)
1587{
1588}
1589
1590enum drm_connector_status
1591vmw_du_connector_detect(struct drm_connector *connector, bool force)
1592{
1593 uint32_t num_displays;
1594 struct drm_device *dev = connector->dev;
1595 struct vmw_private *dev_priv = vmw_priv(dev);
cd2b89e7 1596 struct vmw_display_unit *du = vmw_connector_to_du(connector);
626ab771
JB
1597
1598 mutex_lock(&dev_priv->hw_mutex);
1599 num_displays = vmw_read(dev_priv, SVGA_REG_NUM_DISPLAYS);
1600 mutex_unlock(&dev_priv->hw_mutex);
1601
cd2b89e7
TH
1602 return ((vmw_connector_to_du(connector)->unit < num_displays &&
1603 du->pref_active) ?
626ab771
JB
1604 connector_status_connected : connector_status_disconnected);
1605}
1606
1607static struct drm_display_mode vmw_kms_connector_builtin[] = {
1608 /* 640x480@60Hz */
1609 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
1610 752, 800, 0, 480, 489, 492, 525, 0,
1611 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1612 /* 800x600@60Hz */
1613 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
1614 968, 1056, 0, 600, 601, 605, 628, 0,
1615 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1616 /* 1024x768@60Hz */
1617 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
1618 1184, 1344, 0, 768, 771, 777, 806, 0,
1619 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1620 /* 1152x864@75Hz */
1621 { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
1622 1344, 1600, 0, 864, 865, 868, 900, 0,
1623 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1624 /* 1280x768@60Hz */
1625 { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344,
1626 1472, 1664, 0, 768, 771, 778, 798, 0,
1627 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1628 /* 1280x800@60Hz */
1629 { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352,
1630 1480, 1680, 0, 800, 803, 809, 831, 0,
1631 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
1632 /* 1280x960@60Hz */
1633 { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376,
1634 1488, 1800, 0, 960, 961, 964, 1000, 0,
1635 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1636 /* 1280x1024@60Hz */
1637 { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328,
1638 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
1639 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1640 /* 1360x768@60Hz */
1641 { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424,
1642 1536, 1792, 0, 768, 771, 777, 795, 0,
1643 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1644 /* 1440x1050@60Hz */
1645 { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488,
1646 1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
1647 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1648 /* 1440x900@60Hz */
1649 { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520,
1650 1672, 1904, 0, 900, 903, 909, 934, 0,
1651 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1652 /* 1600x1200@60Hz */
1653 { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664,
1654 1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
1655 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1656 /* 1680x1050@60Hz */
1657 { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784,
1658 1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
1659 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1660 /* 1792x1344@60Hz */
1661 { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920,
1662 2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
1663 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1664 /* 1853x1392@60Hz */
1665 { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952,
1666 2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
1667 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1668 /* 1920x1200@60Hz */
1669 { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056,
1670 2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
1671 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1672 /* 1920x1440@60Hz */
1673 { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048,
1674 2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
1675 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1676 /* 2560x1600@60Hz */
1677 { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752,
1678 3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
1679 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1680 /* Terminate */
1681 { DRM_MODE("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) },
1682};
1683
1543b4dd
TH
1684/**
1685 * vmw_guess_mode_timing - Provide fake timings for a
1686 * 60Hz vrefresh mode.
1687 *
1688 * @mode - Pointer to a struct drm_display_mode with hdisplay and vdisplay
1689 * members filled in.
1690 */
1691static void vmw_guess_mode_timing(struct drm_display_mode *mode)
1692{
1693 mode->hsync_start = mode->hdisplay + 50;
1694 mode->hsync_end = mode->hsync_start + 50;
1695 mode->htotal = mode->hsync_end + 50;
1696
1697 mode->vsync_start = mode->vdisplay + 50;
1698 mode->vsync_end = mode->vsync_start + 50;
1699 mode->vtotal = mode->vsync_end + 50;
1700
1701 mode->clock = (u32)mode->htotal * (u32)mode->vtotal / 100 * 6;
1702 mode->vrefresh = drm_mode_vrefresh(mode);
1703}
1704
1705
626ab771
JB
1706int vmw_du_connector_fill_modes(struct drm_connector *connector,
1707 uint32_t max_width, uint32_t max_height)
1708{
1709 struct vmw_display_unit *du = vmw_connector_to_du(connector);
1710 struct drm_device *dev = connector->dev;
1711 struct vmw_private *dev_priv = vmw_priv(dev);
1712 struct drm_display_mode *mode = NULL;
1713 struct drm_display_mode *bmode;
1714 struct drm_display_mode prefmode = { DRM_MODE("preferred",
1715 DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
1716 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1717 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
1718 };
1719 int i;
1720
1721 /* Add preferred mode */
1722 {
1723 mode = drm_mode_duplicate(dev, &prefmode);
1724 if (!mode)
1725 return 0;
1726 mode->hdisplay = du->pref_width;
1727 mode->vdisplay = du->pref_height;
1543b4dd 1728 vmw_guess_mode_timing(mode);
55bde5b2 1729
626ab771
JB
1730 if (vmw_kms_validate_mode_vram(dev_priv, mode->hdisplay * 2,
1731 mode->vdisplay)) {
1732 drm_mode_probed_add(connector, mode);
55bde5b2
JB
1733 } else {
1734 drm_mode_destroy(dev, mode);
1735 mode = NULL;
1736 }
626ab771 1737
55bde5b2
JB
1738 if (du->pref_mode) {
1739 list_del_init(&du->pref_mode->head);
1740 drm_mode_destroy(dev, du->pref_mode);
626ab771 1741 }
55bde5b2
JB
1742
1743 /* mode might be null here, this is intended */
1744 du->pref_mode = mode;
626ab771
JB
1745 }
1746
1747 for (i = 0; vmw_kms_connector_builtin[i].type != 0; i++) {
1748 bmode = &vmw_kms_connector_builtin[i];
1749 if (bmode->hdisplay > max_width ||
1750 bmode->vdisplay > max_height)
1751 continue;
1752
1753 if (!vmw_kms_validate_mode_vram(dev_priv, bmode->hdisplay * 2,
1754 bmode->vdisplay))
1755 continue;
1756
1757 mode = drm_mode_duplicate(dev, bmode);
1758 if (!mode)
1759 return 0;
1760 mode->vrefresh = drm_mode_vrefresh(mode);
1761
1762 drm_mode_probed_add(connector, mode);
1763 }
1764
d41025c0
JB
1765 /* Move the prefered mode first, help apps pick the right mode. */
1766 if (du->pref_mode)
1767 list_move(&du->pref_mode->head, &connector->probed_modes);
1768
626ab771
JB
1769 drm_mode_connector_list_update(connector);
1770
1771 return 1;
1772}
1773
1774int vmw_du_connector_set_property(struct drm_connector *connector,
1775 struct drm_property *property,
1776 uint64_t val)
1777{
1778 return 0;
1779}
cd2b89e7
TH
1780
1781
1782int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data,
1783 struct drm_file *file_priv)
1784{
1785 struct vmw_private *dev_priv = vmw_priv(dev);
1786 struct drm_vmw_update_layout_arg *arg =
1787 (struct drm_vmw_update_layout_arg *)data;
1788 struct vmw_master *vmaster = vmw_master(file_priv->master);
1789 void __user *user_rects;
1790 struct drm_vmw_rect *rects;
1791 unsigned rects_size;
1792 int ret;
1793 int i;
1794 struct drm_mode_config *mode_config = &dev->mode_config;
1795
1796 ret = ttm_read_lock(&vmaster->lock, true);
1797 if (unlikely(ret != 0))
1798 return ret;
1799
1800 if (!arg->num_outputs) {
1801 struct drm_vmw_rect def_rect = {0, 0, 800, 600};
1802 vmw_du_update_layout(dev_priv, 1, &def_rect);
1803 goto out_unlock;
1804 }
1805
1806 rects_size = arg->num_outputs * sizeof(struct drm_vmw_rect);
1807 rects = kzalloc(rects_size, GFP_KERNEL);
1808 if (unlikely(!rects)) {
1809 ret = -ENOMEM;
1810 goto out_unlock;
1811 }
1812
1813 user_rects = (void __user *)(unsigned long)arg->rects;
1814 ret = copy_from_user(rects, user_rects, rects_size);
1815 if (unlikely(ret != 0)) {
1816 DRM_ERROR("Failed to get rects.\n");
1817 ret = -EFAULT;
1818 goto out_free;
1819 }
1820
1821 for (i = 0; i < arg->num_outputs; ++i) {
1822 if (rects->x < 0 ||
1823 rects->y < 0 ||
1824 rects->x + rects->w > mode_config->max_width ||
1825 rects->y + rects->h > mode_config->max_height) {
1826 DRM_ERROR("Invalid GUI layout.\n");
1827 ret = -EINVAL;
1828 goto out_free;
1829 }
1830 }
1831
1832 vmw_du_update_layout(dev_priv, arg->num_outputs, rects);
1833
1834out_free:
1835 kfree(rects);
1836out_unlock:
1837 ttm_read_unlock(&vmaster->lock);
1838 return ret;
1839}