From 6b9c4b8d95818f7bf38697d69c4deee776044ec4 Mon Sep 17 00:00:00 2001
From: Thomas Zimmermann <tzimmermann@suse.de>
Date: Thu, 14 Nov 2019 15:10:23 +0100
Subject: drm/udl: Store active framebuffer in device structure
Git-commit: ba59b015a0145330e2bf7ab20d504bc624fd618a
Patch-mainline: v5.6-rc1
References: jsc#SLE-12680, jsc#SLE-12880, jsc#SLE-12882, jsc#SLE-12883, jsc#SLE-13496, jsc#SLE-15322

The framebuffer's 'active_16' flag signals which framebuffer to flush
to device memory. Moving the 'active_16' state from struct udl_framebuffer
into struct udl_device prepares for using the generic GEM framebuffer.

Signed-off-by: Thomas Zimmermann <tzimmermann@suse.de>
Acked-by: Sam Ravnborg <sam@ravnborg.org>
Link: https://patchwork.freedesktop.org/patch/msgid/20191114141025.32198-4-tzimmermann@suse.de
Signed-off-by: Patrik Jakobsson <pjakobsson@suse.de>
---
 drivers/gpu/drm/udl/udl_drv.h     |  5 ++++-
 drivers/gpu/drm/udl/udl_fb.c      | 13 +++++++++++--
 drivers/gpu/drm/udl/udl_main.c    |  3 +++
 drivers/gpu/drm/udl/udl_modeset.c | 18 +++++++-----------
 4 files changed, 25 insertions(+), 14 deletions(-)

diff --git a/drivers/gpu/drm/udl/udl_drv.h b/drivers/gpu/drm/udl/udl_drv.h
index be585e3e572d..a0946b955739 100644
--- a/drivers/gpu/drm/udl/udl_drv.h
+++ b/drivers/gpu/drm/udl/udl_drv.h
@@ -53,6 +53,10 @@ struct udl_device {
 	struct usb_device *udev;
 	struct drm_crtc *crtc;
 
+	/* active framebuffer on the 16-bit channel */
+	const struct drm_framebuffer *active_fb_16;
+	spinlock_t active_fb_16_lock;
+
 	struct mutex gem_lock;
 
 	int sku_pixel_limit;
@@ -73,7 +77,6 @@ struct udl_device {
 struct udl_framebuffer {
 	struct drm_framebuffer base;
 	struct drm_gem_shmem_object *shmem;
-	bool active_16; /* active on the 16-bit channel */
 };
 
 #define to_udl_fb(x) container_of(x, struct udl_framebuffer, base)
diff --git a/drivers/gpu/drm/udl/udl_fb.c b/drivers/gpu/drm/udl/udl_fb.c
index a9e6ec360b16..e9a4bbd5b195 100644
--- a/drivers/gpu/drm/udl/udl_fb.c
+++ b/drivers/gpu/drm/udl/udl_fb.c
@@ -78,8 +78,12 @@ int udl_handle_damage(struct udl_framebuffer *fb, int x, int y,
 	BUG_ON(!is_power_of_2(fb->base.format->cpp[0]));
 	log_bpp = __ffs(fb->base.format->cpp[0]);
 
-	if (!fb->active_16)
+	spin_lock(&udl->active_fb_16_lock);
+	if (udl->active_fb_16 != &fb->base) {
+		spin_unlock(&udl->active_fb_16_lock);
 		return 0;
+	}
+	spin_unlock(&udl->active_fb_16_lock);
 
 	vaddr = drm_gem_shmem_vmap(&fb->shmem->base);
 	if (IS_ERR(vaddr)) {
@@ -153,14 +157,19 @@ static int udl_user_framebuffer_dirty(struct drm_framebuffer *fb,
 				      unsigned num_clips)
 {
 	struct udl_framebuffer *ufb = to_udl_fb(fb);
+	struct udl_device *udl = fb->dev->dev_private;
 	struct dma_buf_attachment *import_attach;
 	int i;
 	int ret = 0;
 
 	drm_modeset_lock_all(fb->dev);
 
-	if (!ufb->active_16)
+	spin_lock(&udl->active_fb_16_lock);
+	if (udl->active_fb_16 != fb) {
+		spin_unlock(&udl->active_fb_16_lock);
 		goto unlock;
+	}
+	spin_unlock(&udl->active_fb_16_lock);
 
 	import_attach = ufb->shmem->base.import_attach;
 
diff --git a/drivers/gpu/drm/udl/udl_main.c b/drivers/gpu/drm/udl/udl_main.c
index 9b091b5b063e..a23218fc7d8e 100644
--- a/drivers/gpu/drm/udl/udl_main.c
+++ b/drivers/gpu/drm/udl/udl_main.c
@@ -317,6 +317,9 @@ int udl_init(struct udl_device *udl)
 
 	DRM_DEBUG("\n");
 
+	udl->active_fb_16 = NULL;
+	spin_lock_init(&udl->active_fb_16_lock);
+
 	mutex_init(&udl->gem_lock);
 
 	if (!udl_parse_vendor_descriptor(dev, udl->udev)) {
diff --git a/drivers/gpu/drm/udl/udl_modeset.c b/drivers/gpu/drm/udl/udl_modeset.c
index 6582c9d27a87..44a741f463c9 100644
--- a/drivers/gpu/drm/udl/udl_modeset.c
+++ b/drivers/gpu/drm/udl/udl_modeset.c
@@ -332,11 +332,9 @@ static int udl_crtc_mode_set(struct drm_crtc *crtc,
 
 	wrptr = udl_dummy_render(wrptr);
 
-	if (old_fb) {
-		struct udl_framebuffer *uold_fb = to_udl_fb(old_fb);
-		uold_fb->active_16 = false;
-	}
-	ufb->active_16 = true;
+	spin_lock(&udl->active_fb_16_lock);
+	udl->active_fb_16 = &ufb->base;
+	spin_unlock(&udl->active_fb_16_lock);
 	udl->mode_buf_len = wrptr - buf;
 
 	/* damage all of it */
@@ -364,13 +362,11 @@ static int udl_crtc_page_flip(struct drm_crtc *crtc,
 {
 	struct udl_framebuffer *ufb = to_udl_fb(fb);
 	struct drm_device *dev = crtc->dev;
+	struct udl_device *udl = dev->dev_private;
 
-	struct drm_framebuffer *old_fb = crtc->primary->fb;
-	if (old_fb) {
-		struct udl_framebuffer *uold_fb = to_udl_fb(old_fb);
-		uold_fb->active_16 = false;
-	}
-	ufb->active_16 = true;
+	spin_lock(&udl->active_fb_16_lock);
+	udl->active_fb_16 = fb;
+	spin_unlock(&udl->active_fb_16_lock);
 
 	udl_handle_damage(ufb, 0, 0, fb->width, fb->height);
 
-- 
2.28.0

