Skip to contentSkip to conversation

drm/vram: Add VRAM buffers for HW cursors

PATCH In review Thomas Zimmermann 2 participants 14 messages Sep 18, 2019 – Sep 19, 2019

Subsystems (5)
lore
Share & tools
lore

Thomas Zimmermann

author maintainer
#0 lore raw

cover drm/vram: Add VRAM buffers for HW cursors

This patchset adds dedicated VRAM buffers and cleans up the memory management of HW cursors in ast and mgag200.

The ast and mgag200 drivers support HW cursors of uncommon pixel formats. Both drivers manage cursor memory in dedicated GEM VRAM buffer objects with a double buffering scheme; either by alternating between two GEM BOs, or by alternating between offsets within the same GEM BO. The code is convoluted and can lead to memory fragmentation if the BO is stored the middle of VRAM. This is especially a problem on devices with only a small amount of video memory.

This patchset introduces VRAM buffers, a simple data structure that manages dedicated areas of VRAM. The VRAM buffer supports double- buffering (or more) to allow for flicker-free updates. A typical use case is in managing small, static buffers, such as cursors or sprites.

The patchset then cleans up cursor memory management in ast and mgag200. To avoid the fragmentation issue, at the high end of each device's VRAM, a few KiB are reserved for the HW cursor. This memory is maintained by a double-buffered VRAM buffer. For updating the cursor image, each driver's cursor_set() functions fills the current back buffer and then makes it the front buffer.

The patchset has been tested on ast and mgag200 hardware.

Thomas Zimmermann (11):
drm/vram: Add struct drm_vram_buffer to VRAM helpers drm/ast: Don't call ast_show_cursor() from ast_cursor_move() drm/ast: Move cursor update code to ast_show_cursor() drm/ast: Reserve space for double-buffered cursor image drm/ast: Store cursor in reserved memory area drm/mgag200: Rename cursor functions to use mgag200_ prefix drm/mgag200: Add init and fini functions for cursor handling drm/mgag200: Add separate move-cursor function drm/mgag200: Move cursor-image update to mgag200_show_cursor() drm/mgag200: Reserve video memory for cursor plane drm/mgag200: Implement cursor buffer with struct drm_vram_buffer

 Documentation/gpu/drm-mm.rst             |  19 +-
 drivers/gpu/drm/ast/ast_drv.h            |   5 +-
 drivers/gpu/drm/ast/ast_mode.c           | 198 +++++++++--------
 drivers/gpu/drm/ast/ast_ttm.c            |  13 +-
 drivers/gpu/drm/drm_vram_helper_common.c | 259 ++++++++++++++++++++++-
 drivers/gpu/drm/mgag200/mgag200_cursor.c | 252 +++++++++++-----------
 drivers/gpu/drm/mgag200/mgag200_drv.h    |  24 +--
 drivers/gpu/drm/mgag200/mgag200_main.c   |  20 +-
 drivers/gpu/drm/mgag200/mgag200_mode.c   |   6 +-
 drivers/gpu/drm/mgag200/mgag200_ttm.c    |  15 +-
 include/drm/drm_vram_helper.h            |  64 ++++++
 11 files changed, 607 insertions(+), 268 deletions(-)
 create mode 100644 include/drm/drm_vram_helper.h

--
2.23.0

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

author maintainer
#1 lore raw

PATCH 1/11 drm/vram: Add struct drm_vram_buffer to VRAM helpers

Drivers with dedicated video memory occasionally need to reserve a memory area for a specific purpose, such as cursor images or sprites. Using GEM VRAM buffer objects can be problematic. For small buffers, GEM VRAM buffer objects are inefficient as they are aligned to page boundaries. And they cannot easily be placed at specific memory offsets. This can lead to memory fragmentation and is a problem for hardware with only a small amount of memory.

The data structure struct drm_vram_buffer manages an area of video memory. In contrast to GEM VRAM buffer objects, the memory is of static size and dedicated to a single purpose. The area can be of any size and places at an arbitrary offset. A single buffer can hold multiple frames.

A use case is in ast's and mgag200's cursor handling. Each driver reserves a small amount of dedicated video memory (<=32 KiB) that holds a double-buffered cursor image. The image is in a HW-specific, non-standard format. With a VRAM buffer, the dedicated memory can be of an optimal size and located at the high end of video memory. The remaining video memory is available for the framebuffer's GEM VRAM buffer objects.

---
 Documentation/gpu/drm-mm.rst             |  19 +-
 drivers/gpu/drm/drm_vram_helper_common.c | 259 ++++++++++++++++++++++-
 include/drm/drm_vram_helper.h            |  64 ++++++
 3 files changed, 335 insertions(+), 7 deletions(-)
 create mode 100644 include/drm/drm_vram_helper.h
Documentation/gpu/drm-mm.rst +14 -5
index 99d56015e077..eac12bd0ea3f 100644
@@ -379,15 +379,12 @@ GEM CMA Helper Functions Reference
 .. kernel-doc:: drivers/gpu/drm/drm_gem_cma_helper.c
    :export:
 
-VRAM Helper Function Reference
-==============================
+VRAM Helper Library
+===================
 
 .. kernel-doc:: drivers/gpu/drm/drm_vram_helper_common.c
    :doc: overview
 
-.. kernel-doc:: include/drm/drm_gem_vram_helper.h
-   :internal:
-
 GEM VRAM Helper Functions Reference
 -----------------------------------
 
@@ -400,6 +397,18 @@ GEM VRAM Helper Functions Reference
 .. kernel-doc:: drivers/gpu/drm/drm_gem_vram_helper.c
    :export:
 
+VRAM Buffer Function Reference
+------------------------------
+
+.. kernel-doc:: drivers/gpu/drm/drm_vram_helper_common.c
+   :doc: vbuf overview
+
+.. kernel-doc:: include/drm/drm_vram_helper.h
+   :internal:
+
+.. kernel-doc:: drivers/gpu/drm/drm_vram_helper_common.c
+   :export:
+
 GEM TTM Helper Functions Reference
 -----------------------------------
 
drivers/gpu/drm/drm_vram_helper_common.c +257 -2

278 lines not on this page · raw message

include/drm/drm_vram_helper.h new file +64 -0

64 lines not on this page · raw message

Signature · 6 lines
2.23.0

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#2 lore raw

Re: [PATCH 01/11] drm/vram: Add struct drm_vram_buffer to VRAM helpers

Quoted 4 lines · Thomas Zimmermann

Drivers with dedicated video memory occasionally need to reserve a memory area for a specific purpose, such as cursor images or sprites. Using GEM VRAM buffer objects can be problematic. For small buffers, GEM VRAM buffer objects are inefficient as they are aligned to page boundaries.

I would still allocate gem objects for that. Then use them as pool instead of using them directly.

Not sure this is worth the trouble just for the cursors though as they are big enough that page-sized allocations don't waste memory.

Quoted 3 lines

And they cannot easily be placed at specific memory offsets. This can lead to memory fragmentation and is a problem for hardware with only a small amount of memory.

Note that there is a flag to change the allocation strategy (TTM_PL_FLAG_TOPDOWN). You could allocate the cursor objects with the flag set. Should be good enough to avoid fragmentation.

