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