blob: 8e5cdf8b2cb95817e04f881f611cade4be3337e7 [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;
Sinclair Yeh14979ad2017-07-17 23:26:21 -0700387
388 if (plane->fb) {
389 hotspot_x += plane->fb->hot_x;
390 hotspot_y += plane->fb->hot_y;
391 }
392
Sinclair Yeh060e2ad2017-03-23 14:18:32 -0700393 du->cursor_surface = vps->surf;
394 du->cursor_dmabuf = vps->dmabuf;
395
396 /* setup new image */
397 if (vps->surf) {
398 du->cursor_age = du->cursor_surface->snooper.age;
399
400 ret = vmw_cursor_update_image(dev_priv,
401 vps->surf->snooper.image,
402 64, 64, hotspot_x, hotspot_y);
403 } else if (vps->dmabuf) {
404 ret = vmw_cursor_update_dmabuf(dev_priv, vps->dmabuf,
405 plane->state->crtc_w,
406 plane->state->crtc_h,
407 hotspot_x, hotspot_y);
408 } else {
409 vmw_cursor_update_position(dev_priv, false, 0, 0);
410 return;
411 }
412
413 if (!ret) {
414 du->cursor_x = plane->state->crtc_x + du->set_gui_x;
415 du->cursor_y = plane->state->crtc_y + du->set_gui_y;
416
417 vmw_cursor_update_position(dev_priv, true,
418 du->cursor_x + hotspot_x,
419 du->cursor_y + hotspot_y);
Sinclair Yeh14979ad2017-07-17 23:26:21 -0700420
421 du->core_hotspot_x = hotspot_x - du->hotspot_x;
422 du->core_hotspot_y = hotspot_y - du->hotspot_y;
Sinclair Yeh060e2ad2017-03-23 14:18:32 -0700423 } else {
424 DRM_ERROR("Failed to update cursor image\n");
425 }
426}
427
428
429/**
430 * vmw_du_primary_plane_atomic_check - check if the new state is okay
431 *
432 * @plane: display plane
433 * @state: info on the new plane state, including the FB
434 *
435 * Check if the new state is settable given the current state. Other
436 * than what the atomic helper checks, we care about crtc fitting
437 * the FB and maintaining one active framebuffer.
438 *
439 * Returns 0 on success
440 */
441int vmw_du_primary_plane_atomic_check(struct drm_plane *plane,
442 struct drm_plane_state *state)
443{
444 struct drm_framebuffer *new_fb = state->fb;
Ville Syrjälä13dd5b62017-11-01 20:29:17 +0200445 struct drm_rect clip = {
Sinclair Yeh060e2ad2017-03-23 14:18:32 -0700446 .x1 = state->crtc_x,
447 .y1 = state->crtc_y,
448 .x2 = state->crtc_x + state->crtc_w,
449 .y2 = state->crtc_y + state->crtc_h,
450 };
Sinclair Yeh060e2ad2017-03-23 14:18:32 -0700451 int ret;
452
Ville Syrjälä13dd5b62017-11-01 20:29:17 +0200453 ret = drm_plane_helper_check_state(state, &clip,
Sinclair Yeh060e2ad2017-03-23 14:18:32 -0700454 DRM_PLANE_HELPER_NO_SCALING,
455 DRM_PLANE_HELPER_NO_SCALING,
Ville Syrjälä13dd5b62017-11-01 20:29:17 +0200456 false, true);
Sinclair Yeh060e2ad2017-03-23 14:18:32 -0700457
458
459 if (!ret && new_fb) {
460 struct drm_crtc *crtc = state->crtc;
461 struct vmw_connector_state *vcs;
462 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
463 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
464 struct vmw_framebuffer *vfb = vmw_framebuffer_to_vfb(new_fb);
465
466 vcs = vmw_connector_state_to_vcs(du->connector.state);
467
Sinclair Yeh060e2ad2017-03-23 14:18:32 -0700468 /* Only one active implicit framebuffer at a time. */
469 mutex_lock(&dev_priv->global_kms_state_mutex);
470 if (vcs->is_implicit && dev_priv->implicit_fb &&
471 !(dev_priv->num_implicit == 1 && du->active_implicit)
472 && dev_priv->implicit_fb != vfb) {
473 DRM_ERROR("Multiple implicit framebuffers "
474 "not supported.\n");
475 ret = -EINVAL;
476 }
477 mutex_unlock(&dev_priv->global_kms_state_mutex);
478 }
479
480
481 return ret;
482}
483
484
485/**
486 * vmw_du_cursor_plane_atomic_check - check if the new state is okay
487 *
488 * @plane: cursor plane
489 * @state: info on the new plane state
490 *
491 * This is a chance to fail if the new cursor state does not fit
492 * our requirements.
493 *
494 * Returns 0 on success
495 */
496int vmw_du_cursor_plane_atomic_check(struct drm_plane *plane,
497 struct drm_plane_state *new_state)
498{
499 int ret = 0;
500 struct vmw_surface *surface = NULL;
501 struct drm_framebuffer *fb = new_state->fb;
502
503
504 /* Turning off */
505 if (!fb)
506 return ret;
507
508 /* A lot of the code assumes this */
509 if (new_state->crtc_w != 64 || new_state->crtc_h != 64) {
510 DRM_ERROR("Invalid cursor dimensions (%d, %d)\n",
511 new_state->crtc_w, new_state->crtc_h);
512 ret = -EINVAL;
513 }
514
515 if (!vmw_framebuffer_to_vfb(fb)->dmabuf)
516 surface = vmw_framebuffer_to_vfbs(fb)->surface;
517
518 if (surface && !surface->snooper.image) {
519 DRM_ERROR("surface not suitable for cursor\n");
520 ret = -EINVAL;
521 }
522
523 return ret;
524}
525
526
Sinclair Yeh06ec4192017-03-23 13:14:54 -0700527int vmw_du_crtc_atomic_check(struct drm_crtc *crtc,
528 struct drm_crtc_state *new_state)
529{
530 struct vmw_display_unit *du = vmw_crtc_to_du(new_state->crtc);
531 int connector_mask = 1 << drm_connector_index(&du->connector);
532 bool has_primary = new_state->plane_mask &
533 BIT(drm_plane_index(crtc->primary));
534
535 /* We always want to have an active plane with an active CRTC */
536 if (has_primary != new_state->enable)
537 return -EINVAL;
538
539
540 if (new_state->connector_mask != connector_mask &&
541 new_state->connector_mask != 0) {
542 DRM_ERROR("Invalid connectors configuration\n");
543 return -EINVAL;
544 }
545
546 /*
547 * Our virtual device does not have a dot clock, so use the logical
548 * clock value as the dot clock.
549 */
550 if (new_state->mode.crtc_clock == 0)
551 new_state->adjusted_mode.crtc_clock = new_state->mode.clock;
552
553 return 0;
554}
555
556
557void vmw_du_crtc_atomic_begin(struct drm_crtc *crtc,
558 struct drm_crtc_state *old_crtc_state)
559{
560}
561
562
563void vmw_du_crtc_atomic_flush(struct drm_crtc *crtc,
564 struct drm_crtc_state *old_crtc_state)
565{
566 struct drm_pending_vblank_event *event = crtc->state->event;
567
568 if (event) {
569 crtc->state->event = NULL;
570
571 spin_lock_irq(&crtc->dev->event_lock);
572 if (drm_crtc_vblank_get(crtc) == 0)
573 drm_crtc_arm_vblank_event(crtc, event);
574 else
575 drm_crtc_send_vblank_event(crtc, event);
576 spin_unlock_irq(&crtc->dev->event_lock);
577 }
578
579}
580
581
Sinclair Yeh9c2542a2017-03-23 11:33:39 -0700582/**
583 * vmw_du_crtc_duplicate_state - duplicate crtc state
584 * @crtc: DRM crtc
585 *
586 * Allocates and returns a copy of the crtc state (both common and
587 * vmw-specific) for the specified crtc.
588 *
589 * Returns: The newly allocated crtc state, or NULL on failure.
590 */
591struct drm_crtc_state *
592vmw_du_crtc_duplicate_state(struct drm_crtc *crtc)
593{
594 struct drm_crtc_state *state;
595 struct vmw_crtc_state *vcs;
596
597 if (WARN_ON(!crtc->state))
598 return NULL;
599
600 vcs = kmemdup(crtc->state, sizeof(*vcs), GFP_KERNEL);
601
602 if (!vcs)
603 return NULL;
604
605 state = &vcs->base;
606
607 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
608
609 return state;
610}
611
612
613/**
614 * vmw_du_crtc_reset - creates a blank vmw crtc state
615 * @crtc: DRM crtc
616 *
617 * Resets the atomic state for @crtc by freeing the state pointer (which
618 * might be NULL, e.g. at driver load time) and allocating a new empty state
619 * object.
620 */
621void vmw_du_crtc_reset(struct drm_crtc *crtc)
622{
623 struct vmw_crtc_state *vcs;
624
625
626 if (crtc->state) {
627 __drm_atomic_helper_crtc_destroy_state(crtc->state);
628
629 kfree(vmw_crtc_state_to_vcs(crtc->state));
630 }
631
632 vcs = kzalloc(sizeof(*vcs), GFP_KERNEL);
633
634 if (!vcs) {
635 DRM_ERROR("Cannot allocate vmw_crtc_state\n");
636 return;
637 }
638
639 crtc->state = &vcs->base;
640 crtc->state->crtc = crtc;
641}
642
643
644/**
645 * vmw_du_crtc_destroy_state - destroy crtc state
646 * @crtc: DRM crtc
647 * @state: state object to destroy
648 *
649 * Destroys the crtc state (both common and vmw-specific) for the
650 * specified plane.
651 */
652void
653vmw_du_crtc_destroy_state(struct drm_crtc *crtc,
654 struct drm_crtc_state *state)
655{
656 drm_atomic_helper_crtc_destroy_state(crtc, state);
657}
658
659
Sinclair Yehcc5ec452017-03-23 11:36:05 -0700660/**
661 * vmw_du_plane_duplicate_state - duplicate plane state
662 * @plane: drm plane
663 *
664 * Allocates and returns a copy of the plane state (both common and
665 * vmw-specific) for the specified plane.
666 *
667 * Returns: The newly allocated plane state, or NULL on failure.
668 */
669struct drm_plane_state *
670vmw_du_plane_duplicate_state(struct drm_plane *plane)
671{
672 struct drm_plane_state *state;
673 struct vmw_plane_state *vps;
674
675 vps = kmemdup(plane->state, sizeof(*vps), GFP_KERNEL);
676
677 if (!vps)
678 return NULL;
679
680 vps->pinned = 0;
681
Sinclair Yeh810b3e162017-03-23 15:39:16 -0700682 /* Mapping is managed by prepare_fb/cleanup_fb */
683 memset(&vps->guest_map, 0, sizeof(vps->guest_map));
684 memset(&vps->host_map, 0, sizeof(vps->host_map));
685 vps->cpp = 0;
686
Sinclair Yehcc5ec452017-03-23 11:36:05 -0700687 /* Each ref counted resource needs to be acquired again */
688 if (vps->surf)
689 (void) vmw_surface_reference(vps->surf);
690
691 if (vps->dmabuf)
692 (void) vmw_dmabuf_reference(vps->dmabuf);
693
694 state = &vps->base;
695
696 __drm_atomic_helper_plane_duplicate_state(plane, state);
697
698 return state;
699}
700
701
702/**
703 * vmw_du_plane_reset - creates a blank vmw plane state
704 * @plane: drm plane
705 *
706 * Resets the atomic state for @plane by freeing the state pointer (which might
707 * be NULL, e.g. at driver load time) and allocating a new empty state object.
708 */
709void vmw_du_plane_reset(struct drm_plane *plane)
710{
711 struct vmw_plane_state *vps;
712
713
714 if (plane->state)
715 vmw_du_plane_destroy_state(plane, plane->state);
716
717 vps = kzalloc(sizeof(*vps), GFP_KERNEL);
718
719 if (!vps) {
720 DRM_ERROR("Cannot allocate vmw_plane_state\n");
721 return;
722 }
723
724 plane->state = &vps->base;
725 plane->state->plane = plane;
Robert Fossc2c446a2017-05-19 16:50:17 -0400726 plane->state->rotation = DRM_MODE_ROTATE_0;
Sinclair Yehcc5ec452017-03-23 11:36:05 -0700727}
728
729
730/**
731 * vmw_du_plane_destroy_state - destroy plane state
732 * @plane: DRM plane
733 * @state: state object to destroy
734 *
735 * Destroys the plane state (both common and vmw-specific) for the
736 * specified plane.
737 */
738void
739vmw_du_plane_destroy_state(struct drm_plane *plane,
740 struct drm_plane_state *state)
741{
742 struct vmw_plane_state *vps = vmw_plane_state_to_vps(state);
743
744
Sinclair Yeh810b3e162017-03-23 15:39:16 -0700745 /* Should have been freed by cleanup_fb */
746 if (vps->guest_map.virtual) {
747 DRM_ERROR("Guest mapping not freed\n");
748 ttm_bo_kunmap(&vps->guest_map);
749 }
750
751 if (vps->host_map.virtual) {
752 DRM_ERROR("Host mapping not freed\n");
753 ttm_bo_kunmap(&vps->host_map);
754 }
755
Sinclair Yehcc5ec452017-03-23 11:36:05 -0700756 if (vps->surf)
757 vmw_surface_unreference(&vps->surf);
758
759 if (vps->dmabuf)
760 vmw_dmabuf_unreference(&vps->dmabuf);
761
762 drm_atomic_helper_plane_destroy_state(plane, state);
763}
764
765
Sinclair Yehd7721ca2017-03-23 11:48:44 -0700766/**
767 * vmw_du_connector_duplicate_state - duplicate connector state
768 * @connector: DRM connector
769 *
770 * Allocates and returns a copy of the connector state (both common and
771 * vmw-specific) for the specified connector.
772 *
773 * Returns: The newly allocated connector state, or NULL on failure.
774 */
775struct drm_connector_state *
776vmw_du_connector_duplicate_state(struct drm_connector *connector)
777{
778 struct drm_connector_state *state;
779 struct vmw_connector_state *vcs;
780
781 if (WARN_ON(!connector->state))
782 return NULL;
783
784 vcs = kmemdup(connector->state, sizeof(*vcs), GFP_KERNEL);
785
786 if (!vcs)
787 return NULL;
788
789 state = &vcs->base;
790
791 __drm_atomic_helper_connector_duplicate_state(connector, state);
792
793 return state;
794}
795
796
797/**
798 * vmw_du_connector_reset - creates a blank vmw connector state
799 * @connector: DRM connector
800 *
801 * Resets the atomic state for @connector by freeing the state pointer (which
802 * might be NULL, e.g. at driver load time) and allocating a new empty state
803 * object.
804 */
805void vmw_du_connector_reset(struct drm_connector *connector)
806{
807 struct vmw_connector_state *vcs;
808
809
810 if (connector->state) {
811 __drm_atomic_helper_connector_destroy_state(connector->state);
812
813 kfree(vmw_connector_state_to_vcs(connector->state));
814 }
815
816 vcs = kzalloc(sizeof(*vcs), GFP_KERNEL);
817
818 if (!vcs) {
819 DRM_ERROR("Cannot allocate vmw_connector_state\n");
820 return;
821 }
822
823 __drm_atomic_helper_connector_reset(connector, &vcs->base);
824}
825
826
827/**
828 * vmw_du_connector_destroy_state - destroy connector state
829 * @connector: DRM connector
830 * @state: state object to destroy
831 *
832 * Destroys the connector state (both common and vmw-specific) for the
833 * specified plane.
834 */
835void
836vmw_du_connector_destroy_state(struct drm_connector *connector,
837 struct drm_connector_state *state)
838{
839 drm_atomic_helper_connector_destroy_state(connector, state);
840}
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000841/*
842 * Generic framebuffer code
843 */
844
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000845/*
846 * Surface framebuffer code
847 */
848
Rashika Kheria847c5962014-01-06 22:18:10 +0530849static void vmw_framebuffer_surface_destroy(struct drm_framebuffer *framebuffer)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000850{
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200851 struct vmw_framebuffer_surface *vfbs =
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000852 vmw_framebuffer_to_vfbs(framebuffer);
853
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000854 drm_framebuffer_cleanup(framebuffer);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200855 vmw_surface_unreference(&vfbs->surface);
Thomas Hellstroma2787242015-06-29 12:55:07 -0700856 if (vfbs->base.user_obj)
857 ttm_base_object_unref(&vfbs->base.user_obj);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000858
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200859 kfree(vfbs);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000860}
861
Rashika Kheria847c5962014-01-06 22:18:10 +0530862static int vmw_framebuffer_surface_dirty(struct drm_framebuffer *framebuffer,
Thomas Hellstrom02b00162010-10-05 12:43:02 +0200863 struct drm_file *file_priv,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000864 unsigned flags, unsigned color,
865 struct drm_clip_rect *clips,
866 unsigned num_clips)
867{
868 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
869 struct vmw_framebuffer_surface *vfbs =
870 vmw_framebuffer_to_vfbs(framebuffer);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000871 struct drm_clip_rect norect;
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200872 int ret, inc = 1;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000873
Sinclair Yehc8261a92015-06-26 01:23:42 -0700874 /* Legacy Display Unit does not support 3D */
875 if (dev_priv->active_display_unit == vmw_du_legacy)
Jakob Bornecrantz01e81412011-10-04 20:13:24 +0200876 return -EINVAL;
877
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200878 drm_modeset_lock_all(dev_priv->dev);
879
Thomas Hellstrom294adf72014-02-27 12:34:51 +0100880 ret = ttm_read_lock(&dev_priv->reservation_sem, true);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200881 if (unlikely(ret != 0)) {
882 drm_modeset_unlock_all(dev_priv->dev);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200883 return ret;
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200884 }
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200885
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000886 if (!num_clips) {
887 num_clips = 1;
888 clips = &norect;
889 norect.x1 = norect.y1 = 0;
890 norect.x2 = framebuffer->width;
891 norect.y2 = framebuffer->height;
892 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
893 num_clips /= 2;
894 inc = 2; /* skip source rects */
895 }
896
Sinclair Yehc8261a92015-06-26 01:23:42 -0700897 if (dev_priv->active_display_unit == vmw_du_screen_object)
Thomas Hellstrom10b1e0c2015-06-26 02:14:27 -0700898 ret = vmw_kms_sou_do_surface_dirty(dev_priv, &vfbs->base,
899 clips, NULL, NULL, 0, 0,
900 num_clips, inc, NULL);
Sinclair Yeh35c05122015-06-26 01:42:06 -0700901 else
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700902 ret = vmw_kms_stdu_surface_dirty(dev_priv, &vfbs->base,
903 clips, NULL, NULL, 0, 0,
904 num_clips, inc, NULL);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000905
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700906 vmw_fifo_flush(dev_priv, false);
Thomas Hellstrom294adf72014-02-27 12:34:51 +0100907 ttm_read_unlock(&dev_priv->reservation_sem);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200908
909 drm_modeset_unlock_all(dev_priv->dev);
910
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000911 return 0;
912}
913
Thomas Hellstrom10b1e0c2015-06-26 02:14:27 -0700914/**
915 * vmw_kms_readback - Perform a readback from the screen system to
916 * a dma-buffer backed framebuffer.
917 *
918 * @dev_priv: Pointer to the device private structure.
919 * @file_priv: Pointer to a struct drm_file identifying the caller.
920 * Must be set to NULL if @user_fence_rep is NULL.
921 * @vfb: Pointer to the dma-buffer backed framebuffer.
922 * @user_fence_rep: User-space provided structure for fence information.
923 * Must be set to non-NULL if @file_priv is non-NULL.
924 * @vclips: Array of clip rects.
925 * @num_clips: Number of clip rects in @vclips.
926 *
927 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
928 * interrupted.
929 */
930int vmw_kms_readback(struct vmw_private *dev_priv,
931 struct drm_file *file_priv,
932 struct vmw_framebuffer *vfb,
933 struct drm_vmw_fence_rep __user *user_fence_rep,
934 struct drm_vmw_rect *vclips,
935 uint32_t num_clips)
936{
937 switch (dev_priv->active_display_unit) {
938 case vmw_du_screen_object:
939 return vmw_kms_sou_readback(dev_priv, file_priv, vfb,
940 user_fence_rep, vclips, num_clips);
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700941 case vmw_du_screen_target:
942 return vmw_kms_stdu_dma(dev_priv, file_priv, vfb,
943 user_fence_rep, NULL, vclips, num_clips,
944 1, false, true);
Thomas Hellstrom10b1e0c2015-06-26 02:14:27 -0700945 default:
946 WARN_ONCE(true,
947 "Readback called with invalid display system.\n");
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700948}
Thomas Hellstrom10b1e0c2015-06-26 02:14:27 -0700949
950 return -ENOSYS;
951}
952
953
Ville Syrjäläd7955fc2015-12-15 12:21:15 +0100954static const struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs = {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000955 .destroy = vmw_framebuffer_surface_destroy,
956 .dirty = vmw_framebuffer_surface_dirty,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000957};
958
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200959static int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv,
960 struct vmw_surface *surface,
961 struct vmw_framebuffer **out,
Daniel Vetterdabdcdc2016-12-02 08:07:40 +0100962 const struct drm_mode_fb_cmd2
Sinclair Yehf89c6c32015-06-26 01:54:28 -0700963 *mode_cmd,
964 bool is_dmabuf_proxy)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000965
966{
967 struct drm_device *dev = dev_priv->dev;
968 struct vmw_framebuffer_surface *vfbs;
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200969 enum SVGA3dSurfaceFormat format;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000970 int ret;
Daniel Vetterdabdcdc2016-12-02 08:07:40 +0100971 struct drm_format_name_buf format_name;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000972
Sinclair Yehc8261a92015-06-26 01:23:42 -0700973 /* 3D is only supported on HWv8 and newer hosts */
974 if (dev_priv->active_display_unit == vmw_du_legacy)
Jakob Bornecrantz01e81412011-10-04 20:13:24 +0200975 return -ENOSYS;
976
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200977 /*
978 * Sanity checks.
979 */
980
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100981 /* Surface must be marked as a scanout. */
982 if (unlikely(!surface->scanout))
983 return -EINVAL;
984
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200985 if (unlikely(surface->mip_levels[0] != 1 ||
986 surface->num_sizes != 1 ||
Thomas Hellstromb360a3c2014-01-15 08:51:36 +0100987 surface->base_size.width < mode_cmd->width ||
988 surface->base_size.height < mode_cmd->height ||
989 surface->base_size.depth != 1)) {
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200990 DRM_ERROR("Incompatible surface dimensions "
991 "for requested mode.\n");
992 return -EINVAL;
993 }
994
Daniel Vetterdabdcdc2016-12-02 08:07:40 +0100995 switch (mode_cmd->pixel_format) {
996 case DRM_FORMAT_ARGB8888:
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200997 format = SVGA3D_A8R8G8B8;
998 break;
Daniel Vetterdabdcdc2016-12-02 08:07:40 +0100999 case DRM_FORMAT_XRGB8888:
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001000 format = SVGA3D_X8R8G8B8;
1001 break;
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001002 case DRM_FORMAT_RGB565:
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001003 format = SVGA3D_R5G6B5;
1004 break;
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001005 case DRM_FORMAT_XRGB1555:
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001006 format = SVGA3D_A1R5G5B5;
1007 break;
1008 default:
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001009 DRM_ERROR("Invalid pixel format: %s\n",
1010 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001011 return -EINVAL;
1012 }
1013
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001014 /*
1015 * For DX, surface format validation is done when surface->scanout
1016 * is set.
1017 */
1018 if (!dev_priv->has_dx && format != surface->format) {
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001019 DRM_ERROR("Invalid surface format for requested mode.\n");
1020 return -EINVAL;
1021 }
1022
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001023 vfbs = kzalloc(sizeof(*vfbs), GFP_KERNEL);
1024 if (!vfbs) {
1025 ret = -ENOMEM;
1026 goto out_err1;
1027 }
1028
Ville Syrjäläa3f913c2016-12-14 22:48:59 +02001029 drm_helper_mode_fill_fb_struct(dev, &vfbs->base.base, mode_cmd);
Sinclair Yeh05c95012015-08-11 22:53:39 -07001030 vfbs->surface = vmw_surface_reference(surface);
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001031 vfbs->base.user_handle = mode_cmd->handles[0];
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001032 vfbs->is_dmabuf_proxy = is_dmabuf_proxy;
Thomas Hellstrom3a939a52010-10-05 12:43:03 +02001033
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001034 *out = &vfbs->base;
1035
Daniel Vetter80f0b5a2012-12-13 23:39:01 +01001036 ret = drm_framebuffer_init(dev, &vfbs->base.base,
1037 &vmw_framebuffer_surface_funcs);
1038 if (ret)
Sinclair Yeh05c95012015-08-11 22:53:39 -07001039 goto out_err2;
Daniel Vetter80f0b5a2012-12-13 23:39:01 +01001040
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001041 return 0;
1042
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001043out_err2:
Sinclair Yeh05c95012015-08-11 22:53:39 -07001044 vmw_surface_unreference(&surface);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001045 kfree(vfbs);
1046out_err1:
1047 return ret;
1048}
1049
1050/*
1051 * Dmabuf framebuffer code
1052 */
1053
Rashika Kheria847c5962014-01-06 22:18:10 +05301054static void vmw_framebuffer_dmabuf_destroy(struct drm_framebuffer *framebuffer)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001055{
1056 struct vmw_framebuffer_dmabuf *vfbd =
1057 vmw_framebuffer_to_vfbd(framebuffer);
1058
1059 drm_framebuffer_cleanup(framebuffer);
1060 vmw_dmabuf_unreference(&vfbd->buffer);
Thomas Hellstroma2787242015-06-29 12:55:07 -07001061 if (vfbd->base.user_obj)
1062 ttm_base_object_unref(&vfbd->base.user_obj);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001063
1064 kfree(vfbd);
1065}
1066
Rashika Kheria847c5962014-01-06 22:18:10 +05301067static int vmw_framebuffer_dmabuf_dirty(struct drm_framebuffer *framebuffer,
Thomas Hellstrom02b00162010-10-05 12:43:02 +02001068 struct drm_file *file_priv,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001069 unsigned flags, unsigned color,
1070 struct drm_clip_rect *clips,
1071 unsigned num_clips)
1072{
1073 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +02001074 struct vmw_framebuffer_dmabuf *vfbd =
1075 vmw_framebuffer_to_vfbd(framebuffer);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001076 struct drm_clip_rect norect;
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +02001077 int ret, increment = 1;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001078
Ville Syrjälä73e9efd2013-12-04 14:13:58 +02001079 drm_modeset_lock_all(dev_priv->dev);
1080
Thomas Hellstrom294adf72014-02-27 12:34:51 +01001081 ret = ttm_read_lock(&dev_priv->reservation_sem, true);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +02001082 if (unlikely(ret != 0)) {
1083 drm_modeset_unlock_all(dev_priv->dev);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +02001084 return ret;
Ville Syrjälä73e9efd2013-12-04 14:13:58 +02001085 }
Thomas Hellstrom3a939a52010-10-05 12:43:03 +02001086
Thomas Hellstromdf1c93b2010-01-13 22:28:36 +01001087 if (!num_clips) {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001088 num_clips = 1;
1089 clips = &norect;
1090 norect.x1 = norect.y1 = 0;
1091 norect.x2 = framebuffer->width;
1092 norect.y2 = framebuffer->height;
1093 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
1094 num_clips /= 2;
1095 increment = 2;
1096 }
1097
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001098 switch (dev_priv->active_display_unit) {
1099 case vmw_du_screen_target:
1100 ret = vmw_kms_stdu_dma(dev_priv, NULL, &vfbd->base, NULL,
1101 clips, NULL, num_clips, increment,
1102 true, true);
1103 break;
1104 case vmw_du_screen_object:
Thomas Hellstrom10b1e0c2015-06-26 02:14:27 -07001105 ret = vmw_kms_sou_do_dmabuf_dirty(dev_priv, &vfbd->base,
Thomas Hellstrom897b8182016-02-12 08:32:08 +01001106 clips, NULL, num_clips,
1107 increment, true, NULL);
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001108 break;
Thomas Hellstrom352b20d2015-06-29 12:57:37 -07001109 case vmw_du_legacy:
1110 ret = vmw_kms_ldu_do_dmabuf_dirty(dev_priv, &vfbd->base, 0, 0,
1111 clips, num_clips, increment);
1112 break;
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001113 default:
Thomas Hellstrom352b20d2015-06-29 12:57:37 -07001114 ret = -EINVAL;
1115 WARN_ONCE(true, "Dirty called with invalid display system.\n");
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001116 break;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +02001117 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001118
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07001119 vmw_fifo_flush(dev_priv, false);
Thomas Hellstrom294adf72014-02-27 12:34:51 +01001120 ttm_read_unlock(&dev_priv->reservation_sem);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +02001121
1122 drm_modeset_unlock_all(dev_priv->dev);
1123
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +02001124 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001125}
1126
Ville Syrjäläd7955fc2015-12-15 12:21:15 +01001127static const struct drm_framebuffer_funcs vmw_framebuffer_dmabuf_funcs = {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001128 .destroy = vmw_framebuffer_dmabuf_destroy,
1129 .dirty = vmw_framebuffer_dmabuf_dirty,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001130};
1131
Jakob Bornecrantz497a3ff2011-10-04 20:13:14 +02001132/**
Jakob Bornecrantz497a3ff2011-10-04 20:13:14 +02001133 * Pin the dmabuffer to the start of vram.
1134 */
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001135static int vmw_framebuffer_pin(struct vmw_framebuffer *vfb)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001136{
1137 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001138 struct vmw_dma_buffer *buf;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001139 int ret;
1140
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001141 buf = vfb->dmabuf ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
1142 vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001143
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001144 if (!buf)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001145 return 0;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001146
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001147 switch (dev_priv->active_display_unit) {
1148 case vmw_du_legacy:
1149 vmw_overlay_pause_all(dev_priv);
1150 ret = vmw_dmabuf_pin_in_start_of_vram(dev_priv, buf, false);
1151 vmw_overlay_resume_all(dev_priv);
1152 break;
1153 case vmw_du_screen_object:
1154 case vmw_du_screen_target:
1155 if (vfb->dmabuf)
1156 return vmw_dmabuf_pin_in_vram_or_gmr(dev_priv, buf,
1157 false);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001158
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001159 return vmw_dmabuf_pin_in_placement(dev_priv, buf,
1160 &vmw_mob_placement, false);
1161 default:
1162 return -EINVAL;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001163 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001164
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001165 return ret;
1166}
1167
1168static int vmw_framebuffer_unpin(struct vmw_framebuffer *vfb)
1169{
1170 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
1171 struct vmw_dma_buffer *buf;
1172
1173 buf = vfb->dmabuf ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
1174 vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
1175
1176 if (WARN_ON(!buf))
1177 return 0;
1178
1179 return vmw_dmabuf_unpin(dev_priv, buf, false);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001180}
1181
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001182/**
1183 * vmw_create_dmabuf_proxy - create a proxy surface for the DMA buf
1184 *
1185 * @dev: DRM device
1186 * @mode_cmd: parameters for the new surface
1187 * @dmabuf_mob: MOB backing the DMA buf
1188 * @srf_out: newly created surface
1189 *
1190 * When the content FB is a DMA buf, we create a surface as a proxy to the
1191 * same buffer. This way we can do a surface copy rather than a surface DMA.
1192 * This is a more efficient approach
1193 *
1194 * RETURNS:
1195 * 0 on success, error code otherwise
1196 */
1197static int vmw_create_dmabuf_proxy(struct drm_device *dev,
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001198 const struct drm_mode_fb_cmd2 *mode_cmd,
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001199 struct vmw_dma_buffer *dmabuf_mob,
1200 struct vmw_surface **srf_out)
1201{
1202 uint32_t format;
Thomas Hellstrom8cd9f252017-01-19 10:53:02 -08001203 struct drm_vmw_size content_base_size = {0};
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001204 struct vmw_resource *res;
Thomas Hellstroma50e2bf2016-01-08 20:29:40 +01001205 unsigned int bytes_pp;
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001206 struct drm_format_name_buf format_name;
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001207 int ret;
1208
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001209 switch (mode_cmd->pixel_format) {
1210 case DRM_FORMAT_ARGB8888:
1211 case DRM_FORMAT_XRGB8888:
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001212 format = SVGA3D_X8R8G8B8;
Thomas Hellstroma50e2bf2016-01-08 20:29:40 +01001213 bytes_pp = 4;
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001214 break;
1215
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001216 case DRM_FORMAT_RGB565:
1217 case DRM_FORMAT_XRGB1555:
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001218 format = SVGA3D_R5G6B5;
Thomas Hellstroma50e2bf2016-01-08 20:29:40 +01001219 bytes_pp = 2;
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001220 break;
1221
1222 case 8:
1223 format = SVGA3D_P8;
Thomas Hellstroma50e2bf2016-01-08 20:29:40 +01001224 bytes_pp = 1;
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001225 break;
1226
1227 default:
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001228 DRM_ERROR("Invalid framebuffer format %s\n",
1229 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001230 return -EINVAL;
1231 }
1232
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001233 content_base_size.width = mode_cmd->pitches[0] / bytes_pp;
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001234 content_base_size.height = mode_cmd->height;
1235 content_base_size.depth = 1;
1236
1237 ret = vmw_surface_gb_priv_define(dev,
1238 0, /* kernel visible only */
1239 0, /* flags */
1240 format,
1241 true, /* can be a scanout buffer */
1242 1, /* num of mip levels */
1243 0,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001244 0,
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001245 content_base_size,
1246 srf_out);
1247 if (ret) {
1248 DRM_ERROR("Failed to allocate proxy content buffer\n");
1249 return ret;
1250 }
1251
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001252 res = &(*srf_out)->res;
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001253
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001254 /* Reserve and switch the backing mob. */
1255 mutex_lock(&res->dev_priv->cmdbuf_mutex);
1256 (void) vmw_resource_reserve(res, false, true);
1257 vmw_dmabuf_unreference(&res->backup);
1258 res->backup = vmw_dmabuf_reference(dmabuf_mob);
1259 res->backup_offset = 0;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001260 vmw_resource_unreserve(res, false, NULL, 0);
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001261 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001262
1263 return 0;
1264}
1265
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001266
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001267
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001268static int vmw_kms_new_framebuffer_dmabuf(struct vmw_private *dev_priv,
1269 struct vmw_dma_buffer *dmabuf,
1270 struct vmw_framebuffer **out,
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001271 const struct drm_mode_fb_cmd2
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001272 *mode_cmd)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001273
1274{
1275 struct drm_device *dev = dev_priv->dev;
1276 struct vmw_framebuffer_dmabuf *vfbd;
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001277 unsigned int requested_size;
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001278 struct drm_format_name_buf format_name;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001279 int ret;
1280
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001281 requested_size = mode_cmd->height * mode_cmd->pitches[0];
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001282 if (unlikely(requested_size > dmabuf->base.num_pages * PAGE_SIZE)) {
1283 DRM_ERROR("Screen buffer object size is too small "
1284 "for requested mode.\n");
1285 return -EINVAL;
1286 }
1287
Jakob Bornecrantzc337ada2011-10-04 20:13:34 +02001288 /* Limited framebuffer color depth support for screen objects */
Sinclair Yehc8261a92015-06-26 01:23:42 -07001289 if (dev_priv->active_display_unit == vmw_du_screen_object) {
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001290 switch (mode_cmd->pixel_format) {
1291 case DRM_FORMAT_XRGB8888:
1292 case DRM_FORMAT_ARGB8888:
1293 break;
1294 case DRM_FORMAT_XRGB1555:
1295 case DRM_FORMAT_RGB565:
1296 break;
Jakob Bornecrantzc337ada2011-10-04 20:13:34 +02001297 default:
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001298 DRM_ERROR("Invalid pixel format: %s\n",
1299 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Jakob Bornecrantzc337ada2011-10-04 20:13:34 +02001300 return -EINVAL;
1301 }
1302 }
1303
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001304 vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL);
1305 if (!vfbd) {
1306 ret = -ENOMEM;
1307 goto out_err1;
1308 }
1309
Ville Syrjäläa3f913c2016-12-14 22:48:59 +02001310 drm_helper_mode_fill_fb_struct(dev, &vfbd->base.base, mode_cmd);
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001311 vfbd->base.dmabuf = true;
Sinclair Yeh05c95012015-08-11 22:53:39 -07001312 vfbd->buffer = vmw_dmabuf_reference(dmabuf);
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001313 vfbd->base.user_handle = mode_cmd->handles[0];
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001314 *out = &vfbd->base;
1315
Daniel Vetter80f0b5a2012-12-13 23:39:01 +01001316 ret = drm_framebuffer_init(dev, &vfbd->base.base,
1317 &vmw_framebuffer_dmabuf_funcs);
1318 if (ret)
Sinclair Yeh05c95012015-08-11 22:53:39 -07001319 goto out_err2;
Daniel Vetter80f0b5a2012-12-13 23:39:01 +01001320
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001321 return 0;
1322
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001323out_err2:
Sinclair Yeh05c95012015-08-11 22:53:39 -07001324 vmw_dmabuf_unreference(&dmabuf);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001325 kfree(vfbd);
1326out_err1:
1327 return ret;
1328}
1329
Sinclair Yeh810b3e162017-03-23 15:39:16 -07001330
1331/**
1332 * vmw_kms_srf_ok - check if a surface can be created
1333 *
1334 * @width: requested width
1335 * @height: requested height
1336 *
1337 * Surfaces need to be less than texture size
1338 */
1339static bool
1340vmw_kms_srf_ok(struct vmw_private *dev_priv, uint32_t width, uint32_t height)
1341{
1342 if (width > dev_priv->texture_max_width ||
1343 height > dev_priv->texture_max_height)
1344 return false;
1345
1346 return true;
1347}
1348
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001349/**
1350 * vmw_kms_new_framebuffer - Create a new framebuffer.
1351 *
1352 * @dev_priv: Pointer to device private struct.
1353 * @dmabuf: Pointer to dma buffer to wrap the kms framebuffer around.
1354 * Either @dmabuf or @surface must be NULL.
1355 * @surface: Pointer to a surface to wrap the kms framebuffer around.
1356 * Either @dmabuf or @surface must be NULL.
1357 * @only_2d: No presents will occur to this dma buffer based framebuffer. This
1358 * Helps the code to do some important optimizations.
1359 * @mode_cmd: Frame-buffer metadata.
1360 */
1361struct vmw_framebuffer *
1362vmw_kms_new_framebuffer(struct vmw_private *dev_priv,
1363 struct vmw_dma_buffer *dmabuf,
1364 struct vmw_surface *surface,
1365 bool only_2d,
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001366 const struct drm_mode_fb_cmd2 *mode_cmd)
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001367{
Sinclair Yeh05c95012015-08-11 22:53:39 -07001368 struct vmw_framebuffer *vfb = NULL;
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001369 bool is_dmabuf_proxy = false;
1370 int ret;
1371
1372 /*
1373 * We cannot use the SurfaceDMA command in an non-accelerated VM,
1374 * therefore, wrap the DMA buf in a surface so we can use the
1375 * SurfaceCopy command.
1376 */
Sinclair Yeh810b3e162017-03-23 15:39:16 -07001377 if (vmw_kms_srf_ok(dev_priv, mode_cmd->width, mode_cmd->height) &&
1378 dmabuf && only_2d &&
Sinclair Yehbbd5fef2017-06-02 07:44:53 +02001379 mode_cmd->width > 64 && /* Don't create a proxy for cursor */
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001380 dev_priv->active_display_unit == vmw_du_screen_target) {
1381 ret = vmw_create_dmabuf_proxy(dev_priv->dev, mode_cmd,
1382 dmabuf, &surface);
1383 if (ret)
1384 return ERR_PTR(ret);
1385
1386 is_dmabuf_proxy = true;
1387 }
1388
1389 /* Create the new framebuffer depending one what we have */
Sinclair Yeh05c95012015-08-11 22:53:39 -07001390 if (surface) {
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001391 ret = vmw_kms_new_framebuffer_surface(dev_priv, surface, &vfb,
1392 mode_cmd,
1393 is_dmabuf_proxy);
Sinclair Yeh05c95012015-08-11 22:53:39 -07001394
1395 /*
1396 * vmw_create_dmabuf_proxy() adds a reference that is no longer
1397 * needed
1398 */
1399 if (is_dmabuf_proxy)
1400 vmw_surface_unreference(&surface);
1401 } else if (dmabuf) {
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001402 ret = vmw_kms_new_framebuffer_dmabuf(dev_priv, dmabuf, &vfb,
1403 mode_cmd);
Sinclair Yeh05c95012015-08-11 22:53:39 -07001404 } else {
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001405 BUG();
Sinclair Yeh05c95012015-08-11 22:53:39 -07001406 }
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001407
1408 if (ret)
1409 return ERR_PTR(ret);
1410
1411 vfb->pin = vmw_framebuffer_pin;
1412 vfb->unpin = vmw_framebuffer_unpin;
1413
1414 return vfb;
1415}
1416
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001417/*
1418 * Generic Kernel modesetting functions
1419 */
1420
1421static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev,
1422 struct drm_file *file_priv,
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001423 const struct drm_mode_fb_cmd2 *mode_cmd)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001424{
1425 struct vmw_private *dev_priv = vmw_priv(dev);
1426 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
1427 struct vmw_framebuffer *vfb = NULL;
1428 struct vmw_surface *surface = NULL;
1429 struct vmw_dma_buffer *bo = NULL;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001430 struct ttm_base_object *user_obj;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001431 int ret;
1432
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001433 /**
1434 * This code should be conditioned on Screen Objects not being used.
1435 * If screen objects are used, we can allocate a GMR to hold the
1436 * requested framebuffer.
1437 */
1438
Xi Wang8a783892011-12-21 05:18:33 -05001439 if (!vmw_kms_validate_mode_vram(dev_priv,
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001440 mode_cmd->pitches[0],
1441 mode_cmd->height)) {
Sinclair Yehc8261a92015-06-26 01:23:42 -07001442 DRM_ERROR("Requested mode exceed bounding box limit.\n");
Jakob Bornecrantzd9826402011-11-03 21:03:04 +01001443 return ERR_PTR(-ENOMEM);
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001444 }
1445
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001446 /*
1447 * Take a reference on the user object of the resource
1448 * backing the kms fb. This ensures that user-space handle
1449 * lookups on that resource will always work as long as
1450 * it's registered with a kms framebuffer. This is important,
1451 * since vmw_execbuf_process identifies resources in the
1452 * command stream using user-space handles.
1453 */
1454
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001455 user_obj = ttm_base_object_lookup(tfile, mode_cmd->handles[0]);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001456 if (unlikely(user_obj == NULL)) {
1457 DRM_ERROR("Could not locate requested kms frame buffer.\n");
1458 return ERR_PTR(-ENOENT);
1459 }
1460
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001461 /**
1462 * End conditioned code.
1463 */
1464
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +01001465 /* returns either a dmabuf or surface */
1466 ret = vmw_user_lookup_handle(dev_priv, tfile,
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001467 mode_cmd->handles[0],
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +01001468 &surface, &bo);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001469 if (ret)
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +01001470 goto err_out;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001471
Sinclair Yeh810b3e162017-03-23 15:39:16 -07001472
1473 if (!bo &&
1474 !vmw_kms_srf_ok(dev_priv, mode_cmd->width, mode_cmd->height)) {
1475 DRM_ERROR("Surface size cannot exceed %dx%d",
1476 dev_priv->texture_max_width,
1477 dev_priv->texture_max_height);
1478 goto err_out;
1479 }
1480
1481
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001482 vfb = vmw_kms_new_framebuffer(dev_priv, bo, surface,
1483 !(dev_priv->capabilities & SVGA_CAP_3D),
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001484 mode_cmd);
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001485 if (IS_ERR(vfb)) {
1486 ret = PTR_ERR(vfb);
1487 goto err_out;
1488 }
Jakob Bornecrantz5ffdb652010-01-30 03:38:08 +00001489
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +01001490err_out:
1491 /* vmw_user_lookup_handle takes one ref so does new_fb */
1492 if (bo)
1493 vmw_dmabuf_unreference(&bo);
1494 if (surface)
1495 vmw_surface_unreference(&surface);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001496
1497 if (ret) {
1498 DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001499 ttm_base_object_unref(&user_obj);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01001500 return ERR_PTR(ret);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001501 } else
1502 vfb->user_obj = user_obj;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001503
1504 return &vfb->base;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001505}
1506
Sinclair Yehc46a3062017-03-23 14:24:53 -07001507
1508
1509/**
1510 * vmw_kms_atomic_check_modeset- validate state object for modeset changes
1511 *
1512 * @dev: DRM device
1513 * @state: the driver state object
1514 *
1515 * This is a simple wrapper around drm_atomic_helper_check_modeset() for
1516 * us to assign a value to mode->crtc_clock so that
1517 * drm_calc_timestamping_constants() won't throw an error message
1518 *
1519 * RETURNS
1520 * Zero for success or -errno
1521 */
Maarten Lankhorstbdc362f2017-07-12 10:13:33 +02001522static int
Sinclair Yehc46a3062017-03-23 14:24:53 -07001523vmw_kms_atomic_check_modeset(struct drm_device *dev,
1524 struct drm_atomic_state *state)
1525{
1526 struct drm_crtc_state *crtc_state;
1527 struct drm_crtc *crtc;
1528 struct vmw_private *dev_priv = vmw_priv(dev);
1529 int i;
1530
Maarten Lankhorstbdc362f2017-07-12 10:13:33 +02001531 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
Sinclair Yehc46a3062017-03-23 14:24:53 -07001532 unsigned long requested_bb_mem = 0;
1533
1534 if (dev_priv->active_display_unit == vmw_du_screen_target) {
1535 if (crtc->primary->fb) {
1536 int cpp = crtc->primary->fb->pitches[0] /
1537 crtc->primary->fb->width;
1538
1539 requested_bb_mem += crtc->mode.hdisplay * cpp *
1540 crtc->mode.vdisplay;
1541 }
1542
1543 if (requested_bb_mem > dev_priv->prim_bb_mem)
1544 return -EINVAL;
1545 }
1546 }
1547
1548 return drm_atomic_helper_check(dev, state);
1549}
1550
1551
Sinclair Yeh021aba72017-08-29 18:55:09 +02001552/**
1553 * vmw_kms_atomic_commit - Perform an atomic state commit
1554 *
1555 * @dev: DRM device
1556 * @state: the driver state object
1557 * @nonblock: Whether nonblocking behaviour is requested
1558 *
1559 * This is a simple wrapper around drm_atomic_helper_commit() for
1560 * us to clear the nonblocking value.
1561 *
1562 * Nonblocking commits currently cause synchronization issues
1563 * for vmwgfx.
1564 *
1565 * RETURNS
1566 * Zero for success or negative error code on failure.
1567 */
1568int vmw_kms_atomic_commit(struct drm_device *dev,
1569 struct drm_atomic_state *state,
1570 bool nonblock)
1571{
1572 return drm_atomic_helper_commit(dev, state, false);
1573}
1574
1575
Laurent Pincharte6ecefa2012-05-17 13:27:23 +02001576static const struct drm_mode_config_funcs vmw_kms_funcs = {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001577 .fb_create = vmw_kms_fb_create,
Sinclair Yehc46a3062017-03-23 14:24:53 -07001578 .atomic_check = vmw_kms_atomic_check_modeset,
Sinclair Yeh021aba72017-08-29 18:55:09 +02001579 .atomic_commit = vmw_kms_atomic_commit,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001580};
1581
Thomas Hellstromb9eb1a62015-04-02 02:39:45 -07001582static int vmw_kms_generic_present(struct vmw_private *dev_priv,
1583 struct drm_file *file_priv,
1584 struct vmw_framebuffer *vfb,
1585 struct vmw_surface *surface,
1586 uint32_t sid,
1587 int32_t destX, int32_t destY,
1588 struct drm_vmw_rect *clips,
1589 uint32_t num_clips)
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001590{
Thomas Hellstrom10b1e0c2015-06-26 02:14:27 -07001591 return vmw_kms_sou_do_surface_dirty(dev_priv, vfb, NULL, clips,
1592 &surface->res, destX, destY,
1593 num_clips, 1, NULL);
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001594}
1595
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001596
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001597int vmw_kms_present(struct vmw_private *dev_priv,
1598 struct drm_file *file_priv,
1599 struct vmw_framebuffer *vfb,
1600 struct vmw_surface *surface,
1601 uint32_t sid,
1602 int32_t destX, int32_t destY,
1603 struct drm_vmw_rect *clips,
1604 uint32_t num_clips)
1605{
Sinclair Yeh35c05122015-06-26 01:42:06 -07001606 int ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001607
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001608 switch (dev_priv->active_display_unit) {
1609 case vmw_du_screen_target:
1610 ret = vmw_kms_stdu_surface_dirty(dev_priv, vfb, NULL, clips,
1611 &surface->res, destX, destY,
1612 num_clips, 1, NULL);
1613 break;
1614 case vmw_du_screen_object:
1615 ret = vmw_kms_generic_present(dev_priv, file_priv, vfb, surface,
1616 sid, destX, destY, clips,
1617 num_clips);
1618 break;
1619 default:
1620 WARN_ONCE(true,
1621 "Present called with invalid display system.\n");
1622 ret = -ENOSYS;
1623 break;
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001624 }
Sinclair Yeh35c05122015-06-26 01:42:06 -07001625 if (ret)
1626 return ret;
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001627
Sinclair Yeh35c05122015-06-26 01:42:06 -07001628 vmw_fifo_flush(dev_priv, false);
Michel Dänzer0bef23f2011-08-31 07:42:50 +00001629
Sinclair Yeh35c05122015-06-26 01:42:06 -07001630 return 0;
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001631}
1632
Thomas Hellstrom578e6092016-02-12 09:45:42 +01001633static void
1634vmw_kms_create_hotplug_mode_update_property(struct vmw_private *dev_priv)
1635{
1636 if (dev_priv->hotplug_mode_update_property)
1637 return;
1638
1639 dev_priv->hotplug_mode_update_property =
1640 drm_property_create_range(dev_priv->dev,
1641 DRM_MODE_PROP_IMMUTABLE,
1642 "hotplug_mode_update", 0, 1);
1643
1644 if (!dev_priv->hotplug_mode_update_property)
1645 return;
1646
1647}
1648
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001649int vmw_kms_init(struct vmw_private *dev_priv)
1650{
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001651 struct drm_device *dev = dev_priv->dev;
1652 int ret;
1653
1654 drm_mode_config_init(dev);
1655 dev->mode_config.funcs = &vmw_kms_funcs;
1656 dev->mode_config.min_width = 1;
1657 dev->mode_config.min_height = 1;
Sinclair Yeh65ade7d2015-07-16 10:49:13 -07001658 dev->mode_config.max_width = dev_priv->texture_max_width;
1659 dev->mode_config.max_height = dev_priv->texture_max_height;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001660
Thomas Hellstrom578e6092016-02-12 09:45:42 +01001661 drm_mode_create_suggested_offset_properties(dev);
1662 vmw_kms_create_hotplug_mode_update_property(dev_priv);
1663
Sinclair Yeh35c05122015-06-26 01:42:06 -07001664 ret = vmw_kms_stdu_init_display(dev_priv);
1665 if (ret) {
1666 ret = vmw_kms_sou_init_display(dev_priv);
1667 if (ret) /* Fallback */
1668 ret = vmw_kms_ldu_init_display(dev_priv);
1669 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001670
Sinclair Yehc8261a92015-06-26 01:23:42 -07001671 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001672}
1673
1674int vmw_kms_close(struct vmw_private *dev_priv)
1675{
Daniel Vetter5f58e972017-06-26 18:19:48 +02001676 int ret = 0;
Sinclair Yehc8261a92015-06-26 01:23:42 -07001677
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001678 /*
1679 * Docs says we should take the lock before calling this function
1680 * but since it destroys encoders and our destructor calls
1681 * drm_encoder_cleanup which takes the lock we deadlock.
1682 */
1683 drm_mode_config_cleanup(dev_priv->dev);
Daniel Vetter5f58e972017-06-26 18:19:48 +02001684 if (dev_priv->active_display_unit == vmw_du_legacy)
Sinclair Yehc8261a92015-06-26 01:23:42 -07001685 ret = vmw_kms_ldu_close_display(dev_priv);
1686
1687 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001688}
1689
1690int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
1691 struct drm_file *file_priv)
1692{
1693 struct drm_vmw_cursor_bypass_arg *arg = data;
1694 struct vmw_display_unit *du;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001695 struct drm_crtc *crtc;
1696 int ret = 0;
1697
1698
1699 mutex_lock(&dev->mode_config.mutex);
1700 if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
1701
1702 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1703 du = vmw_crtc_to_du(crtc);
1704 du->hotspot_x = arg->xhot;
1705 du->hotspot_y = arg->yhot;
1706 }
1707
1708 mutex_unlock(&dev->mode_config.mutex);
1709 return 0;
1710 }
1711
Rob Clarka4cd5d62014-07-17 23:30:02 -04001712 crtc = drm_crtc_find(dev, arg->crtc_id);
1713 if (!crtc) {
Ville Syrjälä4ae87ff2013-10-17 13:35:05 +03001714 ret = -ENOENT;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001715 goto out;
1716 }
1717
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001718 du = vmw_crtc_to_du(crtc);
1719
1720 du->hotspot_x = arg->xhot;
1721 du->hotspot_y = arg->yhot;
1722
1723out:
1724 mutex_unlock(&dev->mode_config.mutex);
1725
1726 return ret;
1727}
1728
1729int vmw_kms_write_svga(struct vmw_private *vmw_priv,
1730 unsigned width, unsigned height, unsigned pitch,
1731 unsigned bpp, unsigned depth)
1732{
1733 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1734 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch);
1735 else if (vmw_fifo_have_pitchlock(vmw_priv))
Thomas Hellstromb76ff5e2015-10-28 10:44:04 +01001736 vmw_mmio_write(pitch, vmw_priv->mmio_virt +
1737 SVGA_FIFO_PITCHLOCK);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001738 vmw_write(vmw_priv, SVGA_REG_WIDTH, width);
1739 vmw_write(vmw_priv, SVGA_REG_HEIGHT, height);
1740 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp);
1741
1742 if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) {
1743 DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n",
1744 depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH));
1745 return -EINVAL;
1746 }
1747
1748 return 0;
1749}
1750
1751int vmw_kms_save_vga(struct vmw_private *vmw_priv)
1752{
1753 struct vmw_vga_topology_state *save;
1754 uint32_t i;
1755
1756 vmw_priv->vga_width = vmw_read(vmw_priv, SVGA_REG_WIDTH);
1757 vmw_priv->vga_height = vmw_read(vmw_priv, SVGA_REG_HEIGHT);
1758 vmw_priv->vga_bpp = vmw_read(vmw_priv, SVGA_REG_BITS_PER_PIXEL);
1759 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1760 vmw_priv->vga_pitchlock =
1761 vmw_read(vmw_priv, SVGA_REG_PITCHLOCK);
1762 else if (vmw_fifo_have_pitchlock(vmw_priv))
Thomas Hellstromb76ff5e2015-10-28 10:44:04 +01001763 vmw_priv->vga_pitchlock = vmw_mmio_read(vmw_priv->mmio_virt +
1764 SVGA_FIFO_PITCHLOCK);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001765
1766 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1767 return 0;
1768
1769 vmw_priv->num_displays = vmw_read(vmw_priv,
1770 SVGA_REG_NUM_GUEST_DISPLAYS);
1771
1772 if (vmw_priv->num_displays == 0)
1773 vmw_priv->num_displays = 1;
1774
1775 for (i = 0; i < vmw_priv->num_displays; ++i) {
1776 save = &vmw_priv->vga_save[i];
1777 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1778 save->primary = vmw_read(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY);
1779 save->pos_x = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_X);
1780 save->pos_y = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y);
1781 save->width = vmw_read(vmw_priv, SVGA_REG_DISPLAY_WIDTH);
1782 save->height = vmw_read(vmw_priv, SVGA_REG_DISPLAY_HEIGHT);
1783 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
1784 if (i == 0 && vmw_priv->num_displays == 1 &&
1785 save->width == 0 && save->height == 0) {
1786
1787 /*
1788 * It should be fairly safe to assume that these
1789 * values are uninitialized.
1790 */
1791
1792 save->width = vmw_priv->vga_width - save->pos_x;
1793 save->height = vmw_priv->vga_height - save->pos_y;
1794 }
1795 }
1796
1797 return 0;
1798}
1799
1800int vmw_kms_restore_vga(struct vmw_private *vmw_priv)
1801{
1802 struct vmw_vga_topology_state *save;
1803 uint32_t i;
1804
1805 vmw_write(vmw_priv, SVGA_REG_WIDTH, vmw_priv->vga_width);
1806 vmw_write(vmw_priv, SVGA_REG_HEIGHT, vmw_priv->vga_height);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001807 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, vmw_priv->vga_bpp);
1808 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1809 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK,
1810 vmw_priv->vga_pitchlock);
1811 else if (vmw_fifo_have_pitchlock(vmw_priv))
Thomas Hellstromb76ff5e2015-10-28 10:44:04 +01001812 vmw_mmio_write(vmw_priv->vga_pitchlock,
1813 vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001814
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001815 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1816 return 0;
1817
1818 for (i = 0; i < vmw_priv->num_displays; ++i) {
1819 save = &vmw_priv->vga_save[i];
1820 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1821 vmw_write(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY, save->primary);
1822 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_X, save->pos_x);
1823 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y, save->pos_y);
1824 vmw_write(vmw_priv, SVGA_REG_DISPLAY_WIDTH, save->width);
1825 vmw_write(vmw_priv, SVGA_REG_DISPLAY_HEIGHT, save->height);
1826 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
1827 }
1828
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001829 return 0;
1830}
Jakob Bornecrantzd8bd19d2010-06-01 11:54:20 +02001831
Thomas Hellstrome133e732010-10-05 12:43:04 +02001832bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
1833 uint32_t pitch,
1834 uint32_t height)
1835{
Sinclair Yeh35c05122015-06-26 01:42:06 -07001836 return ((u64) pitch * (u64) height) < (u64)
1837 ((dev_priv->active_display_unit == vmw_du_screen_target) ?
1838 dev_priv->prim_bb_mem : dev_priv->vram_size);
Thomas Hellstrome133e732010-10-05 12:43:04 +02001839}
1840
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001841
1842/**
1843 * Function called by DRM code called with vbl_lock held.
1844 */
Thierry Reding88e72712015-09-24 18:35:31 +02001845u32 vmw_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
Thomas Hellstrom7a1c2f62010-10-01 10:21:49 +02001846{
1847 return 0;
1848}
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001849
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001850/**
1851 * Function called by DRM code called with vbl_lock held.
1852 */
Thierry Reding88e72712015-09-24 18:35:31 +02001853int vmw_enable_vblank(struct drm_device *dev, unsigned int pipe)
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001854{
1855 return -ENOSYS;
1856}
1857
1858/**
1859 * Function called by DRM code called with vbl_lock held.
1860 */
Thierry Reding88e72712015-09-24 18:35:31 +02001861void vmw_disable_vblank(struct drm_device *dev, unsigned int pipe)
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001862{
1863}
1864
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001865
1866/*
1867 * Small shared kms functions.
1868 */
1869
Rashika Kheria847c5962014-01-06 22:18:10 +05301870static int vmw_du_update_layout(struct vmw_private *dev_priv, unsigned num,
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001871 struct drm_vmw_rect *rects)
1872{
1873 struct drm_device *dev = dev_priv->dev;
1874 struct vmw_display_unit *du;
1875 struct drm_connector *con;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001876
1877 mutex_lock(&dev->mode_config.mutex);
1878
1879#if 0
Thomas Hellstrom6ea77d12011-10-04 20:13:36 +02001880 {
1881 unsigned int i;
1882
1883 DRM_INFO("%s: new layout ", __func__);
1884 for (i = 0; i < num; i++)
1885 DRM_INFO("(%i, %i %ux%u) ", rects[i].x, rects[i].y,
1886 rects[i].w, rects[i].h);
1887 DRM_INFO("\n");
1888 }
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001889#endif
1890
1891 list_for_each_entry(con, &dev->mode_config.connector_list, head) {
1892 du = vmw_connector_to_du(con);
1893 if (num > du->unit) {
1894 du->pref_width = rects[du->unit].w;
1895 du->pref_height = rects[du->unit].h;
1896 du->pref_active = true;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001897 du->gui_x = rects[du->unit].x;
1898 du->gui_y = rects[du->unit].y;
Thomas Hellstrom578e6092016-02-12 09:45:42 +01001899 drm_object_property_set_value
1900 (&con->base, dev->mode_config.suggested_x_property,
1901 du->gui_x);
1902 drm_object_property_set_value
1903 (&con->base, dev->mode_config.suggested_y_property,
1904 du->gui_y);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001905 } else {
1906 du->pref_width = 800;
1907 du->pref_height = 600;
1908 du->pref_active = false;
Thomas Hellstrom578e6092016-02-12 09:45:42 +01001909 drm_object_property_set_value
1910 (&con->base, dev->mode_config.suggested_x_property,
1911 0);
1912 drm_object_property_set_value
1913 (&con->base, dev->mode_config.suggested_y_property,
1914 0);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001915 }
1916 con->status = vmw_du_connector_detect(con, true);
1917 }
1918
1919 mutex_unlock(&dev->mode_config.mutex);
Thomas Hellstrom578e6092016-02-12 09:45:42 +01001920 drm_sysfs_hotplug_event(dev);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001921
1922 return 0;
1923}
1924
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02001925int vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
1926 u16 *r, u16 *g, u16 *b,
Daniel Vetter6d124ff2017-04-03 10:33:01 +02001927 uint32_t size,
1928 struct drm_modeset_acquire_ctx *ctx)
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001929{
1930 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
1931 int i;
1932
1933 for (i = 0; i < size; i++) {
1934 DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i,
1935 r[i], g[i], b[i]);
1936 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 0, r[i] >> 8);
1937 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 1, g[i] >> 8);
1938 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 2, b[i] >> 8);
1939 }
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02001940
1941 return 0;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001942}
1943
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02001944int vmw_du_connector_dpms(struct drm_connector *connector, int mode)
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001945{
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02001946 return 0;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001947}
1948
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001949enum drm_connector_status
1950vmw_du_connector_detect(struct drm_connector *connector, bool force)
1951{
1952 uint32_t num_displays;
1953 struct drm_device *dev = connector->dev;
1954 struct vmw_private *dev_priv = vmw_priv(dev);
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001955 struct vmw_display_unit *du = vmw_connector_to_du(connector);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001956
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001957 num_displays = vmw_read(dev_priv, SVGA_REG_NUM_DISPLAYS);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001958
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001959 return ((vmw_connector_to_du(connector)->unit < num_displays &&
1960 du->pref_active) ?
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001961 connector_status_connected : connector_status_disconnected);
1962}
1963
1964static struct drm_display_mode vmw_kms_connector_builtin[] = {
1965 /* 640x480@60Hz */
1966 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
1967 752, 800, 0, 480, 489, 492, 525, 0,
1968 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1969 /* 800x600@60Hz */
1970 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
1971 968, 1056, 0, 600, 601, 605, 628, 0,
1972 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1973 /* 1024x768@60Hz */
1974 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
1975 1184, 1344, 0, 768, 771, 777, 806, 0,
1976 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1977 /* 1152x864@75Hz */
1978 { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
1979 1344, 1600, 0, 864, 865, 868, 900, 0,
1980 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1981 /* 1280x768@60Hz */
1982 { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344,
1983 1472, 1664, 0, 768, 771, 778, 798, 0,
1984 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1985 /* 1280x800@60Hz */
1986 { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352,
1987 1480, 1680, 0, 800, 803, 809, 831, 0,
1988 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
1989 /* 1280x960@60Hz */
1990 { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376,
1991 1488, 1800, 0, 960, 961, 964, 1000, 0,
1992 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1993 /* 1280x1024@60Hz */
1994 { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328,
1995 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
1996 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1997 /* 1360x768@60Hz */
1998 { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424,
1999 1536, 1792, 0, 768, 771, 777, 795, 0,
2000 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
2001 /* 1440x1050@60Hz */
2002 { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488,
2003 1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
2004 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2005 /* 1440x900@60Hz */
2006 { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520,
2007 1672, 1904, 0, 900, 903, 909, 934, 0,
2008 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2009 /* 1600x1200@60Hz */
2010 { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664,
2011 1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
2012 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
2013 /* 1680x1050@60Hz */
2014 { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784,
2015 1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
2016 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2017 /* 1792x1344@60Hz */
2018 { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920,
2019 2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
2020 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2021 /* 1853x1392@60Hz */
2022 { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952,
2023 2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
2024 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2025 /* 1920x1200@60Hz */
2026 { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056,
2027 2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
2028 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2029 /* 1920x1440@60Hz */
2030 { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048,
2031 2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
2032 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2033 /* 2560x1600@60Hz */
2034 { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752,
2035 3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
2036 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2037 /* Terminate */
2038 { DRM_MODE("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) },
2039};
2040
Thomas Hellstrom1543b4d2011-11-02 09:43:10 +01002041/**
2042 * vmw_guess_mode_timing - Provide fake timings for a
2043 * 60Hz vrefresh mode.
2044 *
2045 * @mode - Pointer to a struct drm_display_mode with hdisplay and vdisplay
2046 * members filled in.
2047 */
Thomas Hellstroma2787242015-06-29 12:55:07 -07002048void vmw_guess_mode_timing(struct drm_display_mode *mode)
Thomas Hellstrom1543b4d2011-11-02 09:43:10 +01002049{
2050 mode->hsync_start = mode->hdisplay + 50;
2051 mode->hsync_end = mode->hsync_start + 50;
2052 mode->htotal = mode->hsync_end + 50;
2053
2054 mode->vsync_start = mode->vdisplay + 50;
2055 mode->vsync_end = mode->vsync_start + 50;
2056 mode->vtotal = mode->vsync_end + 50;
2057
2058 mode->clock = (u32)mode->htotal * (u32)mode->vtotal / 100 * 6;
2059 mode->vrefresh = drm_mode_vrefresh(mode);
2060}
2061
2062
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002063int vmw_du_connector_fill_modes(struct drm_connector *connector,
2064 uint32_t max_width, uint32_t max_height)
2065{
2066 struct vmw_display_unit *du = vmw_connector_to_du(connector);
2067 struct drm_device *dev = connector->dev;
2068 struct vmw_private *dev_priv = vmw_priv(dev);
2069 struct drm_display_mode *mode = NULL;
2070 struct drm_display_mode *bmode;
2071 struct drm_display_mode prefmode = { DRM_MODE("preferred",
2072 DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
2073 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2074 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
2075 };
2076 int i;
Sinclair Yeh7c20d212016-06-29 11:29:47 -07002077 u32 assumed_bpp = 4;
Sinclair Yeh9a723842014-10-31 09:58:06 +01002078
Sinclair Yeh04319d82016-06-29 12:15:48 -07002079 if (dev_priv->assume_16bpp)
2080 assumed_bpp = 2;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002081
Sinclair Yeh35c05122015-06-26 01:42:06 -07002082 if (dev_priv->active_display_unit == vmw_du_screen_target) {
2083 max_width = min(max_width, dev_priv->stdu_max_width);
Sinclair Yeh28c95422017-03-27 11:12:27 -07002084 max_width = min(max_width, dev_priv->texture_max_width);
2085
Sinclair Yeh35c05122015-06-26 01:42:06 -07002086 max_height = min(max_height, dev_priv->stdu_max_height);
Sinclair Yeh28c95422017-03-27 11:12:27 -07002087 max_height = min(max_height, dev_priv->texture_max_height);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002088 }
2089
2090 /* Add preferred mode */
Sinclair Yehc8261a92015-06-26 01:23:42 -07002091 mode = drm_mode_duplicate(dev, &prefmode);
2092 if (!mode)
2093 return 0;
2094 mode->hdisplay = du->pref_width;
2095 mode->vdisplay = du->pref_height;
2096 vmw_guess_mode_timing(mode);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002097
Sinclair Yehc8261a92015-06-26 01:23:42 -07002098 if (vmw_kms_validate_mode_vram(dev_priv,
2099 mode->hdisplay * assumed_bpp,
2100 mode->vdisplay)) {
2101 drm_mode_probed_add(connector, mode);
2102 } else {
2103 drm_mode_destroy(dev, mode);
2104 mode = NULL;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002105 }
2106
Sinclair Yehc8261a92015-06-26 01:23:42 -07002107 if (du->pref_mode) {
2108 list_del_init(&du->pref_mode->head);
2109 drm_mode_destroy(dev, du->pref_mode);
2110 }
2111
2112 /* mode might be null here, this is intended */
2113 du->pref_mode = mode;
2114
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002115 for (i = 0; vmw_kms_connector_builtin[i].type != 0; i++) {
2116 bmode = &vmw_kms_connector_builtin[i];
2117 if (bmode->hdisplay > max_width ||
2118 bmode->vdisplay > max_height)
2119 continue;
2120
Sinclair Yeh9a723842014-10-31 09:58:06 +01002121 if (!vmw_kms_validate_mode_vram(dev_priv,
2122 bmode->hdisplay * assumed_bpp,
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002123 bmode->vdisplay))
2124 continue;
2125
2126 mode = drm_mode_duplicate(dev, bmode);
2127 if (!mode)
2128 return 0;
2129 mode->vrefresh = drm_mode_vrefresh(mode);
2130
2131 drm_mode_probed_add(connector, mode);
2132 }
2133
Ville Syrjälä6af3e652015-12-03 23:14:14 +02002134 drm_mode_connector_list_update(connector);
Thomas Hellstromf6b05002015-06-29 12:59:58 -07002135 /* Move the prefered mode first, help apps pick the right mode. */
2136 drm_mode_sort(&connector->modes);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002137
2138 return 1;
2139}
2140
2141int vmw_du_connector_set_property(struct drm_connector *connector,
2142 struct drm_property *property,
2143 uint64_t val)
2144{
Thomas Hellstrom76404ac2016-02-12 09:55:45 +01002145 struct vmw_display_unit *du = vmw_connector_to_du(connector);
2146 struct vmw_private *dev_priv = vmw_priv(connector->dev);
2147
2148 if (property == dev_priv->implicit_placement_property)
2149 du->is_implicit = val;
2150
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002151 return 0;
2152}
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002153
2154
Sinclair Yeh9c2542a2017-03-23 11:33:39 -07002155
Sinclair Yehd7721ca2017-03-23 11:48:44 -07002156/**
2157 * vmw_du_connector_atomic_set_property - Atomic version of get property
2158 *
2159 * @crtc - crtc the property is associated with
2160 *
2161 * Returns:
2162 * Zero on success, negative errno on failure.
2163 */
2164int
2165vmw_du_connector_atomic_set_property(struct drm_connector *connector,
2166 struct drm_connector_state *state,
2167 struct drm_property *property,
2168 uint64_t val)
2169{
2170 struct vmw_private *dev_priv = vmw_priv(connector->dev);
2171 struct vmw_connector_state *vcs = vmw_connector_state_to_vcs(state);
2172 struct vmw_display_unit *du = vmw_connector_to_du(connector);
2173
2174
2175 if (property == dev_priv->implicit_placement_property) {
2176 vcs->is_implicit = val;
2177
2178 /*
2179 * We should really be doing a drm_atomic_commit() to
2180 * commit the new state, but since this doesn't cause
2181 * an immedate state change, this is probably ok
2182 */
2183 du->is_implicit = vcs->is_implicit;
2184 } else {
2185 return -EINVAL;
2186 }
2187
2188 return 0;
2189}
2190
2191
2192/**
2193 * vmw_du_connector_atomic_get_property - Atomic version of get property
2194 *
2195 * @connector - connector the property is associated with
2196 *
2197 * Returns:
2198 * Zero on success, negative errno on failure.
2199 */
2200int
2201vmw_du_connector_atomic_get_property(struct drm_connector *connector,
2202 const struct drm_connector_state *state,
2203 struct drm_property *property,
2204 uint64_t *val)
2205{
2206 struct vmw_private *dev_priv = vmw_priv(connector->dev);
2207 struct vmw_connector_state *vcs = vmw_connector_state_to_vcs(state);
2208
2209 if (property == dev_priv->implicit_placement_property)
2210 *val = vcs->is_implicit;
2211 else {
2212 DRM_ERROR("Invalid Property %s\n", property->name);
2213 return -EINVAL;
2214 }
2215
2216 return 0;
2217}
2218
2219
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002220int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data,
2221 struct drm_file *file_priv)
2222{
2223 struct vmw_private *dev_priv = vmw_priv(dev);
2224 struct drm_vmw_update_layout_arg *arg =
2225 (struct drm_vmw_update_layout_arg *)data;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002226 void __user *user_rects;
2227 struct drm_vmw_rect *rects;
2228 unsigned rects_size;
2229 int ret;
2230 int i;
Sinclair Yeh65ade7d2015-07-16 10:49:13 -07002231 u64 total_pixels = 0;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002232 struct drm_mode_config *mode_config = &dev->mode_config;
Sinclair Yehc8261a92015-06-26 01:23:42 -07002233 struct drm_vmw_rect bounding_box = {0};
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002234
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002235 if (!arg->num_outputs) {
2236 struct drm_vmw_rect def_rect = {0, 0, 800, 600};
2237 vmw_du_update_layout(dev_priv, 1, &def_rect);
Thomas Hellstrom5151adb2015-03-09 01:56:21 -07002238 return 0;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002239 }
2240
2241 rects_size = arg->num_outputs * sizeof(struct drm_vmw_rect);
Xi Wangbab9efc2011-11-28 12:25:43 +01002242 rects = kcalloc(arg->num_outputs, sizeof(struct drm_vmw_rect),
2243 GFP_KERNEL);
Thomas Hellstrom5151adb2015-03-09 01:56:21 -07002244 if (unlikely(!rects))
2245 return -ENOMEM;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002246
2247 user_rects = (void __user *)(unsigned long)arg->rects;
2248 ret = copy_from_user(rects, user_rects, rects_size);
2249 if (unlikely(ret != 0)) {
2250 DRM_ERROR("Failed to get rects.\n");
2251 ret = -EFAULT;
2252 goto out_free;
2253 }
2254
2255 for (i = 0; i < arg->num_outputs; ++i) {
Xi Wangbab9efc2011-11-28 12:25:43 +01002256 if (rects[i].x < 0 ||
2257 rects[i].y < 0 ||
2258 rects[i].x + rects[i].w > mode_config->max_width ||
2259 rects[i].y + rects[i].h > mode_config->max_height) {
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002260 DRM_ERROR("Invalid GUI layout.\n");
2261 ret = -EINVAL;
2262 goto out_free;
2263 }
Sinclair Yehc8261a92015-06-26 01:23:42 -07002264
2265 /*
2266 * bounding_box.w and bunding_box.h are used as
2267 * lower-right coordinates
2268 */
2269 if (rects[i].x + rects[i].w > bounding_box.w)
2270 bounding_box.w = rects[i].x + rects[i].w;
2271
2272 if (rects[i].y + rects[i].h > bounding_box.h)
2273 bounding_box.h = rects[i].y + rects[i].h;
Sinclair Yeh65ade7d2015-07-16 10:49:13 -07002274
2275 total_pixels += (u64) rects[i].w * (u64) rects[i].h;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002276 }
2277
Sinclair Yeh65ade7d2015-07-16 10:49:13 -07002278 if (dev_priv->active_display_unit == vmw_du_screen_target) {
2279 /*
2280 * For Screen Targets, the limits for a toplogy are:
2281 * 1. Bounding box (assuming 32bpp) must be < prim_bb_mem
2282 * 2. Total pixels (assuming 32bpp) must be < prim_bb_mem
2283 */
Thomas Hellstrom0f580382017-01-19 11:01:04 -08002284 u64 bb_mem = (u64) bounding_box.w * bounding_box.h * 4;
Sinclair Yeh65ade7d2015-07-16 10:49:13 -07002285 u64 pixel_mem = total_pixels * 4;
2286
2287 if (bb_mem > dev_priv->prim_bb_mem) {
2288 DRM_ERROR("Topology is beyond supported limits.\n");
Sinclair Yeh35c05122015-06-26 01:42:06 -07002289 ret = -EINVAL;
2290 goto out_free;
2291 }
2292
Sinclair Yeh65ade7d2015-07-16 10:49:13 -07002293 if (pixel_mem > dev_priv->prim_bb_mem) {
2294 DRM_ERROR("Combined output size too large\n");
2295 ret = -EINVAL;
2296 goto out_free;
2297 }
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002298 }
2299
2300 vmw_du_update_layout(dev_priv, arg->num_outputs, rects);
2301
2302out_free:
2303 kfree(rects);
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002304 return ret;
2305}
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07002306
2307/**
2308 * vmw_kms_helper_dirty - Helper to build commands and perform actions based
2309 * on a set of cliprects and a set of display units.
2310 *
2311 * @dev_priv: Pointer to a device private structure.
2312 * @framebuffer: Pointer to the framebuffer on which to perform the actions.
2313 * @clips: A set of struct drm_clip_rect. Either this os @vclips must be NULL.
2314 * Cliprects are given in framebuffer coordinates.
2315 * @vclips: A set of struct drm_vmw_rect cliprects. Either this or @clips must
2316 * be NULL. Cliprects are given in source coordinates.
2317 * @dest_x: X coordinate offset for the crtc / destination clip rects.
2318 * @dest_y: Y coordinate offset for the crtc / destination clip rects.
2319 * @num_clips: Number of cliprects in the @clips or @vclips array.
2320 * @increment: Integer with which to increment the clip counter when looping.
2321 * Used to skip a predetermined number of clip rects.
2322 * @dirty: Closure structure. See the description of struct vmw_kms_dirty.
2323 */
2324int vmw_kms_helper_dirty(struct vmw_private *dev_priv,
2325 struct vmw_framebuffer *framebuffer,
2326 const struct drm_clip_rect *clips,
2327 const struct drm_vmw_rect *vclips,
2328 s32 dest_x, s32 dest_y,
2329 int num_clips,
2330 int increment,
2331 struct vmw_kms_dirty *dirty)
2332{
2333 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
2334 struct drm_crtc *crtc;
2335 u32 num_units = 0;
2336 u32 i, k;
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07002337
2338 dirty->dev_priv = dev_priv;
2339
2340 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
2341 if (crtc->primary->fb != &framebuffer->base)
2342 continue;
2343 units[num_units++] = vmw_crtc_to_du(crtc);
2344 }
2345
2346 for (k = 0; k < num_units; k++) {
2347 struct vmw_display_unit *unit = units[k];
2348 s32 crtc_x = unit->crtc.x;
2349 s32 crtc_y = unit->crtc.y;
2350 s32 crtc_width = unit->crtc.mode.hdisplay;
2351 s32 crtc_height = unit->crtc.mode.vdisplay;
2352 const struct drm_clip_rect *clips_ptr = clips;
2353 const struct drm_vmw_rect *vclips_ptr = vclips;
2354
2355 dirty->unit = unit;
2356 if (dirty->fifo_reserve_size > 0) {
2357 dirty->cmd = vmw_fifo_reserve(dev_priv,
2358 dirty->fifo_reserve_size);
2359 if (!dirty->cmd) {
2360 DRM_ERROR("Couldn't reserve fifo space "
2361 "for dirty blits.\n");
Christian Engelmayerf3b8c0c2015-09-19 00:32:24 +02002362 return -ENOMEM;
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07002363 }
2364 memset(dirty->cmd, 0, dirty->fifo_reserve_size);
2365 }
2366 dirty->num_hits = 0;
2367 for (i = 0; i < num_clips; i++, clips_ptr += increment,
2368 vclips_ptr += increment) {
2369 s32 clip_left;
2370 s32 clip_top;
2371
2372 /*
2373 * Select clip array type. Note that integer type
2374 * in @clips is unsigned short, whereas in @vclips
2375 * it's 32-bit.
2376 */
2377 if (clips) {
2378 dirty->fb_x = (s32) clips_ptr->x1;
2379 dirty->fb_y = (s32) clips_ptr->y1;
2380 dirty->unit_x2 = (s32) clips_ptr->x2 + dest_x -
2381 crtc_x;
2382 dirty->unit_y2 = (s32) clips_ptr->y2 + dest_y -
2383 crtc_y;
2384 } else {
2385 dirty->fb_x = vclips_ptr->x;
2386 dirty->fb_y = vclips_ptr->y;
2387 dirty->unit_x2 = dirty->fb_x + vclips_ptr->w +
2388 dest_x - crtc_x;
2389 dirty->unit_y2 = dirty->fb_y + vclips_ptr->h +
2390 dest_y - crtc_y;
2391 }
2392
2393 dirty->unit_x1 = dirty->fb_x + dest_x - crtc_x;
2394 dirty->unit_y1 = dirty->fb_y + dest_y - crtc_y;
2395
2396 /* Skip this clip if it's outside the crtc region */
2397 if (dirty->unit_x1 >= crtc_width ||
2398 dirty->unit_y1 >= crtc_height ||
2399 dirty->unit_x2 <= 0 || dirty->unit_y2 <= 0)
2400 continue;
2401
2402 /* Clip right and bottom to crtc limits */
2403 dirty->unit_x2 = min_t(s32, dirty->unit_x2,
2404 crtc_width);
2405 dirty->unit_y2 = min_t(s32, dirty->unit_y2,
2406 crtc_height);
2407
2408 /* Clip left and top to crtc limits */
2409 clip_left = min_t(s32, dirty->unit_x1, 0);
2410 clip_top = min_t(s32, dirty->unit_y1, 0);
2411 dirty->unit_x1 -= clip_left;
2412 dirty->unit_y1 -= clip_top;
2413 dirty->fb_x -= clip_left;
2414 dirty->fb_y -= clip_top;
2415
2416 dirty->clip(dirty);
2417 }
2418
2419 dirty->fifo_commit(dirty);
2420 }
2421
2422 return 0;
2423}
2424
2425/**
2426 * vmw_kms_helper_buffer_prepare - Reserve and validate a buffer object before
2427 * command submission.
2428 *
2429 * @dev_priv. Pointer to a device private structure.
2430 * @buf: The buffer object
2431 * @interruptible: Whether to perform waits as interruptible.
2432 * @validate_as_mob: Whether the buffer should be validated as a MOB. If false,
2433 * The buffer will be validated as a GMR. Already pinned buffers will not be
2434 * validated.
2435 *
2436 * Returns 0 on success, negative error code on failure, -ERESTARTSYS if
2437 * interrupted by a signal.
2438 */
2439int vmw_kms_helper_buffer_prepare(struct vmw_private *dev_priv,
2440 struct vmw_dma_buffer *buf,
2441 bool interruptible,
2442 bool validate_as_mob)
2443{
2444 struct ttm_buffer_object *bo = &buf->base;
2445 int ret;
2446
Christian Königdfd5e502016-04-06 11:12:03 +02002447 ttm_bo_reserve(bo, false, false, NULL);
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07002448 ret = vmw_validate_single_buffer(dev_priv, bo, interruptible,
2449 validate_as_mob);
2450 if (ret)
2451 ttm_bo_unreserve(bo);
2452
2453 return ret;
2454}
2455
2456/**
2457 * vmw_kms_helper_buffer_revert - Undo the actions of
2458 * vmw_kms_helper_buffer_prepare.
2459 *
2460 * @res: Pointer to the buffer object.
2461 *
2462 * Helper to be used if an error forces the caller to undo the actions of
2463 * vmw_kms_helper_buffer_prepare.
2464 */
2465void vmw_kms_helper_buffer_revert(struct vmw_dma_buffer *buf)
2466{
2467 if (buf)
2468 ttm_bo_unreserve(&buf->base);
2469}
2470
2471/**
2472 * vmw_kms_helper_buffer_finish - Unreserve and fence a buffer object after
2473 * kms command submission.
2474 *
2475 * @dev_priv: Pointer to a device private structure.
2476 * @file_priv: Pointer to a struct drm_file representing the caller's
2477 * connection. Must be set to NULL if @user_fence_rep is NULL, and conversely
2478 * if non-NULL, @user_fence_rep must be non-NULL.
2479 * @buf: The buffer object.
2480 * @out_fence: Optional pointer to a fence pointer. If non-NULL, a
2481 * ref-counted fence pointer is returned here.
2482 * @user_fence_rep: Optional pointer to a user-space provided struct
2483 * drm_vmw_fence_rep. If provided, @file_priv must also be provided and the
2484 * function copies fence data to user-space in a fail-safe manner.
2485 */
2486void vmw_kms_helper_buffer_finish(struct vmw_private *dev_priv,
2487 struct drm_file *file_priv,
2488 struct vmw_dma_buffer *buf,
2489 struct vmw_fence_obj **out_fence,
2490 struct drm_vmw_fence_rep __user *
2491 user_fence_rep)
2492{
2493 struct vmw_fence_obj *fence;
2494 uint32_t handle;
2495 int ret;
2496
2497 ret = vmw_execbuf_fence_commands(file_priv, dev_priv, &fence,
2498 file_priv ? &handle : NULL);
2499 if (buf)
2500 vmw_fence_single_bo(&buf->base, fence);
2501 if (file_priv)
2502 vmw_execbuf_copy_fence_user(dev_priv, vmw_fpriv(file_priv),
2503 ret, user_fence_rep, fence,
Sinclair Yehc906965d2017-07-05 01:49:32 -07002504 handle, -1, NULL);
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07002505 if (out_fence)
2506 *out_fence = fence;
2507 else
2508 vmw_fence_obj_unreference(&fence);
2509
2510 vmw_kms_helper_buffer_revert(buf);
2511}
2512
2513
2514/**
2515 * vmw_kms_helper_resource_revert - Undo the actions of
2516 * vmw_kms_helper_resource_prepare.
2517 *
2518 * @res: Pointer to the resource. Typically a surface.
2519 *
2520 * Helper to be used if an error forces the caller to undo the actions of
2521 * vmw_kms_helper_resource_prepare.
2522 */
2523void vmw_kms_helper_resource_revert(struct vmw_resource *res)
2524{
2525 vmw_kms_helper_buffer_revert(res->backup);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002526 vmw_resource_unreserve(res, false, NULL, 0);
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07002527 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
2528}
2529
2530/**
2531 * vmw_kms_helper_resource_prepare - Reserve and validate a resource before
2532 * command submission.
2533 *
2534 * @res: Pointer to the resource. Typically a surface.
2535 * @interruptible: Whether to perform waits as interruptible.
2536 *
2537 * Reserves and validates also the backup buffer if a guest-backed resource.
2538 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
2539 * interrupted by a signal.
2540 */
2541int vmw_kms_helper_resource_prepare(struct vmw_resource *res,
2542 bool interruptible)
2543{
2544 int ret = 0;
2545
2546 if (interruptible)
2547 ret = mutex_lock_interruptible(&res->dev_priv->cmdbuf_mutex);
2548 else
2549 mutex_lock(&res->dev_priv->cmdbuf_mutex);
2550
2551 if (unlikely(ret != 0))
2552 return -ERESTARTSYS;
2553
2554 ret = vmw_resource_reserve(res, interruptible, false);
2555 if (ret)
2556 goto out_unlock;
2557
2558 if (res->backup) {
2559 ret = vmw_kms_helper_buffer_prepare(res->dev_priv, res->backup,
2560 interruptible,
2561 res->dev_priv->has_mob);
2562 if (ret)
2563 goto out_unreserve;
2564 }
2565 ret = vmw_resource_validate(res);
2566 if (ret)
2567 goto out_revert;
2568 return 0;
2569
2570out_revert:
2571 vmw_kms_helper_buffer_revert(res->backup);
2572out_unreserve:
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002573 vmw_resource_unreserve(res, false, NULL, 0);
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07002574out_unlock:
2575 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
2576 return ret;
2577}
2578
2579/**
2580 * vmw_kms_helper_resource_finish - Unreserve and fence a resource after
2581 * kms command submission.
2582 *
2583 * @res: Pointer to the resource. Typically a surface.
2584 * @out_fence: Optional pointer to a fence pointer. If non-NULL, a
2585 * ref-counted fence pointer is returned here.
2586 */
2587void vmw_kms_helper_resource_finish(struct vmw_resource *res,
2588 struct vmw_fence_obj **out_fence)
2589{
2590 if (res->backup || out_fence)
2591 vmw_kms_helper_buffer_finish(res->dev_priv, NULL, res->backup,
2592 out_fence, NULL);
2593
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002594 vmw_resource_unreserve(res, false, NULL, 0);
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07002595 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
2596}
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07002597
2598/**
2599 * vmw_kms_update_proxy - Helper function to update a proxy surface from
2600 * its backing MOB.
2601 *
2602 * @res: Pointer to the surface resource
2603 * @clips: Clip rects in framebuffer (surface) space.
2604 * @num_clips: Number of clips in @clips.
2605 * @increment: Integer with which to increment the clip counter when looping.
2606 * Used to skip a predetermined number of clip rects.
2607 *
2608 * This function makes sure the proxy surface is updated from its backing MOB
2609 * using the region given by @clips. The surface resource @res and its backing
2610 * MOB needs to be reserved and validated on call.
2611 */
2612int vmw_kms_update_proxy(struct vmw_resource *res,
2613 const struct drm_clip_rect *clips,
2614 unsigned num_clips,
2615 int increment)
2616{
2617 struct vmw_private *dev_priv = res->dev_priv;
2618 struct drm_vmw_size *size = &vmw_res_to_srf(res)->base_size;
2619 struct {
2620 SVGA3dCmdHeader header;
2621 SVGA3dCmdUpdateGBImage body;
2622 } *cmd;
2623 SVGA3dBox *box;
2624 size_t copy_size = 0;
2625 int i;
2626
2627 if (!clips)
2628 return 0;
2629
2630 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd) * num_clips);
2631 if (!cmd) {
2632 DRM_ERROR("Couldn't reserve fifo space for proxy surface "
2633 "update.\n");
2634 return -ENOMEM;
2635 }
2636
2637 for (i = 0; i < num_clips; ++i, clips += increment, ++cmd) {
2638 box = &cmd->body.box;
2639
2640 cmd->header.id = SVGA_3D_CMD_UPDATE_GB_IMAGE;
2641 cmd->header.size = sizeof(cmd->body);
2642 cmd->body.image.sid = res->id;
2643 cmd->body.image.face = 0;
2644 cmd->body.image.mipmap = 0;
2645
2646 if (clips->x1 > size->width || clips->x2 > size->width ||
2647 clips->y1 > size->height || clips->y2 > size->height) {
2648 DRM_ERROR("Invalid clips outsize of framebuffer.\n");
2649 return -EINVAL;
2650 }
2651
2652 box->x = clips->x1;
2653 box->y = clips->y1;
2654 box->z = 0;
2655 box->w = clips->x2 - clips->x1;
2656 box->h = clips->y2 - clips->y1;
2657 box->d = 1;
2658
2659 copy_size += sizeof(*cmd);
2660 }
2661
2662 vmw_fifo_commit(dev_priv, copy_size);
2663
2664 return 0;
2665}
Thomas Hellstroma2787242015-06-29 12:55:07 -07002666
2667int vmw_kms_fbdev_init_data(struct vmw_private *dev_priv,
2668 unsigned unit,
2669 u32 max_width,
2670 u32 max_height,
2671 struct drm_connector **p_con,
2672 struct drm_crtc **p_crtc,
2673 struct drm_display_mode **p_mode)
2674{
2675 struct drm_connector *con;
2676 struct vmw_display_unit *du;
2677 struct drm_display_mode *mode;
2678 int i = 0;
2679
2680 list_for_each_entry(con, &dev_priv->dev->mode_config.connector_list,
2681 head) {
2682 if (i == unit)
2683 break;
2684
2685 ++i;
2686 }
2687
2688 if (i != unit) {
2689 DRM_ERROR("Could not find initial display unit.\n");
2690 return -EINVAL;
2691 }
2692
2693 if (list_empty(&con->modes))
2694 (void) vmw_du_connector_fill_modes(con, max_width, max_height);
2695
2696 if (list_empty(&con->modes)) {
2697 DRM_ERROR("Could not find initial display mode.\n");
2698 return -EINVAL;
2699 }
2700
2701 du = vmw_connector_to_du(con);
2702 *p_con = con;
2703 *p_crtc = &du->crtc;
2704
2705 list_for_each_entry(mode, &con->modes, head) {
2706 if (mode->type & DRM_MODE_TYPE_PREFERRED)
2707 break;
2708 }
2709
2710 if (mode->type & DRM_MODE_TYPE_PREFERRED)
2711 *p_mode = mode;
2712 else {
2713 WARN_ONCE(true, "Could not find initial preferred mode.\n");
2714 *p_mode = list_first_entry(&con->modes,
2715 struct drm_display_mode,
2716 head);
2717 }
2718
2719 return 0;
2720}
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002721
2722/**
2723 * vmw_kms_del_active - unregister a crtc binding to the implicit framebuffer
2724 *
2725 * @dev_priv: Pointer to a device private struct.
2726 * @du: The display unit of the crtc.
2727 */
2728void vmw_kms_del_active(struct vmw_private *dev_priv,
2729 struct vmw_display_unit *du)
2730{
Thomas Hellstrom93cd1682016-05-03 11:24:35 +02002731 mutex_lock(&dev_priv->global_kms_state_mutex);
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002732 if (du->active_implicit) {
2733 if (--(dev_priv->num_implicit) == 0)
2734 dev_priv->implicit_fb = NULL;
2735 du->active_implicit = false;
2736 }
Thomas Hellstrom93cd1682016-05-03 11:24:35 +02002737 mutex_unlock(&dev_priv->global_kms_state_mutex);
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002738}
2739
2740/**
2741 * vmw_kms_add_active - register a crtc binding to an implicit framebuffer
2742 *
2743 * @vmw_priv: Pointer to a device private struct.
2744 * @du: The display unit of the crtc.
2745 * @vfb: The implicit framebuffer
2746 *
2747 * Registers a binding to an implicit framebuffer.
2748 */
2749void vmw_kms_add_active(struct vmw_private *dev_priv,
2750 struct vmw_display_unit *du,
2751 struct vmw_framebuffer *vfb)
2752{
Thomas Hellstrom93cd1682016-05-03 11:24:35 +02002753 mutex_lock(&dev_priv->global_kms_state_mutex);
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002754 WARN_ON_ONCE(!dev_priv->num_implicit && dev_priv->implicit_fb);
2755
2756 if (!du->active_implicit && du->is_implicit) {
2757 dev_priv->implicit_fb = vfb;
2758 du->active_implicit = true;
2759 dev_priv->num_implicit++;
2760 }
Thomas Hellstrom93cd1682016-05-03 11:24:35 +02002761 mutex_unlock(&dev_priv->global_kms_state_mutex);
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002762}
2763
2764/**
2765 * vmw_kms_screen_object_flippable - Check whether we can page-flip a crtc.
2766 *
2767 * @dev_priv: Pointer to device-private struct.
2768 * @crtc: The crtc we want to flip.
2769 *
2770 * Returns true or false depending whether it's OK to flip this crtc
2771 * based on the criterion that we must not have more than one implicit
2772 * frame-buffer at any one time.
2773 */
2774bool vmw_kms_crtc_flippable(struct vmw_private *dev_priv,
2775 struct drm_crtc *crtc)
2776{
2777 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
Thomas Hellstrom93cd1682016-05-03 11:24:35 +02002778 bool ret;
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002779
Thomas Hellstrom93cd1682016-05-03 11:24:35 +02002780 mutex_lock(&dev_priv->global_kms_state_mutex);
2781 ret = !du->is_implicit || dev_priv->num_implicit == 1;
2782 mutex_unlock(&dev_priv->global_kms_state_mutex);
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002783
Thomas Hellstrom93cd1682016-05-03 11:24:35 +02002784 return ret;
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002785}
2786
2787/**
2788 * vmw_kms_update_implicit_fb - Update the implicit fb.
2789 *
2790 * @dev_priv: Pointer to device-private struct.
2791 * @crtc: The crtc the new implicit frame-buffer is bound to.
2792 */
2793void vmw_kms_update_implicit_fb(struct vmw_private *dev_priv,
2794 struct drm_crtc *crtc)
2795{
2796 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
2797 struct vmw_framebuffer *vfb;
2798
Thomas Hellstrom93cd1682016-05-03 11:24:35 +02002799 mutex_lock(&dev_priv->global_kms_state_mutex);
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002800
2801 if (!du->is_implicit)
Thomas Hellstrom93cd1682016-05-03 11:24:35 +02002802 goto out_unlock;
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002803
2804 vfb = vmw_framebuffer_to_vfb(crtc->primary->fb);
2805 WARN_ON_ONCE(dev_priv->num_implicit != 1 &&
2806 dev_priv->implicit_fb != vfb);
2807
2808 dev_priv->implicit_fb = vfb;
Thomas Hellstrom93cd1682016-05-03 11:24:35 +02002809out_unlock:
2810 mutex_unlock(&dev_priv->global_kms_state_mutex);
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002811}
Thomas Hellstrom76404ac2016-02-12 09:55:45 +01002812
2813/**
2814 * vmw_kms_create_implicit_placement_proparty - Set up the implicit placement
2815 * property.
2816 *
2817 * @dev_priv: Pointer to a device private struct.
2818 * @immutable: Whether the property is immutable.
2819 *
2820 * Sets up the implicit placement property unless it's already set up.
2821 */
2822void
2823vmw_kms_create_implicit_placement_property(struct vmw_private *dev_priv,
2824 bool immutable)
2825{
2826 if (dev_priv->implicit_placement_property)
2827 return;
2828
2829 dev_priv->implicit_placement_property =
2830 drm_property_create_range(dev_priv->dev,
2831 immutable ?
2832 DRM_MODE_PROP_IMMUTABLE : 0,
2833 "implicit_placement", 0, 1);
2834
2835}
Sinclair Yeh904bb5e2017-03-23 14:29:22 -07002836
2837
2838/**
2839 * vmw_kms_set_config - Wrapper around drm_atomic_helper_set_config
2840 *
2841 * @set: The configuration to set.
2842 *
2843 * The vmwgfx Xorg driver doesn't assign the mode::type member, which
2844 * when drm_mode_set_crtcinfo is called as part of the configuration setting
2845 * causes it to return incorrect crtc dimensions causing severe problems in
2846 * the vmwgfx modesetting. So explicitly clear that member before calling
2847 * into drm_atomic_helper_set_config.
2848 */
Dave Airlie320d8c32017-04-03 16:30:24 +10002849int vmw_kms_set_config(struct drm_mode_set *set,
2850 struct drm_modeset_acquire_ctx *ctx)
Sinclair Yeh904bb5e2017-03-23 14:29:22 -07002851{
2852 if (set && set->mode)
2853 set->mode->type = 0;
2854
Dave Airlie320d8c32017-04-03 16:30:24 +10002855 return drm_atomic_helper_set_config(set, ctx);
Sinclair Yeh904bb5e2017-03-23 14:29:22 -07002856}