I have a patch doing exactly that for qxl, for the same reason:
https://git.kraxel.org/cgit/linux/commit/?id=e00e913a64c6fba9630b311f8bc71bd7c9842479
(no, that patch wasn't sent to the list yet).

We could do the same for vram and either let the driver explicitly ask for top-down allocation, or use some threshold like the qxl patch.

So, I'm not convinced we actually need the drm_vram_buffer infrastructure.

cheers,
Gerd

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

author maintainer
#3 lore raw

Re: [PATCH 01/11] drm/vram: Add struct drm_vram_buffer to VRAM helpers

Hi

Am 19.09.19 um 07:58 schrieb Gerd Hoffmann:

Quoted 24 lines · Thomas Zimmermann
Quoted 4 lines

Drivers with dedicated video memory occasionally need to reserve a memory area for a specific purpose, such as cursor images or sprites. Using GEM VRAM buffer objects can be problematic. For small buffers, GEM VRAM buffer objects are inefficient as they are aligned to page boundaries.

I would still allocate gem objects for that. Then use them as pool instead of using them directly.

Not sure this is worth the trouble just for the cursors though as they are big enough that page-sized allocations don't waste memory.

Quoted 3 lines

And they cannot easily be placed at specific memory offsets. This can lead to memory fragmentation and is a problem for hardware with only a small amount of memory.

Note that there is a flag to change the allocation strategy (TTM_PL_FLAG_TOPDOWN). You could allocate the cursor objects with the flag set. Should be good enough to avoid fragmentation.

I have a patch doing exactly that for qxl, for the same reason:
https://git.kraxel.org/cgit/linux/commit/?id=e00e913a64c6fba9630b311f8bc71bd7c9842479
(no, that patch wasn't sent to the list yet).

Quoted 5 lines

We could do the same for vram and either let the driver explicitly ask for top-down allocation, or use some threshold like the qxl patch.

So, I'm not convinced we actually need the drm_vram_buffer infrastructure.

Well, OK. I don't mind using GEM buffers if they can solve the problem. Half of the patch set is unrelated to VRAM buffers anyway.

Best regards
Thomas

Quoted 9 lines

cheers,
Gerd

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

Signature · 5 lines
Thomas Zimmermann
Graphics Driver Developer
SUSE Linux GmbH, Maxfeldstrasse 5, 90409 Nuernberg, Germany
GF: Felix Imendörffer, Mary Higgins, Sri Rasiah
HRB 21284 (AG Nürnberg)

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

author maintainer
#4 lore raw

PATCH 2/11 drm/ast: Don't call ast_show_cursor() from ast_cursor_move()

In reply to Thomas Zimmermann

Separating the cursor's move() function from the show() function in preparation of further rework of the cursor update code.

'Showing' the cursor from within the move() function is required to updates the cursor position.

---
 drivers/gpu/drm/ast/ast_mode.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/gpu/drm/ast/ast_mode.c +4 -1
index 6caa6ebfeaa8..a4cbf2d5ee0a 100644
@@ -1236,6 +1236,7 @@ static int ast_cursor_move(struct drm_crtc *crtc,
 	struct ast_private *ast = crtc->dev->dev_private;
 	int x_offset, y_offset;
 	u8 *sig;
+	u8 jreg;
 
 	sig = drm_gem_vram_kmap(drm_gem_vram_of_gem(ast->cursor_cache),
 				false, NULL);
@@ -1262,7 +1263,9 @@ static int ast_cursor_move(struct drm_crtc *crtc,
 	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc7, ((y >> 8) & 0x07));
 
 	/* dummy write to fire HWC */
-	ast_show_cursor(crtc);
+	jreg = 0x02 |
+	       0x01; /* enable ARGB4444 cursor */
+	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xcb, 0xfc, jreg);
 
 	return 0;
 }
Signature · 6 lines
2.23.0

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
author maintainer
#5 lore raw

PATCH 3/11 drm/ast: Move cursor update code to ast_show_cursor()

In reply to Thomas Zimmermann

A call to ast's show-cursor function now receives the cursor image and updates the buffer. The change splits off image update and base-address update into separate functions. The will help with the upcoming change to VRAM buffers.

---
 drivers/gpu/drm/ast/ast_mode.c | 120 +++++++++++++++++++--------------
 1 file changed, 69 insertions(+), 51 deletions(-)
drivers/gpu/drm/ast/ast_mode.c +69 -51
index a4cbf2d5ee0a..1294f0612fd5 100644
@@ -1064,23 +1064,6 @@ static void ast_i2c_destroy(struct ast_i2c_chan *i2c)
 	kfree(i2c);
 }
 
