1 /**************************************************************************
3 * Copyright © 2011 VMware, Inc., Palo Alto, CA., USA
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 #include "vmwgfx_kms.h"
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)
38 struct vmw_screen_object_display {
39 struct list_head active;
42 unsigned last_num_active;
44 struct vmw_framebuffer *fb;
48 * Display unit using screen objects.
50 struct vmw_screen_object_unit {
51 struct vmw_display_unit base;
53 unsigned long buffer_size; /**< Size of allocated buffer */
54 struct vmw_dma_buffer *buffer; /**< Backing store buffer */
58 struct list_head active;
61 static void vmw_sou_destroy(struct vmw_screen_object_unit *sou)
63 list_del_init(&sou->active);
64 vmw_display_unit_cleanup(&sou->base);
70 * Screen Object Display Unit CRTC functions
73 static void vmw_sou_crtc_destroy(struct drm_crtc *crtc)
75 vmw_sou_destroy(vmw_crtc_to_sou(crtc));
78 static int vmw_sou_del_active(struct vmw_private *vmw_priv,
79 struct vmw_screen_object_unit *sou)
81 struct vmw_screen_object_display *ld = vmw_priv->sou_priv;
82 if (list_empty(&sou->active))
85 /* Must init otherwise list_empty(&sou->active) will not work. */
86 list_del_init(&sou->active);
87 if (--(ld->num_active) == 0) {
90 ld->fb->unpin(ld->fb);
97 static int vmw_sou_add_active(struct vmw_private *vmw_priv,
98 struct vmw_screen_object_unit *sou,
99 struct vmw_framebuffer *vfb)
101 struct vmw_screen_object_display *ld = vmw_priv->sou_priv;
102 struct vmw_screen_object_unit *entry;
103 struct list_head *at;
105 BUG_ON(!ld->num_active && ld->fb);
107 if (ld->fb && ld->fb->unpin)
108 ld->fb->unpin(ld->fb);
114 if (!list_empty(&sou->active))
118 list_for_each_entry(entry, &ld->active, active) {
119 if (entry->base.unit > sou->base.unit)
125 list_add(&sou->active, at);
133 * Send the fifo command to create a screen.
135 static int vmw_sou_fifo_create(struct vmw_private *dev_priv,
136 struct vmw_screen_object_unit *sou,
137 uint32_t x, uint32_t y,
138 struct drm_display_mode *mode)
146 SVGAScreenObject obj;
149 BUG_ON(!sou->buffer);
151 fifo_size = sizeof(*cmd);
152 cmd = vmw_fifo_reserve(dev_priv, fifo_size);
153 /* The hardware has hung, nothing we can do about it here. */
154 if (unlikely(cmd == NULL)) {
155 DRM_ERROR("Fifo reserve failed.\n");
159 memset(cmd, 0, fifo_size);
160 cmd->header.cmdType = SVGA_CMD_DEFINE_SCREEN;
161 cmd->obj.structSize = sizeof(SVGAScreenObject);
162 cmd->obj.id = sou->base.unit;
163 cmd->obj.flags = SVGA_SCREEN_HAS_ROOT |
164 (sou->base.unit == 0 ? SVGA_SCREEN_IS_PRIMARY : 0);
165 cmd->obj.size.width = mode->hdisplay;
166 cmd->obj.size.height = mode->vdisplay;
170 /* Ok to assume that buffer is pinned in vram */
171 vmw_bo_get_guest_ptr(&sou->buffer->base, &cmd->obj.backingStore.ptr);
172 cmd->obj.backingStore.pitch = mode->hdisplay * 4;
174 vmw_fifo_commit(dev_priv, fifo_size);
182 * Send the fifo command to destroy a screen.
184 static int vmw_sou_fifo_destroy(struct vmw_private *dev_priv,
185 struct vmw_screen_object_unit *sou)
194 SVGAFifoCmdDestroyScreen body;
197 /* no need to do anything */
198 if (unlikely(!sou->defined))
201 fifo_size = sizeof(*cmd);
202 cmd = vmw_fifo_reserve(dev_priv, fifo_size);
203 /* the hardware has hung, nothing we can do about it here */
204 if (unlikely(cmd == NULL)) {
205 DRM_ERROR("Fifo reserve failed.\n");
209 memset(cmd, 0, fifo_size);
210 cmd->header.cmdType = SVGA_CMD_DESTROY_SCREEN;
211 cmd->body.screenId = sou->base.unit;
213 vmw_fifo_commit(dev_priv, fifo_size);
216 ret = vmw_fallback_wait(dev_priv, false, true, 0, false, 3*HZ);
217 if (unlikely(ret != 0))
218 DRM_ERROR("Failed to sync with HW");
220 sou->defined = false;
226 * Free the backing store.
228 static void vmw_sou_backing_free(struct vmw_private *dev_priv,
229 struct vmw_screen_object_unit *sou)
231 struct ttm_buffer_object *bo;
233 if (unlikely(sou->buffer == NULL))
236 bo = &sou->buffer->base;
239 sou->buffer_size = 0;
243 * Allocate the backing store for the buffer.
245 static int vmw_sou_backing_alloc(struct vmw_private *dev_priv,
246 struct vmw_screen_object_unit *sou,
251 if (sou->buffer_size == size)
255 vmw_sou_backing_free(dev_priv, sou);
257 sou->buffer = kzalloc(sizeof(*sou->buffer), GFP_KERNEL);
258 if (unlikely(sou->buffer == NULL))
261 /* After we have alloced the backing store might not be able to
262 * resume the overlays, this is preferred to failing to alloc.
264 vmw_overlay_pause_all(dev_priv);
265 ret = vmw_dmabuf_init(dev_priv, sou->buffer, size,
266 &vmw_vram_ne_placement,
267 false, &vmw_dmabuf_bo_free);
268 vmw_overlay_resume_all(dev_priv);
270 if (unlikely(ret != 0))
271 sou->buffer = NULL; /* vmw_dmabuf_init frees on error */
273 sou->buffer_size = size;
278 static int vmw_sou_crtc_set_config(struct drm_mode_set *set)
280 struct vmw_private *dev_priv;
281 struct vmw_screen_object_unit *sou;
282 struct drm_connector *connector;
283 struct drm_display_mode *mode;
284 struct drm_encoder *encoder;
285 struct vmw_framebuffer *vfb;
286 struct drm_framebuffer *fb;
287 struct drm_crtc *crtc;
298 sou = vmw_crtc_to_sou(crtc);
299 vfb = set->fb ? vmw_framebuffer_to_vfb(set->fb) : NULL;
300 dev_priv = vmw_priv(crtc->dev);
302 if (set->num_connectors > 1) {
303 DRM_ERROR("to many connectors\n");
307 if (set->num_connectors == 1 &&
308 set->connectors[0] != &sou->base.connector) {
309 DRM_ERROR("connector doesn't match %p %p\n",
310 set->connectors[0], &sou->base.connector);
314 /* sou only supports one fb active at the time */
315 if (dev_priv->sou_priv->fb && vfb &&
316 !(dev_priv->sou_priv->num_active == 1 &&
317 !list_empty(&sou->active)) &&
318 dev_priv->sou_priv->fb != vfb) {
319 DRM_ERROR("Multiple framebuffers not supported\n");
323 /* since they always map one to one these are safe */
324 connector = &sou->base.connector;
325 encoder = &sou->base.encoder;
327 /* should we turn the crtc off */
328 if (set->num_connectors == 0 || !set->mode || !set->fb) {
329 ret = vmw_sou_fifo_destroy(dev_priv, sou);
330 /* the hardware has hung don't do anything more */
331 if (unlikely(ret != 0))
334 connector->encoder = NULL;
335 encoder->crtc = NULL;
340 vmw_sou_del_active(dev_priv, sou);
342 vmw_sou_backing_free(dev_priv, sou);
348 /* we now know we want to set a mode */
352 if (set->x + mode->hdisplay > fb->width ||
353 set->y + mode->vdisplay > fb->height) {
354 DRM_ERROR("set outside of framebuffer\n");
358 vmw_fb_off(dev_priv);
360 if (mode->hdisplay != crtc->mode.hdisplay ||
361 mode->vdisplay != crtc->mode.vdisplay) {
362 /* no need to check if depth is different, because backing
363 * store depth is forced to 4 by the device.
366 ret = vmw_sou_fifo_destroy(dev_priv, sou);
367 /* the hardware has hung don't do anything more */
368 if (unlikely(ret != 0))
371 vmw_sou_backing_free(dev_priv, sou);
375 /* forced to depth 4 by the device */
376 size_t size = mode->hdisplay * mode->vdisplay * 4;
377 ret = vmw_sou_backing_alloc(dev_priv, sou, size);
378 if (unlikely(ret != 0))
382 ret = vmw_sou_fifo_create(dev_priv, sou, set->x, set->y, mode);
383 if (unlikely(ret != 0)) {
385 * We are in a bit of a situation here, the hardware has
386 * hung and we may or may not have a buffer hanging of
387 * the screen object, best thing to do is not do anything
388 * if we where defined, if not just turn the crtc of.
389 * Not what userspace wants but it needs to htfu.
394 connector->encoder = NULL;
395 encoder->crtc = NULL;
403 vmw_sou_add_active(dev_priv, sou, vfb);
405 connector->encoder = encoder;
406 encoder->crtc = crtc;
415 static struct drm_crtc_funcs vmw_screen_object_crtc_funcs = {
416 .save = vmw_du_crtc_save,
417 .restore = vmw_du_crtc_restore,
418 .cursor_set = vmw_du_crtc_cursor_set,
419 .cursor_move = vmw_du_crtc_cursor_move,
420 .gamma_set = vmw_du_crtc_gamma_set,
421 .destroy = vmw_sou_crtc_destroy,
422 .set_config = vmw_sou_crtc_set_config,
426 * Screen Object Display Unit encoder functions
429 static void vmw_sou_encoder_destroy(struct drm_encoder *encoder)
431 vmw_sou_destroy(vmw_encoder_to_sou(encoder));
434 static struct drm_encoder_funcs vmw_screen_object_encoder_funcs = {
435 .destroy = vmw_sou_encoder_destroy,
439 * Screen Object Display Unit connector functions
442 static void vmw_sou_connector_destroy(struct drm_connector *connector)
444 vmw_sou_destroy(vmw_connector_to_sou(connector));
447 static struct drm_connector_funcs vmw_legacy_connector_funcs = {
448 .dpms = vmw_du_connector_dpms,
449 .save = vmw_du_connector_save,
450 .restore = vmw_du_connector_restore,
451 .detect = vmw_du_connector_detect,
452 .fill_modes = vmw_du_connector_fill_modes,
453 .set_property = vmw_du_connector_set_property,
454 .destroy = vmw_sou_connector_destroy,
457 static int vmw_sou_init(struct vmw_private *dev_priv, unsigned unit)
459 struct vmw_screen_object_unit *sou;
460 struct drm_device *dev = dev_priv->dev;
461 struct drm_connector *connector;
462 struct drm_encoder *encoder;
463 struct drm_crtc *crtc;
465 sou = kzalloc(sizeof(*sou), GFP_KERNEL);
469 sou->base.unit = unit;
470 crtc = &sou->base.crtc;
471 encoder = &sou->base.encoder;
472 connector = &sou->base.connector;
474 INIT_LIST_HEAD(&sou->active);
476 sou->base.pref_active = (unit == 0);
477 sou->base.pref_width = 800;
478 sou->base.pref_height = 600;
479 sou->base.pref_mode = NULL;
481 drm_connector_init(dev, connector, &vmw_legacy_connector_funcs,
482 DRM_MODE_CONNECTOR_LVDS);
483 connector->status = vmw_du_connector_detect(connector, true);
485 drm_encoder_init(dev, encoder, &vmw_screen_object_encoder_funcs,
486 DRM_MODE_ENCODER_LVDS);
487 drm_mode_connector_attach_encoder(connector, encoder);
488 encoder->possible_crtcs = (1 << unit);
489 encoder->possible_clones = 0;
491 drm_crtc_init(dev, crtc, &vmw_screen_object_crtc_funcs);
493 drm_mode_crtc_set_gamma_size(crtc, 256);
495 drm_connector_attach_property(connector,
496 dev->mode_config.dirty_info_property,
502 int vmw_kms_init_screen_object_display(struct vmw_private *dev_priv)
504 struct drm_device *dev = dev_priv->dev;
507 if (dev_priv->sou_priv) {
508 DRM_INFO("sou system already on\n");
512 if (!(dev_priv->fifo.capabilities & SVGA_FIFO_CAP_SCREEN_OBJECT_2)) {
513 DRM_INFO("Not using screen objects,"
514 " missing cap SCREEN_OBJECT_2\n");
519 dev_priv->sou_priv = kmalloc(sizeof(*dev_priv->sou_priv), GFP_KERNEL);
520 if (unlikely(!dev_priv->sou_priv))
523 INIT_LIST_HEAD(&dev_priv->sou_priv->active);
524 dev_priv->sou_priv->num_active = 0;
525 dev_priv->sou_priv->last_num_active = 0;
526 dev_priv->sou_priv->fb = NULL;
528 ret = drm_vblank_init(dev, VMWGFX_NUM_DISPLAY_UNITS);
529 if (unlikely(ret != 0))
532 ret = drm_mode_create_dirty_info_property(dev);
533 if (unlikely(ret != 0))
534 goto err_vblank_cleanup;
536 for (i = 0; i < VMWGFX_NUM_DISPLAY_UNITS; ++i)
537 vmw_sou_init(dev_priv, i);
539 DRM_INFO("Screen objects system initialized\n");
544 drm_vblank_cleanup(dev);
546 kfree(dev_priv->sou_priv);
547 dev_priv->sou_priv = NULL;
552 int vmw_kms_close_screen_object_display(struct vmw_private *dev_priv)
554 struct drm_device *dev = dev_priv->dev;
556 if (!dev_priv->sou_priv)
559 drm_vblank_cleanup(dev);
561 if (!list_empty(&dev_priv->sou_priv->active))
562 DRM_ERROR("Still have active outputs when unloading driver");
564 kfree(dev_priv->sou_priv);