Re: [PATCH 03/11] drm/vmwgfx: Plane atomic state

2017-03-28 Thread Daniel Vetter
On Mon, Mar 27, 2017 at 03:00:56PM -0700, Sinclair Yeh wrote:
> Add plane state handling functions.
> 
> We have to keep track of a few plane states so we cannot use the
> DRM helper for this.
> 
> Created vmw_plane_state along with functions to reset, duplicate,
> and destroty it.
> 
> Signed-off-by: Sinclair Yeh 
> Reviewed-by: Thomas Hellstrom 
> ---
>  drivers/gpu/drm/vmwgfx/vmwgfx_kms.c  | 99 
> 
>  drivers/gpu/drm/vmwgfx/vmwgfx_kms.h  | 24 +
>  drivers/gpu/drm/vmwgfx/vmwgfx_ldu.c  | 13 +
>  drivers/gpu/drm/vmwgfx/vmwgfx_scrn.c | 13 +
>  drivers/gpu/drm/vmwgfx/vmwgfx_stdu.c | 10 
>  5 files changed, 159 insertions(+)
> 
> diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_kms.c 
> b/drivers/gpu/drm/vmwgfx/vmwgfx_kms.c
> index 18bd8dc..d2171d9 100644
> --- a/drivers/gpu/drm/vmwgfx/vmwgfx_kms.c
> +++ b/drivers/gpu/drm/vmwgfx/vmwgfx_kms.c
> @@ -35,6 +35,15 @@
>  
>  void vmw_du_cleanup(struct vmw_display_unit *du)
>  {
> + if (du->cursor.state && du->cursor.state->fb) {
> + /*
> +  * On a layout change, the user mode doesn't call
> +  * drm_mode_cursor_ioctl() to release the cursor, so
> +  * we need to manualy release a reference of it.
> +  */
> + drm_framebuffer_unreference(du->cursor.state->fb);
> + }

This looks funny. drm_plane_cleanup correctly destroys the attached and
hence should take care of unrefing the plane. If you really need this,
something did go wrong somewhere ...
-Daniel

> +
>   drm_plane_cleanup(>primary);
>   drm_plane_cleanup(>cursor);
>  
> @@ -468,6 +477,96 @@ vmw_du_crtc_destroy_state(struct drm_crtc *crtc,
>  }
>  
>  
> +/**
> + * vmw_du_plane_duplicate_state - duplicate plane state
> + * @plane: drm plane
> + *
> + * Allocates and returns a copy of the plane state (both common and
> + * vmw-specific) for the specified plane.
> + *
> + * Returns: The newly allocated plane state, or NULL on failure.
> + */
> +struct drm_plane_state *
> +vmw_du_plane_duplicate_state(struct drm_plane *plane)
> +{
> + struct drm_plane_state *state;
> + struct vmw_plane_state *vps;
> +
> + vps = kmemdup(plane->state, sizeof(*vps), GFP_KERNEL);
> +
> + if (!vps)
> + return NULL;
> +
> + vps->pinned = 0;
> +
> + /* Each ref counted resource needs to be acquired again */
> + if (vps->surf)
> + (void) vmw_surface_reference(vps->surf);
> +
> + if (vps->dmabuf)
> + (void) vmw_dmabuf_reference(vps->dmabuf);
> +
> + state = >base;
> +
> + __drm_atomic_helper_plane_duplicate_state(plane, state);
> +
> + return state;
> +}
> +
> +
> +/**
> + * vmw_du_plane_reset - creates a blank vmw plane state
> + * @plane: drm plane
> + *
> + * Resets the atomic state for @plane by freeing the state pointer (which 
> might
> + * be NULL, e.g. at driver load time) and allocating a new empty state 
> object.
> + */
> +void vmw_du_plane_reset(struct drm_plane *plane)
> +{
> + struct vmw_plane_state *vps;
> +
> +
> + if (plane->state)
> + vmw_du_plane_destroy_state(plane, plane->state);
> +
> + vps = kzalloc(sizeof(*vps), GFP_KERNEL);
> +
> + if (!vps) {
> + DRM_ERROR("Cannot allocate vmw_plane_state\n");
> + return;
> + }
> +
> + plane->state = >base;
> + plane->state->plane = plane;
> + plane->state->rotation = DRM_ROTATE_0;
> +}
> +
> +
> +/**
> + * vmw_du_plane_destroy_state - destroy plane state
> + * @plane: DRM plane
> + * @state: state object to destroy
> + *
> + * Destroys the plane state (both common and vmw-specific) for the
> + * specified plane.
> + */
> +void
> +vmw_du_plane_destroy_state(struct drm_plane *plane,
> +struct drm_plane_state *state)
> +{
> + struct vmw_plane_state *vps = vmw_plane_state_to_vps(state);
> +
> +
> + if (vps->surf)
> + vmw_surface_unreference(>surf);
> +
> + if (vps->dmabuf)
> + vmw_dmabuf_unreference(>dmabuf);
> +
> + drm_atomic_helper_plane_destroy_state(plane, state);
> +}
> +
> +
>  /*
>   * Generic framebuffer code
>   */
> diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_kms.h 
> b/drivers/gpu/drm/vmwgfx/vmwgfx_kms.h
> index 370f75c..5602c24 100644
> --- a/drivers/gpu/drm/vmwgfx/vmwgfx_kms.h
> +++ b/drivers/gpu/drm/vmwgfx/vmwgfx_kms.h
> @@ -141,6 +141,7 @@ static const uint32_t vmw_cursor_plane_formats[] = {
>  
>  
>  #define vmw_crtc_state_to_vcs(x) container_of(x, struct vmw_crtc_state, base)
> +#define vmw_plane_state_to_vps(x) container_of(x, struct vmw_plane_state, 
> base)
>  
>  
>  /**
> @@ -153,6 +154,25 @@ struct vmw_crtc_state {
>  };
>  
>  /**
> + * Derived class for plane state object
> + *
> + * @base DRM plane object
> + * @surf Display surface for STDU
> + * @dmabuf display dmabuf for SOU
> + * @content_fb_type Used by STDU.
> + * @pinned pin count for STDU display surface
> + */
> 

[PATCH 03/11] drm/vmwgfx: Plane atomic state

2017-03-27 Thread Sinclair Yeh
Add plane state handling functions.

We have to keep track of a few plane states so we cannot use the
DRM helper for this.

Created vmw_plane_state along with functions to reset, duplicate,
and destroty it.

Signed-off-by: Sinclair Yeh 
Reviewed-by: Thomas Hellstrom 
---
 drivers/gpu/drm/vmwgfx/vmwgfx_kms.c  | 99 
 drivers/gpu/drm/vmwgfx/vmwgfx_kms.h  | 24 +
 drivers/gpu/drm/vmwgfx/vmwgfx_ldu.c  | 13 +
 drivers/gpu/drm/vmwgfx/vmwgfx_scrn.c | 13 +
 drivers/gpu/drm/vmwgfx/vmwgfx_stdu.c | 10 
 5 files changed, 159 insertions(+)

diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_kms.c 
b/drivers/gpu/drm/vmwgfx/vmwgfx_kms.c
index 18bd8dc..d2171d9 100644
--- a/drivers/gpu/drm/vmwgfx/vmwgfx_kms.c
+++ b/drivers/gpu/drm/vmwgfx/vmwgfx_kms.c
@@ -35,6 +35,15 @@
 
 void vmw_du_cleanup(struct vmw_display_unit *du)
 {
+   if (du->cursor.state && du->cursor.state->fb) {
+   /*
+* On a layout change, the user mode doesn't call
+* drm_mode_cursor_ioctl() to release the cursor, so
+* we need to manualy release a reference of it.
+*/
+   drm_framebuffer_unreference(du->cursor.state->fb);
+   }
+
drm_plane_cleanup(>primary);
drm_plane_cleanup(>cursor);
 
@@ -468,6 +477,96 @@ vmw_du_crtc_destroy_state(struct drm_crtc *crtc,
 }
 
 
+/**
+ * vmw_du_plane_duplicate_state - duplicate plane state
+ * @plane: drm plane
+ *
+ * Allocates and returns a copy of the plane state (both common and
+ * vmw-specific) for the specified plane.
+ *
+ * Returns: The newly allocated plane state, or NULL on failure.
+ */
+struct drm_plane_state *
+vmw_du_plane_duplicate_state(struct drm_plane *plane)
+{
+   struct drm_plane_state *state;
+   struct vmw_plane_state *vps;
+
+   vps = kmemdup(plane->state, sizeof(*vps), GFP_KERNEL);
+
+   if (!vps)
+   return NULL;
+
+   vps->pinned = 0;
+
+   /* Each ref counted resource needs to be acquired again */
+   if (vps->surf)
+   (void) vmw_surface_reference(vps->surf);
+
+   if (vps->dmabuf)
+   (void) vmw_dmabuf_reference(vps->dmabuf);
+
+   state = >base;
+
+   __drm_atomic_helper_plane_duplicate_state(plane, state);
+
+   return state;
+}
+
+
+/**
+ * vmw_du_plane_reset - creates a blank vmw plane state
+ * @plane: drm plane
+ *
+ * Resets the atomic state for @plane by freeing the state pointer (which might
+ * be NULL, e.g. at driver load time) and allocating a new empty state object.
+ */
+void vmw_du_plane_reset(struct drm_plane *plane)
+{
+   struct vmw_plane_state *vps;
+
+
+   if (plane->state)
+   vmw_du_plane_destroy_state(plane, plane->state);
+
+   vps = kzalloc(sizeof(*vps), GFP_KERNEL);
+
+   if (!vps) {
+   DRM_ERROR("Cannot allocate vmw_plane_state\n");
+   return;
+   }
+
+   plane->state = >base;
+   plane->state->plane = plane;
+   plane->state->rotation = DRM_ROTATE_0;
+}
+
+
+/**
+ * vmw_du_plane_destroy_state - destroy plane state
+ * @plane: DRM plane
+ * @state: state object to destroy
+ *
+ * Destroys the plane state (both common and vmw-specific) for the
+ * specified plane.
+ */
+void
+vmw_du_plane_destroy_state(struct drm_plane *plane,
+  struct drm_plane_state *state)
+{
+   struct vmw_plane_state *vps = vmw_plane_state_to_vps(state);
+
+
+   if (vps->surf)
+   vmw_surface_unreference(>surf);
+
+   if (vps->dmabuf)
+   vmw_dmabuf_unreference(>dmabuf);
+
+   drm_atomic_helper_plane_destroy_state(plane, state);
+}
+
+
 /*
  * Generic framebuffer code
  */
diff --git a/drivers/gpu/drm/vmwgfx/vmwgfx_kms.h 
b/drivers/gpu/drm/vmwgfx/vmwgfx_kms.h
index 370f75c..5602c24 100644
--- a/drivers/gpu/drm/vmwgfx/vmwgfx_kms.h
+++ b/drivers/gpu/drm/vmwgfx/vmwgfx_kms.h
@@ -141,6 +141,7 @@ static const uint32_t vmw_cursor_plane_formats[] = {
 
 
 #define vmw_crtc_state_to_vcs(x) container_of(x, struct vmw_crtc_state, base)
+#define vmw_plane_state_to_vps(x) container_of(x, struct vmw_plane_state, base)
 
 
 /**
@@ -153,6 +154,25 @@ struct vmw_crtc_state {
 };
 
 /**
+ * Derived class for plane state object
+ *
+ * @base DRM plane object
+ * @surf Display surface for STDU
+ * @dmabuf display dmabuf for SOU
+ * @content_fb_type Used by STDU.
+ * @pinned pin count for STDU display surface
+ */
+struct vmw_plane_state {
+   struct drm_plane_state base;
+   struct vmw_surface *surf;
+   struct vmw_dma_buffer *dmabuf;
+
+   int content_fb_type;
+
+   int pinned;
+};
+
+/**
  * Base class display unit.
  *
  * Since the SVGA hw doesn't have a concept of a crtc, encoder or connector
@@ -298,6 +318,10 @@ int vmw_du_cursor_plane_update(struct drm_plane *plane,
   uint32_t src_x, uint32_t src_y,
   uint32_t src_w, uint32_t