-static void ast_show_cursor(struct drm_crtc *crtc)
-{
-	struct ast_private *ast = crtc->dev->dev_private;
-	u8 jreg;
-
-	jreg = 0x2;
-	/* enable ARGB cursor */
-	jreg |= 1;
-	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xcb, 0xfc, jreg);
-}
-
-static void ast_hide_cursor(struct drm_crtc *crtc)
-{
-	struct ast_private *ast = crtc->dev->dev_private;
-	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xcb, 0xfc, 0x00);
-}
-
 static u32 copy_cursor_image(u8 *src, u8 *dst, int width, int height)
 {
 	union {
@@ -1137,6 +1120,72 @@ static u32 copy_cursor_image(u8 *src, u8 *dst, int width, int height)
 	return csum;
 }
 
+static int ast_cursor_update(void *dst, void *src, unsigned int width,
+			     unsigned int height)
+{
+	u32 csum;
+
+	/* do data transfer to cursor cache */
+	csum = copy_cursor_image(src, dst, width, height);
+
+	/* write checksum + signature */
+	dst += AST_HWC_SIZE;
+	writel(csum, dst);
+	writel(width, dst + AST_HWC_SIGNATURE_SizeX);
+	writel(height, dst + AST_HWC_SIGNATURE_SizeY);
+	writel(0, dst + AST_HWC_SIGNATURE_HOTSPOTX);
+	writel(0, dst + AST_HWC_SIGNATURE_HOTSPOTY);
+
+	return 0;
+}
+
+static void ast_cursor_set_base(struct ast_private *ast, u64 address)
+{
+	u8 addr0 = (address >> 3) & 0xff;
+	u8 addr1 = (address >> 11) & 0xff;
+	u8 addr2 = (address >> 19) & 0xff;
+
+	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc8, addr0);
+	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc9, addr1);
+	ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xca, addr2);
+}
+
+static int ast_show_cursor(struct drm_crtc *crtc, void *dst, void *src,
+			   unsigned int width, unsigned int height,
+			   u64 dst_gpu)
+{
+	struct ast_private *ast = crtc->dev->dev_private;
+	struct ast_crtc *ast_crtc = to_ast_crtc(crtc);
+	int ret;
+	u8 jreg;
+
+	dst += (AST_HWC_SIZE + AST_HWC_SIGNATURE_SIZE)*ast->next_cursor;
+
+	ret = ast_cursor_update(dst, src, width, height);
+	if (ret)
+		return ret;
+	ast_cursor_set_base(ast, dst_gpu);
+
+	ast->next_cursor = (ast->next_cursor + 1) % AST_DEFAULT_HWC_NUM;
+
+	ast_crtc->offset_x = AST_MAX_HWC_WIDTH - width;
+	ast_crtc->offset_y = AST_MAX_HWC_WIDTH - height;
+
+	jreg = 0x2;
+	/* enable ARGB cursor */
+	jreg |= 1;
+	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xcb, 0xfc, jreg);
+
+	return 0;
+}
+
+static void ast_hide_cursor(struct drm_crtc *crtc)
+{
+	struct ast_private *ast = crtc->dev->dev_private;
+
+	ast_set_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xcb, 0xfc, 0x00);
+}
+
 static int ast_cursor_set(struct drm_crtc *crtc,
 			  struct drm_file *file_priv,
 			  uint32_t handle,
@@ -1144,12 +1193,9 @@ static int ast_cursor_set(struct drm_crtc *crtc,
 			  uint32_t height)
 {
 	struct ast_private *ast = crtc->dev->dev_private;
-	struct ast_crtc *ast_crtc = to_ast_crtc(crtc);
 	struct drm_gem_object *obj;
 	struct drm_gem_vram_object *gbo;
 	s64 dst_gpu;
-	u64 gpu_addr;
-	u32 csum;
 	int ret;
 	u8 *src, *dst;
 
@@ -1185,37 +1231,9 @@ static int ast_cursor_set(struct drm_crtc *crtc,
 		goto err_drm_gem_vram_vunmap;
 	}
 
-	dst += (AST_HWC_SIZE + AST_HWC_SIGNATURE_SIZE)*ast->next_cursor;
-
-	/* do data transfer to cursor cache */
-	csum = copy_cursor_image(src, dst, width, height);
-
-	/* write checksum + signature */
-	{
-		struct drm_gem_vram_object *dst_gbo =
-			drm_gem_vram_of_gem(ast->cursor_cache);
-		u8 *dst = drm_gem_vram_kmap(dst_gbo, false, NULL);
-		dst += (AST_HWC_SIZE + AST_HWC_SIGNATURE_SIZE)*ast->next_cursor + AST_HWC_SIZE;
-		writel(csum, dst);
-		writel(width, dst + AST_HWC_SIGNATURE_SizeX);
-		writel(height, dst + AST_HWC_SIGNATURE_SizeY);
-		writel(0, dst + AST_HWC_SIGNATURE_HOTSPOTX);
-		writel(0, dst + AST_HWC_SIGNATURE_HOTSPOTY);
-
-		/* set pattern offset */
-		gpu_addr = (u64)dst_gpu;
-		gpu_addr += (AST_HWC_SIZE + AST_HWC_SIGNATURE_SIZE)*ast->next_cursor;
-		gpu_addr >>= 3;
-		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc8, gpu_addr & 0xff);
-		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xc9, (gpu_addr >> 8) & 0xff);
-		ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xca, (gpu_addr >> 16) & 0xff);
-	}
-	ast_crtc->offset_x = AST_MAX_HWC_WIDTH - width;
-	ast_crtc->offset_y = AST_MAX_HWC_WIDTH - height;
-
-	ast->next_cursor = (ast->next_cursor + 1) % AST_DEFAULT_HWC_NUM;
-
-	ast_show_cursor(crtc);
+	ret = ast_show_cursor(crtc, dst, src, width, height, dst_gpu);
+	if (ret)
+		goto err_drm_gem_vram_kunmap;
 
 	drm_gem_vram_vunmap(gbo, src);
 	drm_gem_object_put_unlocked(obj);
Signature · 6 lines
2.23.0

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
author maintainer
#6 lore raw

PATCH 4/11 drm/ast: Reserve space for double-buffered cursor image

In reply to Thomas Zimmermann

With the patch, we reserve 2x 16 KiB at the high end of video memory, with each frame aligned to an 8-byte boundary. The remaining memory is available for GEM VRAM buffer objects.

---
 drivers/gpu/drm/ast/ast_ttm.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/gpu/drm/ast/ast_ttm.c +10 -3
