blob: 3c824fd7cbf36d64e72e758bdb7c5b5e3b270dd6 [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
34/* Might need a hrtimer here? */
35#define VMWGFX_PRESENT_RATE ((HZ / 60 > 0) ? HZ / 60 : 1)
36
Sinclair Yehc8261a92015-06-26 01:23:42 -070037void vmw_du_cleanup(struct vmw_display_unit *du)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000038{
Sinclair Yeh36cc79b2017-03-23 11:28:11 -070039 drm_plane_cleanup(&du->primary);
40 drm_plane_cleanup(&du->cursor);
41
Thomas Wood34ea3d32014-05-29 16:57:41 +010042 drm_connector_unregister(&du->connector);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000043 drm_crtc_cleanup(&du->crtc);
44 drm_encoder_cleanup(&du->encoder);
45 drm_connector_cleanup(&du->connector);
46}
47
48/*
49 * Display Unit Cursor functions
50 */
51
Sinclair Yeh36cc79b2017-03-23 11:28:11 -070052static int vmw_cursor_update_image(struct vmw_private *dev_priv,
53 u32 *image, u32 width, u32 height,
54 u32 hotspotX, u32 hotspotY)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000055{
56 struct {
57 u32 cmd;
58 SVGAFifoCmdDefineAlphaCursor cursor;
59 } *cmd;
60 u32 image_size = width * height * 4;
61 u32 cmd_size = sizeof(*cmd) + image_size;
62
63 if (!image)
64 return -EINVAL;
65
66 cmd = vmw_fifo_reserve(dev_priv, cmd_size);
67 if (unlikely(cmd == NULL)) {
68 DRM_ERROR("Fifo reserve failed.\n");
69 return -ENOMEM;
70 }
71
72 memset(cmd, 0, sizeof(*cmd));
73
74 memcpy(&cmd[1], image, image_size);
75
Thomas Hellstromb9eb1a62015-04-02 02:39:45 -070076 cmd->cmd = SVGA_CMD_DEFINE_ALPHA_CURSOR;
77 cmd->cursor.id = 0;
78 cmd->cursor.width = width;
79 cmd->cursor.height = height;
80 cmd->cursor.hotspotX = hotspotX;
81 cmd->cursor.hotspotY = hotspotY;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000082
Thomas Hellstrom4e0858a2015-11-05 02:18:55 -080083 vmw_fifo_commit_flush(dev_priv, cmd_size);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000084
85 return 0;
86}
87
Sinclair Yeh36cc79b2017-03-23 11:28:11 -070088static int vmw_cursor_update_dmabuf(struct vmw_private *dev_priv,
89 struct vmw_dma_buffer *dmabuf,
90 u32 width, u32 height,
91 u32 hotspotX, u32 hotspotY)
Jakob Bornecrantz6a91d972011-11-28 13:19:10 +010092{
93 struct ttm_bo_kmap_obj map;
94 unsigned long kmap_offset;
95 unsigned long kmap_num;
96 void *virtual;
97 bool dummy;
98 int ret;
99
100 kmap_offset = 0;
101 kmap_num = (width*height*4 + PAGE_SIZE - 1) >> PAGE_SHIFT;
102
Christian Königdfd5e502016-04-06 11:12:03 +0200103 ret = ttm_bo_reserve(&dmabuf->base, true, false, NULL);
Jakob Bornecrantz6a91d972011-11-28 13:19:10 +0100104 if (unlikely(ret != 0)) {
105 DRM_ERROR("reserve failed\n");
106 return -EINVAL;
107 }
108
109 ret = ttm_bo_kmap(&dmabuf->base, kmap_offset, kmap_num, &map);
110 if (unlikely(ret != 0))
111 goto err_unreserve;
112
113 virtual = ttm_kmap_obj_virtual(&map, &dummy);
114 ret = vmw_cursor_update_image(dev_priv, virtual, width, height,
115 hotspotX, hotspotY);
116
117 ttm_bo_kunmap(&map);
118err_unreserve:
119 ttm_bo_unreserve(&dmabuf->base);
120
121 return ret;
122}
123
124
Sinclair Yeh36cc79b2017-03-23 11:28:11 -0700125static void vmw_cursor_update_position(struct vmw_private *dev_priv,
126 bool show, int x, int y)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000127{
Thomas Hellstromb76ff5e2015-10-28 10:44:04 +0100128 u32 *fifo_mem = dev_priv->mmio_virt;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000129 uint32_t count;
130
Sinclair Yeh36cc79b2017-03-23 11:28:11 -0700131 spin_lock(&dev_priv->cursor_lock);
Thomas Hellstromb76ff5e2015-10-28 10:44:04 +0100132 vmw_mmio_write(show ? 1 : 0, fifo_mem + SVGA_FIFO_CURSOR_ON);
133 vmw_mmio_write(x, fifo_mem + SVGA_FIFO_CURSOR_X);
134 vmw_mmio_write(y, fifo_mem + SVGA_FIFO_CURSOR_Y);
135 count = vmw_mmio_read(fifo_mem + SVGA_FIFO_CURSOR_COUNT);
136 vmw_mmio_write(++count, fifo_mem + SVGA_FIFO_CURSOR_COUNT);
Sinclair Yeh36cc79b2017-03-23 11:28:11 -0700137 spin_unlock(&dev_priv->cursor_lock);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000138}
139
Thomas Hellstrom8fbf9d92015-11-26 19:45:16 +0100140
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000141void vmw_kms_cursor_snoop(struct vmw_surface *srf,
142 struct ttm_object_file *tfile,
143 struct ttm_buffer_object *bo,
144 SVGA3dCmdHeader *header)
145{
146 struct ttm_bo_kmap_obj map;
147 unsigned long kmap_offset;
148 unsigned long kmap_num;
149 SVGA3dCopyBox *box;
150 unsigned box_count;
151 void *virtual;
152 bool dummy;
153 struct vmw_dma_cmd {
154 SVGA3dCmdHeader header;
155 SVGA3dCmdSurfaceDMA dma;
156 } *cmd;
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100157 int i, ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000158
159 cmd = container_of(header, struct vmw_dma_cmd, header);
160
161 /* No snooper installed */
162 if (!srf->snooper.image)
163 return;
164
165 if (cmd->dma.host.face != 0 || cmd->dma.host.mipmap != 0) {
166 DRM_ERROR("face and mipmap for cursors should never != 0\n");
167 return;
168 }
169
170 if (cmd->header.size < 64) {
171 DRM_ERROR("at least one full copy box must be given\n");
172 return;
173 }
174
175 box = (SVGA3dCopyBox *)&cmd[1];
176 box_count = (cmd->header.size - sizeof(SVGA3dCmdSurfaceDMA)) /
177 sizeof(SVGA3dCopyBox);
178
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100179 if (cmd->dma.guest.ptr.offset % PAGE_SIZE ||
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000180 box->x != 0 || box->y != 0 || box->z != 0 ||
181 box->srcx != 0 || box->srcy != 0 || box->srcz != 0 ||
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100182 box->d != 1 || box_count != 1) {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000183 /* TODO handle none page aligned offsets */
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100184 /* TODO handle more dst & src != 0 */
185 /* TODO handle more then one copy */
186 DRM_ERROR("Cant snoop dma request for cursor!\n");
187 DRM_ERROR("(%u, %u, %u) (%u, %u, %u) (%ux%ux%u) %u %u\n",
188 box->srcx, box->srcy, box->srcz,
189 box->x, box->y, box->z,
190 box->w, box->h, box->d, box_count,
191 cmd->dma.guest.ptr.offset);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000192 return;
193 }
194
195 kmap_offset = cmd->dma.guest.ptr.offset >> PAGE_SHIFT;
196 kmap_num = (64*64*4) >> PAGE_SHIFT;
197
Christian Königdfd5e502016-04-06 11:12:03 +0200198 ret = ttm_bo_reserve(bo, true, false, NULL);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000199 if (unlikely(ret != 0)) {
200 DRM_ERROR("reserve failed\n");
201 return;
202 }
203
204 ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
205 if (unlikely(ret != 0))
206 goto err_unreserve;
207
208 virtual = ttm_kmap_obj_virtual(&map, &dummy);
209
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100210 if (box->w == 64 && cmd->dma.guest.pitch == 64*4) {
211 memcpy(srf->snooper.image, virtual, 64*64*4);
212 } else {
213 /* Image is unsigned pointer. */
214 for (i = 0; i < box->h; i++)
215 memcpy(srf->snooper.image + i * 64,
216 virtual + i * cmd->dma.guest.pitch,
217 box->w * 4);
218 }
219
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000220 srf->snooper.age++;
221
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000222 ttm_bo_kunmap(&map);
223err_unreserve:
224 ttm_bo_unreserve(bo);
225}
226
Thomas Hellstrom8fbf9d92015-11-26 19:45:16 +0100227/**
228 * vmw_kms_legacy_hotspot_clear - Clear legacy hotspots
229 *
230 * @dev_priv: Pointer to the device private struct.
231 *
232 * Clears all legacy hotspots.
233 */
234void vmw_kms_legacy_hotspot_clear(struct vmw_private *dev_priv)
235{
236 struct drm_device *dev = dev_priv->dev;
237 struct vmw_display_unit *du;
238 struct drm_crtc *crtc;
239
240 drm_modeset_lock_all(dev);
241 drm_for_each_crtc(crtc, dev) {
242 du = vmw_crtc_to_du(crtc);
243
244 du->hotspot_x = 0;
245 du->hotspot_y = 0;
246 }
247 drm_modeset_unlock_all(dev);
248}
249
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000250void vmw_kms_cursor_post_execbuf(struct vmw_private *dev_priv)
251{
252 struct drm_device *dev = dev_priv->dev;
253 struct vmw_display_unit *du;
254 struct drm_crtc *crtc;
255
256 mutex_lock(&dev->mode_config.mutex);
257
258 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
259 du = vmw_crtc_to_du(crtc);
260 if (!du->cursor_surface ||
261 du->cursor_age == du->cursor_surface->snooper.age)
262 continue;
263
264 du->cursor_age = du->cursor_surface->snooper.age;
265 vmw_cursor_update_image(dev_priv,
266 du->cursor_surface->snooper.image,
Thomas Hellstrom8fbf9d92015-11-26 19:45:16 +0100267 64, 64,
268 du->hotspot_x + du->core_hotspot_x,
269 du->hotspot_y + du->core_hotspot_y);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000270 }
271
272 mutex_unlock(&dev->mode_config.mutex);
273}
274
Sinclair Yeh36cc79b2017-03-23 11:28:11 -0700275
Sinclair Yeh36cc79b2017-03-23 11:28:11 -0700276void vmw_du_cursor_plane_destroy(struct drm_plane *plane)
277{
278 vmw_cursor_update_position(plane->dev->dev_private, false, 0, 0);
279
280 drm_plane_cleanup(plane);
281}
282
283
284void vmw_du_primary_plane_destroy(struct drm_plane *plane)
285{
286 drm_plane_cleanup(plane);
287
288 /* Planes are static in our case so we don't free it */
289}
290
291
Sinclair Yeh060e2ad2017-03-23 14:18:32 -0700292/**
293 * vmw_du_vps_unpin_surf - unpins resource associated with a framebuffer surface
294 *
295 * @vps: plane state associated with the display surface
296 * @unreference: true if we also want to unreference the display.
297 */
298void vmw_du_plane_unpin_surf(struct vmw_plane_state *vps,
299 bool unreference)
300{
301 if (vps->surf) {
302 if (vps->pinned) {
303 vmw_resource_unpin(&vps->surf->res);
304 vps->pinned--;
305 }
306
307 if (unreference) {
308 if (vps->pinned)
309 DRM_ERROR("Surface still pinned\n");
310 vmw_surface_unreference(&vps->surf);
311 }
312 }
313}
314
315
316/**
317 * vmw_du_plane_cleanup_fb - Unpins the cursor
318 *
319 * @plane: display plane
320 * @old_state: Contains the FB to clean up
321 *
322 * Unpins the framebuffer surface
323 *
324 * Returns 0 on success
325 */
326void
327vmw_du_plane_cleanup_fb(struct drm_plane *plane,
328 struct drm_plane_state *old_state)
329{
330 struct vmw_plane_state *vps = vmw_plane_state_to_vps(old_state);
331
332 vmw_du_plane_unpin_surf(vps, false);
333}
334
335
336/**
337 * vmw_du_cursor_plane_prepare_fb - Readies the cursor by referencing it
338 *
339 * @plane: display plane
340 * @new_state: info on the new plane state, including the FB
341 *
342 * Returns 0 on success
343 */
344int
345vmw_du_cursor_plane_prepare_fb(struct drm_plane *plane,
346 struct drm_plane_state *new_state)
347{
348 struct drm_framebuffer *fb = new_state->fb;
349 struct vmw_plane_state *vps = vmw_plane_state_to_vps(new_state);
350
351
352 if (vps->surf)
353 vmw_surface_unreference(&vps->surf);
354
355 if (vps->dmabuf)
356 vmw_dmabuf_unreference(&vps->dmabuf);
357
358 if (fb) {
359 if (vmw_framebuffer_to_vfb(fb)->dmabuf) {
360 vps->dmabuf = vmw_framebuffer_to_vfbd(fb)->buffer;
361 vmw_dmabuf_reference(vps->dmabuf);
362 } else {
363 vps->surf = vmw_framebuffer_to_vfbs(fb)->surface;
364 vmw_surface_reference(vps->surf);
365 }
366 }
367
368 return 0;
369}
370
371
372void
Sinclair Yeh060e2ad2017-03-23 14:18:32 -0700373vmw_du_cursor_plane_atomic_update(struct drm_plane *plane,
374 struct drm_plane_state *old_state)
375{
376 struct drm_crtc *crtc = plane->state->crtc ?: old_state->crtc;
377 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
378 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
379 struct vmw_plane_state *vps = vmw_plane_state_to_vps(plane->state);
380 s32 hotspot_x, hotspot_y;
381 int ret = 0;
382
383
384 hotspot_x = du->hotspot_x;
385 hotspot_y = du->hotspot_y;
Sinclair Yeh14979ad2017-07-17 23:26:21 -0700386
387 if (plane->fb) {
388 hotspot_x += plane->fb->hot_x;
389 hotspot_y += plane->fb->hot_y;
390 }
391
Sinclair Yeh060e2ad2017-03-23 14:18:32 -0700392 du->cursor_surface = vps->surf;
393 du->cursor_dmabuf = vps->dmabuf;
394
395 /* setup new image */
396 if (vps->surf) {
397 du->cursor_age = du->cursor_surface->snooper.age;
398
399 ret = vmw_cursor_update_image(dev_priv,
400 vps->surf->snooper.image,
401 64, 64, hotspot_x, hotspot_y);
402 } else if (vps->dmabuf) {
403 ret = vmw_cursor_update_dmabuf(dev_priv, vps->dmabuf,
404 plane->state->crtc_w,
405 plane->state->crtc_h,
406 hotspot_x, hotspot_y);
407 } else {
408 vmw_cursor_update_position(dev_priv, false, 0, 0);
409 return;
410 }
411
412 if (!ret) {
413 du->cursor_x = plane->state->crtc_x + du->set_gui_x;
414 du->cursor_y = plane->state->crtc_y + du->set_gui_y;
415
416 vmw_cursor_update_position(dev_priv, true,
417 du->cursor_x + hotspot_x,
418 du->cursor_y + hotspot_y);
Sinclair Yeh14979ad2017-07-17 23:26:21 -0700419
420 du->core_hotspot_x = hotspot_x - du->hotspot_x;
421 du->core_hotspot_y = hotspot_y - du->hotspot_y;
Sinclair Yeh060e2ad2017-03-23 14:18:32 -0700422 } else {
423 DRM_ERROR("Failed to update cursor image\n");
424 }
425}
426
427
428/**
429 * vmw_du_primary_plane_atomic_check - check if the new state is okay
430 *
431 * @plane: display plane
432 * @state: info on the new plane state, including the FB
433 *
434 * Check if the new state is settable given the current state. Other
435 * than what the atomic helper checks, we care about crtc fitting
436 * the FB and maintaining one active framebuffer.
437 *
438 * Returns 0 on success
439 */
440int vmw_du_primary_plane_atomic_check(struct drm_plane *plane,
441 struct drm_plane_state *state)
442{
Ville Syrjälä58a275aa92017-11-01 20:29:18 +0200443 struct drm_crtc_state *crtc_state = NULL;
Sinclair Yeh060e2ad2017-03-23 14:18:32 -0700444 struct drm_framebuffer *new_fb = state->fb;
Ville Syrjälä58a275aa92017-11-01 20:29:18 +0200445 struct drm_rect clip = {};
Sinclair Yeh060e2ad2017-03-23 14:18:32 -0700446 int ret;
447
Ville Syrjälä58a275aa92017-11-01 20:29:18 +0200448 if (state->crtc)
449 crtc_state = drm_atomic_get_new_crtc_state(state->state, state->crtc);
Sinclair Yeh060e2ad2017-03-23 14:18:32 -0700450
Ville Syrjälä58a275aa92017-11-01 20:29:18 +0200451 if (crtc_state && crtc_state->enable) {
452 clip.x2 = crtc_state->adjusted_mode.hdisplay;
453 clip.y2 = crtc_state->adjusted_mode.vdisplay;
454 }
455
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +0200456 ret = drm_atomic_helper_check_plane_state(state, crtc_state, &clip,
457 DRM_PLANE_HELPER_NO_SCALING,
458 DRM_PLANE_HELPER_NO_SCALING,
459 false, true);
Sinclair Yeh060e2ad2017-03-23 14:18:32 -0700460
461 if (!ret && new_fb) {
462 struct drm_crtc *crtc = state->crtc;
463 struct vmw_connector_state *vcs;
464 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
465 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
466 struct vmw_framebuffer *vfb = vmw_framebuffer_to_vfb(new_fb);
467
468 vcs = vmw_connector_state_to_vcs(du->connector.state);
469
Sinclair Yeh060e2ad2017-03-23 14:18:32 -0700470 /* Only one active implicit framebuffer at a time. */
471 mutex_lock(&dev_priv->global_kms_state_mutex);
472 if (vcs->is_implicit && dev_priv->implicit_fb &&
473 !(dev_priv->num_implicit == 1 && du->active_implicit)
474 && dev_priv->implicit_fb != vfb) {
475 DRM_ERROR("Multiple implicit framebuffers "
476 "not supported.\n");
477 ret = -EINVAL;
478 }
479 mutex_unlock(&dev_priv->global_kms_state_mutex);
480 }
481
482
483 return ret;
484}
485
486
487/**
488 * vmw_du_cursor_plane_atomic_check - check if the new state is okay
489 *
490 * @plane: cursor plane
491 * @state: info on the new plane state
492 *
493 * This is a chance to fail if the new cursor state does not fit
494 * our requirements.
495 *
496 * Returns 0 on success
497 */
498int vmw_du_cursor_plane_atomic_check(struct drm_plane *plane,
499 struct drm_plane_state *new_state)
500{
501 int ret = 0;
502 struct vmw_surface *surface = NULL;
503 struct drm_framebuffer *fb = new_state->fb;
504
505
506 /* Turning off */
507 if (!fb)
508 return ret;
509
510 /* A lot of the code assumes this */
511 if (new_state->crtc_w != 64 || new_state->crtc_h != 64) {
512 DRM_ERROR("Invalid cursor dimensions (%d, %d)\n",
513 new_state->crtc_w, new_state->crtc_h);
514 ret = -EINVAL;
515 }
516
517 if (!vmw_framebuffer_to_vfb(fb)->dmabuf)
518 surface = vmw_framebuffer_to_vfbs(fb)->surface;
519
520 if (surface && !surface->snooper.image) {
521 DRM_ERROR("surface not suitable for cursor\n");
522 ret = -EINVAL;
523 }
524
525 return ret;
526}
527
528
Sinclair Yeh06ec4192017-03-23 13:14:54 -0700529int vmw_du_crtc_atomic_check(struct drm_crtc *crtc,
530 struct drm_crtc_state *new_state)
531{
532 struct vmw_display_unit *du = vmw_crtc_to_du(new_state->crtc);
533 int connector_mask = 1 << drm_connector_index(&du->connector);
534 bool has_primary = new_state->plane_mask &
535 BIT(drm_plane_index(crtc->primary));
536
537 /* We always want to have an active plane with an active CRTC */
538 if (has_primary != new_state->enable)
539 return -EINVAL;
540
541
542 if (new_state->connector_mask != connector_mask &&
543 new_state->connector_mask != 0) {
544 DRM_ERROR("Invalid connectors configuration\n");
545 return -EINVAL;
546 }
547
548 /*
549 * Our virtual device does not have a dot clock, so use the logical
550 * clock value as the dot clock.
551 */
552 if (new_state->mode.crtc_clock == 0)
553 new_state->adjusted_mode.crtc_clock = new_state->mode.clock;
554
555 return 0;
556}
557
558
559void vmw_du_crtc_atomic_begin(struct drm_crtc *crtc,
560 struct drm_crtc_state *old_crtc_state)
561{
562}
563
564
565void vmw_du_crtc_atomic_flush(struct drm_crtc *crtc,
566 struct drm_crtc_state *old_crtc_state)
567{
568 struct drm_pending_vblank_event *event = crtc->state->event;
569
570 if (event) {
571 crtc->state->event = NULL;
572
573 spin_lock_irq(&crtc->dev->event_lock);
574 if (drm_crtc_vblank_get(crtc) == 0)
575 drm_crtc_arm_vblank_event(crtc, event);
576 else
577 drm_crtc_send_vblank_event(crtc, event);
578 spin_unlock_irq(&crtc->dev->event_lock);
579 }
580
581}
582
583
Sinclair Yeh9c2542a2017-03-23 11:33:39 -0700584/**
585 * vmw_du_crtc_duplicate_state - duplicate crtc state
586 * @crtc: DRM crtc
587 *
588 * Allocates and returns a copy of the crtc state (both common and
589 * vmw-specific) for the specified crtc.
590 *
591 * Returns: The newly allocated crtc state, or NULL on failure.
592 */
593struct drm_crtc_state *
594vmw_du_crtc_duplicate_state(struct drm_crtc *crtc)
595{
596 struct drm_crtc_state *state;
597 struct vmw_crtc_state *vcs;
598
599 if (WARN_ON(!crtc->state))
600 return NULL;
601
602 vcs = kmemdup(crtc->state, sizeof(*vcs), GFP_KERNEL);
603
604 if (!vcs)
605 return NULL;
606
607 state = &vcs->base;
608
609 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
610
611 return state;
612}
613
614
615/**
616 * vmw_du_crtc_reset - creates a blank vmw crtc state
617 * @crtc: DRM crtc
618 *
619 * Resets the atomic state for @crtc by freeing the state pointer (which
620 * might be NULL, e.g. at driver load time) and allocating a new empty state
621 * object.
622 */
623void vmw_du_crtc_reset(struct drm_crtc *crtc)
624{
625 struct vmw_crtc_state *vcs;
626
627
628 if (crtc->state) {
629 __drm_atomic_helper_crtc_destroy_state(crtc->state);
630
631 kfree(vmw_crtc_state_to_vcs(crtc->state));
632 }
633
634 vcs = kzalloc(sizeof(*vcs), GFP_KERNEL);
635
636 if (!vcs) {
637 DRM_ERROR("Cannot allocate vmw_crtc_state\n");
638 return;
639 }
640
641 crtc->state = &vcs->base;
642 crtc->state->crtc = crtc;
643}
644
645
646/**
647 * vmw_du_crtc_destroy_state - destroy crtc state
648 * @crtc: DRM crtc
649 * @state: state object to destroy
650 *
651 * Destroys the crtc state (both common and vmw-specific) for the
652 * specified plane.
653 */
654void
655vmw_du_crtc_destroy_state(struct drm_crtc *crtc,
656 struct drm_crtc_state *state)
657{
658 drm_atomic_helper_crtc_destroy_state(crtc, state);
659}
660
661
Sinclair Yehcc5ec452017-03-23 11:36:05 -0700662/**
663 * vmw_du_plane_duplicate_state - duplicate plane state
664 * @plane: drm plane
665 *
666 * Allocates and returns a copy of the plane state (both common and
667 * vmw-specific) for the specified plane.
668 *
669 * Returns: The newly allocated plane state, or NULL on failure.
670 */
671struct drm_plane_state *
672vmw_du_plane_duplicate_state(struct drm_plane *plane)
673{
674 struct drm_plane_state *state;
675 struct vmw_plane_state *vps;
676
677 vps = kmemdup(plane->state, sizeof(*vps), GFP_KERNEL);
678
679 if (!vps)
680 return NULL;
681
682 vps->pinned = 0;
683
Sinclair Yeh810b3e162017-03-23 15:39:16 -0700684 /* Mapping is managed by prepare_fb/cleanup_fb */
Sinclair Yeh810b3e162017-03-23 15:39:16 -0700685 memset(&vps->host_map, 0, sizeof(vps->host_map));
686 vps->cpp = 0;
687
Sinclair Yehcc5ec452017-03-23 11:36:05 -0700688 /* Each ref counted resource needs to be acquired again */
689 if (vps->surf)
690 (void) vmw_surface_reference(vps->surf);
691
692 if (vps->dmabuf)
693 (void) vmw_dmabuf_reference(vps->dmabuf);
694
695 state = &vps->base;
696
697 __drm_atomic_helper_plane_duplicate_state(plane, state);
698
699 return state;
700}
701
702
703/**
704 * vmw_du_plane_reset - creates a blank vmw plane state
705 * @plane: drm plane
706 *
707 * Resets the atomic state for @plane by freeing the state pointer (which might
708 * be NULL, e.g. at driver load time) and allocating a new empty state object.
709 */
710void vmw_du_plane_reset(struct drm_plane *plane)
711{
712 struct vmw_plane_state *vps;
713
714
715 if (plane->state)
716 vmw_du_plane_destroy_state(plane, plane->state);
717
718 vps = kzalloc(sizeof(*vps), GFP_KERNEL);
719
720 if (!vps) {
721 DRM_ERROR("Cannot allocate vmw_plane_state\n");
722 return;
723 }
724
725 plane->state = &vps->base;
726 plane->state->plane = plane;
Robert Fossc2c446a2017-05-19 16:50:17 -0400727 plane->state->rotation = DRM_MODE_ROTATE_0;
Sinclair Yehcc5ec452017-03-23 11:36:05 -0700728}
729
730
731/**
732 * vmw_du_plane_destroy_state - destroy plane state
733 * @plane: DRM plane
734 * @state: state object to destroy
735 *
736 * Destroys the plane state (both common and vmw-specific) for the
737 * specified plane.
738 */
739void
740vmw_du_plane_destroy_state(struct drm_plane *plane,
741 struct drm_plane_state *state)
742{
743 struct vmw_plane_state *vps = vmw_plane_state_to_vps(state);
744
745
Sinclair Yeh810b3e162017-03-23 15:39:16 -0700746 /* Should have been freed by cleanup_fb */
Sinclair Yeh810b3e162017-03-23 15:39:16 -0700747 if (vps->host_map.virtual) {
748 DRM_ERROR("Host mapping not freed\n");
749 ttm_bo_kunmap(&vps->host_map);
750 }
751
Sinclair Yehcc5ec452017-03-23 11:36:05 -0700752 if (vps->surf)
753 vmw_surface_unreference(&vps->surf);
754
755 if (vps->dmabuf)
756 vmw_dmabuf_unreference(&vps->dmabuf);
757
758 drm_atomic_helper_plane_destroy_state(plane, state);
759}
760
761
Sinclair Yehd7721ca2017-03-23 11:48:44 -0700762/**
763 * vmw_du_connector_duplicate_state - duplicate connector state
764 * @connector: DRM connector
765 *
766 * Allocates and returns a copy of the connector state (both common and
767 * vmw-specific) for the specified connector.
768 *
769 * Returns: The newly allocated connector state, or NULL on failure.
770 */
771struct drm_connector_state *
772vmw_du_connector_duplicate_state(struct drm_connector *connector)
773{
774 struct drm_connector_state *state;
775 struct vmw_connector_state *vcs;
776
777 if (WARN_ON(!connector->state))
778 return NULL;
779
780 vcs = kmemdup(connector->state, sizeof(*vcs), GFP_KERNEL);
781
782 if (!vcs)
783 return NULL;
784
785 state = &vcs->base;
786
787 __drm_atomic_helper_connector_duplicate_state(connector, state);
788
789 return state;
790}
791
792
793/**
794 * vmw_du_connector_reset - creates a blank vmw connector state
795 * @connector: DRM connector
796 *
797 * Resets the atomic state for @connector by freeing the state pointer (which
798 * might be NULL, e.g. at driver load time) and allocating a new empty state
799 * object.
800 */
801void vmw_du_connector_reset(struct drm_connector *connector)
802{
803 struct vmw_connector_state *vcs;
804
805
806 if (connector->state) {
807 __drm_atomic_helper_connector_destroy_state(connector->state);
808
809 kfree(vmw_connector_state_to_vcs(connector->state));
810 }
811
812 vcs = kzalloc(sizeof(*vcs), GFP_KERNEL);
813
814 if (!vcs) {
815 DRM_ERROR("Cannot allocate vmw_connector_state\n");
816 return;
817 }
818
819 __drm_atomic_helper_connector_reset(connector, &vcs->base);
820}
821
822
823/**
824 * vmw_du_connector_destroy_state - destroy connector state
825 * @connector: DRM connector
826 * @state: state object to destroy
827 *
828 * Destroys the connector state (both common and vmw-specific) for the
829 * specified plane.
830 */
831void
832vmw_du_connector_destroy_state(struct drm_connector *connector,
833 struct drm_connector_state *state)
834{
835 drm_atomic_helper_connector_destroy_state(connector, state);
836}
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000837/*
838 * Generic framebuffer code
839 */
840
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000841/*
842 * Surface framebuffer code
843 */
844
Rashika Kheria847c5962014-01-06 22:18:10 +0530845static void vmw_framebuffer_surface_destroy(struct drm_framebuffer *framebuffer)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000846{
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200847 struct vmw_framebuffer_surface *vfbs =
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000848 vmw_framebuffer_to_vfbs(framebuffer);
849
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000850 drm_framebuffer_cleanup(framebuffer);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200851 vmw_surface_unreference(&vfbs->surface);
Thomas Hellstroma2787242015-06-29 12:55:07 -0700852 if (vfbs->base.user_obj)
853 ttm_base_object_unref(&vfbs->base.user_obj);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000854
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200855 kfree(vfbs);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000856}
857
Rashika Kheria847c5962014-01-06 22:18:10 +0530858static int vmw_framebuffer_surface_dirty(struct drm_framebuffer *framebuffer,
Thomas Hellstrom02b00162010-10-05 12:43:02 +0200859 struct drm_file *file_priv,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000860 unsigned flags, unsigned color,
861 struct drm_clip_rect *clips,
862 unsigned num_clips)
863{
864 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
865 struct vmw_framebuffer_surface *vfbs =
866 vmw_framebuffer_to_vfbs(framebuffer);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000867 struct drm_clip_rect norect;
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +0200868 int ret, inc = 1;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000869
Sinclair Yehc8261a92015-06-26 01:23:42 -0700870 /* Legacy Display Unit does not support 3D */
871 if (dev_priv->active_display_unit == vmw_du_legacy)
Jakob Bornecrantz01e81412011-10-04 20:13:24 +0200872 return -EINVAL;
873
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200874 drm_modeset_lock_all(dev_priv->dev);
875
Thomas Hellstrom294adf72014-02-27 12:34:51 +0100876 ret = ttm_read_lock(&dev_priv->reservation_sem, true);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200877 if (unlikely(ret != 0)) {
878 drm_modeset_unlock_all(dev_priv->dev);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200879 return ret;
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200880 }
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200881
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000882 if (!num_clips) {
883 num_clips = 1;
884 clips = &norect;
885 norect.x1 = norect.y1 = 0;
886 norect.x2 = framebuffer->width;
887 norect.y2 = framebuffer->height;
888 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
889 num_clips /= 2;
890 inc = 2; /* skip source rects */
891 }
892
Sinclair Yehc8261a92015-06-26 01:23:42 -0700893 if (dev_priv->active_display_unit == vmw_du_screen_object)
Thomas Hellstrom10b1e0c2015-06-26 02:14:27 -0700894 ret = vmw_kms_sou_do_surface_dirty(dev_priv, &vfbs->base,
895 clips, NULL, NULL, 0, 0,
896 num_clips, inc, NULL);
Sinclair Yeh35c05122015-06-26 01:42:06 -0700897 else
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700898 ret = vmw_kms_stdu_surface_dirty(dev_priv, &vfbs->base,
899 clips, NULL, NULL, 0, 0,
900 num_clips, inc, NULL);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000901
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -0700902 vmw_fifo_flush(dev_priv, false);
Thomas Hellstrom294adf72014-02-27 12:34:51 +0100903 ttm_read_unlock(&dev_priv->reservation_sem);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +0200904
905 drm_modeset_unlock_all(dev_priv->dev);
906
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000907 return 0;
908}
909
Thomas Hellstrom10b1e0c2015-06-26 02:14:27 -0700910/**
911 * vmw_kms_readback - Perform a readback from the screen system to
912 * a dma-buffer backed framebuffer.
913 *
914 * @dev_priv: Pointer to the device private structure.
915 * @file_priv: Pointer to a struct drm_file identifying the caller.
916 * Must be set to NULL if @user_fence_rep is NULL.
917 * @vfb: Pointer to the dma-buffer backed framebuffer.
918 * @user_fence_rep: User-space provided structure for fence information.
919 * Must be set to non-NULL if @file_priv is non-NULL.
920 * @vclips: Array of clip rects.
921 * @num_clips: Number of clip rects in @vclips.
922 *
923 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
924 * interrupted.
925 */
926int vmw_kms_readback(struct vmw_private *dev_priv,
927 struct drm_file *file_priv,
928 struct vmw_framebuffer *vfb,
929 struct drm_vmw_fence_rep __user *user_fence_rep,
930 struct drm_vmw_rect *vclips,
931 uint32_t num_clips)
932{
933 switch (dev_priv->active_display_unit) {
934 case vmw_du_screen_object:
935 return vmw_kms_sou_readback(dev_priv, file_priv, vfb,
936 user_fence_rep, vclips, num_clips);
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700937 case vmw_du_screen_target:
938 return vmw_kms_stdu_dma(dev_priv, file_priv, vfb,
939 user_fence_rep, NULL, vclips, num_clips,
940 1, false, true);
Thomas Hellstrom10b1e0c2015-06-26 02:14:27 -0700941 default:
942 WARN_ONCE(true,
943 "Readback called with invalid display system.\n");
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700944}
Thomas Hellstrom10b1e0c2015-06-26 02:14:27 -0700945
946 return -ENOSYS;
947}
948
949
Ville Syrjäläd7955fc2015-12-15 12:21:15 +0100950static const struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs = {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000951 .destroy = vmw_framebuffer_surface_destroy,
952 .dirty = vmw_framebuffer_surface_dirty,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000953};
954
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200955static int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv,
956 struct vmw_surface *surface,
957 struct vmw_framebuffer **out,
Daniel Vetterdabdcdc2016-12-02 08:07:40 +0100958 const struct drm_mode_fb_cmd2
Sinclair Yehf89c6c32015-06-26 01:54:28 -0700959 *mode_cmd,
960 bool is_dmabuf_proxy)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000961
962{
963 struct drm_device *dev = dev_priv->dev;
964 struct vmw_framebuffer_surface *vfbs;
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200965 enum SVGA3dSurfaceFormat format;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000966 int ret;
Daniel Vetterdabdcdc2016-12-02 08:07:40 +0100967 struct drm_format_name_buf format_name;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000968
Sinclair Yehc8261a92015-06-26 01:23:42 -0700969 /* 3D is only supported on HWv8 and newer hosts */
970 if (dev_priv->active_display_unit == vmw_du_legacy)
Jakob Bornecrantz01e81412011-10-04 20:13:24 +0200971 return -ENOSYS;
972
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200973 /*
974 * Sanity checks.
975 */
976
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100977 /* Surface must be marked as a scanout. */
978 if (unlikely(!surface->scanout))
979 return -EINVAL;
980
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200981 if (unlikely(surface->mip_levels[0] != 1 ||
982 surface->num_sizes != 1 ||
Thomas Hellstromb360a3c2014-01-15 08:51:36 +0100983 surface->base_size.width < mode_cmd->width ||
984 surface->base_size.height < mode_cmd->height ||
985 surface->base_size.depth != 1)) {
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200986 DRM_ERROR("Incompatible surface dimensions "
987 "for requested mode.\n");
988 return -EINVAL;
989 }
990
Daniel Vetterdabdcdc2016-12-02 08:07:40 +0100991 switch (mode_cmd->pixel_format) {
992 case DRM_FORMAT_ARGB8888:
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200993 format = SVGA3D_A8R8G8B8;
994 break;
Daniel Vetterdabdcdc2016-12-02 08:07:40 +0100995 case DRM_FORMAT_XRGB8888:
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200996 format = SVGA3D_X8R8G8B8;
997 break;
Daniel Vetterdabdcdc2016-12-02 08:07:40 +0100998 case DRM_FORMAT_RGB565:
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200999 format = SVGA3D_R5G6B5;
1000 break;
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001001 case DRM_FORMAT_XRGB1555:
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001002 format = SVGA3D_A1R5G5B5;
1003 break;
1004 default:
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001005 DRM_ERROR("Invalid pixel format: %s\n",
1006 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001007 return -EINVAL;
1008 }
1009
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001010 /*
1011 * For DX, surface format validation is done when surface->scanout
1012 * is set.
1013 */
1014 if (!dev_priv->has_dx && format != surface->format) {
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001015 DRM_ERROR("Invalid surface format for requested mode.\n");
1016 return -EINVAL;
1017 }
1018
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001019 vfbs = kzalloc(sizeof(*vfbs), GFP_KERNEL);
1020 if (!vfbs) {
1021 ret = -ENOMEM;
1022 goto out_err1;
1023 }
1024
Ville Syrjäläa3f913c2016-12-14 22:48:59 +02001025 drm_helper_mode_fill_fb_struct(dev, &vfbs->base.base, mode_cmd);
Sinclair Yeh05c95012015-08-11 22:53:39 -07001026 vfbs->surface = vmw_surface_reference(surface);
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001027 vfbs->base.user_handle = mode_cmd->handles[0];
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001028 vfbs->is_dmabuf_proxy = is_dmabuf_proxy;
Thomas Hellstrom3a939a52010-10-05 12:43:03 +02001029
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001030 *out = &vfbs->base;
1031
Daniel Vetter80f0b5a2012-12-13 23:39:01 +01001032 ret = drm_framebuffer_init(dev, &vfbs->base.base,
1033 &vmw_framebuffer_surface_funcs);
1034 if (ret)
Sinclair Yeh05c95012015-08-11 22:53:39 -07001035 goto out_err2;
Daniel Vetter80f0b5a2012-12-13 23:39:01 +01001036
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001037 return 0;
1038
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001039out_err2:
Sinclair Yeh05c95012015-08-11 22:53:39 -07001040 vmw_surface_unreference(&surface);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001041 kfree(vfbs);
1042out_err1:
1043 return ret;
1044}
1045
1046/*
1047 * Dmabuf framebuffer code
1048 */
1049
Rashika Kheria847c5962014-01-06 22:18:10 +05301050static void vmw_framebuffer_dmabuf_destroy(struct drm_framebuffer *framebuffer)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001051{
1052 struct vmw_framebuffer_dmabuf *vfbd =
1053 vmw_framebuffer_to_vfbd(framebuffer);
1054
1055 drm_framebuffer_cleanup(framebuffer);
1056 vmw_dmabuf_unreference(&vfbd->buffer);
Thomas Hellstroma2787242015-06-29 12:55:07 -07001057 if (vfbd->base.user_obj)
1058 ttm_base_object_unref(&vfbd->base.user_obj);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001059
1060 kfree(vfbd);
1061}
1062
Rashika Kheria847c5962014-01-06 22:18:10 +05301063static int vmw_framebuffer_dmabuf_dirty(struct drm_framebuffer *framebuffer,
Thomas Hellstrom02b00162010-10-05 12:43:02 +02001064 struct drm_file *file_priv,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001065 unsigned flags, unsigned color,
1066 struct drm_clip_rect *clips,
1067 unsigned num_clips)
1068{
1069 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +02001070 struct vmw_framebuffer_dmabuf *vfbd =
1071 vmw_framebuffer_to_vfbd(framebuffer);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001072 struct drm_clip_rect norect;
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +02001073 int ret, increment = 1;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001074
Ville Syrjälä73e9efd2013-12-04 14:13:58 +02001075 drm_modeset_lock_all(dev_priv->dev);
1076
Thomas Hellstrom294adf72014-02-27 12:34:51 +01001077 ret = ttm_read_lock(&dev_priv->reservation_sem, true);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +02001078 if (unlikely(ret != 0)) {
1079 drm_modeset_unlock_all(dev_priv->dev);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +02001080 return ret;
Ville Syrjälä73e9efd2013-12-04 14:13:58 +02001081 }
Thomas Hellstrom3a939a52010-10-05 12:43:03 +02001082
Thomas Hellstromdf1c93b2010-01-13 22:28:36 +01001083 if (!num_clips) {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001084 num_clips = 1;
1085 clips = &norect;
1086 norect.x1 = norect.y1 = 0;
1087 norect.x2 = framebuffer->width;
1088 norect.y2 = framebuffer->height;
1089 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
1090 num_clips /= 2;
1091 increment = 2;
1092 }
1093
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001094 switch (dev_priv->active_display_unit) {
1095 case vmw_du_screen_target:
1096 ret = vmw_kms_stdu_dma(dev_priv, NULL, &vfbd->base, NULL,
1097 clips, NULL, num_clips, increment,
1098 true, true);
1099 break;
1100 case vmw_du_screen_object:
Thomas Hellstrom10b1e0c2015-06-26 02:14:27 -07001101 ret = vmw_kms_sou_do_dmabuf_dirty(dev_priv, &vfbd->base,
Thomas Hellstrom897b8182016-02-12 08:32:08 +01001102 clips, NULL, num_clips,
1103 increment, true, NULL);
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001104 break;
Thomas Hellstrom352b20d2015-06-29 12:57:37 -07001105 case vmw_du_legacy:
1106 ret = vmw_kms_ldu_do_dmabuf_dirty(dev_priv, &vfbd->base, 0, 0,
1107 clips, num_clips, increment);
1108 break;
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001109 default:
Thomas Hellstrom352b20d2015-06-29 12:57:37 -07001110 ret = -EINVAL;
1111 WARN_ONCE(true, "Dirty called with invalid display system.\n");
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001112 break;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +02001113 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001114
Thomas Hellstrom3eab3d92015-06-25 11:57:56 -07001115 vmw_fifo_flush(dev_priv, false);
Thomas Hellstrom294adf72014-02-27 12:34:51 +01001116 ttm_read_unlock(&dev_priv->reservation_sem);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +02001117
1118 drm_modeset_unlock_all(dev_priv->dev);
1119
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +02001120 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001121}
1122
Ville Syrjäläd7955fc2015-12-15 12:21:15 +01001123static const struct drm_framebuffer_funcs vmw_framebuffer_dmabuf_funcs = {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001124 .destroy = vmw_framebuffer_dmabuf_destroy,
1125 .dirty = vmw_framebuffer_dmabuf_dirty,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001126};
1127
Jakob Bornecrantz497a3ff2011-10-04 20:13:14 +02001128/**
Jakob Bornecrantz497a3ff2011-10-04 20:13:14 +02001129 * Pin the dmabuffer to the start of vram.
1130 */
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001131static int vmw_framebuffer_pin(struct vmw_framebuffer *vfb)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001132{
1133 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001134 struct vmw_dma_buffer *buf;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001135 int ret;
1136
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001137 buf = vfb->dmabuf ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
1138 vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001139
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001140 if (!buf)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001141 return 0;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001142
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001143 switch (dev_priv->active_display_unit) {
1144 case vmw_du_legacy:
1145 vmw_overlay_pause_all(dev_priv);
1146 ret = vmw_dmabuf_pin_in_start_of_vram(dev_priv, buf, false);
1147 vmw_overlay_resume_all(dev_priv);
1148 break;
1149 case vmw_du_screen_object:
1150 case vmw_du_screen_target:
1151 if (vfb->dmabuf)
1152 return vmw_dmabuf_pin_in_vram_or_gmr(dev_priv, buf,
1153 false);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001154
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001155 return vmw_dmabuf_pin_in_placement(dev_priv, buf,
1156 &vmw_mob_placement, false);
1157 default:
1158 return -EINVAL;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001159 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001160
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001161 return ret;
1162}
1163
1164static int vmw_framebuffer_unpin(struct vmw_framebuffer *vfb)
1165{
1166 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
1167 struct vmw_dma_buffer *buf;
1168
1169 buf = vfb->dmabuf ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
1170 vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
1171
1172 if (WARN_ON(!buf))
1173 return 0;
1174
1175 return vmw_dmabuf_unpin(dev_priv, buf, false);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001176}
1177
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001178/**
1179 * vmw_create_dmabuf_proxy - create a proxy surface for the DMA buf
1180 *
1181 * @dev: DRM device
1182 * @mode_cmd: parameters for the new surface
1183 * @dmabuf_mob: MOB backing the DMA buf
1184 * @srf_out: newly created surface
1185 *
1186 * When the content FB is a DMA buf, we create a surface as a proxy to the
1187 * same buffer. This way we can do a surface copy rather than a surface DMA.
1188 * This is a more efficient approach
1189 *
1190 * RETURNS:
1191 * 0 on success, error code otherwise
1192 */
1193static int vmw_create_dmabuf_proxy(struct drm_device *dev,
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001194 const struct drm_mode_fb_cmd2 *mode_cmd,
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001195 struct vmw_dma_buffer *dmabuf_mob,
1196 struct vmw_surface **srf_out)
1197{
1198 uint32_t format;
Thomas Hellstrom8cd9f252017-01-19 10:53:02 -08001199 struct drm_vmw_size content_base_size = {0};
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001200 struct vmw_resource *res;
Thomas Hellstroma50e2bf2016-01-08 20:29:40 +01001201 unsigned int bytes_pp;
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001202 struct drm_format_name_buf format_name;
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001203 int ret;
1204
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001205 switch (mode_cmd->pixel_format) {
1206 case DRM_FORMAT_ARGB8888:
1207 case DRM_FORMAT_XRGB8888:
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001208 format = SVGA3D_X8R8G8B8;
Thomas Hellstroma50e2bf2016-01-08 20:29:40 +01001209 bytes_pp = 4;
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001210 break;
1211
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001212 case DRM_FORMAT_RGB565:
1213 case DRM_FORMAT_XRGB1555:
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001214 format = SVGA3D_R5G6B5;
Thomas Hellstroma50e2bf2016-01-08 20:29:40 +01001215 bytes_pp = 2;
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001216 break;
1217
1218 case 8:
1219 format = SVGA3D_P8;
Thomas Hellstroma50e2bf2016-01-08 20:29:40 +01001220 bytes_pp = 1;
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001221 break;
1222
1223 default:
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001224 DRM_ERROR("Invalid framebuffer format %s\n",
1225 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001226 return -EINVAL;
1227 }
1228
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001229 content_base_size.width = mode_cmd->pitches[0] / bytes_pp;
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001230 content_base_size.height = mode_cmd->height;
1231 content_base_size.depth = 1;
1232
1233 ret = vmw_surface_gb_priv_define(dev,
1234 0, /* kernel visible only */
1235 0, /* flags */
1236 format,
1237 true, /* can be a scanout buffer */
1238 1, /* num of mip levels */
1239 0,
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001240 0,
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001241 content_base_size,
1242 srf_out);
1243 if (ret) {
1244 DRM_ERROR("Failed to allocate proxy content buffer\n");
1245 return ret;
1246 }
1247
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001248 res = &(*srf_out)->res;
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001249
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001250 /* Reserve and switch the backing mob. */
1251 mutex_lock(&res->dev_priv->cmdbuf_mutex);
1252 (void) vmw_resource_reserve(res, false, true);
1253 vmw_dmabuf_unreference(&res->backup);
1254 res->backup = vmw_dmabuf_reference(dmabuf_mob);
1255 res->backup_offset = 0;
Thomas Hellstromd80efd52015-08-10 10:39:35 -07001256 vmw_resource_unreserve(res, false, NULL, 0);
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001257 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001258
1259 return 0;
1260}
1261
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001262
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001263
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001264static int vmw_kms_new_framebuffer_dmabuf(struct vmw_private *dev_priv,
1265 struct vmw_dma_buffer *dmabuf,
1266 struct vmw_framebuffer **out,
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001267 const struct drm_mode_fb_cmd2
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001268 *mode_cmd)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001269
1270{
1271 struct drm_device *dev = dev_priv->dev;
1272 struct vmw_framebuffer_dmabuf *vfbd;
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001273 unsigned int requested_size;
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001274 struct drm_format_name_buf format_name;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001275 int ret;
1276
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001277 requested_size = mode_cmd->height * mode_cmd->pitches[0];
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001278 if (unlikely(requested_size > dmabuf->base.num_pages * PAGE_SIZE)) {
1279 DRM_ERROR("Screen buffer object size is too small "
1280 "for requested mode.\n");
1281 return -EINVAL;
1282 }
1283
Jakob Bornecrantzc337ada2011-10-04 20:13:34 +02001284 /* Limited framebuffer color depth support for screen objects */
Sinclair Yehc8261a92015-06-26 01:23:42 -07001285 if (dev_priv->active_display_unit == vmw_du_screen_object) {
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001286 switch (mode_cmd->pixel_format) {
1287 case DRM_FORMAT_XRGB8888:
1288 case DRM_FORMAT_ARGB8888:
1289 break;
1290 case DRM_FORMAT_XRGB1555:
1291 case DRM_FORMAT_RGB565:
1292 break;
Jakob Bornecrantzc337ada2011-10-04 20:13:34 +02001293 default:
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001294 DRM_ERROR("Invalid pixel format: %s\n",
1295 drm_get_format_name(mode_cmd->pixel_format, &format_name));
Jakob Bornecrantzc337ada2011-10-04 20:13:34 +02001296 return -EINVAL;
1297 }
1298 }
1299
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001300 vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL);
1301 if (!vfbd) {
1302 ret = -ENOMEM;
1303 goto out_err1;
1304 }
1305
Ville Syrjäläa3f913c2016-12-14 22:48:59 +02001306 drm_helper_mode_fill_fb_struct(dev, &vfbd->base.base, mode_cmd);
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001307 vfbd->base.dmabuf = true;
Sinclair Yeh05c95012015-08-11 22:53:39 -07001308 vfbd->buffer = vmw_dmabuf_reference(dmabuf);
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001309 vfbd->base.user_handle = mode_cmd->handles[0];
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001310 *out = &vfbd->base;
1311
Daniel Vetter80f0b5a2012-12-13 23:39:01 +01001312 ret = drm_framebuffer_init(dev, &vfbd->base.base,
1313 &vmw_framebuffer_dmabuf_funcs);
1314 if (ret)
Sinclair Yeh05c95012015-08-11 22:53:39 -07001315 goto out_err2;
Daniel Vetter80f0b5a2012-12-13 23:39:01 +01001316
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001317 return 0;
1318
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001319out_err2:
Sinclair Yeh05c95012015-08-11 22:53:39 -07001320 vmw_dmabuf_unreference(&dmabuf);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001321 kfree(vfbd);
1322out_err1:
1323 return ret;
1324}
1325
Sinclair Yeh810b3e162017-03-23 15:39:16 -07001326
1327/**
1328 * vmw_kms_srf_ok - check if a surface can be created
1329 *
1330 * @width: requested width
1331 * @height: requested height
1332 *
1333 * Surfaces need to be less than texture size
1334 */
1335static bool
1336vmw_kms_srf_ok(struct vmw_private *dev_priv, uint32_t width, uint32_t height)
1337{
1338 if (width > dev_priv->texture_max_width ||
1339 height > dev_priv->texture_max_height)
1340 return false;
1341
1342 return true;
1343}
1344
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001345/**
1346 * vmw_kms_new_framebuffer - Create a new framebuffer.
1347 *
1348 * @dev_priv: Pointer to device private struct.
1349 * @dmabuf: Pointer to dma buffer to wrap the kms framebuffer around.
1350 * Either @dmabuf or @surface must be NULL.
1351 * @surface: Pointer to a surface to wrap the kms framebuffer around.
1352 * Either @dmabuf or @surface must be NULL.
1353 * @only_2d: No presents will occur to this dma buffer based framebuffer. This
1354 * Helps the code to do some important optimizations.
1355 * @mode_cmd: Frame-buffer metadata.
1356 */
1357struct vmw_framebuffer *
1358vmw_kms_new_framebuffer(struct vmw_private *dev_priv,
1359 struct vmw_dma_buffer *dmabuf,
1360 struct vmw_surface *surface,
1361 bool only_2d,
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001362 const struct drm_mode_fb_cmd2 *mode_cmd)
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001363{
Sinclair Yeh05c95012015-08-11 22:53:39 -07001364 struct vmw_framebuffer *vfb = NULL;
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001365 bool is_dmabuf_proxy = false;
1366 int ret;
1367
1368 /*
1369 * We cannot use the SurfaceDMA command in an non-accelerated VM,
1370 * therefore, wrap the DMA buf in a surface so we can use the
1371 * SurfaceCopy command.
1372 */
Sinclair Yeh810b3e162017-03-23 15:39:16 -07001373 if (vmw_kms_srf_ok(dev_priv, mode_cmd->width, mode_cmd->height) &&
1374 dmabuf && only_2d &&
Sinclair Yehbbd5fef2017-06-02 07:44:53 +02001375 mode_cmd->width > 64 && /* Don't create a proxy for cursor */
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001376 dev_priv->active_display_unit == vmw_du_screen_target) {
1377 ret = vmw_create_dmabuf_proxy(dev_priv->dev, mode_cmd,
1378 dmabuf, &surface);
1379 if (ret)
1380 return ERR_PTR(ret);
1381
1382 is_dmabuf_proxy = true;
1383 }
1384
1385 /* Create the new framebuffer depending one what we have */
Sinclair Yeh05c95012015-08-11 22:53:39 -07001386 if (surface) {
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001387 ret = vmw_kms_new_framebuffer_surface(dev_priv, surface, &vfb,
1388 mode_cmd,
1389 is_dmabuf_proxy);
Sinclair Yeh05c95012015-08-11 22:53:39 -07001390
1391 /*
1392 * vmw_create_dmabuf_proxy() adds a reference that is no longer
1393 * needed
1394 */
1395 if (is_dmabuf_proxy)
1396 vmw_surface_unreference(&surface);
1397 } else if (dmabuf) {
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001398 ret = vmw_kms_new_framebuffer_dmabuf(dev_priv, dmabuf, &vfb,
1399 mode_cmd);
Sinclair Yeh05c95012015-08-11 22:53:39 -07001400 } else {
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001401 BUG();
Sinclair Yeh05c95012015-08-11 22:53:39 -07001402 }
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001403
1404 if (ret)
1405 return ERR_PTR(ret);
1406
1407 vfb->pin = vmw_framebuffer_pin;
1408 vfb->unpin = vmw_framebuffer_unpin;
1409
1410 return vfb;
1411}
1412
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001413/*
1414 * Generic Kernel modesetting functions
1415 */
1416
1417static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev,
1418 struct drm_file *file_priv,
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001419 const struct drm_mode_fb_cmd2 *mode_cmd)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001420{
1421 struct vmw_private *dev_priv = vmw_priv(dev);
1422 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
1423 struct vmw_framebuffer *vfb = NULL;
1424 struct vmw_surface *surface = NULL;
1425 struct vmw_dma_buffer *bo = NULL;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001426 struct ttm_base_object *user_obj;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001427 int ret;
1428
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001429 /**
1430 * This code should be conditioned on Screen Objects not being used.
1431 * If screen objects are used, we can allocate a GMR to hold the
1432 * requested framebuffer.
1433 */
1434
Xi Wang8a783892011-12-21 05:18:33 -05001435 if (!vmw_kms_validate_mode_vram(dev_priv,
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001436 mode_cmd->pitches[0],
1437 mode_cmd->height)) {
Sinclair Yehc8261a92015-06-26 01:23:42 -07001438 DRM_ERROR("Requested mode exceed bounding box limit.\n");
Jakob Bornecrantzd9826402011-11-03 21:03:04 +01001439 return ERR_PTR(-ENOMEM);
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001440 }
1441
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001442 /*
1443 * Take a reference on the user object of the resource
1444 * backing the kms fb. This ensures that user-space handle
1445 * lookups on that resource will always work as long as
1446 * it's registered with a kms framebuffer. This is important,
1447 * since vmw_execbuf_process identifies resources in the
1448 * command stream using user-space handles.
1449 */
1450
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001451 user_obj = ttm_base_object_lookup(tfile, mode_cmd->handles[0]);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001452 if (unlikely(user_obj == NULL)) {
1453 DRM_ERROR("Could not locate requested kms frame buffer.\n");
1454 return ERR_PTR(-ENOENT);
1455 }
1456
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001457 /**
1458 * End conditioned code.
1459 */
1460
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +01001461 /* returns either a dmabuf or surface */
1462 ret = vmw_user_lookup_handle(dev_priv, tfile,
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001463 mode_cmd->handles[0],
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +01001464 &surface, &bo);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001465 if (ret)
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +01001466 goto err_out;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001467
Sinclair Yeh810b3e162017-03-23 15:39:16 -07001468
1469 if (!bo &&
1470 !vmw_kms_srf_ok(dev_priv, mode_cmd->width, mode_cmd->height)) {
1471 DRM_ERROR("Surface size cannot exceed %dx%d",
1472 dev_priv->texture_max_width,
1473 dev_priv->texture_max_height);
1474 goto err_out;
1475 }
1476
1477
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001478 vfb = vmw_kms_new_framebuffer(dev_priv, bo, surface,
1479 !(dev_priv->capabilities & SVGA_CAP_3D),
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001480 mode_cmd);
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001481 if (IS_ERR(vfb)) {
1482 ret = PTR_ERR(vfb);
1483 goto err_out;
1484 }
Jakob Bornecrantz5ffdb652010-01-30 03:38:08 +00001485
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +01001486err_out:
1487 /* vmw_user_lookup_handle takes one ref so does new_fb */
1488 if (bo)
1489 vmw_dmabuf_unreference(&bo);
1490 if (surface)
1491 vmw_surface_unreference(&surface);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001492
1493 if (ret) {
1494 DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001495 ttm_base_object_unref(&user_obj);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01001496 return ERR_PTR(ret);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001497 } else
1498 vfb->user_obj = user_obj;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001499
1500 return &vfb->base;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001501}
1502
Sinclair Yehc46a3062017-03-23 14:24:53 -07001503
1504
1505/**
1506 * vmw_kms_atomic_check_modeset- validate state object for modeset changes
1507 *
1508 * @dev: DRM device
1509 * @state: the driver state object
1510 *
1511 * This is a simple wrapper around drm_atomic_helper_check_modeset() for
1512 * us to assign a value to mode->crtc_clock so that
1513 * drm_calc_timestamping_constants() won't throw an error message
1514 *
1515 * RETURNS
1516 * Zero for success or -errno
1517 */
Maarten Lankhorstbdc362f2017-07-12 10:13:33 +02001518static int
Sinclair Yehc46a3062017-03-23 14:24:53 -07001519vmw_kms_atomic_check_modeset(struct drm_device *dev,
1520 struct drm_atomic_state *state)
1521{
1522 struct drm_crtc_state *crtc_state;
1523 struct drm_crtc *crtc;
1524 struct vmw_private *dev_priv = vmw_priv(dev);
1525 int i;
1526
Maarten Lankhorstbdc362f2017-07-12 10:13:33 +02001527 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
Sinclair Yehc46a3062017-03-23 14:24:53 -07001528 unsigned long requested_bb_mem = 0;
1529
1530 if (dev_priv->active_display_unit == vmw_du_screen_target) {
1531 if (crtc->primary->fb) {
1532 int cpp = crtc->primary->fb->pitches[0] /
1533 crtc->primary->fb->width;
1534
1535 requested_bb_mem += crtc->mode.hdisplay * cpp *
1536 crtc->mode.vdisplay;
1537 }
1538
1539 if (requested_bb_mem > dev_priv->prim_bb_mem)
1540 return -EINVAL;
1541 }
1542 }
1543
1544 return drm_atomic_helper_check(dev, state);
1545}
1546
1547
Sinclair Yeh021aba72017-08-29 18:55:09 +02001548/**
1549 * vmw_kms_atomic_commit - Perform an atomic state commit
1550 *
1551 * @dev: DRM device
1552 * @state: the driver state object
1553 * @nonblock: Whether nonblocking behaviour is requested
1554 *
1555 * This is a simple wrapper around drm_atomic_helper_commit() for
1556 * us to clear the nonblocking value.
1557 *
1558 * Nonblocking commits currently cause synchronization issues
1559 * for vmwgfx.
1560 *
1561 * RETURNS
1562 * Zero for success or negative error code on failure.
1563 */
1564int vmw_kms_atomic_commit(struct drm_device *dev,
1565 struct drm_atomic_state *state,
1566 bool nonblock)
1567{
1568 return drm_atomic_helper_commit(dev, state, false);
1569}
1570
1571
Laurent Pincharte6ecefa2012-05-17 13:27:23 +02001572static const struct drm_mode_config_funcs vmw_kms_funcs = {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001573 .fb_create = vmw_kms_fb_create,
Sinclair Yehc46a3062017-03-23 14:24:53 -07001574 .atomic_check = vmw_kms_atomic_check_modeset,
Sinclair Yeh021aba72017-08-29 18:55:09 +02001575 .atomic_commit = vmw_kms_atomic_commit,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001576};
1577
Thomas Hellstromb9eb1a62015-04-02 02:39:45 -07001578static int vmw_kms_generic_present(struct vmw_private *dev_priv,
1579 struct drm_file *file_priv,
1580 struct vmw_framebuffer *vfb,
1581 struct vmw_surface *surface,
1582 uint32_t sid,
1583 int32_t destX, int32_t destY,
1584 struct drm_vmw_rect *clips,
1585 uint32_t num_clips)
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001586{
Thomas Hellstrom10b1e0c2015-06-26 02:14:27 -07001587 return vmw_kms_sou_do_surface_dirty(dev_priv, vfb, NULL, clips,
1588 &surface->res, destX, destY,
1589 num_clips, 1, NULL);
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001590}
1591
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001592
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001593int vmw_kms_present(struct vmw_private *dev_priv,
1594 struct drm_file *file_priv,
1595 struct vmw_framebuffer *vfb,
1596 struct vmw_surface *surface,
1597 uint32_t sid,
1598 int32_t destX, int32_t destY,
1599 struct drm_vmw_rect *clips,
1600 uint32_t num_clips)
1601{
Sinclair Yeh35c05122015-06-26 01:42:06 -07001602 int ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001603
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001604 switch (dev_priv->active_display_unit) {
1605 case vmw_du_screen_target:
1606 ret = vmw_kms_stdu_surface_dirty(dev_priv, vfb, NULL, clips,
1607 &surface->res, destX, destY,
1608 num_clips, 1, NULL);
1609 break;
1610 case vmw_du_screen_object:
1611 ret = vmw_kms_generic_present(dev_priv, file_priv, vfb, surface,
1612 sid, destX, destY, clips,
1613 num_clips);
1614 break;
1615 default:
1616 WARN_ONCE(true,
1617 "Present called with invalid display system.\n");
1618 ret = -ENOSYS;
1619 break;
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001620 }
Sinclair Yeh35c05122015-06-26 01:42:06 -07001621 if (ret)
1622 return ret;
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001623
Sinclair Yeh35c05122015-06-26 01:42:06 -07001624 vmw_fifo_flush(dev_priv, false);
Michel Dänzer0bef23f2011-08-31 07:42:50 +00001625
Sinclair Yeh35c05122015-06-26 01:42:06 -07001626 return 0;
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001627}
1628
Thomas Hellstrom578e6092016-02-12 09:45:42 +01001629static void
1630vmw_kms_create_hotplug_mode_update_property(struct vmw_private *dev_priv)
1631{
1632 if (dev_priv->hotplug_mode_update_property)
1633 return;
1634
1635 dev_priv->hotplug_mode_update_property =
1636 drm_property_create_range(dev_priv->dev,
1637 DRM_MODE_PROP_IMMUTABLE,
1638 "hotplug_mode_update", 0, 1);
1639
1640 if (!dev_priv->hotplug_mode_update_property)
1641 return;
1642
1643}
1644
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001645int vmw_kms_init(struct vmw_private *dev_priv)
1646{
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001647 struct drm_device *dev = dev_priv->dev;
1648 int ret;
1649
1650 drm_mode_config_init(dev);
1651 dev->mode_config.funcs = &vmw_kms_funcs;
1652 dev->mode_config.min_width = 1;
1653 dev->mode_config.min_height = 1;
Sinclair Yeh65ade7d2015-07-16 10:49:13 -07001654 dev->mode_config.max_width = dev_priv->texture_max_width;
1655 dev->mode_config.max_height = dev_priv->texture_max_height;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001656
Thomas Hellstrom578e6092016-02-12 09:45:42 +01001657 drm_mode_create_suggested_offset_properties(dev);
1658 vmw_kms_create_hotplug_mode_update_property(dev_priv);
1659
Sinclair Yeh35c05122015-06-26 01:42:06 -07001660 ret = vmw_kms_stdu_init_display(dev_priv);
1661 if (ret) {
1662 ret = vmw_kms_sou_init_display(dev_priv);
1663 if (ret) /* Fallback */
1664 ret = vmw_kms_ldu_init_display(dev_priv);
1665 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001666
Sinclair Yehc8261a92015-06-26 01:23:42 -07001667 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001668}
1669
1670int vmw_kms_close(struct vmw_private *dev_priv)
1671{
Daniel Vetter5f58e972017-06-26 18:19:48 +02001672 int ret = 0;
Sinclair Yehc8261a92015-06-26 01:23:42 -07001673
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001674 /*
1675 * Docs says we should take the lock before calling this function
1676 * but since it destroys encoders and our destructor calls
1677 * drm_encoder_cleanup which takes the lock we deadlock.
1678 */
1679 drm_mode_config_cleanup(dev_priv->dev);
Daniel Vetter5f58e972017-06-26 18:19:48 +02001680 if (dev_priv->active_display_unit == vmw_du_legacy)
Sinclair Yehc8261a92015-06-26 01:23:42 -07001681 ret = vmw_kms_ldu_close_display(dev_priv);
1682
1683 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001684}
1685
1686int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
1687 struct drm_file *file_priv)
1688{
1689 struct drm_vmw_cursor_bypass_arg *arg = data;
1690 struct vmw_display_unit *du;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001691 struct drm_crtc *crtc;
1692 int ret = 0;
1693
1694
1695 mutex_lock(&dev->mode_config.mutex);
1696 if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
1697
1698 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1699 du = vmw_crtc_to_du(crtc);
1700 du->hotspot_x = arg->xhot;
1701 du->hotspot_y = arg->yhot;
1702 }
1703
1704 mutex_unlock(&dev->mode_config.mutex);
1705 return 0;
1706 }
1707
Keith Packard418da172017-03-14 23:25:07 -07001708 crtc = drm_crtc_find(dev, file_priv, arg->crtc_id);
Rob Clarka4cd5d62014-07-17 23:30:02 -04001709 if (!crtc) {
Ville Syrjälä4ae87ff2013-10-17 13:35:05 +03001710 ret = -ENOENT;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001711 goto out;
1712 }
1713
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001714 du = vmw_crtc_to_du(crtc);
1715
1716 du->hotspot_x = arg->xhot;
1717 du->hotspot_y = arg->yhot;
1718
1719out:
1720 mutex_unlock(&dev->mode_config.mutex);
1721
1722 return ret;
1723}
1724
1725int vmw_kms_write_svga(struct vmw_private *vmw_priv,
1726 unsigned width, unsigned height, unsigned pitch,
1727 unsigned bpp, unsigned depth)
1728{
1729 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1730 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch);
1731 else if (vmw_fifo_have_pitchlock(vmw_priv))
Thomas Hellstromb76ff5e2015-10-28 10:44:04 +01001732 vmw_mmio_write(pitch, vmw_priv->mmio_virt +
1733 SVGA_FIFO_PITCHLOCK);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001734 vmw_write(vmw_priv, SVGA_REG_WIDTH, width);
1735 vmw_write(vmw_priv, SVGA_REG_HEIGHT, height);
1736 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp);
1737
1738 if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) {
1739 DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n",
1740 depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH));
1741 return -EINVAL;
1742 }
1743
1744 return 0;
1745}
1746
1747int vmw_kms_save_vga(struct vmw_private *vmw_priv)
1748{
1749 struct vmw_vga_topology_state *save;
1750 uint32_t i;
1751
1752 vmw_priv->vga_width = vmw_read(vmw_priv, SVGA_REG_WIDTH);
1753 vmw_priv->vga_height = vmw_read(vmw_priv, SVGA_REG_HEIGHT);
1754 vmw_priv->vga_bpp = vmw_read(vmw_priv, SVGA_REG_BITS_PER_PIXEL);
1755 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1756 vmw_priv->vga_pitchlock =
1757 vmw_read(vmw_priv, SVGA_REG_PITCHLOCK);
1758 else if (vmw_fifo_have_pitchlock(vmw_priv))
Thomas Hellstromb76ff5e2015-10-28 10:44:04 +01001759 vmw_priv->vga_pitchlock = vmw_mmio_read(vmw_priv->mmio_virt +
1760 SVGA_FIFO_PITCHLOCK);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001761
1762 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1763 return 0;
1764
1765 vmw_priv->num_displays = vmw_read(vmw_priv,
1766 SVGA_REG_NUM_GUEST_DISPLAYS);
1767
1768 if (vmw_priv->num_displays == 0)
1769 vmw_priv->num_displays = 1;
1770
1771 for (i = 0; i < vmw_priv->num_displays; ++i) {
1772 save = &vmw_priv->vga_save[i];
1773 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1774 save->primary = vmw_read(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY);
1775 save->pos_x = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_X);
1776 save->pos_y = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y);
1777 save->width = vmw_read(vmw_priv, SVGA_REG_DISPLAY_WIDTH);
1778 save->height = vmw_read(vmw_priv, SVGA_REG_DISPLAY_HEIGHT);
1779 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
1780 if (i == 0 && vmw_priv->num_displays == 1 &&
1781 save->width == 0 && save->height == 0) {
1782
1783 /*
1784 * It should be fairly safe to assume that these
1785 * values are uninitialized.
1786 */
1787
1788 save->width = vmw_priv->vga_width - save->pos_x;
1789 save->height = vmw_priv->vga_height - save->pos_y;
1790 }
1791 }
1792
1793 return 0;
1794}
1795
1796int vmw_kms_restore_vga(struct vmw_private *vmw_priv)
1797{
1798 struct vmw_vga_topology_state *save;
1799 uint32_t i;
1800
1801 vmw_write(vmw_priv, SVGA_REG_WIDTH, vmw_priv->vga_width);
1802 vmw_write(vmw_priv, SVGA_REG_HEIGHT, vmw_priv->vga_height);
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001803 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, vmw_priv->vga_bpp);
1804 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1805 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK,
1806 vmw_priv->vga_pitchlock);
1807 else if (vmw_fifo_have_pitchlock(vmw_priv))
Thomas Hellstromb76ff5e2015-10-28 10:44:04 +01001808 vmw_mmio_write(vmw_priv->vga_pitchlock,
1809 vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001810
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001811 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1812 return 0;
1813
1814 for (i = 0; i < vmw_priv->num_displays; ++i) {
1815 save = &vmw_priv->vga_save[i];
1816 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1817 vmw_write(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY, save->primary);
1818 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_X, save->pos_x);
1819 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y, save->pos_y);
1820 vmw_write(vmw_priv, SVGA_REG_DISPLAY_WIDTH, save->width);
1821 vmw_write(vmw_priv, SVGA_REG_DISPLAY_HEIGHT, save->height);
1822 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
1823 }
1824
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001825 return 0;
1826}
Jakob Bornecrantzd8bd19d2010-06-01 11:54:20 +02001827
Thomas Hellstrome133e732010-10-05 12:43:04 +02001828bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
1829 uint32_t pitch,
1830 uint32_t height)
1831{
Sinclair Yeh35c05122015-06-26 01:42:06 -07001832 return ((u64) pitch * (u64) height) < (u64)
1833 ((dev_priv->active_display_unit == vmw_du_screen_target) ?
1834 dev_priv->prim_bb_mem : dev_priv->vram_size);
Thomas Hellstrome133e732010-10-05 12:43:04 +02001835}
1836
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001837
1838/**
1839 * Function called by DRM code called with vbl_lock held.
1840 */
Thierry Reding88e72712015-09-24 18:35:31 +02001841u32 vmw_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
Thomas Hellstrom7a1c2f62010-10-01 10:21:49 +02001842{
1843 return 0;
1844}
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001845
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001846/**
1847 * Function called by DRM code called with vbl_lock held.
1848 */
Thierry Reding88e72712015-09-24 18:35:31 +02001849int vmw_enable_vblank(struct drm_device *dev, unsigned int pipe)
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001850{
Woody Suwalski2b0bc682018-01-17 09:07:47 +01001851 return -EINVAL;
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001852}
1853
1854/**
1855 * Function called by DRM code called with vbl_lock held.
1856 */
Thierry Reding88e72712015-09-24 18:35:31 +02001857void vmw_disable_vblank(struct drm_device *dev, unsigned int pipe)
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001858{
1859}
1860
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001861
1862/*
1863 * Small shared kms functions.
1864 */
1865
Rashika Kheria847c5962014-01-06 22:18:10 +05301866static int vmw_du_update_layout(struct vmw_private *dev_priv, unsigned num,
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001867 struct drm_vmw_rect *rects)
1868{
1869 struct drm_device *dev = dev_priv->dev;
1870 struct vmw_display_unit *du;
1871 struct drm_connector *con;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001872
1873 mutex_lock(&dev->mode_config.mutex);
1874
1875#if 0
Thomas Hellstrom6ea77d12011-10-04 20:13:36 +02001876 {
1877 unsigned int i;
1878
1879 DRM_INFO("%s: new layout ", __func__);
1880 for (i = 0; i < num; i++)
1881 DRM_INFO("(%i, %i %ux%u) ", rects[i].x, rects[i].y,
1882 rects[i].w, rects[i].h);
1883 DRM_INFO("\n");
1884 }
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001885#endif
1886
1887 list_for_each_entry(con, &dev->mode_config.connector_list, head) {
1888 du = vmw_connector_to_du(con);
1889 if (num > du->unit) {
1890 du->pref_width = rects[du->unit].w;
1891 du->pref_height = rects[du->unit].h;
1892 du->pref_active = true;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001893 du->gui_x = rects[du->unit].x;
1894 du->gui_y = rects[du->unit].y;
Thomas Hellstrom578e6092016-02-12 09:45:42 +01001895 drm_object_property_set_value
1896 (&con->base, dev->mode_config.suggested_x_property,
1897 du->gui_x);
1898 drm_object_property_set_value
1899 (&con->base, dev->mode_config.suggested_y_property,
1900 du->gui_y);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001901 } else {
1902 du->pref_width = 800;
1903 du->pref_height = 600;
1904 du->pref_active = false;
Thomas Hellstrom578e6092016-02-12 09:45:42 +01001905 drm_object_property_set_value
1906 (&con->base, dev->mode_config.suggested_x_property,
1907 0);
1908 drm_object_property_set_value
1909 (&con->base, dev->mode_config.suggested_y_property,
1910 0);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001911 }
1912 con->status = vmw_du_connector_detect(con, true);
1913 }
1914
1915 mutex_unlock(&dev->mode_config.mutex);
Thomas Hellstrom578e6092016-02-12 09:45:42 +01001916 drm_sysfs_hotplug_event(dev);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001917
1918 return 0;
1919}
1920
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02001921int vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
1922 u16 *r, u16 *g, u16 *b,
Daniel Vetter6d124ff2017-04-03 10:33:01 +02001923 uint32_t size,
1924 struct drm_modeset_acquire_ctx *ctx)
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001925{
1926 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
1927 int i;
1928
1929 for (i = 0; i < size; i++) {
1930 DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i,
1931 r[i], g[i], b[i]);
1932 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 0, r[i] >> 8);
1933 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 1, g[i] >> 8);
1934 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 2, b[i] >> 8);
1935 }
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02001936
1937 return 0;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001938}
1939
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02001940int vmw_du_connector_dpms(struct drm_connector *connector, int mode)
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001941{
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02001942 return 0;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001943}
1944
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001945enum drm_connector_status
1946vmw_du_connector_detect(struct drm_connector *connector, bool force)
1947{
1948 uint32_t num_displays;
1949 struct drm_device *dev = connector->dev;
1950 struct vmw_private *dev_priv = vmw_priv(dev);
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001951 struct vmw_display_unit *du = vmw_connector_to_du(connector);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001952
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001953 num_displays = vmw_read(dev_priv, SVGA_REG_NUM_DISPLAYS);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001954
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02001955 return ((vmw_connector_to_du(connector)->unit < num_displays &&
1956 du->pref_active) ?
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001957 connector_status_connected : connector_status_disconnected);
1958}
1959
1960static struct drm_display_mode vmw_kms_connector_builtin[] = {
1961 /* 640x480@60Hz */
1962 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
1963 752, 800, 0, 480, 489, 492, 525, 0,
1964 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1965 /* 800x600@60Hz */
1966 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
1967 968, 1056, 0, 600, 601, 605, 628, 0,
1968 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1969 /* 1024x768@60Hz */
1970 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
1971 1184, 1344, 0, 768, 771, 777, 806, 0,
1972 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1973 /* 1152x864@75Hz */
1974 { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
1975 1344, 1600, 0, 864, 865, 868, 900, 0,
1976 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1977 /* 1280x768@60Hz */
1978 { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344,
1979 1472, 1664, 0, 768, 771, 778, 798, 0,
1980 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1981 /* 1280x800@60Hz */
1982 { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352,
1983 1480, 1680, 0, 800, 803, 809, 831, 0,
1984 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
1985 /* 1280x960@60Hz */
1986 { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376,
1987 1488, 1800, 0, 960, 961, 964, 1000, 0,
1988 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1989 /* 1280x1024@60Hz */
1990 { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328,
1991 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
1992 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1993 /* 1360x768@60Hz */
1994 { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424,
1995 1536, 1792, 0, 768, 771, 777, 795, 0,
1996 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1997 /* 1440x1050@60Hz */
1998 { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488,
1999 1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
2000 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2001 /* 1440x900@60Hz */
2002 { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520,
2003 1672, 1904, 0, 900, 903, 909, 934, 0,
2004 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2005 /* 1600x1200@60Hz */
2006 { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664,
2007 1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
2008 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
2009 /* 1680x1050@60Hz */
2010 { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784,
2011 1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
2012 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2013 /* 1792x1344@60Hz */
2014 { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920,
2015 2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
2016 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2017 /* 1853x1392@60Hz */
2018 { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952,
2019 2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
2020 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2021 /* 1920x1200@60Hz */
2022 { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056,
2023 2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
2024 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2025 /* 1920x1440@60Hz */
2026 { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048,
2027 2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
2028 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2029 /* 2560x1600@60Hz */
2030 { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752,
2031 3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
2032 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2033 /* Terminate */
2034 { DRM_MODE("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) },
2035};
2036
Thomas Hellstrom1543b4d2011-11-02 09:43:10 +01002037/**
2038 * vmw_guess_mode_timing - Provide fake timings for a
2039 * 60Hz vrefresh mode.
2040 *
2041 * @mode - Pointer to a struct drm_display_mode with hdisplay and vdisplay
2042 * members filled in.
2043 */
Thomas Hellstroma2787242015-06-29 12:55:07 -07002044void vmw_guess_mode_timing(struct drm_display_mode *mode)
Thomas Hellstrom1543b4d2011-11-02 09:43:10 +01002045{
2046 mode->hsync_start = mode->hdisplay + 50;
2047 mode->hsync_end = mode->hsync_start + 50;
2048 mode->htotal = mode->hsync_end + 50;
2049
2050 mode->vsync_start = mode->vdisplay + 50;
2051 mode->vsync_end = mode->vsync_start + 50;
2052 mode->vtotal = mode->vsync_end + 50;
2053
2054 mode->clock = (u32)mode->htotal * (u32)mode->vtotal / 100 * 6;
2055 mode->vrefresh = drm_mode_vrefresh(mode);
2056}
2057
2058
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002059int vmw_du_connector_fill_modes(struct drm_connector *connector,
2060 uint32_t max_width, uint32_t max_height)
2061{
2062 struct vmw_display_unit *du = vmw_connector_to_du(connector);
2063 struct drm_device *dev = connector->dev;
2064 struct vmw_private *dev_priv = vmw_priv(dev);
2065 struct drm_display_mode *mode = NULL;
2066 struct drm_display_mode *bmode;
2067 struct drm_display_mode prefmode = { DRM_MODE("preferred",
2068 DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
2069 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2070 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
2071 };
2072 int i;
Sinclair Yeh7c20d212016-06-29 11:29:47 -07002073 u32 assumed_bpp = 4;
Sinclair Yeh9a723842014-10-31 09:58:06 +01002074
Sinclair Yeh04319d82016-06-29 12:15:48 -07002075 if (dev_priv->assume_16bpp)
2076 assumed_bpp = 2;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002077
Sinclair Yeh35c05122015-06-26 01:42:06 -07002078 if (dev_priv->active_display_unit == vmw_du_screen_target) {
2079 max_width = min(max_width, dev_priv->stdu_max_width);
Sinclair Yeh28c95422017-03-27 11:12:27 -07002080 max_width = min(max_width, dev_priv->texture_max_width);
2081
Sinclair Yeh35c05122015-06-26 01:42:06 -07002082 max_height = min(max_height, dev_priv->stdu_max_height);
Sinclair Yeh28c95422017-03-27 11:12:27 -07002083 max_height = min(max_height, dev_priv->texture_max_height);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002084 }
2085
2086 /* Add preferred mode */
Sinclair Yehc8261a92015-06-26 01:23:42 -07002087 mode = drm_mode_duplicate(dev, &prefmode);
2088 if (!mode)
2089 return 0;
2090 mode->hdisplay = du->pref_width;
2091 mode->vdisplay = du->pref_height;
2092 vmw_guess_mode_timing(mode);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002093
Sinclair Yehc8261a92015-06-26 01:23:42 -07002094 if (vmw_kms_validate_mode_vram(dev_priv,
2095 mode->hdisplay * assumed_bpp,
2096 mode->vdisplay)) {
2097 drm_mode_probed_add(connector, mode);
2098 } else {
2099 drm_mode_destroy(dev, mode);
2100 mode = NULL;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002101 }
2102
Sinclair Yehc8261a92015-06-26 01:23:42 -07002103 if (du->pref_mode) {
2104 list_del_init(&du->pref_mode->head);
2105 drm_mode_destroy(dev, du->pref_mode);
2106 }
2107
2108 /* mode might be null here, this is intended */
2109 du->pref_mode = mode;
2110
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002111 for (i = 0; vmw_kms_connector_builtin[i].type != 0; i++) {
2112 bmode = &vmw_kms_connector_builtin[i];
2113 if (bmode->hdisplay > max_width ||
2114 bmode->vdisplay > max_height)
2115 continue;
2116
Sinclair Yeh9a723842014-10-31 09:58:06 +01002117 if (!vmw_kms_validate_mode_vram(dev_priv,
2118 bmode->hdisplay * assumed_bpp,
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002119 bmode->vdisplay))
2120 continue;
2121
2122 mode = drm_mode_duplicate(dev, bmode);
2123 if (!mode)
2124 return 0;
2125 mode->vrefresh = drm_mode_vrefresh(mode);
2126
2127 drm_mode_probed_add(connector, mode);
2128 }
2129
Ville Syrjälä6af3e652015-12-03 23:14:14 +02002130 drm_mode_connector_list_update(connector);
Thomas Hellstromf6b05002015-06-29 12:59:58 -07002131 /* Move the prefered mode first, help apps pick the right mode. */
2132 drm_mode_sort(&connector->modes);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002133
2134 return 1;
2135}
2136
2137int vmw_du_connector_set_property(struct drm_connector *connector,
2138 struct drm_property *property,
2139 uint64_t val)
2140{
Thomas Hellstrom76404ac2016-02-12 09:55:45 +01002141 struct vmw_display_unit *du = vmw_connector_to_du(connector);
2142 struct vmw_private *dev_priv = vmw_priv(connector->dev);
2143
2144 if (property == dev_priv->implicit_placement_property)
2145 du->is_implicit = val;
2146
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002147 return 0;
2148}
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002149
2150
Sinclair Yeh9c2542a2017-03-23 11:33:39 -07002151
Sinclair Yehd7721ca2017-03-23 11:48:44 -07002152/**
2153 * vmw_du_connector_atomic_set_property - Atomic version of get property
2154 *
2155 * @crtc - crtc the property is associated with
2156 *
2157 * Returns:
2158 * Zero on success, negative errno on failure.
2159 */
2160int
2161vmw_du_connector_atomic_set_property(struct drm_connector *connector,
2162 struct drm_connector_state *state,
2163 struct drm_property *property,
2164 uint64_t val)
2165{
2166 struct vmw_private *dev_priv = vmw_priv(connector->dev);
2167 struct vmw_connector_state *vcs = vmw_connector_state_to_vcs(state);
2168 struct vmw_display_unit *du = vmw_connector_to_du(connector);
2169
2170
2171 if (property == dev_priv->implicit_placement_property) {
2172 vcs->is_implicit = val;
2173
2174 /*
2175 * We should really be doing a drm_atomic_commit() to
2176 * commit the new state, but since this doesn't cause
2177 * an immedate state change, this is probably ok
2178 */
2179 du->is_implicit = vcs->is_implicit;
2180 } else {
2181 return -EINVAL;
2182 }
2183
2184 return 0;
2185}
2186
2187
2188/**
2189 * vmw_du_connector_atomic_get_property - Atomic version of get property
2190 *
2191 * @connector - connector the property is associated with
2192 *
2193 * Returns:
2194 * Zero on success, negative errno on failure.
2195 */
2196int
2197vmw_du_connector_atomic_get_property(struct drm_connector *connector,
2198 const struct drm_connector_state *state,
2199 struct drm_property *property,
2200 uint64_t *val)
2201{
2202 struct vmw_private *dev_priv = vmw_priv(connector->dev);
2203 struct vmw_connector_state *vcs = vmw_connector_state_to_vcs(state);
2204
2205 if (property == dev_priv->implicit_placement_property)
2206 *val = vcs->is_implicit;
2207 else {
2208 DRM_ERROR("Invalid Property %s\n", property->name);
2209 return -EINVAL;
2210 }
2211
2212 return 0;
2213}
2214
2215
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002216int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data,
2217 struct drm_file *file_priv)
2218{
2219 struct vmw_private *dev_priv = vmw_priv(dev);
2220 struct drm_vmw_update_layout_arg *arg =
2221 (struct drm_vmw_update_layout_arg *)data;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002222 void __user *user_rects;
2223 struct drm_vmw_rect *rects;
2224 unsigned rects_size;
2225 int ret;
2226 int i;
Sinclair Yeh65ade7d2015-07-16 10:49:13 -07002227 u64 total_pixels = 0;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002228 struct drm_mode_config *mode_config = &dev->mode_config;
Sinclair Yehc8261a92015-06-26 01:23:42 -07002229 struct drm_vmw_rect bounding_box = {0};
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002230
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002231 if (!arg->num_outputs) {
2232 struct drm_vmw_rect def_rect = {0, 0, 800, 600};
2233 vmw_du_update_layout(dev_priv, 1, &def_rect);
Thomas Hellstrom5151adb2015-03-09 01:56:21 -07002234 return 0;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002235 }
2236
2237 rects_size = arg->num_outputs * sizeof(struct drm_vmw_rect);
Xi Wangbab9efc2011-11-28 12:25:43 +01002238 rects = kcalloc(arg->num_outputs, sizeof(struct drm_vmw_rect),
2239 GFP_KERNEL);
Thomas Hellstrom5151adb2015-03-09 01:56:21 -07002240 if (unlikely(!rects))
2241 return -ENOMEM;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002242
2243 user_rects = (void __user *)(unsigned long)arg->rects;
2244 ret = copy_from_user(rects, user_rects, rects_size);
2245 if (unlikely(ret != 0)) {
2246 DRM_ERROR("Failed to get rects.\n");
2247 ret = -EFAULT;
2248 goto out_free;
2249 }
2250
2251 for (i = 0; i < arg->num_outputs; ++i) {
Xi Wangbab9efc2011-11-28 12:25:43 +01002252 if (rects[i].x < 0 ||
2253 rects[i].y < 0 ||
2254 rects[i].x + rects[i].w > mode_config->max_width ||
2255 rects[i].y + rects[i].h > mode_config->max_height) {
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002256 DRM_ERROR("Invalid GUI layout.\n");
2257 ret = -EINVAL;
2258 goto out_free;
2259 }
Sinclair Yehc8261a92015-06-26 01:23:42 -07002260
2261 /*
2262 * bounding_box.w and bunding_box.h are used as
2263 * lower-right coordinates
2264 */
2265 if (rects[i].x + rects[i].w > bounding_box.w)
2266 bounding_box.w = rects[i].x + rects[i].w;
2267
2268 if (rects[i].y + rects[i].h > bounding_box.h)
2269 bounding_box.h = rects[i].y + rects[i].h;
Sinclair Yeh65ade7d2015-07-16 10:49:13 -07002270
2271 total_pixels += (u64) rects[i].w * (u64) rects[i].h;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002272 }
2273
Sinclair Yeh65ade7d2015-07-16 10:49:13 -07002274 if (dev_priv->active_display_unit == vmw_du_screen_target) {
2275 /*
2276 * For Screen Targets, the limits for a toplogy are:
2277 * 1. Bounding box (assuming 32bpp) must be < prim_bb_mem
2278 * 2. Total pixels (assuming 32bpp) must be < prim_bb_mem
2279 */
Thomas Hellstrom0f580382017-01-19 11:01:04 -08002280 u64 bb_mem = (u64) bounding_box.w * bounding_box.h * 4;
Sinclair Yeh65ade7d2015-07-16 10:49:13 -07002281 u64 pixel_mem = total_pixels * 4;
2282
2283 if (bb_mem > dev_priv->prim_bb_mem) {
2284 DRM_ERROR("Topology is beyond supported limits.\n");
Sinclair Yeh35c05122015-06-26 01:42:06 -07002285 ret = -EINVAL;
2286 goto out_free;
2287 }
2288
Sinclair Yeh65ade7d2015-07-16 10:49:13 -07002289 if (pixel_mem > dev_priv->prim_bb_mem) {
2290 DRM_ERROR("Combined output size too large\n");
2291 ret = -EINVAL;
2292 goto out_free;
2293 }
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002294 }
2295
2296 vmw_du_update_layout(dev_priv, arg->num_outputs, rects);
2297
2298out_free:
2299 kfree(rects);
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002300 return ret;
2301}
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07002302
2303/**
2304 * vmw_kms_helper_dirty - Helper to build commands and perform actions based
2305 * on a set of cliprects and a set of display units.
2306 *
2307 * @dev_priv: Pointer to a device private structure.
2308 * @framebuffer: Pointer to the framebuffer on which to perform the actions.
2309 * @clips: A set of struct drm_clip_rect. Either this os @vclips must be NULL.
2310 * Cliprects are given in framebuffer coordinates.
2311 * @vclips: A set of struct drm_vmw_rect cliprects. Either this or @clips must
2312 * be NULL. Cliprects are given in source coordinates.
2313 * @dest_x: X coordinate offset for the crtc / destination clip rects.
2314 * @dest_y: Y coordinate offset for the crtc / destination clip rects.
2315 * @num_clips: Number of cliprects in the @clips or @vclips array.
2316 * @increment: Integer with which to increment the clip counter when looping.
2317 * Used to skip a predetermined number of clip rects.
2318 * @dirty: Closure structure. See the description of struct vmw_kms_dirty.
2319 */
2320int vmw_kms_helper_dirty(struct vmw_private *dev_priv,
2321 struct vmw_framebuffer *framebuffer,
2322 const struct drm_clip_rect *clips,
2323 const struct drm_vmw_rect *vclips,
2324 s32 dest_x, s32 dest_y,
2325 int num_clips,
2326 int increment,
2327 struct vmw_kms_dirty *dirty)
2328{
2329 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
2330 struct drm_crtc *crtc;
2331 u32 num_units = 0;
2332 u32 i, k;
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07002333
2334 dirty->dev_priv = dev_priv;
2335
2336 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
2337 if (crtc->primary->fb != &framebuffer->base)
2338 continue;
2339 units[num_units++] = vmw_crtc_to_du(crtc);
2340 }
2341
2342 for (k = 0; k < num_units; k++) {
2343 struct vmw_display_unit *unit = units[k];
2344 s32 crtc_x = unit->crtc.x;
2345 s32 crtc_y = unit->crtc.y;
2346 s32 crtc_width = unit->crtc.mode.hdisplay;
2347 s32 crtc_height = unit->crtc.mode.vdisplay;
2348 const struct drm_clip_rect *clips_ptr = clips;
2349 const struct drm_vmw_rect *vclips_ptr = vclips;
2350
2351 dirty->unit = unit;
2352 if (dirty->fifo_reserve_size > 0) {
2353 dirty->cmd = vmw_fifo_reserve(dev_priv,
2354 dirty->fifo_reserve_size);
2355 if (!dirty->cmd) {
2356 DRM_ERROR("Couldn't reserve fifo space "
2357 "for dirty blits.\n");
Christian Engelmayerf3b8c0c2015-09-19 00:32:24 +02002358 return -ENOMEM;
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07002359 }
2360 memset(dirty->cmd, 0, dirty->fifo_reserve_size);
2361 }
2362 dirty->num_hits = 0;
2363 for (i = 0; i < num_clips; i++, clips_ptr += increment,
2364 vclips_ptr += increment) {
2365 s32 clip_left;
2366 s32 clip_top;
2367
2368 /*
2369 * Select clip array type. Note that integer type
2370 * in @clips is unsigned short, whereas in @vclips
2371 * it's 32-bit.
2372 */
2373 if (clips) {
2374 dirty->fb_x = (s32) clips_ptr->x1;
2375 dirty->fb_y = (s32) clips_ptr->y1;
2376 dirty->unit_x2 = (s32) clips_ptr->x2 + dest_x -
2377 crtc_x;
2378 dirty->unit_y2 = (s32) clips_ptr->y2 + dest_y -
2379 crtc_y;
2380 } else {
2381 dirty->fb_x = vclips_ptr->x;
2382 dirty->fb_y = vclips_ptr->y;
2383 dirty->unit_x2 = dirty->fb_x + vclips_ptr->w +
2384 dest_x - crtc_x;
2385 dirty->unit_y2 = dirty->fb_y + vclips_ptr->h +
2386 dest_y - crtc_y;
2387 }
2388
2389 dirty->unit_x1 = dirty->fb_x + dest_x - crtc_x;
2390 dirty->unit_y1 = dirty->fb_y + dest_y - crtc_y;
2391
2392 /* Skip this clip if it's outside the crtc region */
2393 if (dirty->unit_x1 >= crtc_width ||
2394 dirty->unit_y1 >= crtc_height ||
2395 dirty->unit_x2 <= 0 || dirty->unit_y2 <= 0)
2396 continue;
2397
2398 /* Clip right and bottom to crtc limits */
2399 dirty->unit_x2 = min_t(s32, dirty->unit_x2,
2400 crtc_width);
2401 dirty->unit_y2 = min_t(s32, dirty->unit_y2,
2402 crtc_height);
2403
2404 /* Clip left and top to crtc limits */
2405 clip_left = min_t(s32, dirty->unit_x1, 0);
2406 clip_top = min_t(s32, dirty->unit_y1, 0);
2407 dirty->unit_x1 -= clip_left;
2408 dirty->unit_y1 -= clip_top;
2409 dirty->fb_x -= clip_left;
2410 dirty->fb_y -= clip_top;
2411
2412 dirty->clip(dirty);
2413 }
2414
2415 dirty->fifo_commit(dirty);
2416 }
2417
2418 return 0;
2419}
2420
2421/**
2422 * vmw_kms_helper_buffer_prepare - Reserve and validate a buffer object before
2423 * command submission.
2424 *
2425 * @dev_priv. Pointer to a device private structure.
2426 * @buf: The buffer object
2427 * @interruptible: Whether to perform waits as interruptible.
2428 * @validate_as_mob: Whether the buffer should be validated as a MOB. If false,
2429 * The buffer will be validated as a GMR. Already pinned buffers will not be
2430 * validated.
2431 *
2432 * Returns 0 on success, negative error code on failure, -ERESTARTSYS if
2433 * interrupted by a signal.
2434 */
2435int vmw_kms_helper_buffer_prepare(struct vmw_private *dev_priv,
2436 struct vmw_dma_buffer *buf,
2437 bool interruptible,
2438 bool validate_as_mob)
2439{
2440 struct ttm_buffer_object *bo = &buf->base;
2441 int ret;
2442
Christian Königdfd5e502016-04-06 11:12:03 +02002443 ttm_bo_reserve(bo, false, false, NULL);
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07002444 ret = vmw_validate_single_buffer(dev_priv, bo, interruptible,
2445 validate_as_mob);
2446 if (ret)
2447 ttm_bo_unreserve(bo);
2448
2449 return ret;
2450}
2451
2452/**
2453 * vmw_kms_helper_buffer_revert - Undo the actions of
2454 * vmw_kms_helper_buffer_prepare.
2455 *
2456 * @res: Pointer to the buffer object.
2457 *
2458 * Helper to be used if an error forces the caller to undo the actions of
2459 * vmw_kms_helper_buffer_prepare.
2460 */
2461void vmw_kms_helper_buffer_revert(struct vmw_dma_buffer *buf)
2462{
2463 if (buf)
2464 ttm_bo_unreserve(&buf->base);
2465}
2466
2467/**
2468 * vmw_kms_helper_buffer_finish - Unreserve and fence a buffer object after
2469 * kms command submission.
2470 *
2471 * @dev_priv: Pointer to a device private structure.
2472 * @file_priv: Pointer to a struct drm_file representing the caller's
2473 * connection. Must be set to NULL if @user_fence_rep is NULL, and conversely
2474 * if non-NULL, @user_fence_rep must be non-NULL.
2475 * @buf: The buffer object.
2476 * @out_fence: Optional pointer to a fence pointer. If non-NULL, a
2477 * ref-counted fence pointer is returned here.
2478 * @user_fence_rep: Optional pointer to a user-space provided struct
2479 * drm_vmw_fence_rep. If provided, @file_priv must also be provided and the
2480 * function copies fence data to user-space in a fail-safe manner.
2481 */
2482void vmw_kms_helper_buffer_finish(struct vmw_private *dev_priv,
2483 struct drm_file *file_priv,
2484 struct vmw_dma_buffer *buf,
2485 struct vmw_fence_obj **out_fence,
2486 struct drm_vmw_fence_rep __user *
2487 user_fence_rep)
2488{
2489 struct vmw_fence_obj *fence;
2490 uint32_t handle;
2491 int ret;
2492
2493 ret = vmw_execbuf_fence_commands(file_priv, dev_priv, &fence,
2494 file_priv ? &handle : NULL);
2495 if (buf)
2496 vmw_fence_single_bo(&buf->base, fence);
2497 if (file_priv)
2498 vmw_execbuf_copy_fence_user(dev_priv, vmw_fpriv(file_priv),
2499 ret, user_fence_rep, fence,
Sinclair Yehc906965d2017-07-05 01:49:32 -07002500 handle, -1, NULL);
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07002501 if (out_fence)
2502 *out_fence = fence;
2503 else
2504 vmw_fence_obj_unreference(&fence);
2505
2506 vmw_kms_helper_buffer_revert(buf);
2507}
2508
2509
2510/**
2511 * vmw_kms_helper_resource_revert - Undo the actions of
2512 * vmw_kms_helper_resource_prepare.
2513 *
2514 * @res: Pointer to the resource. Typically a surface.
2515 *
2516 * Helper to be used if an error forces the caller to undo the actions of
2517 * vmw_kms_helper_resource_prepare.
2518 */
Thomas Hellstrom73a88252018-03-21 10:18:38 +01002519void vmw_kms_helper_resource_revert(struct vmw_validation_ctx *ctx)
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07002520{
Thomas Hellstrom73a88252018-03-21 10:18:38 +01002521 struct vmw_resource *res = ctx->res;
2522
2523 vmw_kms_helper_buffer_revert(ctx->buf);
2524 vmw_dmabuf_unreference(&ctx->buf);
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002525 vmw_resource_unreserve(res, false, NULL, 0);
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07002526 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
2527}
2528
2529/**
2530 * vmw_kms_helper_resource_prepare - Reserve and validate a resource before
2531 * command submission.
2532 *
2533 * @res: Pointer to the resource. Typically a surface.
2534 * @interruptible: Whether to perform waits as interruptible.
2535 *
2536 * Reserves and validates also the backup buffer if a guest-backed resource.
2537 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
2538 * interrupted by a signal.
2539 */
2540int vmw_kms_helper_resource_prepare(struct vmw_resource *res,
Thomas Hellstrom73a88252018-03-21 10:18:38 +01002541 bool interruptible,
2542 struct vmw_validation_ctx *ctx)
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07002543{
2544 int ret = 0;
2545
Thomas Hellstrom73a88252018-03-21 10:18:38 +01002546 ctx->buf = NULL;
2547 ctx->res = res;
2548
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07002549 if (interruptible)
2550 ret = mutex_lock_interruptible(&res->dev_priv->cmdbuf_mutex);
2551 else
2552 mutex_lock(&res->dev_priv->cmdbuf_mutex);
2553
2554 if (unlikely(ret != 0))
2555 return -ERESTARTSYS;
2556
2557 ret = vmw_resource_reserve(res, interruptible, false);
2558 if (ret)
2559 goto out_unlock;
2560
2561 if (res->backup) {
2562 ret = vmw_kms_helper_buffer_prepare(res->dev_priv, res->backup,
2563 interruptible,
2564 res->dev_priv->has_mob);
2565 if (ret)
2566 goto out_unreserve;
Thomas Hellstrom73a88252018-03-21 10:18:38 +01002567
2568 ctx->buf = vmw_dmabuf_reference(res->backup);
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07002569 }
2570 ret = vmw_resource_validate(res);
2571 if (ret)
2572 goto out_revert;
2573 return 0;
2574
2575out_revert:
Thomas Hellstrom73a88252018-03-21 10:18:38 +01002576 vmw_kms_helper_buffer_revert(ctx->buf);
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07002577out_unreserve:
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002578 vmw_resource_unreserve(res, false, NULL, 0);
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07002579out_unlock:
2580 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
2581 return ret;
2582}
2583
2584/**
2585 * vmw_kms_helper_resource_finish - Unreserve and fence a resource after
2586 * kms command submission.
2587 *
2588 * @res: Pointer to the resource. Typically a surface.
2589 * @out_fence: Optional pointer to a fence pointer. If non-NULL, a
2590 * ref-counted fence pointer is returned here.
2591 */
Thomas Hellstrom73a88252018-03-21 10:18:38 +01002592void vmw_kms_helper_resource_finish(struct vmw_validation_ctx *ctx,
2593 struct vmw_fence_obj **out_fence)
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07002594{
Thomas Hellstrom73a88252018-03-21 10:18:38 +01002595 struct vmw_resource *res = ctx->res;
2596
2597 if (ctx->buf || out_fence)
2598 vmw_kms_helper_buffer_finish(res->dev_priv, NULL, ctx->buf,
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07002599 out_fence, NULL);
2600
Thomas Hellstromd80efd52015-08-10 10:39:35 -07002601 vmw_resource_unreserve(res, false, NULL, 0);
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07002602 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
2603}
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07002604
2605/**
2606 * vmw_kms_update_proxy - Helper function to update a proxy surface from
2607 * its backing MOB.
2608 *
2609 * @res: Pointer to the surface resource
2610 * @clips: Clip rects in framebuffer (surface) space.
2611 * @num_clips: Number of clips in @clips.
2612 * @increment: Integer with which to increment the clip counter when looping.
2613 * Used to skip a predetermined number of clip rects.
2614 *
2615 * This function makes sure the proxy surface is updated from its backing MOB
2616 * using the region given by @clips. The surface resource @res and its backing
2617 * MOB needs to be reserved and validated on call.
2618 */
2619int vmw_kms_update_proxy(struct vmw_resource *res,
2620 const struct drm_clip_rect *clips,
2621 unsigned num_clips,
2622 int increment)
2623{
2624 struct vmw_private *dev_priv = res->dev_priv;
2625 struct drm_vmw_size *size = &vmw_res_to_srf(res)->base_size;
2626 struct {
2627 SVGA3dCmdHeader header;
2628 SVGA3dCmdUpdateGBImage body;
2629 } *cmd;
2630 SVGA3dBox *box;
2631 size_t copy_size = 0;
2632 int i;
2633
2634 if (!clips)
2635 return 0;
2636
2637 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd) * num_clips);
2638 if (!cmd) {
2639 DRM_ERROR("Couldn't reserve fifo space for proxy surface "
2640 "update.\n");
2641 return -ENOMEM;
2642 }
2643
2644 for (i = 0; i < num_clips; ++i, clips += increment, ++cmd) {
2645 box = &cmd->body.box;
2646
2647 cmd->header.id = SVGA_3D_CMD_UPDATE_GB_IMAGE;
2648 cmd->header.size = sizeof(cmd->body);
2649 cmd->body.image.sid = res->id;
2650 cmd->body.image.face = 0;
2651 cmd->body.image.mipmap = 0;
2652
2653 if (clips->x1 > size->width || clips->x2 > size->width ||
2654 clips->y1 > size->height || clips->y2 > size->height) {
2655 DRM_ERROR("Invalid clips outsize of framebuffer.\n");
2656 return -EINVAL;
2657 }
2658
2659 box->x = clips->x1;
2660 box->y = clips->y1;
2661 box->z = 0;
2662 box->w = clips->x2 - clips->x1;
2663 box->h = clips->y2 - clips->y1;
2664 box->d = 1;
2665
2666 copy_size += sizeof(*cmd);
2667 }
2668
2669 vmw_fifo_commit(dev_priv, copy_size);
2670
2671 return 0;
2672}
Thomas Hellstroma2787242015-06-29 12:55:07 -07002673
2674int vmw_kms_fbdev_init_data(struct vmw_private *dev_priv,
2675 unsigned unit,
2676 u32 max_width,
2677 u32 max_height,
2678 struct drm_connector **p_con,
2679 struct drm_crtc **p_crtc,
2680 struct drm_display_mode **p_mode)
2681{
2682 struct drm_connector *con;
2683 struct vmw_display_unit *du;
2684 struct drm_display_mode *mode;
2685 int i = 0;
2686
2687 list_for_each_entry(con, &dev_priv->dev->mode_config.connector_list,
2688 head) {
2689 if (i == unit)
2690 break;
2691
2692 ++i;
2693 }
2694
2695 if (i != unit) {
2696 DRM_ERROR("Could not find initial display unit.\n");
2697 return -EINVAL;
2698 }
2699
2700 if (list_empty(&con->modes))
2701 (void) vmw_du_connector_fill_modes(con, max_width, max_height);
2702
2703 if (list_empty(&con->modes)) {
2704 DRM_ERROR("Could not find initial display mode.\n");
2705 return -EINVAL;
2706 }
2707
2708 du = vmw_connector_to_du(con);
2709 *p_con = con;
2710 *p_crtc = &du->crtc;
2711
2712 list_for_each_entry(mode, &con->modes, head) {
2713 if (mode->type & DRM_MODE_TYPE_PREFERRED)
2714 break;
2715 }
2716
2717 if (mode->type & DRM_MODE_TYPE_PREFERRED)
2718 *p_mode = mode;
2719 else {
2720 WARN_ONCE(true, "Could not find initial preferred mode.\n");
2721 *p_mode = list_first_entry(&con->modes,
2722 struct drm_display_mode,
2723 head);
2724 }
2725
2726 return 0;
2727}
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002728
2729/**
2730 * vmw_kms_del_active - unregister a crtc binding to the implicit framebuffer
2731 *
2732 * @dev_priv: Pointer to a device private struct.
2733 * @du: The display unit of the crtc.
2734 */
2735void vmw_kms_del_active(struct vmw_private *dev_priv,
2736 struct vmw_display_unit *du)
2737{
Thomas Hellstrom93cd1682016-05-03 11:24:35 +02002738 mutex_lock(&dev_priv->global_kms_state_mutex);
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002739 if (du->active_implicit) {
2740 if (--(dev_priv->num_implicit) == 0)
2741 dev_priv->implicit_fb = NULL;
2742 du->active_implicit = false;
2743 }
Thomas Hellstrom93cd1682016-05-03 11:24:35 +02002744 mutex_unlock(&dev_priv->global_kms_state_mutex);
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002745}
2746
2747/**
2748 * vmw_kms_add_active - register a crtc binding to an implicit framebuffer
2749 *
2750 * @vmw_priv: Pointer to a device private struct.
2751 * @du: The display unit of the crtc.
2752 * @vfb: The implicit framebuffer
2753 *
2754 * Registers a binding to an implicit framebuffer.
2755 */
2756void vmw_kms_add_active(struct vmw_private *dev_priv,
2757 struct vmw_display_unit *du,
2758 struct vmw_framebuffer *vfb)
2759{
Thomas Hellstrom93cd1682016-05-03 11:24:35 +02002760 mutex_lock(&dev_priv->global_kms_state_mutex);
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002761 WARN_ON_ONCE(!dev_priv->num_implicit && dev_priv->implicit_fb);
2762
2763 if (!du->active_implicit && du->is_implicit) {
2764 dev_priv->implicit_fb = vfb;
2765 du->active_implicit = true;
2766 dev_priv->num_implicit++;
2767 }
Thomas Hellstrom93cd1682016-05-03 11:24:35 +02002768 mutex_unlock(&dev_priv->global_kms_state_mutex);
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002769}
2770
2771/**
2772 * vmw_kms_screen_object_flippable - Check whether we can page-flip a crtc.
2773 *
2774 * @dev_priv: Pointer to device-private struct.
2775 * @crtc: The crtc we want to flip.
2776 *
2777 * Returns true or false depending whether it's OK to flip this crtc
2778 * based on the criterion that we must not have more than one implicit
2779 * frame-buffer at any one time.
2780 */
2781bool vmw_kms_crtc_flippable(struct vmw_private *dev_priv,
2782 struct drm_crtc *crtc)
2783{
2784 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
Thomas Hellstrom93cd1682016-05-03 11:24:35 +02002785 bool ret;
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002786
Thomas Hellstrom93cd1682016-05-03 11:24:35 +02002787 mutex_lock(&dev_priv->global_kms_state_mutex);
2788 ret = !du->is_implicit || dev_priv->num_implicit == 1;
2789 mutex_unlock(&dev_priv->global_kms_state_mutex);
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002790
Thomas Hellstrom93cd1682016-05-03 11:24:35 +02002791 return ret;
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002792}
2793
2794/**
2795 * vmw_kms_update_implicit_fb - Update the implicit fb.
2796 *
2797 * @dev_priv: Pointer to device-private struct.
2798 * @crtc: The crtc the new implicit frame-buffer is bound to.
2799 */
2800void vmw_kms_update_implicit_fb(struct vmw_private *dev_priv,
2801 struct drm_crtc *crtc)
2802{
2803 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
2804 struct vmw_framebuffer *vfb;
2805
Thomas Hellstrom93cd1682016-05-03 11:24:35 +02002806 mutex_lock(&dev_priv->global_kms_state_mutex);
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002807
2808 if (!du->is_implicit)
Thomas Hellstrom93cd1682016-05-03 11:24:35 +02002809 goto out_unlock;
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002810
2811 vfb = vmw_framebuffer_to_vfb(crtc->primary->fb);
2812 WARN_ON_ONCE(dev_priv->num_implicit != 1 &&
2813 dev_priv->implicit_fb != vfb);
2814
2815 dev_priv->implicit_fb = vfb;
Thomas Hellstrom93cd1682016-05-03 11:24:35 +02002816out_unlock:
2817 mutex_unlock(&dev_priv->global_kms_state_mutex);
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002818}
Thomas Hellstrom76404ac2016-02-12 09:55:45 +01002819
2820/**
2821 * vmw_kms_create_implicit_placement_proparty - Set up the implicit placement
2822 * property.
2823 *
2824 * @dev_priv: Pointer to a device private struct.
2825 * @immutable: Whether the property is immutable.
2826 *
2827 * Sets up the implicit placement property unless it's already set up.
2828 */
2829void
2830vmw_kms_create_implicit_placement_property(struct vmw_private *dev_priv,
2831 bool immutable)
2832{
2833 if (dev_priv->implicit_placement_property)
2834 return;
2835
2836 dev_priv->implicit_placement_property =
2837 drm_property_create_range(dev_priv->dev,
2838 immutable ?
2839 DRM_MODE_PROP_IMMUTABLE : 0,
2840 "implicit_placement", 0, 1);
2841
2842}
Sinclair Yeh904bb5e2017-03-23 14:29:22 -07002843
2844
2845/**
2846 * vmw_kms_set_config - Wrapper around drm_atomic_helper_set_config
2847 *
2848 * @set: The configuration to set.
2849 *
2850 * The vmwgfx Xorg driver doesn't assign the mode::type member, which
2851 * when drm_mode_set_crtcinfo is called as part of the configuration setting
2852 * causes it to return incorrect crtc dimensions causing severe problems in
2853 * the vmwgfx modesetting. So explicitly clear that member before calling
2854 * into drm_atomic_helper_set_config.
2855 */
Dave Airlie320d8c32017-04-03 16:30:24 +10002856int vmw_kms_set_config(struct drm_mode_set *set,
2857 struct drm_modeset_acquire_ctx *ctx)
Sinclair Yeh904bb5e2017-03-23 14:29:22 -07002858{
2859 if (set && set->mode)
2860 set->mode->type = 0;
2861
Dave Airlie320d8c32017-04-03 16:30:24 +10002862 return drm_atomic_helper_set_config(set, ctx);
Sinclair Yeh904bb5e2017-03-23 14:29:22 -07002863}
Thomas Hellstrom140bcaa2018-03-08 10:07:37 +01002864
2865
2866/**
2867 * vmw_kms_lost_device - Notify kms that modesetting capabilities will be lost
2868 *
2869 * @dev: Pointer to the drm device
2870 */
2871void vmw_kms_lost_device(struct drm_device *dev)
2872{
2873 drm_atomic_helper_shutdown(dev);
2874}