From 8c638140c97c945c843c489e86f57ac4a889d74d Mon Sep 17 00:00:00 2001 From: Developerr86 <138863457+Developerr86@users.noreply.github.com> Date: Fri, 4 Dec 2026 04:45:12 +0000 Subject: [PATCH 1/2] drm/tiny: simpledrm: Gather turned tiles with NEON At native resolution the iPad 3's simpledrm spent 73.6 ms on each full-screen update (23 ns a pixel at 1 GHz), most of it in the scalar gather that builds each scanout run one pixel at a time. On ARM with kernel-mode NEON and simpledrm built in, the unzoomed rotation-90 case now gathers a tile in 4x4 blocks instead: four upright rows loaded, transposed with VTRN, and stored as four runs of a scanout row. Other cases, partial blocks and contexts where may_use_simd() is false keep the scalar loop. simpledrm_neon.c includes only the compiler's arm_neon.h, because its stdint.h and the kernel's types do not mix (as in crypto/aegis128-neon-inner.c). It is therefore built in only, with CC_FLAGS_FPU. Checked against the scalar mapping on 2000 random tiles under QEMU. Assisted-by: LLM Signed-off-by: Developerr86 <138863457+Developerr86@users.noreply.github.com> --- drivers/gpu/drm/tiny/Makefile | 6 ++++ drivers/gpu/drm/tiny/simpledrm.c | 36 ++++++++++++++++++++ drivers/gpu/drm/tiny/simpledrm_neon.c | 47 +++++++++++++++++++++++++++ drivers/gpu/drm/tiny/simpledrm_neon.h | 16 +++++++++ 4 files changed, 105 insertions(+) create mode 100644 drivers/gpu/drm/tiny/simpledrm_neon.c create mode 100644 drivers/gpu/drm/tiny/simpledrm_neon.h diff --git a/drivers/gpu/drm/tiny/Makefile b/drivers/gpu/drm/tiny/Makefile index 76dde89a044b7d..8c94b61d99be71 100644 --- a/drivers/gpu/drm/tiny/Makefile +++ b/drivers/gpu/drm/tiny/Makefile @@ -7,6 +7,12 @@ obj-$(CONFIG_DRM_GM12U320) += gm12u320.o obj-$(CONFIG_DRM_OFDRM) += ofdrm.o obj-$(CONFIG_DRM_PANEL_MIPI_DBI) += panel-mipi-dbi.o obj-$(CONFIG_DRM_SIMPLEDRM) += simpledrm.o +# built in only (no exports from a file that cannot include kernel headers) +ifeq ($(CONFIG_DRM_SIMPLEDRM)$(CONFIG_ARM)$(CONFIG_KERNEL_MODE_NEON),yyy) +obj-y += simpledrm_neon.o +CFLAGS_simpledrm_neon.o += $(CC_FLAGS_FPU) +CFLAGS_REMOVE_simpledrm_neon.o += -mgeneral-regs-only +endif obj-$(CONFIG_TINYDRM_HX8357D) += hx8357d.o obj-$(CONFIG_TINYDRM_ILI9163) += ili9163.o obj-$(CONFIG_TINYDRM_ILI9225) += ili9225.o diff --git a/drivers/gpu/drm/tiny/simpledrm.c b/drivers/gpu/drm/tiny/simpledrm.c index 85b3678335baa2..c3447eace3ab57 100644 --- a/drivers/gpu/drm/tiny/simpledrm.c +++ b/drivers/gpu/drm/tiny/simpledrm.c @@ -251,6 +251,8 @@ struct simpledrm_device { unsigned int scan_width, scan_height; unsigned int zoom; u32 *row; + /* one turned tile, SIMPLEDRM_TILE squared, for the NEON copy */ + u32 *tile; /* memory management */ struct iosys_map screen_base; @@ -631,6 +633,16 @@ static int simpledrm_primary_plane_helper_atomic_check(struct drm_plane *plane, #define SIMPLEDRM_TILE 32 +#if IS_BUILTIN(CONFIG_DRM_SIMPLEDRM) && IS_ENABLED(CONFIG_ARM) && \ + IS_ENABLED(CONFIG_KERNEL_MODE_NEON) +#include +#include +#include "simpledrm_neon.h" +#define SIMPLEDRM_NEON 1 +#else +#define SIMPLEDRM_NEON 0 +#endif + /* * A turned scanout can also be presented at a fraction of its size, each * pixel then copied as a ZOOM x ZOOM block: compositors and clients without a @@ -662,6 +674,28 @@ static void simpledrm_blit_rotated(struct simpledrm_device *sdev, const u8 *src, for (tx = clip->x1; tx < clip->x2; tx += SIMPLEDRM_TILE) { unsigned int x2 = min_t(unsigned int, tx + SIMPLEDRM_TILE, clip->x2); +#if SIMPLEDRM_NEON + /* the common case, unzoomed and turned 90, in 4x4 blocks */ + if (z == 1 && sdev->rotation == 90 && sdev->tile && !((x2 - tx) & 3) && + !(count & 3) && may_use_simd()) { + unsigned int r; + + kernel_neon_begin(); + simpledrm_neon_turn90_tile(sdev->tile, SIMPLEDRM_TILE, + src + ty * src_pitch + tx * 4, src_pitch, + x2 - tx, count); + kernel_neon_end(); + for (r = 0; r < x2 - tx; r++) { + size_t off = (tx + r) * sdev->pitch + + (sdev->scan_width - y2) * 4; + + iosys_map_memcpy_to(&sdev->screen_base, off, + sdev->tile + r * SIMPLEDRM_TILE, + count * 4); + } + continue; + } +#endif for (x = tx; x < x2; x++) { const u8 *px = src + x * 4; long step = src_pitch; @@ -962,6 +996,8 @@ static struct simpledrm_device *simpledrm_device_create(struct drm_driver *drv, sdev->rotation = 0; } if (sdev->rotation) { + sdev->tile = devm_kmalloc_array(&pdev->dev, SIMPLEDRM_TILE * SIMPLEDRM_TILE, + sizeof(u32), GFP_KERNEL); sdev->row = devm_kmalloc_array(&pdev->dev, max(width, height), sizeof(u32), GFP_KERNEL); if (!sdev->row) diff --git a/drivers/gpu/drm/tiny/simpledrm_neon.c b/drivers/gpu/drm/tiny/simpledrm_neon.c new file mode 100644 index 00000000000000..2d82b10938b180 --- /dev/null +++ b/drivers/gpu/drm/tiny/simpledrm_neon.c @@ -0,0 +1,47 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * simpledrm's turned copy, the gathering half, with NEON: four upright rows + * of four pixels loaded, transposed in registers, and stored as four runs of + * a scanout row. The scalar loop spent about 23 cycles a pixel on an A5X + * (Cortex-A9) at 1536x2048; this does a 4x4 block in a handful of + * instructions. Built with the kernel's CC_FLAGS_FPU, called between + * kernel_neon_begin() and kernel_neon_end(). + */ + +/* + * Only the compiler's arm_neon.h here: its stdint.h and the kernel's types do + * not mix, as in crypto/aegis128-neon-inner.c. uint32_t is u32 on ARM. + */ +#include + +void simpledrm_neon_turn90_tile(uint32_t *dst, unsigned int dst_stride, const uint8_t *src, + unsigned int src_pitch, unsigned int w, unsigned int h); + +void simpledrm_neon_turn90_tile(uint32_t *dst, unsigned int dst_stride, const uint8_t *src, + unsigned int src_pitch, unsigned int w, unsigned int h) +{ + unsigned int bx, by, i; + + for (by = 0; by < h; by += 4) { + /* the block's rows bottom up: lane order is then the scanout's */ + const uint8_t *r3 = src + (by + 3) * src_pitch, *r2 = src + (by + 2) * src_pitch; + const uint8_t *r1 = src + (by + 1) * src_pitch, *r0 = src + by * src_pitch; + uint32_t *out = dst + (h - 4 - by); + + for (bx = 0; bx < w; bx += 4) { + uint32x4_t a = vld1q_u32((const uint32_t *)(r3 + bx * 4)); + uint32x4_t b = vld1q_u32((const uint32_t *)(r2 + bx * 4)); + uint32x4_t c = vld1q_u32((const uint32_t *)(r1 + bx * 4)); + uint32x4_t d = vld1q_u32((const uint32_t *)(r0 + bx * 4)); + uint32x4x2_t ab = vtrnq_u32(a, b), cd = vtrnq_u32(c, d); + uint32x4_t col[4]; + + col[0] = vcombine_u32(vget_low_u32(ab.val[0]), vget_low_u32(cd.val[0])); + col[1] = vcombine_u32(vget_low_u32(ab.val[1]), vget_low_u32(cd.val[1])); + col[2] = vcombine_u32(vget_high_u32(ab.val[0]), vget_high_u32(cd.val[0])); + col[3] = vcombine_u32(vget_high_u32(ab.val[1]), vget_high_u32(cd.val[1])); + for (i = 0; i < 4; i++) + vst1q_u32(out + (bx + i) * dst_stride, col[i]); + } + } +} diff --git a/drivers/gpu/drm/tiny/simpledrm_neon.h b/drivers/gpu/drm/tiny/simpledrm_neon.h new file mode 100644 index 00000000000000..3fcf1f8f4f7a6d --- /dev/null +++ b/drivers/gpu/drm/tiny/simpledrm_neon.h @@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef SIMPLEDRM_NEON_H +#define SIMPLEDRM_NEON_H + +#include + +/* + * One tile of a scanout turned 90 degrees, through NEON 4x4 transposes: + * dst[r * dst_stride + j] = upright pixel (r, h - 1 - j) of the w x h tile at + * src (XRGB8888, src_pitch bytes a line), w and h multiples of 4. Between + * kernel_neon_begin() and kernel_neon_end(). + */ +void simpledrm_neon_turn90_tile(u32 *dst, unsigned int dst_stride, const u8 *src, + unsigned int src_pitch, unsigned int w, unsigned int h); + +#endif From 82cf64268d41c707ba0bf57282b206810977feb6 Mon Sep 17 00:00:00 2001 From: Developerr86 <138863457+Developerr86@users.noreply.github.com> Date: Fri, 4 Dec 2026 17:13:03 +0000 Subject: [PATCH 2/2] drm/tiny: simpledrm: Offer a turned scanout in its own shape too A turned scanout was offered only upright. Offer its own shape as a second mode, copied unturned, so that a compositor can put the picture on its side by a mode change instead of turning every frame itself, which costs pixman about three times as much per frame here. Assisted-by: LLM Signed-off-by: Developerr86 <138863457+Developerr86@users.noreply.github.com> [Orchard: dropped the flip parameter; rotation should use the plane rotation property] Signed-off-by: Henrik Klee --- drivers/gpu/drm/tiny/simpledrm.c | 80 +++++++++++++++++++++++++++++--- 1 file changed, 74 insertions(+), 6 deletions(-) diff --git a/drivers/gpu/drm/tiny/simpledrm.c b/drivers/gpu/drm/tiny/simpledrm.c index c3447eace3ab57..e2fa3afeaa42b6 100644 --- a/drivers/gpu/drm/tiny/simpledrm.c +++ b/drivers/gpu/drm/tiny/simpledrm.c @@ -249,6 +249,12 @@ struct simpledrm_device { */ unsigned int rotation; unsigned int scan_width, scan_height; + /* + * A turned scanout is also offered in its own shape (mode_other, the + * mode's width and height swapped), copied unturned, so that the + * picture can be put on its side without the compositor turning it. + */ + struct drm_display_mode mode_other; unsigned int zoom; u32 *row; /* one turned tile, SIMPLEDRM_TILE squared, for the NEON copy */ @@ -652,6 +658,35 @@ static unsigned int simpledrm_zoom = 1; module_param_named(zoom, simpledrm_zoom, uint, 0444); MODULE_PARM_DESC(zoom, "Present a turned scanout at 1/zoom size, pixels enlarged in the copy (1 or 2)"); +/* + * Copy CLIP of the XRGB8888 framebuffer SRC to the scanout in its own shape, + * unturned. At zoom z each pixel is a z x z block, as in + * simpledrm_blit_rotated. + */ +static void simpledrm_blit_straight(struct simpledrm_device *sdev, + const u8 *src, unsigned int src_pitch, + const struct drm_rect *clip) +{ + unsigned int z = sdev->zoom, w = clip->x2 - clip->x1, y, i, k; + + for (y = clip->y1; y < clip->y2; y++) { + const u32 *px = (const u32 *)(src + y * src_pitch) + clip->x1; + + if (z == 1) { + iosys_map_memcpy_to(&sdev->screen_base, y * sdev->pitch + clip->x1 * 4, + px, w * 4); + continue; + } + for (i = 0; i < w; i++) + for (k = 0; k < z; k++) + sdev->row[i * z + k] = px[i]; + for (k = 0; k < z; k++) + iosys_map_memcpy_to(&sdev->screen_base, + (y * z + k) * sdev->pitch + clip->x1 * z * 4, + sdev->row, w * z * 4); + } +} + /* * Copy CLIP of the upright XRGB8888 framebuffer SRC (pitch SRC_PITCH) to the * scanout, turned. For rotation 90 the upright pixel (x, y) is at scanout @@ -734,6 +769,7 @@ static void simpledrm_primary_plane_helper_atomic_update(struct drm_plane *plane struct simpledrm_device *sdev = simpledrm_device_of_dev(dev); struct drm_atomic_helper_damage_iter iter; struct drm_rect damage; + bool other = false; int ret, idx; ret = drm_gem_fb_begin_cpu_access(fb, DMA_FROM_DEVICE); @@ -743,6 +779,10 @@ static void simpledrm_primary_plane_helper_atomic_update(struct drm_plane *plane if (!drm_dev_enter(dev, &idx)) goto out_drm_gem_fb_end_cpu_access; + if (sdev->rotation) + other = drm_rect_width(&plane_state->dst) == sdev->mode_other.hdisplay && + drm_rect_height(&plane_state->dst) == sdev->mode_other.vdisplay; + drm_atomic_helper_damage_iter_init(&iter, old_plane_state, plane_state); drm_atomic_for_each_plane_damage(&iter, &damage) { struct drm_rect dst_clip = plane_state->dst; @@ -752,11 +792,15 @@ static void simpledrm_primary_plane_helper_atomic_update(struct drm_plane *plane continue; if (sdev->rotation) { + const u8 *src = shadow_plane_state->data[0].vaddr; + /* only the native format is offered when rotated */ if (fb->format != sdev->format || shadow_plane_state->data[0].is_iomem) continue; - simpledrm_blit_rotated(sdev, shadow_plane_state->data[0].vaddr, - fb->pitches[0], &damage); + if (other) + simpledrm_blit_straight(sdev, src, fb->pitches[0], &damage); + else + simpledrm_blit_rotated(sdev, src, fb->pitches[0], &damage); continue; } @@ -822,7 +866,11 @@ static enum drm_mode_status simpledrm_crtc_helper_mode_valid(struct drm_crtc *cr { struct simpledrm_device *sdev = simpledrm_device_of_dev(crtc->dev); - return drm_crtc_helper_mode_valid_fixed(crtc, mode, &sdev->mode); + enum drm_mode_status status = drm_crtc_helper_mode_valid_fixed(crtc, mode, &sdev->mode); + + if (status != MODE_OK && sdev->rotation) + status = drm_crtc_helper_mode_valid_fixed(crtc, mode, &sdev->mode_other); + return status; } /* @@ -852,7 +900,20 @@ static int simpledrm_connector_helper_get_modes(struct drm_connector *connector) { struct simpledrm_device *sdev = simpledrm_device_of_dev(connector->dev); - return drm_connector_helper_get_modes_fixed(connector, &sdev->mode); + struct drm_display_mode *mode; + int count; + + count = drm_connector_helper_get_modes_fixed(connector, &sdev->mode); + if (!sdev->rotation) + return count; + /* the scanout's own shape, after the upright (preferred) one */ + mode = drm_mode_duplicate(connector->dev, &sdev->mode_other); + if (!mode) + return count; + mode->type |= DRM_MODE_TYPE_DRIVER; + drm_mode_set_name(mode); + drm_mode_probed_add(connector, mode); + return count + 1; } static const struct drm_connector_helper_funcs simpledrm_connector_helper_funcs = { @@ -1020,6 +1081,8 @@ static struct simpledrm_device *simpledrm_device_create(struct drm_driver *drv, drm_warn(dev, "zoom needs a turned scanout; ignored\n"); } sdev->mode = simpledrm_mode(width, height, width_mm, height_mm); + if (sdev->rotation) + sdev->mode_other = simpledrm_mode(height, width, height_mm, width_mm); sdev->format = format; sdev->pitch = stride; @@ -1092,9 +1155,14 @@ static struct simpledrm_device *simpledrm_device_create(struct drm_driver *drv, max_width = max_t(unsigned long, width, DRM_SHADOW_PLANE_MAX_WIDTH); max_height = max_t(unsigned long, height, DRM_SHADOW_PLANE_MAX_HEIGHT); - dev->mode_config.min_width = width; + if (sdev->rotation) { + /* both shapes: the upright mode and the scanout's own */ + max_width = max_t(unsigned long, max(width, height), DRM_SHADOW_PLANE_MAX_WIDTH); + max_height = max_t(unsigned long, max(width, height), DRM_SHADOW_PLANE_MAX_HEIGHT); + } + dev->mode_config.min_width = sdev->rotation ? min(width, height) : width; dev->mode_config.max_width = max_width; - dev->mode_config.min_height = height; + dev->mode_config.min_height = sdev->rotation ? min(width, height) : height; dev->mode_config.max_height = max_height; dev->mode_config.preferred_depth = format->depth; dev->mode_config.funcs = &simpledrm_mode_config_funcs;