index fad34106083a..8e6a1d8917d0 100644
@@ -35,13 +35,20 @@
 
 int ast_mm_init(struct ast_private *ast)
 {
+	unsigned long cursor_size;
 	struct drm_vram_mm *vmm;
 	int ret;
 	struct drm_device *dev = ast->dev;
 
-	vmm = drm_vram_helper_alloc_mm(
-		dev, pci_resource_start(dev->pdev, 0),
-		ast->vram_size);
+	/* At the high end of video memory, we reserve space for
+	 * two cursor images. The cursor plane uses this memory to
+	 * store a double-buffered image of the current cursor.
+	 */
+	cursor_size = roundup((AST_HWC_SIZE + AST_HWC_SIGNATURE_SIZE),
+			      PAGE_SIZE) * AST_DEFAULT_HWC_NUM;
+
+	vmm = drm_vram_helper_alloc_mm(dev, pci_resource_start(dev->pdev, 0),
+				       ast->vram_size - cursor_size);
 	if (IS_ERR(vmm)) {
 		ret = PTR_ERR(vmm);
 		DRM_ERROR("Error initializing VRAM MM; %d\n", ret);
Signature · 6 lines
2.23.0

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
author maintainer
#7 lore raw

PATCH 5/11 drm/ast: Store cursor in reserved memory area

In reply to Thomas Zimmermann

An instance of struct drm_vram_buffer maintains the memory area at the high end of video memory. The VRAM buffer replaces the GEM-based buffers and ast's buffer swapping. Two frames provide double buffering; with each frame aligned to 8 bytes, as required by the hardware.

The full cursor update is currently performed in ast_show_cursor(). After conversing the ast driver to atomic mode setting, The update of the cursor's back buffer can be performed in the cursor plane's implementation of prepare_fb(). The swap operation can be performed in the cursor plane's implementation of atomic_update().

---
 drivers/gpu/drm/ast/ast_drv.h  |   5 +-
 drivers/gpu/drm/ast/ast_mode.c | 103 ++++++++++++---------------------
 2 files changed, 41 insertions(+), 67 deletions(-)
drivers/gpu/drm/ast/ast_drv.h +3 -2
index 244cc7c382af..d459ede43384 100644
@@ -39,6 +39,7 @@
 #include <drm/drm_mode.h>
 #include <drm/drm_framebuffer.h>
 #include <drm/drm_fb_helper.h>
+#include <drm/drm_vram_helper.h>
 
 #define DRIVER_AUTHOR		"Dave Airlie"
 
@@ -97,8 +98,8 @@ struct ast_private {
 
 	int fb_mtrr;
 
-	struct drm_gem_object *cursor_cache;
-	int next_cursor;
+	struct drm_vram_buffer cursor;
+
 	bool support_wide_screen;
 	enum {
 		ast_use_p2a,
drivers/gpu/drm/ast/ast_mode.c +38 -65

175 lines not on this page · raw message

Signature · 6 lines
2.23.0

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
author maintainer
#8 lore raw

PATCH 6/11 drm/mgag200: Rename cursor functions to use mgag200_ prefix

In reply to Thomas Zimmermann

Although the driver source code is fairly inconsistent wrt naming, the prefix should be mgag200. Rename cursor functions accordingly.

---
 drivers/gpu/drm/mgag200/mgag200_cursor.c | 20 ++++++++------------
 drivers/gpu/drm/mgag200/mgag200_drv.h    |  6 +++---
 drivers/gpu/drm/mgag200/mgag200_mode.c   |  4 ++--
 3 files changed, 13 insertions(+), 17 deletions(-)
drivers/gpu/drm/mgag200/mgag200_cursor.c +8 -12
index 89f61573a497..648357ad9a4b 100644
@@ -12,11 +12,10 @@
 static bool warn_transparent = true;
 static bool warn_palette = true;
 
-/*
-  Hide the cursor off screen. We can't disable the cursor hardware because it
-  takes too long to re-activate and causes momentary corruption
-*/
-static void mga_hide_cursor(struct mga_device *mdev)
+/* Hide the cursor off screen. We can't disable the cursor hardware because it
+ * takes too long to re-activate and causes momentary corruption
+ */
+static void mgag200_hide_cursor(struct mga_device *mdev)
 {
 	WREG8(MGA_CURPOSXL, 0);
 	WREG8(MGA_CURPOSXH, 0);
@@ -25,11 +24,8 @@ static void mga_hide_cursor(struct mga_device *mdev)
 	mdev->cursor.pixels_current = NULL;
 }
 
-int mga_crtc_cursor_set(struct drm_crtc *crtc,
-			struct drm_file *file_priv,
-			uint32_t handle,
-			uint32_t width,
-			uint32_t height)
+int mgag200_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
+			    uint32_t handle, uint32_t width, uint32_t height)
 {
 	struct drm_device *dev = crtc->dev;
 	struct mga_device *mdev = (struct mga_device *)dev->dev_private;
@@ -66,7 +62,7 @@ int mga_crtc_cursor_set(struct drm_crtc *crtc,
 	}
 
 	if (!handle || !file_priv) {
-		mga_hide_cursor(mdev);
+		mgag200_hide_cursor(mdev);
 		return 0;
 	}
 
@@ -224,7 +220,7 @@ int mga_crtc_cursor_set(struct drm_crtc *crtc,
 	return ret;
 }
 
-int mga_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
+int mgag200_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
 {
 	struct mga_device *mdev = (struct mga_device *)crtc->dev->dev_private;
 	/* Our origin is at (64,64) */
drivers/gpu/drm/mgag200/mgag200_drv.h +3 -3
index 37c003ed57c0..5244e3fa4203 100644
@@ -203,8 +203,8 @@ int mgag200_mm_init(struct mga_device *mdev);
 void mgag200_mm_fini(struct mga_device *mdev);
 int mgag200_mmap(struct file *filp, struct vm_area_struct *vma);
 
-int mga_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
-						uint32_t handle, uint32_t width, uint32_t height);
-int mga_crtc_cursor_move(struct drm_crtc *crtc, int x, int y);
+int mgag200_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
+			    uint32_t handle, uint32_t width, uint32_t height);
+int mgag200_crtc_cursor_move(struct drm_crtc *crtc, int x, int y);
 
 #endif				/* __MGAG200_DRV_H__ */
drivers/gpu/drm/mgag200/mgag200_mode.c +2 -2
index 68226556044b..0cf5608c3644 100644
@@ -1413,8 +1413,8 @@ static void mga_crtc_disable(struct drm_crtc *crtc)
 
 /* These provide the minimum set of functions required to handle a CRTC */
 static const struct drm_crtc_funcs mga_crtc_funcs = {
-	.cursor_set = mga_crtc_cursor_set,
-	.cursor_move = mga_crtc_cursor_move,
+	.cursor_set = mgag200_crtc_cursor_set,
+	.cursor_move = mgag200_crtc_cursor_move,
 	.gamma_set = mga_crtc_gamma_set,
 	.set_config = drm_crtc_helper_set_config,
 	.destroy = mga_crtc_destroy,
Signature · 6 lines
2.23.0

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
author maintainer
#9 lore raw

PATCH 7/11 drm/mgag200: Add init and fini functions for cursor handling

In reply to Thomas Zimmermann

Moving the cursor initialization and cleanup into separate functions makes the overall code slightly more readable.

---
 drivers/gpu/drm/mgag200/mgag200_cursor.c | 28 ++++++++++++++++++++++++
 drivers/gpu/drm/mgag200/mgag200_drv.h    |  2 ++
 drivers/gpu/drm/mgag200/mgag200_main.c   | 18 +++++----------
 3 files changed, 35 insertions(+), 13 deletions(-)
drivers/gpu/drm/mgag200/mgag200_cursor.c +28 -0
index 648357ad9a4b..c6a7ad11ca72 100644
@@ -24,6 +24,34 @@ static void mgag200_hide_cursor(struct mga_device *mdev)
 	mdev->cursor.pixels_current = NULL;
 }
 
+int mgag200_cursor_init(struct mga_device *mdev)
+{
+	struct drm_device *dev = mdev->dev;
+
+	/*
+	 * Make small buffers to store a hardware cursor (double
+	 * buffered icon updates)
+	 */
+	mdev->cursor.pixels_1 = drm_gem_vram_create(dev, &dev->vram_mm->bdev,
+						    roundup(48*64, PAGE_SIZE),
+						    0, 0);
+	mdev->cursor.pixels_2 = drm_gem_vram_create(dev, &dev->vram_mm->bdev,
+						    roundup(48*64, PAGE_SIZE),
+						    0, 0);
+	if (IS_ERR(mdev->cursor.pixels_2) || IS_ERR(mdev->cursor.pixels_1)) {
+		mdev->cursor.pixels_1 = NULL;
+		mdev->cursor.pixels_2 = NULL;
+		dev_warn(&dev->pdev->dev,
+			"Could not allocate space for cursors. Not doing hardware cursors.\n");
+	}
+	mdev->cursor.pixels_current = NULL;
+
+	return 0;
+}
+
+void mgag200_cursor_fini(struct mga_device *mdev)
+{ }
+
 int mgag200_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
 			    uint32_t handle, uint32_t width, uint32_t height)
 {
drivers/gpu/drm/mgag200/mgag200_drv.h +2 -0
index 5244e3fa4203..01243fa6397c 100644
@@ -203,6 +203,8 @@ int mgag200_mm_init(struct mga_device *mdev);
 void mgag200_mm_fini(struct mga_device *mdev);
 int mgag200_mmap(struct file *filp, struct vm_area_struct *vma);
 
+int mgag200_cursor_init(struct mga_device *mdev);
+void mgag200_cursor_fini(struct mga_device *mdev);
 int mgag200_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
 			    uint32_t handle, uint32_t width, uint32_t height);
 int mgag200_crtc_cursor_move(struct drm_crtc *crtc, int x, int y);
drivers/gpu/drm/mgag200/mgag200_main.c +5 -13
index a9773334dedf..2b59280777a5 100644
@@ -171,20 +171,10 @@ int mgag200_driver_load(struct drm_device *dev, unsigned long flags)
 		goto err_modeset;
 	}
 
-	/* Make small buffers to store a hardware cursor (double buffered icon updates) */
-	mdev->cursor.pixels_1 = drm_gem_vram_create(dev, &dev->vram_mm->bdev,
-						    roundup(48*64, PAGE_SIZE),
-						    0, 0);
-	mdev->cursor.pixels_2 = drm_gem_vram_create(dev, &dev->vram_mm->bdev,
-						    roundup(48*64, PAGE_SIZE),
-						    0, 0);
-	if (IS_ERR(mdev->cursor.pixels_2) || IS_ERR(mdev->cursor.pixels_1)) {
-		mdev->cursor.pixels_1 = NULL;
-		mdev->cursor.pixels_2 = NULL;
+	r = mgag200_cursor_init(mdev);
+	if (r)
 		dev_warn(&dev->pdev->dev,
-			"Could not allocate space for cursors. Not doing hardware cursors.\n");
-	}
-	mdev->cursor.pixels_current = NULL;
+			"Could not initialize cursors. Not doing hardware cursors.\n");
 
 	r = drm_fbdev_generic_setup(mdev->dev, 0);
 	if (r)
