Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (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.subspace.kernel.org (Postfix) with ESMTPS id 7DAC1606A322 for ; Thu, 3 Dec 2026 09:05:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=cD4SW1Fd Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 41E7A11206B; Thu, 3 Dec 2026 09:05:40 +0000 (UTC) Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.18]) by gabe.freedesktop.org (Postfix) with ESMTPS id 3399711206B for ; Thu, 3 Dec 2026 09:05:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796288734; x=1827824734; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ATc7RB977hDMZH3YwAYd0SqQiQBJPTq2S2VdH9zg/g0=; b=cD4SW1FdtK/o36qKBlP8kodQF6f9Jxy8guQ1x7xphRiwg7VsJ8ihDyfy QIn7AXUnsXQkmUbnn/fvJ1BP4kvSXYmMdSInby6KzOplZaQljYZEKVYXy WMNm7dAWF+VrusKqW0cchAsuXSpekBWCIaZFhewGXtL485dR6TQokk3bZ Jp4Jz3+8/tCQlxPeOLKU5AwNLFewgciYQ5z+9YzXWgWUy7xyiqAk0yQeU L4rEaqS7SoqL4SVgkf4XQ2Txgh75MJQ/YIeSwxF6SdSGsGLpTW2I+NOft HeObdY5gAwCkF7p4DvLn4j0aiM7WSmlPbUd+0cfZyxG2Jn5MWDKquqIS4 g==; X-CSE-ConnectionGUID: HytPl8n/QYSYFbuw/kLTwA== X-CSE-MsgGUID: MW6CKAXoSyOleRfXhxVFNw== X-IronPort-AV: E=McAfee;i="6800,10657,11984"; a="5254252" X-IronPort-AV: E=Sophos;i="6.27,248,1787036400"; d="scan'208";a="5254252" Received: from fmviesa013.fm.intel.com ([10.60.135.153]) by orvoesa110.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Dec 2026 01:05:33 -0800 X-CSE-ConnectionGUID: 6FE7JgilTDy4oNUhvPMQow== X-CSE-MsgGUID: C92PEVFPR0yrVgDFiRzWng== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,248,1787036400"; d="scan'208";a="15730370" Received: from sinjan-super-server.iind.intel.com ([10.190.239.39]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 03 Dec 2026 01:05:31 -0800 From: Karthik Poosa To: intel-xe@lists.freedesktop.org Cc: rodrigo.vivi@intel.com, anshuman.gupta@intel.com, badal.nilawar@intel.com, raag.jadav@intel.com, soham.purkait@intel.com, riana.tauro@intel.com, Karthik Poosa Subject: [PATCH v11 04/11] drm/xe/pcode: Add ordered write batch helper Date: Thu, 3 Dec 2026 14:36:58 +0530 Message-Id: <20261203090705.2500610-5-karthik.poosa@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261203090705.2500610-1-karthik.poosa@intel.com> References: <20261203090705.2500610-1-karthik.poosa@intel.com> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: intel-xe@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Intel Xe graphics driver List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "Intel-xe" X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 Add a pcode helper that issues an ordered sequence of mailbox writes while holding the pcode lock, so no other mailbox command can be interleaved. Some pcode interfaces keep state across mailbox commands. A fan table is written one point at a time with FSC_WRITE_FAN_TABLE and loaded by a final FSC_WRITE_NUM_FAN_CONTROL_POINTS write. pcode does not tie the staged points to a requester, so fan table writes from another caller in the middle of the sequence can be merged into it, and the commit then either fails with illegal data or loads a wrong table. Serialize all fan control table writes, for every fan, on the pcode lock rather than per fan. Caller-side locks such as the hwmon lock only order callers that take them; the pcode lock orders every mailbox user in the driver. Lock hold time is the sum of the pcode response times of the batch. Each write is bounded by PCODE_DEFAULT_TIMEOUT_MS, and a write that times out ends the batch, so the worst case is count * PCODE_DEFAULT_TIMEOUT_MS (110 ms for the largest fan table of 10 points plus the count write). Other pcode users wait on the lock with mutex_lock() and start their own mailbox timeout only after acquiring it, so a batch delays them but does not make them time out. This is in line with existing pcode lock users: xe_pcode_request() holds the lock for up to its base timeout plus 50 ms, and xe_pcode_init_min_freq_table() holds it across a loop of writes. This will be used to write fan tables atomically with respect to other pcode users. Reviewed-by: Rodrigo Vivi Reviewed-by: Soham Purkait Signed-off-by: Karthik Poosa --- v2: - Explain why pcode needs the writes without interleaving, and the lock hold time impact on other pcode users. (sashiko) drivers/gpu/drm/xe/xe_pcode.c | 49 +++++++++++++++++++++++++++++++++++ drivers/gpu/drm/xe/xe_pcode.h | 12 +++++++++ 2 files changed, 61 insertions(+) diff --git a/drivers/gpu/drm/xe/xe_pcode.c b/drivers/gpu/drm/xe/xe_pcode.c index fa9f45d565cb..f465decbf946 100644 --- a/drivers/gpu/drm/xe/xe_pcode.c +++ b/drivers/gpu/drm/xe/xe_pcode.c @@ -124,6 +124,55 @@ int xe_pcode_write_timeout(struct xe_tile *tile, u32 mbox, u32 data, int timeout return err; } +/** + * xe_pcode_write_batch - issue pcode writes in order under the mailbox lock + * @tile: tile + * @writes: mailbox/data pairs to write + * @count: number of entries in @writes + * + * Some pcode interfaces keep state across mailbox commands; e.g. a fan table + * is written point by point and loaded by a final point count write. Holding + * the pcode lock across all writes keeps each such sequence atomic with + * respect to every other mailbox user, including fan table writes for any + * other fan, independent of caller-side locks. + * + * The lock is held for the sum of the pcode response times, bounded by + * @count * PCODE_DEFAULT_TIMEOUT_MS; a write that times out ends the batch. + * Other pcode users sleep on the lock and start their own mailbox timeout only + * once they own it, so they are delayed but do not time out. Keep @count small. + * + * Stops at the first failure; earlier writes are not reverted. + * + * Context: Process context. Takes and releases the pcode lock. + * Return: 0 on success, negative error code otherwise. + */ +int xe_pcode_write_batch(struct xe_tile *tile, const struct xe_pcode_write *writes, + unsigned int count) +{ + unsigned int i; + int err = 0; + + if (!count) + return -EINVAL; + + mutex_lock(&tile->pcode.lock); + for (i = 0; i < count; i++) { + u32 data = writes[i].data; + + err = pcode_mailbox_rw(tile, writes[i].mbox, &data, NULL, PCODE_DEFAULT_TIMEOUT_MS, + false, false); + if (err) { + xe_err(tile_to_xe(tile), + "pcode batch write %u/%u failed: mbox 0x%08x data 0x%08x, ret %pe\n", + i + 1, count, writes[i].mbox, writes[i].data, ERR_PTR(err)); + break; + } + } + mutex_unlock(&tile->pcode.lock); + + return err; +} + int xe_pcode_write64_timeout(struct xe_tile *tile, u32 mbox, u32 data0, u32 data1, int timeout) { int err; diff --git a/drivers/gpu/drm/xe/xe_pcode.h b/drivers/gpu/drm/xe/xe_pcode.h index 7e43792b0037..c601a12baba6 100644 --- a/drivers/gpu/drm/xe/xe_pcode.h +++ b/drivers/gpu/drm/xe/xe_pcode.h @@ -18,6 +18,16 @@ struct xe_pcode_version { u32 engg; }; +/** + * struct xe_pcode_write - single pcode mailbox write + * @mbox: mailbox command + * @data: value written to PCODE_DATA0 + */ +struct xe_pcode_write { + u32 mbox; + u32 data; +}; + #define PCODE_DEFAULT_TIMEOUT_MS 10 int xe_pcode_init_early(struct xe_tile *tile); @@ -28,6 +38,8 @@ int xe_pcode_init_min_freq_table(struct xe_tile *tile, u32 min_gt_freq, int xe_pcode_read(struct xe_tile *tile, u32 mbox, u32 *val0, u32 *val1); int xe_pcode_write_timeout(struct xe_tile *tile, u32 mbox, u32 val, int timeout_ms); +int xe_pcode_write_batch(struct xe_tile *tile, const struct xe_pcode_write *writes, + unsigned int count); int xe_pcode_write64_timeout(struct xe_tile *tile, u32 mbox, u32 data0, u32 data1, int timeout); int xe_get_pcode_version(struct xe_device *xe, struct xe_pcode_version *version); -- 2.34.1