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