blob: c9f5ddabfb72fcda85b73bff23185d0fd49888a6 [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"
29
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +020030
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000031/* Might need a hrtimer here? */
32#define VMWGFX_PRESENT_RATE ((HZ / 60 > 0) ? HZ / 60 : 1)
33
Sinclair Yehc8261a92015-06-26 01:23:42 -070034void vmw_du_cleanup(struct vmw_display_unit *du)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000035{
Sinclair Yeh36cc79b2017-03-23 11:28:11 -070036 drm_plane_cleanup(&du->primary);
37 drm_plane_cleanup(&du->cursor);
38
Thomas Wood34ea3d32014-05-29 16:57:41 +010039 drm_connector_unregister(&du->connector);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000040 drm_crtc_cleanup(&du->crtc);
41 drm_encoder_cleanup(&du->encoder);
42 drm_connector_cleanup(&du->connector);
43}
44
45/*
46 * Display Unit Cursor functions
47 */
48
Sinclair Yeh36cc79b2017-03-23 11:28:11 -070049static int vmw_cursor_update_image(struct vmw_private *dev_priv,
50 u32 *image, u32 width, u32 height,
51 u32 hotspotX, u32 hotspotY)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000052{
53 struct {
54 u32 cmd;
55 SVGAFifoCmdDefineAlphaCursor cursor;
56 } *cmd;
57 u32 image_size = width * height * 4;
58 u32 cmd_size = sizeof(*cmd) + image_size;
59
60 if (!image)
61 return -EINVAL;
62
63 cmd = vmw_fifo_reserve(dev_priv, cmd_size);
64 if (unlikely(cmd == NULL)) {
65 DRM_ERROR("Fifo reserve failed.\n");
66 return -ENOMEM;
67 }
68
69 memset(cmd, 0, sizeof(*cmd));
70
71 memcpy(&cmd[1], image, image_size);
72
Thomas Hellstromb9eb1a62015-04-02 02:39:45 -070073 cmd->cmd = SVGA_CMD_DEFINE_ALPHA_CURSOR;
74 cmd->cursor.id = 0;
75 cmd->cursor.width = width;
76 cmd->cursor.height = height;
77 cmd->cursor.hotspotX = hotspotX;
78 cmd->cursor.hotspotY = hotspotY;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000079
Thomas Hellstrom4e0858a2015-11-05 02:18:55 -080080 vmw_fifo_commit_flush(dev_priv, cmd_size);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000081
82 return 0;
83}
84
Sinclair Yeh36cc79b2017-03-23 11:28:11 -070085static int vmw_cursor_update_dmabuf(struct vmw_private *dev_priv,
86 struct vmw_dma_buffer *dmabuf,
87 u32 width, u32 height,
88 u32 hotspotX, u32 hotspotY)
Jakob Bornecrantz6a91d972011-11-28 13:19:10 +010089{
90 struct ttm_bo_kmap_obj map;
91 unsigned long kmap_offset;
92 unsigned long kmap_num;
93 void *virtual;
94 bool dummy;
95 int ret;
96
97 kmap_offset = 0;
98 kmap_num = (width*height*4 + PAGE_SIZE - 1) >> PAGE_SHIFT;
99
Christian Königdfd5e502016-04-06 11:12:03 +0200100 ret = ttm_bo_reserve(&dmabuf->base, true, false, NULL);
Jakob Bornecrantz6a91d972011-11-28 13:19:10 +0100101 if (unlikely(ret != 0)) {
102 DRM_ERROR("reserve failed\n");
103 return -EINVAL;
104 }
105
106 ret = ttm_bo_kmap(&dmabuf->base, kmap_offset, kmap_num, &map);
107 if (unlikely(ret != 0))
108 goto err_unreserve;
109
110 virtual = ttm_kmap_obj_virtual(&map, &dummy);
111 ret = vmw_cursor_update_image(dev_priv, virtual, width, height,
112 hotspotX, hotspotY);
113
114 ttm_bo_kunmap(&map);
115err_unreserve:
116 ttm_bo_unreserve(&dmabuf->base);
117
118 return ret;
119}
120
121
Sinclair Yeh36cc79b2017-03-23 11:28:11 -0700122static void vmw_cursor_update_position(struct vmw_private *dev_priv,
123 bool show, int x, int y)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000124{
Thomas Hellstromb76ff5e2015-10-28 10:44:04 +0100125 u32 *fifo_mem = dev_priv->mmio_virt;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000126 uint32_t count;
127
Sinclair Yeh36cc79b2017-03-23 11:28:11 -0700128 spin_lock(&dev_priv->cursor_lock);
Thomas Hellstromb76ff5e2015-10-28 10:44:04 +0100129 vmw_mmio_write(show ? 1 : 0, fifo_mem + SVGA_FIFO_CURSOR_ON);
130 vmw_mmio_write(x, fifo_mem + SVGA_FIFO_CURSOR_X);
131 vmw_mmio_write(y, fifo_mem + SVGA_FIFO_CURSOR_Y);
132 count = vmw_mmio_read(fifo_mem + SVGA_FIFO_CURSOR_COUNT);
133 vmw_mmio_write(++count, fifo_mem + SVGA_FIFO_CURSOR_COUNT);
Sinclair Yeh36cc79b2017-03-23 11:28:11 -0700134 spin_unlock(&dev_priv->cursor_lock);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000135}
136
Thomas Hellstrom8fbf9d92015-11-26 19:45:16 +0100137
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000138void vmw_kms_cursor_snoop(struct vmw_surface *srf,
139 struct ttm_object_file *tfile,
140 struct ttm_buffer_object *bo,
141 SVGA3dCmdHeader *header)
142{
143 struct ttm_bo_kmap_obj map;
144 unsigned long kmap_offset;
145 unsigned long kmap_num;
146 SVGA3dCopyBox *box;
147 unsigned box_count;
148 void *virtual;
149 bool dummy;
150 struct vmw_dma_cmd {
151 SVGA3dCmdHeader header;
152 SVGA3dCmdSurfaceDMA dma;
153 } *cmd;
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100154 int i, ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000155
156 cmd = container_of(header, struct vmw_dma_cmd, header);
157
158 /* No snooper installed */
159 if (!srf->snooper.image)
160 return;
161
162 if (cmd->dma.host.face != 0 || cmd->dma.host.mipmap != 0) {
163 DRM_ERROR("face and mipmap for cursors should never != 0\n");
164 return;
165 }
166
167 if (cmd->header.size < 64) {
168 DRM_ERROR("at least one full copy box must be given\n");
169 return;
170 }
171
172 box = (SVGA3dCopyBox *)&cmd[1];
173 box_count = (cmd->header.size - sizeof(SVGA3dCmdSurfaceDMA)) /
174 sizeof(SVGA3dCopyBox);
175
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100176 if (cmd->dma.guest.ptr.offset % PAGE_SIZE ||
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000177 box->x != 0 || box->y != 0 || box->z != 0 ||
178 box->srcx != 0 || box->srcy != 0 || box->srcz != 0 ||
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100179 box->d != 1 || box_count != 1) {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000180 /* TODO handle none page aligned offsets */
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100181 /* TODO handle more dst & src != 0 */
182 /* TODO handle more then one copy */
183 DRM_ERROR("Cant snoop dma request for cursor!\n");
184 DRM_ERROR("(%u, %u, %u) (%u, %u, %u) (%ux%ux%u) %u %u\n",
185 box->srcx, box->srcy, box->srcz,
186 box->x, box->y, box->z,
187 box->w, box->h, box->d, box_count,
188 cmd->dma.guest.ptr.offset);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000189 return;
190 }
191
192 kmap_offset = cmd->dma.guest.ptr.offset >> PAGE_SHIFT;
193 kmap_num = (64*64*4) >> PAGE_SHIFT;
194
Christian Königdfd5e502016-04-06 11:12:03 +0200195 ret = ttm_bo_reserve(bo, true, false, NULL);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000196 if (unlikely(ret != 0)) {
197 DRM_ERROR("reserve failed\n");
198 return;
199 }
200
201 ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
202 if (unlikely(ret != 0))
203 goto err_unreserve;
204
205 virtual = ttm_kmap_obj_virtual(&map, &dummy);
206
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100207 if (box->w == 64 && cmd->dma.guest.pitch == 64*4) {
208 memcpy(srf->snooper.image, virtual, 64*64*4);
209 } else {
210 /* Image is unsigned pointer. */
211 for (i = 0; i < box->h; i++)
212 memcpy(srf->snooper.image + i * 64,
213 virtual + i * cmd->dma.guest.pitch,
214 box->w * 4);
215 }
216
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000217 srf->snooper.age++;
218
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000219 ttm_bo_kunmap(&map);
220err_unreserve:
221 ttm_bo_unreserve(bo);
222}
223
Thomas Hellstrom8fbf9d92015-11-26 19:45:16 +0100224/**
225 * vmw_kms_legacy_hotspot_clear - Clear legacy hotspots
226 *
227 * @dev_priv: Pointer to the device private struct.
228 *
229 * Clears all legacy hotspots.
230 */
231void vmw_kms_legacy_hotspot_clear(struct vmw_private *dev_priv)
232{
233 struct drm_device *dev = dev_priv->dev;
234 struct vmw_display_unit *du;
235 struct drm_crtc *crtc;
236
237 drm_modeset_lock_all(dev);
238 drm_for_each_crtc(crtc, dev) {
239 du = vmw_crtc_to_du(crtc);
240
241 du->hotspot_x = 0;
242 du->hotspot_y = 0;
243 }
244 drm_modeset_unlock_all(dev);
245}
246
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000247void vmw_kms_cursor_post_execbuf(struct vmw_private *dev_priv)
248{
249 struct drm_device *dev = dev_priv->dev;
250 struct vmw_display_unit *du;
251 struct drm_crtc *crtc;
252
253 mutex_lock(&dev->mode_config.mutex);
254
255 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
256 du = vmw_crtc_to_du(crtc);
257 if (!du->cursor_surface ||
258 du->cursor_age == du->cursor_surface->snooper.age)
259 continue;
260
261 du->cursor_age = du->cursor_surface->snooper.age;
262 vmw_cursor_update_image(dev_priv,
263 du->cursor_surface->snooper.image,
Thomas Hellstrom8fbf9d92015-11-26 19:45:16 +0100264 64, 64,
265 du->hotspot_x + du->core_hotspot_x,
266 du->hotspot_y + du->core_hotspot_y);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000267 }
268
269 mutex_unlock(&dev->mode_config.mutex);
270}
271
Sinclair Yeh36cc79b2017-03-23 11:28:11 -0700272
273
274/**
275 * vmw_du_cursor_plane_update() - Update cursor image and location
276 *
277 * @plane: plane object to update
278 * @crtc: owning CRTC of @plane
279 * @fb: framebuffer to flip onto plane
280 * @crtc_x: x offset of plane on crtc
281 * @crtc_y: y offset of plane on crtc
282 * @crtc_w: width of plane rectangle on crtc
283 * @crtc_h: height of plane rectangle on crtc
284 * @src_x: Not used
285 * @src_y: Not used
286 * @src_w: Not used
287 * @src_h: Not used
288 *
289 *
290 * RETURNS:
291 * Zero on success, error code on failure
292 */
293int vmw_du_cursor_plane_update(struct drm_plane *plane,
294 struct drm_crtc *crtc,
295 struct drm_framebuffer *fb,
296 int crtc_x, int crtc_y,
297 unsigned int crtc_w,
298 unsigned int crtc_h,
299 uint32_t src_x, uint32_t src_y,
300 uint32_t src_w, uint32_t src_h)
301{
302 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
303 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
304 struct vmw_surface *surface = NULL;
305 struct vmw_dma_buffer *dmabuf = NULL;
306 s32 hotspot_x, hotspot_y;
307 int ret;
308
309 hotspot_x = du->hotspot_x + fb->hot_x;
310 hotspot_y = du->hotspot_y + fb->hot_y;
311
312 /* A lot of the code assumes this */
313 if (crtc_w != 64 || crtc_h != 64) {
314 ret = -EINVAL;
315 goto out;
316 }
317
318 if (vmw_framebuffer_to_vfb(fb)->dmabuf)
319 dmabuf = vmw_framebuffer_to_vfbd(fb)->buffer;
320 else
321 surface = vmw_framebuffer_to_vfbs(fb)->surface;
322
323 if (surface && !surface->snooper.image) {
324 DRM_ERROR("surface not suitable for cursor\n");
325 ret = -EINVAL;
326 goto out;
327 }
328
329 /* setup new image */
330 ret = 0;
331 if (surface) {
332 /* vmw_user_surface_lookup takes one reference */
333 du->cursor_surface = surface;
334
335 du->cursor_age = du->cursor_surface->snooper.age;
336
337 ret = vmw_cursor_update_image(dev_priv, surface->snooper.image,
338 64, 64, hotspot_x, hotspot_y);
339 } else if (dmabuf) {
340 /* vmw_user_surface_lookup takes one reference */
341 du->cursor_dmabuf = dmabuf;
342
343 ret = vmw_cursor_update_dmabuf(dev_priv, dmabuf, crtc_w, crtc_h,
344 hotspot_x, hotspot_y);
345 } else {
346 vmw_cursor_update_position(dev_priv, false, 0, 0);
347 goto out;
348 }
349
350 if (!ret) {
351 du->cursor_x = crtc_x + du->set_gui_x;
352 du->cursor_y = crtc_y + du->set_gui_y;
353
354 vmw_cursor_update_position(dev_priv, true,
355 du->cursor_x + hotspot_x,
356 du->cursor_y + hotspot_y);
357 }
358
359out:
360 return ret;
361}
362
363
364int vmw_du_cursor_plane_disable(struct drm_plane *plane)
365{
366 if (plane->fb) {
367 drm_framebuffer_unreference(plane->fb);
368 plane->fb = NULL;
369 }
370
371 return -EINVAL;
372}
373
374
375void vmw_du_cursor_plane_destroy(struct drm_plane *plane)
376{
377 vmw_cursor_update_position(plane->dev->dev_private, false, 0, 0);
378
379 drm_plane_cleanup(plane);
380}
381
382
383void vmw_du_primary_plane_destroy(struct drm_plane *plane)
384{
385 drm_plane_cleanup(plane);
386
387 /* Planes are static in our case so we don't free it */
388}
389
390
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000391/*
392 * Generic framebuffer code
393 */
394
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000395/*
396 * Surface framebuffer code
397 */
398
Rashika Kheria847c5962014-01-06 22:18:10 +0530399static void vmw_framebuffer_surface_destroy(struct drm_framebuffer *framebuffer)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000400{
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200401 struct vmw_framebuffer_surface *vfbs =
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000402 vmw_framebuffer_to_vfbs(framebuffer);
403
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000404 drm_framebuffer_cleanup(framebuffer);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200405 vmw_surface_unreference(&vfbs->surface);
Thomas Hellstroma2787242015-06-29 12:55:07 -0700406 if (vfbs->base.user_obj)
407 ttm_base_object_unref(&vfbs->base.user_obj);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000408
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200409 kfree(vfbs);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000410}
411
Rashika Kheria847c5962014-01-06 22:18:10 +0530412static int vmw_framebuffer_surface_dirty(struct drm_framebuffer *framebuffer,
Thomas Hellstrom02b00162010-10-05 12:43:02 +0200413 struct drm_file *file_priv,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000414 unsigned flags, unsigned color,
415 struct drm_clip_rect *clips,
416 unsigned num_clips)
417{
418 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
419 struct vmw_framebuffer_surface *vfbs =
420 vmw_framebuffer_to_vfbs(framebuffer);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000421 struct drm_clip_rect norect;
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200422 int ret, inc = 1;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000423
Sinclair Yehc8261a92015-06-26 01:23:42 -0700424 /* Legacy Display Unit does not support 3D */
425 if (dev_priv->active_display_unit == vmw_du_legacy)
Jakob Bornecrantz01e81412011-10-04 20:13:24 +0200426 return -EINVAL;
427
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200428 drm_modeset_lock_all(dev_priv->dev);
429
Thomas Hellstrom294adf72014-02-27 12:34:51 +0100430 ret = ttm_read_lock(&dev_priv->reservation_sem, true);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200431 if (unlikely(ret != 0)) {
432 drm_modeset_unlock_all(dev_priv->dev);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200433 return ret;
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200434 }
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200435
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000436 if (!num_clips) {
437 num_clips = 1;
438 clips = &norect;
439 norect.x1 = norect.y1 = 0;
440 norect.x2 = framebuffer->width;
441 norect.y2 = framebuffer->height;
442 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
443 num_clips /= 2;
444 inc = 2; /* skip source rects */
445 }
446
Sinclair Yehc8261a92015-06-26 01:23:42 -0700447 if (dev_priv->active_display_unit == vmw_du_screen_object)
Thomas Hellstrom10b1e0c2015-06-26 02:14:27 -0700448 ret = vmw_kms_sou_do_surface_dirty(dev_priv, &vfbs->base,
449 clips, NULL, NULL, 0, 0,
450 num_clips, inc, NULL);
Sinclair Yeh35c05122015-06-26 01:42:06 -0700451 else
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700452 ret = vmw_kms_stdu_surface_dirty(dev_priv, &vfbs->base,
453 clips, NULL, NULL, 0, 0,
454 num_clips, inc, NULL);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000455
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700456 vmw_fifo_flush(dev_priv, false);
Thomas Hellstrom294adf72014-02-27 12:34:51 +0100457 ttm_read_unlock(&dev_priv->reservation_sem);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200458
459 drm_modeset_unlock_all(dev_priv->dev);
460
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000461 return 0;
462}
463
Thomas Hellstrom10b1e0c2015-06-26 02:14:27 -0700464/**
465 * vmw_kms_readback - Perform a readback from the screen system to
466 * a dma-buffer backed framebuffer.
467 *
468 * @dev_priv: Pointer to the device private structure.
469 * @file_priv: Pointer to a struct drm_file identifying the caller.
470 * Must be set to NULL if @user_fence_rep is NULL.
471 * @vfb: Pointer to the dma-buffer backed framebuffer.
472 * @user_fence_rep: User-space provided structure for fence information.
473 * Must be set to non-NULL if @file_priv is non-NULL.
474 * @vclips: Array of clip rects.
475 * @num_clips: Number of clip rects in @vclips.
476 *
477 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
478 * interrupted.
479 */
480int vmw_kms_readback(struct vmw_private *dev_priv,
481 struct drm_file *file_priv,
482 struct vmw_framebuffer *vfb,
483 struct drm_vmw_fence_rep __user *user_fence_rep,
484 struct drm_vmw_rect *vclips,
485 uint32_t num_clips)
486{
487 switch (dev_priv->active_display_unit) {
488 case vmw_du_screen_object:
489 return vmw_kms_sou_readback(dev_priv, file_priv, vfb,
490 user_fence_rep, vclips, num_clips);
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700491 case vmw_du_screen_target:
492 return vmw_kms_stdu_dma(dev_priv, file_priv, vfb,
493 user_fence_rep, NULL, vclips, num_clips,
494 1, false, true);
Thomas Hellstrom10b1e0c2015-06-26 02:14:27 -0700495 default:
496 WARN_ONCE(true,
497 "Readback called with invalid display system.\n");
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700498}
Thomas Hellstrom10b1e0c2015-06-26 02:14:27 -0700499
500 return -ENOSYS;
501}
502
503
Ville Syrjäläd7955fc2015-12-15 12:21:15 +0100504static const struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs = {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000505 .destroy = vmw_framebuffer_surface_destroy,
506 .dirty = vmw_framebuffer_surface_dirty,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000507};
508
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200509static int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv,
510 struct vmw_surface *surface,
511 struct vmw_framebuffer **out,
Daniel Vetterdabdcdc2016-12-02 08:07:40 +0100512 const struct drm_mode_fb_cmd2
Sinclair Yehf89c6c32015-06-26 01:54:28 -0700513 *mode_cmd,
514 bool is_dmabuf_proxy)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000515
516{
517 struct drm_device *dev = dev_priv->dev;
518 struct vmw_framebuffer_surface *vfbs;
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200519 enum SVGA3dSurfaceFormat format;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000520 int ret;
Daniel Vetterdabdcdc2016-12-02 08:07:40 +0100521 struct drm_format_name_buf format_name;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000522
Sinclair Yehc8261a92015-06-26 01:23:42 -0700523 /* 3D is only supported on HWv8 and newer hosts */
524 if (dev_priv->active_display_unit == vmw_du_legacy)
Jakob Bornecrantz01e81412011-10-04 20:13:24 +0200525 return -ENOSYS;
526
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200527 /*
528 * Sanity checks.
529 */
530
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100531 /* Surface must be marked as a scanout. */
532 if (unlikely(!surface->scanout))
533 return -EINVAL;
534
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200535 if (unlikely(surface->mip_levels[0] != 1 ||
536 surface->num_sizes != 1 ||
Thomas Hellstromb360a3c2014-01-15 08:51:36 +0100537 surface->base_size.width < mode_cmd->width ||
538 surface->base_size.height < mode_cmd->height ||
539 surface->base_size.depth != 1)) {
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200540 DRM_ERROR("Incompatible surface dimensions "
541 "for requested mode.\n");
542 return -EINVAL;
543 }
544
Daniel Vetterdabdcdc2016-12-02 08:07:40 +0100545 switch (mode_cmd->pixel_format) {
546 case DRM_FORMAT_ARGB8888:
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200547 format = SVGA3D_A8R8G8B8;
548 break;
Daniel Vetterdabdcdc2016-12-02 08:07:40 +0100549 case DRM_FORMAT_XRGB8888:
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200550 format = SVGA3D_X8R8G8B8;
551 break;
Daniel Vetterdabdcdc2016-12-02 08:07:40 +0100552 case DRM_FORMAT_RGB565:
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200553 format = SVGA3D_R5G6B5;
554 break;
Daniel Vetterdabdcdc2016-12-02 08:07:40 +0100555 case DRM_FORMAT_XRGB1555:
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200556 format = SVGA3D_A1R5G5B5;
557 break;
558 default:
Daniel Vetterdabdcdc2016-12-02 08:07:40 +0100559 DRM_ERROR("Invalid pixel format: %s\n",
560 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200561 return -EINVAL;
562 }
563
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700564 /*
565 * For DX, surface format validation is done when surface->scanout
566 * is set.
567 */
568 if (!dev_priv->has_dx && format != surface->format) {
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200569 DRM_ERROR("Invalid surface format for requested mode.\n");
570 return -EINVAL;
571 }
572
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000573 vfbs = kzalloc(sizeof(*vfbs), GFP_KERNEL);
574 if (!vfbs) {
575 ret = -ENOMEM;
576 goto out_err1;
577 }
578
Ville Syrjäläa3f913c2016-12-14 22:48:59 +0200579 drm_helper_mode_fill_fb_struct(dev, &vfbs->base.base, mode_cmd);
Sinclair Yeh05c95012015-08-11 22:53:39 -0700580 vfbs->surface = vmw_surface_reference(surface);
Daniel Vetterdabdcdc2016-12-02 08:07:40 +0100581 vfbs->base.user_handle = mode_cmd->handles[0];
Sinclair Yehf89c6c32015-06-26 01:54:28 -0700582 vfbs->is_dmabuf_proxy = is_dmabuf_proxy;
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200583
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000584 *out = &vfbs->base;
585
Daniel Vetter80f0b5a2012-12-13 23:39:01 +0100586 ret = drm_framebuffer_init(dev, &vfbs->base.base,
587 &vmw_framebuffer_surface_funcs);
588 if (ret)
Sinclair Yeh05c95012015-08-11 22:53:39 -0700589 goto out_err2;
Daniel Vetter80f0b5a2012-12-13 23:39:01 +0100590
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000591 return 0;
592
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000593out_err2:
Sinclair Yeh05c95012015-08-11 22:53:39 -0700594 vmw_surface_unreference(&surface);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000595 kfree(vfbs);
596out_err1:
597 return ret;
598}
599
600/*
601 * Dmabuf framebuffer code
602 */
603
Rashika Kheria847c5962014-01-06 22:18:10 +0530604static void vmw_framebuffer_dmabuf_destroy(struct drm_framebuffer *framebuffer)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000605{
606 struct vmw_framebuffer_dmabuf *vfbd =
607 vmw_framebuffer_to_vfbd(framebuffer);
608
609 drm_framebuffer_cleanup(framebuffer);
610 vmw_dmabuf_unreference(&vfbd->buffer);
Thomas Hellstroma2787242015-06-29 12:55:07 -0700611 if (vfbd->base.user_obj)
612 ttm_base_object_unref(&vfbd->base.user_obj);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000613
614 kfree(vfbd);
615}
616
Rashika Kheria847c5962014-01-06 22:18:10 +0530617static int vmw_framebuffer_dmabuf_dirty(struct drm_framebuffer *framebuffer,
Thomas Hellstrom02b00162010-10-05 12:43:02 +0200618 struct drm_file *file_priv,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000619 unsigned flags, unsigned color,
620 struct drm_clip_rect *clips,
621 unsigned num_clips)
622{
623 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200624 struct vmw_framebuffer_dmabuf *vfbd =
625 vmw_framebuffer_to_vfbd(framebuffer);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000626 struct drm_clip_rect norect;
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200627 int ret, increment = 1;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000628
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200629 drm_modeset_lock_all(dev_priv->dev);
630
Thomas Hellstrom294adf72014-02-27 12:34:51 +0100631 ret = ttm_read_lock(&dev_priv->reservation_sem, true);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200632 if (unlikely(ret != 0)) {
633 drm_modeset_unlock_all(dev_priv->dev);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200634 return ret;
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200635 }
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200636
Thomas Hellstromdf1c93b2010-01-13 22:28:36 +0100637 if (!num_clips) {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000638 num_clips = 1;
639 clips = &norect;
640 norect.x1 = norect.y1 = 0;
641 norect.x2 = framebuffer->width;
642 norect.y2 = framebuffer->height;
643 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
644 num_clips /= 2;
645 increment = 2;
646 }
647
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700648 switch (dev_priv->active_display_unit) {
649 case vmw_du_screen_target:
650 ret = vmw_kms_stdu_dma(dev_priv, NULL, &vfbd->base, NULL,
651 clips, NULL, num_clips, increment,
652 true, true);
653 break;
654 case vmw_du_screen_object:
Thomas Hellstrom10b1e0c2015-06-26 02:14:27 -0700655 ret = vmw_kms_sou_do_dmabuf_dirty(dev_priv, &vfbd->base,
Thomas Hellstrom897b8182016-02-12 08:32:08 +0100656 clips, NULL, num_clips,
657 increment, true, NULL);
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700658 break;
Thomas Hellstrom352b20d2015-06-29 12:57:37 -0700659 case vmw_du_legacy:
660 ret = vmw_kms_ldu_do_dmabuf_dirty(dev_priv, &vfbd->base, 0, 0,
661 clips, num_clips, increment);
662 break;
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700663 default:
Thomas Hellstrom352b20d2015-06-29 12:57:37 -0700664 ret = -EINVAL;
665 WARN_ONCE(true, "Dirty called with invalid display system.\n");
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700666 break;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +0200667 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000668
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700669 vmw_fifo_flush(dev_priv, false);
Thomas Hellstrom294adf72014-02-27 12:34:51 +0100670 ttm_read_unlock(&dev_priv->reservation_sem);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200671
672 drm_modeset_unlock_all(dev_priv->dev);
673
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200674 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000675}
676
Ville Syrjäläd7955fc2015-12-15 12:21:15 +0100677static const struct drm_framebuffer_funcs vmw_framebuffer_dmabuf_funcs = {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000678 .destroy = vmw_framebuffer_dmabuf_destroy,
679 .dirty = vmw_framebuffer_dmabuf_dirty,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000680};
681
Jakob Bornecrantz497a3ff2011-10-04 20:13:14 +0200682/**
Jakob Bornecrantz497a3ff2011-10-04 20:13:14 +0200683 * Pin the dmabuffer to the start of vram.
684 */
Thomas Hellstromfd006a42015-06-28 02:50:56 -0700685static int vmw_framebuffer_pin(struct vmw_framebuffer *vfb)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000686{
687 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
Thomas Hellstromfd006a42015-06-28 02:50:56 -0700688 struct vmw_dma_buffer *buf;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000689 int ret;
690
Thomas Hellstromfd006a42015-06-28 02:50:56 -0700691 buf = vfb->dmabuf ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
692 vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +0200693
Thomas Hellstromfd006a42015-06-28 02:50:56 -0700694 if (!buf)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000695 return 0;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000696
Thomas Hellstromfd006a42015-06-28 02:50:56 -0700697 switch (dev_priv->active_display_unit) {
698 case vmw_du_legacy:
699 vmw_overlay_pause_all(dev_priv);
700 ret = vmw_dmabuf_pin_in_start_of_vram(dev_priv, buf, false);
701 vmw_overlay_resume_all(dev_priv);
702 break;
703 case vmw_du_screen_object:
704 case vmw_du_screen_target:
705 if (vfb->dmabuf)
706 return vmw_dmabuf_pin_in_vram_or_gmr(dev_priv, buf,
707 false);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000708
Thomas Hellstromfd006a42015-06-28 02:50:56 -0700709 return vmw_dmabuf_pin_in_placement(dev_priv, buf,
710 &vmw_mob_placement, false);
711 default:
712 return -EINVAL;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000713 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000714
Thomas Hellstromfd006a42015-06-28 02:50:56 -0700715 return ret;
716}
717
718static int vmw_framebuffer_unpin(struct vmw_framebuffer *vfb)
719{
720 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
721 struct vmw_dma_buffer *buf;
722
723 buf = vfb->dmabuf ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
724 vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
725
726 if (WARN_ON(!buf))
727 return 0;
728
729 return vmw_dmabuf_unpin(dev_priv, buf, false);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000730}
731
Sinclair Yehf89c6c32015-06-26 01:54:28 -0700732/**
733 * vmw_create_dmabuf_proxy - create a proxy surface for the DMA buf
734 *
735 * @dev: DRM device
736 * @mode_cmd: parameters for the new surface
737 * @dmabuf_mob: MOB backing the DMA buf
738 * @srf_out: newly created surface
739 *
740 * When the content FB is a DMA buf, we create a surface as a proxy to the
741 * same buffer. This way we can do a surface copy rather than a surface DMA.
742 * This is a more efficient approach
743 *
744 * RETURNS:
745 * 0 on success, error code otherwise
746 */
747static int vmw_create_dmabuf_proxy(struct drm_device *dev,
Daniel Vetterdabdcdc2016-12-02 08:07:40 +0100748 const struct drm_mode_fb_cmd2 *mode_cmd,
Sinclair Yehf89c6c32015-06-26 01:54:28 -0700749 struct vmw_dma_buffer *dmabuf_mob,
750 struct vmw_surface **srf_out)
751{
752 uint32_t format;
Thomas Hellstrom8cd9f252017-01-19 10:53:02 -0800753 struct drm_vmw_size content_base_size = {0};
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700754 struct vmw_resource *res;
Thomas Hellstroma50e2bf2016-01-08 20:29:40 +0100755 unsigned int bytes_pp;
Daniel Vetterdabdcdc2016-12-02 08:07:40 +0100756 struct drm_format_name_buf format_name;
Sinclair Yehf89c6c32015-06-26 01:54:28 -0700757 int ret;
758
Daniel Vetterdabdcdc2016-12-02 08:07:40 +0100759 switch (mode_cmd->pixel_format) {
760 case DRM_FORMAT_ARGB8888:
761 case DRM_FORMAT_XRGB8888:
Sinclair Yehf89c6c32015-06-26 01:54:28 -0700762 format = SVGA3D_X8R8G8B8;
Thomas Hellstroma50e2bf2016-01-08 20:29:40 +0100763 bytes_pp = 4;
Sinclair Yehf89c6c32015-06-26 01:54:28 -0700764 break;
765
Daniel Vetterdabdcdc2016-12-02 08:07:40 +0100766 case DRM_FORMAT_RGB565:
767 case DRM_FORMAT_XRGB1555:
Sinclair Yehf89c6c32015-06-26 01:54:28 -0700768 format = SVGA3D_R5G6B5;
Thomas Hellstroma50e2bf2016-01-08 20:29:40 +0100769 bytes_pp = 2;
Sinclair Yehf89c6c32015-06-26 01:54:28 -0700770 break;
771
772 case 8:
773 format = SVGA3D_P8;
Thomas Hellstroma50e2bf2016-01-08 20:29:40 +0100774 bytes_pp = 1;
Sinclair Yehf89c6c32015-06-26 01:54:28 -0700775 break;
776
777 default:
Daniel Vetterdabdcdc2016-12-02 08:07:40 +0100778 DRM_ERROR("Invalid framebuffer format %s\n",
779 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Sinclair Yehf89c6c32015-06-26 01:54:28 -0700780 return -EINVAL;
781 }
782
Daniel Vetterdabdcdc2016-12-02 08:07:40 +0100783 content_base_size.width = mode_cmd->pitches[0] / bytes_pp;
Sinclair Yehf89c6c32015-06-26 01:54:28 -0700784 content_base_size.height = mode_cmd->height;
785 content_base_size.depth = 1;
786
787 ret = vmw_surface_gb_priv_define(dev,
788 0, /* kernel visible only */
789 0, /* flags */
790 format,
791 true, /* can be a scanout buffer */
792 1, /* num of mip levels */
793 0,
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700794 0,
Sinclair Yehf89c6c32015-06-26 01:54:28 -0700795 content_base_size,
796 srf_out);
797 if (ret) {
798 DRM_ERROR("Failed to allocate proxy content buffer\n");
799 return ret;
800 }
801
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700802 res = &(*srf_out)->res;
Sinclair Yehf89c6c32015-06-26 01:54:28 -0700803
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700804 /* Reserve and switch the backing mob. */
805 mutex_lock(&res->dev_priv->cmdbuf_mutex);
806 (void) vmw_resource_reserve(res, false, true);
807 vmw_dmabuf_unreference(&res->backup);
808 res->backup = vmw_dmabuf_reference(dmabuf_mob);
809 res->backup_offset = 0;
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700810 vmw_resource_unreserve(res, false, NULL, 0);
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700811 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000812
813 return 0;
814}
815
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000816
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000817
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200818static int vmw_kms_new_framebuffer_dmabuf(struct vmw_private *dev_priv,
819 struct vmw_dma_buffer *dmabuf,
820 struct vmw_framebuffer **out,
Daniel Vetterdabdcdc2016-12-02 08:07:40 +0100821 const struct drm_mode_fb_cmd2
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200822 *mode_cmd)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000823
824{
825 struct drm_device *dev = dev_priv->dev;
826 struct vmw_framebuffer_dmabuf *vfbd;
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200827 unsigned int requested_size;
Daniel Vetterdabdcdc2016-12-02 08:07:40 +0100828 struct drm_format_name_buf format_name;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000829 int ret;
830
Daniel Vetterdabdcdc2016-12-02 08:07:40 +0100831 requested_size = mode_cmd->height * mode_cmd->pitches[0];
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200832 if (unlikely(requested_size > dmabuf->base.num_pages * PAGE_SIZE)) {
833 DRM_ERROR("Screen buffer object size is too small "
834 "for requested mode.\n");
835 return -EINVAL;
836 }
837
Jakob Bornecrantzc337ada2011-10-04 20:13:34 +0200838 /* Limited framebuffer color depth support for screen objects */
Sinclair Yehc8261a92015-06-26 01:23:42 -0700839 if (dev_priv->active_display_unit == vmw_du_screen_object) {
Daniel Vetterdabdcdc2016-12-02 08:07:40 +0100840 switch (mode_cmd->pixel_format) {
841 case DRM_FORMAT_XRGB8888:
842 case DRM_FORMAT_ARGB8888:
843 break;
844 case DRM_FORMAT_XRGB1555:
845 case DRM_FORMAT_RGB565:
846 break;
Jakob Bornecrantzc337ada2011-10-04 20:13:34 +0200847 default:
Daniel Vetterdabdcdc2016-12-02 08:07:40 +0100848 DRM_ERROR("Invalid pixel format: %s\n",
849 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Jakob Bornecrantzc337ada2011-10-04 20:13:34 +0200850 return -EINVAL;
851 }
852 }
853
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000854 vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL);
855 if (!vfbd) {
856 ret = -ENOMEM;
857 goto out_err1;
858 }
859
Ville Syrjäläa3f913c2016-12-14 22:48:59 +0200860 drm_helper_mode_fill_fb_struct(dev, &vfbd->base.base, mode_cmd);
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +0200861 vfbd->base.dmabuf = true;
Sinclair Yeh05c95012015-08-11 22:53:39 -0700862 vfbd->buffer = vmw_dmabuf_reference(dmabuf);
Daniel Vetterdabdcdc2016-12-02 08:07:40 +0100863 vfbd->base.user_handle = mode_cmd->handles[0];
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000864 *out = &vfbd->base;
865
Daniel Vetter80f0b5a2012-12-13 23:39:01 +0100866 ret = drm_framebuffer_init(dev, &vfbd->base.base,
867 &vmw_framebuffer_dmabuf_funcs);
868 if (ret)
Sinclair Yeh05c95012015-08-11 22:53:39 -0700869 goto out_err2;
Daniel Vetter80f0b5a2012-12-13 23:39:01 +0100870
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000871 return 0;
872
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000873out_err2:
Sinclair Yeh05c95012015-08-11 22:53:39 -0700874 vmw_dmabuf_unreference(&dmabuf);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000875 kfree(vfbd);
876out_err1:
877 return ret;
878}
879
Thomas Hellstromfd006a42015-06-28 02:50:56 -0700880/**
881 * vmw_kms_new_framebuffer - Create a new framebuffer.
882 *
883 * @dev_priv: Pointer to device private struct.
884 * @dmabuf: Pointer to dma buffer to wrap the kms framebuffer around.
885 * Either @dmabuf or @surface must be NULL.
886 * @surface: Pointer to a surface to wrap the kms framebuffer around.
887 * Either @dmabuf or @surface must be NULL.
888 * @only_2d: No presents will occur to this dma buffer based framebuffer. This
889 * Helps the code to do some important optimizations.
890 * @mode_cmd: Frame-buffer metadata.
891 */
892struct vmw_framebuffer *
893vmw_kms_new_framebuffer(struct vmw_private *dev_priv,
894 struct vmw_dma_buffer *dmabuf,
895 struct vmw_surface *surface,
896 bool only_2d,
Daniel Vetterdabdcdc2016-12-02 08:07:40 +0100897 const struct drm_mode_fb_cmd2 *mode_cmd)
Thomas Hellstromfd006a42015-06-28 02:50:56 -0700898{
Sinclair Yeh05c95012015-08-11 22:53:39 -0700899 struct vmw_framebuffer *vfb = NULL;
Thomas Hellstromfd006a42015-06-28 02:50:56 -0700900 bool is_dmabuf_proxy = false;
901 int ret;
902
903 /*
904 * We cannot use the SurfaceDMA command in an non-accelerated VM,
905 * therefore, wrap the DMA buf in a surface so we can use the
906 * SurfaceCopy command.
907 */
908 if (dmabuf && only_2d &&
909 dev_priv->active_display_unit == vmw_du_screen_target) {
910 ret = vmw_create_dmabuf_proxy(dev_priv->dev, mode_cmd,
911 dmabuf, &surface);
912 if (ret)
913 return ERR_PTR(ret);
914
915 is_dmabuf_proxy = true;
916 }
917
918 /* Create the new framebuffer depending one what we have */
Sinclair Yeh05c95012015-08-11 22:53:39 -0700919 if (surface) {
Thomas Hellstromfd006a42015-06-28 02:50:56 -0700920 ret = vmw_kms_new_framebuffer_surface(dev_priv, surface, &vfb,
921 mode_cmd,
922 is_dmabuf_proxy);
Sinclair Yeh05c95012015-08-11 22:53:39 -0700923
924 /*
925 * vmw_create_dmabuf_proxy() adds a reference that is no longer
926 * needed
927 */
928 if (is_dmabuf_proxy)
929 vmw_surface_unreference(&surface);
930 } else if (dmabuf) {
Thomas Hellstromfd006a42015-06-28 02:50:56 -0700931 ret = vmw_kms_new_framebuffer_dmabuf(dev_priv, dmabuf, &vfb,
932 mode_cmd);
Sinclair Yeh05c95012015-08-11 22:53:39 -0700933 } else {
Thomas Hellstromfd006a42015-06-28 02:50:56 -0700934 BUG();
Sinclair Yeh05c95012015-08-11 22:53:39 -0700935 }
Thomas Hellstromfd006a42015-06-28 02:50:56 -0700936
937 if (ret)
938 return ERR_PTR(ret);
939
940 vfb->pin = vmw_framebuffer_pin;
941 vfb->unpin = vmw_framebuffer_unpin;
942
943 return vfb;
944}
945
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000946/*
947 * Generic Kernel modesetting functions
948 */
949
950static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev,
951 struct drm_file *file_priv,
Daniel Vetterdabdcdc2016-12-02 08:07:40 +0100952 const struct drm_mode_fb_cmd2 *mode_cmd)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000953{
954 struct vmw_private *dev_priv = vmw_priv(dev);
955 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
956 struct vmw_framebuffer *vfb = NULL;
957 struct vmw_surface *surface = NULL;
958 struct vmw_dma_buffer *bo = NULL;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200959 struct ttm_base_object *user_obj;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000960 int ret;
961
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200962 /**
963 * This code should be conditioned on Screen Objects not being used.
964 * If screen objects are used, we can allocate a GMR to hold the
965 * requested framebuffer.
966 */
967
Xi Wang8a783892011-12-21 05:18:33 -0500968 if (!vmw_kms_validate_mode_vram(dev_priv,
Daniel Vetterdabdcdc2016-12-02 08:07:40 +0100969 mode_cmd->pitches[0],
970 mode_cmd->height)) {
Sinclair Yehc8261a92015-06-26 01:23:42 -0700971 DRM_ERROR("Requested mode exceed bounding box limit.\n");
Jakob Bornecrantzd9826402011-11-03 21:03:04 +0100972 return ERR_PTR(-ENOMEM);
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200973 }
974
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200975 /*
976 * Take a reference on the user object of the resource
977 * backing the kms fb. This ensures that user-space handle
978 * lookups on that resource will always work as long as
979 * it's registered with a kms framebuffer. This is important,
980 * since vmw_execbuf_process identifies resources in the
981 * command stream using user-space handles.
982 */
983
Daniel Vetterdabdcdc2016-12-02 08:07:40 +0100984 user_obj = ttm_base_object_lookup(tfile, mode_cmd->handles[0]);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +0200985 if (unlikely(user_obj == NULL)) {
986 DRM_ERROR("Could not locate requested kms frame buffer.\n");
987 return ERR_PTR(-ENOENT);
988 }
989
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200990 /**
991 * End conditioned code.
992 */
993
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100994 /* returns either a dmabuf or surface */
995 ret = vmw_user_lookup_handle(dev_priv, tfile,
Daniel Vetterdabdcdc2016-12-02 08:07:40 +0100996 mode_cmd->handles[0],
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100997 &surface, &bo);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000998 if (ret)
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100999 goto err_out;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001000
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001001 vfb = vmw_kms_new_framebuffer(dev_priv, bo, surface,
1002 !(dev_priv->capabilities & SVGA_CAP_3D),
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001003 mode_cmd);
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001004 if (IS_ERR(vfb)) {
1005 ret = PTR_ERR(vfb);
1006 goto err_out;
1007 }
Jakob Bornecrantz5ffdb652010-01-30 03:38:08 +00001008
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +01001009err_out:
1010 /* vmw_user_lookup_handle takes one ref so does new_fb */
1011 if (bo)
1012 vmw_dmabuf_unreference(&bo);
1013 if (surface)
1014 vmw_surface_unreference(&surface);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001015
1016 if (ret) {
1017 DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001018 ttm_base_object_unref(&user_obj);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01001019 return ERR_PTR(ret);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001020 } else
1021 vfb->user_obj = user_obj;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001022
1023 return &vfb->base;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001024}
1025
Laurent Pincharte6ecefa2012-05-17 13:27:23 +02001026static const struct drm_mode_config_funcs vmw_kms_funcs = {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001027 .fb_create = vmw_kms_fb_create,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001028};
1029
Thomas Hellstromb9eb1a62015-04-02 02:39:45 -07001030static int vmw_kms_generic_present(struct vmw_private *dev_priv,
1031 struct drm_file *file_priv,
1032 struct vmw_framebuffer *vfb,
1033 struct vmw_surface *surface,
1034 uint32_t sid,
1035 int32_t destX, int32_t destY,
1036 struct drm_vmw_rect *clips,
1037 uint32_t num_clips)
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001038{
Thomas Hellstrom10b1e0c2015-06-26 02:14:27 -07001039 return vmw_kms_sou_do_surface_dirty(dev_priv, vfb, NULL, clips,
1040 &surface->res, destX, destY,
1041 num_clips, 1, NULL);
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001042}
1043
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001044
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001045int vmw_kms_present(struct vmw_private *dev_priv,
1046 struct drm_file *file_priv,
1047 struct vmw_framebuffer *vfb,
1048 struct vmw_surface *surface,
1049 uint32_t sid,
1050 int32_t destX, int32_t destY,
1051 struct drm_vmw_rect *clips,
1052 uint32_t num_clips)
1053{
Sinclair Yeh35c05122015-06-26 01:42:06 -07001054 int ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001055
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001056 switch (dev_priv->active_display_unit) {
1057 case vmw_du_screen_target:
1058 ret = vmw_kms_stdu_surface_dirty(dev_priv, vfb, NULL, clips,
1059 &surface->res, destX, destY,
1060 num_clips, 1, NULL);
1061 break;
1062 case vmw_du_screen_object:
1063 ret = vmw_kms_generic_present(dev_priv, file_priv, vfb, surface,
1064 sid, destX, destY, clips,
1065 num_clips);
1066 break;
1067 default:
1068 WARN_ONCE(true,
1069 "Present called with invalid display system.\n");
1070 ret = -ENOSYS;
1071 break;
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001072 }
Sinclair Yeh35c05122015-06-26 01:42:06 -07001073 if (ret)
1074 return ret;
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001075
Sinclair Yeh35c05122015-06-26 01:42:06 -07001076 vmw_fifo_flush(dev_priv, false);
Michel Dänzer0bef23f2011-08-31 07:42:50 +00001077
Sinclair Yeh35c05122015-06-26 01:42:06 -07001078 return 0;
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001079}
1080
Thomas Hellstrom578e6092016-02-12 09:45:42 +01001081static void
1082vmw_kms_create_hotplug_mode_update_property(struct vmw_private *dev_priv)
1083{
1084 if (dev_priv->hotplug_mode_update_property)
1085 return;
1086
1087 dev_priv->hotplug_mode_update_property =
1088 drm_property_create_range(dev_priv->dev,
1089 DRM_MODE_PROP_IMMUTABLE,
1090 "hotplug_mode_update", 0, 1);
1091
1092 if (!dev_priv->hotplug_mode_update_property)
1093 return;
1094
1095}
1096
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001097int vmw_kms_init(struct vmw_private *dev_priv)
1098{
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001099 struct drm_device *dev = dev_priv->dev;
1100 int ret;
1101
1102 drm_mode_config_init(dev);
1103 dev->mode_config.funcs = &vmw_kms_funcs;
1104 dev->mode_config.min_width = 1;
1105 dev->mode_config.min_height = 1;
Sinclair Yeh65ade7d2015-07-16 10:49:13 -07001106 dev->mode_config.max_width = dev_priv->texture_max_width;
1107 dev->mode_config.max_height = dev_priv->texture_max_height;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001108
Thomas Hellstrom578e6092016-02-12 09:45:42 +01001109 drm_mode_create_suggested_offset_properties(dev);
1110 vmw_kms_create_hotplug_mode_update_property(dev_priv);
1111
Sinclair Yeh35c05122015-06-26 01:42:06 -07001112 ret = vmw_kms_stdu_init_display(dev_priv);
1113 if (ret) {
1114 ret = vmw_kms_sou_init_display(dev_priv);
1115 if (ret) /* Fallback */
1116 ret = vmw_kms_ldu_init_display(dev_priv);
1117 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001118
Sinclair Yehc8261a92015-06-26 01:23:42 -07001119 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001120}
1121
1122int vmw_kms_close(struct vmw_private *dev_priv)
1123{
Sinclair Yehc8261a92015-06-26 01:23:42 -07001124 int ret;
1125
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001126 /*
1127 * Docs says we should take the lock before calling this function
1128 * but since it destroys encoders and our destructor calls
1129 * drm_encoder_cleanup which takes the lock we deadlock.
1130 */
1131 drm_mode_config_cleanup(dev_priv->dev);
Sinclair Yehc8261a92015-06-26 01:23:42 -07001132 if (dev_priv->active_display_unit == vmw_du_screen_object)
1133 ret = vmw_kms_sou_close_display(dev_priv);
Sinclair Yeh35c05122015-06-26 01:42:06 -07001134 else if (dev_priv->active_display_unit == vmw_du_screen_target)
1135 ret = vmw_kms_stdu_close_display(dev_priv);
Jakob Bornecrantzc0d18312011-11-09 10:25:26 +01001136 else
Sinclair Yehc8261a92015-06-26 01:23:42 -07001137 ret = vmw_kms_ldu_close_display(dev_priv);
1138
1139 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001140}
1141
1142int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
1143 struct drm_file *file_priv)
1144{
1145 struct drm_vmw_cursor_bypass_arg *arg = data;
1146 struct vmw_display_unit *du;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001147 struct drm_crtc *crtc;
1148 int ret = 0;
1149
1150
1151 mutex_lock(&dev->mode_config.mutex);
1152 if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
1153
1154 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1155 du = vmw_crtc_to_du(crtc);
1156 du->hotspot_x = arg->xhot;
1157 du->hotspot_y = arg->yhot;
1158 }
1159
1160 mutex_unlock(&dev->mode_config.mutex);
1161 return 0;
1162 }
1163
Rob Clarka4cd5d62014-07-17 23:30:02 -04001164 crtc = drm_crtc_find(dev, arg->crtc_id);
1165 if (!crtc) {
Ville Syrjälä4ae87ff2013-10-17 13:35:05 +03001166 ret = -ENOENT;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001167 goto out;
1168 }
1169
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001170 du = vmw_crtc_to_du(crtc);
1171
1172 du->hotspot_x = arg->xhot;
1173 du->hotspot_y = arg->yhot;
1174
1175out:
1176 mutex_unlock(&dev->mode_config.mutex);
1177
1178 return ret;
1179}
1180
1181int vmw_kms_write_svga(struct vmw_private *vmw_priv,
1182 unsigned width, unsigned height, unsigned pitch,
1183 unsigned bpp, unsigned depth)
1184{
1185 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1186 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch);
1187 else if (vmw_fifo_have_pitchlock(vmw_priv))
Thomas Hellstromb76ff5e2015-10-28 10:44:04 +01001188 vmw_mmio_write(pitch, vmw_priv->mmio_virt +
1189 SVGA_FIFO_PITCHLOCK);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001190 vmw_write(vmw_priv, SVGA_REG_WIDTH, width);
1191 vmw_write(vmw_priv, SVGA_REG_HEIGHT, height);
1192 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp);
1193
1194 if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) {
1195 DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n",
1196 depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH));
1197 return -EINVAL;
1198 }
1199
1200 return 0;
1201}
1202
1203int vmw_kms_save_vga(struct vmw_private *vmw_priv)
1204{
1205 struct vmw_vga_topology_state *save;
1206 uint32_t i;
1207
1208 vmw_priv->vga_width = vmw_read(vmw_priv, SVGA_REG_WIDTH);
1209 vmw_priv->vga_height = vmw_read(vmw_priv, SVGA_REG_HEIGHT);
1210 vmw_priv->vga_bpp = vmw_read(vmw_priv, SVGA_REG_BITS_PER_PIXEL);
1211 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1212 vmw_priv->vga_pitchlock =
1213 vmw_read(vmw_priv, SVGA_REG_PITCHLOCK);
1214 else if (vmw_fifo_have_pitchlock(vmw_priv))
Thomas Hellstromb76ff5e2015-10-28 10:44:04 +01001215 vmw_priv->vga_pitchlock = vmw_mmio_read(vmw_priv->mmio_virt +
1216 SVGA_FIFO_PITCHLOCK);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001217
1218 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1219 return 0;
1220
1221 vmw_priv->num_displays = vmw_read(vmw_priv,
1222 SVGA_REG_NUM_GUEST_DISPLAYS);
1223
1224 if (vmw_priv->num_displays == 0)
1225 vmw_priv->num_displays = 1;
1226
1227 for (i = 0; i < vmw_priv->num_displays; ++i) {
1228 save = &vmw_priv->vga_save[i];
1229 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1230 save->primary = vmw_read(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY);
1231 save->pos_x = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_X);
1232 save->pos_y = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y);
1233 save->width = vmw_read(vmw_priv, SVGA_REG_DISPLAY_WIDTH);
1234 save->height = vmw_read(vmw_priv, SVGA_REG_DISPLAY_HEIGHT);
1235 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
1236 if (i == 0 && vmw_priv->num_displays == 1 &&
1237 save->width == 0 && save->height == 0) {
1238
1239 /*
1240 * It should be fairly safe to assume that these
1241 * values are uninitialized.
1242 */
1243
1244 save->width = vmw_priv->vga_width - save->pos_x;
1245 save->height = vmw_priv->vga_height - save->pos_y;
1246 }
1247 }
1248
1249 return 0;
1250}
1251
1252int vmw_kms_restore_vga(struct vmw_private *vmw_priv)
1253{
1254 struct vmw_vga_topology_state *save;
1255 uint32_t i;
1256
1257 vmw_write(vmw_priv, SVGA_REG_WIDTH, vmw_priv->vga_width);
1258 vmw_write(vmw_priv, SVGA_REG_HEIGHT, vmw_priv->vga_height);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001259 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, vmw_priv->vga_bpp);
1260 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1261 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK,
1262 vmw_priv->vga_pitchlock);
1263 else if (vmw_fifo_have_pitchlock(vmw_priv))
Thomas Hellstromb76ff5e2015-10-28 10:44:04 +01001264 vmw_mmio_write(vmw_priv->vga_pitchlock,
1265 vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001266
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001267 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1268 return 0;
1269
1270 for (i = 0; i < vmw_priv->num_displays; ++i) {
1271 save = &vmw_priv->vga_save[i];
1272 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1273 vmw_write(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY, save->primary);
1274 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_X, save->pos_x);
1275 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y, save->pos_y);
1276 vmw_write(vmw_priv, SVGA_REG_DISPLAY_WIDTH, save->width);
1277 vmw_write(vmw_priv, SVGA_REG_DISPLAY_HEIGHT, save->height);
1278 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
1279 }
1280
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001281 return 0;
1282}
Jakob Bornecrantzd8bd19d2010-06-01 11:54:20 +02001283
Thomas Hellstrome133e732010-10-05 12:43:04 +02001284bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
1285 uint32_t pitch,
1286 uint32_t height)
1287{
Sinclair Yeh35c05122015-06-26 01:42:06 -07001288 return ((u64) pitch * (u64) height) < (u64)
1289 ((dev_priv->active_display_unit == vmw_du_screen_target) ?
1290 dev_priv->prim_bb_mem : dev_priv->vram_size);
Thomas Hellstrome133e732010-10-05 12:43:04 +02001291}
1292
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001293
1294/**
1295 * Function called by DRM code called with vbl_lock held.
1296 */
Thierry Reding88e72712015-09-24 18:35:31 +02001297u32 vmw_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
Thomas Hellstrom7a1c2f62010-10-01 10:21:49 +02001298{
1299 return 0;
1300}
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001301
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001302/**
1303 * Function called by DRM code called with vbl_lock held.
1304 */
Thierry Reding88e72712015-09-24 18:35:31 +02001305int vmw_enable_vblank(struct drm_device *dev, unsigned int pipe)
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001306{
1307 return -ENOSYS;
1308}
1309
1310/**
1311 * Function called by DRM code called with vbl_lock held.
1312 */
Thierry Reding88e72712015-09-24 18:35:31 +02001313void vmw_disable_vblank(struct drm_device *dev, unsigned int pipe)
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001314{
1315}
1316
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001317
1318/*
1319 * Small shared kms functions.
1320 */
1321
Rashika Kheria847c5962014-01-06 22:18:10 +05301322static int vmw_du_update_layout(struct vmw_private *dev_priv, unsigned num,
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001323 struct drm_vmw_rect *rects)
1324{
1325 struct drm_device *dev = dev_priv->dev;
1326 struct vmw_display_unit *du;
1327 struct drm_connector *con;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001328
1329 mutex_lock(&dev->mode_config.mutex);
1330
1331#if 0
Thomas Hellstrom6ea77d12011-10-04 20:13:36 +02001332 {
1333 unsigned int i;
1334
1335 DRM_INFO("%s: new layout ", __func__);
1336 for (i = 0; i < num; i++)
1337 DRM_INFO("(%i, %i %ux%u) ", rects[i].x, rects[i].y,
1338 rects[i].w, rects[i].h);
1339 DRM_INFO("\n");
1340 }
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001341#endif
1342
1343 list_for_each_entry(con, &dev->mode_config.connector_list, head) {
1344 du = vmw_connector_to_du(con);
1345 if (num > du->unit) {
1346 du->pref_width = rects[du->unit].w;
1347 du->pref_height = rects[du->unit].h;
1348 du->pref_active = true;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001349 du->gui_x = rects[du->unit].x;
1350 du->gui_y = rects[du->unit].y;
Thomas Hellstrom578e6092016-02-12 09:45:42 +01001351 drm_object_property_set_value
1352 (&con->base, dev->mode_config.suggested_x_property,
1353 du->gui_x);
1354 drm_object_property_set_value
1355 (&con->base, dev->mode_config.suggested_y_property,
1356 du->gui_y);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001357 } else {
1358 du->pref_width = 800;
1359 du->pref_height = 600;
1360 du->pref_active = false;
Thomas Hellstrom578e6092016-02-12 09:45:42 +01001361 drm_object_property_set_value
1362 (&con->base, dev->mode_config.suggested_x_property,
1363 0);
1364 drm_object_property_set_value
1365 (&con->base, dev->mode_config.suggested_y_property,
1366 0);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001367 }
1368 con->status = vmw_du_connector_detect(con, true);
1369 }
1370
1371 mutex_unlock(&dev->mode_config.mutex);
Thomas Hellstrom578e6092016-02-12 09:45:42 +01001372 drm_sysfs_hotplug_event(dev);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001373
1374 return 0;
1375}
1376
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02001377int vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
1378 u16 *r, u16 *g, u16 *b,
1379 uint32_t size)
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001380{
1381 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
1382 int i;
1383
1384 for (i = 0; i < size; i++) {
1385 DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i,
1386 r[i], g[i], b[i]);
1387 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 0, r[i] >> 8);
1388 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 1, g[i] >> 8);
1389 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 2, b[i] >> 8);
1390 }
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02001391
1392 return 0;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001393}
1394
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02001395int vmw_du_connector_dpms(struct drm_connector *connector, int mode)
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001396{
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02001397 return 0;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001398}
1399
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001400enum drm_connector_status
1401vmw_du_connector_detect(struct drm_connector *connector, bool force)
1402{
1403 uint32_t num_displays;
1404 struct drm_device *dev = connector->dev;
1405 struct vmw_private *dev_priv = vmw_priv(dev);
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001406 struct vmw_display_unit *du = vmw_connector_to_du(connector);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001407
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001408 num_displays = vmw_read(dev_priv, SVGA_REG_NUM_DISPLAYS);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001409
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001410 return ((vmw_connector_to_du(connector)->unit < num_displays &&
1411 du->pref_active) ?
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001412 connector_status_connected : connector_status_disconnected);
1413}
1414
1415static struct drm_display_mode vmw_kms_connector_builtin[] = {
1416 /* 640x480@60Hz */
1417 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
1418 752, 800, 0, 480, 489, 492, 525, 0,
1419 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1420 /* 800x600@60Hz */
1421 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
1422 968, 1056, 0, 600, 601, 605, 628, 0,
1423 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1424 /* 1024x768@60Hz */
1425 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
1426 1184, 1344, 0, 768, 771, 777, 806, 0,
1427 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1428 /* 1152x864@75Hz */
1429 { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
1430 1344, 1600, 0, 864, 865, 868, 900, 0,
1431 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1432 /* 1280x768@60Hz */
1433 { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344,
1434 1472, 1664, 0, 768, 771, 778, 798, 0,
1435 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1436 /* 1280x800@60Hz */
1437 { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352,
1438 1480, 1680, 0, 800, 803, 809, 831, 0,
1439 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
1440 /* 1280x960@60Hz */
1441 { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376,
1442 1488, 1800, 0, 960, 961, 964, 1000, 0,
1443 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1444 /* 1280x1024@60Hz */
1445 { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328,
1446 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
1447 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1448 /* 1360x768@60Hz */
1449 { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424,
1450 1536, 1792, 0, 768, 771, 777, 795, 0,
1451 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1452 /* 1440x1050@60Hz */
1453 { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488,
1454 1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
1455 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1456 /* 1440x900@60Hz */
1457 { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520,
1458 1672, 1904, 0, 900, 903, 909, 934, 0,
1459 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1460 /* 1600x1200@60Hz */
1461 { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664,
1462 1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
1463 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1464 /* 1680x1050@60Hz */
1465 { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784,
1466 1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
1467 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1468 /* 1792x1344@60Hz */
1469 { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920,
1470 2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
1471 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1472 /* 1853x1392@60Hz */
1473 { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952,
1474 2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
1475 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1476 /* 1920x1200@60Hz */
1477 { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056,
1478 2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
1479 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1480 /* 1920x1440@60Hz */
1481 { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048,
1482 2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
1483 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1484 /* 2560x1600@60Hz */
1485 { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752,
1486 3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
1487 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1488 /* Terminate */
1489 { DRM_MODE("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) },
1490};
1491
Thomas Hellstrom1543b4d2011-11-02 09:43:10 +01001492/**
1493 * vmw_guess_mode_timing - Provide fake timings for a
1494 * 60Hz vrefresh mode.
1495 *
1496 * @mode - Pointer to a struct drm_display_mode with hdisplay and vdisplay
1497 * members filled in.
1498 */
Thomas Hellstroma2787242015-06-29 12:55:07 -07001499void vmw_guess_mode_timing(struct drm_display_mode *mode)
Thomas Hellstrom1543b4d2011-11-02 09:43:10 +01001500{
1501 mode->hsync_start = mode->hdisplay + 50;
1502 mode->hsync_end = mode->hsync_start + 50;
1503 mode->htotal = mode->hsync_end + 50;
1504
1505 mode->vsync_start = mode->vdisplay + 50;
1506 mode->vsync_end = mode->vsync_start + 50;
1507 mode->vtotal = mode->vsync_end + 50;
1508
1509 mode->clock = (u32)mode->htotal * (u32)mode->vtotal / 100 * 6;
1510 mode->vrefresh = drm_mode_vrefresh(mode);
1511}
1512
1513
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001514int vmw_du_connector_fill_modes(struct drm_connector *connector,
1515 uint32_t max_width, uint32_t max_height)
1516{
1517 struct vmw_display_unit *du = vmw_connector_to_du(connector);
1518 struct drm_device *dev = connector->dev;
1519 struct vmw_private *dev_priv = vmw_priv(dev);
1520 struct drm_display_mode *mode = NULL;
1521 struct drm_display_mode *bmode;
1522 struct drm_display_mode prefmode = { DRM_MODE("preferred",
1523 DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
1524 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1525 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
1526 };
1527 int i;
Sinclair Yeh7c20d212016-06-29 11:29:47 -07001528 u32 assumed_bpp = 4;
Sinclair Yeh9a723842014-10-31 09:58:06 +01001529
Sinclair Yeh04319d82016-06-29 12:15:48 -07001530 if (dev_priv->assume_16bpp)
1531 assumed_bpp = 2;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001532
Sinclair Yeh35c05122015-06-26 01:42:06 -07001533 if (dev_priv->active_display_unit == vmw_du_screen_target) {
1534 max_width = min(max_width, dev_priv->stdu_max_width);
1535 max_height = min(max_height, dev_priv->stdu_max_height);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001536 }
1537
1538 /* Add preferred mode */
Sinclair Yehc8261a92015-06-26 01:23:42 -07001539 mode = drm_mode_duplicate(dev, &prefmode);
1540 if (!mode)
1541 return 0;
1542 mode->hdisplay = du->pref_width;
1543 mode->vdisplay = du->pref_height;
1544 vmw_guess_mode_timing(mode);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001545
Sinclair Yehc8261a92015-06-26 01:23:42 -07001546 if (vmw_kms_validate_mode_vram(dev_priv,
1547 mode->hdisplay * assumed_bpp,
1548 mode->vdisplay)) {
1549 drm_mode_probed_add(connector, mode);
1550 } else {
1551 drm_mode_destroy(dev, mode);
1552 mode = NULL;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001553 }
1554
Sinclair Yehc8261a92015-06-26 01:23:42 -07001555 if (du->pref_mode) {
1556 list_del_init(&du->pref_mode->head);
1557 drm_mode_destroy(dev, du->pref_mode);
1558 }
1559
1560 /* mode might be null here, this is intended */
1561 du->pref_mode = mode;
1562
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001563 for (i = 0; vmw_kms_connector_builtin[i].type != 0; i++) {
1564 bmode = &vmw_kms_connector_builtin[i];
1565 if (bmode->hdisplay > max_width ||
1566 bmode->vdisplay > max_height)
1567 continue;
1568
Sinclair Yeh9a723842014-10-31 09:58:06 +01001569 if (!vmw_kms_validate_mode_vram(dev_priv,
1570 bmode->hdisplay * assumed_bpp,
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001571 bmode->vdisplay))
1572 continue;
1573
1574 mode = drm_mode_duplicate(dev, bmode);
1575 if (!mode)
1576 return 0;
1577 mode->vrefresh = drm_mode_vrefresh(mode);
1578
1579 drm_mode_probed_add(connector, mode);
1580 }
1581
Ville Syrjälä6af3e652015-12-03 23:14:14 +02001582 drm_mode_connector_list_update(connector);
Thomas Hellstromf6b05002015-06-29 12:59:58 -07001583 /* Move the prefered mode first, help apps pick the right mode. */
1584 drm_mode_sort(&connector->modes);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001585
1586 return 1;
1587}
1588
1589int vmw_du_connector_set_property(struct drm_connector *connector,
1590 struct drm_property *property,
1591 uint64_t val)
1592{
Thomas Hellstrom76404ac2016-02-12 09:55:45 +01001593 struct vmw_display_unit *du = vmw_connector_to_du(connector);
1594 struct vmw_private *dev_priv = vmw_priv(connector->dev);
1595
1596 if (property == dev_priv->implicit_placement_property)
1597 du->is_implicit = val;
1598
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001599 return 0;
1600}
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001601
1602
1603int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data,
1604 struct drm_file *file_priv)
1605{
1606 struct vmw_private *dev_priv = vmw_priv(dev);
1607 struct drm_vmw_update_layout_arg *arg =
1608 (struct drm_vmw_update_layout_arg *)data;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001609 void __user *user_rects;
1610 struct drm_vmw_rect *rects;
1611 unsigned rects_size;
1612 int ret;
1613 int i;
Sinclair Yeh65ade7d2015-07-16 10:49:13 -07001614 u64 total_pixels = 0;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001615 struct drm_mode_config *mode_config = &dev->mode_config;
Sinclair Yehc8261a92015-06-26 01:23:42 -07001616 struct drm_vmw_rect bounding_box = {0};
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001617
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001618 if (!arg->num_outputs) {
1619 struct drm_vmw_rect def_rect = {0, 0, 800, 600};
1620 vmw_du_update_layout(dev_priv, 1, &def_rect);
Thomas Hellstrom5151adb2015-03-09 01:56:21 -07001621 return 0;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001622 }
1623
1624 rects_size = arg->num_outputs * sizeof(struct drm_vmw_rect);
Xi Wangbab9efc2011-11-28 12:25:43 +01001625 rects = kcalloc(arg->num_outputs, sizeof(struct drm_vmw_rect),
1626 GFP_KERNEL);
Thomas Hellstrom5151adb2015-03-09 01:56:21 -07001627 if (unlikely(!rects))
1628 return -ENOMEM;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001629
1630 user_rects = (void __user *)(unsigned long)arg->rects;
1631 ret = copy_from_user(rects, user_rects, rects_size);
1632 if (unlikely(ret != 0)) {
1633 DRM_ERROR("Failed to get rects.\n");
1634 ret = -EFAULT;
1635 goto out_free;
1636 }
1637
1638 for (i = 0; i < arg->num_outputs; ++i) {
Xi Wangbab9efc2011-11-28 12:25:43 +01001639 if (rects[i].x < 0 ||
1640 rects[i].y < 0 ||
1641 rects[i].x + rects[i].w > mode_config->max_width ||
1642 rects[i].y + rects[i].h > mode_config->max_height) {
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001643 DRM_ERROR("Invalid GUI layout.\n");
1644 ret = -EINVAL;
1645 goto out_free;
1646 }
Sinclair Yehc8261a92015-06-26 01:23:42 -07001647
1648 /*
1649 * bounding_box.w and bunding_box.h are used as
1650 * lower-right coordinates
1651 */
1652 if (rects[i].x + rects[i].w > bounding_box.w)
1653 bounding_box.w = rects[i].x + rects[i].w;
1654
1655 if (rects[i].y + rects[i].h > bounding_box.h)
1656 bounding_box.h = rects[i].y + rects[i].h;
Sinclair Yeh65ade7d2015-07-16 10:49:13 -07001657
1658 total_pixels += (u64) rects[i].w * (u64) rects[i].h;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001659 }
1660
Sinclair Yeh65ade7d2015-07-16 10:49:13 -07001661 if (dev_priv->active_display_unit == vmw_du_screen_target) {
1662 /*
1663 * For Screen Targets, the limits for a toplogy are:
1664 * 1. Bounding box (assuming 32bpp) must be < prim_bb_mem
1665 * 2. Total pixels (assuming 32bpp) must be < prim_bb_mem
1666 */
Thomas Hellstrom0f580382017-01-19 11:01:04 -08001667 u64 bb_mem = (u64) bounding_box.w * bounding_box.h * 4;
Sinclair Yeh65ade7d2015-07-16 10:49:13 -07001668 u64 pixel_mem = total_pixels * 4;
1669
1670 if (bb_mem > dev_priv->prim_bb_mem) {
1671 DRM_ERROR("Topology is beyond supported limits.\n");
Sinclair Yeh35c05122015-06-26 01:42:06 -07001672 ret = -EINVAL;
1673 goto out_free;
1674 }
1675
Sinclair Yeh65ade7d2015-07-16 10:49:13 -07001676 if (pixel_mem > dev_priv->prim_bb_mem) {
1677 DRM_ERROR("Combined output size too large\n");
1678 ret = -EINVAL;
1679 goto out_free;
1680 }
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001681 }
1682
1683 vmw_du_update_layout(dev_priv, arg->num_outputs, rects);
1684
1685out_free:
1686 kfree(rects);
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001687 return ret;
1688}
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07001689
1690/**
1691 * vmw_kms_helper_dirty - Helper to build commands and perform actions based
1692 * on a set of cliprects and a set of display units.
1693 *
1694 * @dev_priv: Pointer to a device private structure.
1695 * @framebuffer: Pointer to the framebuffer on which to perform the actions.
1696 * @clips: A set of struct drm_clip_rect. Either this os @vclips must be NULL.
1697 * Cliprects are given in framebuffer coordinates.
1698 * @vclips: A set of struct drm_vmw_rect cliprects. Either this or @clips must
1699 * be NULL. Cliprects are given in source coordinates.
1700 * @dest_x: X coordinate offset for the crtc / destination clip rects.
1701 * @dest_y: Y coordinate offset for the crtc / destination clip rects.
1702 * @num_clips: Number of cliprects in the @clips or @vclips array.
1703 * @increment: Integer with which to increment the clip counter when looping.
1704 * Used to skip a predetermined number of clip rects.
1705 * @dirty: Closure structure. See the description of struct vmw_kms_dirty.
1706 */
1707int vmw_kms_helper_dirty(struct vmw_private *dev_priv,
1708 struct vmw_framebuffer *framebuffer,
1709 const struct drm_clip_rect *clips,
1710 const struct drm_vmw_rect *vclips,
1711 s32 dest_x, s32 dest_y,
1712 int num_clips,
1713 int increment,
1714 struct vmw_kms_dirty *dirty)
1715{
1716 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
1717 struct drm_crtc *crtc;
1718 u32 num_units = 0;
1719 u32 i, k;
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07001720
1721 dirty->dev_priv = dev_priv;
1722
1723 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
1724 if (crtc->primary->fb != &framebuffer->base)
1725 continue;
1726 units[num_units++] = vmw_crtc_to_du(crtc);
1727 }
1728
1729 for (k = 0; k < num_units; k++) {
1730 struct vmw_display_unit *unit = units[k];
1731 s32 crtc_x = unit->crtc.x;
1732 s32 crtc_y = unit->crtc.y;
1733 s32 crtc_width = unit->crtc.mode.hdisplay;
1734 s32 crtc_height = unit->crtc.mode.vdisplay;
1735 const struct drm_clip_rect *clips_ptr = clips;
1736 const struct drm_vmw_rect *vclips_ptr = vclips;
1737
1738 dirty->unit = unit;
1739 if (dirty->fifo_reserve_size > 0) {
1740 dirty->cmd = vmw_fifo_reserve(dev_priv,
1741 dirty->fifo_reserve_size);
1742 if (!dirty->cmd) {
1743 DRM_ERROR("Couldn't reserve fifo space "
1744 "for dirty blits.\n");
Christian Engelmayerf3b8c0c2015-09-19 00:32:24 +02001745 return -ENOMEM;
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07001746 }
1747 memset(dirty->cmd, 0, dirty->fifo_reserve_size);
1748 }
1749 dirty->num_hits = 0;
1750 for (i = 0; i < num_clips; i++, clips_ptr += increment,
1751 vclips_ptr += increment) {
1752 s32 clip_left;
1753 s32 clip_top;
1754
1755 /*
1756 * Select clip array type. Note that integer type
1757 * in @clips is unsigned short, whereas in @vclips
1758 * it's 32-bit.
1759 */
1760 if (clips) {
1761 dirty->fb_x = (s32) clips_ptr->x1;
1762 dirty->fb_y = (s32) clips_ptr->y1;
1763 dirty->unit_x2 = (s32) clips_ptr->x2 + dest_x -
1764 crtc_x;
1765 dirty->unit_y2 = (s32) clips_ptr->y2 + dest_y -
1766 crtc_y;
1767 } else {
1768 dirty->fb_x = vclips_ptr->x;
1769 dirty->fb_y = vclips_ptr->y;
1770 dirty->unit_x2 = dirty->fb_x + vclips_ptr->w +
1771 dest_x - crtc_x;
1772 dirty->unit_y2 = dirty->fb_y + vclips_ptr->h +
1773 dest_y - crtc_y;
1774 }
1775
1776 dirty->unit_x1 = dirty->fb_x + dest_x - crtc_x;
1777 dirty->unit_y1 = dirty->fb_y + dest_y - crtc_y;
1778
1779 /* Skip this clip if it's outside the crtc region */
1780 if (dirty->unit_x1 >= crtc_width ||
1781 dirty->unit_y1 >= crtc_height ||
1782 dirty->unit_x2 <= 0 || dirty->unit_y2 <= 0)
1783 continue;
1784
1785 /* Clip right and bottom to crtc limits */
1786 dirty->unit_x2 = min_t(s32, dirty->unit_x2,
1787 crtc_width);
1788 dirty->unit_y2 = min_t(s32, dirty->unit_y2,
1789 crtc_height);
1790
1791 /* Clip left and top to crtc limits */
1792 clip_left = min_t(s32, dirty->unit_x1, 0);
1793 clip_top = min_t(s32, dirty->unit_y1, 0);
1794 dirty->unit_x1 -= clip_left;
1795 dirty->unit_y1 -= clip_top;
1796 dirty->fb_x -= clip_left;
1797 dirty->fb_y -= clip_top;
1798
1799 dirty->clip(dirty);
1800 }
1801
1802 dirty->fifo_commit(dirty);
1803 }
1804
1805 return 0;
1806}
1807
1808/**
1809 * vmw_kms_helper_buffer_prepare - Reserve and validate a buffer object before
1810 * command submission.
1811 *
1812 * @dev_priv. Pointer to a device private structure.
1813 * @buf: The buffer object
1814 * @interruptible: Whether to perform waits as interruptible.
1815 * @validate_as_mob: Whether the buffer should be validated as a MOB. If false,
1816 * The buffer will be validated as a GMR. Already pinned buffers will not be
1817 * validated.
1818 *
1819 * Returns 0 on success, negative error code on failure, -ERESTARTSYS if
1820 * interrupted by a signal.
1821 */
1822int vmw_kms_helper_buffer_prepare(struct vmw_private *dev_priv,
1823 struct vmw_dma_buffer *buf,
1824 bool interruptible,
1825 bool validate_as_mob)
1826{
1827 struct ttm_buffer_object *bo = &buf->base;
1828 int ret;
1829
Christian Königdfd5e502016-04-06 11:12:03 +02001830 ttm_bo_reserve(bo, false, false, NULL);
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07001831 ret = vmw_validate_single_buffer(dev_priv, bo, interruptible,
1832 validate_as_mob);
1833 if (ret)
1834 ttm_bo_unreserve(bo);
1835
1836 return ret;
1837}
1838
1839/**
1840 * vmw_kms_helper_buffer_revert - Undo the actions of
1841 * vmw_kms_helper_buffer_prepare.
1842 *
1843 * @res: Pointer to the buffer object.
1844 *
1845 * Helper to be used if an error forces the caller to undo the actions of
1846 * vmw_kms_helper_buffer_prepare.
1847 */
1848void vmw_kms_helper_buffer_revert(struct vmw_dma_buffer *buf)
1849{
1850 if (buf)
1851 ttm_bo_unreserve(&buf->base);
1852}
1853
1854/**
1855 * vmw_kms_helper_buffer_finish - Unreserve and fence a buffer object after
1856 * kms command submission.
1857 *
1858 * @dev_priv: Pointer to a device private structure.
1859 * @file_priv: Pointer to a struct drm_file representing the caller's
1860 * connection. Must be set to NULL if @user_fence_rep is NULL, and conversely
1861 * if non-NULL, @user_fence_rep must be non-NULL.
1862 * @buf: The buffer object.
1863 * @out_fence: Optional pointer to a fence pointer. If non-NULL, a
1864 * ref-counted fence pointer is returned here.
1865 * @user_fence_rep: Optional pointer to a user-space provided struct
1866 * drm_vmw_fence_rep. If provided, @file_priv must also be provided and the
1867 * function copies fence data to user-space in a fail-safe manner.
1868 */
1869void vmw_kms_helper_buffer_finish(struct vmw_private *dev_priv,
1870 struct drm_file *file_priv,
1871 struct vmw_dma_buffer *buf,
1872 struct vmw_fence_obj **out_fence,
1873 struct drm_vmw_fence_rep __user *
1874 user_fence_rep)
1875{
1876 struct vmw_fence_obj *fence;
1877 uint32_t handle;
1878 int ret;
1879
1880 ret = vmw_execbuf_fence_commands(file_priv, dev_priv, &fence,
1881 file_priv ? &handle : NULL);
1882 if (buf)
1883 vmw_fence_single_bo(&buf->base, fence);
1884 if (file_priv)
1885 vmw_execbuf_copy_fence_user(dev_priv, vmw_fpriv(file_priv),
1886 ret, user_fence_rep, fence,
1887 handle);
1888 if (out_fence)
1889 *out_fence = fence;
1890 else
1891 vmw_fence_obj_unreference(&fence);
1892
1893 vmw_kms_helper_buffer_revert(buf);
1894}
1895
1896
1897/**
1898 * vmw_kms_helper_resource_revert - Undo the actions of
1899 * vmw_kms_helper_resource_prepare.
1900 *
1901 * @res: Pointer to the resource. Typically a surface.
1902 *
1903 * Helper to be used if an error forces the caller to undo the actions of
1904 * vmw_kms_helper_resource_prepare.
1905 */
1906void vmw_kms_helper_resource_revert(struct vmw_resource *res)
1907{
1908 vmw_kms_helper_buffer_revert(res->backup);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001909 vmw_resource_unreserve(res, false, NULL, 0);
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07001910 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
1911}
1912
1913/**
1914 * vmw_kms_helper_resource_prepare - Reserve and validate a resource before
1915 * command submission.
1916 *
1917 * @res: Pointer to the resource. Typically a surface.
1918 * @interruptible: Whether to perform waits as interruptible.
1919 *
1920 * Reserves and validates also the backup buffer if a guest-backed resource.
1921 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
1922 * interrupted by a signal.
1923 */
1924int vmw_kms_helper_resource_prepare(struct vmw_resource *res,
1925 bool interruptible)
1926{
1927 int ret = 0;
1928
1929 if (interruptible)
1930 ret = mutex_lock_interruptible(&res->dev_priv->cmdbuf_mutex);
1931 else
1932 mutex_lock(&res->dev_priv->cmdbuf_mutex);
1933
1934 if (unlikely(ret != 0))
1935 return -ERESTARTSYS;
1936
1937 ret = vmw_resource_reserve(res, interruptible, false);
1938 if (ret)
1939 goto out_unlock;
1940
1941 if (res->backup) {
1942 ret = vmw_kms_helper_buffer_prepare(res->dev_priv, res->backup,
1943 interruptible,
1944 res->dev_priv->has_mob);
1945 if (ret)
1946 goto out_unreserve;
1947 }
1948 ret = vmw_resource_validate(res);
1949 if (ret)
1950 goto out_revert;
1951 return 0;
1952
1953out_revert:
1954 vmw_kms_helper_buffer_revert(res->backup);
1955out_unreserve:
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001956 vmw_resource_unreserve(res, false, NULL, 0);
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07001957out_unlock:
1958 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
1959 return ret;
1960}
1961
1962/**
1963 * vmw_kms_helper_resource_finish - Unreserve and fence a resource after
1964 * kms command submission.
1965 *
1966 * @res: Pointer to the resource. Typically a surface.
1967 * @out_fence: Optional pointer to a fence pointer. If non-NULL, a
1968 * ref-counted fence pointer is returned here.
1969 */
1970void vmw_kms_helper_resource_finish(struct vmw_resource *res,
1971 struct vmw_fence_obj **out_fence)
1972{
1973 if (res->backup || out_fence)
1974 vmw_kms_helper_buffer_finish(res->dev_priv, NULL, res->backup,
1975 out_fence, NULL);
1976
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001977 vmw_resource_unreserve(res, false, NULL, 0);
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07001978 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
1979}
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001980
1981/**
1982 * vmw_kms_update_proxy - Helper function to update a proxy surface from
1983 * its backing MOB.
1984 *
1985 * @res: Pointer to the surface resource
1986 * @clips: Clip rects in framebuffer (surface) space.
1987 * @num_clips: Number of clips in @clips.
1988 * @increment: Integer with which to increment the clip counter when looping.
1989 * Used to skip a predetermined number of clip rects.
1990 *
1991 * This function makes sure the proxy surface is updated from its backing MOB
1992 * using the region given by @clips. The surface resource @res and its backing
1993 * MOB needs to be reserved and validated on call.
1994 */
1995int vmw_kms_update_proxy(struct vmw_resource *res,
1996 const struct drm_clip_rect *clips,
1997 unsigned num_clips,
1998 int increment)
1999{
2000 struct vmw_private *dev_priv = res->dev_priv;
2001 struct drm_vmw_size *size = &vmw_res_to_srf(res)->base_size;
2002 struct {
2003 SVGA3dCmdHeader header;
2004 SVGA3dCmdUpdateGBImage body;
2005 } *cmd;
2006 SVGA3dBox *box;
2007 size_t copy_size = 0;
2008 int i;
2009
2010 if (!clips)
2011 return 0;
2012
2013 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd) * num_clips);
2014 if (!cmd) {
2015 DRM_ERROR("Couldn't reserve fifo space for proxy surface "
2016 "update.\n");
2017 return -ENOMEM;
2018 }
2019
2020 for (i = 0; i < num_clips; ++i, clips += increment, ++cmd) {
2021 box = &cmd->body.box;
2022
2023 cmd->header.id = SVGA_3D_CMD_UPDATE_GB_IMAGE;
2024 cmd->header.size = sizeof(cmd->body);
2025 cmd->body.image.sid = res->id;
2026 cmd->body.image.face = 0;
2027 cmd->body.image.mipmap = 0;
2028
2029 if (clips->x1 > size->width || clips->x2 > size->width ||
2030 clips->y1 > size->height || clips->y2 > size->height) {
2031 DRM_ERROR("Invalid clips outsize of framebuffer.\n");
2032 return -EINVAL;
2033 }
2034
2035 box->x = clips->x1;
2036 box->y = clips->y1;
2037 box->z = 0;
2038 box->w = clips->x2 - clips->x1;
2039 box->h = clips->y2 - clips->y1;
2040 box->d = 1;
2041
2042 copy_size += sizeof(*cmd);
2043 }
2044
2045 vmw_fifo_commit(dev_priv, copy_size);
2046
2047 return 0;
2048}
Thomas Hellstroma2787242015-06-29 12:55:07 -07002049
2050int vmw_kms_fbdev_init_data(struct vmw_private *dev_priv,
2051 unsigned unit,
2052 u32 max_width,
2053 u32 max_height,
2054 struct drm_connector **p_con,
2055 struct drm_crtc **p_crtc,
2056 struct drm_display_mode **p_mode)
2057{
2058 struct drm_connector *con;
2059 struct vmw_display_unit *du;
2060 struct drm_display_mode *mode;
2061 int i = 0;
2062
2063 list_for_each_entry(con, &dev_priv->dev->mode_config.connector_list,
2064 head) {
2065 if (i == unit)
2066 break;
2067
2068 ++i;
2069 }
2070
2071 if (i != unit) {
2072 DRM_ERROR("Could not find initial display unit.\n");
2073 return -EINVAL;
2074 }
2075
2076 if (list_empty(&con->modes))
2077 (void) vmw_du_connector_fill_modes(con, max_width, max_height);
2078
2079 if (list_empty(&con->modes)) {
2080 DRM_ERROR("Could not find initial display mode.\n");
2081 return -EINVAL;
2082 }
2083
2084 du = vmw_connector_to_du(con);
2085 *p_con = con;
2086 *p_crtc = &du->crtc;
2087
2088 list_for_each_entry(mode, &con->modes, head) {
2089 if (mode->type & DRM_MODE_TYPE_PREFERRED)
2090 break;
2091 }
2092
2093 if (mode->type & DRM_MODE_TYPE_PREFERRED)
2094 *p_mode = mode;
2095 else {
2096 WARN_ONCE(true, "Could not find initial preferred mode.\n");
2097 *p_mode = list_first_entry(&con->modes,
2098 struct drm_display_mode,
2099 head);
2100 }
2101
2102 return 0;
2103}
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002104
2105/**
2106 * vmw_kms_del_active - unregister a crtc binding to the implicit framebuffer
2107 *
2108 * @dev_priv: Pointer to a device private struct.
2109 * @du: The display unit of the crtc.
2110 */
2111void vmw_kms_del_active(struct vmw_private *dev_priv,
2112 struct vmw_display_unit *du)
2113{
Thomas Hellstrom93cd1682016-05-03 11:24:35 +02002114 mutex_lock(&dev_priv->global_kms_state_mutex);
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002115 if (du->active_implicit) {
2116 if (--(dev_priv->num_implicit) == 0)
2117 dev_priv->implicit_fb = NULL;
2118 du->active_implicit = false;
2119 }
Thomas Hellstrom93cd1682016-05-03 11:24:35 +02002120 mutex_unlock(&dev_priv->global_kms_state_mutex);
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002121}
2122
2123/**
2124 * vmw_kms_add_active - register a crtc binding to an implicit framebuffer
2125 *
2126 * @vmw_priv: Pointer to a device private struct.
2127 * @du: The display unit of the crtc.
2128 * @vfb: The implicit framebuffer
2129 *
2130 * Registers a binding to an implicit framebuffer.
2131 */
2132void vmw_kms_add_active(struct vmw_private *dev_priv,
2133 struct vmw_display_unit *du,
2134 struct vmw_framebuffer *vfb)
2135{
Thomas Hellstrom93cd1682016-05-03 11:24:35 +02002136 mutex_lock(&dev_priv->global_kms_state_mutex);
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002137 WARN_ON_ONCE(!dev_priv->num_implicit && dev_priv->implicit_fb);
2138
2139 if (!du->active_implicit && du->is_implicit) {
2140 dev_priv->implicit_fb = vfb;
2141 du->active_implicit = true;
2142 dev_priv->num_implicit++;
2143 }
Thomas Hellstrom93cd1682016-05-03 11:24:35 +02002144 mutex_unlock(&dev_priv->global_kms_state_mutex);
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002145}
2146
2147/**
2148 * vmw_kms_screen_object_flippable - Check whether we can page-flip a crtc.
2149 *
2150 * @dev_priv: Pointer to device-private struct.
2151 * @crtc: The crtc we want to flip.
2152 *
2153 * Returns true or false depending whether it's OK to flip this crtc
2154 * based on the criterion that we must not have more than one implicit
2155 * frame-buffer at any one time.
2156 */
2157bool vmw_kms_crtc_flippable(struct vmw_private *dev_priv,
2158 struct drm_crtc *crtc)
2159{
2160 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
Thomas Hellstrom93cd1682016-05-03 11:24:35 +02002161 bool ret;
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002162
Thomas Hellstrom93cd1682016-05-03 11:24:35 +02002163 mutex_lock(&dev_priv->global_kms_state_mutex);
2164 ret = !du->is_implicit || dev_priv->num_implicit == 1;
2165 mutex_unlock(&dev_priv->global_kms_state_mutex);
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002166
Thomas Hellstrom93cd1682016-05-03 11:24:35 +02002167 return ret;
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002168}
2169
2170/**
2171 * vmw_kms_update_implicit_fb - Update the implicit fb.
2172 *
2173 * @dev_priv: Pointer to device-private struct.
2174 * @crtc: The crtc the new implicit frame-buffer is bound to.
2175 */
2176void vmw_kms_update_implicit_fb(struct vmw_private *dev_priv,
2177 struct drm_crtc *crtc)
2178{
2179 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
2180 struct vmw_framebuffer *vfb;
2181
Thomas Hellstrom93cd1682016-05-03 11:24:35 +02002182 mutex_lock(&dev_priv->global_kms_state_mutex);
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002183
2184 if (!du->is_implicit)
Thomas Hellstrom93cd1682016-05-03 11:24:35 +02002185 goto out_unlock;
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002186
2187 vfb = vmw_framebuffer_to_vfb(crtc->primary->fb);
2188 WARN_ON_ONCE(dev_priv->num_implicit != 1 &&
2189 dev_priv->implicit_fb != vfb);
2190
2191 dev_priv->implicit_fb = vfb;
Thomas Hellstrom93cd1682016-05-03 11:24:35 +02002192out_unlock:
2193 mutex_unlock(&dev_priv->global_kms_state_mutex);
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002194}
Thomas Hellstrom76404ac2016-02-12 09:55:45 +01002195
2196/**
2197 * vmw_kms_create_implicit_placement_proparty - Set up the implicit placement
2198 * property.
2199 *
2200 * @dev_priv: Pointer to a device private struct.
2201 * @immutable: Whether the property is immutable.
2202 *
2203 * Sets up the implicit placement property unless it's already set up.
2204 */
2205void
2206vmw_kms_create_implicit_placement_property(struct vmw_private *dev_priv,
2207 bool immutable)
2208{
2209 if (dev_priv->implicit_placement_property)
2210 return;
2211
2212 dev_priv->implicit_placement_property =
2213 drm_property_create_range(dev_priv->dev,
2214 immutable ?
2215 DRM_MODE_PROP_IMMUTABLE : 0,
2216 "implicit_placement", 0, 1);
2217
2218}