]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/gpu/drm/vmwgfx/vmwgfx_scrn.c
vmwgfx: Make the preferred autofit mode have a 60Hz vrefresh
[mirror_ubuntu-hirsute-kernel.git] / drivers / gpu / drm / vmwgfx / vmwgfx_scrn.c
CommitLineData
56d1c78d
JB
1/**************************************************************************
2 *
3 * Copyright © 2011 VMware, Inc., Palo Alto, CA., USA
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28#include "vmwgfx_kms.h"
29
30
31#define vmw_crtc_to_sou(x) \
32 container_of(x, struct vmw_screen_object_unit, base.crtc)
33#define vmw_encoder_to_sou(x) \
34 container_of(x, struct vmw_screen_object_unit, base.encoder)
35#define vmw_connector_to_sou(x) \
36 container_of(x, struct vmw_screen_object_unit, base.connector)
37
38struct vmw_screen_object_display {
56d1c78d 39 unsigned num_active;
56d1c78d
JB
40
41 struct vmw_framebuffer *fb;
42};
43
44/**
45 * Display unit using screen objects.
46 */
47struct vmw_screen_object_unit {
48 struct vmw_display_unit base;
49
50 unsigned long buffer_size; /**< Size of allocated buffer */
51 struct vmw_dma_buffer *buffer; /**< Backing store buffer */
52
53 bool defined;
0e708bc5 54 bool active;
56d1c78d
JB
55};
56
57static void vmw_sou_destroy(struct vmw_screen_object_unit *sou)
58{
56d1c78d
JB
59 vmw_display_unit_cleanup(&sou->base);
60 kfree(sou);
61}
62
63
64/*
65 * Screen Object Display Unit CRTC functions
66 */
67
68static void vmw_sou_crtc_destroy(struct drm_crtc *crtc)
69{
70 vmw_sou_destroy(vmw_crtc_to_sou(crtc));
71}
72
0e708bc5 73static void vmw_sou_del_active(struct vmw_private *vmw_priv,
56d1c78d
JB
74 struct vmw_screen_object_unit *sou)
75{
76 struct vmw_screen_object_display *ld = vmw_priv->sou_priv;
56d1c78d 77
0e708bc5
TH
78 if (sou->active) {
79 if (--(ld->num_active) == 0)
80 ld->fb = NULL;
81 sou->active = false;
82 }
56d1c78d
JB
83}
84
0e708bc5 85static void vmw_sou_add_active(struct vmw_private *vmw_priv,
56d1c78d
JB
86 struct vmw_screen_object_unit *sou,
87 struct vmw_framebuffer *vfb)
88{
89 struct vmw_screen_object_display *ld = vmw_priv->sou_priv;
56d1c78d
JB
90
91 BUG_ON(!ld->num_active && ld->fb);
56d1c78d 92
0e708bc5
TH
93 if (!sou->active) {
94 ld->fb = vfb;
95 sou->active = true;
96 ld->num_active++;
56d1c78d 97 }
56d1c78d
JB
98}
99
100/**
101 * Send the fifo command to create a screen.
102 */
103static int vmw_sou_fifo_create(struct vmw_private *dev_priv,
104 struct vmw_screen_object_unit *sou,
105 uint32_t x, uint32_t y,
106 struct drm_display_mode *mode)
107{
108 size_t fifo_size;
109
110 struct {
111 struct {
112 uint32_t cmdType;
113 } header;
114 SVGAScreenObject obj;
115 } *cmd;
116
117 BUG_ON(!sou->buffer);
118
119 fifo_size = sizeof(*cmd);
120 cmd = vmw_fifo_reserve(dev_priv, fifo_size);
121 /* The hardware has hung, nothing we can do about it here. */
122 if (unlikely(cmd == NULL)) {
123 DRM_ERROR("Fifo reserve failed.\n");
124 return -ENOMEM;
125 }
126
127 memset(cmd, 0, fifo_size);
128 cmd->header.cmdType = SVGA_CMD_DEFINE_SCREEN;
129 cmd->obj.structSize = sizeof(SVGAScreenObject);
130 cmd->obj.id = sou->base.unit;
131 cmd->obj.flags = SVGA_SCREEN_HAS_ROOT |
132 (sou->base.unit == 0 ? SVGA_SCREEN_IS_PRIMARY : 0);
133 cmd->obj.size.width = mode->hdisplay;
134 cmd->obj.size.height = mode->vdisplay;
135 cmd->obj.root.x = x;
136 cmd->obj.root.y = y;
137
138 /* Ok to assume that buffer is pinned in vram */
b37a6b9a 139 vmw_bo_get_guest_ptr(&sou->buffer->base, &cmd->obj.backingStore.ptr);
56d1c78d
JB
140 cmd->obj.backingStore.pitch = mode->hdisplay * 4;
141
142 vmw_fifo_commit(dev_priv, fifo_size);
143
144 sou->defined = true;
145
146 return 0;
147}
148
149/**
150 * Send the fifo command to destroy a screen.
151 */
152static int vmw_sou_fifo_destroy(struct vmw_private *dev_priv,
153 struct vmw_screen_object_unit *sou)
154{
155 size_t fifo_size;
156 int ret;
157
158 struct {
159 struct {
160 uint32_t cmdType;
161 } header;
162 SVGAFifoCmdDestroyScreen body;
163 } *cmd;
164
165 /* no need to do anything */
166 if (unlikely(!sou->defined))
167 return 0;
168
169 fifo_size = sizeof(*cmd);
170 cmd = vmw_fifo_reserve(dev_priv, fifo_size);
171 /* the hardware has hung, nothing we can do about it here */
172 if (unlikely(cmd == NULL)) {
173 DRM_ERROR("Fifo reserve failed.\n");
174 return -ENOMEM;
175 }
176
177 memset(cmd, 0, fifo_size);
178 cmd->header.cmdType = SVGA_CMD_DESTROY_SCREEN;
179 cmd->body.screenId = sou->base.unit;
180
181 vmw_fifo_commit(dev_priv, fifo_size);
182
183 /* Force sync */
184 ret = vmw_fallback_wait(dev_priv, false, true, 0, false, 3*HZ);
185 if (unlikely(ret != 0))
186 DRM_ERROR("Failed to sync with HW");
187 else
188 sou->defined = false;
189
190 return ret;
191}
192
193/**
194 * Free the backing store.
195 */
196static void vmw_sou_backing_free(struct vmw_private *dev_priv,
197 struct vmw_screen_object_unit *sou)
198{
199 struct ttm_buffer_object *bo;
200
201 if (unlikely(sou->buffer == NULL))
202 return;
203
204 bo = &sou->buffer->base;
205 ttm_bo_unref(&bo);
206 sou->buffer = NULL;
207 sou->buffer_size = 0;
208}
209
210/**
211 * Allocate the backing store for the buffer.
212 */
213static int vmw_sou_backing_alloc(struct vmw_private *dev_priv,
214 struct vmw_screen_object_unit *sou,
215 unsigned long size)
216{
217 int ret;
218
219 if (sou->buffer_size == size)
220 return 0;
221
222 if (sou->buffer)
223 vmw_sou_backing_free(dev_priv, sou);
224
225 sou->buffer = kzalloc(sizeof(*sou->buffer), GFP_KERNEL);
226 if (unlikely(sou->buffer == NULL))
227 return -ENOMEM;
228
229 /* After we have alloced the backing store might not be able to
230 * resume the overlays, this is preferred to failing to alloc.
231 */
232 vmw_overlay_pause_all(dev_priv);
233 ret = vmw_dmabuf_init(dev_priv, sou->buffer, size,
234 &vmw_vram_ne_placement,
235 false, &vmw_dmabuf_bo_free);
236 vmw_overlay_resume_all(dev_priv);
237
238 if (unlikely(ret != 0))
239 sou->buffer = NULL; /* vmw_dmabuf_init frees on error */
240 else
241 sou->buffer_size = size;
242
243 return ret;
244}
245
246static int vmw_sou_crtc_set_config(struct drm_mode_set *set)
247{
248 struct vmw_private *dev_priv;
249 struct vmw_screen_object_unit *sou;
250 struct drm_connector *connector;
251 struct drm_display_mode *mode;
252 struct drm_encoder *encoder;
253 struct vmw_framebuffer *vfb;
254 struct drm_framebuffer *fb;
255 struct drm_crtc *crtc;
256 int ret = 0;
257
258 if (!set)
259 return -EINVAL;
260
261 if (!set->crtc)
262 return -EINVAL;
263
264 /* get the sou */
265 crtc = set->crtc;
266 sou = vmw_crtc_to_sou(crtc);
267 vfb = set->fb ? vmw_framebuffer_to_vfb(set->fb) : NULL;
268 dev_priv = vmw_priv(crtc->dev);
269
270 if (set->num_connectors > 1) {
271 DRM_ERROR("to many connectors\n");
272 return -EINVAL;
273 }
274
275 if (set->num_connectors == 1 &&
276 set->connectors[0] != &sou->base.connector) {
277 DRM_ERROR("connector doesn't match %p %p\n",
278 set->connectors[0], &sou->base.connector);
279 return -EINVAL;
280 }
281
282 /* sou only supports one fb active at the time */
283 if (dev_priv->sou_priv->fb && vfb &&
284 !(dev_priv->sou_priv->num_active == 1 &&
0e708bc5 285 sou->active) &&
56d1c78d
JB
286 dev_priv->sou_priv->fb != vfb) {
287 DRM_ERROR("Multiple framebuffers not supported\n");
288 return -EINVAL;
289 }
290
291 /* since they always map one to one these are safe */
292 connector = &sou->base.connector;
293 encoder = &sou->base.encoder;
294
295 /* should we turn the crtc off */
296 if (set->num_connectors == 0 || !set->mode || !set->fb) {
297 ret = vmw_sou_fifo_destroy(dev_priv, sou);
298 /* the hardware has hung don't do anything more */
299 if (unlikely(ret != 0))
300 return ret;
301
302 connector->encoder = NULL;
303 encoder->crtc = NULL;
304 crtc->fb = NULL;
305 crtc->x = 0;
306 crtc->y = 0;
307
308 vmw_sou_del_active(dev_priv, sou);
309
310 vmw_sou_backing_free(dev_priv, sou);
311
312 return 0;
313 }
314
315
316 /* we now know we want to set a mode */
317 mode = set->mode;
318 fb = set->fb;
319
320 if (set->x + mode->hdisplay > fb->width ||
321 set->y + mode->vdisplay > fb->height) {
322 DRM_ERROR("set outside of framebuffer\n");
323 return -EINVAL;
324 }
325
326 vmw_fb_off(dev_priv);
327
328 if (mode->hdisplay != crtc->mode.hdisplay ||
329 mode->vdisplay != crtc->mode.vdisplay) {
330 /* no need to check if depth is different, because backing
331 * store depth is forced to 4 by the device.
332 */
333
334 ret = vmw_sou_fifo_destroy(dev_priv, sou);
335 /* the hardware has hung don't do anything more */
336 if (unlikely(ret != 0))
337 return ret;
338
339 vmw_sou_backing_free(dev_priv, sou);
340 }
341
342 if (!sou->buffer) {
343 /* forced to depth 4 by the device */
344 size_t size = mode->hdisplay * mode->vdisplay * 4;
345 ret = vmw_sou_backing_alloc(dev_priv, sou, size);
346 if (unlikely(ret != 0))
347 return ret;
348 }
349
350 ret = vmw_sou_fifo_create(dev_priv, sou, set->x, set->y, mode);
351 if (unlikely(ret != 0)) {
352 /*
353 * We are in a bit of a situation here, the hardware has
354 * hung and we may or may not have a buffer hanging of
355 * the screen object, best thing to do is not do anything
356 * if we where defined, if not just turn the crtc of.
357 * Not what userspace wants but it needs to htfu.
358 */
359 if (sou->defined)
360 return ret;
361
362 connector->encoder = NULL;
363 encoder->crtc = NULL;
364 crtc->fb = NULL;
365 crtc->x = 0;
366 crtc->y = 0;
367
368 return ret;
369 }
370
371 vmw_sou_add_active(dev_priv, sou, vfb);
372
373 connector->encoder = encoder;
374 encoder->crtc = crtc;
375 crtc->mode = *mode;
376 crtc->fb = fb;
377 crtc->x = set->x;
378 crtc->y = set->y;
379
380 return 0;
381}
382
383static struct drm_crtc_funcs vmw_screen_object_crtc_funcs = {
384 .save = vmw_du_crtc_save,
385 .restore = vmw_du_crtc_restore,
386 .cursor_set = vmw_du_crtc_cursor_set,
387 .cursor_move = vmw_du_crtc_cursor_move,
388 .gamma_set = vmw_du_crtc_gamma_set,
389 .destroy = vmw_sou_crtc_destroy,
390 .set_config = vmw_sou_crtc_set_config,
391};
392
393/*
394 * Screen Object Display Unit encoder functions
395 */
396
397static void vmw_sou_encoder_destroy(struct drm_encoder *encoder)
398{
399 vmw_sou_destroy(vmw_encoder_to_sou(encoder));
400}
401
402static struct drm_encoder_funcs vmw_screen_object_encoder_funcs = {
403 .destroy = vmw_sou_encoder_destroy,
404};
405
406/*
407 * Screen Object Display Unit connector functions
408 */
409
410static void vmw_sou_connector_destroy(struct drm_connector *connector)
411{
412 vmw_sou_destroy(vmw_connector_to_sou(connector));
413}
414
415static struct drm_connector_funcs vmw_legacy_connector_funcs = {
416 .dpms = vmw_du_connector_dpms,
417 .save = vmw_du_connector_save,
418 .restore = vmw_du_connector_restore,
419 .detect = vmw_du_connector_detect,
420 .fill_modes = vmw_du_connector_fill_modes,
421 .set_property = vmw_du_connector_set_property,
422 .destroy = vmw_sou_connector_destroy,
423};
424
425static int vmw_sou_init(struct vmw_private *dev_priv, unsigned unit)
426{
427 struct vmw_screen_object_unit *sou;
428 struct drm_device *dev = dev_priv->dev;
429 struct drm_connector *connector;
430 struct drm_encoder *encoder;
431 struct drm_crtc *crtc;
432
433 sou = kzalloc(sizeof(*sou), GFP_KERNEL);
434 if (!sou)
435 return -ENOMEM;
436
437 sou->base.unit = unit;
438 crtc = &sou->base.crtc;
439 encoder = &sou->base.encoder;
440 connector = &sou->base.connector;
441
0e708bc5 442 sou->active = false;
56d1c78d
JB
443
444 sou->base.pref_active = (unit == 0);
445 sou->base.pref_width = 800;
446 sou->base.pref_height = 600;
447 sou->base.pref_mode = NULL;
448
449 drm_connector_init(dev, connector, &vmw_legacy_connector_funcs,
305151e3 450 DRM_MODE_CONNECTOR_VIRTUAL);
56d1c78d
JB
451 connector->status = vmw_du_connector_detect(connector, true);
452
453 drm_encoder_init(dev, encoder, &vmw_screen_object_encoder_funcs,
305151e3 454 DRM_MODE_ENCODER_VIRTUAL);
56d1c78d
JB
455 drm_mode_connector_attach_encoder(connector, encoder);
456 encoder->possible_crtcs = (1 << unit);
457 encoder->possible_clones = 0;
458
459 drm_crtc_init(dev, crtc, &vmw_screen_object_crtc_funcs);
460
461 drm_mode_crtc_set_gamma_size(crtc, 256);
462
463 drm_connector_attach_property(connector,
464 dev->mode_config.dirty_info_property,
465 1);
466
467 return 0;
468}
469
470int vmw_kms_init_screen_object_display(struct vmw_private *dev_priv)
471{
472 struct drm_device *dev = dev_priv->dev;
74b5ea30 473 int i, ret;
56d1c78d
JB
474
475 if (dev_priv->sou_priv) {
476 DRM_INFO("sou system already on\n");
477 return -EINVAL;
478 }
479
480 if (!(dev_priv->fifo.capabilities & SVGA_FIFO_CAP_SCREEN_OBJECT_2)) {
481 DRM_INFO("Not using screen objects,"
482 " missing cap SCREEN_OBJECT_2\n");
483 return -ENOSYS;
484 }
485
486 ret = -ENOMEM;
487 dev_priv->sou_priv = kmalloc(sizeof(*dev_priv->sou_priv), GFP_KERNEL);
488 if (unlikely(!dev_priv->sou_priv))
489 goto err_no_mem;
490
56d1c78d 491 dev_priv->sou_priv->num_active = 0;
56d1c78d
JB
492 dev_priv->sou_priv->fb = NULL;
493
494 ret = drm_vblank_init(dev, VMWGFX_NUM_DISPLAY_UNITS);
495 if (unlikely(ret != 0))
496 goto err_free;
497
74b5ea30 498 ret = drm_mode_create_dirty_info_property(dev);
56d1c78d
JB
499 if (unlikely(ret != 0))
500 goto err_vblank_cleanup;
501
502 for (i = 0; i < VMWGFX_NUM_DISPLAY_UNITS; ++i)
503 vmw_sou_init(dev_priv, i);
504
505 DRM_INFO("Screen objects system initialized\n");
506
507 return 0;
508
509err_vblank_cleanup:
510 drm_vblank_cleanup(dev);
511err_free:
512 kfree(dev_priv->sou_priv);
60a16a30 513 dev_priv->sou_priv = NULL;
56d1c78d
JB
514err_no_mem:
515 return ret;
516}
517
518int vmw_kms_close_screen_object_display(struct vmw_private *dev_priv)
519{
520 struct drm_device *dev = dev_priv->dev;
521
56d1c78d
JB
522 if (!dev_priv->sou_priv)
523 return -ENOSYS;
524
60a16a30
JB
525 drm_vblank_cleanup(dev);
526
0e708bc5 527 if (dev_priv->sou_priv->num_active > 0)
56d1c78d
JB
528 DRM_ERROR("Still have active outputs when unloading driver");
529
530 kfree(dev_priv->sou_priv);
531
532 return 0;
533}