Merge branch 'drm-next-4.17' of git://people.freedesktop.org/~agd5f/linux into drm...
[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
060e2ad5
SY
396 if (vps->surf) {
397 du->cursor_age = du->cursor_surface->snooper.age;
398
399 ret = vmw_cursor_update_image(dev_priv,
400 vps->surf->snooper.image,
25db8754
TH
401 64, 64, hotspot_x,
402 hotspot_y);
060e2ad5
SY
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
25db8754
TH
500 struct drm_rect src = drm_plane_state_src(new_state);
501 struct drm_rect dest = drm_plane_state_dest(new_state);
060e2ad5
SY
502
503 /* Turning off */
504 if (!fb)
505 return ret;
506
25db8754
TH
507 ret = drm_plane_helper_check_update(plane, new_state->crtc, fb,
508 &src, &dest,
509 DRM_MODE_ROTATE_0,
510 DRM_PLANE_HELPER_NO_SCALING,
511 DRM_PLANE_HELPER_NO_SCALING,
512 true, true, &new_state->visible);
513 if (!ret)
514 return ret;
515
060e2ad5
SY
516 /* A lot of the code assumes this */
517 if (new_state->crtc_w != 64 || new_state->crtc_h != 64) {
518 DRM_ERROR("Invalid cursor dimensions (%d, %d)\n",
519 new_state->crtc_w, new_state->crtc_h);
520 ret = -EINVAL;
521 }
522
523 if (!vmw_framebuffer_to_vfb(fb)->dmabuf)
524 surface = vmw_framebuffer_to_vfbs(fb)->surface;
525
526 if (surface && !surface->snooper.image) {
527 DRM_ERROR("surface not suitable for cursor\n");
528 ret = -EINVAL;
529 }
530
531 return ret;
532}
533
534
06ec4190
SY
535int vmw_du_crtc_atomic_check(struct drm_crtc *crtc,
536 struct drm_crtc_state *new_state)
537{
538 struct vmw_display_unit *du = vmw_crtc_to_du(new_state->crtc);
539 int connector_mask = 1 << drm_connector_index(&du->connector);
540 bool has_primary = new_state->plane_mask &
541 BIT(drm_plane_index(crtc->primary));
542
543 /* We always want to have an active plane with an active CRTC */
544 if (has_primary != new_state->enable)
545 return -EINVAL;
546
547
548 if (new_state->connector_mask != connector_mask &&
549 new_state->connector_mask != 0) {
550 DRM_ERROR("Invalid connectors configuration\n");
551 return -EINVAL;
552 }
553
554 /*
555 * Our virtual device does not have a dot clock, so use the logical
556 * clock value as the dot clock.
557 */
558 if (new_state->mode.crtc_clock == 0)
559 new_state->adjusted_mode.crtc_clock = new_state->mode.clock;
560
561 return 0;
562}
563
564
565void vmw_du_crtc_atomic_begin(struct drm_crtc *crtc,
566 struct drm_crtc_state *old_crtc_state)
567{
568}
569
570
571void vmw_du_crtc_atomic_flush(struct drm_crtc *crtc,
572 struct drm_crtc_state *old_crtc_state)
573{
574 struct drm_pending_vblank_event *event = crtc->state->event;
575
576 if (event) {
577 crtc->state->event = NULL;
578
579 spin_lock_irq(&crtc->dev->event_lock);
3cbe87fc 580 drm_crtc_send_vblank_event(crtc, event);
06ec4190
SY
581 spin_unlock_irq(&crtc->dev->event_lock);
582 }
06ec4190
SY
583}
584
585
9c2542a4
SY
586/**
587 * vmw_du_crtc_duplicate_state - duplicate crtc state
588 * @crtc: DRM crtc
589 *
590 * Allocates and returns a copy of the crtc state (both common and
591 * vmw-specific) for the specified crtc.
592 *
593 * Returns: The newly allocated crtc state, or NULL on failure.
594 */
595struct drm_crtc_state *
596vmw_du_crtc_duplicate_state(struct drm_crtc *crtc)
597{
598 struct drm_crtc_state *state;
599 struct vmw_crtc_state *vcs;
600
601 if (WARN_ON(!crtc->state))
602 return NULL;
603
604 vcs = kmemdup(crtc->state, sizeof(*vcs), GFP_KERNEL);
605
606 if (!vcs)
607 return NULL;
608
609 state = &vcs->base;
610
611 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
612
613 return state;
614}
615
616
617/**
618 * vmw_du_crtc_reset - creates a blank vmw crtc state
619 * @crtc: DRM crtc
620 *
621 * Resets the atomic state for @crtc by freeing the state pointer (which
622 * might be NULL, e.g. at driver load time) and allocating a new empty state
623 * object.
624 */
625void vmw_du_crtc_reset(struct drm_crtc *crtc)
626{
627 struct vmw_crtc_state *vcs;
628
629
630 if (crtc->state) {
631 __drm_atomic_helper_crtc_destroy_state(crtc->state);
632
633 kfree(vmw_crtc_state_to_vcs(crtc->state));
634 }
635
636 vcs = kzalloc(sizeof(*vcs), GFP_KERNEL);
637
638 if (!vcs) {
639 DRM_ERROR("Cannot allocate vmw_crtc_state\n");
640 return;
641 }
642
643 crtc->state = &vcs->base;
644 crtc->state->crtc = crtc;
645}
646
647
648/**
649 * vmw_du_crtc_destroy_state - destroy crtc state
650 * @crtc: DRM crtc
651 * @state: state object to destroy
652 *
653 * Destroys the crtc state (both common and vmw-specific) for the
654 * specified plane.
655 */
656void
657vmw_du_crtc_destroy_state(struct drm_crtc *crtc,
658 struct drm_crtc_state *state)
659{
660 drm_atomic_helper_crtc_destroy_state(crtc, state);
661}
662
663
cc5ec459
SY
664/**
665 * vmw_du_plane_duplicate_state - duplicate plane state
666 * @plane: drm plane
667 *
668 * Allocates and returns a copy of the plane state (both common and
669 * vmw-specific) for the specified plane.
670 *
671 * Returns: The newly allocated plane state, or NULL on failure.
672 */
673struct drm_plane_state *
674vmw_du_plane_duplicate_state(struct drm_plane *plane)
675{
676 struct drm_plane_state *state;
677 struct vmw_plane_state *vps;
678
679 vps = kmemdup(plane->state, sizeof(*vps), GFP_KERNEL);
680
681 if (!vps)
682 return NULL;
683
684 vps->pinned = 0;
810b3e16
SY
685 vps->cpp = 0;
686
cc5ec459
SY
687 /* Each ref counted resource needs to be acquired again */
688 if (vps->surf)
689 (void) vmw_surface_reference(vps->surf);
690
691 if (vps->dmabuf)
692 (void) vmw_dmabuf_reference(vps->dmabuf);
693
694 state = &vps->base;
695
696 __drm_atomic_helper_plane_duplicate_state(plane, state);
697
698 return state;
699}
700
701
702/**
703 * vmw_du_plane_reset - creates a blank vmw plane state
704 * @plane: drm plane
705 *
706 * Resets the atomic state for @plane by freeing the state pointer (which might
707 * be NULL, e.g. at driver load time) and allocating a new empty state object.
708 */
709void vmw_du_plane_reset(struct drm_plane *plane)
710{
711 struct vmw_plane_state *vps;
712
713
714 if (plane->state)
715 vmw_du_plane_destroy_state(plane, plane->state);
716
717 vps = kzalloc(sizeof(*vps), GFP_KERNEL);
718
719 if (!vps) {
720 DRM_ERROR("Cannot allocate vmw_plane_state\n");
721 return;
722 }
723
724 plane->state = &vps->base;
725 plane->state->plane = plane;
c2c446ad 726 plane->state->rotation = DRM_MODE_ROTATE_0;
cc5ec459
SY
727}
728
729
730/**
731 * vmw_du_plane_destroy_state - destroy plane state
732 * @plane: DRM plane
733 * @state: state object to destroy
734 *
735 * Destroys the plane state (both common and vmw-specific) for the
736 * specified plane.
737 */
738void
739vmw_du_plane_destroy_state(struct drm_plane *plane,
740 struct drm_plane_state *state)
741{
742 struct vmw_plane_state *vps = vmw_plane_state_to_vps(state);
743
744
810b3e16 745 /* Should have been freed by cleanup_fb */
cc5ec459
SY
746 if (vps->surf)
747 vmw_surface_unreference(&vps->surf);
748
749 if (vps->dmabuf)
750 vmw_dmabuf_unreference(&vps->dmabuf);
751
752 drm_atomic_helper_plane_destroy_state(plane, state);
753}
754
755
d7721ca7
SY
756/**
757 * vmw_du_connector_duplicate_state - duplicate connector state
758 * @connector: DRM connector
759 *
760 * Allocates and returns a copy of the connector state (both common and
761 * vmw-specific) for the specified connector.
762 *
763 * Returns: The newly allocated connector state, or NULL on failure.
764 */
765struct drm_connector_state *
766vmw_du_connector_duplicate_state(struct drm_connector *connector)
767{
768 struct drm_connector_state *state;
769 struct vmw_connector_state *vcs;
770
771 if (WARN_ON(!connector->state))
772 return NULL;
773
774 vcs = kmemdup(connector->state, sizeof(*vcs), GFP_KERNEL);
775
776 if (!vcs)
777 return NULL;
778
779 state = &vcs->base;
780
781 __drm_atomic_helper_connector_duplicate_state(connector, state);
782
783 return state;
784}
785
786
787/**
788 * vmw_du_connector_reset - creates a blank vmw connector state
789 * @connector: DRM connector
790 *
791 * Resets the atomic state for @connector by freeing the state pointer (which
792 * might be NULL, e.g. at driver load time) and allocating a new empty state
793 * object.
794 */
795void vmw_du_connector_reset(struct drm_connector *connector)
796{
797 struct vmw_connector_state *vcs;
798
799
800 if (connector->state) {
801 __drm_atomic_helper_connector_destroy_state(connector->state);
802
803 kfree(vmw_connector_state_to_vcs(connector->state));
804 }
805
806 vcs = kzalloc(sizeof(*vcs), GFP_KERNEL);
807
808 if (!vcs) {
809 DRM_ERROR("Cannot allocate vmw_connector_state\n");
810 return;
811 }
812
813 __drm_atomic_helper_connector_reset(connector, &vcs->base);
814}
815
816
817/**
818 * vmw_du_connector_destroy_state - destroy connector state
819 * @connector: DRM connector
820 * @state: state object to destroy
821 *
822 * Destroys the connector state (both common and vmw-specific) for the
823 * specified plane.
824 */
825void
826vmw_du_connector_destroy_state(struct drm_connector *connector,
827 struct drm_connector_state *state)
828{
829 drm_atomic_helper_connector_destroy_state(connector, state);
830}
fb1d9738
JB
831/*
832 * Generic framebuffer code
833 */
834
fb1d9738
JB
835/*
836 * Surface framebuffer code
837 */
838
847c5964 839static void vmw_framebuffer_surface_destroy(struct drm_framebuffer *framebuffer)
fb1d9738 840{
3a939a5e 841 struct vmw_framebuffer_surface *vfbs =
fb1d9738 842 vmw_framebuffer_to_vfbs(framebuffer);
3a939a5e 843
fb1d9738 844 drm_framebuffer_cleanup(framebuffer);
3a939a5e 845 vmw_surface_unreference(&vfbs->surface);
a278724a
TH
846 if (vfbs->base.user_obj)
847 ttm_base_object_unref(&vfbs->base.user_obj);
fb1d9738 848
3a939a5e 849 kfree(vfbs);
fb1d9738
JB
850}
851
847c5964 852static int vmw_framebuffer_surface_dirty(struct drm_framebuffer *framebuffer,
02b00162 853 struct drm_file *file_priv,
fb1d9738
JB
854 unsigned flags, unsigned color,
855 struct drm_clip_rect *clips,
856 unsigned num_clips)
857{
858 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
859 struct vmw_framebuffer_surface *vfbs =
860 vmw_framebuffer_to_vfbs(framebuffer);
fb1d9738 861 struct drm_clip_rect norect;
5deb65cf 862 int ret, inc = 1;
fb1d9738 863
c8261a96
SY
864 /* Legacy Display Unit does not support 3D */
865 if (dev_priv->active_display_unit == vmw_du_legacy)
01e81419
JB
866 return -EINVAL;
867
73e9efd4
VS
868 drm_modeset_lock_all(dev_priv->dev);
869
294adf7d 870 ret = ttm_read_lock(&dev_priv->reservation_sem, true);
73e9efd4
VS
871 if (unlikely(ret != 0)) {
872 drm_modeset_unlock_all(dev_priv->dev);
3a939a5e 873 return ret;
73e9efd4 874 }
3a939a5e 875
fb1d9738
JB
876 if (!num_clips) {
877 num_clips = 1;
878 clips = &norect;
879 norect.x1 = norect.y1 = 0;
880 norect.x2 = framebuffer->width;
881 norect.y2 = framebuffer->height;
882 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
883 num_clips /= 2;
884 inc = 2; /* skip source rects */
885 }
886
c8261a96 887 if (dev_priv->active_display_unit == vmw_du_screen_object)
10b1e0ca
TH
888 ret = vmw_kms_sou_do_surface_dirty(dev_priv, &vfbs->base,
889 clips, NULL, NULL, 0, 0,
91e9f352 890 num_clips, inc, NULL, NULL);
35c05125 891 else
6bf6bf03
TH
892 ret = vmw_kms_stdu_surface_dirty(dev_priv, &vfbs->base,
893 clips, NULL, NULL, 0, 0,
91e9f352 894 num_clips, inc, NULL, NULL);
fb1d9738 895
3eab3d9e 896 vmw_fifo_flush(dev_priv, false);
294adf7d 897 ttm_read_unlock(&dev_priv->reservation_sem);
73e9efd4
VS
898
899 drm_modeset_unlock_all(dev_priv->dev);
900
fb1d9738
JB
901 return 0;
902}
903
10b1e0ca
TH
904/**
905 * vmw_kms_readback - Perform a readback from the screen system to
906 * a dma-buffer backed framebuffer.
907 *
908 * @dev_priv: Pointer to the device private structure.
909 * @file_priv: Pointer to a struct drm_file identifying the caller.
910 * Must be set to NULL if @user_fence_rep is NULL.
911 * @vfb: Pointer to the dma-buffer backed framebuffer.
912 * @user_fence_rep: User-space provided structure for fence information.
913 * Must be set to non-NULL if @file_priv is non-NULL.
914 * @vclips: Array of clip rects.
915 * @num_clips: Number of clip rects in @vclips.
916 *
917 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
918 * interrupted.
919 */
920int vmw_kms_readback(struct vmw_private *dev_priv,
921 struct drm_file *file_priv,
922 struct vmw_framebuffer *vfb,
923 struct drm_vmw_fence_rep __user *user_fence_rep,
924 struct drm_vmw_rect *vclips,
925 uint32_t num_clips)
926{
927 switch (dev_priv->active_display_unit) {
928 case vmw_du_screen_object:
929 return vmw_kms_sou_readback(dev_priv, file_priv, vfb,
91e9f352
DR
930 user_fence_rep, vclips, num_clips,
931 NULL);
6bf6bf03
TH
932 case vmw_du_screen_target:
933 return vmw_kms_stdu_dma(dev_priv, file_priv, vfb,
934 user_fence_rep, NULL, vclips, num_clips,
91e9f352 935 1, false, true, NULL);
10b1e0ca
TH
936 default:
937 WARN_ONCE(true,
938 "Readback called with invalid display system.\n");
6bf6bf03 939}
10b1e0ca
TH
940
941 return -ENOSYS;
942}
943
944
d7955fcf 945static const struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs = {
fb1d9738
JB
946 .destroy = vmw_framebuffer_surface_destroy,
947 .dirty = vmw_framebuffer_surface_dirty,
fb1d9738
JB
948};
949
d3216a0c
TH
950static int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv,
951 struct vmw_surface *surface,
952 struct vmw_framebuffer **out,
dabdcdc9 953 const struct drm_mode_fb_cmd2
f89c6c32
SY
954 *mode_cmd,
955 bool is_dmabuf_proxy)
fb1d9738
JB
956
957{
958 struct drm_device *dev = dev_priv->dev;
959 struct vmw_framebuffer_surface *vfbs;
d3216a0c 960 enum SVGA3dSurfaceFormat format;
fb1d9738 961 int ret;
dabdcdc9 962 struct drm_format_name_buf format_name;
fb1d9738 963
c8261a96
SY
964 /* 3D is only supported on HWv8 and newer hosts */
965 if (dev_priv->active_display_unit == vmw_du_legacy)
01e81419
JB
966 return -ENOSYS;
967
d3216a0c
TH
968 /*
969 * Sanity checks.
970 */
971
e7ac9211
JB
972 /* Surface must be marked as a scanout. */
973 if (unlikely(!surface->scanout))
974 return -EINVAL;
975
d3216a0c
TH
976 if (unlikely(surface->mip_levels[0] != 1 ||
977 surface->num_sizes != 1 ||
b360a3ce
TH
978 surface->base_size.width < mode_cmd->width ||
979 surface->base_size.height < mode_cmd->height ||
980 surface->base_size.depth != 1)) {
d3216a0c
TH
981 DRM_ERROR("Incompatible surface dimensions "
982 "for requested mode.\n");
983 return -EINVAL;
984 }
985
dabdcdc9
DV
986 switch (mode_cmd->pixel_format) {
987 case DRM_FORMAT_ARGB8888:
d3216a0c
TH
988 format = SVGA3D_A8R8G8B8;
989 break;
dabdcdc9 990 case DRM_FORMAT_XRGB8888:
d3216a0c
TH
991 format = SVGA3D_X8R8G8B8;
992 break;
dabdcdc9 993 case DRM_FORMAT_RGB565:
d3216a0c
TH
994 format = SVGA3D_R5G6B5;
995 break;
dabdcdc9 996 case DRM_FORMAT_XRGB1555:
d3216a0c
TH
997 format = SVGA3D_A1R5G5B5;
998 break;
999 default:
dabdcdc9
DV
1000 DRM_ERROR("Invalid pixel format: %s\n",
1001 drm_get_format_name(mode_cmd->pixel_format, &format_name));
d3216a0c
TH
1002 return -EINVAL;
1003 }
1004
d80efd5c
TH
1005 /*
1006 * For DX, surface format validation is done when surface->scanout
1007 * is set.
1008 */
1009 if (!dev_priv->has_dx && format != surface->format) {
d3216a0c
TH
1010 DRM_ERROR("Invalid surface format for requested mode.\n");
1011 return -EINVAL;
1012 }
1013
fb1d9738
JB
1014 vfbs = kzalloc(sizeof(*vfbs), GFP_KERNEL);
1015 if (!vfbs) {
1016 ret = -ENOMEM;
1017 goto out_err1;
1018 }
1019
a3f913ca 1020 drm_helper_mode_fill_fb_struct(dev, &vfbs->base.base, mode_cmd);
05c95018 1021 vfbs->surface = vmw_surface_reference(surface);
dabdcdc9 1022 vfbs->base.user_handle = mode_cmd->handles[0];
f89c6c32 1023 vfbs->is_dmabuf_proxy = is_dmabuf_proxy;
3a939a5e 1024
fb1d9738
JB
1025 *out = &vfbs->base;
1026
80f0b5af
DV
1027 ret = drm_framebuffer_init(dev, &vfbs->base.base,
1028 &vmw_framebuffer_surface_funcs);
1029 if (ret)
05c95018 1030 goto out_err2;
80f0b5af 1031
fb1d9738
JB
1032 return 0;
1033
fb1d9738 1034out_err2:
05c95018 1035 vmw_surface_unreference(&surface);
fb1d9738
JB
1036 kfree(vfbs);
1037out_err1:
1038 return ret;
1039}
1040
1041/*
1042 * Dmabuf framebuffer code
1043 */
1044
847c5964 1045static void vmw_framebuffer_dmabuf_destroy(struct drm_framebuffer *framebuffer)
fb1d9738
JB
1046{
1047 struct vmw_framebuffer_dmabuf *vfbd =
1048 vmw_framebuffer_to_vfbd(framebuffer);
1049
1050 drm_framebuffer_cleanup(framebuffer);
1051 vmw_dmabuf_unreference(&vfbd->buffer);
a278724a
TH
1052 if (vfbd->base.user_obj)
1053 ttm_base_object_unref(&vfbd->base.user_obj);
fb1d9738
JB
1054
1055 kfree(vfbd);
1056}
1057
847c5964 1058static int vmw_framebuffer_dmabuf_dirty(struct drm_framebuffer *framebuffer,
02b00162 1059 struct drm_file *file_priv,
fb1d9738
JB
1060 unsigned flags, unsigned color,
1061 struct drm_clip_rect *clips,
1062 unsigned num_clips)
1063{
1064 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
5deb65cf
JB
1065 struct vmw_framebuffer_dmabuf *vfbd =
1066 vmw_framebuffer_to_vfbd(framebuffer);
fb1d9738 1067 struct drm_clip_rect norect;
5deb65cf 1068 int ret, increment = 1;
fb1d9738 1069
73e9efd4
VS
1070 drm_modeset_lock_all(dev_priv->dev);
1071
294adf7d 1072 ret = ttm_read_lock(&dev_priv->reservation_sem, true);
73e9efd4
VS
1073 if (unlikely(ret != 0)) {
1074 drm_modeset_unlock_all(dev_priv->dev);
3a939a5e 1075 return ret;
73e9efd4 1076 }
3a939a5e 1077
df1c93ba 1078 if (!num_clips) {
fb1d9738
JB
1079 num_clips = 1;
1080 clips = &norect;
1081 norect.x1 = norect.y1 = 0;
1082 norect.x2 = framebuffer->width;
1083 norect.y2 = framebuffer->height;
1084 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
1085 num_clips /= 2;
1086 increment = 2;
1087 }
1088
6bf6bf03
TH
1089 switch (dev_priv->active_display_unit) {
1090 case vmw_du_screen_target:
1091 ret = vmw_kms_stdu_dma(dev_priv, NULL, &vfbd->base, NULL,
1092 clips, NULL, num_clips, increment,
91e9f352 1093 true, true, NULL);
6bf6bf03
TH
1094 break;
1095 case vmw_du_screen_object:
10b1e0ca 1096 ret = vmw_kms_sou_do_dmabuf_dirty(dev_priv, &vfbd->base,
897b8180 1097 clips, NULL, num_clips,
91e9f352 1098 increment, true, NULL, NULL);
6bf6bf03 1099 break;
352b20dc
TH
1100 case vmw_du_legacy:
1101 ret = vmw_kms_ldu_do_dmabuf_dirty(dev_priv, &vfbd->base, 0, 0,
1102 clips, num_clips, increment);
1103 break;
6bf6bf03 1104 default:
352b20dc
TH
1105 ret = -EINVAL;
1106 WARN_ONCE(true, "Dirty called with invalid display system.\n");
6bf6bf03 1107 break;
56d1c78d 1108 }
fb1d9738 1109
3eab3d9e 1110 vmw_fifo_flush(dev_priv, false);
294adf7d 1111 ttm_read_unlock(&dev_priv->reservation_sem);
73e9efd4
VS
1112
1113 drm_modeset_unlock_all(dev_priv->dev);
1114
5deb65cf 1115 return ret;
fb1d9738
JB
1116}
1117
d7955fcf 1118static const struct drm_framebuffer_funcs vmw_framebuffer_dmabuf_funcs = {
fb1d9738
JB
1119 .destroy = vmw_framebuffer_dmabuf_destroy,
1120 .dirty = vmw_framebuffer_dmabuf_dirty,
fb1d9738
JB
1121};
1122
497a3ff9 1123/**
ef86cfee
TH
1124 * Pin the dmabuffer in a location suitable for access by the
1125 * display system.
497a3ff9 1126 */
fd006a43 1127static int vmw_framebuffer_pin(struct vmw_framebuffer *vfb)
fb1d9738
JB
1128{
1129 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
fd006a43 1130 struct vmw_dma_buffer *buf;
ef86cfee 1131 struct ttm_placement *placement;
fb1d9738
JB
1132 int ret;
1133
fd006a43
TH
1134 buf = vfb->dmabuf ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
1135 vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
fb1d9738 1136
fd006a43
TH
1137 if (!buf)
1138 return 0;
fb1d9738 1139
fd006a43
TH
1140 switch (dev_priv->active_display_unit) {
1141 case vmw_du_legacy:
1142 vmw_overlay_pause_all(dev_priv);
1143 ret = vmw_dmabuf_pin_in_start_of_vram(dev_priv, buf, false);
1144 vmw_overlay_resume_all(dev_priv);
1145 break;
1146 case vmw_du_screen_object:
1147 case vmw_du_screen_target:
ef86cfee
TH
1148 if (vfb->dmabuf) {
1149 if (dev_priv->capabilities & SVGA_CAP_3D) {
1150 /*
1151 * Use surface DMA to get content to
1152 * sreen target surface.
1153 */
1154 placement = &vmw_vram_gmr_placement;
1155 } else {
1156 /* Use CPU blit. */
1157 placement = &vmw_sys_placement;
1158 }
1159 } else {
1160 /* Use surface / image update */
1161 placement = &vmw_mob_placement;
1162 }
fb1d9738 1163
ef86cfee
TH
1164 return vmw_dmabuf_pin_in_placement(dev_priv, buf, placement,
1165 false);
fd006a43
TH
1166 default:
1167 return -EINVAL;
1168 }
316ab13a 1169
fd006a43 1170 return ret;
fb1d9738
JB
1171}
1172
fd006a43 1173static int vmw_framebuffer_unpin(struct vmw_framebuffer *vfb)
fb1d9738
JB
1174{
1175 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
fd006a43 1176 struct vmw_dma_buffer *buf;
fb1d9738 1177
fd006a43
TH
1178 buf = vfb->dmabuf ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
1179 vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
fb1d9738 1180
fd006a43 1181 if (WARN_ON(!buf))
fb1d9738 1182 return 0;
fb1d9738 1183
fd006a43 1184 return vmw_dmabuf_unpin(dev_priv, buf, false);
fb1d9738
JB
1185}
1186
f89c6c32
SY
1187/**
1188 * vmw_create_dmabuf_proxy - create a proxy surface for the DMA buf
1189 *
1190 * @dev: DRM device
1191 * @mode_cmd: parameters for the new surface
1192 * @dmabuf_mob: MOB backing the DMA buf
1193 * @srf_out: newly created surface
1194 *
1195 * When the content FB is a DMA buf, we create a surface as a proxy to the
1196 * same buffer. This way we can do a surface copy rather than a surface DMA.
1197 * This is a more efficient approach
1198 *
1199 * RETURNS:
1200 * 0 on success, error code otherwise
1201 */
1202static int vmw_create_dmabuf_proxy(struct drm_device *dev,
dabdcdc9 1203 const struct drm_mode_fb_cmd2 *mode_cmd,
f89c6c32
SY
1204 struct vmw_dma_buffer *dmabuf_mob,
1205 struct vmw_surface **srf_out)
1206{
1207 uint32_t format;
8cd9f251 1208 struct drm_vmw_size content_base_size = {0};
6bf6bf03 1209 struct vmw_resource *res;
a50e2bf5 1210 unsigned int bytes_pp;
dabdcdc9 1211 struct drm_format_name_buf format_name;
f89c6c32
SY
1212 int ret;
1213
dabdcdc9
DV
1214 switch (mode_cmd->pixel_format) {
1215 case DRM_FORMAT_ARGB8888:
1216 case DRM_FORMAT_XRGB8888:
f89c6c32 1217 format = SVGA3D_X8R8G8B8;
a50e2bf5 1218 bytes_pp = 4;
f89c6c32
SY
1219 break;
1220
dabdcdc9
DV
1221 case DRM_FORMAT_RGB565:
1222 case DRM_FORMAT_XRGB1555:
f89c6c32 1223 format = SVGA3D_R5G6B5;
a50e2bf5 1224 bytes_pp = 2;
f89c6c32
SY
1225 break;
1226
1227 case 8:
1228 format = SVGA3D_P8;
a50e2bf5 1229 bytes_pp = 1;
f89c6c32
SY
1230 break;
1231
1232 default:
dabdcdc9
DV
1233 DRM_ERROR("Invalid framebuffer format %s\n",
1234 drm_get_format_name(mode_cmd->pixel_format, &format_name));
f89c6c32
SY
1235 return -EINVAL;
1236 }
1237
dabdcdc9 1238 content_base_size.width = mode_cmd->pitches[0] / bytes_pp;
f89c6c32
SY
1239 content_base_size.height = mode_cmd->height;
1240 content_base_size.depth = 1;
1241
1242 ret = vmw_surface_gb_priv_define(dev,
1243 0, /* kernel visible only */
1244 0, /* flags */
1245 format,
1246 true, /* can be a scanout buffer */
1247 1, /* num of mip levels */
1248 0,
d80efd5c 1249 0,
f89c6c32
SY
1250 content_base_size,
1251 srf_out);
1252 if (ret) {
1253 DRM_ERROR("Failed to allocate proxy content buffer\n");
1254 return ret;
fb1d9738
JB
1255 }
1256
6bf6bf03 1257 res = &(*srf_out)->res;
f89c6c32 1258
6bf6bf03
TH
1259 /* Reserve and switch the backing mob. */
1260 mutex_lock(&res->dev_priv->cmdbuf_mutex);
1261 (void) vmw_resource_reserve(res, false, true);
1262 vmw_dmabuf_unreference(&res->backup);
1263 res->backup = vmw_dmabuf_reference(dmabuf_mob);
1264 res->backup_offset = 0;
d80efd5c 1265 vmw_resource_unreserve(res, false, NULL, 0);
6bf6bf03 1266 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
f89c6c32 1267
6bf6bf03 1268 return 0;
fb1d9738
JB
1269}
1270
f89c6c32
SY
1271
1272
d3216a0c
TH
1273static int vmw_kms_new_framebuffer_dmabuf(struct vmw_private *dev_priv,
1274 struct vmw_dma_buffer *dmabuf,
1275 struct vmw_framebuffer **out,
dabdcdc9 1276 const struct drm_mode_fb_cmd2
d3216a0c 1277 *mode_cmd)
fb1d9738
JB
1278
1279{
1280 struct drm_device *dev = dev_priv->dev;
1281 struct vmw_framebuffer_dmabuf *vfbd;
d3216a0c 1282 unsigned int requested_size;
dabdcdc9 1283 struct drm_format_name_buf format_name;
fb1d9738
JB
1284 int ret;
1285
dabdcdc9 1286 requested_size = mode_cmd->height * mode_cmd->pitches[0];
d3216a0c
TH
1287 if (unlikely(requested_size > dmabuf->base.num_pages * PAGE_SIZE)) {
1288 DRM_ERROR("Screen buffer object size is too small "
1289 "for requested mode.\n");
1290 return -EINVAL;
1291 }
1292
c337ada7 1293 /* Limited framebuffer color depth support for screen objects */
c8261a96 1294 if (dev_priv->active_display_unit == vmw_du_screen_object) {
dabdcdc9
DV
1295 switch (mode_cmd->pixel_format) {
1296 case DRM_FORMAT_XRGB8888:
1297 case DRM_FORMAT_ARGB8888:
1298 break;
1299 case DRM_FORMAT_XRGB1555:
1300 case DRM_FORMAT_RGB565:
1301 break;
c337ada7 1302 default:
dabdcdc9
DV
1303 DRM_ERROR("Invalid pixel format: %s\n",
1304 drm_get_format_name(mode_cmd->pixel_format, &format_name));
c337ada7
JB
1305 return -EINVAL;
1306 }
1307 }
1308
fb1d9738
JB
1309 vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL);
1310 if (!vfbd) {
1311 ret = -ENOMEM;
1312 goto out_err1;
1313 }
1314
a3f913ca 1315 drm_helper_mode_fill_fb_struct(dev, &vfbd->base.base, mode_cmd);
2fcd5a73 1316 vfbd->base.dmabuf = true;
05c95018 1317 vfbd->buffer = vmw_dmabuf_reference(dmabuf);
dabdcdc9 1318 vfbd->base.user_handle = mode_cmd->handles[0];
fb1d9738
JB
1319 *out = &vfbd->base;
1320
80f0b5af
DV
1321 ret = drm_framebuffer_init(dev, &vfbd->base.base,
1322 &vmw_framebuffer_dmabuf_funcs);
1323 if (ret)
05c95018 1324 goto out_err2;
80f0b5af 1325
fb1d9738
JB
1326 return 0;
1327
fb1d9738 1328out_err2:
05c95018 1329 vmw_dmabuf_unreference(&dmabuf);
fb1d9738
JB
1330 kfree(vfbd);
1331out_err1:
1332 return ret;
1333}
1334
810b3e16
SY
1335
1336/**
1337 * vmw_kms_srf_ok - check if a surface can be created
1338 *
1339 * @width: requested width
1340 * @height: requested height
1341 *
1342 * Surfaces need to be less than texture size
1343 */
1344static bool
1345vmw_kms_srf_ok(struct vmw_private *dev_priv, uint32_t width, uint32_t height)
1346{
1347 if (width > dev_priv->texture_max_width ||
1348 height > dev_priv->texture_max_height)
1349 return false;
1350
1351 return true;
1352}
1353
fd006a43
TH
1354/**
1355 * vmw_kms_new_framebuffer - Create a new framebuffer.
1356 *
1357 * @dev_priv: Pointer to device private struct.
1358 * @dmabuf: Pointer to dma buffer to wrap the kms framebuffer around.
1359 * Either @dmabuf or @surface must be NULL.
1360 * @surface: Pointer to a surface to wrap the kms framebuffer around.
1361 * Either @dmabuf or @surface must be NULL.
1362 * @only_2d: No presents will occur to this dma buffer based framebuffer. This
1363 * Helps the code to do some important optimizations.
1364 * @mode_cmd: Frame-buffer metadata.
fb1d9738 1365 */
fd006a43
TH
1366struct vmw_framebuffer *
1367vmw_kms_new_framebuffer(struct vmw_private *dev_priv,
1368 struct vmw_dma_buffer *dmabuf,
1369 struct vmw_surface *surface,
1370 bool only_2d,
dabdcdc9 1371 const struct drm_mode_fb_cmd2 *mode_cmd)
fb1d9738 1372{
fb1d9738 1373 struct vmw_framebuffer *vfb = NULL;
fd006a43 1374 bool is_dmabuf_proxy = false;
fb1d9738
JB
1375 int ret;
1376
fd006a43
TH
1377 /*
1378 * We cannot use the SurfaceDMA command in an non-accelerated VM,
1379 * therefore, wrap the DMA buf in a surface so we can use the
1380 * SurfaceCopy command.
1381 */
810b3e16
SY
1382 if (vmw_kms_srf_ok(dev_priv, mode_cmd->width, mode_cmd->height) &&
1383 dmabuf && only_2d &&
bbd5fefe 1384 mode_cmd->width > 64 && /* Don't create a proxy for cursor */
fd006a43
TH
1385 dev_priv->active_display_unit == vmw_du_screen_target) {
1386 ret = vmw_create_dmabuf_proxy(dev_priv->dev, mode_cmd,
1387 dmabuf, &surface);
1388 if (ret)
1389 return ERR_PTR(ret);
1390
1391 is_dmabuf_proxy = true;
1392 }
1393
1394 /* Create the new framebuffer depending one what we have */
05c95018 1395 if (surface) {
fd006a43
TH
1396 ret = vmw_kms_new_framebuffer_surface(dev_priv, surface, &vfb,
1397 mode_cmd,
1398 is_dmabuf_proxy);
05c95018
SY
1399
1400 /*
1401 * vmw_create_dmabuf_proxy() adds a reference that is no longer
1402 * needed
1403 */
1404 if (is_dmabuf_proxy)
1405 vmw_surface_unreference(&surface);
1406 } else if (dmabuf) {
fd006a43
TH
1407 ret = vmw_kms_new_framebuffer_dmabuf(dev_priv, dmabuf, &vfb,
1408 mode_cmd);
05c95018 1409 } else {
fd006a43 1410 BUG();
05c95018 1411 }
fd006a43
TH
1412
1413 if (ret)
1414 return ERR_PTR(ret);
1415
1416 vfb->pin = vmw_framebuffer_pin;
1417 vfb->unpin = vmw_framebuffer_unpin;
1418
1419 return vfb;
1420}
1421
fb1d9738
JB
1422/*
1423 * Generic Kernel modesetting functions
1424 */
1425
1426static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev,
1427 struct drm_file *file_priv,
dabdcdc9 1428 const struct drm_mode_fb_cmd2 *mode_cmd)
fb1d9738
JB
1429{
1430 struct vmw_private *dev_priv = vmw_priv(dev);
1431 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
1432 struct vmw_framebuffer *vfb = NULL;
1433 struct vmw_surface *surface = NULL;
1434 struct vmw_dma_buffer *bo = NULL;
90ff18bc 1435 struct ttm_base_object *user_obj;
fb1d9738
JB
1436 int ret;
1437
d3216a0c
TH
1438 /**
1439 * This code should be conditioned on Screen Objects not being used.
1440 * If screen objects are used, we can allocate a GMR to hold the
1441 * requested framebuffer.
1442 */
1443
8a783896 1444 if (!vmw_kms_validate_mode_vram(dev_priv,
dabdcdc9
DV
1445 mode_cmd->pitches[0],
1446 mode_cmd->height)) {
c8261a96 1447 DRM_ERROR("Requested mode exceed bounding box limit.\n");
d9826409 1448 return ERR_PTR(-ENOMEM);
d3216a0c
TH
1449 }
1450
90ff18bc
TH
1451 /*
1452 * Take a reference on the user object of the resource
1453 * backing the kms fb. This ensures that user-space handle
1454 * lookups on that resource will always work as long as
1455 * it's registered with a kms framebuffer. This is important,
1456 * since vmw_execbuf_process identifies resources in the
1457 * command stream using user-space handles.
1458 */
1459
dabdcdc9 1460 user_obj = ttm_base_object_lookup(tfile, mode_cmd->handles[0]);
90ff18bc
TH
1461 if (unlikely(user_obj == NULL)) {
1462 DRM_ERROR("Could not locate requested kms frame buffer.\n");
1463 return ERR_PTR(-ENOENT);
1464 }
1465
d3216a0c
TH
1466 /**
1467 * End conditioned code.
1468 */
1469
e7ac9211
JB
1470 /* returns either a dmabuf or surface */
1471 ret = vmw_user_lookup_handle(dev_priv, tfile,
dabdcdc9 1472 mode_cmd->handles[0],
e7ac9211 1473 &surface, &bo);
fb1d9738 1474 if (ret)
e7ac9211
JB
1475 goto err_out;
1476
810b3e16
SY
1477
1478 if (!bo &&
1479 !vmw_kms_srf_ok(dev_priv, mode_cmd->width, mode_cmd->height)) {
1480 DRM_ERROR("Surface size cannot exceed %dx%d",
1481 dev_priv->texture_max_width,
1482 dev_priv->texture_max_height);
1483 goto err_out;
1484 }
1485
1486
fd006a43
TH
1487 vfb = vmw_kms_new_framebuffer(dev_priv, bo, surface,
1488 !(dev_priv->capabilities & SVGA_CAP_3D),
dabdcdc9 1489 mode_cmd);
fd006a43
TH
1490 if (IS_ERR(vfb)) {
1491 ret = PTR_ERR(vfb);
1492 goto err_out;
1493 }
e7ac9211
JB
1494
1495err_out:
1496 /* vmw_user_lookup_handle takes one ref so does new_fb */
1497 if (bo)
1498 vmw_dmabuf_unreference(&bo);
1499 if (surface)
1500 vmw_surface_unreference(&surface);
fb1d9738
JB
1501
1502 if (ret) {
1503 DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
90ff18bc 1504 ttm_base_object_unref(&user_obj);
cce13ff7 1505 return ERR_PTR(ret);
90ff18bc
TH
1506 } else
1507 vfb->user_obj = user_obj;
fb1d9738
JB
1508
1509 return &vfb->base;
1510}
1511
c46a3064
SY
1512
1513
1514/**
1515 * vmw_kms_atomic_check_modeset- validate state object for modeset changes
1516 *
1517 * @dev: DRM device
1518 * @state: the driver state object
1519 *
1520 * This is a simple wrapper around drm_atomic_helper_check_modeset() for
1521 * us to assign a value to mode->crtc_clock so that
1522 * drm_calc_timestamping_constants() won't throw an error message
1523 *
1524 * RETURNS
1525 * Zero for success or -errno
1526 */
bdc362f6 1527static int
c46a3064
SY
1528vmw_kms_atomic_check_modeset(struct drm_device *dev,
1529 struct drm_atomic_state *state)
1530{
1531 struct drm_crtc_state *crtc_state;
1532 struct drm_crtc *crtc;
1533 struct vmw_private *dev_priv = vmw_priv(dev);
1534 int i;
1535
bdc362f6 1536 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
c46a3064
SY
1537 unsigned long requested_bb_mem = 0;
1538
1539 if (dev_priv->active_display_unit == vmw_du_screen_target) {
1540 if (crtc->primary->fb) {
1541 int cpp = crtc->primary->fb->pitches[0] /
1542 crtc->primary->fb->width;
1543
1544 requested_bb_mem += crtc->mode.hdisplay * cpp *
1545 crtc->mode.vdisplay;
1546 }
1547
1548 if (requested_bb_mem > dev_priv->prim_bb_mem)
1549 return -EINVAL;
1550 }
1551 }
1552
1553 return drm_atomic_helper_check(dev, state);
1554}
1555
e6ecefaa 1556static const struct drm_mode_config_funcs vmw_kms_funcs = {
fb1d9738 1557 .fb_create = vmw_kms_fb_create,
c46a3064 1558 .atomic_check = vmw_kms_atomic_check_modeset,
904efd9e 1559 .atomic_commit = drm_atomic_helper_commit,
fb1d9738
JB
1560};
1561
b9eb1a61
TH
1562static int vmw_kms_generic_present(struct vmw_private *dev_priv,
1563 struct drm_file *file_priv,
1564 struct vmw_framebuffer *vfb,
1565 struct vmw_surface *surface,
1566 uint32_t sid,
1567 int32_t destX, int32_t destY,
1568 struct drm_vmw_rect *clips,
1569 uint32_t num_clips)
2fcd5a73 1570{
10b1e0ca
TH
1571 return vmw_kms_sou_do_surface_dirty(dev_priv, vfb, NULL, clips,
1572 &surface->res, destX, destY,
91e9f352 1573 num_clips, 1, NULL, NULL);
2fcd5a73
JB
1574}
1575
6bf6bf03 1576
2fcd5a73
JB
1577int vmw_kms_present(struct vmw_private *dev_priv,
1578 struct drm_file *file_priv,
1579 struct vmw_framebuffer *vfb,
1580 struct vmw_surface *surface,
1581 uint32_t sid,
1582 int32_t destX, int32_t destY,
1583 struct drm_vmw_rect *clips,
1584 uint32_t num_clips)
1585{
35c05125 1586 int ret;
2fcd5a73 1587
6bf6bf03
TH
1588 switch (dev_priv->active_display_unit) {
1589 case vmw_du_screen_target:
1590 ret = vmw_kms_stdu_surface_dirty(dev_priv, vfb, NULL, clips,
1591 &surface->res, destX, destY,
91e9f352 1592 num_clips, 1, NULL, NULL);
6bf6bf03
TH
1593 break;
1594 case vmw_du_screen_object:
1595 ret = vmw_kms_generic_present(dev_priv, file_priv, vfb, surface,
1596 sid, destX, destY, clips,
1597 num_clips);
1598 break;
1599 default:
1600 WARN_ONCE(true,
1601 "Present called with invalid display system.\n");
1602 ret = -ENOSYS;
1603 break;
2fcd5a73 1604 }
35c05125
SY
1605 if (ret)
1606 return ret;
2fcd5a73 1607
35c05125 1608 vmw_fifo_flush(dev_priv, false);
2fcd5a73 1609
35c05125 1610 return 0;
2fcd5a73
JB
1611}
1612
578e609a
TH
1613static void
1614vmw_kms_create_hotplug_mode_update_property(struct vmw_private *dev_priv)
1615{
1616 if (dev_priv->hotplug_mode_update_property)
1617 return;
1618
1619 dev_priv->hotplug_mode_update_property =
1620 drm_property_create_range(dev_priv->dev,
1621 DRM_MODE_PROP_IMMUTABLE,
1622 "hotplug_mode_update", 0, 1);
1623
1624 if (!dev_priv->hotplug_mode_update_property)
1625 return;
1626
1627}
1628
fb1d9738
JB
1629int vmw_kms_init(struct vmw_private *dev_priv)
1630{
1631 struct drm_device *dev = dev_priv->dev;
1632 int ret;
1633
1634 drm_mode_config_init(dev);
1635 dev->mode_config.funcs = &vmw_kms_funcs;
3bef3572
JB
1636 dev->mode_config.min_width = 1;
1637 dev->mode_config.min_height = 1;
65ade7d3
SY
1638 dev->mode_config.max_width = dev_priv->texture_max_width;
1639 dev->mode_config.max_height = dev_priv->texture_max_height;
fb1d9738 1640
578e609a
TH
1641 drm_mode_create_suggested_offset_properties(dev);
1642 vmw_kms_create_hotplug_mode_update_property(dev_priv);
1643
35c05125
SY
1644 ret = vmw_kms_stdu_init_display(dev_priv);
1645 if (ret) {
1646 ret = vmw_kms_sou_init_display(dev_priv);
1647 if (ret) /* Fallback */
1648 ret = vmw_kms_ldu_init_display(dev_priv);
1649 }
fb1d9738 1650
c8261a96 1651 return ret;
fb1d9738
JB
1652}
1653
1654int vmw_kms_close(struct vmw_private *dev_priv)
1655{
5f58e974 1656 int ret = 0;
c8261a96 1657
fb1d9738
JB
1658 /*
1659 * Docs says we should take the lock before calling this function
1660 * but since it destroys encoders and our destructor calls
1661 * drm_encoder_cleanup which takes the lock we deadlock.
1662 */
1663 drm_mode_config_cleanup(dev_priv->dev);
5f58e974 1664 if (dev_priv->active_display_unit == vmw_du_legacy)
c8261a96
SY
1665 ret = vmw_kms_ldu_close_display(dev_priv);
1666
1667 return ret;
fb1d9738
JB
1668}
1669
1670int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
1671 struct drm_file *file_priv)
1672{
1673 struct drm_vmw_cursor_bypass_arg *arg = data;
1674 struct vmw_display_unit *du;
fb1d9738
JB
1675 struct drm_crtc *crtc;
1676 int ret = 0;
1677
1678
1679 mutex_lock(&dev->mode_config.mutex);
1680 if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
1681
1682 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1683 du = vmw_crtc_to_du(crtc);
1684 du->hotspot_x = arg->xhot;
1685 du->hotspot_y = arg->yhot;
1686 }
1687
1688 mutex_unlock(&dev->mode_config.mutex);
1689 return 0;
1690 }
1691
418da172 1692 crtc = drm_crtc_find(dev, file_priv, arg->crtc_id);
a4cd5d68 1693 if (!crtc) {
4ae87ff0 1694 ret = -ENOENT;
fb1d9738
JB
1695 goto out;
1696 }
1697
fb1d9738
JB
1698 du = vmw_crtc_to_du(crtc);
1699
1700 du->hotspot_x = arg->xhot;
1701 du->hotspot_y = arg->yhot;
1702
1703out:
1704 mutex_unlock(&dev->mode_config.mutex);
1705
1706 return ret;
1707}
1708
0bef23f9 1709int vmw_kms_write_svga(struct vmw_private *vmw_priv,
d7e1958d 1710 unsigned width, unsigned height, unsigned pitch,
6558429b 1711 unsigned bpp, unsigned depth)
fb1d9738 1712{
d7e1958d
JB
1713 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1714 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch);
1715 else if (vmw_fifo_have_pitchlock(vmw_priv))
b76ff5ea
TH
1716 vmw_mmio_write(pitch, vmw_priv->mmio_virt +
1717 SVGA_FIFO_PITCHLOCK);
d7e1958d
JB
1718 vmw_write(vmw_priv, SVGA_REG_WIDTH, width);
1719 vmw_write(vmw_priv, SVGA_REG_HEIGHT, height);
6558429b 1720 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp);
0bef23f9
MD
1721
1722 if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) {
1723 DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n",
1724 depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH));
1725 return -EINVAL;
1726 }
1727
1728 return 0;
d7e1958d 1729}
fb1d9738 1730
d7e1958d
JB
1731int vmw_kms_save_vga(struct vmw_private *vmw_priv)
1732{
7c4f7780
TH
1733 struct vmw_vga_topology_state *save;
1734 uint32_t i;
1735
fb1d9738
JB
1736 vmw_priv->vga_width = vmw_read(vmw_priv, SVGA_REG_WIDTH);
1737 vmw_priv->vga_height = vmw_read(vmw_priv, SVGA_REG_HEIGHT);
7c4f7780 1738 vmw_priv->vga_bpp = vmw_read(vmw_priv, SVGA_REG_BITS_PER_PIXEL);
d7e1958d
JB
1739 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1740 vmw_priv->vga_pitchlock =
7c4f7780 1741 vmw_read(vmw_priv, SVGA_REG_PITCHLOCK);
d7e1958d 1742 else if (vmw_fifo_have_pitchlock(vmw_priv))
b76ff5ea
TH
1743 vmw_priv->vga_pitchlock = vmw_mmio_read(vmw_priv->mmio_virt +
1744 SVGA_FIFO_PITCHLOCK);
7c4f7780
TH
1745
1746 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1747 return 0;
fb1d9738 1748
7c4f7780
TH
1749 vmw_priv->num_displays = vmw_read(vmw_priv,
1750 SVGA_REG_NUM_GUEST_DISPLAYS);
1751
029e50bf
TH
1752 if (vmw_priv->num_displays == 0)
1753 vmw_priv->num_displays = 1;
1754
7c4f7780
TH
1755 for (i = 0; i < vmw_priv->num_displays; ++i) {
1756 save = &vmw_priv->vga_save[i];
1757 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1758 save->primary = vmw_read(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY);
1759 save->pos_x = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_X);
1760 save->pos_y = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y);
1761 save->width = vmw_read(vmw_priv, SVGA_REG_DISPLAY_WIDTH);
1762 save->height = vmw_read(vmw_priv, SVGA_REG_DISPLAY_HEIGHT);
1763 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
30c78bb8
TH
1764 if (i == 0 && vmw_priv->num_displays == 1 &&
1765 save->width == 0 && save->height == 0) {
1766
1767 /*
1768 * It should be fairly safe to assume that these
1769 * values are uninitialized.
1770 */
1771
1772 save->width = vmw_priv->vga_width - save->pos_x;
1773 save->height = vmw_priv->vga_height - save->pos_y;
1774 }
7c4f7780 1775 }
30c78bb8 1776
fb1d9738
JB
1777 return 0;
1778}
1779
1780int vmw_kms_restore_vga(struct vmw_private *vmw_priv)
1781{
7c4f7780
TH
1782 struct vmw_vga_topology_state *save;
1783 uint32_t i;
1784
fb1d9738
JB
1785 vmw_write(vmw_priv, SVGA_REG_WIDTH, vmw_priv->vga_width);
1786 vmw_write(vmw_priv, SVGA_REG_HEIGHT, vmw_priv->vga_height);
7c4f7780 1787 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, vmw_priv->vga_bpp);
d7e1958d
JB
1788 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1789 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK,
1790 vmw_priv->vga_pitchlock);
1791 else if (vmw_fifo_have_pitchlock(vmw_priv))
b76ff5ea
TH
1792 vmw_mmio_write(vmw_priv->vga_pitchlock,
1793 vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
fb1d9738 1794
7c4f7780
TH
1795 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1796 return 0;
1797
1798 for (i = 0; i < vmw_priv->num_displays; ++i) {
1799 save = &vmw_priv->vga_save[i];
1800 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1801 vmw_write(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY, save->primary);
1802 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_X, save->pos_x);
1803 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y, save->pos_y);
1804 vmw_write(vmw_priv, SVGA_REG_DISPLAY_WIDTH, save->width);
1805 vmw_write(vmw_priv, SVGA_REG_DISPLAY_HEIGHT, save->height);
1806 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
1807 }
1808
fb1d9738
JB
1809 return 0;
1810}
d8bd19d2 1811
e133e737
TH
1812bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
1813 uint32_t pitch,
1814 uint32_t height)
1815{
35c05125
SY
1816 return ((u64) pitch * (u64) height) < (u64)
1817 ((dev_priv->active_display_unit == vmw_du_screen_target) ?
1818 dev_priv->prim_bb_mem : dev_priv->vram_size);
e133e737
TH
1819}
1820
1c482ab3
JB
1821
1822/**
1823 * Function called by DRM code called with vbl_lock held.
1824 */
88e72717 1825u32 vmw_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
7a1c2f6c
TH
1826{
1827 return 0;
1828}
626ab771 1829
1c482ab3
JB
1830/**
1831 * Function called by DRM code called with vbl_lock held.
1832 */
88e72717 1833int vmw_enable_vblank(struct drm_device *dev, unsigned int pipe)
1c482ab3 1834{
2b0bc68c 1835 return -EINVAL;
1c482ab3
JB
1836}
1837
1838/**
1839 * Function called by DRM code called with vbl_lock held.
1840 */
88e72717 1841void vmw_disable_vblank(struct drm_device *dev, unsigned int pipe)
1c482ab3
JB
1842{
1843}
1844
626ab771
JB
1845
1846/*
1847 * Small shared kms functions.
1848 */
1849
847c5964 1850static int vmw_du_update_layout(struct vmw_private *dev_priv, unsigned num,
626ab771
JB
1851 struct drm_vmw_rect *rects)
1852{
1853 struct drm_device *dev = dev_priv->dev;
1854 struct vmw_display_unit *du;
1855 struct drm_connector *con;
626ab771
JB
1856
1857 mutex_lock(&dev->mode_config.mutex);
1858
1859#if 0
6ea77d13
TH
1860 {
1861 unsigned int i;
1862
1863 DRM_INFO("%s: new layout ", __func__);
1864 for (i = 0; i < num; i++)
1865 DRM_INFO("(%i, %i %ux%u) ", rects[i].x, rects[i].y,
1866 rects[i].w, rects[i].h);
1867 DRM_INFO("\n");
1868 }
626ab771
JB
1869#endif
1870
1871 list_for_each_entry(con, &dev->mode_config.connector_list, head) {
1872 du = vmw_connector_to_du(con);
1873 if (num > du->unit) {
1874 du->pref_width = rects[du->unit].w;
1875 du->pref_height = rects[du->unit].h;
1876 du->pref_active = true;
cd2b89e7
TH
1877 du->gui_x = rects[du->unit].x;
1878 du->gui_y = rects[du->unit].y;
578e609a
TH
1879 drm_object_property_set_value
1880 (&con->base, dev->mode_config.suggested_x_property,
1881 du->gui_x);
1882 drm_object_property_set_value
1883 (&con->base, dev->mode_config.suggested_y_property,
1884 du->gui_y);
626ab771
JB
1885 } else {
1886 du->pref_width = 800;
1887 du->pref_height = 600;
1888 du->pref_active = false;
578e609a
TH
1889 drm_object_property_set_value
1890 (&con->base, dev->mode_config.suggested_x_property,
1891 0);
1892 drm_object_property_set_value
1893 (&con->base, dev->mode_config.suggested_y_property,
1894 0);
626ab771
JB
1895 }
1896 con->status = vmw_du_connector_detect(con, true);
1897 }
1898
1899 mutex_unlock(&dev->mode_config.mutex);
578e609a 1900 drm_sysfs_hotplug_event(dev);
626ab771
JB
1901
1902 return 0;
1903}
1904
7ea77283
ML
1905int vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
1906 u16 *r, u16 *g, u16 *b,
6d124ff8
DV
1907 uint32_t size,
1908 struct drm_modeset_acquire_ctx *ctx)
626ab771
JB
1909{
1910 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
1911 int i;
1912
1913 for (i = 0; i < size; i++) {
1914 DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i,
1915 r[i], g[i], b[i]);
1916 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 0, r[i] >> 8);
1917 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 1, g[i] >> 8);
1918 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 2, b[i] >> 8);
1919 }
7ea77283
ML
1920
1921 return 0;
626ab771
JB
1922}
1923
9a69a9ac 1924int vmw_du_connector_dpms(struct drm_connector *connector, int mode)
626ab771 1925{
9a69a9ac 1926 return 0;
626ab771
JB
1927}
1928
626ab771
JB
1929enum drm_connector_status
1930vmw_du_connector_detect(struct drm_connector *connector, bool force)
1931{
1932 uint32_t num_displays;
1933 struct drm_device *dev = connector->dev;
1934 struct vmw_private *dev_priv = vmw_priv(dev);
cd2b89e7 1935 struct vmw_display_unit *du = vmw_connector_to_du(connector);
626ab771 1936
626ab771 1937 num_displays = vmw_read(dev_priv, SVGA_REG_NUM_DISPLAYS);
626ab771 1938
cd2b89e7
TH
1939 return ((vmw_connector_to_du(connector)->unit < num_displays &&
1940 du->pref_active) ?
626ab771
JB
1941 connector_status_connected : connector_status_disconnected);
1942}
1943
1944static struct drm_display_mode vmw_kms_connector_builtin[] = {
1945 /* 640x480@60Hz */
1946 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
1947 752, 800, 0, 480, 489, 492, 525, 0,
1948 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1949 /* 800x600@60Hz */
1950 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
1951 968, 1056, 0, 600, 601, 605, 628, 0,
1952 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1953 /* 1024x768@60Hz */
1954 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
1955 1184, 1344, 0, 768, 771, 777, 806, 0,
1956 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1957 /* 1152x864@75Hz */
1958 { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
1959 1344, 1600, 0, 864, 865, 868, 900, 0,
1960 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1961 /* 1280x768@60Hz */
1962 { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344,
1963 1472, 1664, 0, 768, 771, 778, 798, 0,
1964 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1965 /* 1280x800@60Hz */
1966 { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352,
1967 1480, 1680, 0, 800, 803, 809, 831, 0,
1968 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
1969 /* 1280x960@60Hz */
1970 { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376,
1971 1488, 1800, 0, 960, 961, 964, 1000, 0,
1972 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1973 /* 1280x1024@60Hz */
1974 { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328,
1975 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
1976 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1977 /* 1360x768@60Hz */
1978 { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424,
1979 1536, 1792, 0, 768, 771, 777, 795, 0,
1980 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1981 /* 1440x1050@60Hz */
1982 { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488,
1983 1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
1984 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1985 /* 1440x900@60Hz */
1986 { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520,
1987 1672, 1904, 0, 900, 903, 909, 934, 0,
1988 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1989 /* 1600x1200@60Hz */
1990 { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664,
1991 1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
1992 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1993 /* 1680x1050@60Hz */
1994 { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784,
1995 1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
1996 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1997 /* 1792x1344@60Hz */
1998 { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920,
1999 2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
2000 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2001 /* 1853x1392@60Hz */
2002 { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952,
2003 2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
2004 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2005 /* 1920x1200@60Hz */
2006 { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056,
2007 2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
2008 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2009 /* 1920x1440@60Hz */
2010 { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048,
2011 2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
2012 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2013 /* 2560x1600@60Hz */
2014 { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752,
2015 3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
2016 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2017 /* Terminate */
2018 { DRM_MODE("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) },
2019};
2020
1543b4dd
TH
2021/**
2022 * vmw_guess_mode_timing - Provide fake timings for a
2023 * 60Hz vrefresh mode.
2024 *
2025 * @mode - Pointer to a struct drm_display_mode with hdisplay and vdisplay
2026 * members filled in.
2027 */
a278724a 2028void vmw_guess_mode_timing(struct drm_display_mode *mode)
1543b4dd
TH
2029{
2030 mode->hsync_start = mode->hdisplay + 50;
2031 mode->hsync_end = mode->hsync_start + 50;
2032 mode->htotal = mode->hsync_end + 50;
2033
2034 mode->vsync_start = mode->vdisplay + 50;
2035 mode->vsync_end = mode->vsync_start + 50;
2036 mode->vtotal = mode->vsync_end + 50;
2037
2038 mode->clock = (u32)mode->htotal * (u32)mode->vtotal / 100 * 6;
2039 mode->vrefresh = drm_mode_vrefresh(mode);
2040}
2041
2042
626ab771
JB
2043int vmw_du_connector_fill_modes(struct drm_connector *connector,
2044 uint32_t max_width, uint32_t max_height)
2045{
2046 struct vmw_display_unit *du = vmw_connector_to_du(connector);
2047 struct drm_device *dev = connector->dev;
2048 struct vmw_private *dev_priv = vmw_priv(dev);
2049 struct drm_display_mode *mode = NULL;
2050 struct drm_display_mode *bmode;
2051 struct drm_display_mode prefmode = { DRM_MODE("preferred",
2052 DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
2053 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2054 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
2055 };
2056 int i;
7c20d213 2057 u32 assumed_bpp = 4;
9a72384d 2058
04319d89
SY
2059 if (dev_priv->assume_16bpp)
2060 assumed_bpp = 2;
626ab771 2061
35c05125
SY
2062 if (dev_priv->active_display_unit == vmw_du_screen_target) {
2063 max_width = min(max_width, dev_priv->stdu_max_width);
28c95429
SY
2064 max_width = min(max_width, dev_priv->texture_max_width);
2065
35c05125 2066 max_height = min(max_height, dev_priv->stdu_max_height);
28c95429 2067 max_height = min(max_height, dev_priv->texture_max_height);
35c05125
SY
2068 }
2069
626ab771 2070 /* Add preferred mode */
c8261a96
SY
2071 mode = drm_mode_duplicate(dev, &prefmode);
2072 if (!mode)
2073 return 0;
2074 mode->hdisplay = du->pref_width;
2075 mode->vdisplay = du->pref_height;
2076 vmw_guess_mode_timing(mode);
626ab771 2077
c8261a96
SY
2078 if (vmw_kms_validate_mode_vram(dev_priv,
2079 mode->hdisplay * assumed_bpp,
2080 mode->vdisplay)) {
2081 drm_mode_probed_add(connector, mode);
2082 } else {
2083 drm_mode_destroy(dev, mode);
2084 mode = NULL;
2085 }
55bde5b2 2086
c8261a96
SY
2087 if (du->pref_mode) {
2088 list_del_init(&du->pref_mode->head);
2089 drm_mode_destroy(dev, du->pref_mode);
626ab771
JB
2090 }
2091
c8261a96
SY
2092 /* mode might be null here, this is intended */
2093 du->pref_mode = mode;
2094
626ab771
JB
2095 for (i = 0; vmw_kms_connector_builtin[i].type != 0; i++) {
2096 bmode = &vmw_kms_connector_builtin[i];
2097 if (bmode->hdisplay > max_width ||
2098 bmode->vdisplay > max_height)
2099 continue;
2100
9a72384d
SY
2101 if (!vmw_kms_validate_mode_vram(dev_priv,
2102 bmode->hdisplay * assumed_bpp,
626ab771
JB
2103 bmode->vdisplay))
2104 continue;
2105
2106 mode = drm_mode_duplicate(dev, bmode);
2107 if (!mode)
2108 return 0;
2109 mode->vrefresh = drm_mode_vrefresh(mode);
2110
2111 drm_mode_probed_add(connector, mode);
2112 }
2113
6af3e656 2114 drm_mode_connector_list_update(connector);
f6b05004
TH
2115 /* Move the prefered mode first, help apps pick the right mode. */
2116 drm_mode_sort(&connector->modes);
626ab771
JB
2117
2118 return 1;
2119}
2120
2121int vmw_du_connector_set_property(struct drm_connector *connector,
2122 struct drm_property *property,
2123 uint64_t val)
2124{
76404ac0
TH
2125 struct vmw_display_unit *du = vmw_connector_to_du(connector);
2126 struct vmw_private *dev_priv = vmw_priv(connector->dev);
2127
2128 if (property == dev_priv->implicit_placement_property)
2129 du->is_implicit = val;
2130
626ab771
JB
2131 return 0;
2132}
cd2b89e7
TH
2133
2134
9c2542a4 2135
d7721ca7
SY
2136/**
2137 * vmw_du_connector_atomic_set_property - Atomic version of get property
2138 *
2139 * @crtc - crtc the property is associated with
2140 *
2141 * Returns:
2142 * Zero on success, negative errno on failure.
2143 */
2144int
2145vmw_du_connector_atomic_set_property(struct drm_connector *connector,
2146 struct drm_connector_state *state,
2147 struct drm_property *property,
2148 uint64_t val)
2149{
2150 struct vmw_private *dev_priv = vmw_priv(connector->dev);
2151 struct vmw_connector_state *vcs = vmw_connector_state_to_vcs(state);
2152 struct vmw_display_unit *du = vmw_connector_to_du(connector);
2153
2154
2155 if (property == dev_priv->implicit_placement_property) {
2156 vcs->is_implicit = val;
2157
2158 /*
2159 * We should really be doing a drm_atomic_commit() to
2160 * commit the new state, but since this doesn't cause
2161 * an immedate state change, this is probably ok
2162 */
2163 du->is_implicit = vcs->is_implicit;
2164 } else {
2165 return -EINVAL;
2166 }
2167
2168 return 0;
2169}
2170
2171
2172/**
2173 * vmw_du_connector_atomic_get_property - Atomic version of get property
2174 *
2175 * @connector - connector the property is associated with
2176 *
2177 * Returns:
2178 * Zero on success, negative errno on failure.
2179 */
2180int
2181vmw_du_connector_atomic_get_property(struct drm_connector *connector,
2182 const struct drm_connector_state *state,
2183 struct drm_property *property,
2184 uint64_t *val)
2185{
2186 struct vmw_private *dev_priv = vmw_priv(connector->dev);
2187 struct vmw_connector_state *vcs = vmw_connector_state_to_vcs(state);
2188
2189 if (property == dev_priv->implicit_placement_property)
2190 *val = vcs->is_implicit;
2191 else {
2192 DRM_ERROR("Invalid Property %s\n", property->name);
2193 return -EINVAL;
2194 }
2195
2196 return 0;
2197}
2198
2199
cd2b89e7
TH
2200int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data,
2201 struct drm_file *file_priv)
2202{
2203 struct vmw_private *dev_priv = vmw_priv(dev);
2204 struct drm_vmw_update_layout_arg *arg =
2205 (struct drm_vmw_update_layout_arg *)data;
cd2b89e7
TH
2206 void __user *user_rects;
2207 struct drm_vmw_rect *rects;
2208 unsigned rects_size;
2209 int ret;
2210 int i;
65ade7d3 2211 u64 total_pixels = 0;
cd2b89e7 2212 struct drm_mode_config *mode_config = &dev->mode_config;
c8261a96 2213 struct drm_vmw_rect bounding_box = {0};
cd2b89e7 2214
cd2b89e7
TH
2215 if (!arg->num_outputs) {
2216 struct drm_vmw_rect def_rect = {0, 0, 800, 600};
2217 vmw_du_update_layout(dev_priv, 1, &def_rect);
5151adb3 2218 return 0;
cd2b89e7
TH
2219 }
2220
2221 rects_size = arg->num_outputs * sizeof(struct drm_vmw_rect);
bab9efc2
XW
2222 rects = kcalloc(arg->num_outputs, sizeof(struct drm_vmw_rect),
2223 GFP_KERNEL);
5151adb3
TH
2224 if (unlikely(!rects))
2225 return -ENOMEM;
cd2b89e7
TH
2226
2227 user_rects = (void __user *)(unsigned long)arg->rects;
2228 ret = copy_from_user(rects, user_rects, rects_size);
2229 if (unlikely(ret != 0)) {
2230 DRM_ERROR("Failed to get rects.\n");
2231 ret = -EFAULT;
2232 goto out_free;
2233 }
2234
2235 for (i = 0; i < arg->num_outputs; ++i) {
bab9efc2
XW
2236 if (rects[i].x < 0 ||
2237 rects[i].y < 0 ||
2238 rects[i].x + rects[i].w > mode_config->max_width ||
2239 rects[i].y + rects[i].h > mode_config->max_height) {
cd2b89e7
TH
2240 DRM_ERROR("Invalid GUI layout.\n");
2241 ret = -EINVAL;
2242 goto out_free;
2243 }
c8261a96
SY
2244
2245 /*
2246 * bounding_box.w and bunding_box.h are used as
2247 * lower-right coordinates
2248 */
2249 if (rects[i].x + rects[i].w > bounding_box.w)
2250 bounding_box.w = rects[i].x + rects[i].w;
2251
2252 if (rects[i].y + rects[i].h > bounding_box.h)
2253 bounding_box.h = rects[i].y + rects[i].h;
65ade7d3
SY
2254
2255 total_pixels += (u64) rects[i].w * (u64) rects[i].h;
cd2b89e7
TH
2256 }
2257
65ade7d3
SY
2258 if (dev_priv->active_display_unit == vmw_du_screen_target) {
2259 /*
2260 * For Screen Targets, the limits for a toplogy are:
2261 * 1. Bounding box (assuming 32bpp) must be < prim_bb_mem
2262 * 2. Total pixels (assuming 32bpp) must be < prim_bb_mem
2263 */
0f580386 2264 u64 bb_mem = (u64) bounding_box.w * bounding_box.h * 4;
65ade7d3
SY
2265 u64 pixel_mem = total_pixels * 4;
2266
2267 if (bb_mem > dev_priv->prim_bb_mem) {
2268 DRM_ERROR("Topology is beyond supported limits.\n");
35c05125
SY
2269 ret = -EINVAL;
2270 goto out_free;
2271 }
2272
65ade7d3
SY
2273 if (pixel_mem > dev_priv->prim_bb_mem) {
2274 DRM_ERROR("Combined output size too large\n");
2275 ret = -EINVAL;
2276 goto out_free;
2277 }
cd2b89e7
TH
2278 }
2279
2280 vmw_du_update_layout(dev_priv, arg->num_outputs, rects);
2281
2282out_free:
2283 kfree(rects);
cd2b89e7
TH
2284 return ret;
2285}
1a4b172a
TH
2286
2287/**
2288 * vmw_kms_helper_dirty - Helper to build commands and perform actions based
2289 * on a set of cliprects and a set of display units.
2290 *
2291 * @dev_priv: Pointer to a device private structure.
2292 * @framebuffer: Pointer to the framebuffer on which to perform the actions.
2293 * @clips: A set of struct drm_clip_rect. Either this os @vclips must be NULL.
2294 * Cliprects are given in framebuffer coordinates.
2295 * @vclips: A set of struct drm_vmw_rect cliprects. Either this or @clips must
2296 * be NULL. Cliprects are given in source coordinates.
2297 * @dest_x: X coordinate offset for the crtc / destination clip rects.
2298 * @dest_y: Y coordinate offset for the crtc / destination clip rects.
2299 * @num_clips: Number of cliprects in the @clips or @vclips array.
2300 * @increment: Integer with which to increment the clip counter when looping.
2301 * Used to skip a predetermined number of clip rects.
2302 * @dirty: Closure structure. See the description of struct vmw_kms_dirty.
2303 */
2304int vmw_kms_helper_dirty(struct vmw_private *dev_priv,
2305 struct vmw_framebuffer *framebuffer,
2306 const struct drm_clip_rect *clips,
2307 const struct drm_vmw_rect *vclips,
2308 s32 dest_x, s32 dest_y,
2309 int num_clips,
2310 int increment,
2311 struct vmw_kms_dirty *dirty)
2312{
2313 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
2314 struct drm_crtc *crtc;
2315 u32 num_units = 0;
2316 u32 i, k;
1a4b172a
TH
2317
2318 dirty->dev_priv = dev_priv;
2319
91e9f352
DR
2320 /* If crtc is passed, no need to iterate over other display units */
2321 if (dirty->crtc) {
2322 units[num_units++] = vmw_crtc_to_du(dirty->crtc);
2323 } else {
2324 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list,
2325 head) {
2326 if (crtc->primary->fb != &framebuffer->base)
2327 continue;
2328 units[num_units++] = vmw_crtc_to_du(crtc);
2329 }
1a4b172a
TH
2330 }
2331
2332 for (k = 0; k < num_units; k++) {
2333 struct vmw_display_unit *unit = units[k];
2334 s32 crtc_x = unit->crtc.x;
2335 s32 crtc_y = unit->crtc.y;
2336 s32 crtc_width = unit->crtc.mode.hdisplay;
2337 s32 crtc_height = unit->crtc.mode.vdisplay;
2338 const struct drm_clip_rect *clips_ptr = clips;
2339 const struct drm_vmw_rect *vclips_ptr = vclips;
2340
2341 dirty->unit = unit;
2342 if (dirty->fifo_reserve_size > 0) {
2343 dirty->cmd = vmw_fifo_reserve(dev_priv,
2344 dirty->fifo_reserve_size);
2345 if (!dirty->cmd) {
2346 DRM_ERROR("Couldn't reserve fifo space "
2347 "for dirty blits.\n");
f3b8c0ca 2348 return -ENOMEM;
1a4b172a
TH
2349 }
2350 memset(dirty->cmd, 0, dirty->fifo_reserve_size);
2351 }
2352 dirty->num_hits = 0;
2353 for (i = 0; i < num_clips; i++, clips_ptr += increment,
2354 vclips_ptr += increment) {
2355 s32 clip_left;
2356 s32 clip_top;
2357
2358 /*
2359 * Select clip array type. Note that integer type
2360 * in @clips is unsigned short, whereas in @vclips
2361 * it's 32-bit.
2362 */
2363 if (clips) {
2364 dirty->fb_x = (s32) clips_ptr->x1;
2365 dirty->fb_y = (s32) clips_ptr->y1;
2366 dirty->unit_x2 = (s32) clips_ptr->x2 + dest_x -
2367 crtc_x;
2368 dirty->unit_y2 = (s32) clips_ptr->y2 + dest_y -
2369 crtc_y;
2370 } else {
2371 dirty->fb_x = vclips_ptr->x;
2372 dirty->fb_y = vclips_ptr->y;
2373 dirty->unit_x2 = dirty->fb_x + vclips_ptr->w +
2374 dest_x - crtc_x;
2375 dirty->unit_y2 = dirty->fb_y + vclips_ptr->h +
2376 dest_y - crtc_y;
2377 }
2378
2379 dirty->unit_x1 = dirty->fb_x + dest_x - crtc_x;
2380 dirty->unit_y1 = dirty->fb_y + dest_y - crtc_y;
2381
2382 /* Skip this clip if it's outside the crtc region */
2383 if (dirty->unit_x1 >= crtc_width ||
2384 dirty->unit_y1 >= crtc_height ||
2385 dirty->unit_x2 <= 0 || dirty->unit_y2 <= 0)
2386 continue;
2387
2388 /* Clip right and bottom to crtc limits */
2389 dirty->unit_x2 = min_t(s32, dirty->unit_x2,
2390 crtc_width);
2391 dirty->unit_y2 = min_t(s32, dirty->unit_y2,
2392 crtc_height);
2393
2394 /* Clip left and top to crtc limits */
2395 clip_left = min_t(s32, dirty->unit_x1, 0);
2396 clip_top = min_t(s32, dirty->unit_y1, 0);
2397 dirty->unit_x1 -= clip_left;
2398 dirty->unit_y1 -= clip_top;
2399 dirty->fb_x -= clip_left;
2400 dirty->fb_y -= clip_top;
2401
2402 dirty->clip(dirty);
2403 }
2404
2405 dirty->fifo_commit(dirty);
2406 }
2407
2408 return 0;
2409}
2410
2411/**
2412 * vmw_kms_helper_buffer_prepare - Reserve and validate a buffer object before
2413 * command submission.
2414 *
2415 * @dev_priv. Pointer to a device private structure.
2416 * @buf: The buffer object
2417 * @interruptible: Whether to perform waits as interruptible.
2418 * @validate_as_mob: Whether the buffer should be validated as a MOB. If false,
2419 * The buffer will be validated as a GMR. Already pinned buffers will not be
2420 * validated.
2421 *
2422 * Returns 0 on success, negative error code on failure, -ERESTARTSYS if
2423 * interrupted by a signal.
2424 */
2425int vmw_kms_helper_buffer_prepare(struct vmw_private *dev_priv,
2426 struct vmw_dma_buffer *buf,
2427 bool interruptible,
ef86cfee
TH
2428 bool validate_as_mob,
2429 bool for_cpu_blit)
1a4b172a 2430{
ef86cfee
TH
2431 struct ttm_operation_ctx ctx = {
2432 .interruptible = interruptible,
2433 .no_wait_gpu = false};
1a4b172a
TH
2434 struct ttm_buffer_object *bo = &buf->base;
2435 int ret;
2436
dfd5e50e 2437 ttm_bo_reserve(bo, false, false, NULL);
ef86cfee
TH
2438 if (for_cpu_blit)
2439 ret = ttm_bo_validate(bo, &vmw_nonfixed_placement, &ctx);
2440 else
2441 ret = vmw_validate_single_buffer(dev_priv, bo, interruptible,
2442 validate_as_mob);
1a4b172a
TH
2443 if (ret)
2444 ttm_bo_unreserve(bo);
2445
2446 return ret;
2447}
2448
2449/**
2450 * vmw_kms_helper_buffer_revert - Undo the actions of
2451 * vmw_kms_helper_buffer_prepare.
2452 *
2453 * @res: Pointer to the buffer object.
2454 *
2455 * Helper to be used if an error forces the caller to undo the actions of
2456 * vmw_kms_helper_buffer_prepare.
2457 */
2458void vmw_kms_helper_buffer_revert(struct vmw_dma_buffer *buf)
2459{
2460 if (buf)
2461 ttm_bo_unreserve(&buf->base);
2462}
2463
2464/**
2465 * vmw_kms_helper_buffer_finish - Unreserve and fence a buffer object after
2466 * kms command submission.
2467 *
2468 * @dev_priv: Pointer to a device private structure.
2469 * @file_priv: Pointer to a struct drm_file representing the caller's
2470 * connection. Must be set to NULL if @user_fence_rep is NULL, and conversely
2471 * if non-NULL, @user_fence_rep must be non-NULL.
2472 * @buf: The buffer object.
2473 * @out_fence: Optional pointer to a fence pointer. If non-NULL, a
2474 * ref-counted fence pointer is returned here.
2475 * @user_fence_rep: Optional pointer to a user-space provided struct
2476 * drm_vmw_fence_rep. If provided, @file_priv must also be provided and the
2477 * function copies fence data to user-space in a fail-safe manner.
2478 */
2479void vmw_kms_helper_buffer_finish(struct vmw_private *dev_priv,
2480 struct drm_file *file_priv,
2481 struct vmw_dma_buffer *buf,
2482 struct vmw_fence_obj **out_fence,
2483 struct drm_vmw_fence_rep __user *
2484 user_fence_rep)
2485{
2486 struct vmw_fence_obj *fence;
2487 uint32_t handle;
2488 int ret;
2489
2490 ret = vmw_execbuf_fence_commands(file_priv, dev_priv, &fence,
2491 file_priv ? &handle : NULL);
2492 if (buf)
2493 vmw_fence_single_bo(&buf->base, fence);
2494 if (file_priv)
2495 vmw_execbuf_copy_fence_user(dev_priv, vmw_fpriv(file_priv),
2496 ret, user_fence_rep, fence,
c906965d 2497 handle, -1, NULL);
1a4b172a
TH
2498 if (out_fence)
2499 *out_fence = fence;
2500 else
2501 vmw_fence_obj_unreference(&fence);
2502
2503 vmw_kms_helper_buffer_revert(buf);
2504}
2505
2506
2507/**
2508 * vmw_kms_helper_resource_revert - Undo the actions of
2509 * vmw_kms_helper_resource_prepare.
2510 *
2511 * @res: Pointer to the resource. Typically a surface.
2512 *
2513 * Helper to be used if an error forces the caller to undo the actions of
2514 * vmw_kms_helper_resource_prepare.
2515 */
2516void vmw_kms_helper_resource_revert(struct vmw_resource *res)
2517{
2518 vmw_kms_helper_buffer_revert(res->backup);
d80efd5c 2519 vmw_resource_unreserve(res, false, NULL, 0);
1a4b172a
TH
2520 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
2521}
2522
2523/**
2524 * vmw_kms_helper_resource_prepare - Reserve and validate a resource before
2525 * command submission.
2526 *
2527 * @res: Pointer to the resource. Typically a surface.
2528 * @interruptible: Whether to perform waits as interruptible.
2529 *
2530 * Reserves and validates also the backup buffer if a guest-backed resource.
2531 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
2532 * interrupted by a signal.
2533 */
2534int vmw_kms_helper_resource_prepare(struct vmw_resource *res,
2535 bool interruptible)
2536{
2537 int ret = 0;
2538
2539 if (interruptible)
2540 ret = mutex_lock_interruptible(&res->dev_priv->cmdbuf_mutex);
2541 else
2542 mutex_lock(&res->dev_priv->cmdbuf_mutex);
2543
2544 if (unlikely(ret != 0))
2545 return -ERESTARTSYS;
2546
2547 ret = vmw_resource_reserve(res, interruptible, false);
2548 if (ret)
2549 goto out_unlock;
2550
2551 if (res->backup) {
2552 ret = vmw_kms_helper_buffer_prepare(res->dev_priv, res->backup,
2553 interruptible,
ef86cfee
TH
2554 res->dev_priv->has_mob,
2555 false);
1a4b172a
TH
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}
c3b9b165
TH
2851
2852
2853/**
2854 * vmw_kms_suspend - Save modesetting state and turn modesetting off.
2855 *
2856 * @dev: Pointer to the drm device
2857 * Return: 0 on success. Negative error code on failure.
2858 */
2859int vmw_kms_suspend(struct drm_device *dev)
2860{
2861 struct vmw_private *dev_priv = vmw_priv(dev);
2862
2863 dev_priv->suspend_state = drm_atomic_helper_suspend(dev);
2864 if (IS_ERR(dev_priv->suspend_state)) {
2865 int ret = PTR_ERR(dev_priv->suspend_state);
2866
2867 DRM_ERROR("Failed kms suspend: %d\n", ret);
2868 dev_priv->suspend_state = NULL;
2869
2870 return ret;
2871 }
2872
2873 return 0;
2874}
2875
2876
2877/**
2878 * vmw_kms_resume - Re-enable modesetting and restore state
2879 *
2880 * @dev: Pointer to the drm device
2881 * Return: 0 on success. Negative error code on failure.
2882 *
2883 * State is resumed from a previous vmw_kms_suspend(). It's illegal
2884 * to call this function without a previous vmw_kms_suspend().
2885 */
2886int vmw_kms_resume(struct drm_device *dev)
2887{
2888 struct vmw_private *dev_priv = vmw_priv(dev);
2889 int ret;
2890
2891 if (WARN_ON(!dev_priv->suspend_state))
2892 return 0;
2893
2894 ret = drm_atomic_helper_resume(dev, dev_priv->suspend_state);
2895 dev_priv->suspend_state = NULL;
2896
2897 return ret;
2898}