@@ -194,6 +184,7 @@ int mgag200_driver_load(struct drm_device *dev, unsigned long flags)
 
 err_modeset:
 	drm_mode_config_cleanup(dev);
+	mgag200_cursor_fini(mdev);
 	mgag200_mm_fini(mdev);
 err_mm:
 	dev->dev_private = NULL;
@@ -209,6 +200,7 @@ void mgag200_driver_unload(struct drm_device *dev)
 		return;
 	mgag200_modeset_fini(mdev);
 	drm_mode_config_cleanup(dev);
+	mgag200_cursor_fini(mdev);
 	mgag200_mm_fini(mdev);
 	dev->dev_private = NULL;
 }
Signature · 6 lines
2.23.0

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
author maintainer
#10 lore raw

PATCH 8/11 drm/mgag200: Add separate move-cursor function

In reply to Thomas Zimmermann

Adding mgag200_move_cursor() makes the cursor code more consistent and will become handy when we move to universal cursor planes.

---
 drivers/gpu/drm/mgag200/mgag200_cursor.c | 29 ++++++++++++++++--------
 1 file changed, 20 insertions(+), 9 deletions(-)
drivers/gpu/drm/mgag200/mgag200_cursor.c +20 -9
index c6a7ad11ca72..aabacdd3e401 100644
@@ -24,6 +24,24 @@ static void mgag200_hide_cursor(struct mga_device *mdev)
 	mdev->cursor.pixels_current = NULL;
 }
 
