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"
29 #include <drm/drm_plane_helper.h>
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)
39 struct vmw_screen_object_display
{
40 unsigned num_implicit
;
42 struct vmw_framebuffer
*implicit_fb
;
46 * Display unit using screen objects.
48 struct vmw_screen_object_unit
{
49 struct vmw_display_unit base
;
51 unsigned long buffer_size
; /**< Size of allocated buffer */
52 struct vmw_dma_buffer
*buffer
; /**< Backing store buffer */
58 static void vmw_sou_destroy(struct vmw_screen_object_unit
*sou
)
60 vmw_display_unit_cleanup(&sou
->base
);
66 * Screen Object Display Unit CRTC functions
69 static void vmw_sou_crtc_destroy(struct drm_crtc
*crtc
)
71 vmw_sou_destroy(vmw_crtc_to_sou(crtc
));
74 static void vmw_sou_del_active(struct vmw_private
*vmw_priv
,
75 struct vmw_screen_object_unit
*sou
)
77 struct vmw_screen_object_display
*ld
= vmw_priv
->sou_priv
;
79 if (sou
->active_implicit
) {
80 if (--(ld
->num_implicit
) == 0)
81 ld
->implicit_fb
= NULL
;
82 sou
->active_implicit
= false;
86 static void vmw_sou_add_active(struct vmw_private
*vmw_priv
,
87 struct vmw_screen_object_unit
*sou
,
88 struct vmw_framebuffer
*vfb
)
90 struct vmw_screen_object_display
*ld
= vmw_priv
->sou_priv
;
92 BUG_ON(!ld
->num_implicit
&& ld
->implicit_fb
);
94 if (!sou
->active_implicit
&& sou
->base
.is_implicit
) {
95 ld
->implicit_fb
= vfb
;
96 sou
->active_implicit
= true;
102 * Send the fifo command to create a screen.
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
)
115 SVGAScreenObject obj
;
118 BUG_ON(!sou
->buffer
);
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");
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
) {
140 cmd
->obj
.root
.x
= sou
->base
.gui_x
;
141 cmd
->obj
.root
.y
= sou
->base
.gui_y
;
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;
148 vmw_fifo_commit(dev_priv
, fifo_size
);
156 * Send the fifo command to destroy a screen.
158 static int vmw_sou_fifo_destroy(struct vmw_private
*dev_priv
,
159 struct vmw_screen_object_unit
*sou
)
168 SVGAFifoCmdDestroyScreen body
;
171 /* no need to do anything */
172 if (unlikely(!sou
->defined
))
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");
183 memset(cmd
, 0, fifo_size
);
184 cmd
->header
.cmdType
= SVGA_CMD_DESTROY_SCREEN
;
185 cmd
->body
.screenId
= sou
->base
.unit
;
187 vmw_fifo_commit(dev_priv
, fifo_size
);
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");
194 sou
->defined
= false;
200 * Free the backing store.
202 static void vmw_sou_backing_free(struct vmw_private
*dev_priv
,
203 struct vmw_screen_object_unit
*sou
)
205 struct ttm_buffer_object
*bo
;
207 if (unlikely(sou
->buffer
== NULL
))
210 bo
= &sou
->buffer
->base
;
213 sou
->buffer_size
= 0;
217 * Allocate the backing store for the buffer.
219 static int vmw_sou_backing_alloc(struct vmw_private
*dev_priv
,
220 struct vmw_screen_object_unit
*sou
,
225 if (sou
->buffer_size
== size
)
229 vmw_sou_backing_free(dev_priv
, sou
);
231 sou
->buffer
= kzalloc(sizeof(*sou
->buffer
), GFP_KERNEL
);
232 if (unlikely(sou
->buffer
== NULL
))
235 /* After we have alloced the backing store might not be able to
236 * resume the overlays, this is preferred to failing to alloc.
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
);
244 if (unlikely(ret
!= 0))
245 sou
->buffer
= NULL
; /* vmw_dmabuf_init frees on error */
247 sou
->buffer_size
= size
;
252 static int vmw_sou_crtc_set_config(struct drm_mode_set
*set
)
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
;
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
);
276 if (set
->num_connectors
> 1) {
277 DRM_ERROR("to many connectors\n");
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
);
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");
298 /* since they always map one to one these are safe */
299 connector
= &sou
->base
.connector
;
300 encoder
= &sou
->base
.encoder
;
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))
309 connector
->encoder
= NULL
;
310 encoder
->crtc
= NULL
;
311 crtc
->primary
->fb
= NULL
;
314 crtc
->enabled
= false;
316 vmw_sou_del_active(dev_priv
, sou
);
318 vmw_sou_backing_free(dev_priv
, sou
);
324 /* we now know we want to set a mode */
328 if (set
->x
+ mode
->hdisplay
> fb
->width
||
329 set
->y
+ mode
->vdisplay
> fb
->height
) {
330 DRM_ERROR("set outside of framebuffer\n");
334 vmw_fb_off(dev_priv
);
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.
342 ret
= vmw_sou_fifo_destroy(dev_priv
, sou
);
343 /* the hardware has hung don't do anything more */
344 if (unlikely(ret
!= 0))
347 vmw_sou_backing_free(dev_priv
, sou
);
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))
358 ret
= vmw_sou_fifo_create(dev_priv
, sou
, set
->x
, set
->y
, mode
);
359 if (unlikely(ret
!= 0)) {
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.
370 connector
->encoder
= NULL
;
371 encoder
->crtc
= NULL
;
372 crtc
->primary
->fb
= NULL
;
375 crtc
->enabled
= false;
380 vmw_sou_add_active(dev_priv
, sou
, vfb
);
382 connector
->encoder
= encoder
;
383 encoder
->crtc
= crtc
;
385 crtc
->primary
->fb
= fb
;
388 crtc
->enabled
= true;
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
,
405 * Screen Object Display Unit encoder functions
408 static void vmw_sou_encoder_destroy(struct drm_encoder
*encoder
)
410 vmw_sou_destroy(vmw_encoder_to_sou(encoder
));
413 static struct drm_encoder_funcs vmw_screen_object_encoder_funcs
= {
414 .destroy
= vmw_sou_encoder_destroy
,
418 * Screen Object Display Unit connector functions
421 static void vmw_sou_connector_destroy(struct drm_connector
*connector
)
423 vmw_sou_destroy(vmw_connector_to_sou(connector
));
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
,
436 static int vmw_sou_init(struct vmw_private
*dev_priv
, unsigned unit
)
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
;
444 sou
= kzalloc(sizeof(*sou
), GFP_KERNEL
);
448 sou
->base
.unit
= unit
;
449 crtc
= &sou
->base
.crtc
;
450 encoder
= &sou
->base
.encoder
;
451 connector
= &sou
->base
.connector
;
453 sou
->active_implicit
= false;
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;
461 drm_connector_init(dev
, connector
, &vmw_legacy_connector_funcs
,
462 DRM_MODE_CONNECTOR_VIRTUAL
);
463 connector
->status
= vmw_du_connector_detect(connector
, true);
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;
471 (void) drm_connector_register(connector
);
473 drm_crtc_init(dev
, crtc
, &vmw_screen_object_crtc_funcs
);
475 drm_mode_crtc_set_gamma_size(crtc
, 256);
477 drm_object_attach_property(&connector
->base
,
478 dev
->mode_config
.dirty_info_property
,
484 int vmw_kms_init_screen_object_display(struct vmw_private
*dev_priv
)
486 struct drm_device
*dev
= dev_priv
->dev
;
489 if (dev_priv
->sou_priv
) {
490 DRM_INFO("sou system already on\n");
494 if (!(dev_priv
->capabilities
& SVGA_CAP_SCREEN_OBJECT_2
)) {
495 DRM_INFO("Not using screen objects,"
496 " missing cap SCREEN_OBJECT_2\n");
501 dev_priv
->sou_priv
= kmalloc(sizeof(*dev_priv
->sou_priv
), GFP_KERNEL
);
502 if (unlikely(!dev_priv
->sou_priv
))
505 dev_priv
->sou_priv
->num_implicit
= 0;
506 dev_priv
->sou_priv
->implicit_fb
= NULL
;
508 ret
= drm_vblank_init(dev
, VMWGFX_NUM_DISPLAY_UNITS
);
509 if (unlikely(ret
!= 0))
512 ret
= drm_mode_create_dirty_info_property(dev
);
513 if (unlikely(ret
!= 0))
514 goto err_vblank_cleanup
;
516 for (i
= 0; i
< VMWGFX_NUM_DISPLAY_UNITS
; ++i
)
517 vmw_sou_init(dev_priv
, i
);
519 DRM_INFO("Screen objects system initialized\n");
524 drm_vblank_cleanup(dev
);
526 kfree(dev_priv
->sou_priv
);
527 dev_priv
->sou_priv
= NULL
;
532 int vmw_kms_close_screen_object_display(struct vmw_private
*dev_priv
)
534 struct drm_device
*dev
= dev_priv
->dev
;
536 if (!dev_priv
->sou_priv
)
539 drm_vblank_cleanup(dev
);
541 kfree(dev_priv
->sou_priv
);
547 * Returns if this unit can be page flipped.
548 * Must be called with the mode_config mutex held.
550 bool vmw_kms_screen_object_flippable(struct vmw_private
*dev_priv
,
551 struct drm_crtc
*crtc
)
553 struct vmw_screen_object_unit
*sou
= vmw_crtc_to_sou(crtc
);
555 if (!sou
->base
.is_implicit
)
558 if (dev_priv
->sou_priv
->num_implicit
!= 1)
565 * Update the implicit fb to the current fb of this crtc.
566 * Must be called with the mode_config mutex held.
568 void vmw_kms_screen_object_update_implicit_fb(struct vmw_private
*dev_priv
,
569 struct drm_crtc
*crtc
)
571 struct vmw_screen_object_unit
*sou
= vmw_crtc_to_sou(crtc
);
573 BUG_ON(!sou
->base
.is_implicit
);
575 dev_priv
->sou_priv
->implicit_fb
=
576 vmw_framebuffer_to_vfb(sou
->base
.crtc
.primary
->fb
);