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 B9F00606A31C for ; Mon, 30 Nov 2026 18:49:28 +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=gruJ8W5F Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 7A50C10E039; Mon, 30 Nov 2026 18:49:23 +0000 (UTC) Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.12]) by gabe.freedesktop.org (Postfix) with ESMTPS id 69BE810E305 for ; Mon, 30 Nov 2026 18:49:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796064554; x=1827600554; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=bawjmgz1vylUYqr0+yHlnh14LWostxXAFWo1FsWgIRo=; b=gruJ8W5FTHhu5KnWPQabOtMJzjLetrZNGtcHNGE2k73ys1Mt/Yl9Infl cswnkDA0F1MlokmTtwIA3tyKek4PxKfkpildmAFOShs6JIfsAHbIj5EjJ zYZ90WF82Yy9vvZ6QCMttpVKv083j7EaMW+yCkHjzZDYt0TJ7+NV4uRrZ LWs8Su5qqQxYA2pq+O55sJm63F/QtIZYB82vyEw6fykn0d2qvCHQPYBGX g2B9FCRYs4fzfF4AGROkhoVAi2+nG+3ZmOy7uulSDv1uJY49oKKxT13RN 06IcMfKaHOF2W69aFVt2y6nFqjcAWUqmvqRHMorL3JE0PS6V5QhxkisvZ Q==; X-CSE-ConnectionGUID: rc48VJwBQGO/Yws8dkO9Ow== X-CSE-MsgGUID: Om5grUZxRQmCsnYhvCiWKg== X-IronPort-AV: E=McAfee;i="6800,10657,11982"; a="4814732" X-IronPort-AV: E=Sophos;i="6.27,243,1787036400"; d="scan'208";a="4814732" Received: from fmviesa006.fm.intel.com ([10.60.135.146]) by fmvoesa106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Nov 2026 10:49:14 -0800 X-CSE-ConnectionGUID: knP49QKRRRqbhJu3bEaSGw== X-CSE-MsgGUID: 39gHJ1mnRlmArj5XJstTYg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,243,1787036400"; d="scan'208";a="13330354" Received: from sinjan-super-server.iind.intel.com ([10.190.239.39]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Nov 2026 10:49:12 -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, riana.tauro@intel.com, sk.anirban@intel.com, mallesh.koujalagi@intel.com, soham.purkait@intel.com, ravi.kishore.koppuravuri@intel.com, Karthik Poosa Subject: [PATCH v6 03/10] drm/xe/pcode: Add ordered write batch helper Date: Tue, 1 Dec 2026 00:20:43 +0530 Message-Id: <20261130185050.36979-4-karthik.poosa@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20261130185050.36979-1-karthik.poosa@intel.com> References: <20261130185050.36979-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. 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