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 #include <drm/drm_plane_helper.h>
30
31
32 #define vmw_crtc_to_sou(x) \
33 container_of(x, struct vmw_screen_object_unit, base.crtc)
34 #define vmw_encoder_to_sou(x) \
35 container_of(x, struct vmw_screen_object_unit, base.encoder)
36 #define vmw_connector_to_sou(x) \
37 container_of(x, struct vmw_screen_object_unit, base.connector)
38
39 struct vmw_screen_object_display {
40 unsigned num_implicit;
41
42 struct vmw_framebuffer *implicit_fb;
43 };
44
45 /**
46 * Display unit using screen objects.
47 */
48 struct vmw_screen_object_unit {
49 struct vmw_display_unit base;
50
51 unsigned long buffer_size; /**< Size of allocated buffer */
52 struct vmw_dma_buffer *buffer; /**< Backing store buffer */
53
54 bool defined;
55 bool active_implicit;
56 };
57
vmw_sou_destroy(struct vmw_screen_object_unit * sou)58 static void vmw_sou_destroy(struct vmw_screen_object_unit *sou)
59 {
60 vmw_display_unit_cleanup(&sou->base);
61 kfree(sou);
62 }
63
64
65 /*
66 * Screen Object Display Unit CRTC functions
67 */
68
vmw_sou_crtc_destroy(struct drm_crtc * crtc)69 static void vmw_sou_crtc_destroy(struct drm_crtc *crtc)
70 {
71 vmw_sou_destroy(vmw_crtc_to_sou(crtc));
72 }
73
vmw_sou_del_active(struct vmw_private * vmw_priv,struct vmw_screen_object_unit * sou)74 static void vmw_sou_del_active(struct vmw_private *vmw_priv,
75 struct vmw_screen_object_unit *sou)
76 {
77 struct vmw_screen_object_display *ld = vmw_priv->sou_priv;
78
79 if (sou->active_implicit) {
80 if (--(ld->num_implicit) == 0)
81 ld->implicit_fb = NULL;
82 sou->active_implicit = false;
83 }
84 }
85
vmw_sou_add_active(struct vmw_private * vmw_priv,struct vmw_screen_object_unit * sou,struct vmw_framebuffer * vfb)86 static void vmw_sou_add_active(struct vmw_private *vmw_priv,
87 struct vmw_screen_object_unit *sou,
88 struct vmw_framebuffer *vfb)
89 {
90 struct vmw_screen_object_display *ld = vmw_priv->sou_priv;
91
92 BUG_ON(!ld->num_implicit && ld->implicit_fb);
93
94 if (!sou->active_implicit && sou->base.is_implicit) {
95 ld->implicit_fb = vfb;
96 sou->active_implicit = true;
97 ld->num_implicit++;
98 }
99 }
100
101 /**
102 * Send the fifo command to create a screen.
103 */
vmw_sou_fifo_create(struct vmw_private * dev_priv,struct vmw_screen_object_unit * sou,uint32_t x,uint32_t y,struct drm_display_mode * mode)104 static int vmw_sou_fifo_create(struct vmw_private *dev_priv,
105 struct vmw_screen_object_unit *sou,
106 uint32_t x, uint32_t y,
107 struct drm_display_mode *mode)
108 {
109 size_t fifo_size;
110
111 struct {
112 struct {
113 uint32_t cmdType;
114 } header;
115 SVGAScreenObject obj;
116 } *cmd;
117
118 BUG_ON(!sou->buffer);
119
120 fifo_size = sizeof(*cmd);
121 cmd = vmw_fifo_reserve(dev_priv, fifo_size);
122 /* The hardware has hung, nothing we can do about it here. */
123 if (unlikely(cmd == NULL)) {
124 DRM_ERROR("Fifo reserve failed.\n");
125 return -ENOMEM;
126 }
127
128 memset(cmd, 0, fifo_size);
129 cmd->header.cmdType = SVGA_CMD_DEFINE_SCREEN;
130 cmd->obj.structSize = sizeof(SVGAScreenObject);
131 cmd->obj.id = sou->base.unit;
132 cmd->obj.flags = SVGA_SCREEN_HAS_ROOT |
133 (sou->base.unit == 0 ? SVGA_SCREEN_IS_PRIMARY : 0);
134 cmd->obj.size.width = mode->hdisplay;
135 cmd->obj.size.height = mode->vdisplay;
136 if (sou->base.is_implicit) {
137 cmd->obj.root.x = x;
138 cmd->obj.root.y = y;
139 } else {
140 cmd->obj.root.x = sou->base.gui_x;
141 cmd->obj.root.y = sou->base.gui_y;
142 }
143
144 /* Ok to assume that buffer is pinned in vram */
145 vmw_bo_get_guest_ptr(&sou->buffer->base, &cmd->obj.backingStore.ptr);
146 cmd->obj.backingStore.pitch = mode->hdisplay * 4;
147
148 vmw_fifo_commit(dev_priv, fifo_size);
149
150 sou->defined = true;
151
152 return 0;
153 }
154
155 /**
156 * Send the fifo command to destroy a screen.
157 */
vmw_sou_fifo_destroy(struct vmw_private * dev_priv,struct vmw_screen_object_unit * sou)158 static int vmw_sou_fifo_destroy(struct vmw_private *dev_priv,
159 struct vmw_screen_object_unit *sou)
160 {
161 size_t fifo_size;
162 int ret;
163
164 struct {
165 struct {
166 uint32_t cmdType;
167 } header;
168 SVGAFifoCmdDestroyScreen body;
169 } *cmd;
170
171 /* no need to do anything */
172 if (unlikely(!sou->defined))
173 return 0;
174
175 fifo_size = sizeof(*cmd);
176 cmd = vmw_fifo_reserve(dev_priv, fifo_size);
177 /* the hardware has hung, nothing we can do about it here */
178 if (unlikely(cmd == NULL)) {
179 DRM_ERROR("Fifo reserve failed.\n");
180 return -ENOMEM;
181 }
182
183 memset(cmd, 0, fifo_size);
184 cmd->header.cmdType = SVGA_CMD_DESTROY_SCREEN;
185 cmd->body.screenId = sou->base.unit;
186
187 vmw_fifo_commit(dev_priv, fifo_size);
188
189 /* Force sync */
190 ret = vmw_fallback_wait(dev_priv, false, true, 0, false, 3*HZ);
191 if (unlikely(ret != 0))
192 DRM_ERROR("Failed to sync with HW");
193 else
194 sou->defined = false;
195
196 return ret;
197 }
198
199 /**
200 * Free the backing store.
201 */
vmw_sou_backing_free(struct vmw_private * dev_priv,struct vmw_screen_object_unit * sou)202 static void vmw_sou_backing_free(struct vmw_private *dev_priv,
203 struct vmw_screen_object_unit *sou)
204 {
205 struct ttm_buffer_object *bo;
206
207 if (unlikely(sou->buffer == NULL))
208 return;
209
210 bo = &sou->buffer->base;
211 ttm_bo_unref(&bo);
212 sou->buffer = NULL;
213 sou->buffer_size = 0;
214 }
215
216 /**
217 * Allocate the backing store for the buffer.
218 */
vmw_sou_backing_alloc(struct vmw_private * dev_priv,struct vmw_screen_object_unit * sou,unsigned long size)219 static int vmw_sou_backing_alloc(struct vmw_private *dev_priv,
220 struct vmw_screen_object_unit *sou,
221 unsigned long size)
222 {
223 int ret;
224
225 if (sou->buffer_size == size)
226 return 0;
227
228 if (sou->buffer)
229 vmw_sou_backing_free(dev_priv, sou);
230
231 sou->buffer = kzalloc(sizeof(*sou->buffer), GFP_KERNEL);
232 if (unlikely(sou->buffer == NULL))
233 return -ENOMEM;
234
235 /* After we have alloced the backing store might not be able to
236 * resume the overlays, this is preferred to failing to alloc.
237 */
238 vmw_overlay_pause_all(dev_priv);
239 ret = vmw_dmabuf_init(dev_priv, sou->buffer, size,
240 &vmw_vram_ne_placement,
241 false, &vmw_dmabuf_bo_free);
242 vmw_overlay_resume_all(dev_priv);
243
244 if (unlikely(ret != 0))
245 sou->buffer = NULL; /* vmw_dmabuf_init frees on error */
246 else
247 sou->buffer_size = size;
248
249 return ret;
250 }
251
vmw_sou_crtc_set_config(struct drm_mode_set * set)252 static int vmw_sou_crtc_set_config(struct drm_mode_set *set)
253 {
254 struct vmw_private *dev_priv;
255 struct vmw_screen_object_unit *sou;
256 struct drm_connector *connector;
257 struct drm_display_mode *mode;
258 struct drm_encoder *encoder;
259 struct vmw_framebuffer *vfb;
260 struct drm_framebuffer *fb;
261 struct drm_crtc *crtc;
262 int ret = 0;
263
264 if (!set)
265 return -EINVAL;
266
267 if (!set->crtc)
268 return -EINVAL;
269
270 /* get the sou */
271 crtc = set->crtc;
272 sou = vmw_crtc_to_sou(crtc);
273 vfb = set->fb ? vmw_framebuffer_to_vfb(set->fb) : NULL;
274 dev_priv = vmw_priv(crtc->dev);
275
276 if (set->num_connectors > 1) {
277 DRM_ERROR("to many connectors\n");
278 return -EINVAL;
279 }
280
281 if (set->num_connectors == 1 &&
282 set->connectors[0] != &sou->base.connector) {
283 DRM_ERROR("connector doesn't match %p %p\n",
284 set->connectors[0], &sou->base.connector);
285 return -EINVAL;
286 }
287
288 /* sou only supports one fb active at the time */
289 if (sou->base.is_implicit &&
290 dev_priv->sou_priv->implicit_fb && vfb &&
291 !(dev_priv->sou_priv->num_implicit == 1 &&
292 sou->active_implicit) &&
293 dev_priv->sou_priv->implicit_fb != vfb) {
294 DRM_ERROR("Multiple framebuffers not supported\n");
295 return -EINVAL;
296 }
297
298 /* since they always map one to one these are safe */
299 connector = &sou->base.connector;
300 encoder = &sou->base.encoder;
301
302 /* should we turn the crtc off */
303 if (set->num_connectors == 0 || !set->mode || !set->fb) {
304 ret = vmw_sou_fifo_destroy(dev_priv, sou);
305 /* the hardware has hung don't do anything more */
306 if (unlikely(ret != 0))
307 return ret;
308
309 connector->encoder = NULL;
310 encoder->crtc = NULL;
311 crtc->primary->fb = NULL;
312 crtc->x = 0;
313 crtc->y = 0;
314 crtc->enabled = false;
315
316 vmw_sou_del_active(dev_priv, sou);
317
318 vmw_sou_backing_free(dev_priv, sou);
319
320 return 0;
321 }
322
323
324 /* we now know we want to set a mode */
325 mode = set->mode;
326 fb = set->fb;
327
328 if (set->x + mode->hdisplay > fb->width ||
329 set->y + mode->vdisplay > fb->height) {
330 DRM_ERROR("set outside of framebuffer\n");
331 return -EINVAL;
332 }
333
334 vmw_fb_off(dev_priv);
335
336 if (mode->hdisplay != crtc->mode.hdisplay ||
337 mode->vdisplay != crtc->mode.vdisplay) {
338 /* no need to check if depth is different, because backing
339 * store depth is forced to 4 by the device.
340 */
341
342 ret = vmw_sou_fifo_destroy(dev_priv, sou);
343 /* the hardware has hung don't do anything more */
344 if (unlikely(ret != 0))
345 return ret;
346
347 vmw_sou_backing_free(dev_priv, sou);
348 }
349
350 if (!sou->buffer) {
351 /* forced to depth 4 by the device */
352 size_t size = mode->hdisplay * mode->vdisplay * 4;
353 ret = vmw_sou_backing_alloc(dev_priv, sou, size);
354 if (unlikely(ret != 0))
355 return ret;
356 }
357
358 ret = vmw_sou_fifo_create(dev_priv, sou, set->x, set->y, mode);
359 if (unlikely(ret != 0)) {
360 /*
361 * We are in a bit of a situation here, the hardware has
362 * hung and we may or may not have a buffer hanging of
363 * the screen object, best thing to do is not do anything
364 * if we where defined, if not just turn the crtc of.
365 * Not what userspace wants but it needs to htfu.
366 */
367 if (sou->defined)
368 return ret;
369
370 connector->encoder = NULL;
371 encoder->crtc = NULL;
372 crtc->primary->fb = NULL;
373 crtc->x = 0;
374 crtc->y = 0;
375 crtc->enabled = false;
376
377 return ret;
378 }
379
380 vmw_sou_add_active(dev_priv, sou, vfb);
381
382 connector->encoder = encoder;
383 encoder->crtc = crtc;
384 crtc->mode = *mode;
385 crtc->primary->fb = fb;
386 crtc->x = set->x;
387 crtc->y = set->y;
388 crtc->enabled = true;
389
390 return 0;
391 }
392
393 static struct drm_crtc_funcs vmw_screen_object_crtc_funcs = {
394 .save = vmw_du_crtc_save,
395 .restore = vmw_du_crtc_restore,
396 .cursor_set = vmw_du_crtc_cursor_set,
397 .cursor_move = vmw_du_crtc_cursor_move,
398 .gamma_set = vmw_du_crtc_gamma_set,
399 .destroy = vmw_sou_crtc_destroy,
400 .set_config = vmw_sou_crtc_set_config,
401 .page_flip = vmw_du_page_flip,
402 };
403
404 /*
405 * Screen Object Display Unit encoder functions
406 */
407
vmw_sou_encoder_destroy(struct drm_encoder * encoder)408 static void vmw_sou_encoder_destroy(struct drm_encoder *encoder)
409 {
410 vmw_sou_destroy(vmw_encoder_to_sou(encoder));
411 }
412
413 static struct drm_encoder_funcs vmw_screen_object_encoder_funcs = {
414 .destroy = vmw_sou_encoder_destroy,
415 };
416
417 /*
418 * Screen Object Display Unit connector functions
419 */
420
vmw_sou_connector_destroy(struct drm_connector * connector)421 static void vmw_sou_connector_destroy(struct drm_connector *connector)
422 {
423 vmw_sou_destroy(vmw_connector_to_sou(connector));
424 }
425
426 static struct drm_connector_funcs vmw_legacy_connector_funcs = {
427 .dpms = vmw_du_connector_dpms,
428 .save = vmw_du_connector_save,
429 .restore = vmw_du_connector_restore,
430 .detect = vmw_du_connector_detect,
431 .fill_modes = vmw_du_connector_fill_modes,
432 .set_property = vmw_du_connector_set_property,
433 .destroy = vmw_sou_connector_destroy,
434 };
435
vmw_sou_init(struct vmw_private * dev_priv,unsigned unit)436 static int vmw_sou_init(struct vmw_private *dev_priv, unsigned unit)
437 {
438 struct vmw_screen_object_unit *sou;
439 struct drm_device *dev = dev_priv->dev;
440 struct drm_connector *connector;
441 struct drm_encoder *encoder;
442 struct drm_crtc *crtc;
443
444 sou = kzalloc(sizeof(*sou), GFP_KERNEL);
445 if (!sou)
446 return -ENOMEM;
447
448 sou->base.unit = unit;
449 crtc = &sou->base.crtc;
450 encoder = &sou->base.encoder;
451 connector = &sou->base.connector;
452
453 sou->active_implicit = false;
454
455 sou->base.pref_active = (unit == 0);
456 sou->base.pref_width = dev_priv->initial_width;
457 sou->base.pref_height = dev_priv->initial_height;
458 sou->base.pref_mode = NULL;
459 sou->base.is_implicit = true;
460
461 drm_connector_init(dev, connector, &vmw_legacy_connector_funcs,
462 DRM_MODE_CONNECTOR_VIRTUAL);
463 connector->status = vmw_du_connector_detect(connector, true);
464
465 drm_encoder_init(dev, encoder, &vmw_screen_object_encoder_funcs,
466 DRM_MODE_ENCODER_VIRTUAL);
467 drm_mode_connector_attach_encoder(connector, encoder);
468 encoder->possible_crtcs = (1 << unit);
469 encoder->possible_clones = 0;
470
471 (void) drm_connector_register(connector);
472
473 drm_crtc_init(dev, crtc, &vmw_screen_object_crtc_funcs);
474
475 drm_mode_crtc_set_gamma_size(crtc, 256);
476
477 drm_object_attach_property(&connector->base,
478 dev->mode_config.dirty_info_property,
479 1);
480
481 return 0;
482 }
483
vmw_kms_init_screen_object_display(struct vmw_private * dev_priv)484 int vmw_kms_init_screen_object_display(struct vmw_private *dev_priv)
485 {
486 struct drm_device *dev = dev_priv->dev;
487 int i, ret;
488
489 if (dev_priv->sou_priv) {
490 DRM_INFO("sou system already on\n");
491 return -EINVAL;
492 }
493
494 if (!(dev_priv->capabilities & SVGA_CAP_SCREEN_OBJECT_2)) {
495 DRM_INFO("Not using screen objects,"
496 " missing cap SCREEN_OBJECT_2\n");
497 return -ENOSYS;
498 }
499
500 ret = -ENOMEM;
501 dev_priv->sou_priv = kmalloc(sizeof(*dev_priv->sou_priv), GFP_KERNEL);
502 if (unlikely(!dev_priv->sou_priv))
503 goto err_no_mem;
504
505 dev_priv->sou_priv->num_implicit = 0;
506 dev_priv->sou_priv->implicit_fb = NULL;
507
508 ret = drm_vblank_init(dev, VMWGFX_NUM_DISPLAY_UNITS);
509 if (unlikely(ret != 0))
510 goto err_free;
511
512 ret = drm_mode_create_dirty_info_property(dev);
513 if (unlikely(ret != 0))
514 goto err_vblank_cleanup;
515
516 for (i = 0; i < VMWGFX_NUM_DISPLAY_UNITS; ++i)
517 vmw_sou_init(dev_priv, i);
518
519 DRM_INFO("Screen objects system initialized\n");
520
521 return 0;
522
523 err_vblank_cleanup:
524 drm_vblank_cleanup(dev);
525 err_free:
526 kfree(dev_priv->sou_priv);
527 dev_priv->sou_priv = NULL;
528 err_no_mem:
529 return ret;
530 }
531
vmw_kms_close_screen_object_display(struct vmw_private * dev_priv)532 int vmw_kms_close_screen_object_display(struct vmw_private *dev_priv)
533 {
534 struct drm_device *dev = dev_priv->dev;
535
536 if (!dev_priv->sou_priv)
537 return -ENOSYS;
538
539 drm_vblank_cleanup(dev);
540
541 kfree(dev_priv->sou_priv);
542
543 return 0;
544 }
545
546 /**
547 * Returns if this unit can be page flipped.
548 * Must be called with the mode_config mutex held.
549 */
vmw_kms_screen_object_flippable(struct vmw_private * dev_priv,struct drm_crtc * crtc)550 bool vmw_kms_screen_object_flippable(struct vmw_private *dev_priv,
551 struct drm_crtc *crtc)
552 {
553 struct vmw_screen_object_unit *sou = vmw_crtc_to_sou(crtc);
554
555 if (!sou->base.is_implicit)
556 return true;
557
558 if (dev_priv->sou_priv->num_implicit != 1)
559 return false;
560
561 return true;
562 }
563
564 /**
565 * Update the implicit fb to the current fb of this crtc.
566 * Must be called with the mode_config mutex held.
567 */
vmw_kms_screen_object_update_implicit_fb(struct vmw_private * dev_priv,struct drm_crtc * crtc)568 void vmw_kms_screen_object_update_implicit_fb(struct vmw_private *dev_priv,
569 struct drm_crtc *crtc)
570 {
571 struct vmw_screen_object_unit *sou = vmw_crtc_to_sou(crtc);
572
573 BUG_ON(!sou->base.is_implicit);
574
575 dev_priv->sou_priv->implicit_fb =
576 vmw_framebuffer_to_vfb(sou->base.crtc.primary->fb);
577 }
578