blob: 620180df13039d75322039a7c45a7df2ec013d51 [file] [log] [blame]
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001/**************************************************************************
2 *
Sinclair Yeh54fbde82015-07-29 12:38:02 -07003 * Copyright © 2009-2015 VMware, Inc., Palo Alto, CA., USA
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00004 * 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"
Sinclair Yeh060e2ad2017-03-23 14:18:32 -070029#include <drm/drm_plane_helper.h>
Sinclair Yeh9c2542a2017-03-23 11:33:39 -070030#include <drm/drm_atomic.h>
31#include <drm/drm_atomic_helper.h>
Sinclair Yeh060e2ad2017-03-23 14:18:32 -070032#include <drm/drm_rect.h>
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000033
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +020034
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000035/* Might need a hrtimer here? */
36#define VMWGFX_PRESENT_RATE ((HZ / 60 > 0) ? HZ / 60 : 1)
37
Sinclair Yehc8261a92015-06-26 01:23:42 -070038void vmw_du_cleanup(struct vmw_display_unit *du)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000039{
Sinclair Yeh36cc79b2017-03-23 11:28:11 -070040 drm_plane_cleanup(&du->primary);
41 drm_plane_cleanup(&du->cursor);
42
Thomas Wood34ea3d32014-05-29 16:57:41 +010043 drm_connector_unregister(&du->connector);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000044 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
Sinclair Yeh36cc79b2017-03-23 11:28:11 -070053static int vmw_cursor_update_image(struct vmw_private *dev_priv,
54 u32 *image, u32 width, u32 height,
55 u32 hotspotX, u32 hotspotY)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000056{
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
Thomas Hellstromb9eb1a62015-04-02 02:39:45 -070077 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;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000083
Thomas Hellstrom4e0858a2015-11-05 02:18:55 -080084 vmw_fifo_commit_flush(dev_priv, cmd_size);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000085
86 return 0;
87}
88
Sinclair Yeh36cc79b2017-03-23 11:28:11 -070089static 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)
Jakob Bornecrantz6a91d972011-11-28 13:19:10 +010093{
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
Christian Königdfd5e502016-04-06 11:12:03 +0200104 ret = ttm_bo_reserve(&dmabuf->base, true, false, NULL);
Jakob Bornecrantz6a91d972011-11-28 13:19:10 +0100105 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
Sinclair Yeh36cc79b2017-03-23 11:28:11 -0700126static void vmw_cursor_update_position(struct vmw_private *dev_priv,
127 bool show, int x, int y)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000128{
Thomas Hellstromb76ff5e2015-10-28 10:44:04 +0100129 u32 *fifo_mem = dev_priv->mmio_virt;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000130 uint32_t count;
131
Sinclair Yeh36cc79b2017-03-23 11:28:11 -0700132 spin_lock(&dev_priv->cursor_lock);
Thomas Hellstromb76ff5e2015-10-28 10:44:04 +0100133 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);
Sinclair Yeh36cc79b2017-03-23 11:28:11 -0700138 spin_unlock(&dev_priv->cursor_lock);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000139}
140
Thomas Hellstrom8fbf9d92015-11-26 19:45:16 +0100141
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000142void 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;
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100158 int i, ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000159
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
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100180 if (cmd->dma.guest.ptr.offset % PAGE_SIZE ||
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000181 box->x != 0 || box->y != 0 || box->z != 0 ||
182 box->srcx != 0 || box->srcy != 0 || box->srcz != 0 ||
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100183 box->d != 1 || box_count != 1) {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000184 /* TODO handle none page aligned offsets */
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100185 /* 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);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000193 return;
194 }
195
196 kmap_offset = cmd->dma.guest.ptr.offset >> PAGE_SHIFT;
197 kmap_num = (64*64*4) >> PAGE_SHIFT;
198
Christian Königdfd5e502016-04-06 11:12:03 +0200199 ret = ttm_bo_reserve(bo, true, false, NULL);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000200 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
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100211 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
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000221 srf->snooper.age++;
222
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000223 ttm_bo_kunmap(&map);
224err_unreserve:
225 ttm_bo_unreserve(bo);
226}
227
Thomas Hellstrom8fbf9d92015-11-26 19:45:16 +0100228/**
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
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000251void 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,
Thomas Hellstrom8fbf9d92015-11-26 19:45:16 +0100268 64, 64,
269 du->hotspot_x + du->core_hotspot_x,
270 du->hotspot_y + du->core_hotspot_y);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000271 }
272
273 mutex_unlock(&dev->mode_config.mutex);
274}
275
Sinclair Yeh36cc79b2017-03-23 11:28:11 -0700276
Sinclair Yeh36cc79b2017-03-23 11:28:11 -0700277void 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
Sinclair Yeh060e2ad2017-03-23 14:18:32 -0700293/**
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
373void
Sinclair Yeh060e2ad2017-03-23 14:18:32 -0700374vmw_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;
387 du->cursor_surface = vps->surf;
388 du->cursor_dmabuf = vps->dmabuf;
389
390 /* setup new image */
391 if (vps->surf) {
392 du->cursor_age = du->cursor_surface->snooper.age;
393
394 ret = vmw_cursor_update_image(dev_priv,
395 vps->surf->snooper.image,
396 64, 64, hotspot_x, hotspot_y);
397 } else if (vps->dmabuf) {
398 ret = vmw_cursor_update_dmabuf(dev_priv, vps->dmabuf,
399 plane->state->crtc_w,
400 plane->state->crtc_h,
401 hotspot_x, hotspot_y);
402 } else {
403 vmw_cursor_update_position(dev_priv, false, 0, 0);
404 return;
405 }
406
407 if (!ret) {
408 du->cursor_x = plane->state->crtc_x + du->set_gui_x;
409 du->cursor_y = plane->state->crtc_y + du->set_gui_y;
410
411 vmw_cursor_update_position(dev_priv, true,
412 du->cursor_x + hotspot_x,
413 du->cursor_y + hotspot_y);
414 } else {
415 DRM_ERROR("Failed to update cursor image\n");
416 }
417}
418
419
420/**
421 * vmw_du_primary_plane_atomic_check - check if the new state is okay
422 *
423 * @plane: display plane
424 * @state: info on the new plane state, including the FB
425 *
426 * Check if the new state is settable given the current state. Other
427 * than what the atomic helper checks, we care about crtc fitting
428 * the FB and maintaining one active framebuffer.
429 *
430 * Returns 0 on success
431 */
432int vmw_du_primary_plane_atomic_check(struct drm_plane *plane,
433 struct drm_plane_state *state)
434{
435 struct drm_framebuffer *new_fb = state->fb;
436 bool visible;
437
438 struct drm_rect src = {
439 .x1 = state->src_x,
440 .y1 = state->src_y,
441 .x2 = state->src_x + state->src_w,
442 .y2 = state->src_y + state->src_h,
443 };
444 struct drm_rect dest = {
445 .x1 = state->crtc_x,
446 .y1 = state->crtc_y,
447 .x2 = state->crtc_x + state->crtc_w,
448 .y2 = state->crtc_y + state->crtc_h,
449 };
450 struct drm_rect clip = dest;
451 int ret;
452
453 ret = drm_plane_helper_check_update(plane, state->crtc, new_fb,
454 &src, &dest, &clip,
Robert Fossc2c446a2017-05-19 16:50:17 -0400455 DRM_MODE_ROTATE_0,
Sinclair Yeh060e2ad2017-03-23 14:18:32 -0700456 DRM_PLANE_HELPER_NO_SCALING,
457 DRM_PLANE_HELPER_NO_SCALING,
458 false, true, &visible);
459
460
461 if (!ret && new_fb) {
462 struct drm_crtc *crtc = state->crtc;
463 struct vmw_connector_state *vcs;
464 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
465 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
466 struct vmw_framebuffer *vfb = vmw_framebuffer_to_vfb(new_fb);
467
468 vcs = vmw_connector_state_to_vcs(du->connector.state);
469
470 if ((dest.x2 > new_fb->width ||
471 dest.y2 > new_fb->height)) {
472 DRM_ERROR("CRTC area outside of framebuffer\n");
473 return -EINVAL;
474 }
475
476 /* Only one active implicit framebuffer at a time. */
477 mutex_lock(&dev_priv->global_kms_state_mutex);
478 if (vcs->is_implicit && dev_priv->implicit_fb &&
479 !(dev_priv->num_implicit == 1 && du->active_implicit)
480 && dev_priv->implicit_fb != vfb) {
481 DRM_ERROR("Multiple implicit framebuffers "
482 "not supported.\n");
483 ret = -EINVAL;
484 }
485 mutex_unlock(&dev_priv->global_kms_state_mutex);
486 }
487
488
489 return ret;
490}
491
492
493/**
494 * vmw_du_cursor_plane_atomic_check - check if the new state is okay
495 *
496 * @plane: cursor plane
497 * @state: info on the new plane state
498 *
499 * This is a chance to fail if the new cursor state does not fit
500 * our requirements.
501 *
502 * Returns 0 on success
503 */
504int vmw_du_cursor_plane_atomic_check(struct drm_plane *plane,
505 struct drm_plane_state *new_state)
506{
507 int ret = 0;
508 struct vmw_surface *surface = NULL;
509 struct drm_framebuffer *fb = new_state->fb;
510
511
512 /* Turning off */
513 if (!fb)
514 return ret;
515
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
Sinclair Yeh06ec4192017-03-23 13:14:54 -0700535int 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);
580 if (drm_crtc_vblank_get(crtc) == 0)
581 drm_crtc_arm_vblank_event(crtc, event);
582 else
583 drm_crtc_send_vblank_event(crtc, event);
584 spin_unlock_irq(&crtc->dev->event_lock);
585 }
586
587}
588
589
Sinclair Yeh9c2542a2017-03-23 11:33:39 -0700590/**
591 * vmw_du_crtc_duplicate_state - duplicate crtc state
592 * @crtc: DRM crtc
593 *
594 * Allocates and returns a copy of the crtc state (both common and
595 * vmw-specific) for the specified crtc.
596 *
597 * Returns: The newly allocated crtc state, or NULL on failure.
598 */
599struct drm_crtc_state *
600vmw_du_crtc_duplicate_state(struct drm_crtc *crtc)
601{
602 struct drm_crtc_state *state;
603 struct vmw_crtc_state *vcs;
604
605 if (WARN_ON(!crtc->state))
606 return NULL;
607
608 vcs = kmemdup(crtc->state, sizeof(*vcs), GFP_KERNEL);
609
610 if (!vcs)
611 return NULL;
612
613 state = &vcs->base;
614
615 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
616
617 return state;
618}
619
620
621/**
622 * vmw_du_crtc_reset - creates a blank vmw crtc state
623 * @crtc: DRM crtc
624 *
625 * Resets the atomic state for @crtc by freeing the state pointer (which
626 * might be NULL, e.g. at driver load time) and allocating a new empty state
627 * object.
628 */
629void vmw_du_crtc_reset(struct drm_crtc *crtc)
630{
631 struct vmw_crtc_state *vcs;
632
633
634 if (crtc->state) {
635 __drm_atomic_helper_crtc_destroy_state(crtc->state);
636
637 kfree(vmw_crtc_state_to_vcs(crtc->state));
638 }
639
640 vcs = kzalloc(sizeof(*vcs), GFP_KERNEL);
641
642 if (!vcs) {
643 DRM_ERROR("Cannot allocate vmw_crtc_state\n");
644 return;
645 }
646
647 crtc->state = &vcs->base;
648 crtc->state->crtc = crtc;
649}
650
651
652/**
653 * vmw_du_crtc_destroy_state - destroy crtc state
654 * @crtc: DRM crtc
655 * @state: state object to destroy
656 *
657 * Destroys the crtc state (both common and vmw-specific) for the
658 * specified plane.
659 */
660void
661vmw_du_crtc_destroy_state(struct drm_crtc *crtc,
662 struct drm_crtc_state *state)
663{
664 drm_atomic_helper_crtc_destroy_state(crtc, state);
665}
666
667
Sinclair Yehcc5ec452017-03-23 11:36:05 -0700668/**
669 * vmw_du_plane_duplicate_state - duplicate plane state
670 * @plane: drm plane
671 *
672 * Allocates and returns a copy of the plane state (both common and
673 * vmw-specific) for the specified plane.
674 *
675 * Returns: The newly allocated plane state, or NULL on failure.
676 */
677struct drm_plane_state *
678vmw_du_plane_duplicate_state(struct drm_plane *plane)
679{
680 struct drm_plane_state *state;
681 struct vmw_plane_state *vps;
682
683 vps = kmemdup(plane->state, sizeof(*vps), GFP_KERNEL);
684
685 if (!vps)
686 return NULL;
687
688 vps->pinned = 0;
689
Sinclair Yeh810b3e162017-03-23 15:39:16 -0700690 /* Mapping is managed by prepare_fb/cleanup_fb */
691 memset(&vps->guest_map, 0, sizeof(vps->guest_map));
692 memset(&vps->host_map, 0, sizeof(vps->host_map));
693 vps->cpp = 0;
694
Sinclair Yehcc5ec452017-03-23 11:36:05 -0700695 /* Each ref counted resource needs to be acquired again */
696 if (vps->surf)
697 (void) vmw_surface_reference(vps->surf);
698
699 if (vps->dmabuf)
700 (void) vmw_dmabuf_reference(vps->dmabuf);
701
702 state = &vps->base;
703
704 __drm_atomic_helper_plane_duplicate_state(plane, state);
705
706 return state;
707}
708
709
710/**
711 * vmw_du_plane_reset - creates a blank vmw plane state
712 * @plane: drm plane
713 *
714 * Resets the atomic state for @plane by freeing the state pointer (which might
715 * be NULL, e.g. at driver load time) and allocating a new empty state object.
716 */
717void vmw_du_plane_reset(struct drm_plane *plane)
718{
719 struct vmw_plane_state *vps;
720
721
722 if (plane->state)
723 vmw_du_plane_destroy_state(plane, plane->state);
724
725 vps = kzalloc(sizeof(*vps), GFP_KERNEL);
726
727 if (!vps) {
728 DRM_ERROR("Cannot allocate vmw_plane_state\n");
729 return;
730 }
731
732 plane->state = &vps->base;
733 plane->state->plane = plane;
Robert Fossc2c446a2017-05-19 16:50:17 -0400734 plane->state->rotation = DRM_MODE_ROTATE_0;
Sinclair Yehcc5ec452017-03-23 11:36:05 -0700735}
736
737
738/**
739 * vmw_du_plane_destroy_state - destroy plane state
740 * @plane: DRM plane
741 * @state: state object to destroy
742 *
743 * Destroys the plane state (both common and vmw-specific) for the
744 * specified plane.
745 */
746void
747vmw_du_plane_destroy_state(struct drm_plane *plane,
748 struct drm_plane_state *state)
749{
750 struct vmw_plane_state *vps = vmw_plane_state_to_vps(state);
751
752
Sinclair Yeh810b3e162017-03-23 15:39:16 -0700753 /* Should have been freed by cleanup_fb */
754 if (vps->guest_map.virtual) {
755 DRM_ERROR("Guest mapping not freed\n");
756 ttm_bo_kunmap(&vps->guest_map);
757 }
758
759 if (vps->host_map.virtual) {
760 DRM_ERROR("Host mapping not freed\n");
761 ttm_bo_kunmap(&vps->host_map);
762 }
763
Sinclair Yehcc5ec452017-03-23 11:36:05 -0700764 if (vps->surf)
765 vmw_surface_unreference(&vps->surf);
766
767 if (vps->dmabuf)
768 vmw_dmabuf_unreference(&vps->dmabuf);
769
770 drm_atomic_helper_plane_destroy_state(plane, state);
771}
772
773
Sinclair Yehd7721ca2017-03-23 11:48:44 -0700774/**
775 * vmw_du_connector_duplicate_state - duplicate connector state
776 * @connector: DRM connector
777 *
778 * Allocates and returns a copy of the connector state (both common and
779 * vmw-specific) for the specified connector.
780 *
781 * Returns: The newly allocated connector state, or NULL on failure.
782 */
783struct drm_connector_state *
784vmw_du_connector_duplicate_state(struct drm_connector *connector)
785{
786 struct drm_connector_state *state;
787 struct vmw_connector_state *vcs;
788
789 if (WARN_ON(!connector->state))
790 return NULL;
791
792 vcs = kmemdup(connector->state, sizeof(*vcs), GFP_KERNEL);
793
794 if (!vcs)
795 return NULL;
796
797 state = &vcs->base;
798
799 __drm_atomic_helper_connector_duplicate_state(connector, state);
800
801 return state;
802}
803
804
805/**
806 * vmw_du_connector_reset - creates a blank vmw connector state
807 * @connector: DRM connector
808 *
809 * Resets the atomic state for @connector by freeing the state pointer (which
810 * might be NULL, e.g. at driver load time) and allocating a new empty state
811 * object.
812 */
813void vmw_du_connector_reset(struct drm_connector *connector)
814{
815 struct vmw_connector_state *vcs;
816
817
818 if (connector->state) {
819 __drm_atomic_helper_connector_destroy_state(connector->state);
820
821 kfree(vmw_connector_state_to_vcs(connector->state));
822 }
823
824 vcs = kzalloc(sizeof(*vcs), GFP_KERNEL);
825
826 if (!vcs) {
827 DRM_ERROR("Cannot allocate vmw_connector_state\n");
828 return;
829 }
830
831 __drm_atomic_helper_connector_reset(connector, &vcs->base);
832}
833
834
835/**
836 * vmw_du_connector_destroy_state - destroy connector state
837 * @connector: DRM connector
838 * @state: state object to destroy
839 *
840 * Destroys the connector state (both common and vmw-specific) for the
841 * specified plane.
842 */
843void
844vmw_du_connector_destroy_state(struct drm_connector *connector,
845 struct drm_connector_state *state)
846{
847 drm_atomic_helper_connector_destroy_state(connector, state);
848}
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000849/*
850 * Generic framebuffer code
851 */
852
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000853/*
854 * Surface framebuffer code
855 */
856
Rashika Kheria847c5962014-01-06 22:18:10 +0530857static void vmw_framebuffer_surface_destroy(struct drm_framebuffer *framebuffer)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000858{
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200859 struct vmw_framebuffer_surface *vfbs =
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000860 vmw_framebuffer_to_vfbs(framebuffer);
861
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000862 drm_framebuffer_cleanup(framebuffer);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200863 vmw_surface_unreference(&vfbs->surface);
Thomas Hellstroma2787242015-06-29 12:55:07 -0700864 if (vfbs->base.user_obj)
865 ttm_base_object_unref(&vfbs->base.user_obj);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000866
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200867 kfree(vfbs);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000868}
869
Rashika Kheria847c5962014-01-06 22:18:10 +0530870static int vmw_framebuffer_surface_dirty(struct drm_framebuffer *framebuffer,
Thomas Hellstrom02b00162010-10-05 12:43:02 +0200871 struct drm_file *file_priv,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000872 unsigned flags, unsigned color,
873 struct drm_clip_rect *clips,
874 unsigned num_clips)
875{
876 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
877 struct vmw_framebuffer_surface *vfbs =
878 vmw_framebuffer_to_vfbs(framebuffer);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000879 struct drm_clip_rect norect;
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200880 int ret, inc = 1;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000881
Sinclair Yehc8261a92015-06-26 01:23:42 -0700882 /* Legacy Display Unit does not support 3D */
883 if (dev_priv->active_display_unit == vmw_du_legacy)
Jakob Bornecrantz01e81412011-10-04 20:13:24 +0200884 return -EINVAL;
885
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200886 drm_modeset_lock_all(dev_priv->dev);
887
Thomas Hellstrom294adf72014-02-27 12:34:51 +0100888 ret = ttm_read_lock(&dev_priv->reservation_sem, true);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200889 if (unlikely(ret != 0)) {
890 drm_modeset_unlock_all(dev_priv->dev);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200891 return ret;
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200892 }
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200893
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000894 if (!num_clips) {
895 num_clips = 1;
896 clips = &norect;
897 norect.x1 = norect.y1 = 0;
898 norect.x2 = framebuffer->width;
899 norect.y2 = framebuffer->height;
900 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
901 num_clips /= 2;
902 inc = 2; /* skip source rects */
903 }
904
Sinclair Yehc8261a92015-06-26 01:23:42 -0700905 if (dev_priv->active_display_unit == vmw_du_screen_object)
Thomas Hellstrom10b1e0c2015-06-26 02:14:27 -0700906 ret = vmw_kms_sou_do_surface_dirty(dev_priv, &vfbs->base,
907 clips, NULL, NULL, 0, 0,
908 num_clips, inc, NULL);
Sinclair Yeh35c05122015-06-26 01:42:06 -0700909 else
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700910 ret = vmw_kms_stdu_surface_dirty(dev_priv, &vfbs->base,
911 clips, NULL, NULL, 0, 0,
912 num_clips, inc, NULL);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000913
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700914 vmw_fifo_flush(dev_priv, false);
Thomas Hellstrom294adf72014-02-27 12:34:51 +0100915 ttm_read_unlock(&dev_priv->reservation_sem);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200916
917 drm_modeset_unlock_all(dev_priv->dev);
918
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000919 return 0;
920}
921
Thomas Hellstrom10b1e0c2015-06-26 02:14:27 -0700922/**
923 * vmw_kms_readback - Perform a readback from the screen system to
924 * a dma-buffer backed framebuffer.
925 *
926 * @dev_priv: Pointer to the device private structure.
927 * @file_priv: Pointer to a struct drm_file identifying the caller.
928 * Must be set to NULL if @user_fence_rep is NULL.
929 * @vfb: Pointer to the dma-buffer backed framebuffer.
930 * @user_fence_rep: User-space provided structure for fence information.
931 * Must be set to non-NULL if @file_priv is non-NULL.
932 * @vclips: Array of clip rects.
933 * @num_clips: Number of clip rects in @vclips.
934 *
935 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
936 * interrupted.
937 */
938int vmw_kms_readback(struct vmw_private *dev_priv,
939 struct drm_file *file_priv,
940 struct vmw_framebuffer *vfb,
941 struct drm_vmw_fence_rep __user *user_fence_rep,
942 struct drm_vmw_rect *vclips,
943 uint32_t num_clips)
944{
945 switch (dev_priv->active_display_unit) {
946 case vmw_du_screen_object:
947 return vmw_kms_sou_readback(dev_priv, file_priv, vfb,
948 user_fence_rep, vclips, num_clips);
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700949 case vmw_du_screen_target:
950 return vmw_kms_stdu_dma(dev_priv, file_priv, vfb,
951 user_fence_rep, NULL, vclips, num_clips,
952 1, false, true);
Thomas Hellstrom10b1e0c2015-06-26 02:14:27 -0700953 default:
954 WARN_ONCE(true,
955 "Readback called with invalid display system.\n");
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700956}
Thomas Hellstrom10b1e0c2015-06-26 02:14:27 -0700957
958 return -ENOSYS;
959}
960
961
Ville Syrjäläd7955fc2015-12-15 12:21:15 +0100962static const struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs = {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000963 .destroy = vmw_framebuffer_surface_destroy,
964 .dirty = vmw_framebuffer_surface_dirty,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000965};
966
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200967static int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv,
968 struct vmw_surface *surface,
969 struct vmw_framebuffer **out,
Daniel Vetterdabdcdc2016-12-02 08:07:40 +0100970 const struct drm_mode_fb_cmd2
Sinclair Yehf89c6c32015-06-26 01:54:28 -0700971 *mode_cmd,
972 bool is_dmabuf_proxy)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000973
974{
975 struct drm_device *dev = dev_priv->dev;
976 struct vmw_framebuffer_surface *vfbs;
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200977 enum SVGA3dSurfaceFormat format;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000978 int ret;
Daniel Vetterdabdcdc2016-12-02 08:07:40 +0100979 struct drm_format_name_buf format_name;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000980
Sinclair Yehc8261a92015-06-26 01:23:42 -0700981 /* 3D is only supported on HWv8 and newer hosts */
982 if (dev_priv->active_display_unit == vmw_du_legacy)
Jakob Bornecrantz01e81412011-10-04 20:13:24 +0200983 return -ENOSYS;
984
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200985 /*
986 * Sanity checks.
987 */
988
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100989 /* Surface must be marked as a scanout. */
990 if (unlikely(!surface->scanout))
991 return -EINVAL;
992
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200993 if (unlikely(surface->mip_levels[0] != 1 ||
994 surface->num_sizes != 1 ||
Thomas Hellstromb360a3c2014-01-15 08:51:36 +0100995 surface->base_size.width < mode_cmd->width ||
996 surface->base_size.height < mode_cmd->height ||
997 surface->base_size.depth != 1)) {
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200998 DRM_ERROR("Incompatible surface dimensions "
999 "for requested mode.\n");
1000 return -EINVAL;
1001 }
1002
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001003 switch (mode_cmd->pixel_format) {
1004 case DRM_FORMAT_ARGB8888:
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001005 format = SVGA3D_A8R8G8B8;
1006 break;
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001007 case DRM_FORMAT_XRGB8888:
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001008 format = SVGA3D_X8R8G8B8;
1009 break;
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001010 case DRM_FORMAT_RGB565:
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001011 format = SVGA3D_R5G6B5;
1012 break;
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001013 case DRM_FORMAT_XRGB1555:
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001014 format = SVGA3D_A1R5G5B5;
1015 break;
1016 default:
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001017 DRM_ERROR("Invalid pixel format: %s\n",
1018 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001019 return -EINVAL;
1020 }
1021
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001022 /*
1023 * For DX, surface format validation is done when surface->scanout
1024 * is set.
1025 */
1026 if (!dev_priv->has_dx && format != surface->format) {
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001027 DRM_ERROR("Invalid surface format for requested mode.\n");
1028 return -EINVAL;
1029 }
1030
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001031 vfbs = kzalloc(sizeof(*vfbs), GFP_KERNEL);
1032 if (!vfbs) {
1033 ret = -ENOMEM;
1034 goto out_err1;
1035 }
1036
Ville Syrjäläa3f913c2016-12-14 22:48:59 +02001037 drm_helper_mode_fill_fb_struct(dev, &vfbs->base.base, mode_cmd);
Sinclair Yeh05c95012015-08-11 22:53:39 -07001038 vfbs->surface = vmw_surface_reference(surface);
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001039 vfbs->base.user_handle = mode_cmd->handles[0];
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001040 vfbs->is_dmabuf_proxy = is_dmabuf_proxy;
Thomas Hellstrom3a939a52010-10-05 12:43:03 +02001041
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001042 *out = &vfbs->base;
1043
Daniel Vetter80f0b5a2012-12-13 23:39:01 +01001044 ret = drm_framebuffer_init(dev, &vfbs->base.base,
1045 &vmw_framebuffer_surface_funcs);
1046 if (ret)
Sinclair Yeh05c95012015-08-11 22:53:39 -07001047 goto out_err2;
Daniel Vetter80f0b5a2012-12-13 23:39:01 +01001048
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001049 return 0;
1050
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001051out_err2:
Sinclair Yeh05c95012015-08-11 22:53:39 -07001052 vmw_surface_unreference(&surface);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001053 kfree(vfbs);
1054out_err1:
1055 return ret;
1056}
1057
1058/*
1059 * Dmabuf framebuffer code
1060 */
1061
Rashika Kheria847c5962014-01-06 22:18:10 +05301062static void vmw_framebuffer_dmabuf_destroy(struct drm_framebuffer *framebuffer)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001063{
1064 struct vmw_framebuffer_dmabuf *vfbd =
1065 vmw_framebuffer_to_vfbd(framebuffer);
1066
1067 drm_framebuffer_cleanup(framebuffer);
1068 vmw_dmabuf_unreference(&vfbd->buffer);
Thomas Hellstroma2787242015-06-29 12:55:07 -07001069 if (vfbd->base.user_obj)
1070 ttm_base_object_unref(&vfbd->base.user_obj);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001071
1072 kfree(vfbd);
1073}
1074
Rashika Kheria847c5962014-01-06 22:18:10 +05301075static int vmw_framebuffer_dmabuf_dirty(struct drm_framebuffer *framebuffer,
Thomas Hellstrom02b00162010-10-05 12:43:02 +02001076 struct drm_file *file_priv,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001077 unsigned flags, unsigned color,
1078 struct drm_clip_rect *clips,
1079 unsigned num_clips)
1080{
1081 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +02001082 struct vmw_framebuffer_dmabuf *vfbd =
1083 vmw_framebuffer_to_vfbd(framebuffer);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001084 struct drm_clip_rect norect;
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +02001085 int ret, increment = 1;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001086
Ville Syrjälä73e9efd2013-12-04 14:13:58 +02001087 drm_modeset_lock_all(dev_priv->dev);
1088
Thomas Hellstrom294adf72014-02-27 12:34:51 +01001089 ret = ttm_read_lock(&dev_priv->reservation_sem, true);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +02001090 if (unlikely(ret != 0)) {
1091 drm_modeset_unlock_all(dev_priv->dev);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +02001092 return ret;
Ville Syrjälä73e9efd2013-12-04 14:13:58 +02001093 }
Thomas Hellstrom3a939a52010-10-05 12:43:03 +02001094
Thomas Hellstromdf1c93b2010-01-13 22:28:36 +01001095 if (!num_clips) {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001096 num_clips = 1;
1097 clips = &norect;
1098 norect.x1 = norect.y1 = 0;
1099 norect.x2 = framebuffer->width;
1100 norect.y2 = framebuffer->height;
1101 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
1102 num_clips /= 2;
1103 increment = 2;
1104 }
1105
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001106 switch (dev_priv->active_display_unit) {
1107 case vmw_du_screen_target:
1108 ret = vmw_kms_stdu_dma(dev_priv, NULL, &vfbd->base, NULL,
1109 clips, NULL, num_clips, increment,
1110 true, true);
1111 break;
1112 case vmw_du_screen_object:
Thomas Hellstrom10b1e0c2015-06-26 02:14:27 -07001113 ret = vmw_kms_sou_do_dmabuf_dirty(dev_priv, &vfbd->base,
Thomas Hellstrom897b8182016-02-12 08:32:08 +01001114 clips, NULL, num_clips,
1115 increment, true, NULL);
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001116 break;
Thomas Hellstrom352b20d2015-06-29 12:57:37 -07001117 case vmw_du_legacy:
1118 ret = vmw_kms_ldu_do_dmabuf_dirty(dev_priv, &vfbd->base, 0, 0,
1119 clips, num_clips, increment);
1120 break;
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001121 default:
Thomas Hellstrom352b20d2015-06-29 12:57:37 -07001122 ret = -EINVAL;
1123 WARN_ONCE(true, "Dirty called with invalid display system.\n");
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001124 break;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +02001125 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001126
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07001127 vmw_fifo_flush(dev_priv, false);
Thomas Hellstrom294adf72014-02-27 12:34:51 +01001128 ttm_read_unlock(&dev_priv->reservation_sem);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +02001129
1130 drm_modeset_unlock_all(dev_priv->dev);
1131
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +02001132 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001133}
1134
Ville Syrjäläd7955fc2015-12-15 12:21:15 +01001135static const struct drm_framebuffer_funcs vmw_framebuffer_dmabuf_funcs = {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001136 .destroy = vmw_framebuffer_dmabuf_destroy,
1137 .dirty = vmw_framebuffer_dmabuf_dirty,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001138};
1139
Jakob Bornecrantz497a3ff2011-10-04 20:13:14 +02001140/**
Jakob Bornecrantz497a3ff2011-10-04 20:13:14 +02001141 * Pin the dmabuffer to the start of vram.
1142 */
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001143static int vmw_framebuffer_pin(struct vmw_framebuffer *vfb)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001144{
1145 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001146 struct vmw_dma_buffer *buf;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001147 int ret;
1148
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001149 buf = vfb->dmabuf ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
1150 vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001151
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001152 if (!buf)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001153 return 0;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001154
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001155 switch (dev_priv->active_display_unit) {
1156 case vmw_du_legacy:
1157 vmw_overlay_pause_all(dev_priv);
1158 ret = vmw_dmabuf_pin_in_start_of_vram(dev_priv, buf, false);
1159 vmw_overlay_resume_all(dev_priv);
1160 break;
1161 case vmw_du_screen_object:
1162 case vmw_du_screen_target:
1163 if (vfb->dmabuf)
1164 return vmw_dmabuf_pin_in_vram_or_gmr(dev_priv, buf,
1165 false);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001166
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001167 return vmw_dmabuf_pin_in_placement(dev_priv, buf,
1168 &vmw_mob_placement, false);
1169 default:
1170 return -EINVAL;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001171 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001172
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001173 return ret;
1174}
1175
1176static int vmw_framebuffer_unpin(struct vmw_framebuffer *vfb)
1177{
1178 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
1179 struct vmw_dma_buffer *buf;
1180
1181 buf = vfb->dmabuf ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
1182 vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
1183
1184 if (WARN_ON(!buf))
1185 return 0;
1186
1187 return vmw_dmabuf_unpin(dev_priv, buf, false);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001188}
1189
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001190/**
1191 * vmw_create_dmabuf_proxy - create a proxy surface for the DMA buf
1192 *
1193 * @dev: DRM device
1194 * @mode_cmd: parameters for the new surface
1195 * @dmabuf_mob: MOB backing the DMA buf
1196 * @srf_out: newly created surface
1197 *
1198 * When the content FB is a DMA buf, we create a surface as a proxy to the
1199 * same buffer. This way we can do a surface copy rather than a surface DMA.
1200 * This is a more efficient approach
1201 *
1202 * RETURNS:
1203 * 0 on success, error code otherwise
1204 */
1205static int vmw_create_dmabuf_proxy(struct drm_device *dev,
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001206 const struct drm_mode_fb_cmd2 *mode_cmd,
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001207 struct vmw_dma_buffer *dmabuf_mob,
1208 struct vmw_surface **srf_out)
1209{
1210 uint32_t format;
Thomas Hellstrom8cd9f252017-01-19 10:53:02 -08001211 struct drm_vmw_size content_base_size = {0};
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001212 struct vmw_resource *res;
Thomas Hellstroma50e2bf2016-01-08 20:29:40 +01001213 unsigned int bytes_pp;
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001214 struct drm_format_name_buf format_name;
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001215 int ret;
1216
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001217 switch (mode_cmd->pixel_format) {
1218 case DRM_FORMAT_ARGB8888:
1219 case DRM_FORMAT_XRGB8888:
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001220 format = SVGA3D_X8R8G8B8;
Thomas Hellstroma50e2bf2016-01-08 20:29:40 +01001221 bytes_pp = 4;
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001222 break;
1223
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001224 case DRM_FORMAT_RGB565:
1225 case DRM_FORMAT_XRGB1555:
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001226 format = SVGA3D_R5G6B5;
Thomas Hellstroma50e2bf2016-01-08 20:29:40 +01001227 bytes_pp = 2;
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001228 break;
1229
1230 case 8:
1231 format = SVGA3D_P8;
Thomas Hellstroma50e2bf2016-01-08 20:29:40 +01001232 bytes_pp = 1;
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001233 break;
1234
1235 default:
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001236 DRM_ERROR("Invalid framebuffer format %s\n",
1237 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001238 return -EINVAL;
1239 }
1240
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001241 content_base_size.width = mode_cmd->pitches[0] / bytes_pp;
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001242 content_base_size.height = mode_cmd->height;
1243 content_base_size.depth = 1;
1244
1245 ret = vmw_surface_gb_priv_define(dev,
1246 0, /* kernel visible only */
1247 0, /* flags */
1248 format,
1249 true, /* can be a scanout buffer */
1250 1, /* num of mip levels */
1251 0,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001252 0,
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001253 content_base_size,
1254 srf_out);
1255 if (ret) {
1256 DRM_ERROR("Failed to allocate proxy content buffer\n");
1257 return ret;
1258 }
1259
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001260 res = &(*srf_out)->res;
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001261
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001262 /* Reserve and switch the backing mob. */
1263 mutex_lock(&res->dev_priv->cmdbuf_mutex);
1264 (void) vmw_resource_reserve(res, false, true);
1265 vmw_dmabuf_unreference(&res->backup);
1266 res->backup = vmw_dmabuf_reference(dmabuf_mob);
1267 res->backup_offset = 0;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001268 vmw_resource_unreserve(res, false, NULL, 0);
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001269 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001270
1271 return 0;
1272}
1273
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001274
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001275
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001276static int vmw_kms_new_framebuffer_dmabuf(struct vmw_private *dev_priv,
1277 struct vmw_dma_buffer *dmabuf,
1278 struct vmw_framebuffer **out,
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001279 const struct drm_mode_fb_cmd2
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001280 *mode_cmd)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001281
1282{
1283 struct drm_device *dev = dev_priv->dev;
1284 struct vmw_framebuffer_dmabuf *vfbd;
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001285 unsigned int requested_size;
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001286 struct drm_format_name_buf format_name;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001287 int ret;
1288
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001289 requested_size = mode_cmd->height * mode_cmd->pitches[0];
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001290 if (unlikely(requested_size > dmabuf->base.num_pages * PAGE_SIZE)) {
1291 DRM_ERROR("Screen buffer object size is too small "
1292 "for requested mode.\n");
1293 return -EINVAL;
1294 }
1295
Jakob Bornecrantzc337ada2011-10-04 20:13:34 +02001296 /* Limited framebuffer color depth support for screen objects */
Sinclair Yehc8261a92015-06-26 01:23:42 -07001297 if (dev_priv->active_display_unit == vmw_du_screen_object) {
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001298 switch (mode_cmd->pixel_format) {
1299 case DRM_FORMAT_XRGB8888:
1300 case DRM_FORMAT_ARGB8888:
1301 break;
1302 case DRM_FORMAT_XRGB1555:
1303 case DRM_FORMAT_RGB565:
1304 break;
Jakob Bornecrantzc337ada2011-10-04 20:13:34 +02001305 default:
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001306 DRM_ERROR("Invalid pixel format: %s\n",
1307 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Jakob Bornecrantzc337ada2011-10-04 20:13:34 +02001308 return -EINVAL;
1309 }
1310 }
1311
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001312 vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL);
1313 if (!vfbd) {
1314 ret = -ENOMEM;
1315 goto out_err1;
1316 }
1317
Ville Syrjäläa3f913c2016-12-14 22:48:59 +02001318 drm_helper_mode_fill_fb_struct(dev, &vfbd->base.base, mode_cmd);
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001319 vfbd->base.dmabuf = true;
Sinclair Yeh05c95012015-08-11 22:53:39 -07001320 vfbd->buffer = vmw_dmabuf_reference(dmabuf);
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001321 vfbd->base.user_handle = mode_cmd->handles[0];
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001322 *out = &vfbd->base;
1323
Daniel Vetter80f0b5a2012-12-13 23:39:01 +01001324 ret = drm_framebuffer_init(dev, &vfbd->base.base,
1325 &vmw_framebuffer_dmabuf_funcs);
1326 if (ret)
Sinclair Yeh05c95012015-08-11 22:53:39 -07001327 goto out_err2;
Daniel Vetter80f0b5a2012-12-13 23:39:01 +01001328
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001329 return 0;
1330
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001331out_err2:
Sinclair Yeh05c95012015-08-11 22:53:39 -07001332 vmw_dmabuf_unreference(&dmabuf);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001333 kfree(vfbd);
1334out_err1:
1335 return ret;
1336}
1337
Sinclair Yeh810b3e162017-03-23 15:39:16 -07001338
1339/**
1340 * vmw_kms_srf_ok - check if a surface can be created
1341 *
1342 * @width: requested width
1343 * @height: requested height
1344 *
1345 * Surfaces need to be less than texture size
1346 */
1347static bool
1348vmw_kms_srf_ok(struct vmw_private *dev_priv, uint32_t width, uint32_t height)
1349{
1350 if (width > dev_priv->texture_max_width ||
1351 height > dev_priv->texture_max_height)
1352 return false;
1353
1354 return true;
1355}
1356
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001357/**
1358 * vmw_kms_new_framebuffer - Create a new framebuffer.
1359 *
1360 * @dev_priv: Pointer to device private struct.
1361 * @dmabuf: Pointer to dma buffer to wrap the kms framebuffer around.
1362 * Either @dmabuf or @surface must be NULL.
1363 * @surface: Pointer to a surface to wrap the kms framebuffer around.
1364 * Either @dmabuf or @surface must be NULL.
1365 * @only_2d: No presents will occur to this dma buffer based framebuffer. This
1366 * Helps the code to do some important optimizations.
1367 * @mode_cmd: Frame-buffer metadata.
1368 */
1369struct vmw_framebuffer *
1370vmw_kms_new_framebuffer(struct vmw_private *dev_priv,
1371 struct vmw_dma_buffer *dmabuf,
1372 struct vmw_surface *surface,
1373 bool only_2d,
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001374 const struct drm_mode_fb_cmd2 *mode_cmd)
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001375{
Sinclair Yeh05c95012015-08-11 22:53:39 -07001376 struct vmw_framebuffer *vfb = NULL;
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001377 bool is_dmabuf_proxy = false;
1378 int ret;
1379
1380 /*
1381 * We cannot use the SurfaceDMA command in an non-accelerated VM,
1382 * therefore, wrap the DMA buf in a surface so we can use the
1383 * SurfaceCopy command.
1384 */
Sinclair Yeh810b3e162017-03-23 15:39:16 -07001385 if (vmw_kms_srf_ok(dev_priv, mode_cmd->width, mode_cmd->height) &&
1386 dmabuf && only_2d &&
Sinclair Yehbbd5fef2017-06-02 07:44:53 +02001387 mode_cmd->width > 64 && /* Don't create a proxy for cursor */
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001388 dev_priv->active_display_unit == vmw_du_screen_target) {
1389 ret = vmw_create_dmabuf_proxy(dev_priv->dev, mode_cmd,
1390 dmabuf, &surface);
1391 if (ret)
1392 return ERR_PTR(ret);
1393
1394 is_dmabuf_proxy = true;
1395 }
1396
1397 /* Create the new framebuffer depending one what we have */
Sinclair Yeh05c95012015-08-11 22:53:39 -07001398 if (surface) {
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001399 ret = vmw_kms_new_framebuffer_surface(dev_priv, surface, &vfb,
1400 mode_cmd,
1401 is_dmabuf_proxy);
Sinclair Yeh05c95012015-08-11 22:53:39 -07001402
1403 /*
1404 * vmw_create_dmabuf_proxy() adds a reference that is no longer
1405 * needed
1406 */
1407 if (is_dmabuf_proxy)
1408 vmw_surface_unreference(&surface);
1409 } else if (dmabuf) {
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001410 ret = vmw_kms_new_framebuffer_dmabuf(dev_priv, dmabuf, &vfb,
1411 mode_cmd);
Sinclair Yeh05c95012015-08-11 22:53:39 -07001412 } else {
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001413 BUG();
Sinclair Yeh05c95012015-08-11 22:53:39 -07001414 }
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001415
1416 if (ret)
1417 return ERR_PTR(ret);
1418
1419 vfb->pin = vmw_framebuffer_pin;
1420 vfb->unpin = vmw_framebuffer_unpin;
1421
1422 return vfb;
1423}
1424
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001425/*
1426 * Generic Kernel modesetting functions
1427 */
1428
1429static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev,
1430 struct drm_file *file_priv,
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001431 const struct drm_mode_fb_cmd2 *mode_cmd)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001432{
1433 struct vmw_private *dev_priv = vmw_priv(dev);
1434 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
1435 struct vmw_framebuffer *vfb = NULL;
1436 struct vmw_surface *surface = NULL;
1437 struct vmw_dma_buffer *bo = NULL;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001438 struct ttm_base_object *user_obj;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001439 int ret;
1440
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001441 /**
1442 * This code should be conditioned on Screen Objects not being used.
1443 * If screen objects are used, we can allocate a GMR to hold the
1444 * requested framebuffer.
1445 */
1446
Xi Wang8a783892011-12-21 05:18:33 -05001447 if (!vmw_kms_validate_mode_vram(dev_priv,
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001448 mode_cmd->pitches[0],
1449 mode_cmd->height)) {
Sinclair Yehc8261a92015-06-26 01:23:42 -07001450 DRM_ERROR("Requested mode exceed bounding box limit.\n");
Jakob Bornecrantzd9826402011-11-03 21:03:04 +01001451 return ERR_PTR(-ENOMEM);
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001452 }
1453
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001454 /*
1455 * Take a reference on the user object of the resource
1456 * backing the kms fb. This ensures that user-space handle
1457 * lookups on that resource will always work as long as
1458 * it's registered with a kms framebuffer. This is important,
1459 * since vmw_execbuf_process identifies resources in the
1460 * command stream using user-space handles.
1461 */
1462
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001463 user_obj = ttm_base_object_lookup(tfile, mode_cmd->handles[0]);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001464 if (unlikely(user_obj == NULL)) {
1465 DRM_ERROR("Could not locate requested kms frame buffer.\n");
1466 return ERR_PTR(-ENOENT);
1467 }
1468
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001469 /**
1470 * End conditioned code.
1471 */
1472
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +01001473 /* returns either a dmabuf or surface */
1474 ret = vmw_user_lookup_handle(dev_priv, tfile,
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001475 mode_cmd->handles[0],
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +01001476 &surface, &bo);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001477 if (ret)
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +01001478 goto err_out;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001479
Sinclair Yeh810b3e162017-03-23 15:39:16 -07001480
1481 if (!bo &&
1482 !vmw_kms_srf_ok(dev_priv, mode_cmd->width, mode_cmd->height)) {
1483 DRM_ERROR("Surface size cannot exceed %dx%d",
1484 dev_priv->texture_max_width,
1485 dev_priv->texture_max_height);
1486 goto err_out;
1487 }
1488
1489
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001490 vfb = vmw_kms_new_framebuffer(dev_priv, bo, surface,
1491 !(dev_priv->capabilities & SVGA_CAP_3D),
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001492 mode_cmd);
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001493 if (IS_ERR(vfb)) {
1494 ret = PTR_ERR(vfb);
1495 goto err_out;
1496 }
Jakob Bornecrantz5ffdb652010-01-30 03:38:08 +00001497
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +01001498err_out:
1499 /* vmw_user_lookup_handle takes one ref so does new_fb */
1500 if (bo)
1501 vmw_dmabuf_unreference(&bo);
1502 if (surface)
1503 vmw_surface_unreference(&surface);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001504
1505 if (ret) {
1506 DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001507 ttm_base_object_unref(&user_obj);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01001508 return ERR_PTR(ret);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001509 } else
1510 vfb->user_obj = user_obj;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001511
1512 return &vfb->base;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001513}
1514
Sinclair Yehc46a3062017-03-23 14:24:53 -07001515
1516
1517/**
1518 * vmw_kms_atomic_check_modeset- validate state object for modeset changes
1519 *
1520 * @dev: DRM device
1521 * @state: the driver state object
1522 *
1523 * This is a simple wrapper around drm_atomic_helper_check_modeset() for
1524 * us to assign a value to mode->crtc_clock so that
1525 * drm_calc_timestamping_constants() won't throw an error message
1526 *
1527 * RETURNS
1528 * Zero for success or -errno
1529 */
Maarten Lankhorstbdc362f2017-07-12 10:13:33 +02001530static int
Sinclair Yehc46a3062017-03-23 14:24:53 -07001531vmw_kms_atomic_check_modeset(struct drm_device *dev,
1532 struct drm_atomic_state *state)
1533{
1534 struct drm_crtc_state *crtc_state;
1535 struct drm_crtc *crtc;
1536 struct vmw_private *dev_priv = vmw_priv(dev);
1537 int i;
1538
Maarten Lankhorstbdc362f2017-07-12 10:13:33 +02001539 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
Sinclair Yehc46a3062017-03-23 14:24:53 -07001540 unsigned long requested_bb_mem = 0;
1541
1542 if (dev_priv->active_display_unit == vmw_du_screen_target) {
1543 if (crtc->primary->fb) {
1544 int cpp = crtc->primary->fb->pitches[0] /
1545 crtc->primary->fb->width;
1546
1547 requested_bb_mem += crtc->mode.hdisplay * cpp *
1548 crtc->mode.vdisplay;
1549 }
1550
1551 if (requested_bb_mem > dev_priv->prim_bb_mem)
1552 return -EINVAL;
1553 }
1554 }
1555
1556 return drm_atomic_helper_check(dev, state);
1557}
1558
1559
Laurent Pincharte6ecefa2012-05-17 13:27:23 +02001560static const struct drm_mode_config_funcs vmw_kms_funcs = {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001561 .fb_create = vmw_kms_fb_create,
Sinclair Yehc46a3062017-03-23 14:24:53 -07001562 .atomic_check = vmw_kms_atomic_check_modeset,
1563 .atomic_commit = drm_atomic_helper_commit,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001564};
1565
Thomas Hellstromb9eb1a62015-04-02 02:39:45 -07001566static int vmw_kms_generic_present(struct vmw_private *dev_priv,
1567 struct drm_file *file_priv,
1568 struct vmw_framebuffer *vfb,
1569 struct vmw_surface *surface,
1570 uint32_t sid,
1571 int32_t destX, int32_t destY,
1572 struct drm_vmw_rect *clips,
1573 uint32_t num_clips)
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001574{
Thomas Hellstrom10b1e0c2015-06-26 02:14:27 -07001575 return vmw_kms_sou_do_surface_dirty(dev_priv, vfb, NULL, clips,
1576 &surface->res, destX, destY,
1577 num_clips, 1, NULL);
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001578}
1579
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001580
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001581int vmw_kms_present(struct vmw_private *dev_priv,
1582 struct drm_file *file_priv,
1583 struct vmw_framebuffer *vfb,
1584 struct vmw_surface *surface,
1585 uint32_t sid,
1586 int32_t destX, int32_t destY,
1587 struct drm_vmw_rect *clips,
1588 uint32_t num_clips)
1589{
Sinclair Yeh35c05122015-06-26 01:42:06 -07001590 int ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001591
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001592 switch (dev_priv->active_display_unit) {
1593 case vmw_du_screen_target:
1594 ret = vmw_kms_stdu_surface_dirty(dev_priv, vfb, NULL, clips,
1595 &surface->res, destX, destY,
1596 num_clips, 1, NULL);
1597 break;
1598 case vmw_du_screen_object:
1599 ret = vmw_kms_generic_present(dev_priv, file_priv, vfb, surface,
1600 sid, destX, destY, clips,
1601 num_clips);
1602 break;
1603 default:
1604 WARN_ONCE(true,
1605 "Present called with invalid display system.\n");
1606 ret = -ENOSYS;
1607 break;
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001608 }
Sinclair Yeh35c05122015-06-26 01:42:06 -07001609 if (ret)
1610 return ret;
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001611
Sinclair Yeh35c05122015-06-26 01:42:06 -07001612 vmw_fifo_flush(dev_priv, false);
Michel Dänzer0bef23f2011-08-31 07:42:50 +00001613
Sinclair Yeh35c05122015-06-26 01:42:06 -07001614 return 0;
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001615}
1616
Thomas Hellstrom578e6092016-02-12 09:45:42 +01001617static void
1618vmw_kms_create_hotplug_mode_update_property(struct vmw_private *dev_priv)
1619{
1620 if (dev_priv->hotplug_mode_update_property)
1621 return;
1622
1623 dev_priv->hotplug_mode_update_property =
1624 drm_property_create_range(dev_priv->dev,
1625 DRM_MODE_PROP_IMMUTABLE,
1626 "hotplug_mode_update", 0, 1);
1627
1628 if (!dev_priv->hotplug_mode_update_property)
1629 return;
1630
1631}
1632
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001633int vmw_kms_init(struct vmw_private *dev_priv)
1634{
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001635 struct drm_device *dev = dev_priv->dev;
1636 int ret;
1637
1638 drm_mode_config_init(dev);
1639 dev->mode_config.funcs = &vmw_kms_funcs;
1640 dev->mode_config.min_width = 1;
1641 dev->mode_config.min_height = 1;
Sinclair Yeh65ade7d2015-07-16 10:49:13 -07001642 dev->mode_config.max_width = dev_priv->texture_max_width;
1643 dev->mode_config.max_height = dev_priv->texture_max_height;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001644
Thomas Hellstrom578e6092016-02-12 09:45:42 +01001645 drm_mode_create_suggested_offset_properties(dev);
1646 vmw_kms_create_hotplug_mode_update_property(dev_priv);
1647
Sinclair Yeh35c05122015-06-26 01:42:06 -07001648 ret = vmw_kms_stdu_init_display(dev_priv);
1649 if (ret) {
1650 ret = vmw_kms_sou_init_display(dev_priv);
1651 if (ret) /* Fallback */
1652 ret = vmw_kms_ldu_init_display(dev_priv);
1653 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001654
Sinclair Yehc8261a92015-06-26 01:23:42 -07001655 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001656}
1657
1658int vmw_kms_close(struct vmw_private *dev_priv)
1659{
Daniel Vetter5f58e972017-06-26 18:19:48 +02001660 int ret = 0;
Sinclair Yehc8261a92015-06-26 01:23:42 -07001661
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001662 /*
1663 * Docs says we should take the lock before calling this function
1664 * but since it destroys encoders and our destructor calls
1665 * drm_encoder_cleanup which takes the lock we deadlock.
1666 */
1667 drm_mode_config_cleanup(dev_priv->dev);
Daniel Vetter5f58e972017-06-26 18:19:48 +02001668 if (dev_priv->active_display_unit == vmw_du_legacy)
Sinclair Yehc8261a92015-06-26 01:23:42 -07001669 ret = vmw_kms_ldu_close_display(dev_priv);
1670
1671 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001672}
1673
1674int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
1675 struct drm_file *file_priv)
1676{
1677 struct drm_vmw_cursor_bypass_arg *arg = data;
1678 struct vmw_display_unit *du;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001679 struct drm_crtc *crtc;
1680 int ret = 0;
1681
1682
1683 mutex_lock(&dev->mode_config.mutex);
1684 if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
1685
1686 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1687 du = vmw_crtc_to_du(crtc);
1688 du->hotspot_x = arg->xhot;
1689 du->hotspot_y = arg->yhot;
1690 }
1691
1692 mutex_unlock(&dev->mode_config.mutex);
1693 return 0;
1694 }
1695
Rob Clarka4cd5d62014-07-17 23:30:02 -04001696 crtc = drm_crtc_find(dev, arg->crtc_id);
1697 if (!crtc) {
Ville Syrjälä4ae87ff2013-10-17 13:35:05 +03001698 ret = -ENOENT;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001699 goto out;
1700 }
1701
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001702 du = vmw_crtc_to_du(crtc);
1703
1704 du->hotspot_x = arg->xhot;
1705 du->hotspot_y = arg->yhot;
1706
1707out:
1708 mutex_unlock(&dev->mode_config.mutex);
1709
1710 return ret;
1711}
1712
1713int vmw_kms_write_svga(struct vmw_private *vmw_priv,
1714 unsigned width, unsigned height, unsigned pitch,
1715 unsigned bpp, unsigned depth)
1716{
1717 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1718 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch);
1719 else if (vmw_fifo_have_pitchlock(vmw_priv))
Thomas Hellstromb76ff5e2015-10-28 10:44:04 +01001720 vmw_mmio_write(pitch, vmw_priv->mmio_virt +
1721 SVGA_FIFO_PITCHLOCK);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001722 vmw_write(vmw_priv, SVGA_REG_WIDTH, width);
1723 vmw_write(vmw_priv, SVGA_REG_HEIGHT, height);
1724 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp);
1725
1726 if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) {
1727 DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n",
1728 depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH));
1729 return -EINVAL;
1730 }
1731
1732 return 0;
1733}
1734
1735int vmw_kms_save_vga(struct vmw_private *vmw_priv)
1736{
1737 struct vmw_vga_topology_state *save;
1738 uint32_t i;
1739
1740 vmw_priv->vga_width = vmw_read(vmw_priv, SVGA_REG_WIDTH);
1741 vmw_priv->vga_height = vmw_read(vmw_priv, SVGA_REG_HEIGHT);
1742 vmw_priv->vga_bpp = vmw_read(vmw_priv, SVGA_REG_BITS_PER_PIXEL);
1743 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1744 vmw_priv->vga_pitchlock =
1745 vmw_read(vmw_priv, SVGA_REG_PITCHLOCK);
1746 else if (vmw_fifo_have_pitchlock(vmw_priv))
Thomas Hellstromb76ff5e2015-10-28 10:44:04 +01001747 vmw_priv->vga_pitchlock = vmw_mmio_read(vmw_priv->mmio_virt +
1748 SVGA_FIFO_PITCHLOCK);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001749
1750 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1751 return 0;
1752
1753 vmw_priv->num_displays = vmw_read(vmw_priv,
1754 SVGA_REG_NUM_GUEST_DISPLAYS);
1755
1756 if (vmw_priv->num_displays == 0)
1757 vmw_priv->num_displays = 1;
1758
1759 for (i = 0; i < vmw_priv->num_displays; ++i) {
1760 save = &vmw_priv->vga_save[i];
1761 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1762 save->primary = vmw_read(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY);
1763 save->pos_x = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_X);
1764 save->pos_y = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y);
1765 save->width = vmw_read(vmw_priv, SVGA_REG_DISPLAY_WIDTH);
1766 save->height = vmw_read(vmw_priv, SVGA_REG_DISPLAY_HEIGHT);
1767 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
1768 if (i == 0 && vmw_priv->num_displays == 1 &&
1769 save->width == 0 && save->height == 0) {
1770
1771 /*
1772 * It should be fairly safe to assume that these
1773 * values are uninitialized.
1774 */
1775
1776 save->width = vmw_priv->vga_width - save->pos_x;
1777 save->height = vmw_priv->vga_height - save->pos_y;
1778 }
1779 }
1780
1781 return 0;
1782}
1783
1784int vmw_kms_restore_vga(struct vmw_private *vmw_priv)
1785{
1786 struct vmw_vga_topology_state *save;
1787 uint32_t i;
1788
1789 vmw_write(vmw_priv, SVGA_REG_WIDTH, vmw_priv->vga_width);
1790 vmw_write(vmw_priv, SVGA_REG_HEIGHT, vmw_priv->vga_height);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001791 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, vmw_priv->vga_bpp);
1792 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1793 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK,
1794 vmw_priv->vga_pitchlock);
1795 else if (vmw_fifo_have_pitchlock(vmw_priv))
Thomas Hellstromb76ff5e2015-10-28 10:44:04 +01001796 vmw_mmio_write(vmw_priv->vga_pitchlock,
1797 vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001798
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001799 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1800 return 0;
1801
1802 for (i = 0; i < vmw_priv->num_displays; ++i) {
1803 save = &vmw_priv->vga_save[i];
1804 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1805 vmw_write(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY, save->primary);
1806 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_X, save->pos_x);
1807 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y, save->pos_y);
1808 vmw_write(vmw_priv, SVGA_REG_DISPLAY_WIDTH, save->width);
1809 vmw_write(vmw_priv, SVGA_REG_DISPLAY_HEIGHT, save->height);
1810 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
1811 }
1812
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001813 return 0;
1814}
Jakob Bornecrantzd8bd19d2010-06-01 11:54:20 +02001815
Thomas Hellstrome133e732010-10-05 12:43:04 +02001816bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
1817 uint32_t pitch,
1818 uint32_t height)
1819{
Sinclair Yeh35c05122015-06-26 01:42:06 -07001820 return ((u64) pitch * (u64) height) < (u64)
1821 ((dev_priv->active_display_unit == vmw_du_screen_target) ?
1822 dev_priv->prim_bb_mem : dev_priv->vram_size);
Thomas Hellstrome133e732010-10-05 12:43:04 +02001823}
1824
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001825
1826/**
1827 * Function called by DRM code called with vbl_lock held.
1828 */
Thierry Reding88e72712015-09-24 18:35:31 +02001829u32 vmw_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
Thomas Hellstrom7a1c2f62010-10-01 10:21:49 +02001830{
1831 return 0;
1832}
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001833
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001834/**
1835 * Function called by DRM code called with vbl_lock held.
1836 */
Thierry Reding88e72712015-09-24 18:35:31 +02001837int vmw_enable_vblank(struct drm_device *dev, unsigned int pipe)
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001838{
1839 return -ENOSYS;
1840}
1841
1842/**
1843 * Function called by DRM code called with vbl_lock held.
1844 */
Thierry Reding88e72712015-09-24 18:35:31 +02001845void vmw_disable_vblank(struct drm_device *dev, unsigned int pipe)
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001846{
1847}
1848
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001849
1850/*
1851 * Small shared kms functions.
1852 */
1853
Rashika Kheria847c5962014-01-06 22:18:10 +05301854static int vmw_du_update_layout(struct vmw_private *dev_priv, unsigned num,
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001855 struct drm_vmw_rect *rects)
1856{
1857 struct drm_device *dev = dev_priv->dev;
1858 struct vmw_display_unit *du;
1859 struct drm_connector *con;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001860
1861 mutex_lock(&dev->mode_config.mutex);
1862
1863#if 0
Thomas Hellstrom6ea77d12011-10-04 20:13:36 +02001864 {
1865 unsigned int i;
1866
1867 DRM_INFO("%s: new layout ", __func__);
1868 for (i = 0; i < num; i++)
1869 DRM_INFO("(%i, %i %ux%u) ", rects[i].x, rects[i].y,
1870 rects[i].w, rects[i].h);
1871 DRM_INFO("\n");
1872 }
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001873#endif
1874
1875 list_for_each_entry(con, &dev->mode_config.connector_list, head) {
1876 du = vmw_connector_to_du(con);
1877 if (num > du->unit) {
1878 du->pref_width = rects[du->unit].w;
1879 du->pref_height = rects[du->unit].h;
1880 du->pref_active = true;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001881 du->gui_x = rects[du->unit].x;
1882 du->gui_y = rects[du->unit].y;
Thomas Hellstrom578e6092016-02-12 09:45:42 +01001883 drm_object_property_set_value
1884 (&con->base, dev->mode_config.suggested_x_property,
1885 du->gui_x);
1886 drm_object_property_set_value
1887 (&con->base, dev->mode_config.suggested_y_property,
1888 du->gui_y);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001889 } else {
1890 du->pref_width = 800;
1891 du->pref_height = 600;
1892 du->pref_active = false;
Thomas Hellstrom578e6092016-02-12 09:45:42 +01001893 drm_object_property_set_value
1894 (&con->base, dev->mode_config.suggested_x_property,
1895 0);
1896 drm_object_property_set_value
1897 (&con->base, dev->mode_config.suggested_y_property,
1898 0);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001899 }
1900 con->status = vmw_du_connector_detect(con, true);
1901 }
1902
1903 mutex_unlock(&dev->mode_config.mutex);
Thomas Hellstrom578e6092016-02-12 09:45:42 +01001904 drm_sysfs_hotplug_event(dev);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001905
1906 return 0;
1907}
1908
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02001909int vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
1910 u16 *r, u16 *g, u16 *b,
Daniel Vetter6d124ff2017-04-03 10:33:01 +02001911 uint32_t size,
1912 struct drm_modeset_acquire_ctx *ctx)
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001913{
1914 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
1915 int i;
1916
1917 for (i = 0; i < size; i++) {
1918 DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i,
1919 r[i], g[i], b[i]);
1920 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 0, r[i] >> 8);
1921 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 1, g[i] >> 8);
1922 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 2, b[i] >> 8);
1923 }
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02001924
1925 return 0;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001926}
1927
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02001928int vmw_du_connector_dpms(struct drm_connector *connector, int mode)
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001929{
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02001930 return 0;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001931}
1932
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001933enum drm_connector_status
1934vmw_du_connector_detect(struct drm_connector *connector, bool force)
1935{
1936 uint32_t num_displays;
1937 struct drm_device *dev = connector->dev;
1938 struct vmw_private *dev_priv = vmw_priv(dev);
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001939 struct vmw_display_unit *du = vmw_connector_to_du(connector);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001940
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001941 num_displays = vmw_read(dev_priv, SVGA_REG_NUM_DISPLAYS);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001942
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001943 return ((vmw_connector_to_du(connector)->unit < num_displays &&
1944 du->pref_active) ?
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001945 connector_status_connected : connector_status_disconnected);
1946}
1947
1948static struct drm_display_mode vmw_kms_connector_builtin[] = {
1949 /* 640x480@60Hz */
1950 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
1951 752, 800, 0, 480, 489, 492, 525, 0,
1952 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1953 /* 800x600@60Hz */
1954 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
1955 968, 1056, 0, 600, 601, 605, 628, 0,
1956 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1957 /* 1024x768@60Hz */
1958 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
1959 1184, 1344, 0, 768, 771, 777, 806, 0,
1960 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1961 /* 1152x864@75Hz */
1962 { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
1963 1344, 1600, 0, 864, 865, 868, 900, 0,
1964 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1965 /* 1280x768@60Hz */
1966 { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344,
1967 1472, 1664, 0, 768, 771, 778, 798, 0,
1968 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1969 /* 1280x800@60Hz */
1970 { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352,
1971 1480, 1680, 0, 800, 803, 809, 831, 0,
1972 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
1973 /* 1280x960@60Hz */
1974 { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376,
1975 1488, 1800, 0, 960, 961, 964, 1000, 0,
1976 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1977 /* 1280x1024@60Hz */
1978 { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328,
1979 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
1980 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1981 /* 1360x768@60Hz */
1982 { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424,
1983 1536, 1792, 0, 768, 771, 777, 795, 0,
1984 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1985 /* 1440x1050@60Hz */
1986 { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488,
1987 1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
1988 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1989 /* 1440x900@60Hz */
1990 { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520,
1991 1672, 1904, 0, 900, 903, 909, 934, 0,
1992 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1993 /* 1600x1200@60Hz */
1994 { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664,
1995 1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
1996 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1997 /* 1680x1050@60Hz */
1998 { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784,
1999 1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
2000 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2001 /* 1792x1344@60Hz */
2002 { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920,
2003 2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
2004 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2005 /* 1853x1392@60Hz */
2006 { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952,
2007 2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
2008 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2009 /* 1920x1200@60Hz */
2010 { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056,
2011 2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
2012 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2013 /* 1920x1440@60Hz */
2014 { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048,
2015 2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
2016 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2017 /* 2560x1600@60Hz */
2018 { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752,
2019 3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
2020 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2021 /* Terminate */
2022 { DRM_MODE("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) },
2023};
2024
Thomas Hellstrom1543b4d2011-11-02 09:43:10 +01002025/**
2026 * vmw_guess_mode_timing - Provide fake timings for a
2027 * 60Hz vrefresh mode.
2028 *
2029 * @mode - Pointer to a struct drm_display_mode with hdisplay and vdisplay
2030 * members filled in.
2031 */
Thomas Hellstroma2787242015-06-29 12:55:07 -07002032void vmw_guess_mode_timing(struct drm_display_mode *mode)
Thomas Hellstrom1543b4d2011-11-02 09:43:10 +01002033{
2034 mode->hsync_start = mode->hdisplay + 50;
2035 mode->hsync_end = mode->hsync_start + 50;
2036 mode->htotal = mode->hsync_end + 50;
2037
2038 mode->vsync_start = mode->vdisplay + 50;
2039 mode->vsync_end = mode->vsync_start + 50;
2040 mode->vtotal = mode->vsync_end + 50;
2041
2042 mode->clock = (u32)mode->htotal * (u32)mode->vtotal / 100 * 6;
2043 mode->vrefresh = drm_mode_vrefresh(mode);
2044}
2045
2046
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002047int vmw_du_connector_fill_modes(struct drm_connector *connector,
2048 uint32_t max_width, uint32_t max_height)
2049{
2050 struct vmw_display_unit *du = vmw_connector_to_du(connector);
2051 struct drm_device *dev = connector->dev;
2052 struct vmw_private *dev_priv = vmw_priv(dev);
2053 struct drm_display_mode *mode = NULL;
2054 struct drm_display_mode *bmode;
2055 struct drm_display_mode prefmode = { DRM_MODE("preferred",
2056 DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
2057 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2058 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
2059 };
2060 int i;
Sinclair Yeh7c20d212016-06-29 11:29:47 -07002061 u32 assumed_bpp = 4;
Sinclair Yeh9a723842014-10-31 09:58:06 +01002062
Sinclair Yeh04319d82016-06-29 12:15:48 -07002063 if (dev_priv->assume_16bpp)
2064 assumed_bpp = 2;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002065
Sinclair Yeh35c05122015-06-26 01:42:06 -07002066 if (dev_priv->active_display_unit == vmw_du_screen_target) {
2067 max_width = min(max_width, dev_priv->stdu_max_width);
Sinclair Yeh28c95422017-03-27 11:12:27 -07002068 max_width = min(max_width, dev_priv->texture_max_width);
2069
Sinclair Yeh35c05122015-06-26 01:42:06 -07002070 max_height = min(max_height, dev_priv->stdu_max_height);
Sinclair Yeh28c95422017-03-27 11:12:27 -07002071 max_height = min(max_height, dev_priv->texture_max_height);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002072 }
2073
2074 /* Add preferred mode */
Sinclair Yehc8261a92015-06-26 01:23:42 -07002075 mode = drm_mode_duplicate(dev, &prefmode);
2076 if (!mode)
2077 return 0;
2078 mode->hdisplay = du->pref_width;
2079 mode->vdisplay = du->pref_height;
2080 vmw_guess_mode_timing(mode);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002081
Sinclair Yehc8261a92015-06-26 01:23:42 -07002082 if (vmw_kms_validate_mode_vram(dev_priv,
2083 mode->hdisplay * assumed_bpp,
2084 mode->vdisplay)) {
2085 drm_mode_probed_add(connector, mode);
2086 } else {
2087 drm_mode_destroy(dev, mode);
2088 mode = NULL;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002089 }
2090
Sinclair Yehc8261a92015-06-26 01:23:42 -07002091 if (du->pref_mode) {
2092 list_del_init(&du->pref_mode->head);
2093 drm_mode_destroy(dev, du->pref_mode);
2094 }
2095
2096 /* mode might be null here, this is intended */
2097 du->pref_mode = mode;
2098
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002099 for (i = 0; vmw_kms_connector_builtin[i].type != 0; i++) {
2100 bmode = &vmw_kms_connector_builtin[i];
2101 if (bmode->hdisplay > max_width ||
2102 bmode->vdisplay > max_height)
2103 continue;
2104
Sinclair Yeh9a723842014-10-31 09:58:06 +01002105 if (!vmw_kms_validate_mode_vram(dev_priv,
2106 bmode->hdisplay * assumed_bpp,
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002107 bmode->vdisplay))
2108 continue;
2109
2110 mode = drm_mode_duplicate(dev, bmode);
2111 if (!mode)
2112 return 0;
2113 mode->vrefresh = drm_mode_vrefresh(mode);
2114
2115 drm_mode_probed_add(connector, mode);
2116 }
2117
Ville Syrjälä6af3e652015-12-03 23:14:14 +02002118 drm_mode_connector_list_update(connector);
Thomas Hellstromf6b05002015-06-29 12:59:58 -07002119 /* Move the prefered mode first, help apps pick the right mode. */
2120 drm_mode_sort(&connector->modes);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002121
2122 return 1;
2123}
2124
2125int vmw_du_connector_set_property(struct drm_connector *connector,
2126 struct drm_property *property,
2127 uint64_t val)
2128{
Thomas Hellstrom76404ac2016-02-12 09:55:45 +01002129 struct vmw_display_unit *du = vmw_connector_to_du(connector);
2130 struct vmw_private *dev_priv = vmw_priv(connector->dev);
2131
2132 if (property == dev_priv->implicit_placement_property)
2133 du->is_implicit = val;
2134
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002135 return 0;
2136}
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002137
2138
Sinclair Yeh9c2542a2017-03-23 11:33:39 -07002139
Sinclair Yehd7721ca2017-03-23 11:48:44 -07002140/**
2141 * vmw_du_connector_atomic_set_property - Atomic version of get property
2142 *
2143 * @crtc - crtc the property is associated with
2144 *
2145 * Returns:
2146 * Zero on success, negative errno on failure.
2147 */
2148int
2149vmw_du_connector_atomic_set_property(struct drm_connector *connector,
2150 struct drm_connector_state *state,
2151 struct drm_property *property,
2152 uint64_t val)
2153{
2154 struct vmw_private *dev_priv = vmw_priv(connector->dev);
2155 struct vmw_connector_state *vcs = vmw_connector_state_to_vcs(state);
2156 struct vmw_display_unit *du = vmw_connector_to_du(connector);
2157
2158
2159 if (property == dev_priv->implicit_placement_property) {
2160 vcs->is_implicit = val;
2161
2162 /*
2163 * We should really be doing a drm_atomic_commit() to
2164 * commit the new state, but since this doesn't cause
2165 * an immedate state change, this is probably ok
2166 */
2167 du->is_implicit = vcs->is_implicit;
2168 } else {
2169 return -EINVAL;
2170 }
2171
2172 return 0;
2173}
2174
2175
2176/**
2177 * vmw_du_connector_atomic_get_property - Atomic version of get property
2178 *
2179 * @connector - connector the property is associated with
2180 *
2181 * Returns:
2182 * Zero on success, negative errno on failure.
2183 */
2184int
2185vmw_du_connector_atomic_get_property(struct drm_connector *connector,
2186 const struct drm_connector_state *state,
2187 struct drm_property *property,
2188 uint64_t *val)
2189{
2190 struct vmw_private *dev_priv = vmw_priv(connector->dev);
2191 struct vmw_connector_state *vcs = vmw_connector_state_to_vcs(state);
2192
2193 if (property == dev_priv->implicit_placement_property)
2194 *val = vcs->is_implicit;
2195 else {
2196 DRM_ERROR("Invalid Property %s\n", property->name);
2197 return -EINVAL;
2198 }
2199
2200 return 0;
2201}
2202
2203
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002204int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data,
2205 struct drm_file *file_priv)
2206{
2207 struct vmw_private *dev_priv = vmw_priv(dev);
2208 struct drm_vmw_update_layout_arg *arg =
2209 (struct drm_vmw_update_layout_arg *)data;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002210 void __user *user_rects;
2211 struct drm_vmw_rect *rects;
2212 unsigned rects_size;
2213 int ret;
2214 int i;
Sinclair Yeh65ade7d2015-07-16 10:49:13 -07002215 u64 total_pixels = 0;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002216 struct drm_mode_config *mode_config = &dev->mode_config;
Sinclair Yehc8261a92015-06-26 01:23:42 -07002217 struct drm_vmw_rect bounding_box = {0};
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002218
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002219 if (!arg->num_outputs) {
2220 struct drm_vmw_rect def_rect = {0, 0, 800, 600};
2221 vmw_du_update_layout(dev_priv, 1, &def_rect);
Thomas Hellstrom5151adb2015-03-09 01:56:21 -07002222 return 0;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002223 }
2224
2225 rects_size = arg->num_outputs * sizeof(struct drm_vmw_rect);
Xi Wangbab9efc2011-11-28 12:25:43 +01002226 rects = kcalloc(arg->num_outputs, sizeof(struct drm_vmw_rect),
2227 GFP_KERNEL);
Thomas Hellstrom5151adb2015-03-09 01:56:21 -07002228 if (unlikely(!rects))
2229 return -ENOMEM;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002230
2231 user_rects = (void __user *)(unsigned long)arg->rects;
2232 ret = copy_from_user(rects, user_rects, rects_size);
2233 if (unlikely(ret != 0)) {
2234 DRM_ERROR("Failed to get rects.\n");
2235 ret = -EFAULT;
2236 goto out_free;
2237 }
2238
2239 for (i = 0; i < arg->num_outputs; ++i) {
Xi Wangbab9efc2011-11-28 12:25:43 +01002240 if (rects[i].x < 0 ||
2241 rects[i].y < 0 ||
2242 rects[i].x + rects[i].w > mode_config->max_width ||
2243 rects[i].y + rects[i].h > mode_config->max_height) {
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002244 DRM_ERROR("Invalid GUI layout.\n");
2245 ret = -EINVAL;
2246 goto out_free;
2247 }
Sinclair Yehc8261a92015-06-26 01:23:42 -07002248
2249 /*
2250 * bounding_box.w and bunding_box.h are used as
2251 * lower-right coordinates
2252 */
2253 if (rects[i].x + rects[i].w > bounding_box.w)
2254 bounding_box.w = rects[i].x + rects[i].w;
2255
2256 if (rects[i].y + rects[i].h > bounding_box.h)
2257 bounding_box.h = rects[i].y + rects[i].h;
Sinclair Yeh65ade7d2015-07-16 10:49:13 -07002258
2259 total_pixels += (u64) rects[i].w * (u64) rects[i].h;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002260 }
2261
Sinclair Yeh65ade7d2015-07-16 10:49:13 -07002262 if (dev_priv->active_display_unit == vmw_du_screen_target) {
2263 /*
2264 * For Screen Targets, the limits for a toplogy are:
2265 * 1. Bounding box (assuming 32bpp) must be < prim_bb_mem
2266 * 2. Total pixels (assuming 32bpp) must be < prim_bb_mem
2267 */
Thomas Hellstrom0f580382017-01-19 11:01:04 -08002268 u64 bb_mem = (u64) bounding_box.w * bounding_box.h * 4;
Sinclair Yeh65ade7d2015-07-16 10:49:13 -07002269 u64 pixel_mem = total_pixels * 4;
2270
2271 if (bb_mem > dev_priv->prim_bb_mem) {
2272 DRM_ERROR("Topology is beyond supported limits.\n");
Sinclair Yeh35c05122015-06-26 01:42:06 -07002273 ret = -EINVAL;
2274 goto out_free;
2275 }
2276
Sinclair Yeh65ade7d2015-07-16 10:49:13 -07002277 if (pixel_mem > dev_priv->prim_bb_mem) {
2278 DRM_ERROR("Combined output size too large\n");
2279 ret = -EINVAL;
2280 goto out_free;
2281 }
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002282 }
2283
2284 vmw_du_update_layout(dev_priv, arg->num_outputs, rects);
2285
2286out_free:
2287 kfree(rects);
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002288 return ret;
2289}
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07002290
2291/**
2292 * vmw_kms_helper_dirty - Helper to build commands and perform actions based
2293 * on a set of cliprects and a set of display units.
2294 *
2295 * @dev_priv: Pointer to a device private structure.
2296 * @framebuffer: Pointer to the framebuffer on which to perform the actions.
2297 * @clips: A set of struct drm_clip_rect. Either this os @vclips must be NULL.
2298 * Cliprects are given in framebuffer coordinates.
2299 * @vclips: A set of struct drm_vmw_rect cliprects. Either this or @clips must
2300 * be NULL. Cliprects are given in source coordinates.
2301 * @dest_x: X coordinate offset for the crtc / destination clip rects.
2302 * @dest_y: Y coordinate offset for the crtc / destination clip rects.
2303 * @num_clips: Number of cliprects in the @clips or @vclips array.
2304 * @increment: Integer with which to increment the clip counter when looping.
2305 * Used to skip a predetermined number of clip rects.
2306 * @dirty: Closure structure. See the description of struct vmw_kms_dirty.
2307 */
2308int vmw_kms_helper_dirty(struct vmw_private *dev_priv,
2309 struct vmw_framebuffer *framebuffer,
2310 const struct drm_clip_rect *clips,
2311 const struct drm_vmw_rect *vclips,
2312 s32 dest_x, s32 dest_y,
2313 int num_clips,
2314 int increment,
2315 struct vmw_kms_dirty *dirty)
2316{
2317 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
2318 struct drm_crtc *crtc;
2319 u32 num_units = 0;
2320 u32 i, k;
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07002321
2322 dirty->dev_priv = dev_priv;
2323
2324 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
2325 if (crtc->primary->fb != &framebuffer->base)
2326 continue;
2327 units[num_units++] = vmw_crtc_to_du(crtc);
2328 }
2329
2330 for (k = 0; k < num_units; k++) {
2331 struct vmw_display_unit *unit = units[k];
2332 s32 crtc_x = unit->crtc.x;
2333 s32 crtc_y = unit->crtc.y;
2334 s32 crtc_width = unit->crtc.mode.hdisplay;
2335 s32 crtc_height = unit->crtc.mode.vdisplay;
2336 const struct drm_clip_rect *clips_ptr = clips;
2337 const struct drm_vmw_rect *vclips_ptr = vclips;
2338
2339 dirty->unit = unit;
2340 if (dirty->fifo_reserve_size > 0) {
2341 dirty->cmd = vmw_fifo_reserve(dev_priv,
2342 dirty->fifo_reserve_size);
2343 if (!dirty->cmd) {
2344 DRM_ERROR("Couldn't reserve fifo space "
2345 "for dirty blits.\n");
Christian Engelmayerf3b8c0c2015-09-19 00:32:24 +02002346 return -ENOMEM;
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07002347 }
2348 memset(dirty->cmd, 0, dirty->fifo_reserve_size);
2349 }
2350 dirty->num_hits = 0;
2351 for (i = 0; i < num_clips; i++, clips_ptr += increment,
2352 vclips_ptr += increment) {
2353 s32 clip_left;
2354 s32 clip_top;
2355
2356 /*
2357 * Select clip array type. Note that integer type
2358 * in @clips is unsigned short, whereas in @vclips
2359 * it's 32-bit.
2360 */
2361 if (clips) {
2362 dirty->fb_x = (s32) clips_ptr->x1;
2363 dirty->fb_y = (s32) clips_ptr->y1;
2364 dirty->unit_x2 = (s32) clips_ptr->x2 + dest_x -
2365 crtc_x;
2366 dirty->unit_y2 = (s32) clips_ptr->y2 + dest_y -
2367 crtc_y;
2368 } else {
2369 dirty->fb_x = vclips_ptr->x;
2370 dirty->fb_y = vclips_ptr->y;
2371 dirty->unit_x2 = dirty->fb_x + vclips_ptr->w +
2372 dest_x - crtc_x;
2373 dirty->unit_y2 = dirty->fb_y + vclips_ptr->h +
2374 dest_y - crtc_y;
2375 }
2376
2377 dirty->unit_x1 = dirty->fb_x + dest_x - crtc_x;
2378 dirty->unit_y1 = dirty->fb_y + dest_y - crtc_y;
2379
2380 /* Skip this clip if it's outside the crtc region */
2381 if (dirty->unit_x1 >= crtc_width ||
2382 dirty->unit_y1 >= crtc_height ||
2383 dirty->unit_x2 <= 0 || dirty->unit_y2 <= 0)
2384 continue;
2385
2386 /* Clip right and bottom to crtc limits */
2387 dirty->unit_x2 = min_t(s32, dirty->unit_x2,
2388 crtc_width);
2389 dirty->unit_y2 = min_t(s32, dirty->unit_y2,
2390 crtc_height);
2391
2392 /* Clip left and top to crtc limits */
2393 clip_left = min_t(s32, dirty->unit_x1, 0);
2394 clip_top = min_t(s32, dirty->unit_y1, 0);
2395 dirty->unit_x1 -= clip_left;
2396 dirty->unit_y1 -= clip_top;
2397 dirty->fb_x -= clip_left;
2398 dirty->fb_y -= clip_top;
2399
2400 dirty->clip(dirty);
2401 }
2402
2403 dirty->fifo_commit(dirty);
2404 }
2405
2406 return 0;
2407}
2408
2409/**
2410 * vmw_kms_helper_buffer_prepare - Reserve and validate a buffer object before
2411 * command submission.
2412 *
2413 * @dev_priv. Pointer to a device private structure.
2414 * @buf: The buffer object
2415 * @interruptible: Whether to perform waits as interruptible.
2416 * @validate_as_mob: Whether the buffer should be validated as a MOB. If false,
2417 * The buffer will be validated as a GMR. Already pinned buffers will not be
2418 * validated.
2419 *
2420 * Returns 0 on success, negative error code on failure, -ERESTARTSYS if
2421 * interrupted by a signal.
2422 */
2423int vmw_kms_helper_buffer_prepare(struct vmw_private *dev_priv,
2424 struct vmw_dma_buffer *buf,
2425 bool interruptible,
2426 bool validate_as_mob)
2427{
2428 struct ttm_buffer_object *bo = &buf->base;
2429 int ret;
2430
Christian Königdfd5e502016-04-06 11:12:03 +02002431 ttm_bo_reserve(bo, false, false, NULL);
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07002432 ret = vmw_validate_single_buffer(dev_priv, bo, interruptible,
2433 validate_as_mob);
2434 if (ret)
2435 ttm_bo_unreserve(bo);
2436
2437 return ret;
2438}
2439
2440/**
2441 * vmw_kms_helper_buffer_revert - Undo the actions of
2442 * vmw_kms_helper_buffer_prepare.
2443 *
2444 * @res: Pointer to the buffer object.
2445 *
2446 * Helper to be used if an error forces the caller to undo the actions of
2447 * vmw_kms_helper_buffer_prepare.
2448 */
2449void vmw_kms_helper_buffer_revert(struct vmw_dma_buffer *buf)
2450{
2451 if (buf)
2452 ttm_bo_unreserve(&buf->base);
2453}
2454
2455/**
2456 * vmw_kms_helper_buffer_finish - Unreserve and fence a buffer object after
2457 * kms command submission.
2458 *
2459 * @dev_priv: Pointer to a device private structure.
2460 * @file_priv: Pointer to a struct drm_file representing the caller's
2461 * connection. Must be set to NULL if @user_fence_rep is NULL, and conversely
2462 * if non-NULL, @user_fence_rep must be non-NULL.
2463 * @buf: The buffer object.
2464 * @out_fence: Optional pointer to a fence pointer. If non-NULL, a
2465 * ref-counted fence pointer is returned here.
2466 * @user_fence_rep: Optional pointer to a user-space provided struct
2467 * drm_vmw_fence_rep. If provided, @file_priv must also be provided and the
2468 * function copies fence data to user-space in a fail-safe manner.
2469 */
2470void vmw_kms_helper_buffer_finish(struct vmw_private *dev_priv,
2471 struct drm_file *file_priv,
2472 struct vmw_dma_buffer *buf,
2473 struct vmw_fence_obj **out_fence,
2474 struct drm_vmw_fence_rep __user *
2475 user_fence_rep)
2476{
2477 struct vmw_fence_obj *fence;
2478 uint32_t handle;
2479 int ret;
2480
2481 ret = vmw_execbuf_fence_commands(file_priv, dev_priv, &fence,
2482 file_priv ? &handle : NULL);
2483 if (buf)
2484 vmw_fence_single_bo(&buf->base, fence);
2485 if (file_priv)
2486 vmw_execbuf_copy_fence_user(dev_priv, vmw_fpriv(file_priv),
2487 ret, user_fence_rep, fence,
2488 handle);
2489 if (out_fence)
2490 *out_fence = fence;
2491 else
2492 vmw_fence_obj_unreference(&fence);
2493
2494 vmw_kms_helper_buffer_revert(buf);
2495}
2496
2497
2498/**
2499 * vmw_kms_helper_resource_revert - Undo the actions of
2500 * vmw_kms_helper_resource_prepare.
2501 *
2502 * @res: Pointer to the resource. Typically a surface.
2503 *
2504 * Helper to be used if an error forces the caller to undo the actions of
2505 * vmw_kms_helper_resource_prepare.
2506 */
2507void vmw_kms_helper_resource_revert(struct vmw_resource *res)
2508{
2509 vmw_kms_helper_buffer_revert(res->backup);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002510 vmw_resource_unreserve(res, false, NULL, 0);
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07002511 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
2512}
2513
2514/**
2515 * vmw_kms_helper_resource_prepare - Reserve and validate a resource before
2516 * command submission.
2517 *
2518 * @res: Pointer to the resource. Typically a surface.
2519 * @interruptible: Whether to perform waits as interruptible.
2520 *
2521 * Reserves and validates also the backup buffer if a guest-backed resource.
2522 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
2523 * interrupted by a signal.
2524 */
2525int vmw_kms_helper_resource_prepare(struct vmw_resource *res,
2526 bool interruptible)
2527{
2528 int ret = 0;
2529
2530 if (interruptible)
2531 ret = mutex_lock_interruptible(&res->dev_priv->cmdbuf_mutex);
2532 else
2533 mutex_lock(&res->dev_priv->cmdbuf_mutex);
2534
2535 if (unlikely(ret != 0))
2536 return -ERESTARTSYS;
2537
2538 ret = vmw_resource_reserve(res, interruptible, false);
2539 if (ret)
2540 goto out_unlock;
2541
2542 if (res->backup) {
2543 ret = vmw_kms_helper_buffer_prepare(res->dev_priv, res->backup,
2544 interruptible,
2545 res->dev_priv->has_mob);
2546 if (ret)
2547 goto out_unreserve;
2548 }
2549 ret = vmw_resource_validate(res);
2550 if (ret)
2551 goto out_revert;
2552 return 0;
2553
2554out_revert:
2555 vmw_kms_helper_buffer_revert(res->backup);
2556out_unreserve:
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002557 vmw_resource_unreserve(res, false, NULL, 0);
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07002558out_unlock:
2559 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
2560 return ret;
2561}
2562
2563/**
2564 * vmw_kms_helper_resource_finish - Unreserve and fence a resource after
2565 * kms command submission.
2566 *
2567 * @res: Pointer to the resource. Typically a surface.
2568 * @out_fence: Optional pointer to a fence pointer. If non-NULL, a
2569 * ref-counted fence pointer is returned here.
2570 */
2571void vmw_kms_helper_resource_finish(struct vmw_resource *res,
2572 struct vmw_fence_obj **out_fence)
2573{
2574 if (res->backup || out_fence)
2575 vmw_kms_helper_buffer_finish(res->dev_priv, NULL, res->backup,
2576 out_fence, NULL);
2577
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002578 vmw_resource_unreserve(res, false, NULL, 0);
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07002579 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
2580}
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07002581
2582/**
2583 * vmw_kms_update_proxy - Helper function to update a proxy surface from
2584 * its backing MOB.
2585 *
2586 * @res: Pointer to the surface resource
2587 * @clips: Clip rects in framebuffer (surface) space.
2588 * @num_clips: Number of clips in @clips.
2589 * @increment: Integer with which to increment the clip counter when looping.
2590 * Used to skip a predetermined number of clip rects.
2591 *
2592 * This function makes sure the proxy surface is updated from its backing MOB
2593 * using the region given by @clips. The surface resource @res and its backing
2594 * MOB needs to be reserved and validated on call.
2595 */
2596int vmw_kms_update_proxy(struct vmw_resource *res,
2597 const struct drm_clip_rect *clips,
2598 unsigned num_clips,
2599 int increment)
2600{
2601 struct vmw_private *dev_priv = res->dev_priv;
2602 struct drm_vmw_size *size = &vmw_res_to_srf(res)->base_size;
2603 struct {
2604 SVGA3dCmdHeader header;
2605 SVGA3dCmdUpdateGBImage body;
2606 } *cmd;
2607 SVGA3dBox *box;
2608 size_t copy_size = 0;
2609 int i;
2610
2611 if (!clips)
2612 return 0;
2613
2614 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd) * num_clips);
2615 if (!cmd) {
2616 DRM_ERROR("Couldn't reserve fifo space for proxy surface "
2617 "update.\n");
2618 return -ENOMEM;
2619 }
2620
2621 for (i = 0; i < num_clips; ++i, clips += increment, ++cmd) {
2622 box = &cmd->body.box;
2623
2624 cmd->header.id = SVGA_3D_CMD_UPDATE_GB_IMAGE;
2625 cmd->header.size = sizeof(cmd->body);
2626 cmd->body.image.sid = res->id;
2627 cmd->body.image.face = 0;
2628 cmd->body.image.mipmap = 0;
2629
2630 if (clips->x1 > size->width || clips->x2 > size->width ||
2631 clips->y1 > size->height || clips->y2 > size->height) {
2632 DRM_ERROR("Invalid clips outsize of framebuffer.\n");
2633 return -EINVAL;
2634 }
2635
2636 box->x = clips->x1;
2637 box->y = clips->y1;
2638 box->z = 0;
2639 box->w = clips->x2 - clips->x1;
2640 box->h = clips->y2 - clips->y1;
2641 box->d = 1;
2642
2643 copy_size += sizeof(*cmd);
2644 }
2645
2646 vmw_fifo_commit(dev_priv, copy_size);
2647
2648 return 0;
2649}
Thomas Hellstroma2787242015-06-29 12:55:07 -07002650
2651int vmw_kms_fbdev_init_data(struct vmw_private *dev_priv,
2652 unsigned unit,
2653 u32 max_width,
2654 u32 max_height,
2655 struct drm_connector **p_con,
2656 struct drm_crtc **p_crtc,
2657 struct drm_display_mode **p_mode)
2658{
2659 struct drm_connector *con;
2660 struct vmw_display_unit *du;
2661 struct drm_display_mode *mode;
2662 int i = 0;
2663
2664 list_for_each_entry(con, &dev_priv->dev->mode_config.connector_list,
2665 head) {
2666 if (i == unit)
2667 break;
2668
2669 ++i;
2670 }
2671
2672 if (i != unit) {
2673 DRM_ERROR("Could not find initial display unit.\n");
2674 return -EINVAL;
2675 }
2676
2677 if (list_empty(&con->modes))
2678 (void) vmw_du_connector_fill_modes(con, max_width, max_height);
2679
2680 if (list_empty(&con->modes)) {
2681 DRM_ERROR("Could not find initial display mode.\n");
2682 return -EINVAL;
2683 }
2684
2685 du = vmw_connector_to_du(con);
2686 *p_con = con;
2687 *p_crtc = &du->crtc;
2688
2689 list_for_each_entry(mode, &con->modes, head) {
2690 if (mode->type & DRM_MODE_TYPE_PREFERRED)
2691 break;
2692 }
2693
2694 if (mode->type & DRM_MODE_TYPE_PREFERRED)
2695 *p_mode = mode;
2696 else {
2697 WARN_ONCE(true, "Could not find initial preferred mode.\n");
2698 *p_mode = list_first_entry(&con->modes,
2699 struct drm_display_mode,
2700 head);
2701 }
2702
2703 return 0;
2704}
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002705
2706/**
2707 * vmw_kms_del_active - unregister a crtc binding to the implicit framebuffer
2708 *
2709 * @dev_priv: Pointer to a device private struct.
2710 * @du: The display unit of the crtc.
2711 */
2712void vmw_kms_del_active(struct vmw_private *dev_priv,
2713 struct vmw_display_unit *du)
2714{
Thomas Hellstrom93cd1682016-05-03 11:24:35 +02002715 mutex_lock(&dev_priv->global_kms_state_mutex);
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002716 if (du->active_implicit) {
2717 if (--(dev_priv->num_implicit) == 0)
2718 dev_priv->implicit_fb = NULL;
2719 du->active_implicit = false;
2720 }
Thomas Hellstrom93cd1682016-05-03 11:24:35 +02002721 mutex_unlock(&dev_priv->global_kms_state_mutex);
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002722}
2723
2724/**
2725 * vmw_kms_add_active - register a crtc binding to an implicit framebuffer
2726 *
2727 * @vmw_priv: Pointer to a device private struct.
2728 * @du: The display unit of the crtc.
2729 * @vfb: The implicit framebuffer
2730 *
2731 * Registers a binding to an implicit framebuffer.
2732 */
2733void vmw_kms_add_active(struct vmw_private *dev_priv,
2734 struct vmw_display_unit *du,
2735 struct vmw_framebuffer *vfb)
2736{
Thomas Hellstrom93cd1682016-05-03 11:24:35 +02002737 mutex_lock(&dev_priv->global_kms_state_mutex);
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002738 WARN_ON_ONCE(!dev_priv->num_implicit && dev_priv->implicit_fb);
2739
2740 if (!du->active_implicit && du->is_implicit) {
2741 dev_priv->implicit_fb = vfb;
2742 du->active_implicit = true;
2743 dev_priv->num_implicit++;
2744 }
Thomas Hellstrom93cd1682016-05-03 11:24:35 +02002745 mutex_unlock(&dev_priv->global_kms_state_mutex);
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002746}
2747
2748/**
2749 * vmw_kms_screen_object_flippable - Check whether we can page-flip a crtc.
2750 *
2751 * @dev_priv: Pointer to device-private struct.
2752 * @crtc: The crtc we want to flip.
2753 *
2754 * Returns true or false depending whether it's OK to flip this crtc
2755 * based on the criterion that we must not have more than one implicit
2756 * frame-buffer at any one time.
2757 */
2758bool vmw_kms_crtc_flippable(struct vmw_private *dev_priv,
2759 struct drm_crtc *crtc)
2760{
2761 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
Thomas Hellstrom93cd1682016-05-03 11:24:35 +02002762 bool ret;
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002763
Thomas Hellstrom93cd1682016-05-03 11:24:35 +02002764 mutex_lock(&dev_priv->global_kms_state_mutex);
2765 ret = !du->is_implicit || dev_priv->num_implicit == 1;
2766 mutex_unlock(&dev_priv->global_kms_state_mutex);
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002767
Thomas Hellstrom93cd1682016-05-03 11:24:35 +02002768 return ret;
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002769}
2770
2771/**
2772 * vmw_kms_update_implicit_fb - Update the implicit fb.
2773 *
2774 * @dev_priv: Pointer to device-private struct.
2775 * @crtc: The crtc the new implicit frame-buffer is bound to.
2776 */
2777void vmw_kms_update_implicit_fb(struct vmw_private *dev_priv,
2778 struct drm_crtc *crtc)
2779{
2780 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
2781 struct vmw_framebuffer *vfb;
2782
Thomas Hellstrom93cd1682016-05-03 11:24:35 +02002783 mutex_lock(&dev_priv->global_kms_state_mutex);
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002784
2785 if (!du->is_implicit)
Thomas Hellstrom93cd1682016-05-03 11:24:35 +02002786 goto out_unlock;
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002787
2788 vfb = vmw_framebuffer_to_vfb(crtc->primary->fb);
2789 WARN_ON_ONCE(dev_priv->num_implicit != 1 &&
2790 dev_priv->implicit_fb != vfb);
2791
2792 dev_priv->implicit_fb = vfb;
Thomas Hellstrom93cd1682016-05-03 11:24:35 +02002793out_unlock:
2794 mutex_unlock(&dev_priv->global_kms_state_mutex);
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002795}
Thomas Hellstrom76404ac2016-02-12 09:55:45 +01002796
2797/**
2798 * vmw_kms_create_implicit_placement_proparty - Set up the implicit placement
2799 * property.
2800 *
2801 * @dev_priv: Pointer to a device private struct.
2802 * @immutable: Whether the property is immutable.
2803 *
2804 * Sets up the implicit placement property unless it's already set up.
2805 */
2806void
2807vmw_kms_create_implicit_placement_property(struct vmw_private *dev_priv,
2808 bool immutable)
2809{
2810 if (dev_priv->implicit_placement_property)
2811 return;
2812
2813 dev_priv->implicit_placement_property =
2814 drm_property_create_range(dev_priv->dev,
2815 immutable ?
2816 DRM_MODE_PROP_IMMUTABLE : 0,
2817 "implicit_placement", 0, 1);
2818
2819}
Sinclair Yeh904bb5e2017-03-23 14:29:22 -07002820
2821
2822/**
2823 * vmw_kms_set_config - Wrapper around drm_atomic_helper_set_config
2824 *
2825 * @set: The configuration to set.
2826 *
2827 * The vmwgfx Xorg driver doesn't assign the mode::type member, which
2828 * when drm_mode_set_crtcinfo is called as part of the configuration setting
2829 * causes it to return incorrect crtc dimensions causing severe problems in
2830 * the vmwgfx modesetting. So explicitly clear that member before calling
2831 * into drm_atomic_helper_set_config.
2832 */
Dave Airlie320d8c32017-04-03 16:30:24 +10002833int vmw_kms_set_config(struct drm_mode_set *set,
2834 struct drm_modeset_acquire_ctx *ctx)
Sinclair Yeh904bb5e2017-03-23 14:29:22 -07002835{
2836 if (set && set->mode)
2837 set->mode->type = 0;
2838
Dave Airlie320d8c32017-04-03 16:30:24 +10002839 return drm_atomic_helper_set_config(set, ctx);
Sinclair Yeh904bb5e2017-03-23 14:29:22 -07002840}