+static void mgag200_move_cursor(struct mga_device *mdev, int x, int y)
+{
+	if (WARN_ON(x <= 0))
+		return;
+	if (WARN_ON(y <= 0))
+		return;
+	if (WARN_ON(x & ~0xffff))
+		return;
+	if (WARN_ON(y & ~0xffff))
+		return;
+
+	WREG8(MGA_CURPOSXL, x & 0xff);
+	WREG8(MGA_CURPOSXH, (x>>8) & 0xff);
+
+	WREG8(MGA_CURPOSYL, y & 0xff);
+	WREG8(MGA_CURPOSYH, (y>>8) & 0xff);
+}
+
 int mgag200_cursor_init(struct mga_device *mdev)
 {
 	struct drm_device *dev = mdev->dev;
@@ -251,19 +269,12 @@ int mgag200_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
 int mgag200_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
 {
 	struct mga_device *mdev = (struct mga_device *)crtc->dev->dev_private;
+
 	/* Our origin is at (64,64) */
 	x += 64;
 	y += 64;
 
-	BUG_ON(x <= 0);
-	BUG_ON(y <= 0);
-	BUG_ON(x & ~0xffff);
-	BUG_ON(y & ~0xffff);
+	mgag200_move_cursor(mdev, x, y);
 
-	WREG8(MGA_CURPOSXL, x & 0xff);
-	WREG8(MGA_CURPOSXH, (x>>8) & 0xff);
-
-	WREG8(MGA_CURPOSYL, y & 0xff);
-	WREG8(MGA_CURPOSYH, (y>>8) & 0xff);
 	return 0;
 }
Signature · 6 lines
2.23.0

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
author maintainer
#11 lore raw

PATCH 9/11 drm/mgag200: Move cursor-image update to mgag200_show_cursor()

In reply to Thomas Zimmermann

This separates the management of buffer objects from updating the hardware cursor buffer. While doing this, we can further split the image-update code into code for writing the buffer, setting the base scan-out address, and enabling the cursor. The first two operations are in dedicated functions update() and set_base(). This simplifies the conversion to a VRAM buffer.

---
 drivers/gpu/drm/mgag200/mgag200_cursor.c | 221 +++++++++++++----------
 1 file changed, 126 insertions(+), 95 deletions(-)
drivers/gpu/drm/mgag200/mgag200_cursor.c +126 -95
index aabacdd3e401..ab3dfe6bdd92 100644
@@ -12,6 +12,128 @@
 static bool warn_transparent = true;
 static bool warn_palette = true;
 
+static int mgag200_cursor_update(struct mga_device *mdev, void *dst, void *src,
+				 unsigned int width, unsigned int height)
+{
+	struct drm_device *dev = mdev->dev;
+	unsigned int i, row, col;
+	uint32_t colour_set[16];
+	uint32_t *next_space = &colour_set[0];
+	uint32_t *palette_iter;
+	uint32_t this_colour;
+	bool found = false;
+	int colour_count = 0;
+	u8 reg_index;
+	u8 this_row[48];
+
+	memset(&colour_set[0], 0, sizeof(uint32_t)*16);
+	/* width*height*4 = 16384 */
+	for (i = 0; i < 16384; i += 4) {
+		this_colour = ioread32(src + i);
+		/* No transparency */
+		if (this_colour>>24 != 0xff &&
+			this_colour>>24 != 0x0) {
+			if (warn_transparent) {
+				dev_info(&dev->pdev->dev, "Video card doesn't support cursors with partial transparency.\n");
+				dev_info(&dev->pdev->dev, "Not enabling hardware cursor.\n");
+				warn_transparent = false; /* Only tell the user once. */
+			}
+			return -EINVAL;
+		}
+		/* Don't need to store transparent pixels as colours */
+		if (this_colour>>24 == 0x0)
+			continue;
+		found = false;
+		for (palette_iter = &colour_set[0]; palette_iter != next_space; palette_iter++) {
+			if (*palette_iter == this_colour) {
+				found = true;
+				break;
+			}
+		}
+		if (found)
+			continue;
+		/* We only support 4bit paletted cursors */
+		if (colour_count >= 16) {
+			if (warn_palette) {
+				dev_info(&dev->pdev->dev, "Video card only supports cursors with up to 16 colours.\n");
+				dev_info(&dev->pdev->dev, "Not enabling hardware cursor.\n");
+				warn_palette = false; /* Only tell the user once. */
+			}
+			return -EINVAL;
+		}
+		*next_space = this_colour;
+		next_space++;
+		colour_count++;
+	}
+
+	/* Program colours from cursor icon into palette */
+	for (i = 0; i < colour_count; i++) {
+		if (i <= 2)
+			reg_index = 0x8 + i*0x4;
+		else
+			reg_index = 0x60 + i*0x3;
+		WREG_DAC(reg_index, colour_set[i] & 0xff);
+		WREG_DAC(reg_index+1, colour_set[i]>>8 & 0xff);
+		WREG_DAC(reg_index+2, colour_set[i]>>16 & 0xff);
+		if (WARN_ON((colour_set[i]>>24 & 0xff) != 0xff))
+			return -EINVAL;
+	}
+
+	/* now write colour indices into hardware cursor buffer */
+	for (row = 0; row < 64; row++) {
+		memset(&this_row[0], 0, 48);
+		for (col = 0; col < 64; col++) {
+			this_colour = ioread32(src + 4*(col + 64*row));
+			/* write transparent pixels */
+			if (this_colour>>24 == 0x0) {
+				this_row[47 - col/8] |= 0x80>>(col%8);
+				continue;
+			}
+
+			/* write colour index here */
+			for (i = 0; i < colour_count; i++) {
+				if (colour_set[i] == this_colour) {
+					if (col % 2)
+						this_row[col/2] |= i<<4;
+					else
+						this_row[col/2] |= i;
+					break;
+				}
+			}
+		}
+		memcpy_toio(dst + row*48, &this_row[0], 48);
+	}
+
+	return 0;
+}
+
+static void mgag200_cursor_set_base(struct mga_device *mdev, u64 address)
+{
+	u8 addrl = (address >> 10) & 0xff;
+	u8 addrh = (address >> 18) & 0x3f;
+
+	/* Program gpu address of cursor buffer */
+	WREG_DAC(MGA1064_CURSOR_BASE_ADR_LOW, addrl);
+	WREG_DAC(MGA1064_CURSOR_BASE_ADR_HI, addrh);
+}
+
+static int mgag200_show_cursor(struct mga_device *mdev, void *dst, void *src,
+			       unsigned int width, unsigned int height,
+			       u64 dst_gpu)
+{
+	int ret;
+
+	ret = mgag200_cursor_update(mdev, dst, src, width, height);
+	if (ret)
+		return ret;
+	mgag200_cursor_set_base(mdev, dst_gpu);
+
+	/* Adjust cursor control register to turn on the cursor */
+	WREG_DAC(MGA1064_CURSOR_CTL, 4); /* 16-colour palletized cursor mode */
+
+	return 0;
+}
+
 /* Hide the cursor off screen. We can't disable the cursor hardware because it
  * takes too long to re-activate and causes momentary corruption
  */
@@ -81,19 +203,10 @@ int mgag200_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
 	struct drm_gem_vram_object *pixels_next;
 	struct drm_gem_object *obj;
 	struct drm_gem_vram_object *gbo = NULL;
-	int ret = 0;
+	int ret;
 	u8 *src, *dst;
-	unsigned int i, row, col;
-	uint32_t colour_set[16];
-	uint32_t *next_space = &colour_set[0];
-	uint32_t *palette_iter;
-	uint32_t this_colour;
-	bool found = false;
-	int colour_count = 0;
 	s64 gpu_addr;
 	u64 dst_gpu;
-	u8 reg_index;
-	u8 this_row[48];
 
 	if (!pixels_1 || !pixels_2) {
 		WREG8(MGA_CURPOSXL, 0);
@@ -158,91 +271,9 @@ int mgag200_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
 	}
 	dst_gpu = (u64)gpu_addr;
 
-	memset(&colour_set[0], 0, sizeof(uint32_t)*16);
-	/* width*height*4 = 16384 */
-	for (i = 0; i < 16384; i += 4) {
-		this_colour = ioread32(src + i);
-		/* No transparency */
-		if (this_colour>>24 != 0xff &&
-			this_colour>>24 != 0x0) {
-			if (warn_transparent) {
-				dev_info(&dev->pdev->dev, "Video card doesn't support cursors with partial transparency.\n");
-				dev_info(&dev->pdev->dev, "Not enabling hardware cursor.\n");
-				warn_transparent = false; /* Only tell the user once. */
-			}
-			ret = -EINVAL;
-			goto err_drm_gem_vram_kunmap_dst;
-		}
-		/* Don't need to store transparent pixels as colours */
-		if (this_colour>>24 == 0x0)
-			continue;
-		found = false;
-		for (palette_iter = &colour_set[0]; palette_iter != next_space; palette_iter++) {
-			if (*palette_iter == this_colour) {
-				found = true;
-				break;
-			}
-		}
-		if (found)
-			continue;
-		/* We only support 4bit paletted cursors */
-		if (colour_count >= 16) {
-			if (warn_palette) {
-				dev_info(&dev->pdev->dev, "Video card only supports cursors with up to 16 colours.\n");
-				dev_info(&dev->pdev->dev, "Not enabling hardware cursor.\n");
-				warn_palette = false; /* Only tell the user once. */
-			}
-			ret = -EINVAL;
-			goto err_drm_gem_vram_kunmap_dst;
-		}
-		*next_space = this_colour;
-		next_space++;
-		colour_count++;
-	}
-
-	/* Program colours from cursor icon into palette */
-	for (i = 0; i < colour_count; i++) {
-		if (i <= 2)
-			reg_index = 0x8 + i*0x4;
-		else
-			reg_index = 0x60 + i*0x3;
-		WREG_DAC(reg_index, colour_set[i] & 0xff);
-		WREG_DAC(reg_index+1, colour_set[i]>>8 & 0xff);
-		WREG_DAC(reg_index+2, colour_set[i]>>16 & 0xff);
-		BUG_ON((colour_set[i]>>24 & 0xff) != 0xff);
-	}
-
-	/* now write colour indices into hardware cursor buffer */
-	for (row = 0; row < 64; row++) {
-		memset(&this_row[0], 0, 48);
-		for (col = 0; col < 64; col++) {
-			this_colour = ioread32(src + 4*(col + 64*row));
-			/* write transparent pixels */
-			if (this_colour>>24 == 0x0) {
-				this_row[47 - col/8] |= 0x80>>(col%8);
-				continue;
-			}
-
-			/* write colour index here */
-			for (i = 0; i < colour_count; i++) {
-				if (colour_set[i] == this_colour) {
-					if (col % 2)
-						this_row[col/2] |= i<<4;
-					else
-						this_row[col/2] |= i;
-					break;
-				}
-			}
-		}
-		memcpy_toio(dst + row*48, &this_row[0], 48);
-	}
-
-	/* Program gpu address of cursor buffer */
-	WREG_DAC(MGA1064_CURSOR_BASE_ADR_LOW, (u8)((dst_gpu>>10) & 0xff));
-	WREG_DAC(MGA1064_CURSOR_BASE_ADR_HI, (u8)((dst_gpu>>18) & 0x3f));
-
-	/* Adjust cursor control register to turn on the cursor */
-	WREG_DAC(MGA1064_CURSOR_CTL, 4); /* 16-colour palletized cursor mode */
+	ret = mgag200_show_cursor(mdev, dst, src, width, height, dst_gpu);
+	if (ret)
+		goto err_drm_gem_vram_kunmap_dst;
 
 	/* Now update internal buffer pointers */
 	if (pixels_current)
Signature · 6 lines
2.23.0

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
author maintainer
#12 lore raw

PATCH 10/11 drm/mgag200: Reserve video memory for cursor plane

In reply to Thomas Zimmermann

The double-buffered cursor image is currently stored in video memory by creating two BOs and pinning them to VRAM. The exact location is chosen by VRAM helpers.

Since the driver has no control over BO placement, pinned cursor BOs can conflict with framebuffer BOs and prevent the primary plane from displaying its framebuffer.

As a first step to solving this problem, we reserve dedicated space at the high end of the video memory for the cursor images. As the amount of video memory now differs from the amount of available framebuffer memory, size tests are performed against the VRAM helper's framebuffer limits.

---
 drivers/gpu/drm/mgag200/mgag200_main.c |  2 +-
 drivers/gpu/drm/mgag200/mgag200_mode.c |  2 +-
 drivers/gpu/drm/mgag200/mgag200_ttm.c  | 15 ++++++++++++++-
 3 files changed, 16 insertions(+), 3 deletions(-)
drivers/gpu/drm/mgag200/mgag200_main.c +1 -1
index 2b59280777a5..4b8686e9a276 100644
@@ -159,7 +159,7 @@ int mgag200_driver_load(struct drm_device *dev, unsigned long flags)
 
 	drm_mode_config_init(dev);
 	dev->mode_config.funcs = (void *)&mga_mode_funcs;
-	if (IS_G200_SE(mdev) && mdev->mc.vram_size < (2048*1024))
+	if (IS_G200_SE(mdev) && dev->vram_mm->vram_size < (2048*1024))
 		dev->mode_config.preferred_depth = 16;
 	else
 		dev->mode_config.preferred_depth = 32;
drivers/gpu/drm/mgag200/mgag200_mode.c +1 -1
index 0cf5608c3644..2ac66a2270bb 100644
@@ -1629,7 +1629,7 @@ static enum drm_mode_status mga_vga_mode_valid(struct drm_connector *connector,
 			bpp = connector->cmdline_mode.bpp;
 	}
 
-	if ((mode->hdisplay * mode->vdisplay * (bpp/8)) > mdev->mc.vram_size) {
+	if ((mode->hdisplay * mode->vdisplay * (bpp/8)) > dev->vram_mm->vram_size) {
 		if (connector->cmdline_mode.specified)
 			connector->cmdline_mode.specified = false;
 		return MODE_BAD;
drivers/gpu/drm/mgag200/mgag200_ttm.c +14 -1
index 69c81ebf3745..1df8504c6cab 100644
@@ -32,12 +32,25 @@
 
 int mgag200_mm_init(struct mga_device *mdev)
 {
+	unsigned long cursor_framesize, cursor_nframes, cursor_size;
+	unsigned long framebuffer_size;
 	struct drm_vram_mm *vmm;
 	int ret;
 	struct drm_device *dev = mdev->dev;
 
+	/* At the high end of video memory, we reserve space for
+	 * two cursor images. The cursor plane uses this memory to
+	 * store a double-buffered image of the current cursor.
+	 */
+
+	cursor_framesize = roundup(64 * 48, 1024);
+	cursor_nframes = 2;
+	cursor_size = roundup(cursor_framesize * cursor_nframes, PAGE_SIZE);
+
+	framebuffer_size = (mdev->mc.vram_size - cursor_size) & PAGE_MASK;
+
 	vmm = drm_vram_helper_alloc_mm(dev, pci_resource_start(dev->pdev, 0),
-				       mdev->mc.vram_size);
+				       framebuffer_size);
 	if (IS_ERR(vmm)) {
 		ret = PTR_ERR(vmm);
 		DRM_ERROR("Error initializing VRAM MM; %d\n", ret);
Signature · 6 lines
2.23.0

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
author maintainer
#13 lore raw

PATCH 11/11 drm/mgag200: Implement cursor buffer with struct drm_vram_buffer

In reply to Thomas Zimmermann

The new VRAM buffer replaces all GEM buffer objects that contained cursor images. The buffer maintains the previously reserved memory at the high end of video RAM. The update() and set_base() now serve as callbacks for the VRAM buffer.

---
 drivers/gpu/drm/mgag200/mgag200_cursor.c | 124 +++++++----------------
 drivers/gpu/drm/mgag200/mgag200_drv.h    |  16 +--
 2 files changed, 38 insertions(+), 102 deletions(-)
drivers/gpu/drm/mgag200/mgag200_cursor.c +36 -88
index ab3dfe6bdd92..650013e488c2 100644
@@ -12,9 +12,12 @@
 static bool warn_transparent = true;
 static bool warn_palette = true;
 
-static int mgag200_cursor_update(struct mga_device *mdev, void *dst, void *src,
-				 unsigned int width, unsigned int height)
+static int mgag200_cursor_vram_buffer_update(struct drm_vram_buffer *vbuf,
+					     void *dst, void *src,
+					     unsigned int width,
+					     unsigned int height)
 {
+	struct mga_device *mdev = container_of(vbuf, struct mga_device, cursor);
 	struct drm_device *dev = mdev->dev;
 	unsigned int i, row, col;
 	uint32_t colour_set[16];
@@ -107,8 +110,10 @@ static int mgag200_cursor_update(struct mga_device *mdev, void *dst, void *src,
 	return 0;
 }
 
-static void mgag200_cursor_set_base(struct mga_device *mdev, u64 address)
+static void mgag200_cursor_vram_buffer_set_base(struct drm_vram_buffer *vbuf,
+						u64 address)
 {
+	struct mga_device *mdev = container_of(vbuf, struct mga_device, cursor);
 	u8 addrl = (address >> 10) & 0xff;
 	u8 addrh = (address >> 18) & 0x3f;
 
@@ -117,16 +122,20 @@ static void mgag200_cursor_set_base(struct mga_device *mdev, u64 address)
 	WREG_DAC(MGA1064_CURSOR_BASE_ADR_HI, addrh);
 }
 
-static int mgag200_show_cursor(struct mga_device *mdev, void *dst, void *src,
-			       unsigned int width, unsigned int height,
-			       u64 dst_gpu)
+static const struct drm_vram_buffer_funcs mgag200_cursor_vram_buffer_funcs = {
+	.update = mgag200_cursor_vram_buffer_update,
+	.set_base = mgag200_cursor_vram_buffer_set_base
+};
+
+static int mgag200_show_cursor(struct mga_device *mdev, void *src,
+			       unsigned int width, unsigned int height)
 {
 	int ret;
 
-	ret = mgag200_cursor_update(mdev, dst, src, width, height);
+	ret = drm_vram_buffer_update(&mdev->cursor, src, width, height);
 	if (ret)
 		return ret;
-	mgag200_cursor_set_base(mdev, dst_gpu);
+	drm_vram_buffer_swap(&mdev->cursor);
 
 	/* Adjust cursor control register to turn on the cursor */
 	WREG_DAC(MGA1064_CURSOR_CTL, 4); /* 16-colour palletized cursor mode */
@@ -141,9 +150,6 @@ static void mgag200_hide_cursor(struct mga_device *mdev)
 {
 	WREG8(MGA_CURPOSXL, 0);
 	WREG8(MGA_CURPOSXH, 0);
-	if (mdev->cursor.pixels_current)
-		drm_gem_vram_unpin(mdev->cursor.pixels_current);
-	mdev->cursor.pixels_current = NULL;
 }
 
 static void mgag200_move_cursor(struct mga_device *mdev, int x, int y)
@@ -167,58 +173,39 @@ static void mgag200_move_cursor(struct mga_device *mdev, int x, int y)
 int mgag200_cursor_init(struct mga_device *mdev)
 {
 	struct drm_device *dev = mdev->dev;
+	unsigned long base, frame_size, nframes, size;
+	int ret;
 
-	/*
-	 * Make small buffers to store a hardware cursor (double
-	 * buffered icon updates)
-	 */
-	mdev->cursor.pixels_1 = drm_gem_vram_create(dev, &dev->vram_mm->bdev,
-						    roundup(48*64, PAGE_SIZE),
-						    0, 0);
-	mdev->cursor.pixels_2 = drm_gem_vram_create(dev, &dev->vram_mm->bdev,
-						    roundup(48*64, PAGE_SIZE),
-						    0, 0);
-	if (IS_ERR(mdev->cursor.pixels_2) || IS_ERR(mdev->cursor.pixels_1)) {
-		mdev->cursor.pixels_1 = NULL;
-		mdev->cursor.pixels_2 = NULL;
-		dev_warn(&dev->pdev->dev,
-			"Could not allocate space for cursors. Not doing hardware cursors.\n");
-	}
-	mdev->cursor.pixels_current = NULL;
+	base = mdev->mc.vram_base + dev->vram_mm->vram_size;
+	frame_size = roundup(64 * 48, 1024);
+	nframes = 2;
+	size = roundup(frame_size * nframes, PAGE_SIZE);
+
+	ret = drm_vram_buffer_init(&mdev->cursor, base, size, frame_size,
+				   nframes, &mgag200_cursor_vram_buffer_funcs);
+	if (ret)
+		return ret;
 
 	return 0;
 }
 
 void mgag200_cursor_fini(struct mga_device *mdev)
-{ }
+{
+	drm_vram_buffer_cleanup(&mdev->cursor);
+}
 
 int mgag200_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
 			    uint32_t handle, uint32_t width, uint32_t height)
 {
 	struct drm_device *dev = crtc->dev;
 	struct mga_device *mdev = (struct mga_device *)dev->dev_private;
-	struct drm_gem_vram_object *pixels_1 = mdev->cursor.pixels_1;
-	struct drm_gem_vram_object *pixels_2 = mdev->cursor.pixels_2;
-	struct drm_gem_vram_object *pixels_current = mdev->cursor.pixels_current;
-	struct drm_gem_vram_object *pixels_next;
 	struct drm_gem_object *obj;
 	struct drm_gem_vram_object *gbo = NULL;
 	int ret;
-	u8 *src, *dst;
-	s64 gpu_addr;
-	u64 dst_gpu;
+	u8 *src;
 
-	if (!pixels_1 || !pixels_2) {
-		WREG8(MGA_CURPOSXL, 0);
-		WREG8(MGA_CURPOSXH, 0);
+	if (!mdev->cursor.mem)
 		return -ENOTSUPP; /* Didn't allocate space for cursors */
-	}
-
-	if (WARN_ON(pixels_current &&
-		    pixels_1 != pixels_current &&
-		    pixels_2 != pixels_current)) {
-		return -ENOTSUPP; /* inconsistent state */
-	}
 
 	if (!handle || !file_priv) {
 		mgag200_hide_cursor(mdev);
@@ -226,16 +213,10 @@ int mgag200_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
 	}
 
 	if (width != 64 || height != 64) {
-		WREG8(MGA_CURPOSXL, 0);
-		WREG8(MGA_CURPOSXH, 0);
+		mgag200_hide_cursor(mdev);
 		return -EINVAL;
 	}
 
-	if (pixels_current == pixels_1)
-		pixels_next = pixels_2;
-	else
-		pixels_next = pixels_1;
-
 	obj = drm_gem_object_lookup(file_priv, handle);
 	if (!obj)
 		return -ENOENT;
@@ -248,48 +229,15 @@ int mgag200_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
 		goto err_drm_gem_object_put_unlocked;
 	}
 
-	/* Pin and map up-coming buffer to write colour indices */
-	ret = drm_gem_vram_pin(pixels_next, DRM_GEM_VRAM_PL_FLAG_VRAM);
-	if (ret) {
-		dev_err(&dev->pdev->dev,
-			"failed to pin cursor buffer: %d\n", ret);
-		goto err_drm_gem_vram_vunmap;
-	}
-	dst = drm_gem_vram_kmap(pixels_next, true, NULL);
-	if (IS_ERR(dst)) {
-		ret = PTR_ERR(dst);
-		dev_err(&dev->pdev->dev,
-			"failed to kmap cursor updates: %d\n", ret);
-		goto err_drm_gem_vram_unpin_dst;
-	}
-	gpu_addr = drm_gem_vram_offset(pixels_next);
-	if (gpu_addr < 0) {
-		ret = (int)gpu_addr;
-		dev_err(&dev->pdev->dev,
-			"failed to get cursor scanout address: %d\n", ret);
-		goto err_drm_gem_vram_kunmap_dst;
-	}
-	dst_gpu = (u64)gpu_addr;
-
-	ret = mgag200_show_cursor(mdev, dst, src, width, height, dst_gpu);
+	ret = mgag200_show_cursor(mdev, src, width, height);
 	if (ret)
-		goto err_drm_gem_vram_kunmap_dst;
-
-	/* Now update internal buffer pointers */
-	if (pixels_current)
-		drm_gem_vram_unpin(pixels_current);
-	mdev->cursor.pixels_current = pixels_next;
+		goto err_drm_gem_vram_vunmap;
 
-	drm_gem_vram_kunmap(pixels_next);
 	drm_gem_vram_vunmap(gbo, src);
 	drm_gem_object_put_unlocked(obj);
 
 	return 0;
 
-err_drm_gem_vram_kunmap_dst:
-	drm_gem_vram_kunmap(pixels_next);
-err_drm_gem_vram_unpin_dst:
-	drm_gem_vram_unpin(pixels_next);
 err_drm_gem_vram_vunmap:
 	drm_gem_vram_vunmap(gbo, src);
 err_drm_gem_object_put_unlocked:
drivers/gpu/drm/mgag200/mgag200_drv.h +2 -14
index 01243fa6397c..7ced98d61c22 100644
@@ -19,6 +19,7 @@
 #include <drm/drm_fb_helper.h>
 #include <drm/drm_gem.h>
 #include <drm/drm_gem_vram_helper.h>
+#include <drm/drm_vram_helper.h>
 
 #include "mgag200_reg.h"
 
@@ -128,19 +129,6 @@ struct mga_connector {
 	struct mga_i2c_chan *i2c;
 };
 
-struct mga_cursor {
-	/*
-	   We have to have 2 buffers for the cursor to avoid occasional
-	   corruption while switching cursor icons.
-	   If either of these is NULL, then don't do hardware cursors, and
-	   fall back to software.
-	*/
-	struct drm_gem_vram_object *pixels_1;
-	struct drm_gem_vram_object *pixels_2;
-	/* The currently displayed icon, this points to one of pixels_1, or pixels_2 */
-	struct drm_gem_vram_object *pixels_current;
-};
-
 struct mga_mc {
 	resource_size_t			vram_size;
 	resource_size_t			vram_base;
@@ -171,7 +159,7 @@ struct mga_device {
 	struct mga_mc			mc;
 	struct mga_mode_info		mode_info;
 
-	struct mga_cursor cursor;
+	struct drm_vram_buffer cursor;
 
 	bool				suspended;
 	int				num_crtc;
Signature · 6 lines
2.23.0

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel