blob: ead61015cd79ceb0a2602615c72cddf7020da85d [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{
Ville Syrjälä58a275aa92017-11-01 20:29:18 +0200444 struct drm_crtc_state *crtc_state = NULL;
Sinclair Yeh060e2ad2017-03-23 14:18:32 -0700445 struct drm_framebuffer *new_fb = state->fb;
Ville Syrjälä58a275aa92017-11-01 20:29:18 +0200446 struct drm_rect clip = {};
Sinclair Yeh060e2ad2017-03-23 14:18:32 -0700447 int ret;
448
Ville Syrjälä58a275aa92017-11-01 20:29:18 +0200449 if (state->crtc)
450 crtc_state = drm_atomic_get_new_crtc_state(state->state, state->crtc);
Sinclair Yeh060e2ad2017-03-23 14:18:32 -0700451
Ville Syrjälä58a275aa92017-11-01 20:29:18 +0200452 if (crtc_state && crtc_state->enable) {
453 clip.x2 = crtc_state->adjusted_mode.hdisplay;
454 clip.y2 = crtc_state->adjusted_mode.vdisplay;
455 }
456
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +0200457 ret = drm_atomic_helper_check_plane_state(state, crtc_state, &clip,
458 DRM_PLANE_HELPER_NO_SCALING,
459 DRM_PLANE_HELPER_NO_SCALING,
460 false, true);
Sinclair Yeh060e2ad2017-03-23 14:18:32 -0700461
462 if (!ret && new_fb) {
463 struct drm_crtc *crtc = state->crtc;
464 struct vmw_connector_state *vcs;
465 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
466 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
467 struct vmw_framebuffer *vfb = vmw_framebuffer_to_vfb(new_fb);
468
469 vcs = vmw_connector_state_to_vcs(du->connector.state);
470
Sinclair Yeh060e2ad2017-03-23 14:18:32 -0700471 /* Only one active implicit framebuffer at a time. */
472 mutex_lock(&dev_priv->global_kms_state_mutex);
473 if (vcs->is_implicit && dev_priv->implicit_fb &&
474 !(dev_priv->num_implicit == 1 && du->active_implicit)
475 && dev_priv->implicit_fb != vfb) {
476 DRM_ERROR("Multiple implicit framebuffers "
477 "not supported.\n");
478 ret = -EINVAL;
479 }
480 mutex_unlock(&dev_priv->global_kms_state_mutex);
481 }
482
483
484 return ret;
485}
486
487
488/**
489 * vmw_du_cursor_plane_atomic_check - check if the new state is okay
490 *
491 * @plane: cursor plane
492 * @state: info on the new plane state
493 *
494 * This is a chance to fail if the new cursor state does not fit
495 * our requirements.
496 *
497 * Returns 0 on success
498 */
499int vmw_du_cursor_plane_atomic_check(struct drm_plane *plane,
500 struct drm_plane_state *new_state)
501{
502 int ret = 0;
503 struct vmw_surface *surface = NULL;
504 struct drm_framebuffer *fb = new_state->fb;
505
506
507 /* Turning off */
508 if (!fb)
509 return ret;
510
511 /* A lot of the code assumes this */
512 if (new_state->crtc_w != 64 || new_state->crtc_h != 64) {
513 DRM_ERROR("Invalid cursor dimensions (%d, %d)\n",
514 new_state->crtc_w, new_state->crtc_h);
515 ret = -EINVAL;
516 }
517
518 if (!vmw_framebuffer_to_vfb(fb)->dmabuf)
519 surface = vmw_framebuffer_to_vfbs(fb)->surface;
520
521 if (surface && !surface->snooper.image) {
522 DRM_ERROR("surface not suitable for cursor\n");
523 ret = -EINVAL;
524 }
525
526 return ret;
527}
528
529
Sinclair Yeh06ec4192017-03-23 13:14:54 -0700530int vmw_du_crtc_atomic_check(struct drm_crtc *crtc,
531 struct drm_crtc_state *new_state)
532{
533 struct vmw_display_unit *du = vmw_crtc_to_du(new_state->crtc);
534 int connector_mask = 1 << drm_connector_index(&du->connector);
535 bool has_primary = new_state->plane_mask &
536 BIT(drm_plane_index(crtc->primary));
537
538 /* We always want to have an active plane with an active CRTC */
539 if (has_primary != new_state->enable)
540 return -EINVAL;
541
542
543 if (new_state->connector_mask != connector_mask &&
544 new_state->connector_mask != 0) {
545 DRM_ERROR("Invalid connectors configuration\n");
546 return -EINVAL;
547 }
548
549 /*
550 * Our virtual device does not have a dot clock, so use the logical
551 * clock value as the dot clock.
552 */
553 if (new_state->mode.crtc_clock == 0)
554 new_state->adjusted_mode.crtc_clock = new_state->mode.clock;
555
556 return 0;
557}
558
559
560void vmw_du_crtc_atomic_begin(struct drm_crtc *crtc,
561 struct drm_crtc_state *old_crtc_state)
562{
563}
564
565
566void vmw_du_crtc_atomic_flush(struct drm_crtc *crtc,
567 struct drm_crtc_state *old_crtc_state)
568{
569 struct drm_pending_vblank_event *event = crtc->state->event;
570
571 if (event) {
572 crtc->state->event = NULL;
573
574 spin_lock_irq(&crtc->dev->event_lock);
575 if (drm_crtc_vblank_get(crtc) == 0)
576 drm_crtc_arm_vblank_event(crtc, event);
577 else
578 drm_crtc_send_vblank_event(crtc, event);
579 spin_unlock_irq(&crtc->dev->event_lock);
580 }
581
582}
583
584
Sinclair Yeh9c2542a2017-03-23 11:33:39 -0700585/**
586 * vmw_du_crtc_duplicate_state - duplicate crtc state
587 * @crtc: DRM crtc
588 *
589 * Allocates and returns a copy of the crtc state (both common and
590 * vmw-specific) for the specified crtc.
591 *
592 * Returns: The newly allocated crtc state, or NULL on failure.
593 */
594struct drm_crtc_state *
595vmw_du_crtc_duplicate_state(struct drm_crtc *crtc)
596{
597 struct drm_crtc_state *state;
598 struct vmw_crtc_state *vcs;
599
600 if (WARN_ON(!crtc->state))
601 return NULL;
602
603 vcs = kmemdup(crtc->state, sizeof(*vcs), GFP_KERNEL);
604
605 if (!vcs)
606 return NULL;
607
608 state = &vcs->base;
609
610 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
611
612 return state;
613}
614
615
616/**
617 * vmw_du_crtc_reset - creates a blank vmw crtc state
618 * @crtc: DRM crtc
619 *
620 * Resets the atomic state for @crtc by freeing the state pointer (which
621 * might be NULL, e.g. at driver load time) and allocating a new empty state
622 * object.
623 */
624void vmw_du_crtc_reset(struct drm_crtc *crtc)
625{
626 struct vmw_crtc_state *vcs;
627
628
629 if (crtc->state) {
630 __drm_atomic_helper_crtc_destroy_state(crtc->state);
631
632 kfree(vmw_crtc_state_to_vcs(crtc->state));
633 }
634
635 vcs = kzalloc(sizeof(*vcs), GFP_KERNEL);
636
637 if (!vcs) {
638 DRM_ERROR("Cannot allocate vmw_crtc_state\n");
639 return;
640 }
641
642 crtc->state = &vcs->base;
643 crtc->state->crtc = crtc;
644}
645
646
647/**
648 * vmw_du_crtc_destroy_state - destroy crtc state
649 * @crtc: DRM crtc
650 * @state: state object to destroy
651 *
652 * Destroys the crtc state (both common and vmw-specific) for the
653 * specified plane.
654 */
655void
656vmw_du_crtc_destroy_state(struct drm_crtc *crtc,
657 struct drm_crtc_state *state)
658{
659 drm_atomic_helper_crtc_destroy_state(crtc, state);
660}
661
662
Sinclair Yehcc5ec452017-03-23 11:36:05 -0700663/**
664 * vmw_du_plane_duplicate_state - duplicate plane state
665 * @plane: drm plane
666 *
667 * Allocates and returns a copy of the plane state (both common and
668 * vmw-specific) for the specified plane.
669 *
670 * Returns: The newly allocated plane state, or NULL on failure.
671 */
672struct drm_plane_state *
673vmw_du_plane_duplicate_state(struct drm_plane *plane)
674{
675 struct drm_plane_state *state;
676 struct vmw_plane_state *vps;
677
678 vps = kmemdup(plane->state, sizeof(*vps), GFP_KERNEL);
679
680 if (!vps)
681 return NULL;
682
683 vps->pinned = 0;
684
Sinclair Yeh810b3e162017-03-23 15:39:16 -0700685 /* Mapping is managed by prepare_fb/cleanup_fb */
Sinclair Yeh810b3e162017-03-23 15:39:16 -0700686 memset(&vps->host_map, 0, sizeof(vps->host_map));
687 vps->cpp = 0;
688
Sinclair Yehcc5ec452017-03-23 11:36:05 -0700689 /* Each ref counted resource needs to be acquired again */
690 if (vps->surf)
691 (void) vmw_surface_reference(vps->surf);
692
693 if (vps->dmabuf)
694 (void) vmw_dmabuf_reference(vps->dmabuf);
695
696 state = &vps->base;
697
698 __drm_atomic_helper_plane_duplicate_state(plane, state);
699
700 return state;
701}
702
703
704/**
705 * vmw_du_plane_reset - creates a blank vmw plane state
706 * @plane: drm plane
707 *
708 * Resets the atomic state for @plane by freeing the state pointer (which might
709 * be NULL, e.g. at driver load time) and allocating a new empty state object.
710 */
711void vmw_du_plane_reset(struct drm_plane *plane)
712{
713 struct vmw_plane_state *vps;
714
715
716 if (plane->state)
717 vmw_du_plane_destroy_state(plane, plane->state);
718
719 vps = kzalloc(sizeof(*vps), GFP_KERNEL);
720
721 if (!vps) {
722 DRM_ERROR("Cannot allocate vmw_plane_state\n");
723 return;
724 }
725
726 plane->state = &vps->base;
727 plane->state->plane = plane;
Robert Fossc2c446a2017-05-19 16:50:17 -0400728 plane->state->rotation = DRM_MODE_ROTATE_0;
Sinclair Yehcc5ec452017-03-23 11:36:05 -0700729}
730
731
732/**
733 * vmw_du_plane_destroy_state - destroy plane state
734 * @plane: DRM plane
735 * @state: state object to destroy
736 *
737 * Destroys the plane state (both common and vmw-specific) for the
738 * specified plane.
739 */
740void
741vmw_du_plane_destroy_state(struct drm_plane *plane,
742 struct drm_plane_state *state)
743{
744 struct vmw_plane_state *vps = vmw_plane_state_to_vps(state);
745
746
Sinclair Yeh810b3e162017-03-23 15:39:16 -0700747 /* Should have been freed by cleanup_fb */
Sinclair Yeh810b3e162017-03-23 15:39:16 -0700748 if (vps->host_map.virtual) {
749 DRM_ERROR("Host mapping not freed\n");
750 ttm_bo_kunmap(&vps->host_map);
751 }
752
Sinclair Yehcc5ec452017-03-23 11:36:05 -0700753 if (vps->surf)
754 vmw_surface_unreference(&vps->surf);
755
756 if (vps->dmabuf)
757 vmw_dmabuf_unreference(&vps->dmabuf);
758
759 drm_atomic_helper_plane_destroy_state(plane, state);
760}
761
762
Sinclair Yehd7721ca2017-03-23 11:48:44 -0700763/**
764 * vmw_du_connector_duplicate_state - duplicate connector state
765 * @connector: DRM connector
766 *
767 * Allocates and returns a copy of the connector state (both common and
768 * vmw-specific) for the specified connector.
769 *
770 * Returns: The newly allocated connector state, or NULL on failure.
771 */
772struct drm_connector_state *
773vmw_du_connector_duplicate_state(struct drm_connector *connector)
774{
775 struct drm_connector_state *state;
776 struct vmw_connector_state *vcs;
777
778 if (WARN_ON(!connector->state))
779 return NULL;
780
781 vcs = kmemdup(connector->state, sizeof(*vcs), GFP_KERNEL);
782
783 if (!vcs)
784 return NULL;
785
786 state = &vcs->base;
787
788 __drm_atomic_helper_connector_duplicate_state(connector, state);
789
790 return state;
791}
792
793
794/**
795 * vmw_du_connector_reset - creates a blank vmw connector state
796 * @connector: DRM connector
797 *
798 * Resets the atomic state for @connector by freeing the state pointer (which
799 * might be NULL, e.g. at driver load time) and allocating a new empty state
800 * object.
801 */
802void vmw_du_connector_reset(struct drm_connector *connector)
803{
804 struct vmw_connector_state *vcs;
805
806
807 if (connector->state) {
808 __drm_atomic_helper_connector_destroy_state(connector->state);
809
810 kfree(vmw_connector_state_to_vcs(connector->state));
811 }
812
813 vcs = kzalloc(sizeof(*vcs), GFP_KERNEL);
814
815 if (!vcs) {
816 DRM_ERROR("Cannot allocate vmw_connector_state\n");
817 return;
818 }
819
820 __drm_atomic_helper_connector_reset(connector, &vcs->base);
821}
822
823
824/**
825 * vmw_du_connector_destroy_state - destroy connector state
826 * @connector: DRM connector
827 * @state: state object to destroy
828 *
829 * Destroys the connector state (both common and vmw-specific) for the
830 * specified plane.
831 */
832void
833vmw_du_connector_destroy_state(struct drm_connector *connector,
834 struct drm_connector_state *state)
835{
836 drm_atomic_helper_connector_destroy_state(connector, state);
837}
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000838/*
839 * Generic framebuffer code
840 */
841
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000842/*
843 * Surface framebuffer code
844 */
845
Rashika Kheria847c5962014-01-06 22:18:10 +0530846static void vmw_framebuffer_surface_destroy(struct drm_framebuffer *framebuffer)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000847{
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200848 struct vmw_framebuffer_surface *vfbs =
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000849 vmw_framebuffer_to_vfbs(framebuffer);
850
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000851 drm_framebuffer_cleanup(framebuffer);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200852 vmw_surface_unreference(&vfbs->surface);
Thomas Hellstroma2787242015-06-29 12:55:07 -0700853 if (vfbs->base.user_obj)
854 ttm_base_object_unref(&vfbs->base.user_obj);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000855
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200856 kfree(vfbs);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000857}
858
Rashika Kheria847c5962014-01-06 22:18:10 +0530859static int vmw_framebuffer_surface_dirty(struct drm_framebuffer *framebuffer,
Thomas Hellstrom02b00162010-10-05 12:43:02 +0200860 struct drm_file *file_priv,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000861 unsigned flags, unsigned color,
862 struct drm_clip_rect *clips,
863 unsigned num_clips)
864{
865 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
866 struct vmw_framebuffer_surface *vfbs =
867 vmw_framebuffer_to_vfbs(framebuffer);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000868 struct drm_clip_rect norect;
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200869 int ret, inc = 1;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000870
Sinclair Yehc8261a92015-06-26 01:23:42 -0700871 /* Legacy Display Unit does not support 3D */
872 if (dev_priv->active_display_unit == vmw_du_legacy)
Jakob Bornecrantz01e81412011-10-04 20:13:24 +0200873 return -EINVAL;
874
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200875 drm_modeset_lock_all(dev_priv->dev);
876
Thomas Hellstrom294adf72014-02-27 12:34:51 +0100877 ret = ttm_read_lock(&dev_priv->reservation_sem, true);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200878 if (unlikely(ret != 0)) {
879 drm_modeset_unlock_all(dev_priv->dev);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200880 return ret;
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200881 }
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200882
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000883 if (!num_clips) {
884 num_clips = 1;
885 clips = &norect;
886 norect.x1 = norect.y1 = 0;
887 norect.x2 = framebuffer->width;
888 norect.y2 = framebuffer->height;
889 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
890 num_clips /= 2;
891 inc = 2; /* skip source rects */
892 }
893
Sinclair Yehc8261a92015-06-26 01:23:42 -0700894 if (dev_priv->active_display_unit == vmw_du_screen_object)
Thomas Hellstrom10b1e0c2015-06-26 02:14:27 -0700895 ret = vmw_kms_sou_do_surface_dirty(dev_priv, &vfbs->base,
896 clips, NULL, NULL, 0, 0,
897 num_clips, inc, NULL);
Sinclair Yeh35c05122015-06-26 01:42:06 -0700898 else
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700899 ret = vmw_kms_stdu_surface_dirty(dev_priv, &vfbs->base,
900 clips, NULL, NULL, 0, 0,
901 num_clips, inc, NULL);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000902
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700903 vmw_fifo_flush(dev_priv, false);
Thomas Hellstrom294adf72014-02-27 12:34:51 +0100904 ttm_read_unlock(&dev_priv->reservation_sem);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200905
906 drm_modeset_unlock_all(dev_priv->dev);
907
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000908 return 0;
909}
910
Thomas Hellstrom10b1e0c2015-06-26 02:14:27 -0700911/**
912 * vmw_kms_readback - Perform a readback from the screen system to
913 * a dma-buffer backed framebuffer.
914 *
915 * @dev_priv: Pointer to the device private structure.
916 * @file_priv: Pointer to a struct drm_file identifying the caller.
917 * Must be set to NULL if @user_fence_rep is NULL.
918 * @vfb: Pointer to the dma-buffer backed framebuffer.
919 * @user_fence_rep: User-space provided structure for fence information.
920 * Must be set to non-NULL if @file_priv is non-NULL.
921 * @vclips: Array of clip rects.
922 * @num_clips: Number of clip rects in @vclips.
923 *
924 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
925 * interrupted.
926 */
927int vmw_kms_readback(struct vmw_private *dev_priv,
928 struct drm_file *file_priv,
929 struct vmw_framebuffer *vfb,
930 struct drm_vmw_fence_rep __user *user_fence_rep,
931 struct drm_vmw_rect *vclips,
932 uint32_t num_clips)
933{
934 switch (dev_priv->active_display_unit) {
935 case vmw_du_screen_object:
936 return vmw_kms_sou_readback(dev_priv, file_priv, vfb,
937 user_fence_rep, vclips, num_clips);
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700938 case vmw_du_screen_target:
939 return vmw_kms_stdu_dma(dev_priv, file_priv, vfb,
940 user_fence_rep, NULL, vclips, num_clips,
941 1, false, true);
Thomas Hellstrom10b1e0c2015-06-26 02:14:27 -0700942 default:
943 WARN_ONCE(true,
944 "Readback called with invalid display system.\n");
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700945}
Thomas Hellstrom10b1e0c2015-06-26 02:14:27 -0700946
947 return -ENOSYS;
948}
949
950
Ville Syrjäläd7955fc2015-12-15 12:21:15 +0100951static const struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs = {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000952 .destroy = vmw_framebuffer_surface_destroy,
953 .dirty = vmw_framebuffer_surface_dirty,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000954};
955
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200956static int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv,
957 struct vmw_surface *surface,
958 struct vmw_framebuffer **out,
Daniel Vetterdabdcdc2016-12-02 08:07:40 +0100959 const struct drm_mode_fb_cmd2
Sinclair Yehf89c6c32015-06-26 01:54:28 -0700960 *mode_cmd,
961 bool is_dmabuf_proxy)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000962
963{
964 struct drm_device *dev = dev_priv->dev;
965 struct vmw_framebuffer_surface *vfbs;
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200966 enum SVGA3dSurfaceFormat format;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000967 int ret;
Daniel Vetterdabdcdc2016-12-02 08:07:40 +0100968 struct drm_format_name_buf format_name;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000969
Sinclair Yehc8261a92015-06-26 01:23:42 -0700970 /* 3D is only supported on HWv8 and newer hosts */
971 if (dev_priv->active_display_unit == vmw_du_legacy)
Jakob Bornecrantz01e81412011-10-04 20:13:24 +0200972 return -ENOSYS;
973
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200974 /*
975 * Sanity checks.
976 */
977
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100978 /* Surface must be marked as a scanout. */
979 if (unlikely(!surface->scanout))
980 return -EINVAL;
981
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200982 if (unlikely(surface->mip_levels[0] != 1 ||
983 surface->num_sizes != 1 ||
Thomas Hellstromb360a3c2014-01-15 08:51:36 +0100984 surface->base_size.width < mode_cmd->width ||
985 surface->base_size.height < mode_cmd->height ||
986 surface->base_size.depth != 1)) {
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200987 DRM_ERROR("Incompatible surface dimensions "
988 "for requested mode.\n");
989 return -EINVAL;
990 }
991
Daniel Vetterdabdcdc2016-12-02 08:07:40 +0100992 switch (mode_cmd->pixel_format) {
993 case DRM_FORMAT_ARGB8888:
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200994 format = SVGA3D_A8R8G8B8;
995 break;
Daniel Vetterdabdcdc2016-12-02 08:07:40 +0100996 case DRM_FORMAT_XRGB8888:
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200997 format = SVGA3D_X8R8G8B8;
998 break;
Daniel Vetterdabdcdc2016-12-02 08:07:40 +0100999 case DRM_FORMAT_RGB565:
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001000 format = SVGA3D_R5G6B5;
1001 break;
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001002 case DRM_FORMAT_XRGB1555:
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001003 format = SVGA3D_A1R5G5B5;
1004 break;
1005 default:
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001006 DRM_ERROR("Invalid pixel format: %s\n",
1007 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001008 return -EINVAL;
1009 }
1010
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001011 /*
1012 * For DX, surface format validation is done when surface->scanout
1013 * is set.
1014 */
1015 if (!dev_priv->has_dx && format != surface->format) {
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001016 DRM_ERROR("Invalid surface format for requested mode.\n");
1017 return -EINVAL;
1018 }
1019
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001020 vfbs = kzalloc(sizeof(*vfbs), GFP_KERNEL);
1021 if (!vfbs) {
1022 ret = -ENOMEM;
1023 goto out_err1;
1024 }
1025
Ville Syrjäläa3f913c2016-12-14 22:48:59 +02001026 drm_helper_mode_fill_fb_struct(dev, &vfbs->base.base, mode_cmd);
Sinclair Yeh05c95012015-08-11 22:53:39 -07001027 vfbs->surface = vmw_surface_reference(surface);
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001028 vfbs->base.user_handle = mode_cmd->handles[0];
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001029 vfbs->is_dmabuf_proxy = is_dmabuf_proxy;
Thomas Hellstrom3a939a52010-10-05 12:43:03 +02001030
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001031 *out = &vfbs->base;
1032
Daniel Vetter80f0b5a2012-12-13 23:39:01 +01001033 ret = drm_framebuffer_init(dev, &vfbs->base.base,
1034 &vmw_framebuffer_surface_funcs);
1035 if (ret)
Sinclair Yeh05c95012015-08-11 22:53:39 -07001036 goto out_err2;
Daniel Vetter80f0b5a2012-12-13 23:39:01 +01001037
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001038 return 0;
1039
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001040out_err2:
Sinclair Yeh05c95012015-08-11 22:53:39 -07001041 vmw_surface_unreference(&surface);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001042 kfree(vfbs);
1043out_err1:
1044 return ret;
1045}
1046
1047/*
1048 * Dmabuf framebuffer code
1049 */
1050
Rashika Kheria847c5962014-01-06 22:18:10 +05301051static void vmw_framebuffer_dmabuf_destroy(struct drm_framebuffer *framebuffer)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001052{
1053 struct vmw_framebuffer_dmabuf *vfbd =
1054 vmw_framebuffer_to_vfbd(framebuffer);
1055
1056 drm_framebuffer_cleanup(framebuffer);
1057 vmw_dmabuf_unreference(&vfbd->buffer);
Thomas Hellstroma2787242015-06-29 12:55:07 -07001058 if (vfbd->base.user_obj)
1059 ttm_base_object_unref(&vfbd->base.user_obj);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001060
1061 kfree(vfbd);
1062}
1063
Rashika Kheria847c5962014-01-06 22:18:10 +05301064static int vmw_framebuffer_dmabuf_dirty(struct drm_framebuffer *framebuffer,
Thomas Hellstrom02b00162010-10-05 12:43:02 +02001065 struct drm_file *file_priv,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001066 unsigned flags, unsigned color,
1067 struct drm_clip_rect *clips,
1068 unsigned num_clips)
1069{
1070 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +02001071 struct vmw_framebuffer_dmabuf *vfbd =
1072 vmw_framebuffer_to_vfbd(framebuffer);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001073 struct drm_clip_rect norect;
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +02001074 int ret, increment = 1;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001075
Ville Syrjälä73e9efd2013-12-04 14:13:58 +02001076 drm_modeset_lock_all(dev_priv->dev);
1077
Thomas Hellstrom294adf72014-02-27 12:34:51 +01001078 ret = ttm_read_lock(&dev_priv->reservation_sem, true);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +02001079 if (unlikely(ret != 0)) {
1080 drm_modeset_unlock_all(dev_priv->dev);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +02001081 return ret;
Ville Syrjälä73e9efd2013-12-04 14:13:58 +02001082 }
Thomas Hellstrom3a939a52010-10-05 12:43:03 +02001083
Thomas Hellstromdf1c93b2010-01-13 22:28:36 +01001084 if (!num_clips) {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001085 num_clips = 1;
1086 clips = &norect;
1087 norect.x1 = norect.y1 = 0;
1088 norect.x2 = framebuffer->width;
1089 norect.y2 = framebuffer->height;
1090 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
1091 num_clips /= 2;
1092 increment = 2;
1093 }
1094
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001095 switch (dev_priv->active_display_unit) {
1096 case vmw_du_screen_target:
1097 ret = vmw_kms_stdu_dma(dev_priv, NULL, &vfbd->base, NULL,
1098 clips, NULL, num_clips, increment,
1099 true, true);
1100 break;
1101 case vmw_du_screen_object:
Thomas Hellstrom10b1e0c2015-06-26 02:14:27 -07001102 ret = vmw_kms_sou_do_dmabuf_dirty(dev_priv, &vfbd->base,
Thomas Hellstrom897b8182016-02-12 08:32:08 +01001103 clips, NULL, num_clips,
1104 increment, true, NULL);
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001105 break;
Thomas Hellstrom352b20d2015-06-29 12:57:37 -07001106 case vmw_du_legacy:
1107 ret = vmw_kms_ldu_do_dmabuf_dirty(dev_priv, &vfbd->base, 0, 0,
1108 clips, num_clips, increment);
1109 break;
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001110 default:
Thomas Hellstrom352b20d2015-06-29 12:57:37 -07001111 ret = -EINVAL;
1112 WARN_ONCE(true, "Dirty called with invalid display system.\n");
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001113 break;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +02001114 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001115
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07001116 vmw_fifo_flush(dev_priv, false);
Thomas Hellstrom294adf72014-02-27 12:34:51 +01001117 ttm_read_unlock(&dev_priv->reservation_sem);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +02001118
1119 drm_modeset_unlock_all(dev_priv->dev);
1120
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +02001121 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001122}
1123
Ville Syrjäläd7955fc2015-12-15 12:21:15 +01001124static const struct drm_framebuffer_funcs vmw_framebuffer_dmabuf_funcs = {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001125 .destroy = vmw_framebuffer_dmabuf_destroy,
1126 .dirty = vmw_framebuffer_dmabuf_dirty,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001127};
1128
Jakob Bornecrantz497a3ff2011-10-04 20:13:14 +02001129/**
Jakob Bornecrantz497a3ff2011-10-04 20:13:14 +02001130 * Pin the dmabuffer to the start of vram.
1131 */
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001132static int vmw_framebuffer_pin(struct vmw_framebuffer *vfb)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001133{
1134 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001135 struct vmw_dma_buffer *buf;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001136 int ret;
1137
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001138 buf = vfb->dmabuf ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
1139 vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001140
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001141 if (!buf)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001142 return 0;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001143
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001144 switch (dev_priv->active_display_unit) {
1145 case vmw_du_legacy:
1146 vmw_overlay_pause_all(dev_priv);
1147 ret = vmw_dmabuf_pin_in_start_of_vram(dev_priv, buf, false);
1148 vmw_overlay_resume_all(dev_priv);
1149 break;
1150 case vmw_du_screen_object:
1151 case vmw_du_screen_target:
1152 if (vfb->dmabuf)
1153 return vmw_dmabuf_pin_in_vram_or_gmr(dev_priv, buf,
1154 false);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001155
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001156 return vmw_dmabuf_pin_in_placement(dev_priv, buf,
1157 &vmw_mob_placement, false);
1158 default:
1159 return -EINVAL;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001160 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001161
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001162 return ret;
1163}
1164
1165static int vmw_framebuffer_unpin(struct vmw_framebuffer *vfb)
1166{
1167 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
1168 struct vmw_dma_buffer *buf;
1169
1170 buf = vfb->dmabuf ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
1171 vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
1172
1173 if (WARN_ON(!buf))
1174 return 0;
1175
1176 return vmw_dmabuf_unpin(dev_priv, buf, false);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001177}
1178
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001179/**
1180 * vmw_create_dmabuf_proxy - create a proxy surface for the DMA buf
1181 *
1182 * @dev: DRM device
1183 * @mode_cmd: parameters for the new surface
1184 * @dmabuf_mob: MOB backing the DMA buf
1185 * @srf_out: newly created surface
1186 *
1187 * When the content FB is a DMA buf, we create a surface as a proxy to the
1188 * same buffer. This way we can do a surface copy rather than a surface DMA.
1189 * This is a more efficient approach
1190 *
1191 * RETURNS:
1192 * 0 on success, error code otherwise
1193 */
1194static int vmw_create_dmabuf_proxy(struct drm_device *dev,
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001195 const struct drm_mode_fb_cmd2 *mode_cmd,
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001196 struct vmw_dma_buffer *dmabuf_mob,
1197 struct vmw_surface **srf_out)
1198{
1199 uint32_t format;
Thomas Hellstrom8cd9f252017-01-19 10:53:02 -08001200 struct drm_vmw_size content_base_size = {0};
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001201 struct vmw_resource *res;
Thomas Hellstroma50e2bf2016-01-08 20:29:40 +01001202 unsigned int bytes_pp;
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001203 struct drm_format_name_buf format_name;
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001204 int ret;
1205
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001206 switch (mode_cmd->pixel_format) {
1207 case DRM_FORMAT_ARGB8888:
1208 case DRM_FORMAT_XRGB8888:
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001209 format = SVGA3D_X8R8G8B8;
Thomas Hellstroma50e2bf2016-01-08 20:29:40 +01001210 bytes_pp = 4;
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001211 break;
1212
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001213 case DRM_FORMAT_RGB565:
1214 case DRM_FORMAT_XRGB1555:
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001215 format = SVGA3D_R5G6B5;
Thomas Hellstroma50e2bf2016-01-08 20:29:40 +01001216 bytes_pp = 2;
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001217 break;
1218
1219 case 8:
1220 format = SVGA3D_P8;
Thomas Hellstroma50e2bf2016-01-08 20:29:40 +01001221 bytes_pp = 1;
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001222 break;
1223
1224 default:
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001225 DRM_ERROR("Invalid framebuffer format %s\n",
1226 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001227 return -EINVAL;
1228 }
1229
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001230 content_base_size.width = mode_cmd->pitches[0] / bytes_pp;
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001231 content_base_size.height = mode_cmd->height;
1232 content_base_size.depth = 1;
1233
1234 ret = vmw_surface_gb_priv_define(dev,
1235 0, /* kernel visible only */
1236 0, /* flags */
1237 format,
1238 true, /* can be a scanout buffer */
1239 1, /* num of mip levels */
1240 0,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001241 0,
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001242 content_base_size,
1243 srf_out);
1244 if (ret) {
1245 DRM_ERROR("Failed to allocate proxy content buffer\n");
1246 return ret;
1247 }
1248
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001249 res = &(*srf_out)->res;
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001250
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001251 /* Reserve and switch the backing mob. */
1252 mutex_lock(&res->dev_priv->cmdbuf_mutex);
1253 (void) vmw_resource_reserve(res, false, true);
1254 vmw_dmabuf_unreference(&res->backup);
1255 res->backup = vmw_dmabuf_reference(dmabuf_mob);
1256 res->backup_offset = 0;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001257 vmw_resource_unreserve(res, false, NULL, 0);
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001258 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001259
1260 return 0;
1261}
1262
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001263
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001264
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001265static int vmw_kms_new_framebuffer_dmabuf(struct vmw_private *dev_priv,
1266 struct vmw_dma_buffer *dmabuf,
1267 struct vmw_framebuffer **out,
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001268 const struct drm_mode_fb_cmd2
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001269 *mode_cmd)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001270
1271{
1272 struct drm_device *dev = dev_priv->dev;
1273 struct vmw_framebuffer_dmabuf *vfbd;
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001274 unsigned int requested_size;
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001275 struct drm_format_name_buf format_name;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001276 int ret;
1277
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001278 requested_size = mode_cmd->height * mode_cmd->pitches[0];
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001279 if (unlikely(requested_size > dmabuf->base.num_pages * PAGE_SIZE)) {
1280 DRM_ERROR("Screen buffer object size is too small "
1281 "for requested mode.\n");
1282 return -EINVAL;
1283 }
1284
Jakob Bornecrantzc337ada2011-10-04 20:13:34 +02001285 /* Limited framebuffer color depth support for screen objects */
Sinclair Yehc8261a92015-06-26 01:23:42 -07001286 if (dev_priv->active_display_unit == vmw_du_screen_object) {
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001287 switch (mode_cmd->pixel_format) {
1288 case DRM_FORMAT_XRGB8888:
1289 case DRM_FORMAT_ARGB8888:
1290 break;
1291 case DRM_FORMAT_XRGB1555:
1292 case DRM_FORMAT_RGB565:
1293 break;
Jakob Bornecrantzc337ada2011-10-04 20:13:34 +02001294 default:
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001295 DRM_ERROR("Invalid pixel format: %s\n",
1296 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Jakob Bornecrantzc337ada2011-10-04 20:13:34 +02001297 return -EINVAL;
1298 }
1299 }
1300
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001301 vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL);
1302 if (!vfbd) {
1303 ret = -ENOMEM;
1304 goto out_err1;
1305 }
1306
Ville Syrjäläa3f913c2016-12-14 22:48:59 +02001307 drm_helper_mode_fill_fb_struct(dev, &vfbd->base.base, mode_cmd);
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001308 vfbd->base.dmabuf = true;
Sinclair Yeh05c95012015-08-11 22:53:39 -07001309 vfbd->buffer = vmw_dmabuf_reference(dmabuf);
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001310 vfbd->base.user_handle = mode_cmd->handles[0];
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001311 *out = &vfbd->base;
1312
Daniel Vetter80f0b5a2012-12-13 23:39:01 +01001313 ret = drm_framebuffer_init(dev, &vfbd->base.base,
1314 &vmw_framebuffer_dmabuf_funcs);
1315 if (ret)
Sinclair Yeh05c95012015-08-11 22:53:39 -07001316 goto out_err2;
Daniel Vetter80f0b5a2012-12-13 23:39:01 +01001317
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001318 return 0;
1319
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001320out_err2:
Sinclair Yeh05c95012015-08-11 22:53:39 -07001321 vmw_dmabuf_unreference(&dmabuf);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001322 kfree(vfbd);
1323out_err1:
1324 return ret;
1325}
1326
Sinclair Yeh810b3e162017-03-23 15:39:16 -07001327
1328/**
1329 * vmw_kms_srf_ok - check if a surface can be created
1330 *
1331 * @width: requested width
1332 * @height: requested height
1333 *
1334 * Surfaces need to be less than texture size
1335 */
1336static bool
1337vmw_kms_srf_ok(struct vmw_private *dev_priv, uint32_t width, uint32_t height)
1338{
1339 if (width > dev_priv->texture_max_width ||
1340 height > dev_priv->texture_max_height)
1341 return false;
1342
1343 return true;
1344}
1345
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001346/**
1347 * vmw_kms_new_framebuffer - Create a new framebuffer.
1348 *
1349 * @dev_priv: Pointer to device private struct.
1350 * @dmabuf: Pointer to dma buffer to wrap the kms framebuffer around.
1351 * Either @dmabuf or @surface must be NULL.
1352 * @surface: Pointer to a surface to wrap the kms framebuffer around.
1353 * Either @dmabuf or @surface must be NULL.
1354 * @only_2d: No presents will occur to this dma buffer based framebuffer. This
1355 * Helps the code to do some important optimizations.
1356 * @mode_cmd: Frame-buffer metadata.
1357 */
1358struct vmw_framebuffer *
1359vmw_kms_new_framebuffer(struct vmw_private *dev_priv,
1360 struct vmw_dma_buffer *dmabuf,
1361 struct vmw_surface *surface,
1362 bool only_2d,
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001363 const struct drm_mode_fb_cmd2 *mode_cmd)
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001364{
Sinclair Yeh05c95012015-08-11 22:53:39 -07001365 struct vmw_framebuffer *vfb = NULL;
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001366 bool is_dmabuf_proxy = false;
1367 int ret;
1368
1369 /*
1370 * We cannot use the SurfaceDMA command in an non-accelerated VM,
1371 * therefore, wrap the DMA buf in a surface so we can use the
1372 * SurfaceCopy command.
1373 */
Sinclair Yeh810b3e162017-03-23 15:39:16 -07001374 if (vmw_kms_srf_ok(dev_priv, mode_cmd->width, mode_cmd->height) &&
1375 dmabuf && only_2d &&
Sinclair Yehbbd5fef2017-06-02 07:44:53 +02001376 mode_cmd->width > 64 && /* Don't create a proxy for cursor */
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001377 dev_priv->active_display_unit == vmw_du_screen_target) {
1378 ret = vmw_create_dmabuf_proxy(dev_priv->dev, mode_cmd,
1379 dmabuf, &surface);
1380 if (ret)
1381 return ERR_PTR(ret);
1382
1383 is_dmabuf_proxy = true;
1384 }
1385
1386 /* Create the new framebuffer depending one what we have */
Sinclair Yeh05c95012015-08-11 22:53:39 -07001387 if (surface) {
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001388 ret = vmw_kms_new_framebuffer_surface(dev_priv, surface, &vfb,
1389 mode_cmd,
1390 is_dmabuf_proxy);
Sinclair Yeh05c95012015-08-11 22:53:39 -07001391
1392 /*
1393 * vmw_create_dmabuf_proxy() adds a reference that is no longer
1394 * needed
1395 */
1396 if (is_dmabuf_proxy)
1397 vmw_surface_unreference(&surface);
1398 } else if (dmabuf) {
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001399 ret = vmw_kms_new_framebuffer_dmabuf(dev_priv, dmabuf, &vfb,
1400 mode_cmd);
Sinclair Yeh05c95012015-08-11 22:53:39 -07001401 } else {
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001402 BUG();
Sinclair Yeh05c95012015-08-11 22:53:39 -07001403 }
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001404
1405 if (ret)
1406 return ERR_PTR(ret);
1407
1408 vfb->pin = vmw_framebuffer_pin;
1409 vfb->unpin = vmw_framebuffer_unpin;
1410
1411 return vfb;
1412}
1413
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001414/*
1415 * Generic Kernel modesetting functions
1416 */
1417
1418static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev,
1419 struct drm_file *file_priv,
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001420 const struct drm_mode_fb_cmd2 *mode_cmd)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001421{
1422 struct vmw_private *dev_priv = vmw_priv(dev);
1423 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
1424 struct vmw_framebuffer *vfb = NULL;
1425 struct vmw_surface *surface = NULL;
1426 struct vmw_dma_buffer *bo = NULL;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001427 struct ttm_base_object *user_obj;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001428 int ret;
1429
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001430 /**
1431 * This code should be conditioned on Screen Objects not being used.
1432 * If screen objects are used, we can allocate a GMR to hold the
1433 * requested framebuffer.
1434 */
1435
Xi Wang8a783892011-12-21 05:18:33 -05001436 if (!vmw_kms_validate_mode_vram(dev_priv,
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001437 mode_cmd->pitches[0],
1438 mode_cmd->height)) {
Sinclair Yehc8261a92015-06-26 01:23:42 -07001439 DRM_ERROR("Requested mode exceed bounding box limit.\n");
Jakob Bornecrantzd9826402011-11-03 21:03:04 +01001440 return ERR_PTR(-ENOMEM);
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001441 }
1442
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001443 /*
1444 * Take a reference on the user object of the resource
1445 * backing the kms fb. This ensures that user-space handle
1446 * lookups on that resource will always work as long as
1447 * it's registered with a kms framebuffer. This is important,
1448 * since vmw_execbuf_process identifies resources in the
1449 * command stream using user-space handles.
1450 */
1451
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001452 user_obj = ttm_base_object_lookup(tfile, mode_cmd->handles[0]);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001453 if (unlikely(user_obj == NULL)) {
1454 DRM_ERROR("Could not locate requested kms frame buffer.\n");
1455 return ERR_PTR(-ENOENT);
1456 }
1457
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001458 /**
1459 * End conditioned code.
1460 */
1461
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +01001462 /* returns either a dmabuf or surface */
1463 ret = vmw_user_lookup_handle(dev_priv, tfile,
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001464 mode_cmd->handles[0],
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +01001465 &surface, &bo);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001466 if (ret)
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +01001467 goto err_out;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001468
Sinclair Yeh810b3e162017-03-23 15:39:16 -07001469
1470 if (!bo &&
1471 !vmw_kms_srf_ok(dev_priv, mode_cmd->width, mode_cmd->height)) {
1472 DRM_ERROR("Surface size cannot exceed %dx%d",
1473 dev_priv->texture_max_width,
1474 dev_priv->texture_max_height);
1475 goto err_out;
1476 }
1477
1478
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001479 vfb = vmw_kms_new_framebuffer(dev_priv, bo, surface,
1480 !(dev_priv->capabilities & SVGA_CAP_3D),
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001481 mode_cmd);
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001482 if (IS_ERR(vfb)) {
1483 ret = PTR_ERR(vfb);
1484 goto err_out;
1485 }
Jakob Bornecrantz5ffdb652010-01-30 03:38:08 +00001486
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +01001487err_out:
1488 /* vmw_user_lookup_handle takes one ref so does new_fb */
1489 if (bo)
1490 vmw_dmabuf_unreference(&bo);
1491 if (surface)
1492 vmw_surface_unreference(&surface);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001493
1494 if (ret) {
1495 DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001496 ttm_base_object_unref(&user_obj);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01001497 return ERR_PTR(ret);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001498 } else
1499 vfb->user_obj = user_obj;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001500
1501 return &vfb->base;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001502}
1503
Sinclair Yehc46a3062017-03-23 14:24:53 -07001504
1505
1506/**
1507 * vmw_kms_atomic_check_modeset- validate state object for modeset changes
1508 *
1509 * @dev: DRM device
1510 * @state: the driver state object
1511 *
1512 * This is a simple wrapper around drm_atomic_helper_check_modeset() for
1513 * us to assign a value to mode->crtc_clock so that
1514 * drm_calc_timestamping_constants() won't throw an error message
1515 *
1516 * RETURNS
1517 * Zero for success or -errno
1518 */
Maarten Lankhorstbdc362f2017-07-12 10:13:33 +02001519static int
Sinclair Yehc46a3062017-03-23 14:24:53 -07001520vmw_kms_atomic_check_modeset(struct drm_device *dev,
1521 struct drm_atomic_state *state)
1522{
1523 struct drm_crtc_state *crtc_state;
1524 struct drm_crtc *crtc;
1525 struct vmw_private *dev_priv = vmw_priv(dev);
1526 int i;
1527
Maarten Lankhorstbdc362f2017-07-12 10:13:33 +02001528 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
Sinclair Yehc46a3062017-03-23 14:24:53 -07001529 unsigned long requested_bb_mem = 0;
1530
1531 if (dev_priv->active_display_unit == vmw_du_screen_target) {
1532 if (crtc->primary->fb) {
1533 int cpp = crtc->primary->fb->pitches[0] /
1534 crtc->primary->fb->width;
1535
1536 requested_bb_mem += crtc->mode.hdisplay * cpp *
1537 crtc->mode.vdisplay;
1538 }
1539
1540 if (requested_bb_mem > dev_priv->prim_bb_mem)
1541 return -EINVAL;
1542 }
1543 }
1544
1545 return drm_atomic_helper_check(dev, state);
1546}
1547
1548
Sinclair Yeh021aba72017-08-29 18:55:09 +02001549/**
1550 * vmw_kms_atomic_commit - Perform an atomic state commit
1551 *
1552 * @dev: DRM device
1553 * @state: the driver state object
1554 * @nonblock: Whether nonblocking behaviour is requested
1555 *
1556 * This is a simple wrapper around drm_atomic_helper_commit() for
1557 * us to clear the nonblocking value.
1558 *
1559 * Nonblocking commits currently cause synchronization issues
1560 * for vmwgfx.
1561 *
1562 * RETURNS
1563 * Zero for success or negative error code on failure.
1564 */
1565int vmw_kms_atomic_commit(struct drm_device *dev,
1566 struct drm_atomic_state *state,
1567 bool nonblock)
1568{
1569 return drm_atomic_helper_commit(dev, state, false);
1570}
1571
1572
Laurent Pincharte6ecefa2012-05-17 13:27:23 +02001573static const struct drm_mode_config_funcs vmw_kms_funcs = {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001574 .fb_create = vmw_kms_fb_create,
Sinclair Yehc46a3062017-03-23 14:24:53 -07001575 .atomic_check = vmw_kms_atomic_check_modeset,
Sinclair Yeh021aba72017-08-29 18:55:09 +02001576 .atomic_commit = vmw_kms_atomic_commit,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001577};
1578
Thomas Hellstromb9eb1a62015-04-02 02:39:45 -07001579static int vmw_kms_generic_present(struct vmw_private *dev_priv,
1580 struct drm_file *file_priv,
1581 struct vmw_framebuffer *vfb,
1582 struct vmw_surface *surface,
1583 uint32_t sid,
1584 int32_t destX, int32_t destY,
1585 struct drm_vmw_rect *clips,
1586 uint32_t num_clips)
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001587{
Thomas Hellstrom10b1e0c2015-06-26 02:14:27 -07001588 return vmw_kms_sou_do_surface_dirty(dev_priv, vfb, NULL, clips,
1589 &surface->res, destX, destY,
1590 num_clips, 1, NULL);
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001591}
1592
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001593
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001594int vmw_kms_present(struct vmw_private *dev_priv,
1595 struct drm_file *file_priv,
1596 struct vmw_framebuffer *vfb,
1597 struct vmw_surface *surface,
1598 uint32_t sid,
1599 int32_t destX, int32_t destY,
1600 struct drm_vmw_rect *clips,
1601 uint32_t num_clips)
1602{
Sinclair Yeh35c05122015-06-26 01:42:06 -07001603 int ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001604
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001605 switch (dev_priv->active_display_unit) {
1606 case vmw_du_screen_target:
1607 ret = vmw_kms_stdu_surface_dirty(dev_priv, vfb, NULL, clips,
1608 &surface->res, destX, destY,
1609 num_clips, 1, NULL);
1610 break;
1611 case vmw_du_screen_object:
1612 ret = vmw_kms_generic_present(dev_priv, file_priv, vfb, surface,
1613 sid, destX, destY, clips,
1614 num_clips);
1615 break;
1616 default:
1617 WARN_ONCE(true,
1618 "Present called with invalid display system.\n");
1619 ret = -ENOSYS;
1620 break;
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001621 }
Sinclair Yeh35c05122015-06-26 01:42:06 -07001622 if (ret)
1623 return ret;
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001624
Sinclair Yeh35c05122015-06-26 01:42:06 -07001625 vmw_fifo_flush(dev_priv, false);
Michel Dänzer0bef23f2011-08-31 07:42:50 +00001626
Sinclair Yeh35c05122015-06-26 01:42:06 -07001627 return 0;
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001628}
1629
Thomas Hellstrom578e6092016-02-12 09:45:42 +01001630static void
1631vmw_kms_create_hotplug_mode_update_property(struct vmw_private *dev_priv)
1632{
1633 if (dev_priv->hotplug_mode_update_property)
1634 return;
1635
1636 dev_priv->hotplug_mode_update_property =
1637 drm_property_create_range(dev_priv->dev,
1638 DRM_MODE_PROP_IMMUTABLE,
1639 "hotplug_mode_update", 0, 1);
1640
1641 if (!dev_priv->hotplug_mode_update_property)
1642 return;
1643
1644}
1645
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001646int vmw_kms_init(struct vmw_private *dev_priv)
1647{
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001648 struct drm_device *dev = dev_priv->dev;
1649 int ret;
1650
1651 drm_mode_config_init(dev);
1652 dev->mode_config.funcs = &vmw_kms_funcs;
1653 dev->mode_config.min_width = 1;
1654 dev->mode_config.min_height = 1;
Sinclair Yeh65ade7d2015-07-16 10:49:13 -07001655 dev->mode_config.max_width = dev_priv->texture_max_width;
1656 dev->mode_config.max_height = dev_priv->texture_max_height;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001657
Thomas Hellstrom578e6092016-02-12 09:45:42 +01001658 drm_mode_create_suggested_offset_properties(dev);
1659 vmw_kms_create_hotplug_mode_update_property(dev_priv);
1660
Sinclair Yeh35c05122015-06-26 01:42:06 -07001661 ret = vmw_kms_stdu_init_display(dev_priv);
1662 if (ret) {
1663 ret = vmw_kms_sou_init_display(dev_priv);
1664 if (ret) /* Fallback */
1665 ret = vmw_kms_ldu_init_display(dev_priv);
1666 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001667
Sinclair Yehc8261a92015-06-26 01:23:42 -07001668 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001669}
1670
1671int vmw_kms_close(struct vmw_private *dev_priv)
1672{
Daniel Vetter5f58e972017-06-26 18:19:48 +02001673 int ret = 0;
Sinclair Yehc8261a92015-06-26 01:23:42 -07001674
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001675 /*
1676 * Docs says we should take the lock before calling this function
1677 * but since it destroys encoders and our destructor calls
1678 * drm_encoder_cleanup which takes the lock we deadlock.
1679 */
1680 drm_mode_config_cleanup(dev_priv->dev);
Daniel Vetter5f58e972017-06-26 18:19:48 +02001681 if (dev_priv->active_display_unit == vmw_du_legacy)
Sinclair Yehc8261a92015-06-26 01:23:42 -07001682 ret = vmw_kms_ldu_close_display(dev_priv);
1683
1684 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001685}
1686
1687int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
1688 struct drm_file *file_priv)
1689{
1690 struct drm_vmw_cursor_bypass_arg *arg = data;
1691 struct vmw_display_unit *du;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001692 struct drm_crtc *crtc;
1693 int ret = 0;
1694
1695
1696 mutex_lock(&dev->mode_config.mutex);
1697 if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
1698
1699 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1700 du = vmw_crtc_to_du(crtc);
1701 du->hotspot_x = arg->xhot;
1702 du->hotspot_y = arg->yhot;
1703 }
1704
1705 mutex_unlock(&dev->mode_config.mutex);
1706 return 0;
1707 }
1708
Keith Packard418da172017-03-14 23:25:07 -07001709 crtc = drm_crtc_find(dev, file_priv, arg->crtc_id);
Rob Clarka4cd5d62014-07-17 23:30:02 -04001710 if (!crtc) {
Ville Syrjälä4ae87ff2013-10-17 13:35:05 +03001711 ret = -ENOENT;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001712 goto out;
1713 }
1714
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001715 du = vmw_crtc_to_du(crtc);
1716
1717 du->hotspot_x = arg->xhot;
1718 du->hotspot_y = arg->yhot;
1719
1720out:
1721 mutex_unlock(&dev->mode_config.mutex);
1722
1723 return ret;
1724}
1725
1726int vmw_kms_write_svga(struct vmw_private *vmw_priv,
1727 unsigned width, unsigned height, unsigned pitch,
1728 unsigned bpp, unsigned depth)
1729{
1730 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1731 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch);
1732 else if (vmw_fifo_have_pitchlock(vmw_priv))
Thomas Hellstromb76ff5e2015-10-28 10:44:04 +01001733 vmw_mmio_write(pitch, vmw_priv->mmio_virt +
1734 SVGA_FIFO_PITCHLOCK);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001735 vmw_write(vmw_priv, SVGA_REG_WIDTH, width);
1736 vmw_write(vmw_priv, SVGA_REG_HEIGHT, height);
1737 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp);
1738
1739 if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) {
1740 DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n",
1741 depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH));
1742 return -EINVAL;
1743 }
1744
1745 return 0;
1746}
1747
1748int vmw_kms_save_vga(struct vmw_private *vmw_priv)
1749{
1750 struct vmw_vga_topology_state *save;
1751 uint32_t i;
1752
1753 vmw_priv->vga_width = vmw_read(vmw_priv, SVGA_REG_WIDTH);
1754 vmw_priv->vga_height = vmw_read(vmw_priv, SVGA_REG_HEIGHT);
1755 vmw_priv->vga_bpp = vmw_read(vmw_priv, SVGA_REG_BITS_PER_PIXEL);
1756 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1757 vmw_priv->vga_pitchlock =
1758 vmw_read(vmw_priv, SVGA_REG_PITCHLOCK);
1759 else if (vmw_fifo_have_pitchlock(vmw_priv))
Thomas Hellstromb76ff5e2015-10-28 10:44:04 +01001760 vmw_priv->vga_pitchlock = vmw_mmio_read(vmw_priv->mmio_virt +
1761 SVGA_FIFO_PITCHLOCK);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001762
1763 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1764 return 0;
1765
1766 vmw_priv->num_displays = vmw_read(vmw_priv,
1767 SVGA_REG_NUM_GUEST_DISPLAYS);
1768
1769 if (vmw_priv->num_displays == 0)
1770 vmw_priv->num_displays = 1;
1771
1772 for (i = 0; i < vmw_priv->num_displays; ++i) {
1773 save = &vmw_priv->vga_save[i];
1774 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1775 save->primary = vmw_read(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY);
1776 save->pos_x = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_X);
1777 save->pos_y = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y);
1778 save->width = vmw_read(vmw_priv, SVGA_REG_DISPLAY_WIDTH);
1779 save->height = vmw_read(vmw_priv, SVGA_REG_DISPLAY_HEIGHT);
1780 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
1781 if (i == 0 && vmw_priv->num_displays == 1 &&
1782 save->width == 0 && save->height == 0) {
1783
1784 /*
1785 * It should be fairly safe to assume that these
1786 * values are uninitialized.
1787 */
1788
1789 save->width = vmw_priv->vga_width - save->pos_x;
1790 save->height = vmw_priv->vga_height - save->pos_y;
1791 }
1792 }
1793
1794 return 0;
1795}
1796
1797int vmw_kms_restore_vga(struct vmw_private *vmw_priv)
1798{
1799 struct vmw_vga_topology_state *save;
1800 uint32_t i;
1801
1802 vmw_write(vmw_priv, SVGA_REG_WIDTH, vmw_priv->vga_width);
1803 vmw_write(vmw_priv, SVGA_REG_HEIGHT, vmw_priv->vga_height);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001804 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, vmw_priv->vga_bpp);
1805 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1806 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK,
1807 vmw_priv->vga_pitchlock);
1808 else if (vmw_fifo_have_pitchlock(vmw_priv))
Thomas Hellstromb76ff5e2015-10-28 10:44:04 +01001809 vmw_mmio_write(vmw_priv->vga_pitchlock,
1810 vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001811
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001812 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1813 return 0;
1814
1815 for (i = 0; i < vmw_priv->num_displays; ++i) {
1816 save = &vmw_priv->vga_save[i];
1817 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1818 vmw_write(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY, save->primary);
1819 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_X, save->pos_x);
1820 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y, save->pos_y);
1821 vmw_write(vmw_priv, SVGA_REG_DISPLAY_WIDTH, save->width);
1822 vmw_write(vmw_priv, SVGA_REG_DISPLAY_HEIGHT, save->height);
1823 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
1824 }
1825
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001826 return 0;
1827}
Jakob Bornecrantzd8bd19d2010-06-01 11:54:20 +02001828
Thomas Hellstrome133e732010-10-05 12:43:04 +02001829bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
1830 uint32_t pitch,
1831 uint32_t height)
1832{
Sinclair Yeh35c05122015-06-26 01:42:06 -07001833 return ((u64) pitch * (u64) height) < (u64)
1834 ((dev_priv->active_display_unit == vmw_du_screen_target) ?
1835 dev_priv->prim_bb_mem : dev_priv->vram_size);
Thomas Hellstrome133e732010-10-05 12:43:04 +02001836}
1837
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001838
1839/**
1840 * Function called by DRM code called with vbl_lock held.
1841 */
Thierry Reding88e72712015-09-24 18:35:31 +02001842u32 vmw_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
Thomas Hellstrom7a1c2f62010-10-01 10:21:49 +02001843{
1844 return 0;
1845}
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001846
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001847/**
1848 * Function called by DRM code called with vbl_lock held.
1849 */
Thierry Reding88e72712015-09-24 18:35:31 +02001850int vmw_enable_vblank(struct drm_device *dev, unsigned int pipe)
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001851{
Woody Suwalski2b0bc682018-01-17 09:07:47 +01001852 return -EINVAL;
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001853}
1854
1855/**
1856 * Function called by DRM code called with vbl_lock held.
1857 */
Thierry Reding88e72712015-09-24 18:35:31 +02001858void vmw_disable_vblank(struct drm_device *dev, unsigned int pipe)
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001859{
1860}
1861
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001862
1863/*
1864 * Small shared kms functions.
1865 */
1866
Rashika Kheria847c5962014-01-06 22:18:10 +05301867static int vmw_du_update_layout(struct vmw_private *dev_priv, unsigned num,
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001868 struct drm_vmw_rect *rects)
1869{
1870 struct drm_device *dev = dev_priv->dev;
1871 struct vmw_display_unit *du;
1872 struct drm_connector *con;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001873
1874 mutex_lock(&dev->mode_config.mutex);
1875
1876#if 0
Thomas Hellstrom6ea77d12011-10-04 20:13:36 +02001877 {
1878 unsigned int i;
1879
1880 DRM_INFO("%s: new layout ", __func__);
1881 for (i = 0; i < num; i++)
1882 DRM_INFO("(%i, %i %ux%u) ", rects[i].x, rects[i].y,
1883 rects[i].w, rects[i].h);
1884 DRM_INFO("\n");
1885 }
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001886#endif
1887
1888 list_for_each_entry(con, &dev->mode_config.connector_list, head) {
1889 du = vmw_connector_to_du(con);
1890 if (num > du->unit) {
1891 du->pref_width = rects[du->unit].w;
1892 du->pref_height = rects[du->unit].h;
1893 du->pref_active = true;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001894 du->gui_x = rects[du->unit].x;
1895 du->gui_y = rects[du->unit].y;
Thomas Hellstrom578e6092016-02-12 09:45:42 +01001896 drm_object_property_set_value
1897 (&con->base, dev->mode_config.suggested_x_property,
1898 du->gui_x);
1899 drm_object_property_set_value
1900 (&con->base, dev->mode_config.suggested_y_property,
1901 du->gui_y);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001902 } else {
1903 du->pref_width = 800;
1904 du->pref_height = 600;
1905 du->pref_active = false;
Thomas Hellstrom578e6092016-02-12 09:45:42 +01001906 drm_object_property_set_value
1907 (&con->base, dev->mode_config.suggested_x_property,
1908 0);
1909 drm_object_property_set_value
1910 (&con->base, dev->mode_config.suggested_y_property,
1911 0);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001912 }
1913 con->status = vmw_du_connector_detect(con, true);
1914 }
1915
1916 mutex_unlock(&dev->mode_config.mutex);
Thomas Hellstrom578e6092016-02-12 09:45:42 +01001917 drm_sysfs_hotplug_event(dev);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001918
1919 return 0;
1920}
1921
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02001922int vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
1923 u16 *r, u16 *g, u16 *b,
Daniel Vetter6d124ff2017-04-03 10:33:01 +02001924 uint32_t size,
1925 struct drm_modeset_acquire_ctx *ctx)
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001926{
1927 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
1928 int i;
1929
1930 for (i = 0; i < size; i++) {
1931 DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i,
1932 r[i], g[i], b[i]);
1933 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 0, r[i] >> 8);
1934 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 1, g[i] >> 8);
1935 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 2, b[i] >> 8);
1936 }
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02001937
1938 return 0;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001939}
1940
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02001941int vmw_du_connector_dpms(struct drm_connector *connector, int mode)
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001942{
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02001943 return 0;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001944}
1945
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001946enum drm_connector_status
1947vmw_du_connector_detect(struct drm_connector *connector, bool force)
1948{
1949 uint32_t num_displays;
1950 struct drm_device *dev = connector->dev;
1951 struct vmw_private *dev_priv = vmw_priv(dev);
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001952 struct vmw_display_unit *du = vmw_connector_to_du(connector);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001953
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001954 num_displays = vmw_read(dev_priv, SVGA_REG_NUM_DISPLAYS);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001955
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001956 return ((vmw_connector_to_du(connector)->unit < num_displays &&
1957 du->pref_active) ?
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001958 connector_status_connected : connector_status_disconnected);
1959}
1960
1961static struct drm_display_mode vmw_kms_connector_builtin[] = {
1962 /* 640x480@60Hz */
1963 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
1964 752, 800, 0, 480, 489, 492, 525, 0,
1965 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1966 /* 800x600@60Hz */
1967 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
1968 968, 1056, 0, 600, 601, 605, 628, 0,
1969 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1970 /* 1024x768@60Hz */
1971 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
1972 1184, 1344, 0, 768, 771, 777, 806, 0,
1973 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1974 /* 1152x864@75Hz */
1975 { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
1976 1344, 1600, 0, 864, 865, 868, 900, 0,
1977 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1978 /* 1280x768@60Hz */
1979 { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344,
1980 1472, 1664, 0, 768, 771, 778, 798, 0,
1981 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1982 /* 1280x800@60Hz */
1983 { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352,
1984 1480, 1680, 0, 800, 803, 809, 831, 0,
1985 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
1986 /* 1280x960@60Hz */
1987 { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376,
1988 1488, 1800, 0, 960, 961, 964, 1000, 0,
1989 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1990 /* 1280x1024@60Hz */
1991 { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328,
1992 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
1993 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1994 /* 1360x768@60Hz */
1995 { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424,
1996 1536, 1792, 0, 768, 771, 777, 795, 0,
1997 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1998 /* 1440x1050@60Hz */
1999 { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488,
2000 1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
2001 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2002 /* 1440x900@60Hz */
2003 { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520,
2004 1672, 1904, 0, 900, 903, 909, 934, 0,
2005 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2006 /* 1600x1200@60Hz */
2007 { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664,
2008 1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
2009 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
2010 /* 1680x1050@60Hz */
2011 { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784,
2012 1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
2013 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2014 /* 1792x1344@60Hz */
2015 { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920,
2016 2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
2017 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2018 /* 1853x1392@60Hz */
2019 { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952,
2020 2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
2021 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2022 /* 1920x1200@60Hz */
2023 { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056,
2024 2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
2025 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2026 /* 1920x1440@60Hz */
2027 { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048,
2028 2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
2029 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2030 /* 2560x1600@60Hz */
2031 { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752,
2032 3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
2033 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2034 /* Terminate */
2035 { DRM_MODE("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) },
2036};
2037
Thomas Hellstrom1543b4d2011-11-02 09:43:10 +01002038/**
2039 * vmw_guess_mode_timing - Provide fake timings for a
2040 * 60Hz vrefresh mode.
2041 *
2042 * @mode - Pointer to a struct drm_display_mode with hdisplay and vdisplay
2043 * members filled in.
2044 */
Thomas Hellstroma2787242015-06-29 12:55:07 -07002045void vmw_guess_mode_timing(struct drm_display_mode *mode)
Thomas Hellstrom1543b4d2011-11-02 09:43:10 +01002046{
2047 mode->hsync_start = mode->hdisplay + 50;
2048 mode->hsync_end = mode->hsync_start + 50;
2049 mode->htotal = mode->hsync_end + 50;
2050
2051 mode->vsync_start = mode->vdisplay + 50;
2052 mode->vsync_end = mode->vsync_start + 50;
2053 mode->vtotal = mode->vsync_end + 50;
2054
2055 mode->clock = (u32)mode->htotal * (u32)mode->vtotal / 100 * 6;
2056 mode->vrefresh = drm_mode_vrefresh(mode);
2057}
2058
2059
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002060int vmw_du_connector_fill_modes(struct drm_connector *connector,
2061 uint32_t max_width, uint32_t max_height)
2062{
2063 struct vmw_display_unit *du = vmw_connector_to_du(connector);
2064 struct drm_device *dev = connector->dev;
2065 struct vmw_private *dev_priv = vmw_priv(dev);
2066 struct drm_display_mode *mode = NULL;
2067 struct drm_display_mode *bmode;
2068 struct drm_display_mode prefmode = { DRM_MODE("preferred",
2069 DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
2070 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2071 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
2072 };
2073 int i;
Sinclair Yeh7c20d212016-06-29 11:29:47 -07002074 u32 assumed_bpp = 4;
Sinclair Yeh9a723842014-10-31 09:58:06 +01002075
Sinclair Yeh04319d82016-06-29 12:15:48 -07002076 if (dev_priv->assume_16bpp)
2077 assumed_bpp = 2;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002078
Sinclair Yeh35c05122015-06-26 01:42:06 -07002079 if (dev_priv->active_display_unit == vmw_du_screen_target) {
2080 max_width = min(max_width, dev_priv->stdu_max_width);
Sinclair Yeh28c95422017-03-27 11:12:27 -07002081 max_width = min(max_width, dev_priv->texture_max_width);
2082
Sinclair Yeh35c05122015-06-26 01:42:06 -07002083 max_height = min(max_height, dev_priv->stdu_max_height);
Sinclair Yeh28c95422017-03-27 11:12:27 -07002084 max_height = min(max_height, dev_priv->texture_max_height);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002085 }
2086
2087 /* Add preferred mode */
Sinclair Yehc8261a92015-06-26 01:23:42 -07002088 mode = drm_mode_duplicate(dev, &prefmode);
2089 if (!mode)
2090 return 0;
2091 mode->hdisplay = du->pref_width;
2092 mode->vdisplay = du->pref_height;
2093 vmw_guess_mode_timing(mode);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002094
Sinclair Yehc8261a92015-06-26 01:23:42 -07002095 if (vmw_kms_validate_mode_vram(dev_priv,
2096 mode->hdisplay * assumed_bpp,
2097 mode->vdisplay)) {
2098 drm_mode_probed_add(connector, mode);
2099 } else {
2100 drm_mode_destroy(dev, mode);
2101 mode = NULL;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002102 }
2103
Sinclair Yehc8261a92015-06-26 01:23:42 -07002104 if (du->pref_mode) {
2105 list_del_init(&du->pref_mode->head);
2106 drm_mode_destroy(dev, du->pref_mode);
2107 }
2108
2109 /* mode might be null here, this is intended */
2110 du->pref_mode = mode;
2111
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002112 for (i = 0; vmw_kms_connector_builtin[i].type != 0; i++) {
2113 bmode = &vmw_kms_connector_builtin[i];
2114 if (bmode->hdisplay > max_width ||
2115 bmode->vdisplay > max_height)
2116 continue;
2117
Sinclair Yeh9a723842014-10-31 09:58:06 +01002118 if (!vmw_kms_validate_mode_vram(dev_priv,
2119 bmode->hdisplay * assumed_bpp,
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002120 bmode->vdisplay))
2121 continue;
2122
2123 mode = drm_mode_duplicate(dev, bmode);
2124 if (!mode)
2125 return 0;
2126 mode->vrefresh = drm_mode_vrefresh(mode);
2127
2128 drm_mode_probed_add(connector, mode);
2129 }
2130
Ville Syrjälä6af3e652015-12-03 23:14:14 +02002131 drm_mode_connector_list_update(connector);
Thomas Hellstromf6b05002015-06-29 12:59:58 -07002132 /* Move the prefered mode first, help apps pick the right mode. */
2133 drm_mode_sort(&connector->modes);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002134
2135 return 1;
2136}
2137
2138int vmw_du_connector_set_property(struct drm_connector *connector,
2139 struct drm_property *property,
2140 uint64_t val)
2141{
Thomas Hellstrom76404ac2016-02-12 09:55:45 +01002142 struct vmw_display_unit *du = vmw_connector_to_du(connector);
2143 struct vmw_private *dev_priv = vmw_priv(connector->dev);
2144
2145 if (property == dev_priv->implicit_placement_property)
2146 du->is_implicit = val;
2147
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002148 return 0;
2149}
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002150
2151
Sinclair Yeh9c2542a2017-03-23 11:33:39 -07002152
Sinclair Yehd7721ca2017-03-23 11:48:44 -07002153/**
2154 * vmw_du_connector_atomic_set_property - Atomic version of get property
2155 *
2156 * @crtc - crtc the property is associated with
2157 *
2158 * Returns:
2159 * Zero on success, negative errno on failure.
2160 */
2161int
2162vmw_du_connector_atomic_set_property(struct drm_connector *connector,
2163 struct drm_connector_state *state,
2164 struct drm_property *property,
2165 uint64_t val)
2166{
2167 struct vmw_private *dev_priv = vmw_priv(connector->dev);
2168 struct vmw_connector_state *vcs = vmw_connector_state_to_vcs(state);
2169 struct vmw_display_unit *du = vmw_connector_to_du(connector);
2170
2171
2172 if (property == dev_priv->implicit_placement_property) {
2173 vcs->is_implicit = val;
2174
2175 /*
2176 * We should really be doing a drm_atomic_commit() to
2177 * commit the new state, but since this doesn't cause
2178 * an immedate state change, this is probably ok
2179 */
2180 du->is_implicit = vcs->is_implicit;
2181 } else {
2182 return -EINVAL;
2183 }
2184
2185 return 0;
2186}
2187
2188
2189/**
2190 * vmw_du_connector_atomic_get_property - Atomic version of get property
2191 *
2192 * @connector - connector the property is associated with
2193 *
2194 * Returns:
2195 * Zero on success, negative errno on failure.
2196 */
2197int
2198vmw_du_connector_atomic_get_property(struct drm_connector *connector,
2199 const struct drm_connector_state *state,
2200 struct drm_property *property,
2201 uint64_t *val)
2202{
2203 struct vmw_private *dev_priv = vmw_priv(connector->dev);
2204 struct vmw_connector_state *vcs = vmw_connector_state_to_vcs(state);
2205
2206 if (property == dev_priv->implicit_placement_property)
2207 *val = vcs->is_implicit;
2208 else {
2209 DRM_ERROR("Invalid Property %s\n", property->name);
2210 return -EINVAL;
2211 }
2212
2213 return 0;
2214}
2215
2216
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002217int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data,
2218 struct drm_file *file_priv)
2219{
2220 struct vmw_private *dev_priv = vmw_priv(dev);
2221 struct drm_vmw_update_layout_arg *arg =
2222 (struct drm_vmw_update_layout_arg *)data;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002223 void __user *user_rects;
2224 struct drm_vmw_rect *rects;
2225 unsigned rects_size;
2226 int ret;
2227 int i;
Sinclair Yeh65ade7d2015-07-16 10:49:13 -07002228 u64 total_pixels = 0;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002229 struct drm_mode_config *mode_config = &dev->mode_config;
Sinclair Yehc8261a92015-06-26 01:23:42 -07002230 struct drm_vmw_rect bounding_box = {0};
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002231
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002232 if (!arg->num_outputs) {
2233 struct drm_vmw_rect def_rect = {0, 0, 800, 600};
2234 vmw_du_update_layout(dev_priv, 1, &def_rect);
Thomas Hellstrom5151adb2015-03-09 01:56:21 -07002235 return 0;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002236 }
2237
2238 rects_size = arg->num_outputs * sizeof(struct drm_vmw_rect);
Xi Wangbab9efc2011-11-28 12:25:43 +01002239 rects = kcalloc(arg->num_outputs, sizeof(struct drm_vmw_rect),
2240 GFP_KERNEL);
Thomas Hellstrom5151adb2015-03-09 01:56:21 -07002241 if (unlikely(!rects))
2242 return -ENOMEM;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002243
2244 user_rects = (void __user *)(unsigned long)arg->rects;
2245 ret = copy_from_user(rects, user_rects, rects_size);
2246 if (unlikely(ret != 0)) {
2247 DRM_ERROR("Failed to get rects.\n");
2248 ret = -EFAULT;
2249 goto out_free;
2250 }
2251
2252 for (i = 0; i < arg->num_outputs; ++i) {
Xi Wangbab9efc2011-11-28 12:25:43 +01002253 if (rects[i].x < 0 ||
2254 rects[i].y < 0 ||
2255 rects[i].x + rects[i].w > mode_config->max_width ||
2256 rects[i].y + rects[i].h > mode_config->max_height) {
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002257 DRM_ERROR("Invalid GUI layout.\n");
2258 ret = -EINVAL;
2259 goto out_free;
2260 }
Sinclair Yehc8261a92015-06-26 01:23:42 -07002261
2262 /*
2263 * bounding_box.w and bunding_box.h are used as
2264 * lower-right coordinates
2265 */
2266 if (rects[i].x + rects[i].w > bounding_box.w)
2267 bounding_box.w = rects[i].x + rects[i].w;
2268
2269 if (rects[i].y + rects[i].h > bounding_box.h)
2270 bounding_box.h = rects[i].y + rects[i].h;
Sinclair Yeh65ade7d2015-07-16 10:49:13 -07002271
2272 total_pixels += (u64) rects[i].w * (u64) rects[i].h;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002273 }
2274
Sinclair Yeh65ade7d2015-07-16 10:49:13 -07002275 if (dev_priv->active_display_unit == vmw_du_screen_target) {
2276 /*
2277 * For Screen Targets, the limits for a toplogy are:
2278 * 1. Bounding box (assuming 32bpp) must be < prim_bb_mem
2279 * 2. Total pixels (assuming 32bpp) must be < prim_bb_mem
2280 */
Thomas Hellstrom0f580382017-01-19 11:01:04 -08002281 u64 bb_mem = (u64) bounding_box.w * bounding_box.h * 4;
Sinclair Yeh65ade7d2015-07-16 10:49:13 -07002282 u64 pixel_mem = total_pixels * 4;
2283
2284 if (bb_mem > dev_priv->prim_bb_mem) {
2285 DRM_ERROR("Topology is beyond supported limits.\n");
Sinclair Yeh35c05122015-06-26 01:42:06 -07002286 ret = -EINVAL;
2287 goto out_free;
2288 }
2289
Sinclair Yeh65ade7d2015-07-16 10:49:13 -07002290 if (pixel_mem > dev_priv->prim_bb_mem) {
2291 DRM_ERROR("Combined output size too large\n");
2292 ret = -EINVAL;
2293 goto out_free;
2294 }
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002295 }
2296
2297 vmw_du_update_layout(dev_priv, arg->num_outputs, rects);
2298
2299out_free:
2300 kfree(rects);
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002301 return ret;
2302}
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07002303
2304/**
2305 * vmw_kms_helper_dirty - Helper to build commands and perform actions based
2306 * on a set of cliprects and a set of display units.
2307 *
2308 * @dev_priv: Pointer to a device private structure.
2309 * @framebuffer: Pointer to the framebuffer on which to perform the actions.
2310 * @clips: A set of struct drm_clip_rect. Either this os @vclips must be NULL.
2311 * Cliprects are given in framebuffer coordinates.
2312 * @vclips: A set of struct drm_vmw_rect cliprects. Either this or @clips must
2313 * be NULL. Cliprects are given in source coordinates.
2314 * @dest_x: X coordinate offset for the crtc / destination clip rects.
2315 * @dest_y: Y coordinate offset for the crtc / destination clip rects.
2316 * @num_clips: Number of cliprects in the @clips or @vclips array.
2317 * @increment: Integer with which to increment the clip counter when looping.
2318 * Used to skip a predetermined number of clip rects.
2319 * @dirty: Closure structure. See the description of struct vmw_kms_dirty.
2320 */
2321int vmw_kms_helper_dirty(struct vmw_private *dev_priv,
2322 struct vmw_framebuffer *framebuffer,
2323 const struct drm_clip_rect *clips,
2324 const struct drm_vmw_rect *vclips,
2325 s32 dest_x, s32 dest_y,
2326 int num_clips,
2327 int increment,
2328 struct vmw_kms_dirty *dirty)
2329{
2330 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
2331 struct drm_crtc *crtc;
2332 u32 num_units = 0;
2333 u32 i, k;
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07002334
2335 dirty->dev_priv = dev_priv;
2336
2337 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
2338 if (crtc->primary->fb != &framebuffer->base)
2339 continue;
2340 units[num_units++] = vmw_crtc_to_du(crtc);
2341 }
2342
2343 for (k = 0; k < num_units; k++) {
2344 struct vmw_display_unit *unit = units[k];
2345 s32 crtc_x = unit->crtc.x;
2346 s32 crtc_y = unit->crtc.y;
2347 s32 crtc_width = unit->crtc.mode.hdisplay;
2348 s32 crtc_height = unit->crtc.mode.vdisplay;
2349 const struct drm_clip_rect *clips_ptr = clips;
2350 const struct drm_vmw_rect *vclips_ptr = vclips;
2351
2352 dirty->unit = unit;
2353 if (dirty->fifo_reserve_size > 0) {
2354 dirty->cmd = vmw_fifo_reserve(dev_priv,
2355 dirty->fifo_reserve_size);
2356 if (!dirty->cmd) {
2357 DRM_ERROR("Couldn't reserve fifo space "
2358 "for dirty blits.\n");
Christian Engelmayerf3b8c0c2015-09-19 00:32:24 +02002359 return -ENOMEM;
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07002360 }
2361 memset(dirty->cmd, 0, dirty->fifo_reserve_size);
2362 }
2363 dirty->num_hits = 0;
2364 for (i = 0; i < num_clips; i++, clips_ptr += increment,
2365 vclips_ptr += increment) {
2366 s32 clip_left;
2367 s32 clip_top;
2368
2369 /*
2370 * Select clip array type. Note that integer type
2371 * in @clips is unsigned short, whereas in @vclips
2372 * it's 32-bit.
2373 */
2374 if (clips) {
2375 dirty->fb_x = (s32) clips_ptr->x1;
2376 dirty->fb_y = (s32) clips_ptr->y1;
2377 dirty->unit_x2 = (s32) clips_ptr->x2 + dest_x -
2378 crtc_x;
2379 dirty->unit_y2 = (s32) clips_ptr->y2 + dest_y -
2380 crtc_y;
2381 } else {
2382 dirty->fb_x = vclips_ptr->x;
2383 dirty->fb_y = vclips_ptr->y;
2384 dirty->unit_x2 = dirty->fb_x + vclips_ptr->w +
2385 dest_x - crtc_x;
2386 dirty->unit_y2 = dirty->fb_y + vclips_ptr->h +
2387 dest_y - crtc_y;
2388 }
2389
2390 dirty->unit_x1 = dirty->fb_x + dest_x - crtc_x;
2391 dirty->unit_y1 = dirty->fb_y + dest_y - crtc_y;
2392
2393 /* Skip this clip if it's outside the crtc region */
2394 if (dirty->unit_x1 >= crtc_width ||
2395 dirty->unit_y1 >= crtc_height ||
2396 dirty->unit_x2 <= 0 || dirty->unit_y2 <= 0)
2397 continue;
2398
2399 /* Clip right and bottom to crtc limits */
2400 dirty->unit_x2 = min_t(s32, dirty->unit_x2,
2401 crtc_width);
2402 dirty->unit_y2 = min_t(s32, dirty->unit_y2,
2403 crtc_height);
2404
2405 /* Clip left and top to crtc limits */
2406 clip_left = min_t(s32, dirty->unit_x1, 0);
2407 clip_top = min_t(s32, dirty->unit_y1, 0);
2408 dirty->unit_x1 -= clip_left;
2409 dirty->unit_y1 -= clip_top;
2410 dirty->fb_x -= clip_left;
2411 dirty->fb_y -= clip_top;
2412
2413 dirty->clip(dirty);
2414 }
2415
2416 dirty->fifo_commit(dirty);
2417 }
2418
2419 return 0;
2420}
2421
2422/**
2423 * vmw_kms_helper_buffer_prepare - Reserve and validate a buffer object before
2424 * command submission.
2425 *
2426 * @dev_priv. Pointer to a device private structure.
2427 * @buf: The buffer object
2428 * @interruptible: Whether to perform waits as interruptible.
2429 * @validate_as_mob: Whether the buffer should be validated as a MOB. If false,
2430 * The buffer will be validated as a GMR. Already pinned buffers will not be
2431 * validated.
2432 *
2433 * Returns 0 on success, negative error code on failure, -ERESTARTSYS if
2434 * interrupted by a signal.
2435 */
2436int vmw_kms_helper_buffer_prepare(struct vmw_private *dev_priv,
2437 struct vmw_dma_buffer *buf,
2438 bool interruptible,
2439 bool validate_as_mob)
2440{
2441 struct ttm_buffer_object *bo = &buf->base;
2442 int ret;
2443
Christian Königdfd5e502016-04-06 11:12:03 +02002444 ttm_bo_reserve(bo, false, false, NULL);
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07002445 ret = vmw_validate_single_buffer(dev_priv, bo, interruptible,
2446 validate_as_mob);
2447 if (ret)
2448 ttm_bo_unreserve(bo);
2449
2450 return ret;
2451}
2452
2453/**
2454 * vmw_kms_helper_buffer_revert - Undo the actions of
2455 * vmw_kms_helper_buffer_prepare.
2456 *
2457 * @res: Pointer to the buffer object.
2458 *
2459 * Helper to be used if an error forces the caller to undo the actions of
2460 * vmw_kms_helper_buffer_prepare.
2461 */
2462void vmw_kms_helper_buffer_revert(struct vmw_dma_buffer *buf)
2463{
2464 if (buf)
2465 ttm_bo_unreserve(&buf->base);
2466}
2467
2468/**
2469 * vmw_kms_helper_buffer_finish - Unreserve and fence a buffer object after
2470 * kms command submission.
2471 *
2472 * @dev_priv: Pointer to a device private structure.
2473 * @file_priv: Pointer to a struct drm_file representing the caller's
2474 * connection. Must be set to NULL if @user_fence_rep is NULL, and conversely
2475 * if non-NULL, @user_fence_rep must be non-NULL.
2476 * @buf: The buffer object.
2477 * @out_fence: Optional pointer to a fence pointer. If non-NULL, a
2478 * ref-counted fence pointer is returned here.
2479 * @user_fence_rep: Optional pointer to a user-space provided struct
2480 * drm_vmw_fence_rep. If provided, @file_priv must also be provided and the
2481 * function copies fence data to user-space in a fail-safe manner.
2482 */
2483void vmw_kms_helper_buffer_finish(struct vmw_private *dev_priv,
2484 struct drm_file *file_priv,
2485 struct vmw_dma_buffer *buf,
2486 struct vmw_fence_obj **out_fence,
2487 struct drm_vmw_fence_rep __user *
2488 user_fence_rep)
2489{
2490 struct vmw_fence_obj *fence;
2491 uint32_t handle;
2492 int ret;
2493
2494 ret = vmw_execbuf_fence_commands(file_priv, dev_priv, &fence,
2495 file_priv ? &handle : NULL);
2496 if (buf)
2497 vmw_fence_single_bo(&buf->base, fence);
2498 if (file_priv)
2499 vmw_execbuf_copy_fence_user(dev_priv, vmw_fpriv(file_priv),
2500 ret, user_fence_rep, fence,
Sinclair Yehc906965d2017-07-05 01:49:32 -07002501 handle, -1, NULL);
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07002502 if (out_fence)
2503 *out_fence = fence;
2504 else
2505 vmw_fence_obj_unreference(&fence);
2506
2507 vmw_kms_helper_buffer_revert(buf);
2508}
2509
2510
2511/**
2512 * vmw_kms_helper_resource_revert - Undo the actions of
2513 * vmw_kms_helper_resource_prepare.
2514 *
2515 * @res: Pointer to the resource. Typically a surface.
2516 *
2517 * Helper to be used if an error forces the caller to undo the actions of
2518 * vmw_kms_helper_resource_prepare.
2519 */
2520void vmw_kms_helper_resource_revert(struct vmw_resource *res)
2521{
2522 vmw_kms_helper_buffer_revert(res->backup);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002523 vmw_resource_unreserve(res, false, NULL, 0);
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07002524 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
2525}
2526
2527/**
2528 * vmw_kms_helper_resource_prepare - Reserve and validate a resource before
2529 * command submission.
2530 *
2531 * @res: Pointer to the resource. Typically a surface.
2532 * @interruptible: Whether to perform waits as interruptible.
2533 *
2534 * Reserves and validates also the backup buffer if a guest-backed resource.
2535 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
2536 * interrupted by a signal.
2537 */
2538int vmw_kms_helper_resource_prepare(struct vmw_resource *res,
2539 bool interruptible)
2540{
2541 int ret = 0;
2542
2543 if (interruptible)
2544 ret = mutex_lock_interruptible(&res->dev_priv->cmdbuf_mutex);
2545 else
2546 mutex_lock(&res->dev_priv->cmdbuf_mutex);
2547
2548 if (unlikely(ret != 0))
2549 return -ERESTARTSYS;
2550
2551 ret = vmw_resource_reserve(res, interruptible, false);
2552 if (ret)
2553 goto out_unlock;
2554
2555 if (res->backup) {
2556 ret = vmw_kms_helper_buffer_prepare(res->dev_priv, res->backup,
2557 interruptible,
2558 res->dev_priv->has_mob);
2559 if (ret)
2560 goto out_unreserve;
2561 }
2562 ret = vmw_resource_validate(res);
2563 if (ret)
2564 goto out_revert;
2565 return 0;
2566
2567out_revert:
2568 vmw_kms_helper_buffer_revert(res->backup);
2569out_unreserve:
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002570 vmw_resource_unreserve(res, false, NULL, 0);
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07002571out_unlock:
2572 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
2573 return ret;
2574}
2575
2576/**
2577 * vmw_kms_helper_resource_finish - Unreserve and fence a resource after
2578 * kms command submission.
2579 *
2580 * @res: Pointer to the resource. Typically a surface.
2581 * @out_fence: Optional pointer to a fence pointer. If non-NULL, a
2582 * ref-counted fence pointer is returned here.
2583 */
2584void vmw_kms_helper_resource_finish(struct vmw_resource *res,
2585 struct vmw_fence_obj **out_fence)
2586{
2587 if (res->backup || out_fence)
2588 vmw_kms_helper_buffer_finish(res->dev_priv, NULL, res->backup,
2589 out_fence, NULL);
2590
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002591 vmw_resource_unreserve(res, false, NULL, 0);
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07002592 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
2593}
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07002594
2595/**
2596 * vmw_kms_update_proxy - Helper function to update a proxy surface from
2597 * its backing MOB.
2598 *
2599 * @res: Pointer to the surface resource
2600 * @clips: Clip rects in framebuffer (surface) space.
2601 * @num_clips: Number of clips in @clips.
2602 * @increment: Integer with which to increment the clip counter when looping.
2603 * Used to skip a predetermined number of clip rects.
2604 *
2605 * This function makes sure the proxy surface is updated from its backing MOB
2606 * using the region given by @clips. The surface resource @res and its backing
2607 * MOB needs to be reserved and validated on call.
2608 */
2609int vmw_kms_update_proxy(struct vmw_resource *res,
2610 const struct drm_clip_rect *clips,
2611 unsigned num_clips,
2612 int increment)
2613{
2614 struct vmw_private *dev_priv = res->dev_priv;
2615 struct drm_vmw_size *size = &vmw_res_to_srf(res)->base_size;
2616 struct {
2617 SVGA3dCmdHeader header;
2618 SVGA3dCmdUpdateGBImage body;
2619 } *cmd;
2620 SVGA3dBox *box;
2621 size_t copy_size = 0;
2622 int i;
2623
2624 if (!clips)
2625 return 0;
2626
2627 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd) * num_clips);
2628 if (!cmd) {
2629 DRM_ERROR("Couldn't reserve fifo space for proxy surface "
2630 "update.\n");
2631 return -ENOMEM;
2632 }
2633
2634 for (i = 0; i < num_clips; ++i, clips += increment, ++cmd) {
2635 box = &cmd->body.box;
2636
2637 cmd->header.id = SVGA_3D_CMD_UPDATE_GB_IMAGE;
2638 cmd->header.size = sizeof(cmd->body);
2639 cmd->body.image.sid = res->id;
2640 cmd->body.image.face = 0;
2641 cmd->body.image.mipmap = 0;
2642
2643 if (clips->x1 > size->width || clips->x2 > size->width ||
2644 clips->y1 > size->height || clips->y2 > size->height) {
2645 DRM_ERROR("Invalid clips outsize of framebuffer.\n");
2646 return -EINVAL;
2647 }
2648
2649 box->x = clips->x1;
2650 box->y = clips->y1;
2651 box->z = 0;
2652 box->w = clips->x2 - clips->x1;
2653 box->h = clips->y2 - clips->y1;
2654 box->d = 1;
2655
2656 copy_size += sizeof(*cmd);
2657 }
2658
2659 vmw_fifo_commit(dev_priv, copy_size);
2660
2661 return 0;
2662}
Thomas Hellstroma2787242015-06-29 12:55:07 -07002663
2664int vmw_kms_fbdev_init_data(struct vmw_private *dev_priv,
2665 unsigned unit,
2666 u32 max_width,
2667 u32 max_height,
2668 struct drm_connector **p_con,
2669 struct drm_crtc **p_crtc,
2670 struct drm_display_mode **p_mode)
2671{
2672 struct drm_connector *con;
2673 struct vmw_display_unit *du;
2674 struct drm_display_mode *mode;
2675 int i = 0;
2676
2677 list_for_each_entry(con, &dev_priv->dev->mode_config.connector_list,
2678 head) {
2679 if (i == unit)
2680 break;
2681
2682 ++i;
2683 }
2684
2685 if (i != unit) {
2686 DRM_ERROR("Could not find initial display unit.\n");
2687 return -EINVAL;
2688 }
2689
2690 if (list_empty(&con->modes))
2691 (void) vmw_du_connector_fill_modes(con, max_width, max_height);
2692
2693 if (list_empty(&con->modes)) {
2694 DRM_ERROR("Could not find initial display mode.\n");
2695 return -EINVAL;
2696 }
2697
2698 du = vmw_connector_to_du(con);
2699 *p_con = con;
2700 *p_crtc = &du->crtc;
2701
2702 list_for_each_entry(mode, &con->modes, head) {
2703 if (mode->type & DRM_MODE_TYPE_PREFERRED)
2704 break;
2705 }
2706
2707 if (mode->type & DRM_MODE_TYPE_PREFERRED)
2708 *p_mode = mode;
2709 else {
2710 WARN_ONCE(true, "Could not find initial preferred mode.\n");
2711 *p_mode = list_first_entry(&con->modes,
2712 struct drm_display_mode,
2713 head);
2714 }
2715
2716 return 0;
2717}
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002718
2719/**
2720 * vmw_kms_del_active - unregister a crtc binding to the implicit framebuffer
2721 *
2722 * @dev_priv: Pointer to a device private struct.
2723 * @du: The display unit of the crtc.
2724 */
2725void vmw_kms_del_active(struct vmw_private *dev_priv,
2726 struct vmw_display_unit *du)
2727{
Thomas Hellstrom93cd1682016-05-03 11:24:35 +02002728 mutex_lock(&dev_priv->global_kms_state_mutex);
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002729 if (du->active_implicit) {
2730 if (--(dev_priv->num_implicit) == 0)
2731 dev_priv->implicit_fb = NULL;
2732 du->active_implicit = false;
2733 }
Thomas Hellstrom93cd1682016-05-03 11:24:35 +02002734 mutex_unlock(&dev_priv->global_kms_state_mutex);
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002735}
2736
2737/**
2738 * vmw_kms_add_active - register a crtc binding to an implicit framebuffer
2739 *
2740 * @vmw_priv: Pointer to a device private struct.
2741 * @du: The display unit of the crtc.
2742 * @vfb: The implicit framebuffer
2743 *
2744 * Registers a binding to an implicit framebuffer.
2745 */
2746void vmw_kms_add_active(struct vmw_private *dev_priv,
2747 struct vmw_display_unit *du,
2748 struct vmw_framebuffer *vfb)
2749{
Thomas Hellstrom93cd1682016-05-03 11:24:35 +02002750 mutex_lock(&dev_priv->global_kms_state_mutex);
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002751 WARN_ON_ONCE(!dev_priv->num_implicit && dev_priv->implicit_fb);
2752
2753 if (!du->active_implicit && du->is_implicit) {
2754 dev_priv->implicit_fb = vfb;
2755 du->active_implicit = true;
2756 dev_priv->num_implicit++;
2757 }
Thomas Hellstrom93cd1682016-05-03 11:24:35 +02002758 mutex_unlock(&dev_priv->global_kms_state_mutex);
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002759}
2760
2761/**
2762 * vmw_kms_screen_object_flippable - Check whether we can page-flip a crtc.
2763 *
2764 * @dev_priv: Pointer to device-private struct.
2765 * @crtc: The crtc we want to flip.
2766 *
2767 * Returns true or false depending whether it's OK to flip this crtc
2768 * based on the criterion that we must not have more than one implicit
2769 * frame-buffer at any one time.
2770 */
2771bool vmw_kms_crtc_flippable(struct vmw_private *dev_priv,
2772 struct drm_crtc *crtc)
2773{
2774 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
Thomas Hellstrom93cd1682016-05-03 11:24:35 +02002775 bool ret;
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002776
Thomas Hellstrom93cd1682016-05-03 11:24:35 +02002777 mutex_lock(&dev_priv->global_kms_state_mutex);
2778 ret = !du->is_implicit || dev_priv->num_implicit == 1;
2779 mutex_unlock(&dev_priv->global_kms_state_mutex);
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002780
Thomas Hellstrom93cd1682016-05-03 11:24:35 +02002781 return ret;
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002782}
2783
2784/**
2785 * vmw_kms_update_implicit_fb - Update the implicit fb.
2786 *
2787 * @dev_priv: Pointer to device-private struct.
2788 * @crtc: The crtc the new implicit frame-buffer is bound to.
2789 */
2790void vmw_kms_update_implicit_fb(struct vmw_private *dev_priv,
2791 struct drm_crtc *crtc)
2792{
2793 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
2794 struct vmw_framebuffer *vfb;
2795
Thomas Hellstrom93cd1682016-05-03 11:24:35 +02002796 mutex_lock(&dev_priv->global_kms_state_mutex);
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002797
2798 if (!du->is_implicit)
Thomas Hellstrom93cd1682016-05-03 11:24:35 +02002799 goto out_unlock;
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002800
2801 vfb = vmw_framebuffer_to_vfb(crtc->primary->fb);
2802 WARN_ON_ONCE(dev_priv->num_implicit != 1 &&
2803 dev_priv->implicit_fb != vfb);
2804
2805 dev_priv->implicit_fb = vfb;
Thomas Hellstrom93cd1682016-05-03 11:24:35 +02002806out_unlock:
2807 mutex_unlock(&dev_priv->global_kms_state_mutex);
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002808}
Thomas Hellstrom76404ac2016-02-12 09:55:45 +01002809
2810/**
2811 * vmw_kms_create_implicit_placement_proparty - Set up the implicit placement
2812 * property.
2813 *
2814 * @dev_priv: Pointer to a device private struct.
2815 * @immutable: Whether the property is immutable.
2816 *
2817 * Sets up the implicit placement property unless it's already set up.
2818 */
2819void
2820vmw_kms_create_implicit_placement_property(struct vmw_private *dev_priv,
2821 bool immutable)
2822{
2823 if (dev_priv->implicit_placement_property)
2824 return;
2825
2826 dev_priv->implicit_placement_property =
2827 drm_property_create_range(dev_priv->dev,
2828 immutable ?
2829 DRM_MODE_PROP_IMMUTABLE : 0,
2830 "implicit_placement", 0, 1);
2831
2832}
Sinclair Yeh904bb5e2017-03-23 14:29:22 -07002833
2834
2835/**
2836 * vmw_kms_set_config - Wrapper around drm_atomic_helper_set_config
2837 *
2838 * @set: The configuration to set.
2839 *
2840 * The vmwgfx Xorg driver doesn't assign the mode::type member, which
2841 * when drm_mode_set_crtcinfo is called as part of the configuration setting
2842 * causes it to return incorrect crtc dimensions causing severe problems in
2843 * the vmwgfx modesetting. So explicitly clear that member before calling
2844 * into drm_atomic_helper_set_config.
2845 */
Dave Airlie320d8c32017-04-03 16:30:24 +10002846int vmw_kms_set_config(struct drm_mode_set *set,
2847 struct drm_modeset_acquire_ctx *ctx)
Sinclair Yeh904bb5e2017-03-23 14:29:22 -07002848{
2849 if (set && set->mode)
2850 set->mode->type = 0;
2851
Dave Airlie320d8c32017-04-03 16:30:24 +10002852 return drm_atomic_helper_set_config(set, ctx);
Sinclair Yeh904bb5e2017-03-23 14:29:22 -07002853}