From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1795955577; b=mo/tujW/ORrGgvm9PFKeiOykNuhDU27O5zQdwYEMGaLupeQIiqXGqE8/rPgG5tblzsA/ 12sIGkHfSqLoF5h0/TC8zgwnBHeh2e6uDWpdG5bHt+ypnCdDbjSVD6FbO+7H9Ke3fygZN KMUIgw+iUKRQb3++OqhVvkxTweo4yUVFCPnWWzhvvhkLhaoFVo7eusVq7ywwiGhzY7PVY yGOvKoh3w5ktqAzsDfVwoMgPXkLKgvPcPwsY10vd2ErsOZE/GEmhoF5fjS3EZpt1ueHYt fWxw4CEMKvrQ6CdGDMl5CmuzYlk+/n42CMoZ74RpCfFrAc3pEVhVjbLU3qcTuEc7qOQ== ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795955577; h=Received:DKIM-Signature:Received:From:Date:Subject:MIME-Version: Content-Type:Content-Transfer-Encoding:Message-Id:References: In-Reply-To:To:Cc:X-Mailer:X-Developer-Signature:X-Developer-Key: X-Endpoint-Received:X-Original-From:Reply-To; bh=/pB1cA/a4fwj6ncEkPzBPPPFNvhA49MTPNhihJh4Mns=; b=VlVvUQoYB/jwIhCzVXj+WpLwkmQQ9uEahOcOl2u+TbwUAvcQt92FLbkTRfT1/YW1Z5Th WANNdS0kfrGd9+n97Vw7uOgagM4LQFEf6PkXYgboGtYhIUwkS814DRzzRUpxXPqi8P1d8 kYToGeSlovOZWhDq/H6I/iT1ffuk3FEiycWJq1sO1YJSWmAVd17Tx0eKEcMExw7VXmJu0 1l+x5DwGnRCKJoNjEaf5Z/xbkdMjJUqXahVSIeync6MFFe32e0oQku/nqhpwFYCc5gVzD pGOHGcXZOy70V5gN7I4NC5gaxwrsgXcxTf7af+E5fLKOIzEL4oF1Q1+ibiExCBg6bkQ== ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b="jLZ/ucCM"; arc=pass header.oldest-pass=0 smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B05BF606A687; Sun, 29 Nov 2026 12:32:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=jLZ/ucCM Authentication-Results: smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 ARC-Seal: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; cv=none; t=1795955577; b=kbqc6RO1Du7zIxxUA67wLCuZQKt5TA6L2z2BbrSggSNBA7Rh3JzoSXaZH72F7Z+Rrj34 YIQCLpTtO8xyI2RrjYWoXfl/8h0zQtYXapr2ACZJrtTUnFP5v1sYYnufMtL4gVIA60FEB HoJzDAvhSMFnukXTTD2lbbACaW58mlHxWl4NSByWzrhl7nWRL4bdBXm3Fb13u6VR3QsNQ OYpRSI+kQvKkA96zhM8/GbJ2zm5fPO+loQGAsIakLL1pUbpfRYduKcji0moRRSrEYvNsk WEGpmZcWnnjhUbrigcH2d7j3NdqGJoP61Ifo8A7UwItZpWyoCtQo81VavpbRDlp1nyg== ARC-Message-Signature: i=1; d=kernel.org; s=arc20260519; a=rsa-sha256; c=relaxed/relaxed; t=1795955577; h=DKIM-Signature:Received:From:Date:Subject:MIME-Version:Content-Type: Content-Transfer-Encoding:Message-Id:References:In-Reply-To:To:Cc: X-Mailer:X-Developer-Signature:X-Developer-Key:X-Endpoint-Received: X-Original-From:Reply-To; bh=/pB1cA/a4fwj6ncEkPzBPPPFNvhA49MTPNhihJh4Mns=; b=iwkS90MFBxte72dHh9YEoyLq4s6/DTj5TbiVyfRQlbEHbO9ZzZYr2UunvAULcVPljqGA AuIJy70qRmfM0J7AW69UcsifZC4e+NIa9Qi1yn+ctn62nBJQ13kL4k19XkfeG3s760aiW OArZ9+Q6dIj7JsflNaUTRmB1hMzSvDuFZxSmvN4GV3R6fZ+WwGrVUAsM82rDluJAEx0LA qW0XQy70H9pQukR3dxQ+woRZKyVX7jD9pStW7wRhjhxCrAdcdP2BiV4u9+e7Zt7SEDfYM /6TRt597PPXMSiOwVd2NKlzNCWyTQfxWWPPRCepVtjESjxiP88qLTQo5BXfdw4wD7VQ== ARC-Authentication-Results: i=1; smtp.kernel.org; arc=none smtp.remote-ip=100.80.115.77 Received: from lkml.kernel.org (nexus.subspace.kernel.org [100.80.115.77]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.kernel.org (Postfix) with ESMTPS id EA6B41F00898; Sun, 29 Nov 2026 12:32:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795955577; bh=/pB1cA/a4fwj6ncEkPzBPPPFNvhA49MTPNhihJh4Mns=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To; b=jLZ/ucCMdg575+heSmNLhSjNdA1w+N/T3JN9/jesBp5t1SaSM+GmbWD6G/AATny61 sGIATYkgzJ0YlD4dmWuhAutlU4IHX3R9fXl++Oi5ltVHV5hBXwS9jWYs1xVuo9wOiY 6ywLEf5+BwUs9C9ZLgyjCPW4V06n/5FF0s0UMlL2/Ydl2LjiQc6+T+W0KDsZVCsNmq tOh2j1V2DnrQT5OFWybd+GIBICOY+InM9oedzSEeFM+3ZEgMSSltZrvsAfDIWblTNV awU23XzuThQQguotSi9aRvpKf4dichbnf9jLFr2bk7LdCGwrW4FoWqfU2pjbXIBm2V 5uqQE93fav0pw== Received: from nexus.subspace.kernel.org (localhost [IPv6:::1]) by lkml.kernel.org (Postfix) with ESMTP id 9E61A4E4A5; Sun, 29 Nov 2026 12:32:30 +0000 (UTC) From: Cody Kang via B4 Relay Date: Sun, 29 Nov 2026 20:32:21 +0800 Subject: [PATCH v4 09/16] drm/spacemit: add Saturn DPU KMS pipeline X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261129-k3-display-v4-9-ce6cf7988202@gmail.com> References: <20261129-k3-display-v4-0-ce6cf7988202@gmail.com> In-Reply-To: <20261129-k3-display-v4-0-ce6cf7988202@gmail.com> To: David Airlie , Simona Vetter , Maarten Lankhorst , Maxime Ripard , Thomas Zimmermann , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Yixun Lan , Troy Mitchell , Vinod Koul , Neil Armstrong , Manivannan Sadhasivam , Haylen Chu , Michael Turquette , Stephen Boyd , Brian Masney , Philipp Zabel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexandre Ghiti , Yao Zi , =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= , Guodong Xu , Icenowy Zheng , Jerome Brunet , Sarah Emery , Ze Huang , Dmitry Baryshkov Cc: Cody Kang , James Morris , dri-devel@lists.freedesktop.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, spacemit@lists.linux.dev, linux-kernel@vger.kernel.org, linux-phy@lists.infradead.org, linux-clk@vger.kernel.org X-Mailer: b4 0.16.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1795955545; l=32964; i=codykang.hk@gmail.com; s=codykang; h=from:subject:message-id; bh=9DgFbGELxMQQZ01YUycT7PRRtZIbK1UfbWWJX63JnX8=; b=EYi99Q8q06GcPDIRFsCPdbmFV9EXqSBzYWhXf5JTTVBeJhGF//p8oiDgIu9F1u5EN5Sc9L1at W3M0H0xIguFADTFUKf6zDZUtYfs+uMJJqLvQXFsu1TQIVdSU/TWeeMw X-Developer-Key: i=codykang.hk@gmail.com; a=ed25519; pk=Osn6/cs9Su/mHHw4P19cmVc3v2FMQfiWahjsN0vEkuk= X-Endpoint-Received: by B4 Relay for codykang.hk@gmail.com/codykang with auth_id=895 X-Original-From: Cody Kang Reply-To: codykang.hk@gmail.com From: Cody Kang Add the atomic KMS implementation on top of the hardware backend: one CRTC and one primary plane per DPU instance. The plane's atomic_check sizes its share of the RDMA line buffer and prepare_fb builds the framebuffer's DMMU table; atomic commit fills the per-channel command lists and arms the CFG_RDY handshake, and the commit's event is delivered at the vblank after the engine has taken the lists. The DPU is powered through runtime PM. Signed-off-by: Cody Kang --- v4: - create the initial state with drm_mode_config_create_initial_state() at bind (Maxime Ripard) - let the primary plane keep its framebuffer on a disabled CRTC - reject modes whose porches or sync widths do not fit the timing generator fields, vscan above 1, and interlaced or doublescan modes - ack a latched VSYNC before vblank is enabled; keep the commit's event for atomic_flush when a modeset turns the CRTC off and on; stop connector polling before a failed bind unbinds its components; shut the CFG_RDY watchdog down in unbind - allocate the DRM device with devm_drm_dev_alloc() and stop using dev_private (Guodong Xu) - fail enable_vblank while the DPU is off - remove firmware framebuffers before the probe-time reset, and run the clocks for that reset v3: - use atomic_create_state; .reset is gone from the CRTC and plane funcs in v7.4-rc1 - drop the ops table between the KMS code and the DPU backend (Thomas Zimmermann) - drop the tracepoints; use drm_dbg() and drm_WARN_ON() (Thomas Zimmermann) - wrap only the hardware access in drm_dev_enter() (Thomas Zimmermann) - enable vblank after programming the mode, and initialise vblank after drm_mode_config_reset() (Thomas Zimmermann) - program the plane from the clipped rectangles; drop the 1:1 and zero-size source checks (Thomas Zimmermann) - submit the plane's command list from its atomic_update, and turn its channel off when the plane is disabled (Thomas Zimmermann) - size the line buffer in the plane's atomic_check; no allocation in atomic_check (Thomas Zimmermann) - add prepare_fb: build the display MMU table there and wait for the buffer's fences (Thomas Zimmermann) - stop the boot loader's pipeline at probe instead of at the first modeset, and request the interrupt after it (Thomas Zimmermann) - power the DPU through runtime PM instead of from atomic_disable (Thomas Zimmermann) - release the drm_device when the aggregate unbinds, and stop output polling before its components unbind; take the mode and the plane state from the commit - reject a framebuffer with an offset, a src_x beyond the DMMU table padding, or a buffer larger than one TBU can map - reject modes above 3840x2160 in the CRTC's mode_valid (Icenowy Zheng) - do not advertise a gamma table (Guodong Xu) v2: - put all five DPU reset lines behind one reset_control_bulk_* pair (Philipp Zabel, Guodong Xu) - keep the online IRQ alive across drm_dev_unregister() and drm_atomic_helper_shutdown() in unbind (Sashiko) - free the per-state DMA buffers in the plane .reset path instead of bypassing the custom state destructor (Sashiko) - release the duplicated state's references on the rdmas allocation failure path in crtc duplicate_state (Sashiko) - set clocks_on only after dpu_pm_resume() has enabled the clocks (Sashiko) --- drivers/gpu/drm/spacemit/spacemit_crtc.c | 647 +++++++++++++++++++++++++++++ drivers/gpu/drm/spacemit/spacemit_planes.c | 303 ++++++++++++++ 2 files changed, 950 insertions(+) diff --git a/drivers/gpu/drm/spacemit/spacemit_crtc.c b/drivers/gpu/drm/spacemit/spacemit_crtc.c new file mode 100644 index 0000000000000..62b33f959f467 --- /dev/null +++ b/drivers/gpu/drm/spacemit/spacemit_crtc.c @@ -0,0 +1,647 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2025-2026 SpacemiT Co., Ltd. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include "dpu/dpu_saturn.h" +#include "dpu/saturn_regs/ops_hee.h" +#include "dpu/saturn_regs/saturn_fields.h" +#include "spacemit_cmdlist.h" +#include "spacemit_crtc.h" +#include "spacemit_dpu_reg.h" +#include "spacemit_drm.h" + +static void spacemit_crtc_atomic_enable(struct drm_crtc *crtc, + struct drm_atomic_commit *state) +{ + struct drm_crtc_state *crtc_state = + drm_atomic_get_new_crtc_state(state, crtc); + const struct drm_display_mode *mode = &crtc_state->adjusted_mode; + struct spacemit_crtc *a_crtc = to_spacemit_crtc(crtc); + struct spacemit_drm_private *priv = spacemit_drm_priv(crtc->dev); + int idx; + int ret; + + /* + * Not pm_runtime_resume_and_get(): atomic_disable runs and drops this + * reference even when the power-up failed. + */ + ret = pm_runtime_get_sync(priv->dev); + if (ret < 0) { + drm_dbg(crtc->dev, "DPU power-up failed: %d\n", ret); + return; + } + + if (drm_dev_enter(crtc->dev, &idx)) { + spacemit_dpu_init(a_crtc, mode); + drm_dev_exit(idx); + } + + drm_crtc_vblank_on(&a_crtc->crtc); +} + +static void spacemit_crtc_atomic_disable(struct drm_crtc *crtc, + struct drm_atomic_commit *state) +{ + struct drm_crtc_state *crtc_state = + drm_atomic_get_new_crtc_state(state, crtc); + struct spacemit_crtc *a_crtc = to_spacemit_crtc(crtc); + struct drm_device *drm = a_crtc->crtc.dev; + struct spacemit_drm_private *priv = spacemit_drm_priv(drm); + int idx; + + if (drm_dev_enter(drm, &idx)) { + spacemit_dpu_uninit(a_crtc); + drm_dev_exit(idx); + } + + drm_crtc_vblank_off(&a_crtc->crtc); + + /* A commit may still be waiting for CFG_RDY: stop its watchdog. */ + timer_delete_sync(&a_crtc->cfg_rdy_timer); + + pm_runtime_put_sync(priv->dev); + + /* + * No further vblank will come; complete the disable event here. On + * a modeset the CRTC comes back on and atomic_flush owns the event. + */ + spacemit_dpu_complete_event(a_crtc); + spin_lock_irq(&drm->event_lock); + if (crtc_state->event && !crtc_state->active) { + drm_crtc_send_vblank_event(crtc, crtc_state->event); + crtc_state->event = NULL; + } + spin_unlock_irq(&drm->event_lock); +} + +/* Planes write composer rows into the CRTC list, so open it before them. */ +static void spacemit_crtc_atomic_begin(struct drm_crtc *crtc, + struct drm_atomic_commit *state) +{ + struct spacemit_crtc *a_crtc = to_spacemit_crtc(crtc); + + spacemit_cmdlist_ring_next(a_crtc->cl, &a_crtc->cl_cur); +} + +static void spacemit_crtc_atomic_flush(struct drm_crtc *crtc, + struct drm_atomic_commit *state) +{ + struct drm_crtc_state *crtc_state = + drm_atomic_get_new_crtc_state(state, crtc); + struct spacemit_crtc *a_crtc = to_spacemit_crtc(crtc); + struct spacemit_drm_private *priv = spacemit_drm_priv(crtc->dev); + int idx; + + /* + * The event releases the next commit, which rewrites the other + * command list and points the engine at it. Hold the event until + * CFG_RDY says the engine has taken this commit's lists; the + * interrupt then arms it for the following vblank. + */ + spin_lock_irq(&crtc->dev->event_lock); + if (crtc_state->event) { + if (drm_crtc_vblank_get(crtc) == 0) + a_crtc->event = crtc_state->event; + else + drm_crtc_send_vblank_event(crtc, crtc_state->event); + crtc_state->event = NULL; + } + spin_unlock_irq(&crtc->dev->event_lock); + + /* clocks_on is false here only after a failed power-up. */ + if (priv->clocks_on && drm_dev_enter(crtc->dev, &idx)) { + spacemit_dpu_run(crtc, state); + drm_dev_exit(idx); + } else { + spacemit_dpu_complete_event(a_crtc); + } +} + +static int spacemit_crtc_enable_vblank(struct drm_crtc *crtc) +{ + struct spacemit_drm_private *priv = spacemit_drm_priv(crtc->dev); + struct spacemit_crtc *a_crtc = to_spacemit_crtc(crtc); + + /* Not powered: the mask cannot be opened and no VSYNC will come. */ + if (!READ_ONCE(priv->clocks_on)) + return -EINVAL; + + /* + * A VSYNC that latched while masked would interrupt as the mask + * opens and count a vblank that is already over. + */ + saturn_hee_clr_int_sts(a_crtc, saturn_hee_get_irq_bit(INT_VSYNC)); + saturn_hee_enable_irq(a_crtc, INT_VSYNC, true); + + return 0; +} + +static void spacemit_crtc_disable_vblank(struct drm_crtc *crtc) +{ + saturn_hee_enable_irq(to_spacemit_crtc(crtc), INT_VSYNC, false); +} + +static enum drm_mode_status +spacemit_crtc_mode_valid(struct drm_crtc *crtc, + const struct drm_display_mode *mode) +{ + unsigned int hfp = mode->hsync_start - mode->hdisplay; + unsigned int hsync = mode->hsync_end - mode->hsync_start; + unsigned int hbp = mode->htotal - mode->hsync_end; + unsigned int vfp = mode->vsync_start - mode->vdisplay; + unsigned int vsync = mode->vsync_end - mode->vsync_start; + unsigned int vbp = mode->vtotal - mode->vsync_end; + + if (mode->hdisplay > SPACEMIT_DPU_MAX_HDISPLAY) + return MODE_BAD_HVALUE; + if (mode->vdisplay > SPACEMIT_DPU_MAX_VDISPLAY) + return MODE_BAD_VVALUE; + + /* Progressive scan only; the timing generator cannot repeat lines. */ + if (mode->flags & DRM_MODE_FLAG_INTERLACE) + return MODE_NO_INTERLACE; + if (mode->flags & DRM_MODE_FLAG_DBLSCAN) + return MODE_NO_DBLESCAN; + if (mode->vscan > 1) + return MODE_NO_VSCAN; + + /* Porches and sync widths must fit their timing generator fields. */ + if (hfp > FIELD_MAX(dpu_field_mask(TMG_REG, hfp)) || + hbp > FIELD_MAX(dpu_field_mask(TMG_REG, hbp))) + return MODE_HBLANK_WIDE; + if (hsync > FIELD_MAX(dpu_field_mask(TMG_REG, hsync_width))) + return MODE_HSYNC_WIDE; + if (vfp > FIELD_MAX(dpu_field_mask(TMG_REG, vfp)) || + vbp > FIELD_MAX(dpu_field_mask(TMG_REG, vbp))) + return MODE_VBLANK_WIDE; + if (vsync > FIELD_MAX(dpu_field_mask(TMG_REG, vsync_width))) + return MODE_VSYNC_WIDE; + + return MODE_OK; +} + +static const struct drm_crtc_helper_funcs spacemit_crtc_helper_funcs = { + .mode_valid = spacemit_crtc_mode_valid, + .atomic_begin = spacemit_crtc_atomic_begin, + .atomic_flush = spacemit_crtc_atomic_flush, + .atomic_enable = spacemit_crtc_atomic_enable, + .atomic_disable = spacemit_crtc_atomic_disable, +}; + +static const struct drm_crtc_funcs spacemit_crtc_funcs = { + .set_config = drm_atomic_helper_set_config, + .page_flip = drm_atomic_helper_page_flip, + .atomic_create_state = drm_atomic_helper_crtc_create_state, + .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state, + .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state, + .enable_vblank = spacemit_crtc_enable_vblank, + .disable_vblank = spacemit_crtc_disable_vblank, +}; + +static void spacemit_crtc_put_port(struct drm_device *drm, void *port) +{ + of_node_put(port); +} + +static struct spacemit_crtc *spacemit_crtc_create(struct drm_device *drm, + struct drm_plane *primary, + struct device_node *port) +{ + struct spacemit_crtc *a_crtc; + int err; + + a_crtc = drmm_crtc_alloc_with_planes(drm, struct spacemit_crtc, crtc, + primary, NULL, + &spacemit_crtc_funcs, NULL); + if (IS_ERR(a_crtc)) { + drm_err(drm, "failed to init crtc\n"); + return a_crtc; + } + + /* + * drm_of_find_possible_crtcs() walks crtc->port for the life of the + * drm_device. + */ + a_crtc->crtc.port = of_node_get(port); + err = drmm_add_action_or_reset(drm, spacemit_crtc_put_port, port); + if (err) + return ERR_PTR(err); + + drm_crtc_helper_add(&a_crtc->crtc, &spacemit_crtc_helper_funcs); + + err = spacemit_cmdlist_ring_init(drm, a_crtc->cl, CMDLIST_CRTC, NULL, + &a_crtc->crtc); + if (err) + return ERR_PTR(err); + + a_crtc->cl_cmp = spacemit_cmdlist_regs_alloc(drm, CMPS_X_REG__NWORDS); + a_crtc->cl_pp = spacemit_cmdlist_regs_alloc(drm, POSTPIPE_REG__NWORDS); + if (!a_crtc->cl_cmp || !a_crtc->cl_pp) + return ERR_PTR(-ENOMEM); + + return a_crtc; +} + +/* The one plane and the one CRTC of a DPU instance. */ +static struct spacemit_crtc *spacemit_drm_pipeline_init(struct drm_device *drm) +{ + struct spacemit_crtc *a_crtc; + struct device_node *port; + struct drm_plane *plane; + + plane = spacemit_plane_init(drm); + if (IS_ERR(plane)) + return ERR_CAST(plane); + + port = of_graph_get_port_by_id(drm->dev->of_node, 0); + if (!port) + return ERR_PTR(dev_err_probe(drm->dev, -EINVAL, + "no output port\n")); + + a_crtc = spacemit_crtc_create(drm, plane, port); + of_node_put(port); + if (IS_ERR(a_crtc)) + return a_crtc; + + timer_setup(&a_crtc->cfg_rdy_timer, spacemit_cfg_rdy_timer_handler, 0); + + return a_crtc; +} + +static irqreturn_t spacemit_dpu_isr(int irq, void *data) +{ + struct spacemit_drm_private *priv = data; + struct spacemit_crtc *a_crtc = priv->a_crtc; + + if (!a_crtc) + return IRQ_NONE; + + /* Reading a clock-gated DPU's status registers stalls the bus. */ + if (unlikely(!READ_ONCE(priv->clocks_on))) + return IRQ_NONE; + + /* + * Not under drm_dev_enter(): an unplugged device raises interrupts + * until its CRTC is off, and they still have to be acked. + */ + return spacemit_dpu_online_isr(a_crtc) ? IRQ_HANDLED : IRQ_NONE; +} + +static int spacemit_dpu_hw_resources_init(struct spacemit_drm_private *priv, + struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + int n_rstc; + int ret; + + priv->clks[DPU_PXCLK].id = "pxclk"; + priv->clks[DPU_MCLK].id = "mclk"; + priv->clks[DPU_ESCCLK].id = "escclk"; + priv->clks[DPU_ACLK].id = "aclk"; + priv->clks[DPU_DSCCLK].id = "dscclk"; + ret = devm_clk_bulk_get(dev, ARRAY_SIZE(priv->clks), priv->clks); + if (ret) + return ret; + + priv->pipe_resets[0].id = "lcd"; + priv->pipe_resets[1].id = "esc"; + priv->pipe_resets[2].id = "mclk"; + priv->pipe_resets[3].id = "aclk"; + priv->pipe_resets[4].id = "dsc"; + n_rstc = ARRAY_SIZE(priv->pipe_resets); + ret = devm_reset_control_bulk_get_exclusive(dev, n_rstc, + priv->pipe_resets); + if (ret) + return dev_err_probe(dev, ret, "failed to get resets\n"); + + ret = aperture_remove_all_conflicting_devices(spacemit_drm_drv.name); + if (ret) + return dev_err_probe(dev, ret, + "failed to remove existing framebuffers\n"); + + /* + * The boot loader may leave its boot-logo pipeline running. Stop it + * here and hold the resets until the first enable brings the block + * up. The clocks may have been gated as unused by now, and the resets + * only reach a clocked block. + */ + ret = clk_bulk_prepare_enable(ARRAY_SIZE(priv->clks), priv->clks); + if (ret) + return dev_err_probe(dev, ret, "failed to enable clocks\n"); + ret = reset_control_bulk_assert(n_rstc, priv->pipe_resets); + usleep_range(10000, 20000); + clk_bulk_disable_unprepare(ARRAY_SIZE(priv->clks), priv->clks); + if (ret) + return dev_err_probe(dev, ret, "failed to assert resets\n"); + + priv->irq_online = platform_get_irq_byname(pdev, "online"); + if (priv->irq_online < 0) + return priv->irq_online; + + /* Keep the line disabled until bind: the handler needs bind state. */ + return devm_request_irq(dev, priv->irq_online, spacemit_dpu_isr, + IRQF_NO_AUTOEN, "DPU_ONLINE", priv); +} + +static const struct regmap_config spacemit_dpu_regmap_config = { + .name = "dpu", + .reg_bits = 32, + .val_bits = 32, + .reg_stride = 4, + .max_register = 0x53ffc, /* 0x54000-byte DPU register window */ +}; + +static int spacemit_drm_master_bind(struct device *dev) +{ + struct spacemit_drm_private *priv = dev_get_drvdata(dev); + struct spacemit_drm_device *sdrm; + struct spacemit_crtc *a_crtc; + struct drm_device *drm; + int ret; + + /* + * devres taken in bind is scoped to the bind: the component core + * releases it when the aggregate unbinds, which puts the drm_device. + */ + sdrm = devm_drm_dev_alloc(dev, &spacemit_drm_drv, + struct spacemit_drm_device, base); + if (IS_ERR(sdrm)) + return PTR_ERR(sdrm); + sdrm->priv = priv; + drm = &sdrm->base; + priv->ddev = drm; + + ret = spacemit_drm_mode_config_init(drm); + if (ret) + goto err_dc; + + a_crtc = spacemit_drm_pipeline_init(drm); + if (IS_ERR(a_crtc)) { + ret = PTR_ERR(a_crtc); + goto err_dc; + } + priv->a_crtc = a_crtc; + + /* + * component_bind_all() unbinds what it bound before returning an error. + */ + ret = component_bind_all(dev, drm); + if (ret) { + dev_err_probe(dev, ret, "failed to bind all components\n"); + goto err_dc; + } + + ret = drm_mode_config_create_initial_state(drm); + if (ret) { + dev_err_probe(dev, ret, "failed to create the initial state\n"); + goto err_unbind_all; + } + + ret = drm_vblank_init(drm, drm->mode_config.num_crtc); + if (ret) { + dev_err_probe(dev, ret, "failed to initialize vblank\n"); + goto err_unbind_all; + } + + drmm_kms_helper_poll_init(drm); + + enable_irq(priv->irq_online); + + ret = drm_dev_register(drm, 0); + if (ret < 0) + goto err_disable_irq; + drm_client_setup(drm, NULL); + + return 0; + +err_disable_irq: + /* The poll worker must not find the connectors half torn down. */ + drm_kms_helper_poll_fini(drm); + disable_irq(priv->irq_online); + +err_unbind_all: + component_unbind_all(dev, drm); +err_dc: + /* + * .shutdown tests priv->ddev; never leave it on a half-initialised + * device. + */ + priv->ddev = NULL; + priv->a_crtc = NULL; + return ret; +} + +static void spacemit_drm_master_unbind(struct device *dev) +{ + struct spacemit_drm_private *priv = dev_get_drvdata(dev); + struct drm_device *drm = priv->ddev; + + /* + * A process may keep the card open past this point. Unplug, so that + * its ioctls fail and the drm_dev_enter() sections keep off the + * registers. The commits that follow still turn the outputs off: the + * CRTC drops its runtime PM reference, and the suspend resets the DPU. + * The interrupt stays alive until then, for a commit that was already + * waiting on it. + */ + drm_dev_unplug(drm); + /* An open file keeps the drm_device, and its poll worker, alive. */ + drm_kms_helper_poll_fini(drm); + drm_atomic_helper_shutdown(drm); + + disable_irq(priv->irq_online); + /* + * A shutdown commit that failed has not stopped the watchdog of a + * commit still waiting for CFG_RDY, and the CRTC is freed once unbind + * returns. + */ + timer_shutdown_sync(&priv->a_crtc->cfg_rdy_timer); + + component_unbind_all(dev, drm); + + /* .shutdown and a later re-bind must not touch the stale instance. */ + priv->ddev = NULL; + priv->a_crtc = NULL; +} + +const struct component_master_ops spacemit_drm_master_ops = { + .bind = spacemit_drm_master_bind, + .unbind = spacemit_drm_master_unbind, +}; + +static int spacemit_dpu_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + const struct spacemit_hw_device *hwdev_template; + struct spacemit_drm_private *priv; + int ret; + + /* A display MMU entry holds a 26-bit page frame number. */ + ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(38)); + if (ret) + return dev_err_probe(dev, ret, "failed to set DMA mask\n"); + + priv = devm_kzalloc_obj(dev, *priv); + if (!priv) + return -ENOMEM; + priv->dev = dev; + dev_set_drvdata(dev, priv); + + /* The match data is shared const; the MMIO mapping is per-instance. */ + hwdev_template = device_get_match_data(dev); + if (!hwdev_template) + return -ENODEV; + priv->hwdev = devm_kmemdup(dev, hwdev_template, + sizeof(*hwdev_template), GFP_KERNEL); + if (!priv->hwdev) + return -ENOMEM; + + priv->hwdev->base = devm_platform_ioremap_resource(pdev, 0); + if (IS_ERR(priv->hwdev->base)) + return PTR_ERR(priv->hwdev->base); + + priv->hwdev->regmap = devm_regmap_init_mmio(dev, priv->hwdev->base, + &spacemit_dpu_regmap_config); + if (IS_ERR(priv->hwdev->regmap)) + return dev_err_probe(dev, PTR_ERR(priv->hwdev->regmap), + "failed to init DPU regmap\n"); + + ret = spacemit_dpu_hw_resources_init(priv, pdev); + if (ret) + return ret; + + /* Held in reset with the clocks off: the suspended state. */ + ret = devm_pm_runtime_enable(dev); + if (ret) + return ret; + + return spacemit_drm_of_component_probe(dev); +} + +static void spacemit_dpu_remove(struct platform_device *pdev) +{ + component_master_del(&pdev->dev, &spacemit_drm_master_ops); +} + +static void spacemit_dpu_shutdown(struct platform_device *pdev) +{ + struct spacemit_drm_private *priv = dev_get_drvdata(&pdev->dev); + struct drm_device *drm = priv->ddev; + + /* Not bound: nothing was ever enabled. */ + if (!drm) + return; + + drm_atomic_helper_shutdown(drm); +} + +static int spacemit_dpu_runtime_suspend(struct device *dev) +{ + struct spacemit_drm_private *priv = dev_get_drvdata(dev); + int ret; + + /* + * Close the ISR's MMIO gate, then let a handler that is already past + * it finish before the clocks stop. + */ + WRITE_ONCE(priv->clocks_on, false); + synchronize_irq(priv->irq_online); + + /* + * Assert before gating: a reset asserted into an already-gated block + * has no clock edges to propagate on and leaves the register file + * untouched. + */ + ret = reset_control_bulk_assert(ARRAY_SIZE(priv->pipe_resets), + priv->pipe_resets); + if (ret) + dev_warn(dev, "failed to assert resets: %d\n", ret); + usleep_range(10000, 20000); + + clk_bulk_disable_unprepare(ARRAY_SIZE(priv->clks), priv->clks); + + return 0; +} + +static int spacemit_dpu_runtime_resume(struct device *dev) +{ + struct spacemit_drm_private *priv = dev_get_drvdata(dev); + int ret; + + ret = reset_control_bulk_deassert(ARRAY_SIZE(priv->pipe_resets), + priv->pipe_resets); + if (ret) { + dev_err(dev, "failed to deassert resets: %d\n", ret); + return ret; + } + + ret = spacemit_dpu_enable_clocks(priv); + if (ret) { + reset_control_bulk_assert(ARRAY_SIZE(priv->pipe_resets), + priv->pipe_resets); + return ret; + } + + /* The DPU must settle after reset deassert or CFG_RDY races the flush. */ + usleep_range(10000, 20000); + WRITE_ONCE(priv->clocks_on, true); + + return 0; +} + +static const struct dev_pm_ops spacemit_dpu_pm_ops = { + RUNTIME_PM_OPS(spacemit_dpu_runtime_suspend, spacemit_dpu_runtime_resume, + NULL) +}; + +static const struct of_device_id spacemit_dpu_match_table[] = { + { + .compatible = "spacemit,k3-saturn-dpu", + .data = &spacemit_k3_saturn_dpu, + }, + {}, +}; +MODULE_DEVICE_TABLE(of, spacemit_dpu_match_table); + +static struct platform_driver spacemit_dpu_driver = { + .probe = spacemit_dpu_probe, + .remove = spacemit_dpu_remove, + .shutdown = spacemit_dpu_shutdown, + .driver = { + .name = "spacemit-dpu-drv", + .of_match_table = spacemit_dpu_match_table, + .pm = pm_ptr(&spacemit_dpu_pm_ops), + .suppress_bind_attrs = true, + }, +}; +module_platform_driver(spacemit_dpu_driver); + +MODULE_DESCRIPTION("SpacemiT DRM KMS master driver"); +MODULE_LICENSE("GPL"); diff --git a/drivers/gpu/drm/spacemit/spacemit_planes.c b/drivers/gpu/drm/spacemit/spacemit_planes.c new file mode 100644 index 0000000000000..6a528e539abca --- /dev/null +++ b/drivers/gpu/drm/spacemit/spacemit_planes.c @@ -0,0 +1,303 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2025-2026 SpacemiT Co., Ltd. + * + */ + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "dpu/dpu_saturn.h" +#include "dpu/saturn_fbcmem.h" +#include "dpu/saturn_regs/ops_hee.h" +#include "dpu/saturn_regs/saturn_fields.h" +#include "spacemit_cmdlist.h" +#include "spacemit_dmmu.h" +#include "spacemit_drm.h" + +/* The AFBC decoder exists but no modifier has been validated on hardware. */ +static const u64 supported_format_modifiers[] = { + DRM_FORMAT_MOD_LINEAR, + DRM_FORMAT_MOD_INVALID +}; + +static int spacemit_plane_atomic_check(struct drm_plane *plane, + struct drm_atomic_commit *atomic_state) +{ + struct drm_plane_state *state = + drm_atomic_get_new_plane_state(atomic_state, plane); + struct drm_framebuffer *fb = state->fb; + struct spacemit_plane_state *cur_state = to_spacemit_plane_state(state); + struct spacemit_drm_private *priv = spacemit_drm_priv(plane->dev); + struct drm_crtc_state *crtc_state = NULL; + int ret; + + if (state->crtc) + crtc_state = drm_atomic_get_new_crtc_state(atomic_state, + state->crtc); + + ret = drm_atomic_helper_check_plane_state(state, crtc_state, + DRM_PLANE_NO_SCALING, + DRM_PLANE_NO_SCALING, + true, true); + if (ret) + return ret; + + if (!state->visible) + return 0; + + /* The width limit bounds the default pitch; userspace may pass a larger one. */ + if (fb->pitches[0] > SPACEMIT_DPU_MAX_PITCH) { + drm_dbg(plane->dev, "pitch %u exceeds the stride field\n", + fb->pitches[0]); + return -EINVAL; + } + + /* Scanout starts at the first byte of the buffer object. */ + if (fb->offsets[0]) { + drm_dbg(plane->dev, "framebuffer offset %u is not supported\n", + fb->offsets[0]); + return -EINVAL; + } + + if (state->src.x1 >> 16 > SPACEMIT_DMMU_MAX_SRC_X) { + drm_dbg(plane->dev, "src_x %d exceeds the DMMU table padding\n", + state->src.x1 >> 16); + return -EINVAL; + } + + if (fb->obj[0]->size >> PAGE_SHIFT > SPACEMIT_DMMU_MAX_PAGES) { + drm_dbg(plane->dev, "buffer of %zu bytes exceeds one TBU\n", + fb->obj[0]->size); + return -EINVAL; + } + + if (saturn_cal_layer_fbcmem_size(plane, state)) { + drm_dbg(plane->dev, "invalid fbcmem size\n"); + return -EINVAL; + } + + cur_state->format = spacemit_plane_hw_get_format_id(priv->hwdev, + fb->format->format); + + return 0; +} + +static void spacemit_plane_program(struct drm_plane *plane, + struct drm_plane_state *state) +{ + struct spacemit_plane *p = to_spacemit_plane(plane); + u32 rdma_id = p->rdma_id; + + spacemit_cmdlist_ring_next(p->cl, &p->cl_cur); + + saturn_hee_plane_update_hw_channel(plane, state); + spacemit_dmmu_map(state->fb, rdma_id * 2, p->cl_rdma, plane); + spacemit_cmdlist_regs_packing(spacemit_plane_to_cl(plane), CMDLIST_MOD_RDMA, + p->cl_rdma); + + spacemit_cmdlist_submit_plane(plane, state); +} + +/* Take the layer out of the composition and stop its read channel. */ +static void spacemit_plane_off(struct drm_plane *plane, struct drm_crtc *crtc) +{ + struct spacemit_drm_private *priv = spacemit_drm_priv(plane->dev); + struct spacemit_plane *p = to_spacemit_plane(plane); + + saturn_hee_plane_disable_hw_channel(plane, crtc); + saturn_hee_enable_cmdlist(priv->hwdev, p->rdma_id, false); +} + +static void spacemit_plane_atomic_update(struct drm_plane *plane, + struct drm_atomic_commit *state) +{ + struct drm_plane_state *new_state = + drm_atomic_get_new_plane_state(state, plane); + struct spacemit_drm_private *priv = spacemit_drm_priv(plane->dev); + int idx; + + if (priv->clocks_on && drm_dev_enter(plane->dev, &idx)) { + /* A plane moved off the screen has no rectangle to program. */ + if (new_state->visible) + spacemit_plane_program(plane, new_state); + else + spacemit_plane_off(plane, new_state->crtc); + drm_dev_exit(idx); + } +} + +static void spacemit_plane_atomic_disable(struct drm_plane *plane, + struct drm_atomic_commit *state) +{ + struct drm_plane_state *old_state = + drm_atomic_get_old_plane_state(state, plane); + struct spacemit_drm_private *priv = spacemit_drm_priv(plane->dev); + int idx; + + if (priv->clocks_on && drm_dev_enter(plane->dev, &idx)) { + spacemit_plane_off(plane, old_state->crtc); + drm_dev_exit(idx); + } +} + +static struct drm_plane_state * +spacemit_plane_atomic_create_state(struct drm_plane *plane) +{ + struct spacemit_plane_state *s; + + s = kzalloc_obj(*s); + if (!s) + return ERR_PTR(-ENOMEM); + + __drm_atomic_helper_plane_state_init(&s->state, plane); + + return &s->state; +} + +static struct drm_plane_state * +spacemit_plane_atomic_duplicate_state(struct drm_plane *plane) +{ + struct spacemit_plane_state *s; + struct spacemit_plane_state *old_state = + to_spacemit_plane_state(plane->state); + + s = kzalloc_obj(*s); + if (!s) + return NULL; + + __drm_atomic_helper_plane_duplicate_state(plane, &s->state); + + s->format = old_state->format; + s->fbcmem_size = old_state->fbcmem_size; + + return &s->state; +} + +static void spacemit_plane_atomic_destroy_state(struct drm_plane *plane, + struct drm_plane_state *state) +{ + __drm_atomic_helper_plane_destroy_state(state); + + kfree(to_spacemit_plane_state(state)); +} + +static int spacemit_plane_create_properties(struct spacemit_plane *p) +{ + unsigned int support_modes = BIT(DRM_MODE_BLEND_PIXEL_NONE) | + BIT(DRM_MODE_BLEND_PREMULTI) | + BIT(DRM_MODE_BLEND_COVERAGE); + int ret; + + ret = drm_plane_create_alpha_property(&p->plane); + if (ret) + return ret; + + return drm_plane_create_blend_mode_property(&p->plane, support_modes); +} + +/* Collect the buffer's fences and build its DMMU table once. */ +static int spacemit_plane_prepare_fb(struct drm_plane *plane, + struct drm_plane_state *state) +{ + int ret; + + if (!state->fb) + return 0; + + ret = drm_gem_plane_helper_prepare_fb(plane, state); + if (ret) + return ret; + + return spacemit_dmmu_prepare(state->fb->obj[0]); +} + +static const struct drm_plane_helper_funcs spacemit_plane_helper_funcs = { + .prepare_fb = spacemit_plane_prepare_fb, + .atomic_check = spacemit_plane_atomic_check, + .atomic_update = spacemit_plane_atomic_update, + .atomic_disable = spacemit_plane_atomic_disable, +}; + +static void spacemit_plane_atomic_print_state(struct drm_printer *p, + const struct drm_plane_state *state) +{ + const struct spacemit_plane_state *s = to_spacemit_plane_state(state); + + drm_printf(p, "\tformat-id=%u\n", s->format); + drm_printf(p, "\tfbcmem-size=%u\n", s->fbcmem_size); +} + +static const struct drm_plane_funcs spacemit_plane_funcs = { + .update_plane = drm_atomic_helper_update_plane, + .disable_plane = drm_atomic_helper_disable_plane, + .atomic_create_state = spacemit_plane_atomic_create_state, + .atomic_duplicate_state = spacemit_plane_atomic_duplicate_state, + .atomic_destroy_state = spacemit_plane_atomic_destroy_state, + .atomic_print_state = spacemit_plane_atomic_print_state, +}; + +struct drm_plane *spacemit_plane_init(struct drm_device *drm) +{ + struct spacemit_plane *p; + int err, j; + u32 *formats; + struct spacemit_drm_private *priv = spacemit_drm_priv(drm); + struct spacemit_hw_device *hwdev = priv->hwdev; + u8 n_formats = hwdev->n_formats; + u32 plane_crtc_mask; + + formats = kzalloc_objs(*formats, n_formats); + if (!formats) + return ERR_PTR(-ENOMEM); + + /* + * The CRTC is created right after this, so its index is the current + * count. + */ + plane_crtc_mask = BIT(drm->mode_config.num_crtc); + + for (j = 0; j < n_formats; j++) + formats[j] = hwdev->formats[j].format; + + p = drmm_universal_plane_alloc(drm, struct spacemit_plane, plane, + plane_crtc_mask, &spacemit_plane_funcs, + formats, n_formats, + supported_format_modifiers, + DRM_PLANE_TYPE_PRIMARY, NULL); + kfree(formats); + if (IS_ERR(p)) { + drm_err(drm, "failed to init primary plane\n"); + return ERR_CAST(p); + } + + /* The single primary plane owns the first read channel. */ + p->rdma_id = 0; + + drm_plane_helper_add(&p->plane, &spacemit_plane_helper_funcs); + + err = spacemit_cmdlist_ring_init(drm, p->cl, CMDLIST_PLANE, &p->plane, NULL); + if (err) + return ERR_PTR(err); + + err = spacemit_plane_create_properties(p); + if (err) + return ERR_PTR(err); + + p->cl_rdma = spacemit_cmdlist_regs_alloc(drm, RDMA_PATH_X_REG__NWORDS); + p->cl_tbu = spacemit_cmdlist_regs_alloc(drm, MMU_TBU_REG__NWORDS); + if (!p->cl_rdma || !p->cl_tbu) + return ERR_PTR(-ENOMEM); + + return &p->plane; +} -- 2.43.0