drm/vmwgfx: Move the stdu vblank event to atomic function
[linux-2.6-block.git] / drivers / gpu / drm / vmwgfx / vmwgfx_kms.c
CommitLineData
fb1d9738
JB
1/**************************************************************************
2 *
54fbde8a 3 * Copyright © 2009-2015 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"
060e2ad5 29#include <drm/drm_plane_helper.h>
9c2542a4
SY
30#include <drm/drm_atomic.h>
31#include <drm/drm_atomic_helper.h>
060e2ad5 32#include <drm/drm_rect.h>
fb1d9738 33
56d1c78d 34
fb1d9738
JB
35/* Might need a hrtimer here? */
36#define VMWGFX_PRESENT_RATE ((HZ / 60 > 0) ? HZ / 60 : 1)
37
c8261a96 38void vmw_du_cleanup(struct vmw_display_unit *du)
fb1d9738 39{
36cc79bc
SY
40 drm_plane_cleanup(&du->primary);
41 drm_plane_cleanup(&du->cursor);
42
34ea3d38 43 drm_connector_unregister(&du->connector);
fb1d9738
JB
44 drm_crtc_cleanup(&du->crtc);
45 drm_encoder_cleanup(&du->encoder);
46 drm_connector_cleanup(&du->connector);
47}
48
49/*
50 * Display Unit Cursor functions
51 */
52
36cc79bc
SY
53static int vmw_cursor_update_image(struct vmw_private *dev_priv,
54 u32 *image, u32 width, u32 height,
55 u32 hotspotX, u32 hotspotY)
fb1d9738
JB
56{
57 struct {
58 u32 cmd;
59 SVGAFifoCmdDefineAlphaCursor cursor;
60 } *cmd;
61 u32 image_size = width * height * 4;
62 u32 cmd_size = sizeof(*cmd) + image_size;
63
64 if (!image)
65 return -EINVAL;
66
67 cmd = vmw_fifo_reserve(dev_priv, cmd_size);
68 if (unlikely(cmd == NULL)) {
69 DRM_ERROR("Fifo reserve failed.\n");
70 return -ENOMEM;
71 }
72
73 memset(cmd, 0, sizeof(*cmd));
74
75 memcpy(&cmd[1], image, image_size);
76
b9eb1a61
TH
77 cmd->cmd = SVGA_CMD_DEFINE_ALPHA_CURSOR;
78 cmd->cursor.id = 0;
79 cmd->cursor.width = width;
80 cmd->cursor.height = height;
81 cmd->cursor.hotspotX = hotspotX;
82 cmd->cursor.hotspotY = hotspotY;
fb1d9738 83
4e0858a6 84 vmw_fifo_commit_flush(dev_priv, cmd_size);
fb1d9738
JB
85
86 return 0;
87}
88
36cc79bc
SY
89static int vmw_cursor_update_dmabuf(struct vmw_private *dev_priv,
90 struct vmw_dma_buffer *dmabuf,
91 u32 width, u32 height,
92 u32 hotspotX, u32 hotspotY)
6a91d97e
JB
93{
94 struct ttm_bo_kmap_obj map;
95 unsigned long kmap_offset;
96 unsigned long kmap_num;
97 void *virtual;
98 bool dummy;
99 int ret;
100
101 kmap_offset = 0;
102 kmap_num = (width*height*4 + PAGE_SIZE - 1) >> PAGE_SHIFT;
103
dfd5e50e 104 ret = ttm_bo_reserve(&dmabuf->base, true, false, NULL);
6a91d97e
JB
105 if (unlikely(ret != 0)) {
106 DRM_ERROR("reserve failed\n");
107 return -EINVAL;
108 }
109
110 ret = ttm_bo_kmap(&dmabuf->base, kmap_offset, kmap_num, &map);
111 if (unlikely(ret != 0))
112 goto err_unreserve;
113
114 virtual = ttm_kmap_obj_virtual(&map, &dummy);
115 ret = vmw_cursor_update_image(dev_priv, virtual, width, height,
116 hotspotX, hotspotY);
117
118 ttm_bo_kunmap(&map);
119err_unreserve:
120 ttm_bo_unreserve(&dmabuf->base);
121
122 return ret;
123}
124
125
36cc79bc
SY
126static void vmw_cursor_update_position(struct vmw_private *dev_priv,
127 bool show, int x, int y)
fb1d9738 128{
b76ff5ea 129 u32 *fifo_mem = dev_priv->mmio_virt;
fb1d9738
JB
130 uint32_t count;
131
36cc79bc 132 spin_lock(&dev_priv->cursor_lock);
b76ff5ea
TH
133 vmw_mmio_write(show ? 1 : 0, fifo_mem + SVGA_FIFO_CURSOR_ON);
134 vmw_mmio_write(x, fifo_mem + SVGA_FIFO_CURSOR_X);
135 vmw_mmio_write(y, fifo_mem + SVGA_FIFO_CURSOR_Y);
136 count = vmw_mmio_read(fifo_mem + SVGA_FIFO_CURSOR_COUNT);
137 vmw_mmio_write(++count, fifo_mem + SVGA_FIFO_CURSOR_COUNT);
36cc79bc 138 spin_unlock(&dev_priv->cursor_lock);
fb1d9738
JB
139}
140
8fbf9d92 141
fb1d9738
JB
142void vmw_kms_cursor_snoop(struct vmw_surface *srf,
143 struct ttm_object_file *tfile,
144 struct ttm_buffer_object *bo,
145 SVGA3dCmdHeader *header)
146{
147 struct ttm_bo_kmap_obj map;
148 unsigned long kmap_offset;
149 unsigned long kmap_num;
150 SVGA3dCopyBox *box;
151 unsigned box_count;
152 void *virtual;
153 bool dummy;
154 struct vmw_dma_cmd {
155 SVGA3dCmdHeader header;
156 SVGA3dCmdSurfaceDMA dma;
157 } *cmd;
2ac86371 158 int i, ret;
fb1d9738
JB
159
160 cmd = container_of(header, struct vmw_dma_cmd, header);
161
162 /* No snooper installed */
163 if (!srf->snooper.image)
164 return;
165
166 if (cmd->dma.host.face != 0 || cmd->dma.host.mipmap != 0) {
167 DRM_ERROR("face and mipmap for cursors should never != 0\n");
168 return;
169 }
170
171 if (cmd->header.size < 64) {
172 DRM_ERROR("at least one full copy box must be given\n");
173 return;
174 }
175
176 box = (SVGA3dCopyBox *)&cmd[1];
177 box_count = (cmd->header.size - sizeof(SVGA3dCmdSurfaceDMA)) /
178 sizeof(SVGA3dCopyBox);
179
2ac86371 180 if (cmd->dma.guest.ptr.offset % PAGE_SIZE ||
fb1d9738
JB
181 box->x != 0 || box->y != 0 || box->z != 0 ||
182 box->srcx != 0 || box->srcy != 0 || box->srcz != 0 ||
2ac86371 183 box->d != 1 || box_count != 1) {
fb1d9738 184 /* TODO handle none page aligned offsets */
2ac86371
JB
185 /* TODO handle more dst & src != 0 */
186 /* TODO handle more then one copy */
187 DRM_ERROR("Cant snoop dma request for cursor!\n");
188 DRM_ERROR("(%u, %u, %u) (%u, %u, %u) (%ux%ux%u) %u %u\n",
189 box->srcx, box->srcy, box->srcz,
190 box->x, box->y, box->z,
191 box->w, box->h, box->d, box_count,
192 cmd->dma.guest.ptr.offset);
fb1d9738
JB
193 return;
194 }
195
196 kmap_offset = cmd->dma.guest.ptr.offset >> PAGE_SHIFT;
197 kmap_num = (64*64*4) >> PAGE_SHIFT;
198
dfd5e50e 199 ret = ttm_bo_reserve(bo, true, false, NULL);
fb1d9738
JB
200 if (unlikely(ret != 0)) {
201 DRM_ERROR("reserve failed\n");
202 return;
203 }
204
205 ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
206 if (unlikely(ret != 0))
207 goto err_unreserve;
208
209 virtual = ttm_kmap_obj_virtual(&map, &dummy);
210
2ac86371
JB
211 if (box->w == 64 && cmd->dma.guest.pitch == 64*4) {
212 memcpy(srf->snooper.image, virtual, 64*64*4);
213 } else {
214 /* Image is unsigned pointer. */
215 for (i = 0; i < box->h; i++)
216 memcpy(srf->snooper.image + i * 64,
217 virtual + i * cmd->dma.guest.pitch,
218 box->w * 4);
219 }
220
fb1d9738
JB
221 srf->snooper.age++;
222
fb1d9738
JB
223 ttm_bo_kunmap(&map);
224err_unreserve:
225 ttm_bo_unreserve(bo);
226}
227
8fbf9d92
TH
228/**
229 * vmw_kms_legacy_hotspot_clear - Clear legacy hotspots
230 *
231 * @dev_priv: Pointer to the device private struct.
232 *
233 * Clears all legacy hotspots.
234 */
235void vmw_kms_legacy_hotspot_clear(struct vmw_private *dev_priv)
236{
237 struct drm_device *dev = dev_priv->dev;
238 struct vmw_display_unit *du;
239 struct drm_crtc *crtc;
240
241 drm_modeset_lock_all(dev);
242 drm_for_each_crtc(crtc, dev) {
243 du = vmw_crtc_to_du(crtc);
244
245 du->hotspot_x = 0;
246 du->hotspot_y = 0;
247 }
248 drm_modeset_unlock_all(dev);
249}
250
fb1d9738
JB
251void vmw_kms_cursor_post_execbuf(struct vmw_private *dev_priv)
252{
253 struct drm_device *dev = dev_priv->dev;
254 struct vmw_display_unit *du;
255 struct drm_crtc *crtc;
256
257 mutex_lock(&dev->mode_config.mutex);
258
259 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
260 du = vmw_crtc_to_du(crtc);
261 if (!du->cursor_surface ||
262 du->cursor_age == du->cursor_surface->snooper.age)
263 continue;
264
265 du->cursor_age = du->cursor_surface->snooper.age;
266 vmw_cursor_update_image(dev_priv,
267 du->cursor_surface->snooper.image,
8fbf9d92
TH
268 64, 64,
269 du->hotspot_x + du->core_hotspot_x,
270 du->hotspot_y + du->core_hotspot_y);
fb1d9738
JB
271 }
272
273 mutex_unlock(&dev->mode_config.mutex);
274}
275
36cc79bc 276
36cc79bc
SY
277void vmw_du_cursor_plane_destroy(struct drm_plane *plane)
278{
279 vmw_cursor_update_position(plane->dev->dev_private, false, 0, 0);
280
281 drm_plane_cleanup(plane);
282}
283
284
285void vmw_du_primary_plane_destroy(struct drm_plane *plane)
286{
287 drm_plane_cleanup(plane);
288
289 /* Planes are static in our case so we don't free it */
290}
291
292
060e2ad5
SY
293/**
294 * vmw_du_vps_unpin_surf - unpins resource associated with a framebuffer surface
295 *
296 * @vps: plane state associated with the display surface
297 * @unreference: true if we also want to unreference the display.
298 */
299void vmw_du_plane_unpin_surf(struct vmw_plane_state *vps,
300 bool unreference)
301{
302 if (vps->surf) {
303 if (vps->pinned) {
304 vmw_resource_unpin(&vps->surf->res);
305 vps->pinned--;
306 }
307
308 if (unreference) {
309 if (vps->pinned)
310 DRM_ERROR("Surface still pinned\n");
311 vmw_surface_unreference(&vps->surf);
312 }
313 }
314}
315
316
317/**
318 * vmw_du_plane_cleanup_fb - Unpins the cursor
319 *
320 * @plane: display plane
321 * @old_state: Contains the FB to clean up
322 *
323 * Unpins the framebuffer surface
324 *
325 * Returns 0 on success
326 */
327void
328vmw_du_plane_cleanup_fb(struct drm_plane *plane,
329 struct drm_plane_state *old_state)
330{
331 struct vmw_plane_state *vps = vmw_plane_state_to_vps(old_state);
332
333 vmw_du_plane_unpin_surf(vps, false);
334}
335
336
337/**
338 * vmw_du_cursor_plane_prepare_fb - Readies the cursor by referencing it
339 *
340 * @plane: display plane
341 * @new_state: info on the new plane state, including the FB
342 *
343 * Returns 0 on success
344 */
345int
346vmw_du_cursor_plane_prepare_fb(struct drm_plane *plane,
347 struct drm_plane_state *new_state)
348{
349 struct drm_framebuffer *fb = new_state->fb;
350 struct vmw_plane_state *vps = vmw_plane_state_to_vps(new_state);
351
352
353 if (vps->surf)
354 vmw_surface_unreference(&vps->surf);
355
356 if (vps->dmabuf)
357 vmw_dmabuf_unreference(&vps->dmabuf);
358
359 if (fb) {
360 if (vmw_framebuffer_to_vfb(fb)->dmabuf) {
361 vps->dmabuf = vmw_framebuffer_to_vfbd(fb)->buffer;
362 vmw_dmabuf_reference(vps->dmabuf);
363 } else {
364 vps->surf = vmw_framebuffer_to_vfbs(fb)->surface;
365 vmw_surface_reference(vps->surf);
366 }
367 }
368
369 return 0;
370}
371
372
060e2ad5
SY
373void
374vmw_du_cursor_plane_atomic_update(struct drm_plane *plane,
375 struct drm_plane_state *old_state)
376{
377 struct drm_crtc *crtc = plane->state->crtc ?: old_state->crtc;
378 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
379 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
380 struct vmw_plane_state *vps = vmw_plane_state_to_vps(plane->state);
381 s32 hotspot_x, hotspot_y;
382 int ret = 0;
383
384
385 hotspot_x = du->hotspot_x;
386 hotspot_y = du->hotspot_y;
14979adb
SY
387
388 if (plane->fb) {
389 hotspot_x += plane->fb->hot_x;
390 hotspot_y += plane->fb->hot_y;
391 }
392
060e2ad5
SY
393 du->cursor_surface = vps->surf;
394 du->cursor_dmabuf = vps->dmabuf;
395
396 /* setup new image */
397 if (vps->surf) {
398 du->cursor_age = du->cursor_surface->snooper.age;
399
400 ret = vmw_cursor_update_image(dev_priv,
401 vps->surf->snooper.image,
402 64, 64, hotspot_x, hotspot_y);
403 } else if (vps->dmabuf) {
404 ret = vmw_cursor_update_dmabuf(dev_priv, vps->dmabuf,
405 plane->state->crtc_w,
406 plane->state->crtc_h,
407 hotspot_x, hotspot_y);
408 } else {
409 vmw_cursor_update_position(dev_priv, false, 0, 0);
410 return;
411 }
412
413 if (!ret) {
414 du->cursor_x = plane->state->crtc_x + du->set_gui_x;
415 du->cursor_y = plane->state->crtc_y + du->set_gui_y;
416
417 vmw_cursor_update_position(dev_priv, true,
418 du->cursor_x + hotspot_x,
419 du->cursor_y + hotspot_y);
14979adb
SY
420
421 du->core_hotspot_x = hotspot_x - du->hotspot_x;
422 du->core_hotspot_y = hotspot_y - du->hotspot_y;
060e2ad5
SY
423 } else {
424 DRM_ERROR("Failed to update cursor image\n");
425 }
426}
427
428
429/**
430 * vmw_du_primary_plane_atomic_check - check if the new state is okay
431 *
432 * @plane: display plane
433 * @state: info on the new plane state, including the FB
434 *
435 * Check if the new state is settable given the current state. Other
436 * than what the atomic helper checks, we care about crtc fitting
437 * the FB and maintaining one active framebuffer.
438 *
439 * Returns 0 on success
440 */
441int vmw_du_primary_plane_atomic_check(struct drm_plane *plane,
442 struct drm_plane_state *state)
443{
58a275aa 444 struct drm_crtc_state *crtc_state = NULL;
060e2ad5 445 struct drm_framebuffer *new_fb = state->fb;
060e2ad5
SY
446 int ret;
447
58a275aa
VS
448 if (state->crtc)
449 crtc_state = drm_atomic_get_new_crtc_state(state->state, state->crtc);
060e2ad5 450
81af63a4 451 ret = drm_atomic_helper_check_plane_state(state, crtc_state,
a01cb8ba
VS
452 DRM_PLANE_HELPER_NO_SCALING,
453 DRM_PLANE_HELPER_NO_SCALING,
454 false, true);
060e2ad5
SY
455
456 if (!ret && new_fb) {
457 struct drm_crtc *crtc = state->crtc;
458 struct vmw_connector_state *vcs;
459 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
460 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
461 struct vmw_framebuffer *vfb = vmw_framebuffer_to_vfb(new_fb);
462
463 vcs = vmw_connector_state_to_vcs(du->connector.state);
464
060e2ad5
SY
465 /* Only one active implicit framebuffer at a time. */
466 mutex_lock(&dev_priv->global_kms_state_mutex);
467 if (vcs->is_implicit && dev_priv->implicit_fb &&
468 !(dev_priv->num_implicit == 1 && du->active_implicit)
469 && dev_priv->implicit_fb != vfb) {
470 DRM_ERROR("Multiple implicit framebuffers "
471 "not supported.\n");
472 ret = -EINVAL;
473 }
474 mutex_unlock(&dev_priv->global_kms_state_mutex);
475 }
476
477
478 return ret;
479}
480
481
482/**
483 * vmw_du_cursor_plane_atomic_check - check if the new state is okay
484 *
485 * @plane: cursor plane
486 * @state: info on the new plane state
487 *
488 * This is a chance to fail if the new cursor state does not fit
489 * our requirements.
490 *
491 * Returns 0 on success
492 */
493int vmw_du_cursor_plane_atomic_check(struct drm_plane *plane,
494 struct drm_plane_state *new_state)
495{
496 int ret = 0;
497 struct vmw_surface *surface = NULL;
498 struct drm_framebuffer *fb = new_state->fb;
499
500
501 /* Turning off */
502 if (!fb)
503 return ret;
504
505 /* A lot of the code assumes this */
506 if (new_state->crtc_w != 64 || new_state->crtc_h != 64) {
507 DRM_ERROR("Invalid cursor dimensions (%d, %d)\n",
508 new_state->crtc_w, new_state->crtc_h);
509 ret = -EINVAL;
510 }
511
512 if (!vmw_framebuffer_to_vfb(fb)->dmabuf)
513 surface = vmw_framebuffer_to_vfbs(fb)->surface;
514
515 if (surface && !surface->snooper.image) {
516 DRM_ERROR("surface not suitable for cursor\n");
517 ret = -EINVAL;
518 }
519
520 return ret;
521}
522
523
06ec4190
SY
524int vmw_du_crtc_atomic_check(struct drm_crtc *crtc,
525 struct drm_crtc_state *new_state)
526{
527 struct vmw_display_unit *du = vmw_crtc_to_du(new_state->crtc);
528 int connector_mask = 1 << drm_connector_index(&du->connector);
529 bool has_primary = new_state->plane_mask &
530 BIT(drm_plane_index(crtc->primary));
531
532 /* We always want to have an active plane with an active CRTC */
533 if (has_primary != new_state->enable)
534 return -EINVAL;
535
536
537 if (new_state->connector_mask != connector_mask &&
538 new_state->connector_mask != 0) {
539 DRM_ERROR("Invalid connectors configuration\n");
540 return -EINVAL;
541 }
542
543 /*
544 * Our virtual device does not have a dot clock, so use the logical
545 * clock value as the dot clock.
546 */
547 if (new_state->mode.crtc_clock == 0)
548 new_state->adjusted_mode.crtc_clock = new_state->mode.clock;
549
550 return 0;
551}
552
553
554void vmw_du_crtc_atomic_begin(struct drm_crtc *crtc,
555 struct drm_crtc_state *old_crtc_state)
556{
557}
558
559
560void vmw_du_crtc_atomic_flush(struct drm_crtc *crtc,
561 struct drm_crtc_state *old_crtc_state)
562{
563 struct drm_pending_vblank_event *event = crtc->state->event;
564
565 if (event) {
566 crtc->state->event = NULL;
567
568 spin_lock_irq(&crtc->dev->event_lock);
3cbe87fc 569 drm_crtc_send_vblank_event(crtc, event);
06ec4190
SY
570 spin_unlock_irq(&crtc->dev->event_lock);
571 }
06ec4190
SY
572}
573
574
9c2542a4
SY
575/**
576 * vmw_du_crtc_duplicate_state - duplicate crtc state
577 * @crtc: DRM crtc
578 *
579 * Allocates and returns a copy of the crtc state (both common and
580 * vmw-specific) for the specified crtc.
581 *
582 * Returns: The newly allocated crtc state, or NULL on failure.
583 */
584struct drm_crtc_state *
585vmw_du_crtc_duplicate_state(struct drm_crtc *crtc)
586{
587 struct drm_crtc_state *state;
588 struct vmw_crtc_state *vcs;
589
590 if (WARN_ON(!crtc->state))
591 return NULL;
592
593 vcs = kmemdup(crtc->state, sizeof(*vcs), GFP_KERNEL);
594
595 if (!vcs)
596 return NULL;
597
598 state = &vcs->base;
599
600 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
601
602 return state;
603}
604
605
606/**
607 * vmw_du_crtc_reset - creates a blank vmw crtc state
608 * @crtc: DRM crtc
609 *
610 * Resets the atomic state for @crtc by freeing the state pointer (which
611 * might be NULL, e.g. at driver load time) and allocating a new empty state
612 * object.
613 */
614void vmw_du_crtc_reset(struct drm_crtc *crtc)
615{
616 struct vmw_crtc_state *vcs;
617
618
619 if (crtc->state) {
620 __drm_atomic_helper_crtc_destroy_state(crtc->state);
621
622 kfree(vmw_crtc_state_to_vcs(crtc->state));
623 }
624
625 vcs = kzalloc(sizeof(*vcs), GFP_KERNEL);
626
627 if (!vcs) {
628 DRM_ERROR("Cannot allocate vmw_crtc_state\n");
629 return;
630 }
631
632 crtc->state = &vcs->base;
633 crtc->state->crtc = crtc;
634}
635
636
637/**
638 * vmw_du_crtc_destroy_state - destroy crtc state
639 * @crtc: DRM crtc
640 * @state: state object to destroy
641 *
642 * Destroys the crtc state (both common and vmw-specific) for the
643 * specified plane.
644 */
645void
646vmw_du_crtc_destroy_state(struct drm_crtc *crtc,
647 struct drm_crtc_state *state)
648{
649 drm_atomic_helper_crtc_destroy_state(crtc, state);
650}
651
652
cc5ec459
SY
653/**
654 * vmw_du_plane_duplicate_state - duplicate plane state
655 * @plane: drm plane
656 *
657 * Allocates and returns a copy of the plane state (both common and
658 * vmw-specific) for the specified plane.
659 *
660 * Returns: The newly allocated plane state, or NULL on failure.
661 */
662struct drm_plane_state *
663vmw_du_plane_duplicate_state(struct drm_plane *plane)
664{
665 struct drm_plane_state *state;
666 struct vmw_plane_state *vps;
667
668 vps = kmemdup(plane->state, sizeof(*vps), GFP_KERNEL);
669
670 if (!vps)
671 return NULL;
672
673 vps->pinned = 0;
674
810b3e16 675 /* Mapping is managed by prepare_fb/cleanup_fb */
810b3e16
SY
676 memset(&vps->host_map, 0, sizeof(vps->host_map));
677 vps->cpp = 0;
678
cc5ec459
SY
679 /* Each ref counted resource needs to be acquired again */
680 if (vps->surf)
681 (void) vmw_surface_reference(vps->surf);
682
683 if (vps->dmabuf)
684 (void) vmw_dmabuf_reference(vps->dmabuf);
685
686 state = &vps->base;
687
688 __drm_atomic_helper_plane_duplicate_state(plane, state);
689
690 return state;
691}
692
693
694/**
695 * vmw_du_plane_reset - creates a blank vmw plane state
696 * @plane: drm plane
697 *
698 * Resets the atomic state for @plane by freeing the state pointer (which might
699 * be NULL, e.g. at driver load time) and allocating a new empty state object.
700 */
701void vmw_du_plane_reset(struct drm_plane *plane)
702{
703 struct vmw_plane_state *vps;
704
705
706 if (plane->state)
707 vmw_du_plane_destroy_state(plane, plane->state);
708
709 vps = kzalloc(sizeof(*vps), GFP_KERNEL);
710
711 if (!vps) {
712 DRM_ERROR("Cannot allocate vmw_plane_state\n");
713 return;
714 }
715
716 plane->state = &vps->base;
717 plane->state->plane = plane;
c2c446ad 718 plane->state->rotation = DRM_MODE_ROTATE_0;
cc5ec459
SY
719}
720
721
722/**
723 * vmw_du_plane_destroy_state - destroy plane state
724 * @plane: DRM plane
725 * @state: state object to destroy
726 *
727 * Destroys the plane state (both common and vmw-specific) for the
728 * specified plane.
729 */
730void
731vmw_du_plane_destroy_state(struct drm_plane *plane,
732 struct drm_plane_state *state)
733{
734 struct vmw_plane_state *vps = vmw_plane_state_to_vps(state);
735
736
810b3e16 737 /* Should have been freed by cleanup_fb */
810b3e16
SY
738 if (vps->host_map.virtual) {
739 DRM_ERROR("Host mapping not freed\n");
740 ttm_bo_kunmap(&vps->host_map);
741 }
742
cc5ec459
SY
743 if (vps->surf)
744 vmw_surface_unreference(&vps->surf);
745
746 if (vps->dmabuf)
747 vmw_dmabuf_unreference(&vps->dmabuf);
748
749 drm_atomic_helper_plane_destroy_state(plane, state);
750}
751
752
d7721ca7
SY
753/**
754 * vmw_du_connector_duplicate_state - duplicate connector state
755 * @connector: DRM connector
756 *
757 * Allocates and returns a copy of the connector state (both common and
758 * vmw-specific) for the specified connector.
759 *
760 * Returns: The newly allocated connector state, or NULL on failure.
761 */
762struct drm_connector_state *
763vmw_du_connector_duplicate_state(struct drm_connector *connector)
764{
765 struct drm_connector_state *state;
766 struct vmw_connector_state *vcs;
767
768 if (WARN_ON(!connector->state))
769 return NULL;
770
771 vcs = kmemdup(connector->state, sizeof(*vcs), GFP_KERNEL);
772
773 if (!vcs)
774 return NULL;
775
776 state = &vcs->base;
777
778 __drm_atomic_helper_connector_duplicate_state(connector, state);
779
780 return state;
781}
782
783
784/**
785 * vmw_du_connector_reset - creates a blank vmw connector state
786 * @connector: DRM connector
787 *
788 * Resets the atomic state for @connector by freeing the state pointer (which
789 * might be NULL, e.g. at driver load time) and allocating a new empty state
790 * object.
791 */
792void vmw_du_connector_reset(struct drm_connector *connector)
793{
794 struct vmw_connector_state *vcs;
795
796
797 if (connector->state) {
798 __drm_atomic_helper_connector_destroy_state(connector->state);
799
800 kfree(vmw_connector_state_to_vcs(connector->state));
801 }
802
803 vcs = kzalloc(sizeof(*vcs), GFP_KERNEL);
804
805 if (!vcs) {
806 DRM_ERROR("Cannot allocate vmw_connector_state\n");
807 return;
808 }
809
810 __drm_atomic_helper_connector_reset(connector, &vcs->base);
811}
812
813
814/**
815 * vmw_du_connector_destroy_state - destroy connector state
816 * @connector: DRM connector
817 * @state: state object to destroy
818 *
819 * Destroys the connector state (both common and vmw-specific) for the
820 * specified plane.
821 */
822void
823vmw_du_connector_destroy_state(struct drm_connector *connector,
824 struct drm_connector_state *state)
825{
826 drm_atomic_helper_connector_destroy_state(connector, state);
827}
fb1d9738
JB
828/*
829 * Generic framebuffer code
830 */
831
fb1d9738
JB
832/*
833 * Surface framebuffer code
834 */
835
847c5964 836static void vmw_framebuffer_surface_destroy(struct drm_framebuffer *framebuffer)
fb1d9738 837{
3a939a5e 838 struct vmw_framebuffer_surface *vfbs =
fb1d9738 839 vmw_framebuffer_to_vfbs(framebuffer);
3a939a5e 840
fb1d9738 841 drm_framebuffer_cleanup(framebuffer);
3a939a5e 842 vmw_surface_unreference(&vfbs->surface);
a278724a
TH
843 if (vfbs->base.user_obj)
844 ttm_base_object_unref(&vfbs->base.user_obj);
fb1d9738 845
3a939a5e 846 kfree(vfbs);
fb1d9738
JB
847}
848
847c5964 849static int vmw_framebuffer_surface_dirty(struct drm_framebuffer *framebuffer,
02b00162 850 struct drm_file *file_priv,
fb1d9738
JB
851 unsigned flags, unsigned color,
852 struct drm_clip_rect *clips,
853 unsigned num_clips)
854{
855 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
856 struct vmw_framebuffer_surface *vfbs =
857 vmw_framebuffer_to_vfbs(framebuffer);
fb1d9738 858 struct drm_clip_rect norect;
5deb65cf 859 int ret, inc = 1;
fb1d9738 860
c8261a96
SY
861 /* Legacy Display Unit does not support 3D */
862 if (dev_priv->active_display_unit == vmw_du_legacy)
01e81419
JB
863 return -EINVAL;
864
73e9efd4
VS
865 drm_modeset_lock_all(dev_priv->dev);
866
294adf7d 867 ret = ttm_read_lock(&dev_priv->reservation_sem, true);
73e9efd4
VS
868 if (unlikely(ret != 0)) {
869 drm_modeset_unlock_all(dev_priv->dev);
3a939a5e 870 return ret;
73e9efd4 871 }
3a939a5e 872
fb1d9738
JB
873 if (!num_clips) {
874 num_clips = 1;
875 clips = &norect;
876 norect.x1 = norect.y1 = 0;
877 norect.x2 = framebuffer->width;
878 norect.y2 = framebuffer->height;
879 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
880 num_clips /= 2;
881 inc = 2; /* skip source rects */
882 }
883
c8261a96 884 if (dev_priv->active_display_unit == vmw_du_screen_object)
10b1e0ca
TH
885 ret = vmw_kms_sou_do_surface_dirty(dev_priv, &vfbs->base,
886 clips, NULL, NULL, 0, 0,
91e9f352 887 num_clips, inc, NULL, NULL);
35c05125 888 else
6bf6bf03
TH
889 ret = vmw_kms_stdu_surface_dirty(dev_priv, &vfbs->base,
890 clips, NULL, NULL, 0, 0,
91e9f352 891 num_clips, inc, NULL, NULL);
fb1d9738 892
3eab3d9e 893 vmw_fifo_flush(dev_priv, false);
294adf7d 894 ttm_read_unlock(&dev_priv->reservation_sem);
73e9efd4
VS
895
896 drm_modeset_unlock_all(dev_priv->dev);
897
fb1d9738
JB
898 return 0;
899}
900
10b1e0ca
TH
901/**
902 * vmw_kms_readback - Perform a readback from the screen system to
903 * a dma-buffer backed framebuffer.
904 *
905 * @dev_priv: Pointer to the device private structure.
906 * @file_priv: Pointer to a struct drm_file identifying the caller.
907 * Must be set to NULL if @user_fence_rep is NULL.
908 * @vfb: Pointer to the dma-buffer backed framebuffer.
909 * @user_fence_rep: User-space provided structure for fence information.
910 * Must be set to non-NULL if @file_priv is non-NULL.
911 * @vclips: Array of clip rects.
912 * @num_clips: Number of clip rects in @vclips.
913 *
914 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
915 * interrupted.
916 */
917int vmw_kms_readback(struct vmw_private *dev_priv,
918 struct drm_file *file_priv,
919 struct vmw_framebuffer *vfb,
920 struct drm_vmw_fence_rep __user *user_fence_rep,
921 struct drm_vmw_rect *vclips,
922 uint32_t num_clips)
923{
924 switch (dev_priv->active_display_unit) {
925 case vmw_du_screen_object:
926 return vmw_kms_sou_readback(dev_priv, file_priv, vfb,
91e9f352
DR
927 user_fence_rep, vclips, num_clips,
928 NULL);
6bf6bf03
TH
929 case vmw_du_screen_target:
930 return vmw_kms_stdu_dma(dev_priv, file_priv, vfb,
931 user_fence_rep, NULL, vclips, num_clips,
91e9f352 932 1, false, true, NULL);
10b1e0ca
TH
933 default:
934 WARN_ONCE(true,
935 "Readback called with invalid display system.\n");
6bf6bf03 936}
10b1e0ca
TH
937
938 return -ENOSYS;
939}
940
941
d7955fcf 942static const struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs = {
fb1d9738
JB
943 .destroy = vmw_framebuffer_surface_destroy,
944 .dirty = vmw_framebuffer_surface_dirty,
fb1d9738
JB
945};
946
d3216a0c
TH
947static int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv,
948 struct vmw_surface *surface,
949 struct vmw_framebuffer **out,
dabdcdc9 950 const struct drm_mode_fb_cmd2
f89c6c32
SY
951 *mode_cmd,
952 bool is_dmabuf_proxy)
fb1d9738
JB
953
954{
955 struct drm_device *dev = dev_priv->dev;
956 struct vmw_framebuffer_surface *vfbs;
d3216a0c 957 enum SVGA3dSurfaceFormat format;
fb1d9738 958 int ret;
dabdcdc9 959 struct drm_format_name_buf format_name;
fb1d9738 960
c8261a96
SY
961 /* 3D is only supported on HWv8 and newer hosts */
962 if (dev_priv->active_display_unit == vmw_du_legacy)
01e81419
JB
963 return -ENOSYS;
964
d3216a0c
TH
965 /*
966 * Sanity checks.
967 */
968
e7ac9211
JB
969 /* Surface must be marked as a scanout. */
970 if (unlikely(!surface->scanout))
971 return -EINVAL;
972
d3216a0c
TH
973 if (unlikely(surface->mip_levels[0] != 1 ||
974 surface->num_sizes != 1 ||
b360a3ce
TH
975 surface->base_size.width < mode_cmd->width ||
976 surface->base_size.height < mode_cmd->height ||
977 surface->base_size.depth != 1)) {
d3216a0c
TH
978 DRM_ERROR("Incompatible surface dimensions "
979 "for requested mode.\n");
980 return -EINVAL;
981 }
982
dabdcdc9
DV
983 switch (mode_cmd->pixel_format) {
984 case DRM_FORMAT_ARGB8888:
d3216a0c
TH
985 format = SVGA3D_A8R8G8B8;
986 break;
dabdcdc9 987 case DRM_FORMAT_XRGB8888:
d3216a0c
TH
988 format = SVGA3D_X8R8G8B8;
989 break;
dabdcdc9 990 case DRM_FORMAT_RGB565:
d3216a0c
TH
991 format = SVGA3D_R5G6B5;
992 break;
dabdcdc9 993 case DRM_FORMAT_XRGB1555:
d3216a0c
TH
994 format = SVGA3D_A1R5G5B5;
995 break;
996 default:
dabdcdc9
DV
997 DRM_ERROR("Invalid pixel format: %s\n",
998 drm_get_format_name(mode_cmd->pixel_format, &format_name));
d3216a0c
TH
999 return -EINVAL;
1000 }
1001
d80efd5c
TH
1002 /*
1003 * For DX, surface format validation is done when surface->scanout
1004 * is set.
1005 */
1006 if (!dev_priv->has_dx && format != surface->format) {
d3216a0c
TH
1007 DRM_ERROR("Invalid surface format for requested mode.\n");
1008 return -EINVAL;
1009 }
1010
fb1d9738
JB
1011 vfbs = kzalloc(sizeof(*vfbs), GFP_KERNEL);
1012 if (!vfbs) {
1013 ret = -ENOMEM;
1014 goto out_err1;
1015 }
1016
a3f913ca 1017 drm_helper_mode_fill_fb_struct(dev, &vfbs->base.base, mode_cmd);
05c95018 1018 vfbs->surface = vmw_surface_reference(surface);
dabdcdc9 1019 vfbs->base.user_handle = mode_cmd->handles[0];
f89c6c32 1020 vfbs->is_dmabuf_proxy = is_dmabuf_proxy;
3a939a5e 1021
fb1d9738
JB
1022 *out = &vfbs->base;
1023
80f0b5af
DV
1024 ret = drm_framebuffer_init(dev, &vfbs->base.base,
1025 &vmw_framebuffer_surface_funcs);
1026 if (ret)
05c95018 1027 goto out_err2;
80f0b5af 1028
fb1d9738
JB
1029 return 0;
1030
fb1d9738 1031out_err2:
05c95018 1032 vmw_surface_unreference(&surface);
fb1d9738
JB
1033 kfree(vfbs);
1034out_err1:
1035 return ret;
1036}
1037
1038/*
1039 * Dmabuf framebuffer code
1040 */
1041
847c5964 1042static void vmw_framebuffer_dmabuf_destroy(struct drm_framebuffer *framebuffer)
fb1d9738
JB
1043{
1044 struct vmw_framebuffer_dmabuf *vfbd =
1045 vmw_framebuffer_to_vfbd(framebuffer);
1046
1047 drm_framebuffer_cleanup(framebuffer);
1048 vmw_dmabuf_unreference(&vfbd->buffer);
a278724a
TH
1049 if (vfbd->base.user_obj)
1050 ttm_base_object_unref(&vfbd->base.user_obj);
fb1d9738
JB
1051
1052 kfree(vfbd);
1053}
1054
847c5964 1055static int vmw_framebuffer_dmabuf_dirty(struct drm_framebuffer *framebuffer,
02b00162 1056 struct drm_file *file_priv,
fb1d9738
JB
1057 unsigned flags, unsigned color,
1058 struct drm_clip_rect *clips,
1059 unsigned num_clips)
1060{
1061 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
5deb65cf
JB
1062 struct vmw_framebuffer_dmabuf *vfbd =
1063 vmw_framebuffer_to_vfbd(framebuffer);
fb1d9738 1064 struct drm_clip_rect norect;
5deb65cf 1065 int ret, increment = 1;
fb1d9738 1066
73e9efd4
VS
1067 drm_modeset_lock_all(dev_priv->dev);
1068
294adf7d 1069 ret = ttm_read_lock(&dev_priv->reservation_sem, true);
73e9efd4
VS
1070 if (unlikely(ret != 0)) {
1071 drm_modeset_unlock_all(dev_priv->dev);
3a939a5e 1072 return ret;
73e9efd4 1073 }
3a939a5e 1074
df1c93ba 1075 if (!num_clips) {
fb1d9738
JB
1076 num_clips = 1;
1077 clips = &norect;
1078 norect.x1 = norect.y1 = 0;
1079 norect.x2 = framebuffer->width;
1080 norect.y2 = framebuffer->height;
1081 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
1082 num_clips /= 2;
1083 increment = 2;
1084 }
1085
6bf6bf03
TH
1086 switch (dev_priv->active_display_unit) {
1087 case vmw_du_screen_target:
1088 ret = vmw_kms_stdu_dma(dev_priv, NULL, &vfbd->base, NULL,
1089 clips, NULL, num_clips, increment,
91e9f352 1090 true, true, NULL);
6bf6bf03
TH
1091 break;
1092 case vmw_du_screen_object:
10b1e0ca 1093 ret = vmw_kms_sou_do_dmabuf_dirty(dev_priv, &vfbd->base,
897b8180 1094 clips, NULL, num_clips,
91e9f352 1095 increment, true, NULL, NULL);
6bf6bf03 1096 break;
352b20dc
TH
1097 case vmw_du_legacy:
1098 ret = vmw_kms_ldu_do_dmabuf_dirty(dev_priv, &vfbd->base, 0, 0,
1099 clips, num_clips, increment);
1100 break;
6bf6bf03 1101 default:
352b20dc
TH
1102 ret = -EINVAL;
1103 WARN_ONCE(true, "Dirty called with invalid display system.\n");
6bf6bf03 1104 break;
56d1c78d 1105 }
fb1d9738 1106
3eab3d9e 1107 vmw_fifo_flush(dev_priv, false);
294adf7d 1108 ttm_read_unlock(&dev_priv->reservation_sem);
73e9efd4
VS
1109
1110 drm_modeset_unlock_all(dev_priv->dev);
1111
5deb65cf 1112 return ret;
fb1d9738
JB
1113}
1114
d7955fcf 1115static const struct drm_framebuffer_funcs vmw_framebuffer_dmabuf_funcs = {
fb1d9738
JB
1116 .destroy = vmw_framebuffer_dmabuf_destroy,
1117 .dirty = vmw_framebuffer_dmabuf_dirty,
fb1d9738
JB
1118};
1119
497a3ff9
JB
1120/**
1121 * Pin the dmabuffer to the start of vram.
1122 */
fd006a43 1123static int vmw_framebuffer_pin(struct vmw_framebuffer *vfb)
fb1d9738
JB
1124{
1125 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
fd006a43 1126 struct vmw_dma_buffer *buf;
fb1d9738
JB
1127 int ret;
1128
fd006a43
TH
1129 buf = vfb->dmabuf ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
1130 vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
fb1d9738 1131
fd006a43
TH
1132 if (!buf)
1133 return 0;
fb1d9738 1134
fd006a43
TH
1135 switch (dev_priv->active_display_unit) {
1136 case vmw_du_legacy:
1137 vmw_overlay_pause_all(dev_priv);
1138 ret = vmw_dmabuf_pin_in_start_of_vram(dev_priv, buf, false);
1139 vmw_overlay_resume_all(dev_priv);
1140 break;
1141 case vmw_du_screen_object:
1142 case vmw_du_screen_target:
1143 if (vfb->dmabuf)
1144 return vmw_dmabuf_pin_in_vram_or_gmr(dev_priv, buf,
1145 false);
fb1d9738 1146
fd006a43
TH
1147 return vmw_dmabuf_pin_in_placement(dev_priv, buf,
1148 &vmw_mob_placement, false);
1149 default:
1150 return -EINVAL;
1151 }
316ab13a 1152
fd006a43 1153 return ret;
fb1d9738
JB
1154}
1155
fd006a43 1156static int vmw_framebuffer_unpin(struct vmw_framebuffer *vfb)
fb1d9738
JB
1157{
1158 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
fd006a43 1159 struct vmw_dma_buffer *buf;
fb1d9738 1160
fd006a43
TH
1161 buf = vfb->dmabuf ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
1162 vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
fb1d9738 1163
fd006a43 1164 if (WARN_ON(!buf))
fb1d9738 1165 return 0;
fb1d9738 1166
fd006a43 1167 return vmw_dmabuf_unpin(dev_priv, buf, false);
fb1d9738
JB
1168}
1169
f89c6c32
SY
1170/**
1171 * vmw_create_dmabuf_proxy - create a proxy surface for the DMA buf
1172 *
1173 * @dev: DRM device
1174 * @mode_cmd: parameters for the new surface
1175 * @dmabuf_mob: MOB backing the DMA buf
1176 * @srf_out: newly created surface
1177 *
1178 * When the content FB is a DMA buf, we create a surface as a proxy to the
1179 * same buffer. This way we can do a surface copy rather than a surface DMA.
1180 * This is a more efficient approach
1181 *
1182 * RETURNS:
1183 * 0 on success, error code otherwise
1184 */
1185static int vmw_create_dmabuf_proxy(struct drm_device *dev,
dabdcdc9 1186 const struct drm_mode_fb_cmd2 *mode_cmd,
f89c6c32
SY
1187 struct vmw_dma_buffer *dmabuf_mob,
1188 struct vmw_surface **srf_out)
1189{
1190 uint32_t format;
8cd9f251 1191 struct drm_vmw_size content_base_size = {0};
6bf6bf03 1192 struct vmw_resource *res;
a50e2bf5 1193 unsigned int bytes_pp;
dabdcdc9 1194 struct drm_format_name_buf format_name;
f89c6c32
SY
1195 int ret;
1196
dabdcdc9
DV
1197 switch (mode_cmd->pixel_format) {
1198 case DRM_FORMAT_ARGB8888:
1199 case DRM_FORMAT_XRGB8888:
f89c6c32 1200 format = SVGA3D_X8R8G8B8;
a50e2bf5 1201 bytes_pp = 4;
f89c6c32
SY
1202 break;
1203
dabdcdc9
DV
1204 case DRM_FORMAT_RGB565:
1205 case DRM_FORMAT_XRGB1555:
f89c6c32 1206 format = SVGA3D_R5G6B5;
a50e2bf5 1207 bytes_pp = 2;
f89c6c32
SY
1208 break;
1209
1210 case 8:
1211 format = SVGA3D_P8;
a50e2bf5 1212 bytes_pp = 1;
f89c6c32
SY
1213 break;
1214
1215 default:
dabdcdc9
DV
1216 DRM_ERROR("Invalid framebuffer format %s\n",
1217 drm_get_format_name(mode_cmd->pixel_format, &format_name));
f89c6c32
SY
1218 return -EINVAL;
1219 }
1220
dabdcdc9 1221 content_base_size.width = mode_cmd->pitches[0] / bytes_pp;
f89c6c32
SY
1222 content_base_size.height = mode_cmd->height;
1223 content_base_size.depth = 1;
1224
1225 ret = vmw_surface_gb_priv_define(dev,
1226 0, /* kernel visible only */
1227 0, /* flags */
1228 format,
1229 true, /* can be a scanout buffer */
1230 1, /* num of mip levels */
1231 0,
d80efd5c 1232 0,
f89c6c32
SY
1233 content_base_size,
1234 srf_out);
1235 if (ret) {
1236 DRM_ERROR("Failed to allocate proxy content buffer\n");
1237 return ret;
fb1d9738
JB
1238 }
1239
6bf6bf03 1240 res = &(*srf_out)->res;
f89c6c32 1241
6bf6bf03
TH
1242 /* Reserve and switch the backing mob. */
1243 mutex_lock(&res->dev_priv->cmdbuf_mutex);
1244 (void) vmw_resource_reserve(res, false, true);
1245 vmw_dmabuf_unreference(&res->backup);
1246 res->backup = vmw_dmabuf_reference(dmabuf_mob);
1247 res->backup_offset = 0;
d80efd5c 1248 vmw_resource_unreserve(res, false, NULL, 0);
6bf6bf03 1249 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
f89c6c32 1250
6bf6bf03 1251 return 0;
fb1d9738
JB
1252}
1253
f89c6c32
SY
1254
1255
d3216a0c
TH
1256static int vmw_kms_new_framebuffer_dmabuf(struct vmw_private *dev_priv,
1257 struct vmw_dma_buffer *dmabuf,
1258 struct vmw_framebuffer **out,
dabdcdc9 1259 const struct drm_mode_fb_cmd2
d3216a0c 1260 *mode_cmd)
fb1d9738
JB
1261
1262{
1263 struct drm_device *dev = dev_priv->dev;
1264 struct vmw_framebuffer_dmabuf *vfbd;
d3216a0c 1265 unsigned int requested_size;
dabdcdc9 1266 struct drm_format_name_buf format_name;
fb1d9738
JB
1267 int ret;
1268
dabdcdc9 1269 requested_size = mode_cmd->height * mode_cmd->pitches[0];
d3216a0c
TH
1270 if (unlikely(requested_size > dmabuf->base.num_pages * PAGE_SIZE)) {
1271 DRM_ERROR("Screen buffer object size is too small "
1272 "for requested mode.\n");
1273 return -EINVAL;
1274 }
1275
c337ada7 1276 /* Limited framebuffer color depth support for screen objects */
c8261a96 1277 if (dev_priv->active_display_unit == vmw_du_screen_object) {
dabdcdc9
DV
1278 switch (mode_cmd->pixel_format) {
1279 case DRM_FORMAT_XRGB8888:
1280 case DRM_FORMAT_ARGB8888:
1281 break;
1282 case DRM_FORMAT_XRGB1555:
1283 case DRM_FORMAT_RGB565:
1284 break;
c337ada7 1285 default:
dabdcdc9
DV
1286 DRM_ERROR("Invalid pixel format: %s\n",
1287 drm_get_format_name(mode_cmd->pixel_format, &format_name));
c337ada7
JB
1288 return -EINVAL;
1289 }
1290 }
1291
fb1d9738
JB
1292 vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL);
1293 if (!vfbd) {
1294 ret = -ENOMEM;
1295 goto out_err1;
1296 }
1297
a3f913ca 1298 drm_helper_mode_fill_fb_struct(dev, &vfbd->base.base, mode_cmd);
2fcd5a73 1299 vfbd->base.dmabuf = true;
05c95018 1300 vfbd->buffer = vmw_dmabuf_reference(dmabuf);
dabdcdc9 1301 vfbd->base.user_handle = mode_cmd->handles[0];
fb1d9738
JB
1302 *out = &vfbd->base;
1303
80f0b5af
DV
1304 ret = drm_framebuffer_init(dev, &vfbd->base.base,
1305 &vmw_framebuffer_dmabuf_funcs);
1306 if (ret)
05c95018 1307 goto out_err2;
80f0b5af 1308
fb1d9738
JB
1309 return 0;
1310
fb1d9738 1311out_err2:
05c95018 1312 vmw_dmabuf_unreference(&dmabuf);
fb1d9738
JB
1313 kfree(vfbd);
1314out_err1:
1315 return ret;
1316}
1317
810b3e16
SY
1318
1319/**
1320 * vmw_kms_srf_ok - check if a surface can be created
1321 *
1322 * @width: requested width
1323 * @height: requested height
1324 *
1325 * Surfaces need to be less than texture size
1326 */
1327static bool
1328vmw_kms_srf_ok(struct vmw_private *dev_priv, uint32_t width, uint32_t height)
1329{
1330 if (width > dev_priv->texture_max_width ||
1331 height > dev_priv->texture_max_height)
1332 return false;
1333
1334 return true;
1335}
1336
fd006a43
TH
1337/**
1338 * vmw_kms_new_framebuffer - Create a new framebuffer.
1339 *
1340 * @dev_priv: Pointer to device private struct.
1341 * @dmabuf: Pointer to dma buffer to wrap the kms framebuffer around.
1342 * Either @dmabuf or @surface must be NULL.
1343 * @surface: Pointer to a surface to wrap the kms framebuffer around.
1344 * Either @dmabuf or @surface must be NULL.
1345 * @only_2d: No presents will occur to this dma buffer based framebuffer. This
1346 * Helps the code to do some important optimizations.
1347 * @mode_cmd: Frame-buffer metadata.
fb1d9738 1348 */
fd006a43
TH
1349struct vmw_framebuffer *
1350vmw_kms_new_framebuffer(struct vmw_private *dev_priv,
1351 struct vmw_dma_buffer *dmabuf,
1352 struct vmw_surface *surface,
1353 bool only_2d,
dabdcdc9 1354 const struct drm_mode_fb_cmd2 *mode_cmd)
fb1d9738 1355{
fb1d9738 1356 struct vmw_framebuffer *vfb = NULL;
fd006a43 1357 bool is_dmabuf_proxy = false;
fb1d9738
JB
1358 int ret;
1359
fd006a43
TH
1360 /*
1361 * We cannot use the SurfaceDMA command in an non-accelerated VM,
1362 * therefore, wrap the DMA buf in a surface so we can use the
1363 * SurfaceCopy command.
1364 */
810b3e16
SY
1365 if (vmw_kms_srf_ok(dev_priv, mode_cmd->width, mode_cmd->height) &&
1366 dmabuf && only_2d &&
bbd5fefe 1367 mode_cmd->width > 64 && /* Don't create a proxy for cursor */
fd006a43
TH
1368 dev_priv->active_display_unit == vmw_du_screen_target) {
1369 ret = vmw_create_dmabuf_proxy(dev_priv->dev, mode_cmd,
1370 dmabuf, &surface);
1371 if (ret)
1372 return ERR_PTR(ret);
1373
1374 is_dmabuf_proxy = true;
1375 }
1376
1377 /* Create the new framebuffer depending one what we have */
05c95018 1378 if (surface) {
fd006a43
TH
1379 ret = vmw_kms_new_framebuffer_surface(dev_priv, surface, &vfb,
1380 mode_cmd,
1381 is_dmabuf_proxy);
05c95018
SY
1382
1383 /*
1384 * vmw_create_dmabuf_proxy() adds a reference that is no longer
1385 * needed
1386 */
1387 if (is_dmabuf_proxy)
1388 vmw_surface_unreference(&surface);
1389 } else if (dmabuf) {
fd006a43
TH
1390 ret = vmw_kms_new_framebuffer_dmabuf(dev_priv, dmabuf, &vfb,
1391 mode_cmd);
05c95018 1392 } else {
fd006a43 1393 BUG();
05c95018 1394 }
fd006a43
TH
1395
1396 if (ret)
1397 return ERR_PTR(ret);
1398
1399 vfb->pin = vmw_framebuffer_pin;
1400 vfb->unpin = vmw_framebuffer_unpin;
1401
1402 return vfb;
1403}
1404
fb1d9738
JB
1405/*
1406 * Generic Kernel modesetting functions
1407 */
1408
1409static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev,
1410 struct drm_file *file_priv,
dabdcdc9 1411 const struct drm_mode_fb_cmd2 *mode_cmd)
fb1d9738
JB
1412{
1413 struct vmw_private *dev_priv = vmw_priv(dev);
1414 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
1415 struct vmw_framebuffer *vfb = NULL;
1416 struct vmw_surface *surface = NULL;
1417 struct vmw_dma_buffer *bo = NULL;
90ff18bc 1418 struct ttm_base_object *user_obj;
fb1d9738
JB
1419 int ret;
1420
d3216a0c
TH
1421 /**
1422 * This code should be conditioned on Screen Objects not being used.
1423 * If screen objects are used, we can allocate a GMR to hold the
1424 * requested framebuffer.
1425 */
1426
8a783896 1427 if (!vmw_kms_validate_mode_vram(dev_priv,
dabdcdc9
DV
1428 mode_cmd->pitches[0],
1429 mode_cmd->height)) {
c8261a96 1430 DRM_ERROR("Requested mode exceed bounding box limit.\n");
d9826409 1431 return ERR_PTR(-ENOMEM);
d3216a0c
TH
1432 }
1433
90ff18bc
TH
1434 /*
1435 * Take a reference on the user object of the resource
1436 * backing the kms fb. This ensures that user-space handle
1437 * lookups on that resource will always work as long as
1438 * it's registered with a kms framebuffer. This is important,
1439 * since vmw_execbuf_process identifies resources in the
1440 * command stream using user-space handles.
1441 */
1442
dabdcdc9 1443 user_obj = ttm_base_object_lookup(tfile, mode_cmd->handles[0]);
90ff18bc
TH
1444 if (unlikely(user_obj == NULL)) {
1445 DRM_ERROR("Could not locate requested kms frame buffer.\n");
1446 return ERR_PTR(-ENOENT);
1447 }
1448
d3216a0c
TH
1449 /**
1450 * End conditioned code.
1451 */
1452
e7ac9211
JB
1453 /* returns either a dmabuf or surface */
1454 ret = vmw_user_lookup_handle(dev_priv, tfile,
dabdcdc9 1455 mode_cmd->handles[0],
e7ac9211 1456 &surface, &bo);
fb1d9738 1457 if (ret)
e7ac9211
JB
1458 goto err_out;
1459
810b3e16
SY
1460
1461 if (!bo &&
1462 !vmw_kms_srf_ok(dev_priv, mode_cmd->width, mode_cmd->height)) {
1463 DRM_ERROR("Surface size cannot exceed %dx%d",
1464 dev_priv->texture_max_width,
1465 dev_priv->texture_max_height);
1466 goto err_out;
1467 }
1468
1469
fd006a43
TH
1470 vfb = vmw_kms_new_framebuffer(dev_priv, bo, surface,
1471 !(dev_priv->capabilities & SVGA_CAP_3D),
dabdcdc9 1472 mode_cmd);
fd006a43
TH
1473 if (IS_ERR(vfb)) {
1474 ret = PTR_ERR(vfb);
1475 goto err_out;
1476 }
e7ac9211
JB
1477
1478err_out:
1479 /* vmw_user_lookup_handle takes one ref so does new_fb */
1480 if (bo)
1481 vmw_dmabuf_unreference(&bo);
1482 if (surface)
1483 vmw_surface_unreference(&surface);
fb1d9738
JB
1484
1485 if (ret) {
1486 DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
90ff18bc 1487 ttm_base_object_unref(&user_obj);
cce13ff7 1488 return ERR_PTR(ret);
90ff18bc
TH
1489 } else
1490 vfb->user_obj = user_obj;
fb1d9738
JB
1491
1492 return &vfb->base;
1493}
1494
c46a3064
SY
1495
1496
1497/**
1498 * vmw_kms_atomic_check_modeset- validate state object for modeset changes
1499 *
1500 * @dev: DRM device
1501 * @state: the driver state object
1502 *
1503 * This is a simple wrapper around drm_atomic_helper_check_modeset() for
1504 * us to assign a value to mode->crtc_clock so that
1505 * drm_calc_timestamping_constants() won't throw an error message
1506 *
1507 * RETURNS
1508 * Zero for success or -errno
1509 */
bdc362f6 1510static int
c46a3064
SY
1511vmw_kms_atomic_check_modeset(struct drm_device *dev,
1512 struct drm_atomic_state *state)
1513{
1514 struct drm_crtc_state *crtc_state;
1515 struct drm_crtc *crtc;
1516 struct vmw_private *dev_priv = vmw_priv(dev);
1517 int i;
1518
bdc362f6 1519 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
c46a3064
SY
1520 unsigned long requested_bb_mem = 0;
1521
1522 if (dev_priv->active_display_unit == vmw_du_screen_target) {
1523 if (crtc->primary->fb) {
1524 int cpp = crtc->primary->fb->pitches[0] /
1525 crtc->primary->fb->width;
1526
1527 requested_bb_mem += crtc->mode.hdisplay * cpp *
1528 crtc->mode.vdisplay;
1529 }
1530
1531 if (requested_bb_mem > dev_priv->prim_bb_mem)
1532 return -EINVAL;
1533 }
1534 }
1535
1536 return drm_atomic_helper_check(dev, state);
1537}
1538
1539
021aba76
SY
1540/**
1541 * vmw_kms_atomic_commit - Perform an atomic state commit
1542 *
1543 * @dev: DRM device
1544 * @state: the driver state object
1545 * @nonblock: Whether nonblocking behaviour is requested
1546 *
1547 * This is a simple wrapper around drm_atomic_helper_commit() for
1548 * us to clear the nonblocking value.
1549 *
1550 * Nonblocking commits currently cause synchronization issues
1551 * for vmwgfx.
1552 *
1553 * RETURNS
1554 * Zero for success or negative error code on failure.
1555 */
1556int vmw_kms_atomic_commit(struct drm_device *dev,
1557 struct drm_atomic_state *state,
1558 bool nonblock)
1559{
1560 return drm_atomic_helper_commit(dev, state, false);
1561}
1562
1563
e6ecefaa 1564static const struct drm_mode_config_funcs vmw_kms_funcs = {
fb1d9738 1565 .fb_create = vmw_kms_fb_create,
c46a3064 1566 .atomic_check = vmw_kms_atomic_check_modeset,
021aba76 1567 .atomic_commit = vmw_kms_atomic_commit,
fb1d9738
JB
1568};
1569
b9eb1a61
TH
1570static int vmw_kms_generic_present(struct vmw_private *dev_priv,
1571 struct drm_file *file_priv,
1572 struct vmw_framebuffer *vfb,
1573 struct vmw_surface *surface,
1574 uint32_t sid,
1575 int32_t destX, int32_t destY,
1576 struct drm_vmw_rect *clips,
1577 uint32_t num_clips)
2fcd5a73 1578{
10b1e0ca
TH
1579 return vmw_kms_sou_do_surface_dirty(dev_priv, vfb, NULL, clips,
1580 &surface->res, destX, destY,
91e9f352 1581 num_clips, 1, NULL, NULL);
2fcd5a73
JB
1582}
1583
6bf6bf03 1584
2fcd5a73
JB
1585int vmw_kms_present(struct vmw_private *dev_priv,
1586 struct drm_file *file_priv,
1587 struct vmw_framebuffer *vfb,
1588 struct vmw_surface *surface,
1589 uint32_t sid,
1590 int32_t destX, int32_t destY,
1591 struct drm_vmw_rect *clips,
1592 uint32_t num_clips)
1593{
35c05125 1594 int ret;
2fcd5a73 1595
6bf6bf03
TH
1596 switch (dev_priv->active_display_unit) {
1597 case vmw_du_screen_target:
1598 ret = vmw_kms_stdu_surface_dirty(dev_priv, vfb, NULL, clips,
1599 &surface->res, destX, destY,
91e9f352 1600 num_clips, 1, NULL, NULL);
6bf6bf03
TH
1601 break;
1602 case vmw_du_screen_object:
1603 ret = vmw_kms_generic_present(dev_priv, file_priv, vfb, surface,
1604 sid, destX, destY, clips,
1605 num_clips);
1606 break;
1607 default:
1608 WARN_ONCE(true,
1609 "Present called with invalid display system.\n");
1610 ret = -ENOSYS;
1611 break;
2fcd5a73 1612 }
35c05125
SY
1613 if (ret)
1614 return ret;
2fcd5a73 1615
35c05125 1616 vmw_fifo_flush(dev_priv, false);
2fcd5a73 1617
35c05125 1618 return 0;
2fcd5a73
JB
1619}
1620
578e609a
TH
1621static void
1622vmw_kms_create_hotplug_mode_update_property(struct vmw_private *dev_priv)
1623{
1624 if (dev_priv->hotplug_mode_update_property)
1625 return;
1626
1627 dev_priv->hotplug_mode_update_property =
1628 drm_property_create_range(dev_priv->dev,
1629 DRM_MODE_PROP_IMMUTABLE,
1630 "hotplug_mode_update", 0, 1);
1631
1632 if (!dev_priv->hotplug_mode_update_property)
1633 return;
1634
1635}
1636
fb1d9738
JB
1637int vmw_kms_init(struct vmw_private *dev_priv)
1638{
1639 struct drm_device *dev = dev_priv->dev;
1640 int ret;
1641
1642 drm_mode_config_init(dev);
1643 dev->mode_config.funcs = &vmw_kms_funcs;
3bef3572
JB
1644 dev->mode_config.min_width = 1;
1645 dev->mode_config.min_height = 1;
65ade7d3
SY
1646 dev->mode_config.max_width = dev_priv->texture_max_width;
1647 dev->mode_config.max_height = dev_priv->texture_max_height;
fb1d9738 1648
578e609a
TH
1649 drm_mode_create_suggested_offset_properties(dev);
1650 vmw_kms_create_hotplug_mode_update_property(dev_priv);
1651
35c05125
SY
1652 ret = vmw_kms_stdu_init_display(dev_priv);
1653 if (ret) {
1654 ret = vmw_kms_sou_init_display(dev_priv);
1655 if (ret) /* Fallback */
1656 ret = vmw_kms_ldu_init_display(dev_priv);
1657 }
fb1d9738 1658
c8261a96 1659 return ret;
fb1d9738
JB
1660}
1661
1662int vmw_kms_close(struct vmw_private *dev_priv)
1663{
5f58e974 1664 int ret = 0;
c8261a96 1665
fb1d9738
JB
1666 /*
1667 * Docs says we should take the lock before calling this function
1668 * but since it destroys encoders and our destructor calls
1669 * drm_encoder_cleanup which takes the lock we deadlock.
1670 */
1671 drm_mode_config_cleanup(dev_priv->dev);
5f58e974 1672 if (dev_priv->active_display_unit == vmw_du_legacy)
c8261a96
SY
1673 ret = vmw_kms_ldu_close_display(dev_priv);
1674
1675 return ret;
fb1d9738
JB
1676}
1677
1678int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
1679 struct drm_file *file_priv)
1680{
1681 struct drm_vmw_cursor_bypass_arg *arg = data;
1682 struct vmw_display_unit *du;
fb1d9738
JB
1683 struct drm_crtc *crtc;
1684 int ret = 0;
1685
1686
1687 mutex_lock(&dev->mode_config.mutex);
1688 if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
1689
1690 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1691 du = vmw_crtc_to_du(crtc);
1692 du->hotspot_x = arg->xhot;
1693 du->hotspot_y = arg->yhot;
1694 }
1695
1696 mutex_unlock(&dev->mode_config.mutex);
1697 return 0;
1698 }
1699
418da172 1700 crtc = drm_crtc_find(dev, file_priv, arg->crtc_id);
a4cd5d68 1701 if (!crtc) {
4ae87ff0 1702 ret = -ENOENT;
fb1d9738
JB
1703 goto out;
1704 }
1705
fb1d9738
JB
1706 du = vmw_crtc_to_du(crtc);
1707
1708 du->hotspot_x = arg->xhot;
1709 du->hotspot_y = arg->yhot;
1710
1711out:
1712 mutex_unlock(&dev->mode_config.mutex);
1713
1714 return ret;
1715}
1716
0bef23f9 1717int vmw_kms_write_svga(struct vmw_private *vmw_priv,
d7e1958d 1718 unsigned width, unsigned height, unsigned pitch,
6558429b 1719 unsigned bpp, unsigned depth)
fb1d9738 1720{
d7e1958d
JB
1721 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1722 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch);
1723 else if (vmw_fifo_have_pitchlock(vmw_priv))
b76ff5ea
TH
1724 vmw_mmio_write(pitch, vmw_priv->mmio_virt +
1725 SVGA_FIFO_PITCHLOCK);
d7e1958d
JB
1726 vmw_write(vmw_priv, SVGA_REG_WIDTH, width);
1727 vmw_write(vmw_priv, SVGA_REG_HEIGHT, height);
6558429b 1728 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp);
0bef23f9
MD
1729
1730 if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) {
1731 DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n",
1732 depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH));
1733 return -EINVAL;
1734 }
1735
1736 return 0;
d7e1958d 1737}
fb1d9738 1738
d7e1958d
JB
1739int vmw_kms_save_vga(struct vmw_private *vmw_priv)
1740{
7c4f7780
TH
1741 struct vmw_vga_topology_state *save;
1742 uint32_t i;
1743
fb1d9738
JB
1744 vmw_priv->vga_width = vmw_read(vmw_priv, SVGA_REG_WIDTH);
1745 vmw_priv->vga_height = vmw_read(vmw_priv, SVGA_REG_HEIGHT);
7c4f7780 1746 vmw_priv->vga_bpp = vmw_read(vmw_priv, SVGA_REG_BITS_PER_PIXEL);
d7e1958d
JB
1747 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1748 vmw_priv->vga_pitchlock =
7c4f7780 1749 vmw_read(vmw_priv, SVGA_REG_PITCHLOCK);
d7e1958d 1750 else if (vmw_fifo_have_pitchlock(vmw_priv))
b76ff5ea
TH
1751 vmw_priv->vga_pitchlock = vmw_mmio_read(vmw_priv->mmio_virt +
1752 SVGA_FIFO_PITCHLOCK);
7c4f7780
TH
1753
1754 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1755 return 0;
fb1d9738 1756
7c4f7780
TH
1757 vmw_priv->num_displays = vmw_read(vmw_priv,
1758 SVGA_REG_NUM_GUEST_DISPLAYS);
1759
029e50bf
TH
1760 if (vmw_priv->num_displays == 0)
1761 vmw_priv->num_displays = 1;
1762
7c4f7780
TH
1763 for (i = 0; i < vmw_priv->num_displays; ++i) {
1764 save = &vmw_priv->vga_save[i];
1765 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1766 save->primary = vmw_read(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY);
1767 save->pos_x = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_X);
1768 save->pos_y = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y);
1769 save->width = vmw_read(vmw_priv, SVGA_REG_DISPLAY_WIDTH);
1770 save->height = vmw_read(vmw_priv, SVGA_REG_DISPLAY_HEIGHT);
1771 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
30c78bb8
TH
1772 if (i == 0 && vmw_priv->num_displays == 1 &&
1773 save->width == 0 && save->height == 0) {
1774
1775 /*
1776 * It should be fairly safe to assume that these
1777 * values are uninitialized.
1778 */
1779
1780 save->width = vmw_priv->vga_width - save->pos_x;
1781 save->height = vmw_priv->vga_height - save->pos_y;
1782 }
7c4f7780 1783 }
30c78bb8 1784
fb1d9738
JB
1785 return 0;
1786}
1787
1788int vmw_kms_restore_vga(struct vmw_private *vmw_priv)
1789{
7c4f7780
TH
1790 struct vmw_vga_topology_state *save;
1791 uint32_t i;
1792
fb1d9738
JB
1793 vmw_write(vmw_priv, SVGA_REG_WIDTH, vmw_priv->vga_width);
1794 vmw_write(vmw_priv, SVGA_REG_HEIGHT, vmw_priv->vga_height);
7c4f7780 1795 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, vmw_priv->vga_bpp);
d7e1958d
JB
1796 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1797 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK,
1798 vmw_priv->vga_pitchlock);
1799 else if (vmw_fifo_have_pitchlock(vmw_priv))
b76ff5ea
TH
1800 vmw_mmio_write(vmw_priv->vga_pitchlock,
1801 vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
fb1d9738 1802
7c4f7780
TH
1803 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1804 return 0;
1805
1806 for (i = 0; i < vmw_priv->num_displays; ++i) {
1807 save = &vmw_priv->vga_save[i];
1808 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1809 vmw_write(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY, save->primary);
1810 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_X, save->pos_x);
1811 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y, save->pos_y);
1812 vmw_write(vmw_priv, SVGA_REG_DISPLAY_WIDTH, save->width);
1813 vmw_write(vmw_priv, SVGA_REG_DISPLAY_HEIGHT, save->height);
1814 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
1815 }
1816
fb1d9738
JB
1817 return 0;
1818}
d8bd19d2 1819
e133e737
TH
1820bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
1821 uint32_t pitch,
1822 uint32_t height)
1823{
35c05125
SY
1824 return ((u64) pitch * (u64) height) < (u64)
1825 ((dev_priv->active_display_unit == vmw_du_screen_target) ?
1826 dev_priv->prim_bb_mem : dev_priv->vram_size);
e133e737
TH
1827}
1828
1c482ab3
JB
1829
1830/**
1831 * Function called by DRM code called with vbl_lock held.
1832 */
88e72717 1833u32 vmw_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
7a1c2f6c
TH
1834{
1835 return 0;
1836}
626ab771 1837
1c482ab3
JB
1838/**
1839 * Function called by DRM code called with vbl_lock held.
1840 */
88e72717 1841int vmw_enable_vblank(struct drm_device *dev, unsigned int pipe)
1c482ab3 1842{
2b0bc68c 1843 return -EINVAL;
1c482ab3
JB
1844}
1845
1846/**
1847 * Function called by DRM code called with vbl_lock held.
1848 */
88e72717 1849void vmw_disable_vblank(struct drm_device *dev, unsigned int pipe)
1c482ab3
JB
1850{
1851}
1852
626ab771
JB
1853
1854/*
1855 * Small shared kms functions.
1856 */
1857
847c5964 1858static int vmw_du_update_layout(struct vmw_private *dev_priv, unsigned num,
626ab771
JB
1859 struct drm_vmw_rect *rects)
1860{
1861 struct drm_device *dev = dev_priv->dev;
1862 struct vmw_display_unit *du;
1863 struct drm_connector *con;
626ab771
JB
1864
1865 mutex_lock(&dev->mode_config.mutex);
1866
1867#if 0
6ea77d13
TH
1868 {
1869 unsigned int i;
1870
1871 DRM_INFO("%s: new layout ", __func__);
1872 for (i = 0; i < num; i++)
1873 DRM_INFO("(%i, %i %ux%u) ", rects[i].x, rects[i].y,
1874 rects[i].w, rects[i].h);
1875 DRM_INFO("\n");
1876 }
626ab771
JB
1877#endif
1878
1879 list_for_each_entry(con, &dev->mode_config.connector_list, head) {
1880 du = vmw_connector_to_du(con);
1881 if (num > du->unit) {
1882 du->pref_width = rects[du->unit].w;
1883 du->pref_height = rects[du->unit].h;
1884 du->pref_active = true;
cd2b89e7
TH
1885 du->gui_x = rects[du->unit].x;
1886 du->gui_y = rects[du->unit].y;
578e609a
TH
1887 drm_object_property_set_value
1888 (&con->base, dev->mode_config.suggested_x_property,
1889 du->gui_x);
1890 drm_object_property_set_value
1891 (&con->base, dev->mode_config.suggested_y_property,
1892 du->gui_y);
626ab771
JB
1893 } else {
1894 du->pref_width = 800;
1895 du->pref_height = 600;
1896 du->pref_active = false;
578e609a
TH
1897 drm_object_property_set_value
1898 (&con->base, dev->mode_config.suggested_x_property,
1899 0);
1900 drm_object_property_set_value
1901 (&con->base, dev->mode_config.suggested_y_property,
1902 0);
626ab771
JB
1903 }
1904 con->status = vmw_du_connector_detect(con, true);
1905 }
1906
1907 mutex_unlock(&dev->mode_config.mutex);
578e609a 1908 drm_sysfs_hotplug_event(dev);
626ab771
JB
1909
1910 return 0;
1911}
1912
7ea77283
ML
1913int vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
1914 u16 *r, u16 *g, u16 *b,
6d124ff8
DV
1915 uint32_t size,
1916 struct drm_modeset_acquire_ctx *ctx)
626ab771
JB
1917{
1918 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
1919 int i;
1920
1921 for (i = 0; i < size; i++) {
1922 DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i,
1923 r[i], g[i], b[i]);
1924 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 0, r[i] >> 8);
1925 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 1, g[i] >> 8);
1926 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 2, b[i] >> 8);
1927 }
7ea77283
ML
1928
1929 return 0;
626ab771
JB
1930}
1931
9a69a9ac 1932int vmw_du_connector_dpms(struct drm_connector *connector, int mode)
626ab771 1933{
9a69a9ac 1934 return 0;
626ab771
JB
1935}
1936
626ab771
JB
1937enum drm_connector_status
1938vmw_du_connector_detect(struct drm_connector *connector, bool force)
1939{
1940 uint32_t num_displays;
1941 struct drm_device *dev = connector->dev;
1942 struct vmw_private *dev_priv = vmw_priv(dev);
cd2b89e7 1943 struct vmw_display_unit *du = vmw_connector_to_du(connector);
626ab771 1944
626ab771 1945 num_displays = vmw_read(dev_priv, SVGA_REG_NUM_DISPLAYS);
626ab771 1946
cd2b89e7
TH
1947 return ((vmw_connector_to_du(connector)->unit < num_displays &&
1948 du->pref_active) ?
626ab771
JB
1949 connector_status_connected : connector_status_disconnected);
1950}
1951
1952static struct drm_display_mode vmw_kms_connector_builtin[] = {
1953 /* 640x480@60Hz */
1954 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
1955 752, 800, 0, 480, 489, 492, 525, 0,
1956 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1957 /* 800x600@60Hz */
1958 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
1959 968, 1056, 0, 600, 601, 605, 628, 0,
1960 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1961 /* 1024x768@60Hz */
1962 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
1963 1184, 1344, 0, 768, 771, 777, 806, 0,
1964 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1965 /* 1152x864@75Hz */
1966 { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
1967 1344, 1600, 0, 864, 865, 868, 900, 0,
1968 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1969 /* 1280x768@60Hz */
1970 { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344,
1971 1472, 1664, 0, 768, 771, 778, 798, 0,
1972 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1973 /* 1280x800@60Hz */
1974 { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352,
1975 1480, 1680, 0, 800, 803, 809, 831, 0,
1976 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
1977 /* 1280x960@60Hz */
1978 { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376,
1979 1488, 1800, 0, 960, 961, 964, 1000, 0,
1980 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1981 /* 1280x1024@60Hz */
1982 { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328,
1983 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
1984 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1985 /* 1360x768@60Hz */
1986 { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424,
1987 1536, 1792, 0, 768, 771, 777, 795, 0,
1988 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1989 /* 1440x1050@60Hz */
1990 { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488,
1991 1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
1992 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1993 /* 1440x900@60Hz */
1994 { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520,
1995 1672, 1904, 0, 900, 903, 909, 934, 0,
1996 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1997 /* 1600x1200@60Hz */
1998 { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664,
1999 1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
2000 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
2001 /* 1680x1050@60Hz */
2002 { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784,
2003 1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
2004 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2005 /* 1792x1344@60Hz */
2006 { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920,
2007 2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
2008 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2009 /* 1853x1392@60Hz */
2010 { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952,
2011 2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
2012 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2013 /* 1920x1200@60Hz */
2014 { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056,
2015 2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
2016 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2017 /* 1920x1440@60Hz */
2018 { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048,
2019 2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
2020 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2021 /* 2560x1600@60Hz */
2022 { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752,
2023 3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
2024 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2025 /* Terminate */
2026 { DRM_MODE("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) },
2027};
2028
1543b4dd
TH
2029/**
2030 * vmw_guess_mode_timing - Provide fake timings for a
2031 * 60Hz vrefresh mode.
2032 *
2033 * @mode - Pointer to a struct drm_display_mode with hdisplay and vdisplay
2034 * members filled in.
2035 */
a278724a 2036void vmw_guess_mode_timing(struct drm_display_mode *mode)
1543b4dd
TH
2037{
2038 mode->hsync_start = mode->hdisplay + 50;
2039 mode->hsync_end = mode->hsync_start + 50;
2040 mode->htotal = mode->hsync_end + 50;
2041
2042 mode->vsync_start = mode->vdisplay + 50;
2043 mode->vsync_end = mode->vsync_start + 50;
2044 mode->vtotal = mode->vsync_end + 50;
2045
2046 mode->clock = (u32)mode->htotal * (u32)mode->vtotal / 100 * 6;
2047 mode->vrefresh = drm_mode_vrefresh(mode);
2048}
2049
2050
626ab771
JB
2051int vmw_du_connector_fill_modes(struct drm_connector *connector,
2052 uint32_t max_width, uint32_t max_height)
2053{
2054 struct vmw_display_unit *du = vmw_connector_to_du(connector);
2055 struct drm_device *dev = connector->dev;
2056 struct vmw_private *dev_priv = vmw_priv(dev);
2057 struct drm_display_mode *mode = NULL;
2058 struct drm_display_mode *bmode;
2059 struct drm_display_mode prefmode = { DRM_MODE("preferred",
2060 DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
2061 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2062 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
2063 };
2064 int i;
7c20d213 2065 u32 assumed_bpp = 4;
9a72384d 2066
04319d89
SY
2067 if (dev_priv->assume_16bpp)
2068 assumed_bpp = 2;
626ab771 2069
35c05125
SY
2070 if (dev_priv->active_display_unit == vmw_du_screen_target) {
2071 max_width = min(max_width, dev_priv->stdu_max_width);
28c95429
SY
2072 max_width = min(max_width, dev_priv->texture_max_width);
2073
35c05125 2074 max_height = min(max_height, dev_priv->stdu_max_height);
28c95429 2075 max_height = min(max_height, dev_priv->texture_max_height);
35c05125
SY
2076 }
2077
626ab771 2078 /* Add preferred mode */
c8261a96
SY
2079 mode = drm_mode_duplicate(dev, &prefmode);
2080 if (!mode)
2081 return 0;
2082 mode->hdisplay = du->pref_width;
2083 mode->vdisplay = du->pref_height;
2084 vmw_guess_mode_timing(mode);
626ab771 2085
c8261a96
SY
2086 if (vmw_kms_validate_mode_vram(dev_priv,
2087 mode->hdisplay * assumed_bpp,
2088 mode->vdisplay)) {
2089 drm_mode_probed_add(connector, mode);
2090 } else {
2091 drm_mode_destroy(dev, mode);
2092 mode = NULL;
2093 }
55bde5b2 2094
c8261a96
SY
2095 if (du->pref_mode) {
2096 list_del_init(&du->pref_mode->head);
2097 drm_mode_destroy(dev, du->pref_mode);
626ab771
JB
2098 }
2099
c8261a96
SY
2100 /* mode might be null here, this is intended */
2101 du->pref_mode = mode;
2102
626ab771
JB
2103 for (i = 0; vmw_kms_connector_builtin[i].type != 0; i++) {
2104 bmode = &vmw_kms_connector_builtin[i];
2105 if (bmode->hdisplay > max_width ||
2106 bmode->vdisplay > max_height)
2107 continue;
2108
9a72384d
SY
2109 if (!vmw_kms_validate_mode_vram(dev_priv,
2110 bmode->hdisplay * assumed_bpp,
626ab771
JB
2111 bmode->vdisplay))
2112 continue;
2113
2114 mode = drm_mode_duplicate(dev, bmode);
2115 if (!mode)
2116 return 0;
2117 mode->vrefresh = drm_mode_vrefresh(mode);
2118
2119 drm_mode_probed_add(connector, mode);
2120 }
2121
6af3e656 2122 drm_mode_connector_list_update(connector);
f6b05004
TH
2123 /* Move the prefered mode first, help apps pick the right mode. */
2124 drm_mode_sort(&connector->modes);
626ab771
JB
2125
2126 return 1;
2127}
2128
2129int vmw_du_connector_set_property(struct drm_connector *connector,
2130 struct drm_property *property,
2131 uint64_t val)
2132{
76404ac0
TH
2133 struct vmw_display_unit *du = vmw_connector_to_du(connector);
2134 struct vmw_private *dev_priv = vmw_priv(connector->dev);
2135
2136 if (property == dev_priv->implicit_placement_property)
2137 du->is_implicit = val;
2138
626ab771
JB
2139 return 0;
2140}
cd2b89e7
TH
2141
2142
9c2542a4 2143
d7721ca7
SY
2144/**
2145 * vmw_du_connector_atomic_set_property - Atomic version of get property
2146 *
2147 * @crtc - crtc the property is associated with
2148 *
2149 * Returns:
2150 * Zero on success, negative errno on failure.
2151 */
2152int
2153vmw_du_connector_atomic_set_property(struct drm_connector *connector,
2154 struct drm_connector_state *state,
2155 struct drm_property *property,
2156 uint64_t val)
2157{
2158 struct vmw_private *dev_priv = vmw_priv(connector->dev);
2159 struct vmw_connector_state *vcs = vmw_connector_state_to_vcs(state);
2160 struct vmw_display_unit *du = vmw_connector_to_du(connector);
2161
2162
2163 if (property == dev_priv->implicit_placement_property) {
2164 vcs->is_implicit = val;
2165
2166 /*
2167 * We should really be doing a drm_atomic_commit() to
2168 * commit the new state, but since this doesn't cause
2169 * an immedate state change, this is probably ok
2170 */
2171 du->is_implicit = vcs->is_implicit;
2172 } else {
2173 return -EINVAL;
2174 }
2175
2176 return 0;
2177}
2178
2179
2180/**
2181 * vmw_du_connector_atomic_get_property - Atomic version of get property
2182 *
2183 * @connector - connector the property is associated with
2184 *
2185 * Returns:
2186 * Zero on success, negative errno on failure.
2187 */
2188int
2189vmw_du_connector_atomic_get_property(struct drm_connector *connector,
2190 const struct drm_connector_state *state,
2191 struct drm_property *property,
2192 uint64_t *val)
2193{
2194 struct vmw_private *dev_priv = vmw_priv(connector->dev);
2195 struct vmw_connector_state *vcs = vmw_connector_state_to_vcs(state);
2196
2197 if (property == dev_priv->implicit_placement_property)
2198 *val = vcs->is_implicit;
2199 else {
2200 DRM_ERROR("Invalid Property %s\n", property->name);
2201 return -EINVAL;
2202 }
2203
2204 return 0;
2205}
2206
2207
cd2b89e7
TH
2208int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data,
2209 struct drm_file *file_priv)
2210{
2211 struct vmw_private *dev_priv = vmw_priv(dev);
2212 struct drm_vmw_update_layout_arg *arg =
2213 (struct drm_vmw_update_layout_arg *)data;
cd2b89e7
TH
2214 void __user *user_rects;
2215 struct drm_vmw_rect *rects;
2216 unsigned rects_size;
2217 int ret;
2218 int i;
65ade7d3 2219 u64 total_pixels = 0;
cd2b89e7 2220 struct drm_mode_config *mode_config = &dev->mode_config;
c8261a96 2221 struct drm_vmw_rect bounding_box = {0};
cd2b89e7 2222
cd2b89e7
TH
2223 if (!arg->num_outputs) {
2224 struct drm_vmw_rect def_rect = {0, 0, 800, 600};
2225 vmw_du_update_layout(dev_priv, 1, &def_rect);
5151adb3 2226 return 0;
cd2b89e7
TH
2227 }
2228
2229 rects_size = arg->num_outputs * sizeof(struct drm_vmw_rect);
bab9efc2
XW
2230 rects = kcalloc(arg->num_outputs, sizeof(struct drm_vmw_rect),
2231 GFP_KERNEL);
5151adb3
TH
2232 if (unlikely(!rects))
2233 return -ENOMEM;
cd2b89e7
TH
2234
2235 user_rects = (void __user *)(unsigned long)arg->rects;
2236 ret = copy_from_user(rects, user_rects, rects_size);
2237 if (unlikely(ret != 0)) {
2238 DRM_ERROR("Failed to get rects.\n");
2239 ret = -EFAULT;
2240 goto out_free;
2241 }
2242
2243 for (i = 0; i < arg->num_outputs; ++i) {
bab9efc2
XW
2244 if (rects[i].x < 0 ||
2245 rects[i].y < 0 ||
2246 rects[i].x + rects[i].w > mode_config->max_width ||
2247 rects[i].y + rects[i].h > mode_config->max_height) {
cd2b89e7
TH
2248 DRM_ERROR("Invalid GUI layout.\n");
2249 ret = -EINVAL;
2250 goto out_free;
2251 }
c8261a96
SY
2252
2253 /*
2254 * bounding_box.w and bunding_box.h are used as
2255 * lower-right coordinates
2256 */
2257 if (rects[i].x + rects[i].w > bounding_box.w)
2258 bounding_box.w = rects[i].x + rects[i].w;
2259
2260 if (rects[i].y + rects[i].h > bounding_box.h)
2261 bounding_box.h = rects[i].y + rects[i].h;
65ade7d3
SY
2262
2263 total_pixels += (u64) rects[i].w * (u64) rects[i].h;
cd2b89e7
TH
2264 }
2265
65ade7d3
SY
2266 if (dev_priv->active_display_unit == vmw_du_screen_target) {
2267 /*
2268 * For Screen Targets, the limits for a toplogy are:
2269 * 1. Bounding box (assuming 32bpp) must be < prim_bb_mem
2270 * 2. Total pixels (assuming 32bpp) must be < prim_bb_mem
2271 */
0f580386 2272 u64 bb_mem = (u64) bounding_box.w * bounding_box.h * 4;
65ade7d3
SY
2273 u64 pixel_mem = total_pixels * 4;
2274
2275 if (bb_mem > dev_priv->prim_bb_mem) {
2276 DRM_ERROR("Topology is beyond supported limits.\n");
35c05125
SY
2277 ret = -EINVAL;
2278 goto out_free;
2279 }
2280
65ade7d3
SY
2281 if (pixel_mem > dev_priv->prim_bb_mem) {
2282 DRM_ERROR("Combined output size too large\n");
2283 ret = -EINVAL;
2284 goto out_free;
2285 }
cd2b89e7
TH
2286 }
2287
2288 vmw_du_update_layout(dev_priv, arg->num_outputs, rects);
2289
2290out_free:
2291 kfree(rects);
cd2b89e7
TH
2292 return ret;
2293}
1a4b172a
TH
2294
2295/**
2296 * vmw_kms_helper_dirty - Helper to build commands and perform actions based
2297 * on a set of cliprects and a set of display units.
2298 *
2299 * @dev_priv: Pointer to a device private structure.
2300 * @framebuffer: Pointer to the framebuffer on which to perform the actions.
2301 * @clips: A set of struct drm_clip_rect. Either this os @vclips must be NULL.
2302 * Cliprects are given in framebuffer coordinates.
2303 * @vclips: A set of struct drm_vmw_rect cliprects. Either this or @clips must
2304 * be NULL. Cliprects are given in source coordinates.
2305 * @dest_x: X coordinate offset for the crtc / destination clip rects.
2306 * @dest_y: Y coordinate offset for the crtc / destination clip rects.
2307 * @num_clips: Number of cliprects in the @clips or @vclips array.
2308 * @increment: Integer with which to increment the clip counter when looping.
2309 * Used to skip a predetermined number of clip rects.
2310 * @dirty: Closure structure. See the description of struct vmw_kms_dirty.
2311 */
2312int vmw_kms_helper_dirty(struct vmw_private *dev_priv,
2313 struct vmw_framebuffer *framebuffer,
2314 const struct drm_clip_rect *clips,
2315 const struct drm_vmw_rect *vclips,
2316 s32 dest_x, s32 dest_y,
2317 int num_clips,
2318 int increment,
2319 struct vmw_kms_dirty *dirty)
2320{
2321 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
2322 struct drm_crtc *crtc;
2323 u32 num_units = 0;
2324 u32 i, k;
1a4b172a
TH
2325
2326 dirty->dev_priv = dev_priv;
2327
91e9f352
DR
2328 /* If crtc is passed, no need to iterate over other display units */
2329 if (dirty->crtc) {
2330 units[num_units++] = vmw_crtc_to_du(dirty->crtc);
2331 } else {
2332 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list,
2333 head) {
2334 if (crtc->primary->fb != &framebuffer->base)
2335 continue;
2336 units[num_units++] = vmw_crtc_to_du(crtc);
2337 }
1a4b172a
TH
2338 }
2339
2340 for (k = 0; k < num_units; k++) {
2341 struct vmw_display_unit *unit = units[k];
2342 s32 crtc_x = unit->crtc.x;
2343 s32 crtc_y = unit->crtc.y;
2344 s32 crtc_width = unit->crtc.mode.hdisplay;
2345 s32 crtc_height = unit->crtc.mode.vdisplay;
2346 const struct drm_clip_rect *clips_ptr = clips;
2347 const struct drm_vmw_rect *vclips_ptr = vclips;
2348
2349 dirty->unit = unit;
2350 if (dirty->fifo_reserve_size > 0) {
2351 dirty->cmd = vmw_fifo_reserve(dev_priv,
2352 dirty->fifo_reserve_size);
2353 if (!dirty->cmd) {
2354 DRM_ERROR("Couldn't reserve fifo space "
2355 "for dirty blits.\n");
f3b8c0ca 2356 return -ENOMEM;
1a4b172a
TH
2357 }
2358 memset(dirty->cmd, 0, dirty->fifo_reserve_size);
2359 }
2360 dirty->num_hits = 0;
2361 for (i = 0; i < num_clips; i++, clips_ptr += increment,
2362 vclips_ptr += increment) {
2363 s32 clip_left;
2364 s32 clip_top;
2365
2366 /*
2367 * Select clip array type. Note that integer type
2368 * in @clips is unsigned short, whereas in @vclips
2369 * it's 32-bit.
2370 */
2371 if (clips) {
2372 dirty->fb_x = (s32) clips_ptr->x1;
2373 dirty->fb_y = (s32) clips_ptr->y1;
2374 dirty->unit_x2 = (s32) clips_ptr->x2 + dest_x -
2375 crtc_x;
2376 dirty->unit_y2 = (s32) clips_ptr->y2 + dest_y -
2377 crtc_y;
2378 } else {
2379 dirty->fb_x = vclips_ptr->x;
2380 dirty->fb_y = vclips_ptr->y;
2381 dirty->unit_x2 = dirty->fb_x + vclips_ptr->w +
2382 dest_x - crtc_x;
2383 dirty->unit_y2 = dirty->fb_y + vclips_ptr->h +
2384 dest_y - crtc_y;
2385 }
2386
2387 dirty->unit_x1 = dirty->fb_x + dest_x - crtc_x;
2388 dirty->unit_y1 = dirty->fb_y + dest_y - crtc_y;
2389
2390 /* Skip this clip if it's outside the crtc region */
2391 if (dirty->unit_x1 >= crtc_width ||
2392 dirty->unit_y1 >= crtc_height ||
2393 dirty->unit_x2 <= 0 || dirty->unit_y2 <= 0)
2394 continue;
2395
2396 /* Clip right and bottom to crtc limits */
2397 dirty->unit_x2 = min_t(s32, dirty->unit_x2,
2398 crtc_width);
2399 dirty->unit_y2 = min_t(s32, dirty->unit_y2,
2400 crtc_height);
2401
2402 /* Clip left and top to crtc limits */
2403 clip_left = min_t(s32, dirty->unit_x1, 0);
2404 clip_top = min_t(s32, dirty->unit_y1, 0);
2405 dirty->unit_x1 -= clip_left;
2406 dirty->unit_y1 -= clip_top;
2407 dirty->fb_x -= clip_left;
2408 dirty->fb_y -= clip_top;
2409
2410 dirty->clip(dirty);
2411 }
2412
2413 dirty->fifo_commit(dirty);
2414 }
2415
2416 return 0;
2417}
2418
2419/**
2420 * vmw_kms_helper_buffer_prepare - Reserve and validate a buffer object before
2421 * command submission.
2422 *
2423 * @dev_priv. Pointer to a device private structure.
2424 * @buf: The buffer object
2425 * @interruptible: Whether to perform waits as interruptible.
2426 * @validate_as_mob: Whether the buffer should be validated as a MOB. If false,
2427 * The buffer will be validated as a GMR. Already pinned buffers will not be
2428 * validated.
2429 *
2430 * Returns 0 on success, negative error code on failure, -ERESTARTSYS if
2431 * interrupted by a signal.
2432 */
2433int vmw_kms_helper_buffer_prepare(struct vmw_private *dev_priv,
2434 struct vmw_dma_buffer *buf,
2435 bool interruptible,
2436 bool validate_as_mob)
2437{
2438 struct ttm_buffer_object *bo = &buf->base;
2439 int ret;
2440
dfd5e50e 2441 ttm_bo_reserve(bo, false, false, NULL);
1a4b172a
TH
2442 ret = vmw_validate_single_buffer(dev_priv, bo, interruptible,
2443 validate_as_mob);
2444 if (ret)
2445 ttm_bo_unreserve(bo);
2446
2447 return ret;
2448}
2449
2450/**
2451 * vmw_kms_helper_buffer_revert - Undo the actions of
2452 * vmw_kms_helper_buffer_prepare.
2453 *
2454 * @res: Pointer to the buffer object.
2455 *
2456 * Helper to be used if an error forces the caller to undo the actions of
2457 * vmw_kms_helper_buffer_prepare.
2458 */
2459void vmw_kms_helper_buffer_revert(struct vmw_dma_buffer *buf)
2460{
2461 if (buf)
2462 ttm_bo_unreserve(&buf->base);
2463}
2464
2465/**
2466 * vmw_kms_helper_buffer_finish - Unreserve and fence a buffer object after
2467 * kms command submission.
2468 *
2469 * @dev_priv: Pointer to a device private structure.
2470 * @file_priv: Pointer to a struct drm_file representing the caller's
2471 * connection. Must be set to NULL if @user_fence_rep is NULL, and conversely
2472 * if non-NULL, @user_fence_rep must be non-NULL.
2473 * @buf: The buffer object.
2474 * @out_fence: Optional pointer to a fence pointer. If non-NULL, a
2475 * ref-counted fence pointer is returned here.
2476 * @user_fence_rep: Optional pointer to a user-space provided struct
2477 * drm_vmw_fence_rep. If provided, @file_priv must also be provided and the
2478 * function copies fence data to user-space in a fail-safe manner.
2479 */
2480void vmw_kms_helper_buffer_finish(struct vmw_private *dev_priv,
2481 struct drm_file *file_priv,
2482 struct vmw_dma_buffer *buf,
2483 struct vmw_fence_obj **out_fence,
2484 struct drm_vmw_fence_rep __user *
2485 user_fence_rep)
2486{
2487 struct vmw_fence_obj *fence;
2488 uint32_t handle;
2489 int ret;
2490
2491 ret = vmw_execbuf_fence_commands(file_priv, dev_priv, &fence,
2492 file_priv ? &handle : NULL);
2493 if (buf)
2494 vmw_fence_single_bo(&buf->base, fence);
2495 if (file_priv)
2496 vmw_execbuf_copy_fence_user(dev_priv, vmw_fpriv(file_priv),
2497 ret, user_fence_rep, fence,
c906965d 2498 handle, -1, NULL);
1a4b172a
TH
2499 if (out_fence)
2500 *out_fence = fence;
2501 else
2502 vmw_fence_obj_unreference(&fence);
2503
2504 vmw_kms_helper_buffer_revert(buf);
2505}
2506
2507
2508/**
2509 * vmw_kms_helper_resource_revert - Undo the actions of
2510 * vmw_kms_helper_resource_prepare.
2511 *
2512 * @res: Pointer to the resource. Typically a surface.
2513 *
2514 * Helper to be used if an error forces the caller to undo the actions of
2515 * vmw_kms_helper_resource_prepare.
2516 */
2517void vmw_kms_helper_resource_revert(struct vmw_resource *res)
2518{
2519 vmw_kms_helper_buffer_revert(res->backup);
d80efd5c 2520 vmw_resource_unreserve(res, false, NULL, 0);
1a4b172a
TH
2521 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
2522}
2523
2524/**
2525 * vmw_kms_helper_resource_prepare - Reserve and validate a resource before
2526 * command submission.
2527 *
2528 * @res: Pointer to the resource. Typically a surface.
2529 * @interruptible: Whether to perform waits as interruptible.
2530 *
2531 * Reserves and validates also the backup buffer if a guest-backed resource.
2532 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
2533 * interrupted by a signal.
2534 */
2535int vmw_kms_helper_resource_prepare(struct vmw_resource *res,
2536 bool interruptible)
2537{
2538 int ret = 0;
2539
2540 if (interruptible)
2541 ret = mutex_lock_interruptible(&res->dev_priv->cmdbuf_mutex);
2542 else
2543 mutex_lock(&res->dev_priv->cmdbuf_mutex);
2544
2545 if (unlikely(ret != 0))
2546 return -ERESTARTSYS;
2547
2548 ret = vmw_resource_reserve(res, interruptible, false);
2549 if (ret)
2550 goto out_unlock;
2551
2552 if (res->backup) {
2553 ret = vmw_kms_helper_buffer_prepare(res->dev_priv, res->backup,
2554 interruptible,
2555 res->dev_priv->has_mob);
2556 if (ret)
2557 goto out_unreserve;
2558 }
2559 ret = vmw_resource_validate(res);
2560 if (ret)
2561 goto out_revert;
2562 return 0;
2563
2564out_revert:
2565 vmw_kms_helper_buffer_revert(res->backup);
2566out_unreserve:
d80efd5c 2567 vmw_resource_unreserve(res, false, NULL, 0);
1a4b172a
TH
2568out_unlock:
2569 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
2570 return ret;
2571}
2572
2573/**
2574 * vmw_kms_helper_resource_finish - Unreserve and fence a resource after
2575 * kms command submission.
2576 *
2577 * @res: Pointer to the resource. Typically a surface.
2578 * @out_fence: Optional pointer to a fence pointer. If non-NULL, a
2579 * ref-counted fence pointer is returned here.
2580 */
2581void vmw_kms_helper_resource_finish(struct vmw_resource *res,
2582 struct vmw_fence_obj **out_fence)
2583{
2584 if (res->backup || out_fence)
2585 vmw_kms_helper_buffer_finish(res->dev_priv, NULL, res->backup,
2586 out_fence, NULL);
2587
d80efd5c 2588 vmw_resource_unreserve(res, false, NULL, 0);
1a4b172a
TH
2589 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
2590}
6bf6bf03
TH
2591
2592/**
2593 * vmw_kms_update_proxy - Helper function to update a proxy surface from
2594 * its backing MOB.
2595 *
2596 * @res: Pointer to the surface resource
2597 * @clips: Clip rects in framebuffer (surface) space.
2598 * @num_clips: Number of clips in @clips.
2599 * @increment: Integer with which to increment the clip counter when looping.
2600 * Used to skip a predetermined number of clip rects.
2601 *
2602 * This function makes sure the proxy surface is updated from its backing MOB
2603 * using the region given by @clips. The surface resource @res and its backing
2604 * MOB needs to be reserved and validated on call.
2605 */
2606int vmw_kms_update_proxy(struct vmw_resource *res,
2607 const struct drm_clip_rect *clips,
2608 unsigned num_clips,
2609 int increment)
2610{
2611 struct vmw_private *dev_priv = res->dev_priv;
2612 struct drm_vmw_size *size = &vmw_res_to_srf(res)->base_size;
2613 struct {
2614 SVGA3dCmdHeader header;
2615 SVGA3dCmdUpdateGBImage body;
2616 } *cmd;
2617 SVGA3dBox *box;
2618 size_t copy_size = 0;
2619 int i;
2620
2621 if (!clips)
2622 return 0;
2623
2624 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd) * num_clips);
2625 if (!cmd) {
2626 DRM_ERROR("Couldn't reserve fifo space for proxy surface "
2627 "update.\n");
2628 return -ENOMEM;
2629 }
2630
2631 for (i = 0; i < num_clips; ++i, clips += increment, ++cmd) {
2632 box = &cmd->body.box;
2633
2634 cmd->header.id = SVGA_3D_CMD_UPDATE_GB_IMAGE;
2635 cmd->header.size = sizeof(cmd->body);
2636 cmd->body.image.sid = res->id;
2637 cmd->body.image.face = 0;
2638 cmd->body.image.mipmap = 0;
2639
2640 if (clips->x1 > size->width || clips->x2 > size->width ||
2641 clips->y1 > size->height || clips->y2 > size->height) {
2642 DRM_ERROR("Invalid clips outsize of framebuffer.\n");
2643 return -EINVAL;
2644 }
2645
2646 box->x = clips->x1;
2647 box->y = clips->y1;
2648 box->z = 0;
2649 box->w = clips->x2 - clips->x1;
2650 box->h = clips->y2 - clips->y1;
2651 box->d = 1;
2652
2653 copy_size += sizeof(*cmd);
2654 }
2655
2656 vmw_fifo_commit(dev_priv, copy_size);
2657
2658 return 0;
2659}
a278724a
TH
2660
2661int vmw_kms_fbdev_init_data(struct vmw_private *dev_priv,
2662 unsigned unit,
2663 u32 max_width,
2664 u32 max_height,
2665 struct drm_connector **p_con,
2666 struct drm_crtc **p_crtc,
2667 struct drm_display_mode **p_mode)
2668{
2669 struct drm_connector *con;
2670 struct vmw_display_unit *du;
2671 struct drm_display_mode *mode;
2672 int i = 0;
2673
2674 list_for_each_entry(con, &dev_priv->dev->mode_config.connector_list,
2675 head) {
2676 if (i == unit)
2677 break;
2678
2679 ++i;
2680 }
2681
2682 if (i != unit) {
2683 DRM_ERROR("Could not find initial display unit.\n");
2684 return -EINVAL;
2685 }
2686
2687 if (list_empty(&con->modes))
2688 (void) vmw_du_connector_fill_modes(con, max_width, max_height);
2689
2690 if (list_empty(&con->modes)) {
2691 DRM_ERROR("Could not find initial display mode.\n");
2692 return -EINVAL;
2693 }
2694
2695 du = vmw_connector_to_du(con);
2696 *p_con = con;
2697 *p_crtc = &du->crtc;
2698
2699 list_for_each_entry(mode, &con->modes, head) {
2700 if (mode->type & DRM_MODE_TYPE_PREFERRED)
2701 break;
2702 }
2703
2704 if (mode->type & DRM_MODE_TYPE_PREFERRED)
2705 *p_mode = mode;
2706 else {
2707 WARN_ONCE(true, "Could not find initial preferred mode.\n");
2708 *p_mode = list_first_entry(&con->modes,
2709 struct drm_display_mode,
2710 head);
2711 }
2712
2713 return 0;
2714}
75c06855
TH
2715
2716/**
2717 * vmw_kms_del_active - unregister a crtc binding to the implicit framebuffer
2718 *
2719 * @dev_priv: Pointer to a device private struct.
2720 * @du: The display unit of the crtc.
2721 */
2722void vmw_kms_del_active(struct vmw_private *dev_priv,
2723 struct vmw_display_unit *du)
2724{
93cd1681 2725 mutex_lock(&dev_priv->global_kms_state_mutex);
75c06855
TH
2726 if (du->active_implicit) {
2727 if (--(dev_priv->num_implicit) == 0)
2728 dev_priv->implicit_fb = NULL;
2729 du->active_implicit = false;
2730 }
93cd1681 2731 mutex_unlock(&dev_priv->global_kms_state_mutex);
75c06855
TH
2732}
2733
2734/**
2735 * vmw_kms_add_active - register a crtc binding to an implicit framebuffer
2736 *
2737 * @vmw_priv: Pointer to a device private struct.
2738 * @du: The display unit of the crtc.
2739 * @vfb: The implicit framebuffer
2740 *
2741 * Registers a binding to an implicit framebuffer.
2742 */
2743void vmw_kms_add_active(struct vmw_private *dev_priv,
2744 struct vmw_display_unit *du,
2745 struct vmw_framebuffer *vfb)
2746{
93cd1681 2747 mutex_lock(&dev_priv->global_kms_state_mutex);
75c06855
TH
2748 WARN_ON_ONCE(!dev_priv->num_implicit && dev_priv->implicit_fb);
2749
2750 if (!du->active_implicit && du->is_implicit) {
2751 dev_priv->implicit_fb = vfb;
2752 du->active_implicit = true;
2753 dev_priv->num_implicit++;
2754 }
93cd1681 2755 mutex_unlock(&dev_priv->global_kms_state_mutex);
75c06855
TH
2756}
2757
2758/**
2759 * vmw_kms_screen_object_flippable - Check whether we can page-flip a crtc.
2760 *
2761 * @dev_priv: Pointer to device-private struct.
2762 * @crtc: The crtc we want to flip.
2763 *
2764 * Returns true or false depending whether it's OK to flip this crtc
2765 * based on the criterion that we must not have more than one implicit
2766 * frame-buffer at any one time.
2767 */
2768bool vmw_kms_crtc_flippable(struct vmw_private *dev_priv,
2769 struct drm_crtc *crtc)
2770{
2771 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
93cd1681 2772 bool ret;
75c06855 2773
93cd1681
TH
2774 mutex_lock(&dev_priv->global_kms_state_mutex);
2775 ret = !du->is_implicit || dev_priv->num_implicit == 1;
2776 mutex_unlock(&dev_priv->global_kms_state_mutex);
75c06855 2777
93cd1681 2778 return ret;
75c06855
TH
2779}
2780
2781/**
2782 * vmw_kms_update_implicit_fb - Update the implicit fb.
2783 *
2784 * @dev_priv: Pointer to device-private struct.
2785 * @crtc: The crtc the new implicit frame-buffer is bound to.
2786 */
2787void vmw_kms_update_implicit_fb(struct vmw_private *dev_priv,
2788 struct drm_crtc *crtc)
2789{
2790 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
2791 struct vmw_framebuffer *vfb;
2792
93cd1681 2793 mutex_lock(&dev_priv->global_kms_state_mutex);
75c06855
TH
2794
2795 if (!du->is_implicit)
93cd1681 2796 goto out_unlock;
75c06855
TH
2797
2798 vfb = vmw_framebuffer_to_vfb(crtc->primary->fb);
2799 WARN_ON_ONCE(dev_priv->num_implicit != 1 &&
2800 dev_priv->implicit_fb != vfb);
2801
2802 dev_priv->implicit_fb = vfb;
93cd1681
TH
2803out_unlock:
2804 mutex_unlock(&dev_priv->global_kms_state_mutex);
75c06855 2805}
76404ac0
TH
2806
2807/**
2808 * vmw_kms_create_implicit_placement_proparty - Set up the implicit placement
2809 * property.
2810 *
2811 * @dev_priv: Pointer to a device private struct.
2812 * @immutable: Whether the property is immutable.
2813 *
2814 * Sets up the implicit placement property unless it's already set up.
2815 */
2816void
2817vmw_kms_create_implicit_placement_property(struct vmw_private *dev_priv,
2818 bool immutable)
2819{
2820 if (dev_priv->implicit_placement_property)
2821 return;
2822
2823 dev_priv->implicit_placement_property =
2824 drm_property_create_range(dev_priv->dev,
2825 immutable ?
2826 DRM_MODE_PROP_IMMUTABLE : 0,
2827 "implicit_placement", 0, 1);
2828
2829}
904bb5e5
SY
2830
2831
2832/**
2833 * vmw_kms_set_config - Wrapper around drm_atomic_helper_set_config
2834 *
2835 * @set: The configuration to set.
2836 *
2837 * The vmwgfx Xorg driver doesn't assign the mode::type member, which
2838 * when drm_mode_set_crtcinfo is called as part of the configuration setting
2839 * causes it to return incorrect crtc dimensions causing severe problems in
2840 * the vmwgfx modesetting. So explicitly clear that member before calling
2841 * into drm_atomic_helper_set_config.
2842 */
320d8c3d
DA
2843int vmw_kms_set_config(struct drm_mode_set *set,
2844 struct drm_modeset_acquire_ctx *ctx)
904bb5e5
SY
2845{
2846 if (set && set->mode)
2847 set->mode->type = 0;
2848
320d8c3d 2849 return drm_atomic_helper_set_config(set, ctx);
904bb5e5 2850}