[PATCH 1/3] drm/sitronix: st7920: Use DMA-safe serialized TX buffer for SPI writes

From: Elkin Cruz

Date: Wed Nov 25 2026 - 17:26:18 EST


In st7920_spi_write(), u8 reg[CMD_SIZE] is allocated on the stack and
passed directly to spi_write(), violating SPI DMA safety requirements
when CONFIG_VMAP_STACK is enabled or an IOMMU is in use.

Embed an ARCH_DMA_MINALIGN-aligned TX buffer at the end of
struct st7920_device protected by a DRM-managed mutex, and pass
st7920 to st7920_spi_write().

Fixes: 88b09229f34a ("drm: Add driver for Sitronix ST7920 LCD displays")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Elkin Cruz <elkin@xxxxxxxxxx>
---
drivers/gpu/drm/sitronix/st7920.c | 43 +++++++++++++++++++++----------
1 file changed, 30 insertions(+), 13 deletions(-)

diff --git a/drivers/gpu/drm/sitronix/st7920.c b/drivers/gpu/drm/sitronix/st7920.c
index 616d83387d31..091e80d8bda5 100644
--- a/drivers/gpu/drm/sitronix/st7920.c
+++ b/drivers/gpu/drm/sitronix/st7920.c
@@ -7,9 +7,12 @@
*/

#include <linux/bitrev.h>
+#include <linux/cache.h>
+#include <linux/cleanup.h>
#include <linux/delay.h>
#include <linux/gpio/consumer.h>
#include <linux/module.h>
+#include <linux/mutex.h>
#include <linux/regmap.h>
#include <linux/spi/spi.h>

@@ -24,6 +27,7 @@
#include <drm/drm_gem_atomic_helper.h>
#include <drm/drm_gem_framebuffer_helper.h>
#include <drm/drm_gem_shmem_helper.h>
+#include <drm/drm_managed.h>
#include <drm/drm_plane.h>
#include <drm/drm_print.h>
#include <drm/drm_probe_helper.h>
@@ -86,6 +90,10 @@ struct st7920_device {

u32 height;
u32 width;
+
+ /* Serializes SPI command and GDRAM transfers using tx_buf */
+ struct mutex tx_lock;
+ u8 tx_buf[CMD_SIZE] __aligned(ARCH_DMA_MINALIGN);
};

struct st7920_plane_state {
@@ -165,16 +173,21 @@ static int st7920_store_others(int cmd, const void *data, u8 *reg)
return i;
}

-static void st7920_spi_write(struct spi_device *spi, int cmd, const void *data,
- int delay_us, struct spi7920_error *err)
+static void st7920_spi_write(struct st7920_device *st7920, int cmd,
+ const void *data, int delay_us,
+ struct spi7920_error *err)
{
- u8 reg[CMD_SIZE] = {0};
+ u8 *reg = st7920->tx_buf;
int size = 0;
int ret;

if (err->errno)
return;

+ guard(mutex)(&st7920->tx_lock);
+
+ memset(reg, 0, CMD_SIZE);
+
/*
* First the sync bits are sent: 11111WS0.
* Where W is the read/write (RW) bit and S is the register/data (RS) bit.
@@ -190,7 +203,7 @@ static void st7920_spi_write(struct spi_device *spi, int cmd, const void *data,
else
size = st7920_store_others(cmd, data, reg);

- ret = spi_write(spi, reg, size);
+ ret = spi_write(st7920->spi, reg, size);
if (ret) {
err->errno = ret;
return;
@@ -232,14 +245,14 @@ MODULE_DEVICE_TABLE(spi, st7920_spi_id);
static void st7920_power_on(struct st7920_device *st7920,
struct spi7920_error *err)
{
- st7920_spi_write(st7920->spi, SET_DISPLAY_ON, NULL, 72, err);
+ st7920_spi_write(st7920, SET_DISPLAY_ON, NULL, 72, err);
}

static void st7920_power_off(struct st7920_device *st7920,
struct spi7920_error *err)
{
- st7920_spi_write(st7920->spi, SET_DISPLAY_CLEAR, NULL, 1600, err);
- st7920_spi_write(st7920->spi, SET_DISPLAY_OFF, NULL, 72, err);
+ st7920_spi_write(st7920, SET_DISPLAY_CLEAR, NULL, 1600, err);
+ st7920_spi_write(st7920, SET_DISPLAY_OFF, NULL, 72, err);
}

static void st7920_hw_reset(struct st7920_device *st7920)
@@ -257,10 +270,10 @@ static int st7920_init(struct st7920_device *st7920)
{
struct spi7920_error err = {0};

- st7920_spi_write(st7920->spi, SET_BASIC_INSTRUCTION_SET, NULL, 72, &err);
+ st7920_spi_write(st7920, SET_BASIC_INSTRUCTION_SET, NULL, 72, &err);
st7920_power_on(st7920, &err);
- st7920_spi_write(st7920->spi, SET_GRAPHICS_DISPLAY, NULL, 72, &err);
- st7920_spi_write(st7920->spi, SET_DISPLAY_CLEAR, NULL, 1600, &err);
+ st7920_spi_write(st7920, SET_GRAPHICS_DISPLAY, NULL, 72, &err);
+ st7920_spi_write(st7920, SET_DISPLAY_CLEAR, NULL, 1600, &err);

return err.errno;
}
@@ -296,8 +309,8 @@ static int st7920_update_rect(struct st7920_device *st7920,
data_array[array_idx++] = line_buffer[j];
}

- st7920_spi_write(st7920->spi, SET_GDRAM_ADDRESS, &i, 72, &err);
- st7920_spi_write(st7920->spi, SET_GDRAM_DATA, line_buffer, 72, &err);
+ st7920_spi_write(st7920, SET_GDRAM_ADDRESS, &i, 72, &err);
+ st7920_spi_write(st7920, SET_GDRAM_DATA, line_buffer, 72, &err);
}

return err.errno;
@@ -309,7 +322,7 @@ static void st7920_clear_screen(struct st7920_device *st7920, u8 *data_array)

memset(data_array, 0, BYTES_IN_DISPLAY);

- st7920_spi_write(st7920->spi, SET_DISPLAY_CLEAR, NULL, 1600, &err);
+ st7920_spi_write(st7920, SET_DISPLAY_CLEAR, NULL, 1600, &err);
}

static int st7920_fb_blit_rect(struct drm_framebuffer *fb,
@@ -812,6 +825,10 @@ static int st7920_probe(struct spi_device *spi)
st7920->width = ST7920_DEFAULT_WIDTH;
st7920->height = ST7920_DEFAULT_HEIGHT;

+ ret = drmm_mutex_init(drm, &st7920->tx_lock);
+ if (ret)
+ return ret;
+
st7920->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW);
if (IS_ERR(st7920->reset_gpio)) {
ret = PTR_ERR(st7920->reset_gpio);
--
2.56.1.839.g22a8290db9-goog