blob: d6487376838fad2ef9fab4457f2e05b9bd568912 [file] [log] [blame]
Dirk Hohndel (VMware)dff96882018-05-07 01:16:26 +02001// SPDX-License-Identifier: GPL-2.0 OR MIT
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00002/**************************************************************************
3 *
Dirk Hohndel (VMware)dff96882018-05-07 01:16:26 +02004 * Copyright 2009-2015 VMware, Inc., Palo Alto, CA., USA
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00005 *
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
Sinclair Yeh9c2542a2017-03-23 11:33:39 -070028#include <drm/drm_atomic.h>
29#include <drm/drm_atomic_helper.h>
Deepak Rawate41774c2018-07-11 15:42:45 -070030#include <drm/drm_damage_helper.h>
Sam Ravnborgd5c1f012019-06-23 12:23:33 +020031#include <drm/drm_fourcc.h>
32#include <drm/drm_plane_helper.h>
33#include <drm/drm_rect.h>
34#include <drm/drm_sysfs.h>
35#include <drm/drm_vblank.h>
36
37#include "vmwgfx_kms.h"
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000038
Sinclair Yehc8261a92015-06-26 01:23:42 -070039void vmw_du_cleanup(struct vmw_display_unit *du)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000040{
Sinclair Yeh36cc79b2017-03-23 11:28:11 -070041 drm_plane_cleanup(&du->primary);
42 drm_plane_cleanup(&du->cursor);
43
Thomas Wood34ea3d32014-05-29 16:57:41 +010044 drm_connector_unregister(&du->connector);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000045 drm_crtc_cleanup(&du->crtc);
46 drm_encoder_cleanup(&du->encoder);
47 drm_connector_cleanup(&du->connector);
48}
49
50/*
51 * Display Unit Cursor functions
52 */
53
Sinclair Yeh36cc79b2017-03-23 11:28:11 -070054static int vmw_cursor_update_image(struct vmw_private *dev_priv,
55 u32 *image, u32 width, u32 height,
56 u32 hotspotX, u32 hotspotY)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000057{
58 struct {
59 u32 cmd;
60 SVGAFifoCmdDefineAlphaCursor cursor;
61 } *cmd;
62 u32 image_size = width * height * 4;
63 u32 cmd_size = sizeof(*cmd) + image_size;
64
65 if (!image)
66 return -EINVAL;
67
Zack Rusin8426ed92020-11-18 12:54:19 -050068 cmd = VMW_CMD_RESERVE(dev_priv, cmd_size);
Deepak Rawat11c45412019-02-14 16:15:39 -080069 if (unlikely(cmd == NULL))
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000070 return -ENOMEM;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000071
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
Zack Rusin8426ed92020-11-18 12:54:19 -050083 vmw_cmd_commit_flush(dev_priv, cmd_size);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +000084
85 return 0;
86}
87
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +020088static int vmw_cursor_update_bo(struct vmw_private *dev_priv,
89 struct vmw_buffer_object *bo,
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
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +0200103 ret = ttm_bo_reserve(&bo->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
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +0200109 ret = ttm_bo_kmap(&bo->base, kmap_offset, kmap_num, &map);
Jakob Bornecrantz6a91d972011-11-28 13:19:10 +0100110 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:
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +0200119 ttm_bo_unreserve(&bo->base);
Jakob Bornecrantz6a91d972011-11-28 13:19:10 +0100120
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{
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000128 uint32_t count;
129
Sinclair Yeh36cc79b2017-03-23 11:28:11 -0700130 spin_lock(&dev_priv->cursor_lock);
Zack Rusinbe4f77a2020-11-04 13:46:09 -0500131 vmw_fifo_mem_write(dev_priv, SVGA_FIFO_CURSOR_ON, show ? 1 : 0);
132 vmw_fifo_mem_write(dev_priv, SVGA_FIFO_CURSOR_X, x);
133 vmw_fifo_mem_write(dev_priv, SVGA_FIFO_CURSOR_Y, y);
134 count = vmw_fifo_mem_read(dev_priv, SVGA_FIFO_CURSOR_COUNT);
135 vmw_fifo_mem_write(dev_priv, SVGA_FIFO_CURSOR_COUNT, ++count);
Sinclair Yeh36cc79b2017-03-23 11:28:11 -0700136 spin_unlock(&dev_priv->cursor_lock);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000137}
138
Thomas Hellstrom8fbf9d92015-11-26 19:45:16 +0100139
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000140void vmw_kms_cursor_snoop(struct vmw_surface *srf,
141 struct ttm_object_file *tfile,
142 struct ttm_buffer_object *bo,
143 SVGA3dCmdHeader *header)
144{
145 struct ttm_bo_kmap_obj map;
146 unsigned long kmap_offset;
147 unsigned long kmap_num;
148 SVGA3dCopyBox *box;
149 unsigned box_count;
150 void *virtual;
151 bool dummy;
152 struct vmw_dma_cmd {
153 SVGA3dCmdHeader header;
154 SVGA3dCmdSurfaceDMA dma;
155 } *cmd;
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100156 int i, ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000157
158 cmd = container_of(header, struct vmw_dma_cmd, header);
159
160 /* No snooper installed */
161 if (!srf->snooper.image)
162 return;
163
164 if (cmd->dma.host.face != 0 || cmd->dma.host.mipmap != 0) {
165 DRM_ERROR("face and mipmap for cursors should never != 0\n");
166 return;
167 }
168
169 if (cmd->header.size < 64) {
170 DRM_ERROR("at least one full copy box must be given\n");
171 return;
172 }
173
174 box = (SVGA3dCopyBox *)&cmd[1];
175 box_count = (cmd->header.size - sizeof(SVGA3dCmdSurfaceDMA)) /
176 sizeof(SVGA3dCopyBox);
177
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100178 if (cmd->dma.guest.ptr.offset % PAGE_SIZE ||
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000179 box->x != 0 || box->y != 0 || box->z != 0 ||
180 box->srcx != 0 || box->srcy != 0 || box->srcz != 0 ||
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100181 box->d != 1 || box_count != 1) {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000182 /* TODO handle none page aligned offsets */
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100183 /* TODO handle more dst & src != 0 */
184 /* TODO handle more then one copy */
Colin Ian Kinge97644e2020-08-10 11:04:01 +0100185 DRM_ERROR("Can't snoop dma request for cursor!\n");
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100186 DRM_ERROR("(%u, %u, %u) (%u, %u, %u) (%ux%ux%u) %u %u\n",
187 box->srcx, box->srcy, box->srcz,
188 box->x, box->y, box->z,
189 box->w, box->h, box->d, box_count,
190 cmd->dma.guest.ptr.offset);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000191 return;
192 }
193
194 kmap_offset = cmd->dma.guest.ptr.offset >> PAGE_SHIFT;
195 kmap_num = (64*64*4) >> PAGE_SHIFT;
196
Christian Königdfd5e502016-04-06 11:12:03 +0200197 ret = ttm_bo_reserve(bo, true, false, NULL);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000198 if (unlikely(ret != 0)) {
199 DRM_ERROR("reserve failed\n");
200 return;
201 }
202
203 ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
204 if (unlikely(ret != 0))
205 goto err_unreserve;
206
207 virtual = ttm_kmap_obj_virtual(&map, &dummy);
208
Jakob Bornecrantz2ac86372011-11-03 21:03:08 +0100209 if (box->w == 64 && cmd->dma.guest.pitch == 64*4) {
210 memcpy(srf->snooper.image, virtual, 64*64*4);
211 } else {
212 /* Image is unsigned pointer. */
213 for (i = 0; i < box->h; i++)
214 memcpy(srf->snooper.image + i * 64,
215 virtual + i * cmd->dma.guest.pitch,
216 box->w * 4);
217 }
218
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000219 srf->snooper.age++;
220
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000221 ttm_bo_kunmap(&map);
222err_unreserve:
223 ttm_bo_unreserve(bo);
224}
225
Thomas Hellstrom8fbf9d92015-11-26 19:45:16 +0100226/**
227 * vmw_kms_legacy_hotspot_clear - Clear legacy hotspots
228 *
229 * @dev_priv: Pointer to the device private struct.
230 *
231 * Clears all legacy hotspots.
232 */
233void vmw_kms_legacy_hotspot_clear(struct vmw_private *dev_priv)
234{
Zack Rusin9703bb32020-11-03 22:21:34 -0500235 struct drm_device *dev = &dev_priv->drm;
Thomas Hellstrom8fbf9d92015-11-26 19:45:16 +0100236 struct vmw_display_unit *du;
237 struct drm_crtc *crtc;
238
239 drm_modeset_lock_all(dev);
240 drm_for_each_crtc(crtc, dev) {
241 du = vmw_crtc_to_du(crtc);
242
243 du->hotspot_x = 0;
244 du->hotspot_y = 0;
245 }
246 drm_modeset_unlock_all(dev);
247}
248
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000249void vmw_kms_cursor_post_execbuf(struct vmw_private *dev_priv)
250{
Zack Rusin9703bb32020-11-03 22:21:34 -0500251 struct drm_device *dev = &dev_priv->drm;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000252 struct vmw_display_unit *du;
253 struct drm_crtc *crtc;
254
255 mutex_lock(&dev->mode_config.mutex);
256
257 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
258 du = vmw_crtc_to_du(crtc);
259 if (!du->cursor_surface ||
260 du->cursor_age == du->cursor_surface->snooper.age)
261 continue;
262
263 du->cursor_age = du->cursor_surface->snooper.age;
264 vmw_cursor_update_image(dev_priv,
265 du->cursor_surface->snooper.image,
Thomas Hellstrom8fbf9d92015-11-26 19:45:16 +0100266 64, 64,
267 du->hotspot_x + du->core_hotspot_x,
268 du->hotspot_y + du->core_hotspot_y);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000269 }
270
271 mutex_unlock(&dev->mode_config.mutex);
272}
273
Sinclair Yeh36cc79b2017-03-23 11:28:11 -0700274
Sinclair Yeh36cc79b2017-03-23 11:28:11 -0700275void vmw_du_cursor_plane_destroy(struct drm_plane *plane)
276{
277 vmw_cursor_update_position(plane->dev->dev_private, false, 0, 0);
278
279 drm_plane_cleanup(plane);
280}
281
282
283void vmw_du_primary_plane_destroy(struct drm_plane *plane)
284{
285 drm_plane_cleanup(plane);
286
287 /* Planes are static in our case so we don't free it */
288}
289
290
Sinclair Yeh060e2ad2017-03-23 14:18:32 -0700291/**
292 * vmw_du_vps_unpin_surf - unpins resource associated with a framebuffer surface
293 *
294 * @vps: plane state associated with the display surface
295 * @unreference: true if we also want to unreference the display.
296 */
297void vmw_du_plane_unpin_surf(struct vmw_plane_state *vps,
298 bool unreference)
299{
300 if (vps->surf) {
301 if (vps->pinned) {
302 vmw_resource_unpin(&vps->surf->res);
303 vps->pinned--;
304 }
305
306 if (unreference) {
307 if (vps->pinned)
308 DRM_ERROR("Surface still pinned\n");
309 vmw_surface_unreference(&vps->surf);
310 }
311 }
312}
313
314
315/**
316 * vmw_du_plane_cleanup_fb - Unpins the cursor
317 *
318 * @plane: display plane
319 * @old_state: Contains the FB to clean up
320 *
321 * Unpins the framebuffer surface
322 *
323 * Returns 0 on success
324 */
325void
326vmw_du_plane_cleanup_fb(struct drm_plane *plane,
327 struct drm_plane_state *old_state)
328{
329 struct vmw_plane_state *vps = vmw_plane_state_to_vps(old_state);
330
331 vmw_du_plane_unpin_surf(vps, false);
332}
333
334
335/**
336 * vmw_du_cursor_plane_prepare_fb - Readies the cursor by referencing it
337 *
338 * @plane: display plane
339 * @new_state: info on the new plane state, including the FB
340 *
341 * Returns 0 on success
342 */
343int
344vmw_du_cursor_plane_prepare_fb(struct drm_plane *plane,
345 struct drm_plane_state *new_state)
346{
347 struct drm_framebuffer *fb = new_state->fb;
348 struct vmw_plane_state *vps = vmw_plane_state_to_vps(new_state);
349
350
351 if (vps->surf)
352 vmw_surface_unreference(&vps->surf);
353
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +0200354 if (vps->bo)
355 vmw_bo_unreference(&vps->bo);
Sinclair Yeh060e2ad2017-03-23 14:18:32 -0700356
357 if (fb) {
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +0200358 if (vmw_framebuffer_to_vfb(fb)->bo) {
359 vps->bo = vmw_framebuffer_to_vfbd(fb)->buffer;
360 vmw_bo_reference(vps->bo);
Sinclair Yeh060e2ad2017-03-23 14:18:32 -0700361 } else {
362 vps->surf = vmw_framebuffer_to_vfbs(fb)->surface;
363 vmw_surface_reference(vps->surf);
364 }
365 }
366
367 return 0;
368}
369
370
371void
Sinclair Yeh060e2ad2017-03-23 14:18:32 -0700372vmw_du_cursor_plane_atomic_update(struct drm_plane *plane,
373 struct drm_plane_state *old_state)
374{
375 struct drm_crtc *crtc = plane->state->crtc ?: old_state->crtc;
376 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
377 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
378 struct vmw_plane_state *vps = vmw_plane_state_to_vps(plane->state);
379 s32 hotspot_x, hotspot_y;
380 int ret = 0;
381
382
383 hotspot_x = du->hotspot_x;
384 hotspot_y = du->hotspot_y;
Sinclair Yeh14979ad2017-07-17 23:26:21 -0700385
Ville Syrjälä2bb01c42018-03-22 17:23:02 +0200386 if (plane->state->fb) {
387 hotspot_x += plane->state->fb->hot_x;
388 hotspot_y += plane->state->fb->hot_y;
Sinclair Yeh14979ad2017-07-17 23:26:21 -0700389 }
390
Sinclair Yeh060e2ad2017-03-23 14:18:32 -0700391 du->cursor_surface = vps->surf;
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +0200392 du->cursor_bo = vps->bo;
Sinclair Yeh060e2ad2017-03-23 14:18:32 -0700393
Sinclair Yeh060e2ad2017-03-23 14:18:32 -0700394 if (vps->surf) {
395 du->cursor_age = du->cursor_surface->snooper.age;
396
397 ret = vmw_cursor_update_image(dev_priv,
398 vps->surf->snooper.image,
Thomas Hellstrom25db8752018-01-16 08:54:30 +0100399 64, 64, hotspot_x,
400 hotspot_y);
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +0200401 } else if (vps->bo) {
402 ret = vmw_cursor_update_bo(dev_priv, vps->bo,
403 plane->state->crtc_w,
404 plane->state->crtc_h,
405 hotspot_x, hotspot_y);
Sinclair Yeh060e2ad2017-03-23 14:18:32 -0700406 } else {
407 vmw_cursor_update_position(dev_priv, false, 0, 0);
408 return;
409 }
410
411 if (!ret) {
412 du->cursor_x = plane->state->crtc_x + du->set_gui_x;
413 du->cursor_y = plane->state->crtc_y + du->set_gui_y;
414
415 vmw_cursor_update_position(dev_priv, true,
416 du->cursor_x + hotspot_x,
417 du->cursor_y + hotspot_y);
Sinclair Yeh14979ad2017-07-17 23:26:21 -0700418
419 du->core_hotspot_x = hotspot_x - du->hotspot_x;
420 du->core_hotspot_y = hotspot_y - du->hotspot_y;
Sinclair Yeh060e2ad2017-03-23 14:18:32 -0700421 } else {
422 DRM_ERROR("Failed to update cursor image\n");
423 }
424}
425
426
427/**
428 * vmw_du_primary_plane_atomic_check - check if the new state is okay
429 *
430 * @plane: display plane
431 * @state: info on the new plane state, including the FB
432 *
433 * Check if the new state is settable given the current state. Other
434 * than what the atomic helper checks, we care about crtc fitting
435 * the FB and maintaining one active framebuffer.
436 *
437 * Returns 0 on success
438 */
439int vmw_du_primary_plane_atomic_check(struct drm_plane *plane,
Maxime Ripard7c11b992021-02-19 13:00:24 +0100440 struct drm_atomic_state *state)
Sinclair Yeh060e2ad2017-03-23 14:18:32 -0700441{
Maxime Ripard7c11b992021-02-19 13:00:24 +0100442 struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
443 plane);
Ville Syrjälä58a275aa92017-11-01 20:29:18 +0200444 struct drm_crtc_state *crtc_state = NULL;
Maxime Ripardba5c1642021-02-19 13:00:22 +0100445 struct drm_framebuffer *new_fb = new_state->fb;
Sinclair Yeh060e2ad2017-03-23 14:18:32 -0700446 int ret;
447
Maxime Ripardba5c1642021-02-19 13:00:22 +0100448 if (new_state->crtc)
Maxime Riparddec92022021-02-19 13:00:25 +0100449 crtc_state = drm_atomic_get_new_crtc_state(state,
Maxime Ripardba5c1642021-02-19 13:00:22 +0100450 new_state->crtc);
Sinclair Yeh060e2ad2017-03-23 14:18:32 -0700451
Maxime Ripardba5c1642021-02-19 13:00:22 +0100452 ret = drm_atomic_helper_check_plane_state(new_state, crtc_state,
Ville Syrjäläa01cb8b2017-11-01 22:16:19 +0200453 DRM_PLANE_HELPER_NO_SCALING,
454 DRM_PLANE_HELPER_NO_SCALING,
455 false, true);
Sinclair Yeh060e2ad2017-03-23 14:18:32 -0700456
457 if (!ret && new_fb) {
Maxime Ripardba5c1642021-02-19 13:00:22 +0100458 struct drm_crtc *crtc = new_state->crtc;
Sinclair Yeh060e2ad2017-03-23 14:18:32 -0700459 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
Sinclair Yeh060e2ad2017-03-23 14:18:32 -0700460
Lee Jonesabd64e52021-01-15 18:12:37 +0000461 vmw_connector_state_to_vcs(du->connector.state);
Sinclair Yeh060e2ad2017-03-23 14:18:32 -0700462 }
463
464
465 return ret;
466}
467
468
469/**
470 * vmw_du_cursor_plane_atomic_check - check if the new state is okay
471 *
472 * @plane: cursor plane
Lee Jonesc88c25a2021-01-15 18:12:53 +0000473 * @new_state: info on the new plane state
Sinclair Yeh060e2ad2017-03-23 14:18:32 -0700474 *
475 * This is a chance to fail if the new cursor state does not fit
476 * our requirements.
477 *
478 * Returns 0 on success
479 */
480int vmw_du_cursor_plane_atomic_check(struct drm_plane *plane,
Maxime Ripard7c11b992021-02-19 13:00:24 +0100481 struct drm_atomic_state *state)
Sinclair Yeh060e2ad2017-03-23 14:18:32 -0700482{
Maxime Ripard7c11b992021-02-19 13:00:24 +0100483 struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
484 plane);
Sinclair Yeh060e2ad2017-03-23 14:18:32 -0700485 int ret = 0;
Thomas Hellstrom40e3def2018-10-04 22:24:41 +0200486 struct drm_crtc_state *crtc_state = NULL;
Sinclair Yeh060e2ad2017-03-23 14:18:32 -0700487 struct vmw_surface *surface = NULL;
488 struct drm_framebuffer *fb = new_state->fb;
489
Thomas Hellstrom40e3def2018-10-04 22:24:41 +0200490 if (new_state->crtc)
491 crtc_state = drm_atomic_get_new_crtc_state(new_state->state,
492 new_state->crtc);
493
494 ret = drm_atomic_helper_check_plane_state(new_state, crtc_state,
495 DRM_PLANE_HELPER_NO_SCALING,
496 DRM_PLANE_HELPER_NO_SCALING,
497 true, true);
498 if (ret)
499 return ret;
Sinclair Yeh060e2ad2017-03-23 14:18:32 -0700500
501 /* Turning off */
502 if (!fb)
Thomas Hellstrom40e3def2018-10-04 22:24:41 +0200503 return 0;
Thomas Hellstrom25db8752018-01-16 08:54:30 +0100504
Sinclair Yeh060e2ad2017-03-23 14:18:32 -0700505 /* A lot of the code assumes this */
506 if (new_state->crtc_w != 64 || new_state->crtc_h != 64) {
507 DRM_ERROR("Invalid cursor dimensions (%d, %d)\n",
508 new_state->crtc_w, new_state->crtc_h);
509 ret = -EINVAL;
510 }
511
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +0200512 if (!vmw_framebuffer_to_vfb(fb)->bo)
Sinclair Yeh060e2ad2017-03-23 14:18:32 -0700513 surface = vmw_framebuffer_to_vfbs(fb)->surface;
514
515 if (surface && !surface->snooper.image) {
516 DRM_ERROR("surface not suitable for cursor\n");
517 ret = -EINVAL;
518 }
519
520 return ret;
521}
522
523
Sinclair Yeh06ec4192017-03-23 13:14:54 -0700524int vmw_du_crtc_atomic_check(struct drm_crtc *crtc,
Maxime Ripard29b77ad2020-10-28 13:32:21 +0100525 struct drm_atomic_state *state)
Sinclair Yeh06ec4192017-03-23 13:14:54 -0700526{
Maxime Ripard29b77ad2020-10-28 13:32:21 +0100527 struct drm_crtc_state *new_state = drm_atomic_get_new_crtc_state(state,
528 crtc);
Sinclair Yeh06ec4192017-03-23 13:14:54 -0700529 struct vmw_display_unit *du = vmw_crtc_to_du(new_state->crtc);
Ville Syrjäläea632722018-06-26 22:47:16 +0300530 int connector_mask = drm_connector_mask(&du->connector);
Sinclair Yeh06ec4192017-03-23 13:14:54 -0700531 bool has_primary = new_state->plane_mask &
Ville Syrjäläea632722018-06-26 22:47:16 +0300532 drm_plane_mask(crtc->primary);
Sinclair Yeh06ec4192017-03-23 13:14:54 -0700533
534 /* We always want to have an active plane with an active CRTC */
535 if (has_primary != new_state->enable)
536 return -EINVAL;
537
538
539 if (new_state->connector_mask != connector_mask &&
540 new_state->connector_mask != 0) {
541 DRM_ERROR("Invalid connectors configuration\n");
542 return -EINVAL;
543 }
544
545 /*
546 * Our virtual device does not have a dot clock, so use the logical
547 * clock value as the dot clock.
548 */
549 if (new_state->mode.crtc_clock == 0)
550 new_state->adjusted_mode.crtc_clock = new_state->mode.clock;
551
552 return 0;
553}
554
555
556void vmw_du_crtc_atomic_begin(struct drm_crtc *crtc,
Maxime Ripardf6ebe9f2020-10-28 13:32:22 +0100557 struct drm_atomic_state *state)
Sinclair Yeh06ec4192017-03-23 13:14:54 -0700558{
559}
560
561
562void vmw_du_crtc_atomic_flush(struct drm_crtc *crtc,
Maxime Ripardf6ebe9f2020-10-28 13:32:22 +0100563 struct drm_atomic_state *state)
Sinclair Yeh06ec4192017-03-23 13:14:54 -0700564{
565 struct drm_pending_vblank_event *event = crtc->state->event;
566
567 if (event) {
568 crtc->state->event = NULL;
569
570 spin_lock_irq(&crtc->dev->event_lock);
Deepak Rawat3cbe87f2018-01-16 08:27:17 +0100571 drm_crtc_send_vblank_event(crtc, event);
Sinclair Yeh06ec4192017-03-23 13:14:54 -0700572 spin_unlock_irq(&crtc->dev->event_lock);
573 }
Sinclair Yeh06ec4192017-03-23 13:14:54 -0700574}
575
576
Sinclair Yeh9c2542a2017-03-23 11:33:39 -0700577/**
578 * vmw_du_crtc_duplicate_state - duplicate crtc state
579 * @crtc: DRM crtc
580 *
581 * Allocates and returns a copy of the crtc state (both common and
582 * vmw-specific) for the specified crtc.
583 *
584 * Returns: The newly allocated crtc state, or NULL on failure.
585 */
586struct drm_crtc_state *
587vmw_du_crtc_duplicate_state(struct drm_crtc *crtc)
588{
589 struct drm_crtc_state *state;
590 struct vmw_crtc_state *vcs;
591
592 if (WARN_ON(!crtc->state))
593 return NULL;
594
595 vcs = kmemdup(crtc->state, sizeof(*vcs), GFP_KERNEL);
596
597 if (!vcs)
598 return NULL;
599
600 state = &vcs->base;
601
602 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
603
604 return state;
605}
606
607
608/**
609 * vmw_du_crtc_reset - creates a blank vmw crtc state
610 * @crtc: DRM crtc
611 *
612 * Resets the atomic state for @crtc by freeing the state pointer (which
613 * might be NULL, e.g. at driver load time) and allocating a new empty state
614 * object.
615 */
616void vmw_du_crtc_reset(struct drm_crtc *crtc)
617{
618 struct vmw_crtc_state *vcs;
619
620
621 if (crtc->state) {
622 __drm_atomic_helper_crtc_destroy_state(crtc->state);
623
624 kfree(vmw_crtc_state_to_vcs(crtc->state));
625 }
626
627 vcs = kzalloc(sizeof(*vcs), GFP_KERNEL);
628
629 if (!vcs) {
630 DRM_ERROR("Cannot allocate vmw_crtc_state\n");
631 return;
632 }
633
Daniel Vettera1643472020-06-12 22:49:40 +0200634 __drm_atomic_helper_crtc_reset(crtc, &vcs->base);
Sinclair Yeh9c2542a2017-03-23 11:33:39 -0700635}
636
637
638/**
639 * vmw_du_crtc_destroy_state - destroy crtc state
640 * @crtc: DRM crtc
641 * @state: state object to destroy
642 *
643 * Destroys the crtc state (both common and vmw-specific) for the
644 * specified plane.
645 */
646void
647vmw_du_crtc_destroy_state(struct drm_crtc *crtc,
648 struct drm_crtc_state *state)
649{
650 drm_atomic_helper_crtc_destroy_state(crtc, state);
651}
652
653
Sinclair Yehcc5ec452017-03-23 11:36:05 -0700654/**
655 * vmw_du_plane_duplicate_state - duplicate plane state
656 * @plane: drm plane
657 *
658 * Allocates and returns a copy of the plane state (both common and
659 * vmw-specific) for the specified plane.
660 *
661 * Returns: The newly allocated plane state, or NULL on failure.
662 */
663struct drm_plane_state *
664vmw_du_plane_duplicate_state(struct drm_plane *plane)
665{
666 struct drm_plane_state *state;
667 struct vmw_plane_state *vps;
668
669 vps = kmemdup(plane->state, sizeof(*vps), GFP_KERNEL);
670
671 if (!vps)
672 return NULL;
673
674 vps->pinned = 0;
Sinclair Yeh810b3e162017-03-23 15:39:16 -0700675 vps->cpp = 0;
676
Sinclair Yehcc5ec452017-03-23 11:36:05 -0700677 /* Each ref counted resource needs to be acquired again */
678 if (vps->surf)
679 (void) vmw_surface_reference(vps->surf);
680
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +0200681 if (vps->bo)
682 (void) vmw_bo_reference(vps->bo);
Sinclair Yehcc5ec452017-03-23 11:36:05 -0700683
684 state = &vps->base;
685
686 __drm_atomic_helper_plane_duplicate_state(plane, state);
687
688 return state;
689}
690
691
692/**
693 * vmw_du_plane_reset - creates a blank vmw plane state
694 * @plane: drm plane
695 *
696 * Resets the atomic state for @plane by freeing the state pointer (which might
697 * be NULL, e.g. at driver load time) and allocating a new empty state object.
698 */
699void vmw_du_plane_reset(struct drm_plane *plane)
700{
701 struct vmw_plane_state *vps;
702
703
704 if (plane->state)
705 vmw_du_plane_destroy_state(plane, plane->state);
706
707 vps = kzalloc(sizeof(*vps), GFP_KERNEL);
708
709 if (!vps) {
710 DRM_ERROR("Cannot allocate vmw_plane_state\n");
711 return;
712 }
713
Alexandru Gheorghee81eb982018-08-04 17:15:30 +0100714 __drm_atomic_helper_plane_reset(plane, &vps->base);
Sinclair Yehcc5ec452017-03-23 11:36:05 -0700715}
716
717
718/**
719 * vmw_du_plane_destroy_state - destroy plane state
720 * @plane: DRM plane
721 * @state: state object to destroy
722 *
723 * Destroys the plane state (both common and vmw-specific) for the
724 * specified plane.
725 */
726void
727vmw_du_plane_destroy_state(struct drm_plane *plane,
728 struct drm_plane_state *state)
729{
730 struct vmw_plane_state *vps = vmw_plane_state_to_vps(state);
731
732
Sinclair Yeh810b3e162017-03-23 15:39:16 -0700733 /* Should have been freed by cleanup_fb */
Sinclair Yehcc5ec452017-03-23 11:36:05 -0700734 if (vps->surf)
735 vmw_surface_unreference(&vps->surf);
736
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +0200737 if (vps->bo)
738 vmw_bo_unreference(&vps->bo);
Sinclair Yehcc5ec452017-03-23 11:36:05 -0700739
740 drm_atomic_helper_plane_destroy_state(plane, state);
741}
742
743
Sinclair Yehd7721ca2017-03-23 11:48:44 -0700744/**
745 * vmw_du_connector_duplicate_state - duplicate connector state
746 * @connector: DRM connector
747 *
748 * Allocates and returns a copy of the connector state (both common and
749 * vmw-specific) for the specified connector.
750 *
751 * Returns: The newly allocated connector state, or NULL on failure.
752 */
753struct drm_connector_state *
754vmw_du_connector_duplicate_state(struct drm_connector *connector)
755{
756 struct drm_connector_state *state;
757 struct vmw_connector_state *vcs;
758
759 if (WARN_ON(!connector->state))
760 return NULL;
761
762 vcs = kmemdup(connector->state, sizeof(*vcs), GFP_KERNEL);
763
764 if (!vcs)
765 return NULL;
766
767 state = &vcs->base;
768
769 __drm_atomic_helper_connector_duplicate_state(connector, state);
770
771 return state;
772}
773
774
775/**
776 * vmw_du_connector_reset - creates a blank vmw connector state
777 * @connector: DRM connector
778 *
779 * Resets the atomic state for @connector by freeing the state pointer (which
780 * might be NULL, e.g. at driver load time) and allocating a new empty state
781 * object.
782 */
783void vmw_du_connector_reset(struct drm_connector *connector)
784{
785 struct vmw_connector_state *vcs;
786
787
788 if (connector->state) {
789 __drm_atomic_helper_connector_destroy_state(connector->state);
790
791 kfree(vmw_connector_state_to_vcs(connector->state));
792 }
793
794 vcs = kzalloc(sizeof(*vcs), GFP_KERNEL);
795
796 if (!vcs) {
797 DRM_ERROR("Cannot allocate vmw_connector_state\n");
798 return;
799 }
800
801 __drm_atomic_helper_connector_reset(connector, &vcs->base);
802}
803
804
805/**
806 * vmw_du_connector_destroy_state - destroy connector state
807 * @connector: DRM connector
808 * @state: state object to destroy
809 *
810 * Destroys the connector state (both common and vmw-specific) for the
811 * specified plane.
812 */
813void
814vmw_du_connector_destroy_state(struct drm_connector *connector,
815 struct drm_connector_state *state)
816{
817 drm_atomic_helper_connector_destroy_state(connector, state);
818}
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000819/*
820 * Generic framebuffer code
821 */
822
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000823/*
824 * Surface framebuffer code
825 */
826
Rashika Kheria847c5962014-01-06 22:18:10 +0530827static void vmw_framebuffer_surface_destroy(struct drm_framebuffer *framebuffer)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000828{
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200829 struct vmw_framebuffer_surface *vfbs =
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000830 vmw_framebuffer_to_vfbs(framebuffer);
831
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000832 drm_framebuffer_cleanup(framebuffer);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200833 vmw_surface_unreference(&vfbs->surface);
Thomas Hellstroma2787242015-06-29 12:55:07 -0700834 if (vfbs->base.user_obj)
835 ttm_base_object_unref(&vfbs->base.user_obj);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000836
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200837 kfree(vfbs);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000838}
839
Thomas Hellstrom10b1e0c2015-06-26 02:14:27 -0700840/**
841 * vmw_kms_readback - Perform a readback from the screen system to
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +0200842 * a buffer-object backed framebuffer.
Thomas Hellstrom10b1e0c2015-06-26 02:14:27 -0700843 *
844 * @dev_priv: Pointer to the device private structure.
845 * @file_priv: Pointer to a struct drm_file identifying the caller.
846 * Must be set to NULL if @user_fence_rep is NULL.
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +0200847 * @vfb: Pointer to the buffer-object backed framebuffer.
Thomas Hellstrom10b1e0c2015-06-26 02:14:27 -0700848 * @user_fence_rep: User-space provided structure for fence information.
849 * Must be set to non-NULL if @file_priv is non-NULL.
850 * @vclips: Array of clip rects.
851 * @num_clips: Number of clip rects in @vclips.
852 *
853 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
854 * interrupted.
855 */
856int vmw_kms_readback(struct vmw_private *dev_priv,
857 struct drm_file *file_priv,
858 struct vmw_framebuffer *vfb,
859 struct drm_vmw_fence_rep __user *user_fence_rep,
860 struct drm_vmw_rect *vclips,
861 uint32_t num_clips)
862{
863 switch (dev_priv->active_display_unit) {
864 case vmw_du_screen_object:
865 return vmw_kms_sou_readback(dev_priv, file_priv, vfb,
Deepak Rawat91e9f352018-01-16 08:24:17 +0100866 user_fence_rep, vclips, num_clips,
867 NULL);
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700868 case vmw_du_screen_target:
869 return vmw_kms_stdu_dma(dev_priv, file_priv, vfb,
870 user_fence_rep, NULL, vclips, num_clips,
Deepak Rawat91e9f352018-01-16 08:24:17 +0100871 1, false, true, NULL);
Thomas Hellstrom10b1e0c2015-06-26 02:14:27 -0700872 default:
873 WARN_ONCE(true,
874 "Readback called with invalid display system.\n");
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700875}
Thomas Hellstrom10b1e0c2015-06-26 02:14:27 -0700876
877 return -ENOSYS;
878}
879
880
Ville Syrjäläd7955fc2015-12-15 12:21:15 +0100881static const struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs = {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000882 .destroy = vmw_framebuffer_surface_destroy,
Deepak Rawat2f5544f2018-09-21 14:34:02 -0700883 .dirty = drm_atomic_helper_dirtyfb,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000884};
885
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200886static int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv,
887 struct vmw_surface *surface,
888 struct vmw_framebuffer **out,
Daniel Vetterdabdcdc2016-12-02 08:07:40 +0100889 const struct drm_mode_fb_cmd2
Sinclair Yehf89c6c32015-06-26 01:54:28 -0700890 *mode_cmd,
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +0200891 bool is_bo_proxy)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000892
893{
Zack Rusin9703bb32020-11-03 22:21:34 -0500894 struct drm_device *dev = &dev_priv->drm;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000895 struct vmw_framebuffer_surface *vfbs;
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200896 enum SVGA3dSurfaceFormat format;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000897 int ret;
898
Sinclair Yehc8261a92015-06-26 01:23:42 -0700899 /* 3D is only supported on HWv8 and newer hosts */
900 if (dev_priv->active_display_unit == vmw_du_legacy)
Jakob Bornecrantz01e81412011-10-04 20:13:24 +0200901 return -ENOSYS;
902
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200903 /*
904 * Sanity checks.
905 */
906
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100907 /* Surface must be marked as a scanout. */
Deepak Rawat26b82872019-04-29 11:42:45 -0700908 if (unlikely(!surface->metadata.scanout))
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +0100909 return -EINVAL;
910
Deepak Rawat26b82872019-04-29 11:42:45 -0700911 if (unlikely(surface->metadata.mip_levels[0] != 1 ||
912 surface->metadata.num_sizes != 1 ||
913 surface->metadata.base_size.width < mode_cmd->width ||
914 surface->metadata.base_size.height < mode_cmd->height ||
915 surface->metadata.base_size.depth != 1)) {
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200916 DRM_ERROR("Incompatible surface dimensions "
917 "for requested mode.\n");
918 return -EINVAL;
919 }
920
Daniel Vetterdabdcdc2016-12-02 08:07:40 +0100921 switch (mode_cmd->pixel_format) {
922 case DRM_FORMAT_ARGB8888:
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200923 format = SVGA3D_A8R8G8B8;
924 break;
Daniel Vetterdabdcdc2016-12-02 08:07:40 +0100925 case DRM_FORMAT_XRGB8888:
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200926 format = SVGA3D_X8R8G8B8;
927 break;
Daniel Vetterdabdcdc2016-12-02 08:07:40 +0100928 case DRM_FORMAT_RGB565:
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200929 format = SVGA3D_R5G6B5;
930 break;
Daniel Vetterdabdcdc2016-12-02 08:07:40 +0100931 case DRM_FORMAT_XRGB1555:
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200932 format = SVGA3D_A1R5G5B5;
933 break;
934 default:
Sakari Ailus92f1d092021-02-16 17:57:22 +0200935 DRM_ERROR("Invalid pixel format: %p4cc\n",
936 &mode_cmd->pixel_format);
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200937 return -EINVAL;
938 }
939
Thomas Hellstromd80efd52015-08-10 10:39:35 -0700940 /*
941 * For DX, surface format validation is done when surface->scanout
942 * is set.
943 */
Deepak Rawat26b82872019-04-29 11:42:45 -0700944 if (!has_sm4_context(dev_priv) && format != surface->metadata.format) {
Thomas Hellstromd3216a02010-10-05 12:42:59 +0200945 DRM_ERROR("Invalid surface format for requested mode.\n");
946 return -EINVAL;
947 }
948
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000949 vfbs = kzalloc(sizeof(*vfbs), GFP_KERNEL);
950 if (!vfbs) {
951 ret = -ENOMEM;
952 goto out_err1;
953 }
954
Ville Syrjäläa3f913c2016-12-14 22:48:59 +0200955 drm_helper_mode_fill_fb_struct(dev, &vfbs->base.base, mode_cmd);
Sinclair Yeh05c95012015-08-11 22:53:39 -0700956 vfbs->surface = vmw_surface_reference(surface);
Daniel Vetterdabdcdc2016-12-02 08:07:40 +0100957 vfbs->base.user_handle = mode_cmd->handles[0];
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +0200958 vfbs->is_bo_proxy = is_bo_proxy;
Thomas Hellstrom3a939a52010-10-05 12:43:03 +0200959
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000960 *out = &vfbs->base;
961
Daniel Vetter80f0b5a2012-12-13 23:39:01 +0100962 ret = drm_framebuffer_init(dev, &vfbs->base.base,
963 &vmw_framebuffer_surface_funcs);
964 if (ret)
Sinclair Yeh05c95012015-08-11 22:53:39 -0700965 goto out_err2;
Daniel Vetter80f0b5a2012-12-13 23:39:01 +0100966
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000967 return 0;
968
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000969out_err2:
Sinclair Yeh05c95012015-08-11 22:53:39 -0700970 vmw_surface_unreference(&surface);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000971 kfree(vfbs);
972out_err1:
973 return ret;
974}
975
976/*
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +0200977 * Buffer-object framebuffer code
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000978 */
979
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +0200980static void vmw_framebuffer_bo_destroy(struct drm_framebuffer *framebuffer)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000981{
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +0200982 struct vmw_framebuffer_bo *vfbd =
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000983 vmw_framebuffer_to_vfbd(framebuffer);
984
985 drm_framebuffer_cleanup(framebuffer);
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +0200986 vmw_bo_unreference(&vfbd->buffer);
Thomas Hellstroma2787242015-06-29 12:55:07 -0700987 if (vfbd->base.user_obj)
988 ttm_base_object_unref(&vfbd->base.user_obj);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000989
990 kfree(vfbd);
991}
992
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +0200993static int vmw_framebuffer_bo_dirty(struct drm_framebuffer *framebuffer,
994 struct drm_file *file_priv,
995 unsigned int flags, unsigned int color,
996 struct drm_clip_rect *clips,
997 unsigned int num_clips)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +0000998{
999 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001000 struct vmw_framebuffer_bo *vfbd =
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +02001001 vmw_framebuffer_to_vfbd(framebuffer);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001002 struct drm_clip_rect norect;
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +02001003 int ret, increment = 1;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001004
Zack Rusin9703bb32020-11-03 22:21:34 -05001005 drm_modeset_lock_all(&dev_priv->drm);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +02001006
Thomas Hellstrom294adf72014-02-27 12:34:51 +01001007 ret = ttm_read_lock(&dev_priv->reservation_sem, true);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +02001008 if (unlikely(ret != 0)) {
Zack Rusin9703bb32020-11-03 22:21:34 -05001009 drm_modeset_unlock_all(&dev_priv->drm);
Thomas Hellstrom3a939a52010-10-05 12:43:03 +02001010 return ret;
Ville Syrjälä73e9efd2013-12-04 14:13:58 +02001011 }
Thomas Hellstrom3a939a52010-10-05 12:43:03 +02001012
Thomas Hellstromdf1c93b2010-01-13 22:28:36 +01001013 if (!num_clips) {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001014 num_clips = 1;
1015 clips = &norect;
1016 norect.x1 = norect.y1 = 0;
1017 norect.x2 = framebuffer->width;
1018 norect.y2 = framebuffer->height;
1019 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
1020 num_clips /= 2;
1021 increment = 2;
1022 }
1023
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001024 switch (dev_priv->active_display_unit) {
Thomas Hellstrom352b20dc2015-06-29 12:57:37 -07001025 case vmw_du_legacy:
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001026 ret = vmw_kms_ldu_do_bo_dirty(dev_priv, &vfbd->base, 0, 0,
1027 clips, num_clips, increment);
Thomas Hellstrom352b20dc2015-06-29 12:57:37 -07001028 break;
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001029 default:
Thomas Hellstrom352b20dc2015-06-29 12:57:37 -07001030 ret = -EINVAL;
1031 WARN_ONCE(true, "Dirty called with invalid display system.\n");
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001032 break;
Jakob Bornecrantz56d1c782011-10-04 20:13:22 +02001033 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001034
Zack Rusin8426ed92020-11-18 12:54:19 -05001035 vmw_cmd_flush(dev_priv, false);
Thomas Hellstrom294adf72014-02-27 12:34:51 +01001036 ttm_read_unlock(&dev_priv->reservation_sem);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +02001037
Zack Rusin9703bb32020-11-03 22:21:34 -05001038 drm_modeset_unlock_all(&dev_priv->drm);
Ville Syrjälä73e9efd2013-12-04 14:13:58 +02001039
Jakob Bornecrantz5deb65c2011-10-04 20:13:18 +02001040 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001041}
1042
Deepak Rawat2f5544f2018-09-21 14:34:02 -07001043static int vmw_framebuffer_bo_dirty_ext(struct drm_framebuffer *framebuffer,
1044 struct drm_file *file_priv,
1045 unsigned int flags, unsigned int color,
1046 struct drm_clip_rect *clips,
1047 unsigned int num_clips)
1048{
1049 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
1050
1051 if (dev_priv->active_display_unit == vmw_du_legacy)
1052 return vmw_framebuffer_bo_dirty(framebuffer, file_priv, flags,
1053 color, clips, num_clips);
1054
1055 return drm_atomic_helper_dirtyfb(framebuffer, file_priv, flags, color,
1056 clips, num_clips);
1057}
1058
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001059static const struct drm_framebuffer_funcs vmw_framebuffer_bo_funcs = {
1060 .destroy = vmw_framebuffer_bo_destroy,
Deepak Rawat2f5544f2018-09-21 14:34:02 -07001061 .dirty = vmw_framebuffer_bo_dirty_ext,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001062};
1063
Lee Jonesc88c25a2021-01-15 18:12:53 +00001064/*
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001065 * Pin the bofer in a location suitable for access by the
Thomas Hellstromef86cfe2018-01-16 11:07:30 +01001066 * display system.
Jakob Bornecrantz497a3ff2011-10-04 20:13:14 +02001067 */
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001068static int vmw_framebuffer_pin(struct vmw_framebuffer *vfb)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001069{
1070 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001071 struct vmw_buffer_object *buf;
Thomas Hellstromef86cfe2018-01-16 11:07:30 +01001072 struct ttm_placement *placement;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001073 int ret;
1074
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001075 buf = vfb->bo ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001076 vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001077
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001078 if (!buf)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001079 return 0;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001080
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001081 switch (dev_priv->active_display_unit) {
1082 case vmw_du_legacy:
1083 vmw_overlay_pause_all(dev_priv);
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001084 ret = vmw_bo_pin_in_start_of_vram(dev_priv, buf, false);
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001085 vmw_overlay_resume_all(dev_priv);
1086 break;
1087 case vmw_du_screen_object:
1088 case vmw_du_screen_target:
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001089 if (vfb->bo) {
Thomas Hellstromef86cfe2018-01-16 11:07:30 +01001090 if (dev_priv->capabilities & SVGA_CAP_3D) {
1091 /*
1092 * Use surface DMA to get content to
1093 * sreen target surface.
1094 */
1095 placement = &vmw_vram_gmr_placement;
1096 } else {
1097 /* Use CPU blit. */
1098 placement = &vmw_sys_placement;
1099 }
1100 } else {
1101 /* Use surface / image update */
1102 placement = &vmw_mob_placement;
1103 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001104
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001105 return vmw_bo_pin_in_placement(dev_priv, buf, placement, false);
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001106 default:
1107 return -EINVAL;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001108 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001109
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001110 return ret;
1111}
1112
1113static int vmw_framebuffer_unpin(struct vmw_framebuffer *vfb)
1114{
1115 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001116 struct vmw_buffer_object *buf;
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001117
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001118 buf = vfb->bo ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001119 vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
1120
1121 if (WARN_ON(!buf))
1122 return 0;
1123
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001124 return vmw_bo_unpin(dev_priv, buf, false);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001125}
1126
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001127/**
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001128 * vmw_create_bo_proxy - create a proxy surface for the buffer object
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001129 *
1130 * @dev: DRM device
1131 * @mode_cmd: parameters for the new surface
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001132 * @bo_mob: MOB backing the buffer object
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001133 * @srf_out: newly created surface
1134 *
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001135 * When the content FB is a buffer object, we create a surface as a proxy to the
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001136 * same buffer. This way we can do a surface copy rather than a surface DMA.
1137 * This is a more efficient approach
1138 *
1139 * RETURNS:
1140 * 0 on success, error code otherwise
1141 */
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001142static int vmw_create_bo_proxy(struct drm_device *dev,
1143 const struct drm_mode_fb_cmd2 *mode_cmd,
1144 struct vmw_buffer_object *bo_mob,
1145 struct vmw_surface **srf_out)
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001146{
Deepak Rawat504901d2019-04-29 14:55:44 -07001147 struct vmw_surface_metadata metadata = {0};
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001148 uint32_t format;
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001149 struct vmw_resource *res;
Thomas Hellstroma50e2bf2016-01-08 20:29:40 +01001150 unsigned int bytes_pp;
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001151 int ret;
1152
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001153 switch (mode_cmd->pixel_format) {
1154 case DRM_FORMAT_ARGB8888:
1155 case DRM_FORMAT_XRGB8888:
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001156 format = SVGA3D_X8R8G8B8;
Thomas Hellstroma50e2bf2016-01-08 20:29:40 +01001157 bytes_pp = 4;
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001158 break;
1159
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001160 case DRM_FORMAT_RGB565:
1161 case DRM_FORMAT_XRGB1555:
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001162 format = SVGA3D_R5G6B5;
Thomas Hellstroma50e2bf2016-01-08 20:29:40 +01001163 bytes_pp = 2;
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001164 break;
1165
1166 case 8:
1167 format = SVGA3D_P8;
Thomas Hellstroma50e2bf2016-01-08 20:29:40 +01001168 bytes_pp = 1;
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001169 break;
1170
1171 default:
Sakari Ailus92f1d092021-02-16 17:57:22 +02001172 DRM_ERROR("Invalid framebuffer format %p4cc\n",
1173 &mode_cmd->pixel_format);
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001174 return -EINVAL;
1175 }
1176
Deepak Rawat504901d2019-04-29 14:55:44 -07001177 metadata.format = format;
1178 metadata.mip_levels[0] = 1;
1179 metadata.num_sizes = 1;
1180 metadata.base_size.width = mode_cmd->pitches[0] / bytes_pp;
1181 metadata.base_size.height = mode_cmd->height;
1182 metadata.base_size.depth = 1;
1183 metadata.scanout = true;
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001184
Deepak Rawat504901d2019-04-29 14:55:44 -07001185 ret = vmw_gb_surface_define(vmw_priv(dev), 0, &metadata, srf_out);
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001186 if (ret) {
1187 DRM_ERROR("Failed to allocate proxy content buffer\n");
1188 return ret;
1189 }
1190
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001191 res = &(*srf_out)->res;
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001192
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001193 /* Reserve and switch the backing mob. */
1194 mutex_lock(&res->dev_priv->cmdbuf_mutex);
1195 (void) vmw_resource_reserve(res, false, true);
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001196 vmw_bo_unreference(&res->backup);
1197 res->backup = vmw_bo_reference(bo_mob);
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001198 res->backup_offset = 0;
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01001199 vmw_resource_unreserve(res, false, false, false, NULL, 0);
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001200 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001201
1202 return 0;
1203}
1204
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001205
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001206
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001207static int vmw_kms_new_framebuffer_bo(struct vmw_private *dev_priv,
1208 struct vmw_buffer_object *bo,
1209 struct vmw_framebuffer **out,
1210 const struct drm_mode_fb_cmd2
1211 *mode_cmd)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001212
1213{
Zack Rusin9703bb32020-11-03 22:21:34 -05001214 struct drm_device *dev = &dev_priv->drm;
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001215 struct vmw_framebuffer_bo *vfbd;
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001216 unsigned int requested_size;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001217 int ret;
1218
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001219 requested_size = mode_cmd->height * mode_cmd->pitches[0];
Christian Könige11bfb92020-12-09 15:07:50 +01001220 if (unlikely(requested_size > bo->base.base.size)) {
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001221 DRM_ERROR("Screen buffer object size is too small "
1222 "for requested mode.\n");
1223 return -EINVAL;
1224 }
1225
Jakob Bornecrantzc337ada2011-10-04 20:13:34 +02001226 /* Limited framebuffer color depth support for screen objects */
Sinclair Yehc8261a92015-06-26 01:23:42 -07001227 if (dev_priv->active_display_unit == vmw_du_screen_object) {
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001228 switch (mode_cmd->pixel_format) {
1229 case DRM_FORMAT_XRGB8888:
1230 case DRM_FORMAT_ARGB8888:
1231 break;
1232 case DRM_FORMAT_XRGB1555:
1233 case DRM_FORMAT_RGB565:
1234 break;
Jakob Bornecrantzc337ada2011-10-04 20:13:34 +02001235 default:
Sakari Ailus92f1d092021-02-16 17:57:22 +02001236 DRM_ERROR("Invalid pixel format: %p4cc\n",
1237 &mode_cmd->pixel_format);
Jakob Bornecrantzc337ada2011-10-04 20:13:34 +02001238 return -EINVAL;
1239 }
1240 }
1241
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001242 vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL);
1243 if (!vfbd) {
1244 ret = -ENOMEM;
1245 goto out_err1;
1246 }
1247
Ville Syrjäläa3f913c2016-12-14 22:48:59 +02001248 drm_helper_mode_fill_fb_struct(dev, &vfbd->base.base, mode_cmd);
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001249 vfbd->base.bo = true;
1250 vfbd->buffer = vmw_bo_reference(bo);
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001251 vfbd->base.user_handle = mode_cmd->handles[0];
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001252 *out = &vfbd->base;
1253
Daniel Vetter80f0b5a2012-12-13 23:39:01 +01001254 ret = drm_framebuffer_init(dev, &vfbd->base.base,
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001255 &vmw_framebuffer_bo_funcs);
Daniel Vetter80f0b5a2012-12-13 23:39:01 +01001256 if (ret)
Sinclair Yeh05c95012015-08-11 22:53:39 -07001257 goto out_err2;
Daniel Vetter80f0b5a2012-12-13 23:39:01 +01001258
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001259 return 0;
1260
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001261out_err2:
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001262 vmw_bo_unreference(&bo);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001263 kfree(vfbd);
1264out_err1:
1265 return ret;
1266}
1267
Sinclair Yeh810b3e162017-03-23 15:39:16 -07001268
1269/**
1270 * vmw_kms_srf_ok - check if a surface can be created
1271 *
Lee Jonesc88c25a2021-01-15 18:12:53 +00001272 * @dev_priv: Pointer to device private struct.
Sinclair Yeh810b3e162017-03-23 15:39:16 -07001273 * @width: requested width
1274 * @height: requested height
1275 *
1276 * Surfaces need to be less than texture size
1277 */
1278static bool
1279vmw_kms_srf_ok(struct vmw_private *dev_priv, uint32_t width, uint32_t height)
1280{
1281 if (width > dev_priv->texture_max_width ||
1282 height > dev_priv->texture_max_height)
1283 return false;
1284
1285 return true;
1286}
1287
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001288/**
1289 * vmw_kms_new_framebuffer - Create a new framebuffer.
1290 *
1291 * @dev_priv: Pointer to device private struct.
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001292 * @bo: Pointer to buffer object to wrap the kms framebuffer around.
1293 * Either @bo or @surface must be NULL.
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001294 * @surface: Pointer to a surface to wrap the kms framebuffer around.
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001295 * Either @bo or @surface must be NULL.
1296 * @only_2d: No presents will occur to this buffer object based framebuffer.
1297 * This helps the code to do some important optimizations.
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001298 * @mode_cmd: Frame-buffer metadata.
1299 */
1300struct vmw_framebuffer *
1301vmw_kms_new_framebuffer(struct vmw_private *dev_priv,
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001302 struct vmw_buffer_object *bo,
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001303 struct vmw_surface *surface,
1304 bool only_2d,
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001305 const struct drm_mode_fb_cmd2 *mode_cmd)
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001306{
Sinclair Yeh05c95012015-08-11 22:53:39 -07001307 struct vmw_framebuffer *vfb = NULL;
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001308 bool is_bo_proxy = false;
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001309 int ret;
1310
1311 /*
1312 * We cannot use the SurfaceDMA command in an non-accelerated VM,
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001313 * therefore, wrap the buffer object in a surface so we can use the
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001314 * SurfaceCopy command.
1315 */
Sinclair Yeh810b3e162017-03-23 15:39:16 -07001316 if (vmw_kms_srf_ok(dev_priv, mode_cmd->width, mode_cmd->height) &&
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001317 bo && only_2d &&
Sinclair Yehbbd5fef2017-06-02 07:44:53 +02001318 mode_cmd->width > 64 && /* Don't create a proxy for cursor */
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001319 dev_priv->active_display_unit == vmw_du_screen_target) {
Zack Rusin9703bb32020-11-03 22:21:34 -05001320 ret = vmw_create_bo_proxy(&dev_priv->drm, mode_cmd,
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001321 bo, &surface);
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001322 if (ret)
1323 return ERR_PTR(ret);
1324
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001325 is_bo_proxy = true;
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001326 }
1327
1328 /* Create the new framebuffer depending one what we have */
Sinclair Yeh05c95012015-08-11 22:53:39 -07001329 if (surface) {
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001330 ret = vmw_kms_new_framebuffer_surface(dev_priv, surface, &vfb,
1331 mode_cmd,
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001332 is_bo_proxy);
Sinclair Yeh05c95012015-08-11 22:53:39 -07001333
1334 /*
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001335 * vmw_create_bo_proxy() adds a reference that is no longer
Sinclair Yeh05c95012015-08-11 22:53:39 -07001336 * needed
1337 */
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001338 if (is_bo_proxy)
Sinclair Yeh05c95012015-08-11 22:53:39 -07001339 vmw_surface_unreference(&surface);
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001340 } else if (bo) {
1341 ret = vmw_kms_new_framebuffer_bo(dev_priv, bo, &vfb,
1342 mode_cmd);
Sinclair Yeh05c95012015-08-11 22:53:39 -07001343 } else {
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001344 BUG();
Sinclair Yeh05c95012015-08-11 22:53:39 -07001345 }
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001346
1347 if (ret)
1348 return ERR_PTR(ret);
1349
1350 vfb->pin = vmw_framebuffer_pin;
1351 vfb->unpin = vmw_framebuffer_unpin;
1352
1353 return vfb;
1354}
1355
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001356/*
1357 * Generic Kernel modesetting functions
1358 */
1359
1360static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev,
1361 struct drm_file *file_priv,
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001362 const struct drm_mode_fb_cmd2 *mode_cmd)
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001363{
1364 struct vmw_private *dev_priv = vmw_priv(dev);
1365 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
1366 struct vmw_framebuffer *vfb = NULL;
1367 struct vmw_surface *surface = NULL;
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001368 struct vmw_buffer_object *bo = NULL;
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001369 struct ttm_base_object *user_obj;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001370 int ret;
1371
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001372 /*
1373 * Take a reference on the user object of the resource
1374 * backing the kms fb. This ensures that user-space handle
1375 * lookups on that resource will always work as long as
1376 * it's registered with a kms framebuffer. This is important,
1377 * since vmw_execbuf_process identifies resources in the
1378 * command stream using user-space handles.
1379 */
1380
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001381 user_obj = ttm_base_object_lookup(tfile, mode_cmd->handles[0]);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001382 if (unlikely(user_obj == NULL)) {
1383 DRM_ERROR("Could not locate requested kms frame buffer.\n");
1384 return ERR_PTR(-ENOENT);
1385 }
1386
Thomas Hellstromd3216a02010-10-05 12:42:59 +02001387 /**
1388 * End conditioned code.
1389 */
1390
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001391 /* returns either a bo or surface */
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +01001392 ret = vmw_user_lookup_handle(dev_priv, tfile,
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001393 mode_cmd->handles[0],
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +01001394 &surface, &bo);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001395 if (ret)
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +01001396 goto err_out;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001397
Sinclair Yeh810b3e162017-03-23 15:39:16 -07001398
1399 if (!bo &&
1400 !vmw_kms_srf_ok(dev_priv, mode_cmd->width, mode_cmd->height)) {
1401 DRM_ERROR("Surface size cannot exceed %dx%d",
1402 dev_priv->texture_max_width,
1403 dev_priv->texture_max_height);
1404 goto err_out;
1405 }
1406
1407
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001408 vfb = vmw_kms_new_framebuffer(dev_priv, bo, surface,
1409 !(dev_priv->capabilities & SVGA_CAP_3D),
Daniel Vetterdabdcdc2016-12-02 08:07:40 +01001410 mode_cmd);
Thomas Hellstromfd006a42015-06-28 02:50:56 -07001411 if (IS_ERR(vfb)) {
1412 ret = PTR_ERR(vfb);
1413 goto err_out;
1414 }
Jakob Bornecrantz5ffdb652010-01-30 03:38:08 +00001415
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +01001416err_out:
1417 /* vmw_user_lookup_handle takes one ref so does new_fb */
1418 if (bo)
Thomas Hellstromf1d34bf2018-06-19 15:02:16 +02001419 vmw_bo_unreference(&bo);
Jakob Bornecrantze7ac9212011-11-28 13:19:12 +01001420 if (surface)
1421 vmw_surface_unreference(&surface);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001422
1423 if (ret) {
1424 DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001425 ttm_base_object_unref(&user_obj);
Chris Wilsoncce13ff2010-08-08 13:36:38 +01001426 return ERR_PTR(ret);
Thomas Hellstrom90ff18b2011-10-04 20:13:32 +02001427 } else
1428 vfb->user_obj = user_obj;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001429
1430 return &vfb->base;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001431}
1432
Deepak Rawat0a80eb42018-06-19 19:51:15 +02001433/**
1434 * vmw_kms_check_display_memory - Validates display memory required for a
1435 * topology
1436 * @dev: DRM device
1437 * @num_rects: number of drm_rect in rects
1438 * @rects: array of drm_rect representing the topology to validate indexed by
1439 * crtc index.
1440 *
1441 * Returns:
1442 * 0 on success otherwise negative error code
1443 */
1444static int vmw_kms_check_display_memory(struct drm_device *dev,
1445 uint32_t num_rects,
1446 struct drm_rect *rects)
1447{
1448 struct vmw_private *dev_priv = vmw_priv(dev);
Deepak Rawat0a80eb42018-06-19 19:51:15 +02001449 struct drm_rect bounding_box = {0};
1450 u64 total_pixels = 0, pixel_mem, bb_mem;
1451 int i;
Sinclair Yehc46a3062017-03-23 14:24:53 -07001452
Deepak Rawat0a80eb42018-06-19 19:51:15 +02001453 for (i = 0; i < num_rects; i++) {
1454 /*
Deepak Rawat0c1b1742018-09-13 12:34:37 +02001455 * For STDU only individual screen (screen target) is limited by
1456 * SCREENTARGET_MAX_WIDTH/HEIGHT registers.
Deepak Rawat0a80eb42018-06-19 19:51:15 +02001457 */
Deepak Rawat0c1b1742018-09-13 12:34:37 +02001458 if (dev_priv->active_display_unit == vmw_du_screen_target &&
1459 (drm_rect_width(&rects[i]) > dev_priv->stdu_max_width ||
1460 drm_rect_height(&rects[i]) > dev_priv->stdu_max_height)) {
Deepak Rawat9bb34e92019-05-09 12:42:58 -07001461 VMW_DEBUG_KMS("Screen size not supported.\n");
Deepak Rawat0a80eb42018-06-19 19:51:15 +02001462 return -EINVAL;
1463 }
1464
1465 /* Bounding box upper left is at (0,0). */
1466 if (rects[i].x2 > bounding_box.x2)
1467 bounding_box.x2 = rects[i].x2;
1468
1469 if (rects[i].y2 > bounding_box.y2)
1470 bounding_box.y2 = rects[i].y2;
1471
1472 total_pixels += (u64) drm_rect_width(&rects[i]) *
1473 (u64) drm_rect_height(&rects[i]);
1474 }
1475
1476 /* Virtual svga device primary limits are always in 32-bpp. */
1477 pixel_mem = total_pixels * 4;
1478
1479 /*
1480 * For HV10 and below prim_bb_mem is vram size. When
1481 * SVGA_REG_MAX_PRIMARY_BOUNDING_BOX_MEM is not present vram size is
1482 * limit on primary bounding box
1483 */
1484 if (pixel_mem > dev_priv->prim_bb_mem) {
Deepak Rawat9bb34e92019-05-09 12:42:58 -07001485 VMW_DEBUG_KMS("Combined output size too large.\n");
Deepak Rawat0a80eb42018-06-19 19:51:15 +02001486 return -EINVAL;
1487 }
1488
1489 /* SVGA_CAP_NO_BB_RESTRICTION is available for STDU only. */
1490 if (dev_priv->active_display_unit != vmw_du_screen_target ||
1491 !(dev_priv->capabilities & SVGA_CAP_NO_BB_RESTRICTION)) {
1492 bb_mem = (u64) bounding_box.x2 * bounding_box.y2 * 4;
1493
1494 if (bb_mem > dev_priv->prim_bb_mem) {
Deepak Rawat9bb34e92019-05-09 12:42:58 -07001495 VMW_DEBUG_KMS("Topology is beyond supported limits.\n");
Deepak Rawat0a80eb42018-06-19 19:51:15 +02001496 return -EINVAL;
1497 }
1498 }
1499
1500 return 0;
1501}
1502
1503/**
Thomas Hellstrom9d9486e2018-10-04 22:38:17 +00001504 * vmw_crtc_state_and_lock - Return new or current crtc state with locked
1505 * crtc mutex
1506 * @state: The atomic state pointer containing the new atomic state
1507 * @crtc: The crtc
1508 *
1509 * This function returns the new crtc state if it's part of the state update.
1510 * Otherwise returns the current crtc state. It also makes sure that the
1511 * crtc mutex is locked.
1512 *
1513 * Returns: A valid crtc state pointer or NULL. It may also return a
1514 * pointer error, in particular -EDEADLK if locking needs to be rerun.
1515 */
1516static struct drm_crtc_state *
1517vmw_crtc_state_and_lock(struct drm_atomic_state *state, struct drm_crtc *crtc)
1518{
1519 struct drm_crtc_state *crtc_state;
1520
1521 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
1522 if (crtc_state) {
1523 lockdep_assert_held(&crtc->mutex.mutex.base);
1524 } else {
1525 int ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
1526
1527 if (ret != 0 && ret != -EALREADY)
1528 return ERR_PTR(ret);
1529
1530 crtc_state = crtc->state;
1531 }
1532
1533 return crtc_state;
1534}
1535
1536/**
1537 * vmw_kms_check_implicit - Verify that all implicit display units scan out
1538 * from the same fb after the new state is committed.
1539 * @dev: The drm_device.
1540 * @state: The new state to be checked.
1541 *
1542 * Returns:
1543 * Zero on success,
1544 * -EINVAL on invalid state,
1545 * -EDEADLK if modeset locking needs to be rerun.
1546 */
1547static int vmw_kms_check_implicit(struct drm_device *dev,
1548 struct drm_atomic_state *state)
1549{
1550 struct drm_framebuffer *implicit_fb = NULL;
1551 struct drm_crtc *crtc;
1552 struct drm_crtc_state *crtc_state;
1553 struct drm_plane_state *plane_state;
1554
1555 drm_for_each_crtc(crtc, dev) {
1556 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
1557
1558 if (!du->is_implicit)
1559 continue;
1560
1561 crtc_state = vmw_crtc_state_and_lock(state, crtc);
1562 if (IS_ERR(crtc_state))
1563 return PTR_ERR(crtc_state);
1564
1565 if (!crtc_state || !crtc_state->enable)
1566 continue;
1567
1568 /*
1569 * Can't move primary planes across crtcs, so this is OK.
1570 * It also means we don't need to take the plane mutex.
1571 */
1572 plane_state = du->primary.state;
1573 if (plane_state->crtc != crtc)
1574 continue;
1575
1576 if (!implicit_fb)
1577 implicit_fb = plane_state->fb;
1578 else if (implicit_fb != plane_state->fb)
1579 return -EINVAL;
1580 }
1581
1582 return 0;
1583}
1584
1585/**
Deepak Rawat0a80eb42018-06-19 19:51:15 +02001586 * vmw_kms_check_topology - Validates topology in drm_atomic_state
1587 * @dev: DRM device
1588 * @state: the driver state object
1589 *
1590 * Returns:
1591 * 0 on success otherwise negative error code
1592 */
1593static int vmw_kms_check_topology(struct drm_device *dev,
1594 struct drm_atomic_state *state)
1595{
1596 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
1597 struct drm_rect *rects;
1598 struct drm_crtc *crtc;
1599 uint32_t i;
1600 int ret = 0;
1601
1602 rects = kcalloc(dev->mode_config.num_crtc, sizeof(struct drm_rect),
1603 GFP_KERNEL);
1604 if (!rects)
1605 return -ENOMEM;
1606
1607 drm_for_each_crtc(crtc, dev) {
1608 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
Thomas Hellstrom9da6e262018-11-12 15:46:39 +01001609 struct drm_crtc_state *crtc_state;
Deepak Rawat0a80eb42018-06-19 19:51:15 +02001610
1611 i = drm_crtc_index(crtc);
1612
Thomas Hellstrom9da6e262018-11-12 15:46:39 +01001613 crtc_state = vmw_crtc_state_and_lock(state, crtc);
1614 if (IS_ERR(crtc_state)) {
1615 ret = PTR_ERR(crtc_state);
1616 goto clean;
1617 }
1618
1619 if (!crtc_state)
1620 continue;
1621
1622 if (crtc_state->enable) {
Deepak Rawat0a80eb42018-06-19 19:51:15 +02001623 rects[i].x1 = du->gui_x;
1624 rects[i].y1 = du->gui_y;
1625 rects[i].x2 = du->gui_x + crtc_state->mode.hdisplay;
1626 rects[i].y2 = du->gui_y + crtc_state->mode.vdisplay;
Thomas Hellstrom9da6e262018-11-12 15:46:39 +01001627 } else {
1628 rects[i].x1 = 0;
1629 rects[i].y1 = 0;
1630 rects[i].x2 = 0;
1631 rects[i].y2 = 0;
Deepak Rawat0a80eb42018-06-19 19:51:15 +02001632 }
1633 }
1634
1635 /* Determine change to topology due to new atomic state */
1636 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
1637 new_crtc_state, i) {
1638 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
Deepak Rawat3e79ecd2018-06-20 11:34:26 +02001639 struct drm_connector *connector;
1640 struct drm_connector_state *conn_state;
1641 struct vmw_connector_state *vmw_conn_state;
Deepak Rawat0a80eb42018-06-19 19:51:15 +02001642
Deepak Rawat479d5902018-12-21 14:38:35 -08001643 if (!du->pref_active && new_crtc_state->enable) {
Deepak Rawat9bb34e92019-05-09 12:42:58 -07001644 VMW_DEBUG_KMS("Enabling a disabled display unit\n");
Deepak Rawat3e79ecd2018-06-20 11:34:26 +02001645 ret = -EINVAL;
1646 goto clean;
Deepak Rawat0a80eb42018-06-19 19:51:15 +02001647 }
Deepak Rawat3e79ecd2018-06-20 11:34:26 +02001648
1649 /*
1650 * For vmwgfx each crtc has only one connector attached and it
1651 * is not changed so don't really need to check the
1652 * crtc->connector_mask and iterate over it.
1653 */
1654 connector = &du->connector;
1655 conn_state = drm_atomic_get_connector_state(state, connector);
1656 if (IS_ERR(conn_state)) {
1657 ret = PTR_ERR(conn_state);
1658 goto clean;
1659 }
1660
1661 vmw_conn_state = vmw_connector_state_to_vcs(conn_state);
1662 vmw_conn_state->gui_x = du->gui_x;
1663 vmw_conn_state->gui_y = du->gui_y;
Deepak Rawat0a80eb42018-06-19 19:51:15 +02001664 }
1665
1666 ret = vmw_kms_check_display_memory(dev, dev->mode_config.num_crtc,
1667 rects);
1668
1669clean:
1670 kfree(rects);
1671 return ret;
1672}
Sinclair Yehc46a3062017-03-23 14:24:53 -07001673
1674/**
1675 * vmw_kms_atomic_check_modeset- validate state object for modeset changes
1676 *
1677 * @dev: DRM device
1678 * @state: the driver state object
1679 *
1680 * This is a simple wrapper around drm_atomic_helper_check_modeset() for
1681 * us to assign a value to mode->crtc_clock so that
1682 * drm_calc_timestamping_constants() won't throw an error message
1683 *
Deepak Rawat0a80eb42018-06-19 19:51:15 +02001684 * Returns:
Sinclair Yehc46a3062017-03-23 14:24:53 -07001685 * Zero for success or -errno
1686 */
Maarten Lankhorstbdc362f2017-07-12 10:13:33 +02001687static int
Sinclair Yehc46a3062017-03-23 14:24:53 -07001688vmw_kms_atomic_check_modeset(struct drm_device *dev,
1689 struct drm_atomic_state *state)
1690{
Sinclair Yehc46a3062017-03-23 14:24:53 -07001691 struct drm_crtc *crtc;
Deepak Rawat7e14eab2018-06-20 11:23:55 +02001692 struct drm_crtc_state *crtc_state;
1693 bool need_modeset = false;
1694 int i, ret;
Sinclair Yehc46a3062017-03-23 14:24:53 -07001695
Sinclair Yehb249cb42018-06-19 19:46:58 +02001696 ret = drm_atomic_helper_check(dev, state);
Deepak Rawat0a80eb42018-06-19 19:51:15 +02001697 if (ret)
Sinclair Yehb249cb42018-06-19 19:46:58 +02001698 return ret;
1699
Thomas Hellstrom9d9486e2018-10-04 22:38:17 +00001700 ret = vmw_kms_check_implicit(dev, state);
Deepak Rawat9bb34e92019-05-09 12:42:58 -07001701 if (ret) {
1702 VMW_DEBUG_KMS("Invalid implicit state\n");
Thomas Hellstrom9d9486e2018-10-04 22:38:17 +00001703 return ret;
Deepak Rawat9bb34e92019-05-09 12:42:58 -07001704 }
Thomas Hellstrom9d9486e2018-10-04 22:38:17 +00001705
Maarten Lankhorstbdc362f2017-07-12 10:13:33 +02001706 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
Deepak Rawat7e14eab2018-06-20 11:23:55 +02001707 if (drm_atomic_crtc_needs_modeset(crtc_state))
1708 need_modeset = true;
Sinclair Yehc46a3062017-03-23 14:24:53 -07001709 }
1710
Deepak Rawat7e14eab2018-06-20 11:23:55 +02001711 if (need_modeset)
1712 return vmw_kms_check_topology(dev, state);
1713
1714 return ret;
Sinclair Yehc46a3062017-03-23 14:24:53 -07001715}
1716
Laurent Pincharte6ecefa2012-05-17 13:27:23 +02001717static const struct drm_mode_config_funcs vmw_kms_funcs = {
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001718 .fb_create = vmw_kms_fb_create,
Sinclair Yehc46a3062017-03-23 14:24:53 -07001719 .atomic_check = vmw_kms_atomic_check_modeset,
Deepak Rawat904efd92018-01-16 08:48:09 +01001720 .atomic_commit = drm_atomic_helper_commit,
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001721};
1722
Thomas Hellstromb9eb1a62015-04-02 02:39:45 -07001723static int vmw_kms_generic_present(struct vmw_private *dev_priv,
1724 struct drm_file *file_priv,
1725 struct vmw_framebuffer *vfb,
1726 struct vmw_surface *surface,
1727 uint32_t sid,
1728 int32_t destX, int32_t destY,
1729 struct drm_vmw_rect *clips,
1730 uint32_t num_clips)
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001731{
Thomas Hellstrom10b1e0c2015-06-26 02:14:27 -07001732 return vmw_kms_sou_do_surface_dirty(dev_priv, vfb, NULL, clips,
1733 &surface->res, destX, destY,
Deepak Rawat91e9f352018-01-16 08:24:17 +01001734 num_clips, 1, NULL, NULL);
Jakob Bornecrantz2fcd5a72011-10-04 20:13:26 +02001735}
1736
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001737
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001738int vmw_kms_present(struct vmw_private *dev_priv,
1739 struct drm_file *file_priv,
1740 struct vmw_framebuffer *vfb,
1741 struct vmw_surface *surface,
1742 uint32_t sid,
1743 int32_t destX, int32_t destY,
1744 struct drm_vmw_rect *clips,
1745 uint32_t num_clips)
1746{
Sinclair Yeh35c05122015-06-26 01:42:06 -07001747 int ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001748
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001749 switch (dev_priv->active_display_unit) {
1750 case vmw_du_screen_target:
1751 ret = vmw_kms_stdu_surface_dirty(dev_priv, vfb, NULL, clips,
1752 &surface->res, destX, destY,
Deepak Rawat91e9f352018-01-16 08:24:17 +01001753 num_clips, 1, NULL, NULL);
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001754 break;
1755 case vmw_du_screen_object:
1756 ret = vmw_kms_generic_present(dev_priv, file_priv, vfb, surface,
1757 sid, destX, destY, clips,
1758 num_clips);
1759 break;
1760 default:
1761 WARN_ONCE(true,
1762 "Present called with invalid display system.\n");
1763 ret = -ENOSYS;
1764 break;
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001765 }
Sinclair Yeh35c05122015-06-26 01:42:06 -07001766 if (ret)
1767 return ret;
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001768
Zack Rusin8426ed92020-11-18 12:54:19 -05001769 vmw_cmd_flush(dev_priv, false);
Michel Dänzer0bef23f2011-08-31 07:42:50 +00001770
Sinclair Yeh35c05122015-06-26 01:42:06 -07001771 return 0;
Thomas Hellstrom7c4f7782010-06-01 11:38:17 +02001772}
1773
Thomas Hellstrom578e6092016-02-12 09:45:42 +01001774static void
1775vmw_kms_create_hotplug_mode_update_property(struct vmw_private *dev_priv)
1776{
1777 if (dev_priv->hotplug_mode_update_property)
1778 return;
1779
1780 dev_priv->hotplug_mode_update_property =
Zack Rusin9703bb32020-11-03 22:21:34 -05001781 drm_property_create_range(&dev_priv->drm,
Thomas Hellstrom578e6092016-02-12 09:45:42 +01001782 DRM_MODE_PROP_IMMUTABLE,
1783 "hotplug_mode_update", 0, 1);
Thomas Hellstrom578e6092016-02-12 09:45:42 +01001784}
1785
Jakob Bornecrantzd7e19582010-05-28 11:21:59 +02001786int vmw_kms_init(struct vmw_private *dev_priv)
1787{
Zack Rusin9703bb32020-11-03 22:21:34 -05001788 struct drm_device *dev = &dev_priv->drm;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001789 int ret;
1790
1791 drm_mode_config_init(dev);
1792 dev->mode_config.funcs = &vmw_kms_funcs;
1793 dev->mode_config.min_width = 1;
1794 dev->mode_config.min_height = 1;
Sinclair Yeh65ade7d2015-07-16 10:49:13 -07001795 dev->mode_config.max_width = dev_priv->texture_max_width;
1796 dev->mode_config.max_height = dev_priv->texture_max_height;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001797
Thomas Hellstrom578e6092016-02-12 09:45:42 +01001798 drm_mode_create_suggested_offset_properties(dev);
1799 vmw_kms_create_hotplug_mode_update_property(dev_priv);
1800
Sinclair Yeh35c05122015-06-26 01:42:06 -07001801 ret = vmw_kms_stdu_init_display(dev_priv);
1802 if (ret) {
1803 ret = vmw_kms_sou_init_display(dev_priv);
1804 if (ret) /* Fallback */
1805 ret = vmw_kms_ldu_init_display(dev_priv);
1806 }
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001807
Sinclair Yehc8261a92015-06-26 01:23:42 -07001808 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001809}
1810
1811int vmw_kms_close(struct vmw_private *dev_priv)
1812{
Daniel Vetter5f58e972017-06-26 18:19:48 +02001813 int ret = 0;
Sinclair Yehc8261a92015-06-26 01:23:42 -07001814
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001815 /*
1816 * Docs says we should take the lock before calling this function
1817 * but since it destroys encoders and our destructor calls
1818 * drm_encoder_cleanup which takes the lock we deadlock.
1819 */
Zack Rusin9703bb32020-11-03 22:21:34 -05001820 drm_mode_config_cleanup(&dev_priv->drm);
Daniel Vetter5f58e972017-06-26 18:19:48 +02001821 if (dev_priv->active_display_unit == vmw_du_legacy)
Sinclair Yehc8261a92015-06-26 01:23:42 -07001822 ret = vmw_kms_ldu_close_display(dev_priv);
1823
1824 return ret;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001825}
1826
1827int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
1828 struct drm_file *file_priv)
1829{
1830 struct drm_vmw_cursor_bypass_arg *arg = data;
1831 struct vmw_display_unit *du;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001832 struct drm_crtc *crtc;
1833 int ret = 0;
1834
1835
1836 mutex_lock(&dev->mode_config.mutex);
1837 if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
1838
1839 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1840 du = vmw_crtc_to_du(crtc);
1841 du->hotspot_x = arg->xhot;
1842 du->hotspot_y = arg->yhot;
1843 }
1844
1845 mutex_unlock(&dev->mode_config.mutex);
1846 return 0;
1847 }
1848
Keith Packard418da172017-03-14 23:25:07 -07001849 crtc = drm_crtc_find(dev, file_priv, arg->crtc_id);
Rob Clarka4cd5d62014-07-17 23:30:02 -04001850 if (!crtc) {
Ville Syrjälä4ae87ff2013-10-17 13:35:05 +03001851 ret = -ENOENT;
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001852 goto out;
1853 }
1854
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001855 du = vmw_crtc_to_du(crtc);
1856
1857 du->hotspot_x = arg->xhot;
1858 du->hotspot_y = arg->yhot;
1859
1860out:
1861 mutex_unlock(&dev->mode_config.mutex);
1862
1863 return ret;
1864}
1865
1866int vmw_kms_write_svga(struct vmw_private *vmw_priv,
1867 unsigned width, unsigned height, unsigned pitch,
1868 unsigned bpp, unsigned depth)
1869{
1870 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1871 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch);
1872 else if (vmw_fifo_have_pitchlock(vmw_priv))
Zack Rusinbe4f77a2020-11-04 13:46:09 -05001873 vmw_fifo_mem_write(vmw_priv, SVGA_FIFO_PITCHLOCK, pitch);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001874 vmw_write(vmw_priv, SVGA_REG_WIDTH, width);
1875 vmw_write(vmw_priv, SVGA_REG_HEIGHT, height);
Zack Rusin625f8fb2020-11-19 19:37:07 -05001876 if ((vmw_priv->capabilities & SVGA_CAP_8BIT_EMULATION) != 0)
1877 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp);
Jakob Bornecrantzfb1d9732009-12-10 00:19:58 +00001878
1879 if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) {
1880 DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n",
1881 depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH));
1882 return -EINVAL;
1883 }
1884
1885 return 0;
1886}
1887
Thomas Hellstrome133e7372010-10-05 12:43:04 +02001888bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
1889 uint32_t pitch,
1890 uint32_t height)
1891{
Sinclair Yeh35c05122015-06-26 01:42:06 -07001892 return ((u64) pitch * (u64) height) < (u64)
1893 ((dev_priv->active_display_unit == vmw_du_screen_target) ?
1894 dev_priv->prim_bb_mem : dev_priv->vram_size);
Thomas Hellstrome133e7372010-10-05 12:43:04 +02001895}
1896
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001897
Lee Jonesc88c25a2021-01-15 18:12:53 +00001898/*
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001899 * Function called by DRM code called with vbl_lock held.
1900 */
Thomas Zimmermann4bebe912020-01-23 14:59:41 +01001901u32 vmw_get_vblank_counter(struct drm_crtc *crtc)
Thomas Hellstrom7a1c2f62010-10-01 10:21:49 +02001902{
1903 return 0;
1904}
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001905
Lee Jonesc88c25a2021-01-15 18:12:53 +00001906/*
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001907 * Function called by DRM code called with vbl_lock held.
1908 */
Thomas Zimmermann4bebe912020-01-23 14:59:41 +01001909int vmw_enable_vblank(struct drm_crtc *crtc)
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001910{
Woody Suwalski2b0bc682018-01-17 09:07:47 +01001911 return -EINVAL;
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001912}
1913
Lee Jonesc88c25a2021-01-15 18:12:53 +00001914/*
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001915 * Function called by DRM code called with vbl_lock held.
1916 */
Thomas Zimmermann4bebe912020-01-23 14:59:41 +01001917void vmw_disable_vblank(struct drm_crtc *crtc)
Jakob Bornecrantz1c482ab2011-10-17 11:59:45 +02001918{
1919}
1920
Deepak Rawat5e241332018-06-20 11:17:16 +02001921/**
1922 * vmw_du_update_layout - Update the display unit with topology from resolution
1923 * plugin and generate DRM uevent
1924 * @dev_priv: device private
1925 * @num_rects: number of drm_rect in rects
1926 * @rects: toplogy to update
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001927 */
Deepak Rawat5e241332018-06-20 11:17:16 +02001928static int vmw_du_update_layout(struct vmw_private *dev_priv,
1929 unsigned int num_rects, struct drm_rect *rects)
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001930{
Zack Rusin9703bb32020-11-03 22:21:34 -05001931 struct drm_device *dev = &dev_priv->drm;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001932 struct vmw_display_unit *du;
1933 struct drm_connector *con;
Deepak Rawatb89e5ff2018-06-20 11:32:29 +02001934 struct drm_connector_list_iter conn_iter;
Thomas Hellstrom9da6e262018-11-12 15:46:39 +01001935 struct drm_modeset_acquire_ctx ctx;
1936 struct drm_crtc *crtc;
1937 int ret;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001938
Thomas Hellstrom9da6e262018-11-12 15:46:39 +01001939 /* Currently gui_x/y is protected with the crtc mutex */
1940 mutex_lock(&dev->mode_config.mutex);
1941 drm_modeset_acquire_init(&ctx, 0);
1942retry:
1943 drm_for_each_crtc(crtc, dev) {
1944 ret = drm_modeset_lock(&crtc->mutex, &ctx);
1945 if (ret < 0) {
1946 if (ret == -EDEADLK) {
1947 drm_modeset_backoff(&ctx);
1948 goto retry;
1949 }
1950 goto out_fini;
1951 }
1952 }
1953
Deepak Rawatb89e5ff2018-06-20 11:32:29 +02001954 drm_connector_list_iter_begin(dev, &conn_iter);
1955 drm_for_each_connector_iter(con, &conn_iter) {
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001956 du = vmw_connector_to_du(con);
Deepak Rawat5e241332018-06-20 11:17:16 +02001957 if (num_rects > du->unit) {
1958 du->pref_width = drm_rect_width(&rects[du->unit]);
1959 du->pref_height = drm_rect_height(&rects[du->unit]);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001960 du->pref_active = true;
Deepak Rawat5e241332018-06-20 11:17:16 +02001961 du->gui_x = rects[du->unit].x1;
1962 du->gui_y = rects[du->unit].y1;
Deepak Rawatb89e5ff2018-06-20 11:32:29 +02001963 } else {
1964 du->pref_width = 800;
1965 du->pref_height = 600;
1966 du->pref_active = false;
1967 du->gui_x = 0;
1968 du->gui_y = 0;
1969 }
1970 }
1971 drm_connector_list_iter_end(&conn_iter);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001972
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001973 list_for_each_entry(con, &dev->mode_config.connector_list, head) {
1974 du = vmw_connector_to_du(con);
Deepak Rawatb89e5ff2018-06-20 11:32:29 +02001975 if (num_rects > du->unit) {
Thomas Hellstrom578e6092016-02-12 09:45:42 +01001976 drm_object_property_set_value
1977 (&con->base, dev->mode_config.suggested_x_property,
1978 du->gui_x);
1979 drm_object_property_set_value
1980 (&con->base, dev->mode_config.suggested_y_property,
1981 du->gui_y);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001982 } else {
Thomas Hellstrom578e6092016-02-12 09:45:42 +01001983 drm_object_property_set_value
1984 (&con->base, dev->mode_config.suggested_x_property,
1985 0);
1986 drm_object_property_set_value
1987 (&con->base, dev->mode_config.suggested_y_property,
1988 0);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001989 }
1990 con->status = vmw_du_connector_detect(con, true);
1991 }
Deepak Rawatb89e5ff2018-06-20 11:32:29 +02001992
Thomas Hellstrom578e6092016-02-12 09:45:42 +01001993 drm_sysfs_hotplug_event(dev);
Thomas Hellstrom9da6e262018-11-12 15:46:39 +01001994out_fini:
1995 drm_modeset_drop_locks(&ctx);
1996 drm_modeset_acquire_fini(&ctx);
1997 mutex_unlock(&dev->mode_config.mutex);
1998
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02001999 return 0;
2000}
2001
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02002002int vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
2003 u16 *r, u16 *g, u16 *b,
Daniel Vetter6d124ff2017-04-03 10:33:01 +02002004 uint32_t size,
2005 struct drm_modeset_acquire_ctx *ctx)
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002006{
2007 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
2008 int i;
2009
2010 for (i = 0; i < size; i++) {
2011 DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i,
2012 r[i], g[i], b[i]);
2013 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 0, r[i] >> 8);
2014 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 1, g[i] >> 8);
2015 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 2, b[i] >> 8);
2016 }
Maarten Lankhorst7ea77282016-06-07 12:49:30 +02002017
2018 return 0;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002019}
2020
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002021int vmw_du_connector_dpms(struct drm_connector *connector, int mode)
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002022{
Maarten Lankhorst9a69a9a2015-07-21 11:34:55 +02002023 return 0;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002024}
2025
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002026enum drm_connector_status
2027vmw_du_connector_detect(struct drm_connector *connector, bool force)
2028{
2029 uint32_t num_displays;
2030 struct drm_device *dev = connector->dev;
2031 struct vmw_private *dev_priv = vmw_priv(dev);
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002032 struct vmw_display_unit *du = vmw_connector_to_du(connector);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002033
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002034 num_displays = vmw_read(dev_priv, SVGA_REG_NUM_DISPLAYS);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002035
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002036 return ((vmw_connector_to_du(connector)->unit < num_displays &&
2037 du->pref_active) ?
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002038 connector_status_connected : connector_status_disconnected);
2039}
2040
2041static struct drm_display_mode vmw_kms_connector_builtin[] = {
2042 /* 640x480@60Hz */
2043 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
2044 752, 800, 0, 480, 489, 492, 525, 0,
2045 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
2046 /* 800x600@60Hz */
2047 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
2048 968, 1056, 0, 600, 601, 605, 628, 0,
2049 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
2050 /* 1024x768@60Hz */
2051 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
2052 1184, 1344, 0, 768, 771, 777, 806, 0,
2053 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
2054 /* 1152x864@75Hz */
2055 { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
2056 1344, 1600, 0, 864, 865, 868, 900, 0,
2057 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
Roland Scheideggercfc51a72021-02-05 02:04:46 +01002058 /* 1280x720@60Hz */
2059 { DRM_MODE("1280x720", DRM_MODE_TYPE_DRIVER, 74500, 1280, 1344,
2060 1472, 1664, 0, 720, 723, 728, 748, 0,
2061 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002062 /* 1280x768@60Hz */
2063 { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344,
2064 1472, 1664, 0, 768, 771, 778, 798, 0,
2065 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2066 /* 1280x800@60Hz */
2067 { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352,
2068 1480, 1680, 0, 800, 803, 809, 831, 0,
2069 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
2070 /* 1280x960@60Hz */
2071 { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376,
2072 1488, 1800, 0, 960, 961, 964, 1000, 0,
2073 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
2074 /* 1280x1024@60Hz */
2075 { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328,
2076 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
2077 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
2078 /* 1360x768@60Hz */
2079 { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424,
2080 1536, 1792, 0, 768, 771, 777, 795, 0,
2081 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
2082 /* 1440x1050@60Hz */
2083 { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488,
2084 1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
2085 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2086 /* 1440x900@60Hz */
2087 { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520,
2088 1672, 1904, 0, 900, 903, 909, 934, 0,
2089 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2090 /* 1600x1200@60Hz */
2091 { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664,
2092 1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
2093 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
2094 /* 1680x1050@60Hz */
2095 { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784,
2096 1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
2097 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2098 /* 1792x1344@60Hz */
2099 { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920,
2100 2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
2101 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2102 /* 1853x1392@60Hz */
2103 { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952,
2104 2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
2105 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
Roland Scheideggercfc51a72021-02-05 02:04:46 +01002106 /* 1920x1080@60Hz */
2107 { DRM_MODE("1920x1080", DRM_MODE_TYPE_DRIVER, 173000, 1920, 2048,
2108 2248, 2576, 0, 1080, 1083, 1088, 1120, 0,
2109 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002110 /* 1920x1200@60Hz */
2111 { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056,
2112 2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
2113 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2114 /* 1920x1440@60Hz */
2115 { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048,
2116 2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
2117 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
Roland Scheideggercfc51a72021-02-05 02:04:46 +01002118 /* 2560x1440@60Hz */
2119 { DRM_MODE("2560x1440", DRM_MODE_TYPE_DRIVER, 241500, 2560, 2608,
2120 2640, 2720, 0, 1440, 1443, 1448, 1481, 0,
2121 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002122 /* 2560x1600@60Hz */
2123 { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752,
2124 3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
2125 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
Roland Scheideggercfc51a72021-02-05 02:04:46 +01002126 /* 2880x1800@60Hz */
2127 { DRM_MODE("2880x1800", DRM_MODE_TYPE_DRIVER, 337500, 2880, 2928,
2128 2960, 3040, 0, 1800, 1803, 1809, 1852, 0,
2129 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
2130 /* 3840x2160@60Hz */
2131 { DRM_MODE("3840x2160", DRM_MODE_TYPE_DRIVER, 533000, 3840, 3888,
2132 3920, 4000, 0, 2160, 2163, 2168, 2222, 0,
2133 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
2134 /* 3840x2400@60Hz */
2135 { DRM_MODE("3840x2400", DRM_MODE_TYPE_DRIVER, 592250, 3840, 3888,
2136 3920, 4000, 0, 2400, 2403, 2409, 2469, 0,
2137 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002138 /* Terminate */
2139 { DRM_MODE("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) },
2140};
2141
Thomas Hellstrom1543b4dd2011-11-02 09:43:10 +01002142/**
2143 * vmw_guess_mode_timing - Provide fake timings for a
2144 * 60Hz vrefresh mode.
2145 *
Lee Jonesc88c25a2021-01-15 18:12:53 +00002146 * @mode: Pointer to a struct drm_display_mode with hdisplay and vdisplay
Thomas Hellstrom1543b4dd2011-11-02 09:43:10 +01002147 * members filled in.
2148 */
Thomas Hellstroma2787242015-06-29 12:55:07 -07002149void vmw_guess_mode_timing(struct drm_display_mode *mode)
Thomas Hellstrom1543b4dd2011-11-02 09:43:10 +01002150{
2151 mode->hsync_start = mode->hdisplay + 50;
2152 mode->hsync_end = mode->hsync_start + 50;
2153 mode->htotal = mode->hsync_end + 50;
2154
2155 mode->vsync_start = mode->vdisplay + 50;
2156 mode->vsync_end = mode->vsync_start + 50;
2157 mode->vtotal = mode->vsync_end + 50;
2158
2159 mode->clock = (u32)mode->htotal * (u32)mode->vtotal / 100 * 6;
Thomas Hellstrom1543b4dd2011-11-02 09:43:10 +01002160}
2161
2162
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002163int vmw_du_connector_fill_modes(struct drm_connector *connector,
2164 uint32_t max_width, uint32_t max_height)
2165{
2166 struct vmw_display_unit *du = vmw_connector_to_du(connector);
2167 struct drm_device *dev = connector->dev;
2168 struct vmw_private *dev_priv = vmw_priv(dev);
2169 struct drm_display_mode *mode = NULL;
2170 struct drm_display_mode *bmode;
2171 struct drm_display_mode prefmode = { DRM_MODE("preferred",
2172 DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
2173 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2174 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
2175 };
2176 int i;
Sinclair Yeh7c20d212016-06-29 11:29:47 -07002177 u32 assumed_bpp = 4;
Sinclair Yeh9a723842014-10-31 09:58:06 +01002178
Sinclair Yeh04319d82016-06-29 12:15:48 -07002179 if (dev_priv->assume_16bpp)
2180 assumed_bpp = 2;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002181
Deepak Rawat140b4e62018-09-13 12:44:13 +02002182 max_width = min(max_width, dev_priv->texture_max_width);
2183 max_height = min(max_height, dev_priv->texture_max_height);
2184
2185 /*
2186 * For STDU extra limit for a mode on SVGA_REG_SCREENTARGET_MAX_WIDTH/
2187 * HEIGHT registers.
2188 */
Sinclair Yeh35c05122015-06-26 01:42:06 -07002189 if (dev_priv->active_display_unit == vmw_du_screen_target) {
2190 max_width = min(max_width, dev_priv->stdu_max_width);
2191 max_height = min(max_height, dev_priv->stdu_max_height);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002192 }
2193
2194 /* Add preferred mode */
Sinclair Yehc8261a92015-06-26 01:23:42 -07002195 mode = drm_mode_duplicate(dev, &prefmode);
2196 if (!mode)
2197 return 0;
2198 mode->hdisplay = du->pref_width;
2199 mode->vdisplay = du->pref_height;
2200 vmw_guess_mode_timing(mode);
Zack Rusine3dc5522021-02-08 14:48:44 -05002201 drm_mode_set_name(mode);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002202
Sinclair Yehc8261a92015-06-26 01:23:42 -07002203 if (vmw_kms_validate_mode_vram(dev_priv,
2204 mode->hdisplay * assumed_bpp,
2205 mode->vdisplay)) {
2206 drm_mode_probed_add(connector, mode);
2207 } else {
2208 drm_mode_destroy(dev, mode);
2209 mode = NULL;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002210 }
2211
Sinclair Yehc8261a92015-06-26 01:23:42 -07002212 if (du->pref_mode) {
2213 list_del_init(&du->pref_mode->head);
2214 drm_mode_destroy(dev, du->pref_mode);
2215 }
2216
2217 /* mode might be null here, this is intended */
2218 du->pref_mode = mode;
2219
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002220 for (i = 0; vmw_kms_connector_builtin[i].type != 0; i++) {
2221 bmode = &vmw_kms_connector_builtin[i];
2222 if (bmode->hdisplay > max_width ||
2223 bmode->vdisplay > max_height)
2224 continue;
2225
Sinclair Yeh9a723842014-10-31 09:58:06 +01002226 if (!vmw_kms_validate_mode_vram(dev_priv,
2227 bmode->hdisplay * assumed_bpp,
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002228 bmode->vdisplay))
2229 continue;
2230
2231 mode = drm_mode_duplicate(dev, bmode);
2232 if (!mode)
2233 return 0;
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002234
2235 drm_mode_probed_add(connector, mode);
2236 }
2237
Daniel Vetter97e14fb2018-07-09 10:40:08 +02002238 drm_connector_list_update(connector);
Thomas Hellstromf6b05002015-06-29 12:59:58 -07002239 /* Move the prefered mode first, help apps pick the right mode. */
2240 drm_mode_sort(&connector->modes);
Jakob Bornecrantz626ab772011-10-04 20:13:20 +02002241
2242 return 1;
2243}
2244
Deepak Rawat5e241332018-06-20 11:17:16 +02002245/**
2246 * vmw_kms_update_layout_ioctl - Handler for DRM_VMW_UPDATE_LAYOUT ioctl
2247 * @dev: drm device for the ioctl
2248 * @data: data pointer for the ioctl
2249 * @file_priv: drm file for the ioctl call
2250 *
2251 * Update preferred topology of display unit as per ioctl request. The topology
2252 * is expressed as array of drm_vmw_rect.
2253 * e.g.
2254 * [0 0 640 480] [640 0 800 600] [0 480 640 480]
2255 *
2256 * NOTE:
2257 * The x and y offset (upper left) in drm_vmw_rect cannot be less than 0. Beside
2258 * device limit on topology, x + w and y + h (lower right) cannot be greater
2259 * than INT_MAX. So topology beyond these limits will return with error.
2260 *
2261 * Returns:
2262 * Zero on success, negative errno on failure.
2263 */
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002264int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data,
2265 struct drm_file *file_priv)
2266{
2267 struct vmw_private *dev_priv = vmw_priv(dev);
Deepak Rawat0c1b1742018-09-13 12:34:37 +02002268 struct drm_mode_config *mode_config = &dev->mode_config;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002269 struct drm_vmw_update_layout_arg *arg =
2270 (struct drm_vmw_update_layout_arg *)data;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002271 void __user *user_rects;
2272 struct drm_vmw_rect *rects;
Deepak Rawat5e241332018-06-20 11:17:16 +02002273 struct drm_rect *drm_rects;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002274 unsigned rects_size;
Deepak Rawat5e241332018-06-20 11:17:16 +02002275 int ret, i;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002276
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002277 if (!arg->num_outputs) {
Deepak Rawat5e241332018-06-20 11:17:16 +02002278 struct drm_rect def_rect = {0, 0, 800, 600};
Deepak Rawat745adc32019-05-09 11:47:40 -07002279 VMW_DEBUG_KMS("Default layout x1 = %d y1 = %d x2 = %d y2 = %d\n",
2280 def_rect.x1, def_rect.y1,
2281 def_rect.x2, def_rect.y2);
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002282 vmw_du_update_layout(dev_priv, 1, &def_rect);
Thomas Hellstrom5151adb2015-03-09 01:56:21 -07002283 return 0;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002284 }
2285
2286 rects_size = arg->num_outputs * sizeof(struct drm_vmw_rect);
Xi Wangbab9efc2011-11-28 12:25:43 +01002287 rects = kcalloc(arg->num_outputs, sizeof(struct drm_vmw_rect),
2288 GFP_KERNEL);
Thomas Hellstrom5151adb2015-03-09 01:56:21 -07002289 if (unlikely(!rects))
2290 return -ENOMEM;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002291
2292 user_rects = (void __user *)(unsigned long)arg->rects;
2293 ret = copy_from_user(rects, user_rects, rects_size);
2294 if (unlikely(ret != 0)) {
2295 DRM_ERROR("Failed to get rects.\n");
2296 ret = -EFAULT;
2297 goto out_free;
2298 }
2299
Deepak Rawat5e241332018-06-20 11:17:16 +02002300 drm_rects = (struct drm_rect *)rects;
2301
Deepak Rawat745adc32019-05-09 11:47:40 -07002302 VMW_DEBUG_KMS("Layout count = %u\n", arg->num_outputs);
Deepak Rawat5e241332018-06-20 11:17:16 +02002303 for (i = 0; i < arg->num_outputs; i++) {
2304 struct drm_vmw_rect curr_rect;
2305
2306 /* Verify user-space for overflow as kernel use drm_rect */
2307 if ((rects[i].x + rects[i].w > INT_MAX) ||
2308 (rects[i].y + rects[i].h > INT_MAX)) {
2309 ret = -ERANGE;
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002310 goto out_free;
2311 }
Sinclair Yehc8261a92015-06-26 01:23:42 -07002312
Deepak Rawat5e241332018-06-20 11:17:16 +02002313 curr_rect = rects[i];
2314 drm_rects[i].x1 = curr_rect.x;
2315 drm_rects[i].y1 = curr_rect.y;
2316 drm_rects[i].x2 = curr_rect.x + curr_rect.w;
2317 drm_rects[i].y2 = curr_rect.y + curr_rect.h;
Deepak Rawat0c1b1742018-09-13 12:34:37 +02002318
Deepak Rawat745adc32019-05-09 11:47:40 -07002319 VMW_DEBUG_KMS(" x1 = %d y1 = %d x2 = %d y2 = %d\n",
2320 drm_rects[i].x1, drm_rects[i].y1,
2321 drm_rects[i].x2, drm_rects[i].y2);
2322
Deepak Rawat0c1b1742018-09-13 12:34:37 +02002323 /*
2324 * Currently this check is limiting the topology within
2325 * mode_config->max (which actually is max texture size
2326 * supported by virtual device). This limit is here to address
2327 * window managers that create a big framebuffer for whole
2328 * topology.
2329 */
2330 if (drm_rects[i].x1 < 0 || drm_rects[i].y1 < 0 ||
2331 drm_rects[i].x2 > mode_config->max_width ||
2332 drm_rects[i].y2 > mode_config->max_height) {
Deepak Rawat9bb34e92019-05-09 12:42:58 -07002333 VMW_DEBUG_KMS("Invalid layout %d %d %d %d\n",
2334 drm_rects[i].x1, drm_rects[i].y1,
2335 drm_rects[i].x2, drm_rects[i].y2);
Deepak Rawat0c1b1742018-09-13 12:34:37 +02002336 ret = -EINVAL;
2337 goto out_free;
2338 }
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002339 }
2340
Deepak Rawat5e241332018-06-20 11:17:16 +02002341 ret = vmw_kms_check_display_memory(dev, arg->num_outputs, drm_rects);
Sinclair Yeh65ade7d2015-07-16 10:49:13 -07002342
Deepak Rawat5e241332018-06-20 11:17:16 +02002343 if (ret == 0)
2344 vmw_du_update_layout(dev_priv, arg->num_outputs, drm_rects);
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002345
2346out_free:
2347 kfree(rects);
Thomas Hellstromcd2b89e2011-10-25 23:35:53 +02002348 return ret;
2349}
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07002350
2351/**
2352 * vmw_kms_helper_dirty - Helper to build commands and perform actions based
2353 * on a set of cliprects and a set of display units.
2354 *
2355 * @dev_priv: Pointer to a device private structure.
2356 * @framebuffer: Pointer to the framebuffer on which to perform the actions.
2357 * @clips: A set of struct drm_clip_rect. Either this os @vclips must be NULL.
2358 * Cliprects are given in framebuffer coordinates.
2359 * @vclips: A set of struct drm_vmw_rect cliprects. Either this or @clips must
2360 * be NULL. Cliprects are given in source coordinates.
2361 * @dest_x: X coordinate offset for the crtc / destination clip rects.
2362 * @dest_y: Y coordinate offset for the crtc / destination clip rects.
2363 * @num_clips: Number of cliprects in the @clips or @vclips array.
2364 * @increment: Integer with which to increment the clip counter when looping.
2365 * Used to skip a predetermined number of clip rects.
2366 * @dirty: Closure structure. See the description of struct vmw_kms_dirty.
2367 */
2368int vmw_kms_helper_dirty(struct vmw_private *dev_priv,
2369 struct vmw_framebuffer *framebuffer,
2370 const struct drm_clip_rect *clips,
2371 const struct drm_vmw_rect *vclips,
2372 s32 dest_x, s32 dest_y,
2373 int num_clips,
2374 int increment,
2375 struct vmw_kms_dirty *dirty)
2376{
2377 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
2378 struct drm_crtc *crtc;
2379 u32 num_units = 0;
2380 u32 i, k;
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07002381
2382 dirty->dev_priv = dev_priv;
2383
Deepak Rawat91e9f352018-01-16 08:24:17 +01002384 /* If crtc is passed, no need to iterate over other display units */
2385 if (dirty->crtc) {
2386 units[num_units++] = vmw_crtc_to_du(dirty->crtc);
2387 } else {
Zack Rusin9703bb32020-11-03 22:21:34 -05002388 list_for_each_entry(crtc, &dev_priv->drm.mode_config.crtc_list,
Deepak Rawat91e9f352018-01-16 08:24:17 +01002389 head) {
Ville Syrjälä464ce092018-05-25 21:50:34 +03002390 struct drm_plane *plane = crtc->primary;
2391
2392 if (plane->state->fb == &framebuffer->base)
2393 units[num_units++] = vmw_crtc_to_du(crtc);
Deepak Rawat91e9f352018-01-16 08:24:17 +01002394 }
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07002395 }
2396
2397 for (k = 0; k < num_units; k++) {
2398 struct vmw_display_unit *unit = units[k];
2399 s32 crtc_x = unit->crtc.x;
2400 s32 crtc_y = unit->crtc.y;
2401 s32 crtc_width = unit->crtc.mode.hdisplay;
2402 s32 crtc_height = unit->crtc.mode.vdisplay;
2403 const struct drm_clip_rect *clips_ptr = clips;
2404 const struct drm_vmw_rect *vclips_ptr = vclips;
2405
2406 dirty->unit = unit;
2407 if (dirty->fifo_reserve_size > 0) {
Zack Rusin8426ed92020-11-18 12:54:19 -05002408 dirty->cmd = VMW_CMD_RESERVE(dev_priv,
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07002409 dirty->fifo_reserve_size);
Deepak Rawat11c45412019-02-14 16:15:39 -08002410 if (!dirty->cmd)
Christian Engelmayerf3b8c0c2015-09-19 00:32:24 +02002411 return -ENOMEM;
Deepak Rawat11c45412019-02-14 16:15:39 -08002412
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07002413 memset(dirty->cmd, 0, dirty->fifo_reserve_size);
2414 }
2415 dirty->num_hits = 0;
2416 for (i = 0; i < num_clips; i++, clips_ptr += increment,
2417 vclips_ptr += increment) {
2418 s32 clip_left;
2419 s32 clip_top;
2420
2421 /*
2422 * Select clip array type. Note that integer type
2423 * in @clips is unsigned short, whereas in @vclips
2424 * it's 32-bit.
2425 */
2426 if (clips) {
2427 dirty->fb_x = (s32) clips_ptr->x1;
2428 dirty->fb_y = (s32) clips_ptr->y1;
2429 dirty->unit_x2 = (s32) clips_ptr->x2 + dest_x -
2430 crtc_x;
2431 dirty->unit_y2 = (s32) clips_ptr->y2 + dest_y -
2432 crtc_y;
2433 } else {
2434 dirty->fb_x = vclips_ptr->x;
2435 dirty->fb_y = vclips_ptr->y;
2436 dirty->unit_x2 = dirty->fb_x + vclips_ptr->w +
2437 dest_x - crtc_x;
2438 dirty->unit_y2 = dirty->fb_y + vclips_ptr->h +
2439 dest_y - crtc_y;
2440 }
2441
2442 dirty->unit_x1 = dirty->fb_x + dest_x - crtc_x;
2443 dirty->unit_y1 = dirty->fb_y + dest_y - crtc_y;
2444
2445 /* Skip this clip if it's outside the crtc region */
2446 if (dirty->unit_x1 >= crtc_width ||
2447 dirty->unit_y1 >= crtc_height ||
2448 dirty->unit_x2 <= 0 || dirty->unit_y2 <= 0)
2449 continue;
2450
2451 /* Clip right and bottom to crtc limits */
2452 dirty->unit_x2 = min_t(s32, dirty->unit_x2,
2453 crtc_width);
2454 dirty->unit_y2 = min_t(s32, dirty->unit_y2,
2455 crtc_height);
2456
2457 /* Clip left and top to crtc limits */
2458 clip_left = min_t(s32, dirty->unit_x1, 0);
2459 clip_top = min_t(s32, dirty->unit_y1, 0);
2460 dirty->unit_x1 -= clip_left;
2461 dirty->unit_y1 -= clip_top;
2462 dirty->fb_x -= clip_left;
2463 dirty->fb_y -= clip_top;
2464
2465 dirty->clip(dirty);
2466 }
2467
2468 dirty->fifo_commit(dirty);
2469 }
2470
2471 return 0;
2472}
2473
2474/**
Thomas Hellstrom2724b2d2018-09-26 15:34:50 +02002475 * vmw_kms_helper_validation_finish - Helper for post KMS command submission
2476 * cleanup and fencing
2477 * @dev_priv: Pointer to the device-private struct
2478 * @file_priv: Pointer identifying the client when user-space fencing is used
2479 * @ctx: Pointer to the validation context
2480 * @out_fence: If non-NULL, returned refcounted fence-pointer
2481 * @user_fence_rep: If non-NULL, pointer to user-space address area
2482 * in which to copy user-space fence info
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07002483 */
Thomas Hellstrom2724b2d2018-09-26 15:34:50 +02002484void vmw_kms_helper_validation_finish(struct vmw_private *dev_priv,
2485 struct drm_file *file_priv,
2486 struct vmw_validation_context *ctx,
2487 struct vmw_fence_obj **out_fence,
2488 struct drm_vmw_fence_rep __user *
2489 user_fence_rep)
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07002490{
Thomas Hellstrom2724b2d2018-09-26 15:34:50 +02002491 struct vmw_fence_obj *fence = NULL;
Thomas Hellstrom51fdbeb2019-01-31 10:52:21 +01002492 uint32_t handle = 0;
2493 int ret = 0;
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07002494
Thomas Hellstrom2724b2d2018-09-26 15:34:50 +02002495 if (file_priv || user_fence_rep || vmw_validation_has_bos(ctx) ||
2496 out_fence)
2497 ret = vmw_execbuf_fence_commands(file_priv, dev_priv, &fence,
2498 file_priv ? &handle : NULL);
2499 vmw_validation_done(ctx, fence);
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07002500 if (file_priv)
2501 vmw_execbuf_copy_fence_user(dev_priv, vmw_fpriv(file_priv),
2502 ret, user_fence_rep, fence,
Sinclair Yehc906965d2017-07-05 01:49:32 -07002503 handle, -1, NULL);
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07002504 if (out_fence)
2505 *out_fence = fence;
2506 else
2507 vmw_fence_obj_unreference(&fence);
Thomas Hellstrom1a4b1722015-06-26 02:03:53 -07002508}
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07002509
2510/**
2511 * vmw_kms_update_proxy - Helper function to update a proxy surface from
2512 * its backing MOB.
2513 *
2514 * @res: Pointer to the surface resource
2515 * @clips: Clip rects in framebuffer (surface) space.
2516 * @num_clips: Number of clips in @clips.
2517 * @increment: Integer with which to increment the clip counter when looping.
2518 * Used to skip a predetermined number of clip rects.
2519 *
2520 * This function makes sure the proxy surface is updated from its backing MOB
2521 * using the region given by @clips. The surface resource @res and its backing
2522 * MOB needs to be reserved and validated on call.
2523 */
2524int vmw_kms_update_proxy(struct vmw_resource *res,
2525 const struct drm_clip_rect *clips,
2526 unsigned num_clips,
2527 int increment)
2528{
2529 struct vmw_private *dev_priv = res->dev_priv;
Deepak Rawat26b82872019-04-29 11:42:45 -07002530 struct drm_vmw_size *size = &vmw_res_to_srf(res)->metadata.base_size;
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07002531 struct {
2532 SVGA3dCmdHeader header;
2533 SVGA3dCmdUpdateGBImage body;
2534 } *cmd;
2535 SVGA3dBox *box;
2536 size_t copy_size = 0;
2537 int i;
2538
2539 if (!clips)
2540 return 0;
2541
Zack Rusin8426ed92020-11-18 12:54:19 -05002542 cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd) * num_clips);
Deepak Rawat11c45412019-02-14 16:15:39 -08002543 if (!cmd)
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07002544 return -ENOMEM;
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07002545
2546 for (i = 0; i < num_clips; ++i, clips += increment, ++cmd) {
2547 box = &cmd->body.box;
2548
2549 cmd->header.id = SVGA_3D_CMD_UPDATE_GB_IMAGE;
2550 cmd->header.size = sizeof(cmd->body);
2551 cmd->body.image.sid = res->id;
2552 cmd->body.image.face = 0;
2553 cmd->body.image.mipmap = 0;
2554
2555 if (clips->x1 > size->width || clips->x2 > size->width ||
2556 clips->y1 > size->height || clips->y2 > size->height) {
2557 DRM_ERROR("Invalid clips outsize of framebuffer.\n");
2558 return -EINVAL;
2559 }
2560
2561 box->x = clips->x1;
2562 box->y = clips->y1;
2563 box->z = 0;
2564 box->w = clips->x2 - clips->x1;
2565 box->h = clips->y2 - clips->y1;
2566 box->d = 1;
2567
2568 copy_size += sizeof(*cmd);
2569 }
2570
Zack Rusin8426ed92020-11-18 12:54:19 -05002571 vmw_cmd_commit(dev_priv, copy_size);
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07002572
2573 return 0;
2574}
Thomas Hellstroma2787242015-06-29 12:55:07 -07002575
2576int vmw_kms_fbdev_init_data(struct vmw_private *dev_priv,
2577 unsigned unit,
2578 u32 max_width,
2579 u32 max_height,
2580 struct drm_connector **p_con,
2581 struct drm_crtc **p_crtc,
2582 struct drm_display_mode **p_mode)
2583{
2584 struct drm_connector *con;
2585 struct vmw_display_unit *du;
2586 struct drm_display_mode *mode;
2587 int i = 0;
Thomas Hellstrom21fbd082018-04-26 09:48:55 +02002588 int ret = 0;
Thomas Hellstroma2787242015-06-29 12:55:07 -07002589
Zack Rusin9703bb32020-11-03 22:21:34 -05002590 mutex_lock(&dev_priv->drm.mode_config.mutex);
2591 list_for_each_entry(con, &dev_priv->drm.mode_config.connector_list,
Thomas Hellstroma2787242015-06-29 12:55:07 -07002592 head) {
2593 if (i == unit)
2594 break;
2595
2596 ++i;
2597 }
2598
Zack Rusin9703bb32020-11-03 22:21:34 -05002599 if (&con->head == &dev_priv->drm.mode_config.connector_list) {
Thomas Hellstroma2787242015-06-29 12:55:07 -07002600 DRM_ERROR("Could not find initial display unit.\n");
Thomas Hellstrom21fbd082018-04-26 09:48:55 +02002601 ret = -EINVAL;
2602 goto out_unlock;
Thomas Hellstroma2787242015-06-29 12:55:07 -07002603 }
2604
2605 if (list_empty(&con->modes))
2606 (void) vmw_du_connector_fill_modes(con, max_width, max_height);
2607
2608 if (list_empty(&con->modes)) {
2609 DRM_ERROR("Could not find initial display mode.\n");
Thomas Hellstrom21fbd082018-04-26 09:48:55 +02002610 ret = -EINVAL;
2611 goto out_unlock;
Thomas Hellstroma2787242015-06-29 12:55:07 -07002612 }
2613
2614 du = vmw_connector_to_du(con);
2615 *p_con = con;
2616 *p_crtc = &du->crtc;
2617
2618 list_for_each_entry(mode, &con->modes, head) {
2619 if (mode->type & DRM_MODE_TYPE_PREFERRED)
2620 break;
2621 }
2622
Dan Carpenter4437c112020-06-26 13:39:59 +03002623 if (&mode->head == &con->modes) {
Thomas Hellstroma2787242015-06-29 12:55:07 -07002624 WARN_ONCE(true, "Could not find initial preferred mode.\n");
2625 *p_mode = list_first_entry(&con->modes,
2626 struct drm_display_mode,
2627 head);
Dan Carpenter4437c112020-06-26 13:39:59 +03002628 } else {
2629 *p_mode = mode;
Thomas Hellstroma2787242015-06-29 12:55:07 -07002630 }
2631
Thomas Hellstrom21fbd082018-04-26 09:48:55 +02002632 out_unlock:
Zack Rusin9703bb32020-11-03 22:21:34 -05002633 mutex_unlock(&dev_priv->drm.mode_config.mutex);
Thomas Hellstrom21fbd082018-04-26 09:48:55 +02002634
2635 return ret;
Thomas Hellstroma2787242015-06-29 12:55:07 -07002636}
Thomas Hellstrom75c06852016-02-12 09:00:26 +01002637
2638/**
Thomas Hellstrom76404ac2016-02-12 09:55:45 +01002639 * vmw_kms_create_implicit_placement_proparty - Set up the implicit placement
2640 * property.
2641 *
2642 * @dev_priv: Pointer to a device private struct.
Thomas Hellstrom76404ac2016-02-12 09:55:45 +01002643 *
2644 * Sets up the implicit placement property unless it's already set up.
2645 */
2646void
Thomas Hellstrom9d9486e2018-10-04 22:38:17 +00002647vmw_kms_create_implicit_placement_property(struct vmw_private *dev_priv)
Thomas Hellstrom76404ac2016-02-12 09:55:45 +01002648{
2649 if (dev_priv->implicit_placement_property)
2650 return;
2651
2652 dev_priv->implicit_placement_property =
Zack Rusin9703bb32020-11-03 22:21:34 -05002653 drm_property_create_range(&dev_priv->drm,
Thomas Hellstrom9d9486e2018-10-04 22:38:17 +00002654 DRM_MODE_PROP_IMMUTABLE,
Thomas Hellstrom76404ac2016-02-12 09:55:45 +01002655 "implicit_placement", 0, 1);
Thomas Hellstrom76404ac2016-02-12 09:55:45 +01002656}
Sinclair Yeh904bb5e2017-03-23 14:29:22 -07002657
Thomas Hellstromc3b9b162018-03-22 10:26:37 +01002658/**
2659 * vmw_kms_suspend - Save modesetting state and turn modesetting off.
2660 *
2661 * @dev: Pointer to the drm device
2662 * Return: 0 on success. Negative error code on failure.
2663 */
2664int vmw_kms_suspend(struct drm_device *dev)
2665{
2666 struct vmw_private *dev_priv = vmw_priv(dev);
2667
2668 dev_priv->suspend_state = drm_atomic_helper_suspend(dev);
2669 if (IS_ERR(dev_priv->suspend_state)) {
2670 int ret = PTR_ERR(dev_priv->suspend_state);
2671
2672 DRM_ERROR("Failed kms suspend: %d\n", ret);
2673 dev_priv->suspend_state = NULL;
2674
2675 return ret;
2676 }
2677
2678 return 0;
2679}
2680
2681
2682/**
2683 * vmw_kms_resume - Re-enable modesetting and restore state
2684 *
2685 * @dev: Pointer to the drm device
2686 * Return: 0 on success. Negative error code on failure.
2687 *
2688 * State is resumed from a previous vmw_kms_suspend(). It's illegal
2689 * to call this function without a previous vmw_kms_suspend().
2690 */
2691int vmw_kms_resume(struct drm_device *dev)
2692{
2693 struct vmw_private *dev_priv = vmw_priv(dev);
2694 int ret;
2695
2696 if (WARN_ON(!dev_priv->suspend_state))
2697 return 0;
2698
2699 ret = drm_atomic_helper_resume(dev, dev_priv->suspend_state);
2700 dev_priv->suspend_state = NULL;
2701
2702 return ret;
2703}
Dave Airlie2b4f44ee2018-03-28 14:30:41 +10002704
2705/**
Thomas Hellstrom140bcaa2018-03-08 10:07:37 +01002706 * vmw_kms_lost_device - Notify kms that modesetting capabilities will be lost
2707 *
2708 * @dev: Pointer to the drm device
2709 */
2710void vmw_kms_lost_device(struct drm_device *dev)
2711{
2712 drm_atomic_helper_shutdown(dev);
2713}
Deepak Rawate41774c2018-07-11 15:42:45 -07002714
2715/**
2716 * vmw_du_helper_plane_update - Helper to do plane update on a display unit.
2717 * @update: The closure structure.
2718 *
2719 * Call this helper after setting callbacks in &vmw_du_update_plane to do plane
2720 * update on display unit.
2721 *
2722 * Return: 0 on success or a negative error code on failure.
2723 */
2724int vmw_du_helper_plane_update(struct vmw_du_update_plane *update)
2725{
2726 struct drm_plane_state *state = update->plane->state;
2727 struct drm_plane_state *old_state = update->old_state;
2728 struct drm_atomic_helper_damage_iter iter;
2729 struct drm_rect clip;
2730 struct drm_rect bb;
2731 DECLARE_VAL_CONTEXT(val_ctx, NULL, 0);
2732 uint32_t reserved_size = 0;
2733 uint32_t submit_size = 0;
2734 uint32_t curr_size = 0;
2735 uint32_t num_hits = 0;
2736 void *cmd_start;
2737 char *cmd_next;
2738 int ret;
2739
2740 /*
2741 * Iterate in advance to check if really need plane update and find the
2742 * number of clips that actually are in plane src for fifo allocation.
2743 */
2744 drm_atomic_helper_damage_iter_init(&iter, old_state, state);
2745 drm_atomic_for_each_plane_damage(&iter, &clip)
2746 num_hits++;
2747
2748 if (num_hits == 0)
2749 return 0;
2750
2751 if (update->vfb->bo) {
2752 struct vmw_framebuffer_bo *vfbbo =
2753 container_of(update->vfb, typeof(*vfbbo), base);
2754
2755 ret = vmw_validation_add_bo(&val_ctx, vfbbo->buffer, false,
2756 update->cpu_blit);
2757 } else {
2758 struct vmw_framebuffer_surface *vfbs =
2759 container_of(update->vfb, typeof(*vfbs), base);
2760
2761 ret = vmw_validation_add_resource(&val_ctx, &vfbs->surface->res,
Thomas Hellstroma9f58c42019-02-20 08:21:26 +01002762 0, VMW_RES_DIRTY_NONE, NULL,
2763 NULL);
Deepak Rawate41774c2018-07-11 15:42:45 -07002764 }
2765
2766 if (ret)
2767 return ret;
2768
2769 ret = vmw_validation_prepare(&val_ctx, update->mutex, update->intr);
2770 if (ret)
2771 goto out_unref;
2772
2773 reserved_size = update->calc_fifo_size(update, num_hits);
Zack Rusin8426ed92020-11-18 12:54:19 -05002774 cmd_start = VMW_CMD_RESERVE(update->dev_priv, reserved_size);
Deepak Rawate41774c2018-07-11 15:42:45 -07002775 if (!cmd_start) {
2776 ret = -ENOMEM;
2777 goto out_revert;
2778 }
2779
2780 cmd_next = cmd_start;
2781
2782 if (update->post_prepare) {
2783 curr_size = update->post_prepare(update, cmd_next);
2784 cmd_next += curr_size;
2785 submit_size += curr_size;
2786 }
2787
2788 if (update->pre_clip) {
2789 curr_size = update->pre_clip(update, cmd_next, num_hits);
2790 cmd_next += curr_size;
2791 submit_size += curr_size;
2792 }
2793
2794 bb.x1 = INT_MAX;
2795 bb.y1 = INT_MAX;
2796 bb.x2 = INT_MIN;
2797 bb.y2 = INT_MIN;
2798
2799 drm_atomic_helper_damage_iter_init(&iter, old_state, state);
2800 drm_atomic_for_each_plane_damage(&iter, &clip) {
2801 uint32_t fb_x = clip.x1;
2802 uint32_t fb_y = clip.y1;
2803
2804 vmw_du_translate_to_crtc(state, &clip);
2805 if (update->clip) {
2806 curr_size = update->clip(update, cmd_next, &clip, fb_x,
2807 fb_y);
2808 cmd_next += curr_size;
2809 submit_size += curr_size;
2810 }
2811 bb.x1 = min_t(int, bb.x1, clip.x1);
2812 bb.y1 = min_t(int, bb.y1, clip.y1);
2813 bb.x2 = max_t(int, bb.x2, clip.x2);
2814 bb.y2 = max_t(int, bb.y2, clip.y2);
2815 }
2816
2817 curr_size = update->post_clip(update, cmd_next, &bb);
2818 submit_size += curr_size;
2819
2820 if (reserved_size < submit_size)
2821 submit_size = 0;
2822
Zack Rusin8426ed92020-11-18 12:54:19 -05002823 vmw_cmd_commit(update->dev_priv, submit_size);
Deepak Rawate41774c2018-07-11 15:42:45 -07002824
2825 vmw_kms_helper_validation_finish(update->dev_priv, NULL, &val_ctx,
2826 update->out_fence, NULL);
2827 return ret;
2828
2829out_revert:
2830 vmw_validation_revert(&val_ctx);
2831
2832out_unref:
2833 vmw_validation_unref_lists(&val_ctx);
2834 return ret;
2835}