From patchwork Mon Feb 19 22:33:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Lechner X-Patchwork-Id: 203296 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp73467dyc; Mon, 19 Feb 2024 14:36:13 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCU7O5zsSNJDH7D7mAYxuYwc0cok6i1EJED3SFYLdffMDwHEy9Yo9jpWE3arH1MB3ZnrZ5nNu7UZ3OLHcsa97F7qhiD2nA== X-Google-Smtp-Source: AGHT+IEUT3oNuLKcK3cXik1JFjcfYyhhG6l5WYeyJLA+nkK5tDMhyBEQVGXyzLRph79nS4uhCQqM X-Received: by 2002:a17:906:2811:b0:a3c:b430:7f54 with SMTP id r17-20020a170906281100b00a3cb4307f54mr9485080ejc.3.1708382173470; Mon, 19 Feb 2024 14:36:13 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708382173; cv=pass; d=google.com; s=arc-20160816; b=n8XHik8pzMYVcLqUvwMfa3GOUPw2ZpmVtRBhKmgJqbXQTR455FRGL3NvC3wISBR0IJ ZFine1RBWupVDt3W67CaMpRQ08mLw56fM+6ACUuBmypCuJyoWfezXvZIq8QVJt3yhL13 NNV/sR7oB6UZ/g2SYDzp14Ug3yKDTTp5p95fNLperT/UuCwhd3hKdM4ObZtU0mdGpVdz EJmsnVX049QHLoapOal6HVA7V+/NbcDtTdAH/1JPnG+D4vKnfd2eCFGNZryXAGcSYiEs wnIi+M24jOp+UbkmDtLClAljgU9ALBhjv8n/DDB/btZOZwrMB9Ef8X9IMyBRsVN1bFSI i0Ag== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=MLCTVGvQiuHnJPyCKcNxKRFg4bqjUrIJf0Fm+LcW4EY=; fh=S5uIPWDUzk7FflhWnwPRjhMVR5KwoSn8w9a4860VNJg=; b=eRRP91LsE0SXGsk5GaR+7JuqK39gImnmLbYrtTmP9mU2VD1bDXbtf/TeumpkZ+/D9P relBeWxozMqPHtGNklk/aSa1xAZ3EUNfy0jRqFUdTuaOMy27K9lg1NiFEJ90khELLwp9 PMy7kEYv3xGerYTekqTqTXeFMqTj6AYg86TZmB3ln7aReJSky28ZNP11HcdegNxzPe8D 9yde7B/eGZ+wsF/Aw3bU/O7yumL28RVNY+xRlc1Ms2U6tmpbinp/RTE9Smj82Q1YppPw BzpTE52Htc3dWxWeTeB4EDxN7ba8aVsbDVGp1iSQHUuDVy2/jDL+XE9yKrmi7lnZMvzH Cb3w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@baylibre-com.20230601.gappssmtp.com header.s=20230601 header.b="I/gD1Y49"; arc=pass (i=1 spf=pass spfdomain=baylibre.com dkim=pass dkdomain=baylibre-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-72065-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-72065-ouuuleilei=gmail.com@vger.kernel.org" Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id i17-20020a170906251100b00a3ebecae05bsi865212ejb.797.2024.02.19.14.36.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Feb 2024 14:36:13 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-72065-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@baylibre-com.20230601.gappssmtp.com header.s=20230601 header.b="I/gD1Y49"; arc=pass (i=1 spf=pass spfdomain=baylibre.com dkim=pass dkdomain=baylibre-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-72065-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-72065-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id E6C771F235B8 for ; Mon, 19 Feb 2024 22:36:12 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 83B4E58212; Mon, 19 Feb 2024 22:35:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="I/gD1Y49" Received: from mail-ot1-f49.google.com (mail-ot1-f49.google.com [209.85.210.49]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 294B355C36 for ; Mon, 19 Feb 2024 22:34:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.49 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708382100; cv=none; b=sPqO/NPZg5y6ivtwupXiiwExCrLW0fWsA4CCOv1aoQPTq9c/rnQGmkDGXgtOijMNfRUlh2poUHKofHnkoYgrJyRX5x9fwhSzBtgTkcjXwTrbRbQLummE3sEaAaAiLv2S+N6U5KV955kTaD5/IS4YkGxbe3zrpjFzXejJ5YyteeU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708382100; c=relaxed/simple; bh=P7oCg3suc1nu3j3ycQVGLb7qbHEAwvKuiRYXbHJEPwg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=AKC1pJu7Xhx7OUVvmrgZ8YKx7QOn28wZSnooPIcC3s+eYXF/VmyQbc4cEUWkofxige4Wz18S4uzwp4jwSIeb4aaYSM9jmAr42Le5PavNRoB8nmdISEk0owvF6apl90KtWkhV9hx8UqIJAoX4l1vUaeELPqdqpAXG9c5kpFeHaoc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=I/gD1Y49; arc=none smtp.client-ip=209.85.210.49 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Received: by mail-ot1-f49.google.com with SMTP id 46e09a7af769-6e0f43074edso2800284a34.1 for ; Mon, 19 Feb 2024 14:34:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1708382097; x=1708986897; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=MLCTVGvQiuHnJPyCKcNxKRFg4bqjUrIJf0Fm+LcW4EY=; b=I/gD1Y49qIMdE9QWMKXtpCPdf5YhSsjzOEpGhh44ojtUCQUNgPLjUgMgSAx7J65u9V Nwra8qR6QiInPodldmhmOSEu46tdSlQ6aVTQBIIicRGluV3l3Gv96oG4NxO+bp+tf/F6 RhkUnBDsdmME2YK0kOCy6coKsuwrPrkOEjWChZuw1k6gcIjcwvx6g5cXz+GuJuOXDVU+ 8/KNeF/HwxVyoysjsBT943OIQhtk84fzTSDDuyzJMZMTPblFDbAe/7HnIfO3Bdiaqal+ WGlkhlm74RREOy77KtvxIMea5TnatfdiItmVO9qrKlZVTKfrp14Mo1+bg3qOP7fB7BMW 7g4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708382097; x=1708986897; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=MLCTVGvQiuHnJPyCKcNxKRFg4bqjUrIJf0Fm+LcW4EY=; b=RTdOQ5GUgLtV8nn0kXZiCBWGdfHkjov2TI9FHQyMLlbFMRfhPTzOeWueFq28lwEZc/ lODQNL0cv3Zxo0WLKgpeNV3U9+1RoJKAG7x6HsZ0Tj+L2yE5YnEN+dMbvD/NnRyUKcaF PZ1vmhvXwXQC55ue/XyEimgIa47ioHu3+6TrcNvWG/MN3P61kwiEcIFQ69T+WTS0OVkz aWYoJbAqRfg42SssKjVDk6EH0g4jlyZy/WgSYPYWdUwP8LBlvVxwB5pc4MrX51J0d7nN tEe0G0fK9wPA5GCnugjIMFKWVll3rcwgOWhqA9tymdwtw3ZXjatYfKVn2rZ2nUl2/1wK iipw== X-Forwarded-Encrypted: i=1; AJvYcCUtxSzDCoeOyYMYTvb9NSrv3Y8T+OnU0BFGT6Zzy9hSuC6+sdWhYQjoHXQripzTzUF4kU9mUsJLDvidwZi6nnUgOdwhIGYyDitrGxit X-Gm-Message-State: AOJu0YyJx0/K4SlbJHCOls2wv0GNjB8S+m1/tkMRR4QXPelzZpz31bRd pu4521q6r53LBCmadcBizAdkhRGZVhNN036SFM/FrIewzsa/NV6QB7K/pF6ahEg= X-Received: by 2002:a9d:6a8c:0:b0:6e2:dd76:1ee0 with SMTP id l12-20020a9d6a8c000000b006e2dd761ee0mr12143722otq.35.1708382097231; Mon, 19 Feb 2024 14:34:57 -0800 (PST) Received: from freyr.lechnology.com (ip98-183-112-25.ok.ok.cox.net. [98.183.112.25]) by smtp.gmail.com with ESMTPSA id h25-20020a9d6419000000b006e45a5f0a70sm171776otl.49.2024.02.19.14.34.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Feb 2024 14:34:56 -0800 (PST) From: David Lechner To: Mark Brown Cc: David Lechner , Martin Sperl , David Jander , Jonathan Cameron , Michael Hennerich , =?utf-8?q?Nuno_S=C3=A1?= , Alain Volmat , Maxime Coquelin , Alexandre Torgue , linux-spi@vger.kernel.org, linux-kernel@vger.kernel.org, linux-stm32@st-md-mailman.stormreply.com, linux-arm-kernel@lists.infradead.org, linux-iio@vger.kernel.org Subject: [PATCH v2 1/5] spi: add spi_optimize_message() APIs Date: Mon, 19 Feb 2024 16:33:18 -0600 Message-ID: <20240219-mainline-spi-precook-message-v2-1-4a762c6701b9@baylibre.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240219-mainline-spi-precook-message-v2-0-4a762c6701b9@baylibre.com> References: <20240219-mainline-spi-precook-message-v2-0-4a762c6701b9@baylibre.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Mailer: b4 0.12.4 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791368545750259446 X-GMAIL-MSGID: 1791368545750259446 This adds a new spi_optimize_message() function that can be used to optimize SPI messages that are used more than once. Peripheral drivers that use the same message multiple times can use this API to perform SPI message validation and controller-specific optimizations once and then reuse the message while avoiding the overhead of revalidating the message on each spi_(a)sync() call. Internally, the SPI core will also call this function for each message if the peripheral driver did not explicitly call it. This is done to so that controller drivers don't have to have multiple code paths for optimized and non-optimized messages. A hook is provided for controller drivers to perform controller-specific optimizations. Suggested-by: Martin Sperl Link: https://lore.kernel.org/linux-spi/39DEC004-10A1-47EF-9D77-276188D2580C@martin.sperl.org/ Signed-off-by: David Lechner Reviewed-by: Jonathan Cameron --- v2 changes: - Removed pre_optimized parameter from __spi_optimize_message() - Added comment explaining purpose of pre_optimized flag - Fixed missing doc comment for @pre_optimized - Removed kernel doc inclusion (/** -> /*) from static members - Removed unrelated comment about calling spi_finalize_current_message() drivers/spi/spi.c | 151 ++++++++++++++++++++++++++++++++++++++++++++++-- include/linux/spi/spi.h | 20 +++++++ 2 files changed, 167 insertions(+), 4 deletions(-) diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c index c2b10e2c75f0..f68d92b57543 100644 --- a/drivers/spi/spi.c +++ b/drivers/spi/spi.c @@ -2106,6 +2106,41 @@ struct spi_message *spi_get_next_queued_message(struct spi_controller *ctlr) } EXPORT_SYMBOL_GPL(spi_get_next_queued_message); +/* + * __spi_unoptimize_message - shared implementation of spi_unoptimize_message() + * and spi_maybe_unoptimize_message() + * @msg: the message to unoptimize + * + * Peripheral drivers should use spi_unoptimize_message() and callers inside + * core should use spi_maybe_unoptimize_message() rather than calling this + * function directly. + * + * It is not valid to call this on a message that is not currently optimized. + */ +static void __spi_unoptimize_message(struct spi_message *msg) +{ + struct spi_controller *ctlr = msg->spi->controller; + + if (ctlr->unoptimize_message) + ctlr->unoptimize_message(msg); + + msg->optimized = false; + msg->opt_state = NULL; +} + +/* + * spi_maybe_unoptimize_message - unoptimize msg not managed by a peripheral + * @msg: the message to unoptimize + * + * This function is used to unoptimize a message if and only if it was + * optimized by the core (via spi_maybe_optimize_message()). + */ +static void spi_maybe_unoptimize_message(struct spi_message *msg) +{ + if (!msg->pre_optimized && msg->optimized) + __spi_unoptimize_message(msg); +} + /** * spi_finalize_current_message() - the current message is complete * @ctlr: the controller to return the message to @@ -2153,6 +2188,8 @@ void spi_finalize_current_message(struct spi_controller *ctlr) mesg->prepared = false; + spi_maybe_unoptimize_message(mesg); + WRITE_ONCE(ctlr->cur_msg_incomplete, false); smp_mb(); /* See __spi_pump_transfer_message()... */ if (READ_ONCE(ctlr->cur_msg_need_completion)) @@ -4194,6 +4231,110 @@ static int __spi_validate(struct spi_device *spi, struct spi_message *message) return 0; } +/* + * __spi_optimize_message - shared implementation for spi_optimize_message() + * and spi_maybe_optimize_message() + * @spi: the device that will be used for the message + * @msg: the message to optimize + * + * Peripheral drivers will call spi_optimize_message() and the spi core will + * call spi_maybe_optimize_message() instead of calling this directly. + * + * It is not valid to call this on a message that has already been optimized. + * + * Return: zero on success, else a negative error code + */ +static int __spi_optimize_message(struct spi_device *spi, + struct spi_message *msg) +{ + struct spi_controller *ctlr = spi->controller; + int ret; + + ret = __spi_validate(spi, msg); + if (ret) + return ret; + + if (ctlr->optimize_message) { + ret = ctlr->optimize_message(msg); + if (ret) + return ret; + } + + msg->optimized = true; + + return 0; +} + +/* + * spi_maybe_optimize_message - optimize message if it isn't already pre-optimized + * @spi: the device that will be used for the message + * @msg: the message to optimize + * Return: zero on success, else a negative error code + */ +static int spi_maybe_optimize_message(struct spi_device *spi, + struct spi_message *msg) +{ + if (msg->pre_optimized) + return 0; + + return __spi_optimize_message(spi, msg); +} + +/** + * spi_optimize_message - do any one-time validation and setup for a SPI message + * @spi: the device that will be used for the message + * @msg: the message to optimize + * + * Peripheral drivers that reuse the same message repeatedly may call this to + * perform as much message prep as possible once, rather than repeating it each + * time a message transfer is performed to improve throughput and reduce CPU + * usage. + * + * Once a message has been optimized, it cannot be modified with the exception + * of updating the contents of any xfer->tx_buf (the pointer can't be changed, + * only the data in the memory it points to). + * + * Calls to this function must be balanced with calls to spi_unoptimize_message() + * to avoid leaking resources. + * + * Context: can sleep + * Return: zero on success, else a negative error code + */ +int spi_optimize_message(struct spi_device *spi, struct spi_message *msg) +{ + int ret; + + ret = __spi_optimize_message(spi, msg); + if (ret) + return ret; + + /* + * This flag indicates that the peripheral driver called spi_optimize_message() + * and therefore we shouldn't unoptimize message automatically when finalizing + * the message but rather wait until spi_unoptimize_message() is called + * by the peripheral driver. + */ + msg->pre_optimized = true; + + return 0; +} +EXPORT_SYMBOL_GPL(spi_optimize_message); + +/** + * spi_unoptimize_message - releases any resources allocated by spi_optimize_message() + * @msg: the message to unoptimize + * + * Calls to this function must be balanced with calls to spi_optimize_message(). + * + * Context: can sleep + */ +void spi_unoptimize_message(struct spi_message *msg) +{ + __spi_unoptimize_message(msg); + msg->pre_optimized = false; +} +EXPORT_SYMBOL_GPL(spi_unoptimize_message); + static int __spi_async(struct spi_device *spi, struct spi_message *message) { struct spi_controller *ctlr = spi->controller; @@ -4258,8 +4399,8 @@ int spi_async(struct spi_device *spi, struct spi_message *message) int ret; unsigned long flags; - ret = __spi_validate(spi, message); - if (ret != 0) + ret = spi_maybe_optimize_message(spi, message); + if (ret) return ret; spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); @@ -4271,6 +4412,8 @@ int spi_async(struct spi_device *spi, struct spi_message *message) spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); + spi_maybe_unoptimize_message(message); + return ret; } EXPORT_SYMBOL_GPL(spi_async); @@ -4331,8 +4474,8 @@ static int __spi_sync(struct spi_device *spi, struct spi_message *message) return -ESHUTDOWN; } - status = __spi_validate(spi, message); - if (status != 0) + status = spi_maybe_optimize_message(spi, message); + if (status) return status; SPI_STATISTICS_INCREMENT_FIELD(ctlr->pcpu_statistics, spi_sync); diff --git a/include/linux/spi/spi.h b/include/linux/spi/spi.h index 2b8e2746769a..ddfb66dd4caf 100644 --- a/include/linux/spi/spi.h +++ b/include/linux/spi/spi.h @@ -475,6 +475,8 @@ extern struct spi_device *spi_new_ancillary_device(struct spi_device *spi, u8 ch * * @set_cs: set the logic level of the chip select line. May be called * from interrupt context. + * @optimize_message: optimize the message for reuse + * @unoptimize_message: release resources allocated by optimize_message * @prepare_message: set up the controller to transfer a single message, * for example doing DMA mapping. Called from threaded * context. @@ -715,6 +717,8 @@ struct spi_controller { struct completion xfer_completion; size_t max_dma_len; + int (*optimize_message)(struct spi_message *msg); + int (*unoptimize_message)(struct spi_message *msg); int (*prepare_transfer_hardware)(struct spi_controller *ctlr); int (*transfer_one_message)(struct spi_controller *ctlr, struct spi_message *mesg); @@ -1111,6 +1115,8 @@ struct spi_transfer { * @spi: SPI device to which the transaction is queued * @is_dma_mapped: if true, the caller provided both DMA and CPU virtual * addresses for each transfer buffer + * @pre_optimized: peripheral driver pre-optimized the message + * @optimized: the message is in the optimized state * @prepared: spi_prepare_message was called for the this message * @status: zero for success, else negative errno * @complete: called to report transaction completions @@ -1120,6 +1126,7 @@ struct spi_transfer { * successful segments * @queue: for use by whichever driver currently owns the message * @state: for use by whichever driver currently owns the message + * @opt_state: for use by whichever driver currently owns the message * @resources: for resource management when the SPI message is processed * * A @spi_message is used to execute an atomic sequence of data transfers, @@ -1143,6 +1150,11 @@ struct spi_message { unsigned is_dma_mapped:1; + /* spi_optimize_message() was called for this message */ + bool pre_optimized; + /* __spi_optimize_message() was called for this message */ + bool optimized; + /* spi_prepare_message() was called for this message */ bool prepared; @@ -1172,6 +1184,11 @@ struct spi_message { */ struct list_head queue; void *state; + /* + * Optional state for use by controller driver between calls to + * __spi_optimize_message() and __spi_unoptimize_message(). + */ + void *opt_state; /* List of spi_res resources when the SPI message is processed */ struct list_head resources; @@ -1255,6 +1272,9 @@ static inline void spi_message_free(struct spi_message *m) kfree(m); } +extern int spi_optimize_message(struct spi_device *spi, struct spi_message *msg); +extern void spi_unoptimize_message(struct spi_message *msg); + extern int spi_setup(struct spi_device *spi); extern int spi_async(struct spi_device *spi, struct spi_message *message); extern int spi_slave_abort(struct spi_device *spi); From patchwork Mon Feb 19 22:33:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Lechner X-Patchwork-Id: 203295 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp73392dyc; Mon, 19 Feb 2024 14:36:01 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUusKVwF1B1zPjM4RX7cVbMGo9uBG2J51PFku3vJfOLskXfsxZzaDTVT/S3le3JJ1OtkCzA03YnSavgAnDb6pgxhzUnFA== X-Google-Smtp-Source: AGHT+IFV85XZL+hglJ55PP7cnhYK4j7rNvpctBsEzkKtbwGPAuqkMlrF3UFB9pB9+gLAb8t36q9z X-Received: by 2002:a25:bccf:0:b0:dc7:45f4:44f7 with SMTP id l15-20020a25bccf000000b00dc745f444f7mr9144172ybm.14.1708382161086; Mon, 19 Feb 2024 14:36:01 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708382161; cv=pass; d=google.com; s=arc-20160816; b=VvzOMHo4ekfXo1mT9/iwkB2i690FH9+kJTOGvsEubXW4Lu8v30JYeZWrO95dYN3E60 p8pVfZlqYtjFIG/IBpAtzX0kUMKGnKB+b98Eg8nyu2G3shsKqTlzog3jtNpF7GfNvCa6 hZ1cI7c67xCjWXghmVv5IF3zJVyAo2d+GPsMfF5bTTJG2GcdSPEZ0u3mZvqH1gg8QTnh hEptTQXWJO0xN0vJTkMHGbROZUfeCtt85nOFHlnCSelvD61UAO8sVOKNWxoDhwk/qYH7 /e+OTHYPNnVBgrb/mZjDOWOGmgBv8pRqwhLWNZnhHM0jSReUxsw58jzfBZDBmI8f0u0m qTXw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=MXFb+RtnzRHPAv3WrnV5x/KjkLxl12244GFTo5rtzY0=; fh=oYR8g7IDVLpcyR8CfwRNmLCZyk6/zhkNRecHFMULs1A=; b=qpVL+jrdHFK5SOmRQzlVn8zGnvB4n8XklvvbNhQnJG/Kt3hg6oW6vTgKE8xE0g/Tck EJDZnI5RtfRnL/ILnqI8nvE8IsB3sLavdzWCdTM+O53D1fpshvWJZpZJBqGZxUraVOMT qA31QhOWFFpm/BQOZ5+o0loJ+ycV1LOx7tLNFsS/6pZ3LFUuY15B4efOKJXDjgR5DgyT uhG9XSK7x3c9K+DbxUgA9eufloElrXJTn6Se6KWmAB7IL4CTMcA+Yp/SUYvqWTubjGxh GNTMgAIXb+XoKdghHCMxhmhbgcVIaVumbUImz3LFM1zKjLlGpQTupvhbBjau3cuPd427 lR7w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@baylibre-com.20230601.gappssmtp.com header.s=20230601 header.b=EXC9etBQ; arc=pass (i=1 spf=pass spfdomain=baylibre.com dkim=pass dkdomain=baylibre-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-72066-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-72066-ouuuleilei=gmail.com@vger.kernel.org" Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id c12-20020a05620a134c00b00787221ee5acsi7376811qkl.579.2024.02.19.14.36.00 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Feb 2024 14:36:01 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-72066-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@baylibre-com.20230601.gappssmtp.com header.s=20230601 header.b=EXC9etBQ; arc=pass (i=1 spf=pass spfdomain=baylibre.com dkim=pass dkdomain=baylibre-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-72066-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-72066-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id C25A41C20EF5 for ; Mon, 19 Feb 2024 22:36:00 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id CC72C58109; Mon, 19 Feb 2024 22:35:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="EXC9etBQ" Received: from mail-ot1-f43.google.com (mail-ot1-f43.google.com [209.85.210.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 05BA120335 for ; Mon, 19 Feb 2024 22:34:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708382100; cv=none; b=ur3w6EBn1XVOaxa6sSBpAMnAAhK4Z1Ja8OD1qlbZub/AFSRfNq+lR413R2DXzP6FpZAaxftaTrV0HaxaVS0lCBoGQEOX1Qa4A3BV2k7bd3PAOO/IYxpOuWq9X+j/K9xMVK4hMwz31+fUZ6r0TAi6MBo7hn84uNjKri3zzHgSvyA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708382100; c=relaxed/simple; bh=sSmWV+EMEyVWLEUpEqFePlqyEc61WeEx3NpFQkH6xKY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=X8d5x0ntxNCdazSUrGadccWn8bgdvTKOkzRkzCwZqD5IwFLiiPDCh0drwOvsueYidvCKhLNRbgT+Rq0hZW+pF+KEdxPqgls42SO00WELgXZaZ4PgHlktGduVFGnPt/ZTkN7ZSsEMl8HzN/XY22XWhJ1BOVgiL1LwiusJ/oitUvI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=EXC9etBQ; arc=none smtp.client-ip=209.85.210.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Received: by mail-ot1-f43.google.com with SMTP id 46e09a7af769-6e2e5824687so1587111a34.0 for ; Mon, 19 Feb 2024 14:34:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1708382098; x=1708986898; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=MXFb+RtnzRHPAv3WrnV5x/KjkLxl12244GFTo5rtzY0=; b=EXC9etBQKuw9L0LX5Qq5BQ1xKiKBy+qFfdRsxfHU1o+W1NGtdFoNY3kdY9i2AgzpRv gyJz3awidTYySxFx2SVXhjEc+HPhy1u4gk+DMdubyd+bCto/pBVlLv6YDiSl0dbo6M/7 zUVHOAHEnS5yMzF2/eMWuDAhe7NVZqU8gU10JuwO+56DwPJSNU+Z9ZGd+U2riHa6aAYT f43C9/mkSw1jmyWT1m7Y+zJ3lGf0KwbLlMEEuKQxcizRETvUAFXlrU6mcMl50oHbxq3E ljQPSbRKtRnon/52XAt8bscc7T17HUeYWEes12ilK4xbVq1do4EPY/vivIjNXPdcCPB/ 87Fw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708382098; x=1708986898; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=MXFb+RtnzRHPAv3WrnV5x/KjkLxl12244GFTo5rtzY0=; b=NdvcQLSfj+vHWSdr6Vva/oY0dqn8jAI/AYYmMEtV1f/AeYIZ5OiJNpQZe6po7dHpWE P76zY+SsR7dNVPLV5/besUwJXSDUWsvUNvkBgIxaU5wlJf3onUGuRaEVDtt2eUnWid9Z kFBfzqkR6dmARDvLX2KirY47xvhCfa50h9mZuUpF8DmvLtGxFIkW28iJ0hPBodEwAxSm AfKYlQHgrdfwQJhAroF58O+0PZKCEsJd/yxhBr5MJ6xx4tomOJTROrkAEj+d6Czc9H8T IiNfxS0UOZWrlQhQUXu2NmvnIXHkbXfS7H4DPA6MK9uUDEVfvgarCU+mLfOyGESQ5MUV C3cA== X-Forwarded-Encrypted: i=1; AJvYcCVLoosRaePhRDa3nFZOv23kQTNGoUvlTTtxZ28FBSZx1WMkDFm+lAN3wrqxQ/D/eiNTynM1kRq2bzjb4e2+cnIb+pz0xOq/G4S9bms4 X-Gm-Message-State: AOJu0YwAUvVeJDpy7kkIOHLufOG9+7GD/kvQzZHRBbyfYBg7DOTpij39 qbzqrhbGmDCIvLa77Q1+rDAYc7r0X0/zQMOf0VzXJhfcWlqxy4myPXsE8DL4Ou4= X-Received: by 2002:a05:6830:1355:b0:6e2:dd64:c53e with SMTP id r21-20020a056830135500b006e2dd64c53emr12780325otq.2.1708382098245; Mon, 19 Feb 2024 14:34:58 -0800 (PST) Received: from freyr.lechnology.com (ip98-183-112-25.ok.ok.cox.net. [98.183.112.25]) by smtp.gmail.com with ESMTPSA id h25-20020a9d6419000000b006e45a5f0a70sm171776otl.49.2024.02.19.14.34.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Feb 2024 14:34:57 -0800 (PST) From: David Lechner To: Mark Brown Cc: David Lechner , Martin Sperl , David Jander , Jonathan Cameron , Michael Hennerich , =?utf-8?q?Nuno_S=C3=A1?= , Alain Volmat , Maxime Coquelin , Alexandre Torgue , linux-spi@vger.kernel.org, linux-kernel@vger.kernel.org, linux-stm32@st-md-mailman.stormreply.com, linux-arm-kernel@lists.infradead.org, linux-iio@vger.kernel.org, Jonathan Cameron , Julien Stephan Subject: [PATCH v2 2/5] spi: move splitting transfers to spi_optimize_message() Date: Mon, 19 Feb 2024 16:33:19 -0600 Message-ID: <20240219-mainline-spi-precook-message-v2-2-4a762c6701b9@baylibre.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240219-mainline-spi-precook-message-v2-0-4a762c6701b9@baylibre.com> References: <20240219-mainline-spi-precook-message-v2-0-4a762c6701b9@baylibre.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Mailer: b4 0.12.4 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791368533256807290 X-GMAIL-MSGID: 1791368533256807290 Splitting transfers is an expensive operation so we can potentially optimize it by doing it only once per optimization of the message instead of repeating each time the message is transferred. The transfer splitting functions are currently the only user of spi_res_alloc() so spi_res_release() can be safely moved at this time from spi_finalize_current_message() to spi_unoptimize_message(). The doc comments of the public functions for splitting transfers are also updated so that callers will know when it is safe to call them to ensure proper resource management. Reviewed-by: Jonathan Cameron Signed-off-by: David Lechner Acked-by: Nuno Sa --- v2 changes: - Changed line break for multiline if condition - Removed kernel doc inclusion (/** -> /*) from static members - Picked up Jonathan's Reviewed-by drivers/spi/spi.c | 110 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 68 insertions(+), 42 deletions(-) diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c index f68d92b57543..ba4d3fde2054 100644 --- a/drivers/spi/spi.c +++ b/drivers/spi/spi.c @@ -1747,38 +1747,6 @@ static int __spi_pump_transfer_message(struct spi_controller *ctlr, trace_spi_message_start(msg); - /* - * If an SPI controller does not support toggling the CS line on each - * transfer (indicated by the SPI_CS_WORD flag) or we are using a GPIO - * for the CS line, we can emulate the CS-per-word hardware function by - * splitting transfers into one-word transfers and ensuring that - * cs_change is set for each transfer. - */ - if ((msg->spi->mode & SPI_CS_WORD) && (!(ctlr->mode_bits & SPI_CS_WORD) || - spi_is_csgpiod(msg->spi))) { - ret = spi_split_transfers_maxwords(ctlr, msg, 1); - if (ret) { - msg->status = ret; - spi_finalize_current_message(ctlr); - return ret; - } - - list_for_each_entry(xfer, &msg->transfers, transfer_list) { - /* Don't change cs_change on the last entry in the list */ - if (list_is_last(&xfer->transfer_list, &msg->transfers)) - break; - xfer->cs_change = 1; - } - } else { - ret = spi_split_transfers_maxsize(ctlr, msg, - spi_max_transfer_size(msg->spi)); - if (ret) { - msg->status = ret; - spi_finalize_current_message(ctlr); - return ret; - } - } - if (ctlr->prepare_message) { ret = ctlr->prepare_message(ctlr, msg); if (ret) { @@ -2124,6 +2092,8 @@ static void __spi_unoptimize_message(struct spi_message *msg) if (ctlr->unoptimize_message) ctlr->unoptimize_message(msg); + spi_res_release(ctlr, msg); + msg->optimized = false; msg->opt_state = NULL; } @@ -2169,15 +2139,6 @@ void spi_finalize_current_message(struct spi_controller *ctlr) spi_unmap_msg(ctlr, mesg); - /* - * In the prepare_messages callback the SPI bus has the opportunity - * to split a transfer to smaller chunks. - * - * Release the split transfers here since spi_map_msg() is done on - * the split transfers. - */ - spi_res_release(ctlr, mesg); - if (mesg->prepared && ctlr->unprepare_message) { ret = ctlr->unprepare_message(ctlr, mesg); if (ret) { @@ -3819,6 +3780,10 @@ static int __spi_split_transfer_maxsize(struct spi_controller *ctlr, * @msg: the @spi_message to transform * @maxsize: the maximum when to apply this * + * This function allocates resources that are automatically freed during the + * spi message unoptimize phase so this function should only be called from + * optimize_message callbacks. + * * Return: status of transformation */ int spi_split_transfers_maxsize(struct spi_controller *ctlr, @@ -3857,6 +3822,10 @@ EXPORT_SYMBOL_GPL(spi_split_transfers_maxsize); * @msg: the @spi_message to transform * @maxwords: the number of words to limit each transfer to * + * This function allocates resources that are automatically freed during the + * spi message unoptimize phase so this function should only be called from + * optimize_message callbacks. + * * Return: status of transformation */ int spi_split_transfers_maxwords(struct spi_controller *ctlr, @@ -4231,6 +4200,57 @@ static int __spi_validate(struct spi_device *spi, struct spi_message *message) return 0; } +/* + * spi_split_transfers - generic handling of transfer splitting + * @msg: the message to split + * + * Under certain conditions, a SPI controller may not support arbitrary + * transfer sizes or other features required by a peripheral. This function + * will split the transfers in the message into smaller transfers that are + * supported by the controller. + * + * Controllers with special requirements not covered here can also split + * transfers in the optimize_message() callback. + * + * Context: can sleep + * Return: zero on success, else a negative error code + */ +static int spi_split_transfers(struct spi_message *msg) +{ + struct spi_controller *ctlr = msg->spi->controller; + struct spi_transfer *xfer; + int ret; + + /* + * If an SPI controller does not support toggling the CS line on each + * transfer (indicated by the SPI_CS_WORD flag) or we are using a GPIO + * for the CS line, we can emulate the CS-per-word hardware function by + * splitting transfers into one-word transfers and ensuring that + * cs_change is set for each transfer. + */ + if ((msg->spi->mode & SPI_CS_WORD) && + (!(ctlr->mode_bits & SPI_CS_WORD) || spi_is_csgpiod(msg->spi))) { + ret = spi_split_transfers_maxwords(ctlr, msg, 1); + if (ret) + return ret; + + list_for_each_entry(xfer, &msg->transfers, transfer_list) { + /* Don't change cs_change on the last entry in the list */ + if (list_is_last(&xfer->transfer_list, &msg->transfers)) + break; + + xfer->cs_change = 1; + } + } else { + ret = spi_split_transfers_maxsize(ctlr, msg, + spi_max_transfer_size(msg->spi)); + if (ret) + return ret; + } + + return 0; +} + /* * __spi_optimize_message - shared implementation for spi_optimize_message() * and spi_maybe_optimize_message() @@ -4254,10 +4274,16 @@ static int __spi_optimize_message(struct spi_device *spi, if (ret) return ret; + ret = spi_split_transfers(msg); + if (ret) + return ret; + if (ctlr->optimize_message) { ret = ctlr->optimize_message(msg); - if (ret) + if (ret) { + spi_res_release(ctlr, msg); return ret; + } } msg->optimized = true; From patchwork Mon Feb 19 22:33:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Lechner X-Patchwork-Id: 203297 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp73481dyc; Mon, 19 Feb 2024 14:36:15 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUbUL4fPQvJXrEavgHmAFXZKsq38jaUFSZm5hgGFyqZYQPQwg5bwbySKnOAtOhvaiG+oDW3fnlOejSfPv8MAojTNj/zxg== X-Google-Smtp-Source: AGHT+IENcNGl53TKSwbYPXm/HTx/uv+yrWUTpizFGVruYUeN802cttisTpdN9hyK4hxOMEWjBnUe X-Received: by 2002:a17:902:ce01:b0:1db:8eb2:9a11 with SMTP id k1-20020a170902ce0100b001db8eb29a11mr14962383plg.53.1708382175652; Mon, 19 Feb 2024 14:36:15 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708382175; cv=pass; d=google.com; s=arc-20160816; b=ZJJ1bE6TMqCbgD8ZZYgMUWRNcbXisewjt8GcAQzvgwjmvKHIn4AprLWg/dfYcOJtYM noOwsoMQCHYoiyjx+JTHa8Aox4vSTgMhdctJXjVAgqhPuIkasinSlytz/dwHndhGWq52 RmMuRqCmiNCDhagXrGifa7pYojvg0Wrl2Y7grtehubC24AfICkmRHiR92nfKm9Ee+CdX vHmPAb55LlivROhhhdE7dW0EAcmWkE61zPyMPimoDRiqnZ5GznGELMpYLu9/4+BBXq93 3T8ar6jsnGRGKWmjXBIaC/yEHqRfD1PZVmOtIU2Wyq2szRuDy4aZhmCKBD09422iwgql b7OQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=aiEnyHrsiH+BHrvR4qpLSBhsawzqJKIHbW1KRtu4mJg=; fh=JaPokbaD7YnO8GqCNf5WJX2Lk2nc0my/2/vYo3UZvjg=; b=zmjdxCexgKmlhjqZDEAklRy1TqcrxsWTR03NiSIECrnfvdNjyd2j85PfHXln5VARpY AVfJhZSbef2p0Q8ZqUk0gMtSb9uwoV3KN90ITloIjwXXRxY7n2l1pPP3qoE77iNhjjLd o7tsccoEh3veCmzAO/sg7f7o3gbMEQ8XzF4LgJfd0prGo59FbA2nZB7Ebns7Rq4+6XrG r4As4G7nSVuZhrs2DX1jXgaJmPXY3iVZJOTU++IJcVrRWgsAcmH9t0VqC1+8Y8np16iS pj2uiyR/n+mVwFqb/XrJnk7sLgNWKBvpqrjebUHr1BOxQVIPI+LxpMEdalKXSWTD7PJ4 ivRw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@baylibre-com.20230601.gappssmtp.com header.s=20230601 header.b="W+6Fy/Tf"; arc=pass (i=1 spf=pass spfdomain=baylibre.com dkim=pass dkdomain=baylibre-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-72067-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-72067-ouuuleilei=gmail.com@vger.kernel.org" Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id l4-20020a170902f68400b001d7857c99d0si5161820plg.282.2024.02.19.14.36.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Feb 2024 14:36:15 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-72067-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@baylibre-com.20230601.gappssmtp.com header.s=20230601 header.b="W+6Fy/Tf"; arc=pass (i=1 spf=pass spfdomain=baylibre.com dkim=pass dkdomain=baylibre-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-72067-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-72067-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id E27FE2833BF for ; Mon, 19 Feb 2024 22:36:12 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AD02B58222; Mon, 19 Feb 2024 22:35:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="W+6Fy/Tf" Received: from mail-ot1-f42.google.com (mail-ot1-f42.google.com [209.85.210.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0C52855E75 for ; Mon, 19 Feb 2024 22:34:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708382102; cv=none; b=shF9yososTtIqQc6sPGcBierlRCZ392+S0g549btyRhpzQE47qBmOAnrpjN6dlgCFQqjck5frbDgjQfIk+Cm4xQMFKbiyOQgZHtoQr/MrWBlxIhPiWpyrIehaUAXTYdkUh/vaaAhYxYKjn+fU/YRXY2X6ubcJIfYx7z1C11BZVg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708382102; c=relaxed/simple; bh=ha0zApPrKXA3SwGVUgjy23s32EsrJI2UFXFPZrDIckk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=Pr6tmjGXS6lnlVIp6WTydGv4wEEiJAsJTMTaQGlPzk7Gcr1QG04zLTSadPyW0j+bLPfpGG/wgDxoKWT6c7X+AI7e1Z2BIJQOMxRbsdFElICBHSyDzcu6WnthpzI43EjpzQYKXS3dRLlakpE5/+mXpsz+HDyLs/NArOHwc6pcyfE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=W+6Fy/Tf; arc=none smtp.client-ip=209.85.210.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Received: by mail-ot1-f42.google.com with SMTP id 46e09a7af769-6e2edb28554so1450598a34.2 for ; Mon, 19 Feb 2024 14:34:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1708382099; x=1708986899; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=aiEnyHrsiH+BHrvR4qpLSBhsawzqJKIHbW1KRtu4mJg=; b=W+6Fy/TfrSv1+iMfwiiv2OlGyCMpy/mRk2YFAnCsXFBUNS1uieUojpJwUXgyKk0wzK VZ6JUp7kC8SzHDZDdO8g/xakacMuyjd4LtSlmNbP3vK2lXD7TRyjVZnAEvasK1/ESpqg OrabYAMTJdFlCdJ8QMjv4U3SvU2ZwH57XD2oUXtCJ10XQROuk0n2mARelqfIQoENCvpS NFqxQkKlZFt08J5f+tghI8lBTjZ2Y9q7sGlO+ABZZq0FYVsVNyxHxeUhtafQOXMplfhJ dEnUi8VKmeVtFABxUeHKW3WVyxHFolu6c+yt+SOQDoGjuuEe/4gtBSUtT6/xjcYgalek HNUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708382099; x=1708986899; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=aiEnyHrsiH+BHrvR4qpLSBhsawzqJKIHbW1KRtu4mJg=; b=JR0fGokP4lSdv5EHSRs5Sqyy2gLh15R3udH/6N5LI0g8cxtGBeNe085GdpOB7vIdWj oHj6SkM8s75rksF5z+FKHIi9BupfDakt687uc891OA2HU3kEH8TuX/AaHjaPsM9qt9jv y6kcup8EL3494UjOVTv9lmZPrOD0bq/0WNaloRAMv4SK0JAUUVC6+CN06Dhx4+pGWW9L GWpk9uzXdrGP9T6RoRcLtnTz0vo4QiRuTOVeua+SKI9I0Z7luQyp7e5pvMfcNugKKDgt hC+dnFsh3goOgyifanumjNg+tsZSBLuswPrHC8K49kffxGG0SbHScub1WtOWtqVHBWwD eFOQ== X-Forwarded-Encrypted: i=1; AJvYcCWcHMxtX8S7M9uL4kKGlAL+BCRF+/wN98kGeARYefAU8BoKXSbS//iUqsu0iWyd2HuPgDjkaiXlkvBqoab+g7yezdx8RlXoLzv0vdYt X-Gm-Message-State: AOJu0YxOKyRzE8ixDbTnrRdpOIaR8OIzm0LGYYFJfgkT+VCkdurMRL0q AyxlrO38g0o6UhQ/+4LKXMQ9hxvVbhE6xbjkzbpMGfgmo8m4jzwA/UHcQgEOjek= X-Received: by 2002:a9d:7a97:0:b0:6e2:d93f:376 with SMTP id l23-20020a9d7a97000000b006e2d93f0376mr13724839otn.8.1708382099209; Mon, 19 Feb 2024 14:34:59 -0800 (PST) Received: from freyr.lechnology.com (ip98-183-112-25.ok.ok.cox.net. [98.183.112.25]) by smtp.gmail.com with ESMTPSA id h25-20020a9d6419000000b006e45a5f0a70sm171776otl.49.2024.02.19.14.34.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Feb 2024 14:34:58 -0800 (PST) From: David Lechner To: Mark Brown Cc: David Lechner , Martin Sperl , David Jander , Jonathan Cameron , Michael Hennerich , =?utf-8?q?Nuno_S=C3=A1?= , Alain Volmat , Maxime Coquelin , Alexandre Torgue , linux-spi@vger.kernel.org, linux-kernel@vger.kernel.org, linux-stm32@st-md-mailman.stormreply.com, linux-arm-kernel@lists.infradead.org, linux-iio@vger.kernel.org, Julien Stephan Subject: [PATCH v2 3/5] spi: stm32: move splitting transfers to optimize_message Date: Mon, 19 Feb 2024 16:33:20 -0600 Message-ID: <20240219-mainline-spi-precook-message-v2-3-4a762c6701b9@baylibre.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240219-mainline-spi-precook-message-v2-0-4a762c6701b9@baylibre.com> References: <20240219-mainline-spi-precook-message-v2-0-4a762c6701b9@baylibre.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Mailer: b4 0.12.4 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791368548021489406 X-GMAIL-MSGID: 1791368548021489406 Since splitting transfers was moved to spi_optimize_message() in the core SPI code, we now need to use the optimize_message callback in the STM32 SPI driver to ensure that the operation is only performed once when spi_optimize_message() is used by peripheral drivers explicitly. Signed-off-by: David Lechner Reviewed-by: Jonathan Cameron --- v2 changes: none drivers/spi/spi-stm32.c | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/drivers/spi/spi-stm32.c b/drivers/spi/spi-stm32.c index c32e57bb38bd..e4e7ddb7524a 100644 --- a/drivers/spi/spi-stm32.c +++ b/drivers/spi/spi-stm32.c @@ -1118,6 +1118,21 @@ static irqreturn_t stm32h7_spi_irq_thread(int irq, void *dev_id) return IRQ_HANDLED; } +static int stm32_spi_optimize_message(struct spi_message *msg) +{ + struct spi_controller *ctrl = msg->spi->controller; + struct stm32_spi *spi = spi_controller_get_devdata(ctrl); + + /* On STM32H7, messages should not exceed a maximum size set + * later via the set_number_of_data function. In order to + * ensure that, split large messages into several messages + */ + if (spi->cfg->set_number_of_data) + return spi_split_transfers_maxwords(ctrl, msg, spi->t_size_max); + + return 0; +} + /** * stm32_spi_prepare_msg - set up the controller to transfer a single message * @ctrl: controller interface @@ -1163,18 +1178,6 @@ static int stm32_spi_prepare_msg(struct spi_controller *ctrl, !!(spi_dev->mode & SPI_LSB_FIRST), !!(spi_dev->mode & SPI_CS_HIGH)); - /* On STM32H7, messages should not exceed a maximum size setted - * afterward via the set_number_of_data function. In order to - * ensure that, split large messages into several messages - */ - if (spi->cfg->set_number_of_data) { - int ret; - - ret = spi_split_transfers_maxwords(ctrl, msg, spi->t_size_max); - if (ret) - return ret; - } - spin_lock_irqsave(&spi->lock, flags); /* CPOL, CPHA and LSB FIRST bits have common register */ @@ -2180,6 +2183,7 @@ static int stm32_spi_probe(struct platform_device *pdev) ctrl->max_speed_hz = spi->clk_rate / spi->cfg->baud_rate_div_min; ctrl->min_speed_hz = spi->clk_rate / spi->cfg->baud_rate_div_max; ctrl->use_gpio_descriptors = true; + ctrl->optimize_message = stm32_spi_optimize_message; ctrl->prepare_message = stm32_spi_prepare_msg; ctrl->transfer_one = stm32_spi_transfer_one; ctrl->unprepare_message = stm32_spi_unprepare_msg; From patchwork Mon Feb 19 22:33:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Lechner X-Patchwork-Id: 203301 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp74092dyc; Mon, 19 Feb 2024 14:38:04 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVM2bvin9YoOgoq92gofzzde21oM+d55TbHpTT4+OTvuUa+hfkPAE0cHZL17dqZshPme+j26kvVG6ohnebvZdc2j6ICuA== X-Google-Smtp-Source: AGHT+IEZ/MSd0NzKILt5R2ROPHkHXOapn8uEyC2fKbLaz36LJEPkHPfqo0ke7+zEccQ4+8Guog41 X-Received: by 2002:a05:6a00:8006:b0:6e4:61cf:337f with SMTP id eg6-20020a056a00800600b006e461cf337fmr5870147pfb.26.1708382284179; Mon, 19 Feb 2024 14:38:04 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708382284; cv=pass; d=google.com; s=arc-20160816; b=gvL8V/PeS+c+dlh4dg4RxKQ4P1SZiMazTWIN/jJptv3MtPekRBrtmoWBBidfNlFeWO jyfgorGm0lXDGnOiSWaIwd53WWF3x4pxkzsE7gniaJnHlZhAKjDEd5ZDYEsAoFAfEC18 X22uYZSTPsjdhxWRB3hu8YNlXGhG4H/oi04cm3JoqW9zs4x9IO7I+48KdkY09uofdYhE mUNuz/SegObmgS6drBouSOlCATItjt8b668JpVIdK2N/gp4z1lI/bu7HKAq8mNolHgWk ZIZXnpxexd+uDf2Hk8FpMSoXoHZhnFS53DUNUg/aWfYk1rW/AiF4qDU4D7ZTw/wzzgNE HETg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=Dm08qHNGM2q4SeLkv5aeyRrRYY/ISTOBJCodG60HdvI=; fh=+J5ur2UhhxyymSNePu7Zy3u2WcrF7lQluy5hVpxR4YI=; b=bmoOOlqu96BxRJznQiTNXN+A0Rk4yIeJVO+9C+SnQt5ugpx/c1ke3IC14hSlVBefMR 92n5UoNnlHMgAtObGDWxhf54igunUVHPtUbIpKm5lphR/AMDb90H2+YPtINaJNxS24Rt z05d6VtNAnSq4sCTU7IJ3ZYMRiQZ3at8YiqaBRTgWn0/dDiC4e/8MO3B7TiYTiFB+NhA lsBPUEoeofR5R/60od9Oo33ltB1dbFPv9f6Z32h59KWeGMob5OphJ2RGdWGSXSylLvm6 fC8C4hPgHOby4LzqJT8Zgo9oiP0BFDNNNqD/cpHcv5mRgAXLymKsMHqiV3Egzwh7iQA7 u65w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@baylibre-com.20230601.gappssmtp.com header.s=20230601 header.b=fz19xTEJ; arc=pass (i=1 spf=pass spfdomain=baylibre.com dkim=pass dkdomain=baylibre-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-72068-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-72068-ouuuleilei=gmail.com@vger.kernel.org" Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id jo30-20020a056a00909e00b006e10e1d298bsi5129078pfb.158.2024.02.19.14.38.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Feb 2024 14:38:04 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-72068-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@baylibre-com.20230601.gappssmtp.com header.s=20230601 header.b=fz19xTEJ; arc=pass (i=1 spf=pass spfdomain=baylibre.com dkim=pass dkdomain=baylibre-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-72068-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-72068-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id EA869B20BEA for ; Mon, 19 Feb 2024 22:36:22 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 58CFC58AAB; Mon, 19 Feb 2024 22:35:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="fz19xTEJ" Received: from mail-ot1-f48.google.com (mail-ot1-f48.google.com [209.85.210.48]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0F4EF56765 for ; Mon, 19 Feb 2024 22:35:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708382102; cv=none; b=gY6WJ1ow3bOENLvJ4YAMc9Wpb8EkusQzwYg5bnmKYc8rw2alDxctbbf7276X92FPolgageXpocn+uuapTxzOohlPZmusju0pupHdvX+G5kGserepYkzaoTUOT7vn5WheQIeQ0IZ0Xky3NNT/lrgGMTkvWsuZM2R3frvd1OJnmSI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708382102; c=relaxed/simple; bh=kNFrx8UJo78sQ8Q1iFWKP1SHsh62/7UZ71I45n2Sq1I=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=BtyHI+9jVngiSMP37nuo6BbBH+4LTJFIfTISdyRGGV5o3bqt+b2sZc1fcYfL1/M4M4v9AFhlgE/8MaTnU7ueaCgocsV3jSuJRxTPPdb4EPJHDl2Z1PSkxdhvOHaJm9zSaBq3JsFsEHT9hnV9m6sH5qE7ZqE1izwHSs1vDBNBqaI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=fz19xTEJ; arc=none smtp.client-ip=209.85.210.48 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Received: by mail-ot1-f48.google.com with SMTP id 46e09a7af769-6e432beab47so1755676a34.1 for ; Mon, 19 Feb 2024 14:35:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1708382100; x=1708986900; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Dm08qHNGM2q4SeLkv5aeyRrRYY/ISTOBJCodG60HdvI=; b=fz19xTEJjEPyxXMhuRnWPNX0N6BdtSeRoG3o7MjQb4S+cdF+bFyUdYzQw9pLbW1baW 9aXdAVzIBIGuzG65DplGDXIa+9b5GgXEhfR21qu9IFnn0ctGl1BkDniL5oz4RAyFqfqk HDOXjkOZ/OLoD3L4KbbhQRMb4PppAI0BfGtTcj9lcGlFjTPscD0yGZvaxLA5C60509f6 Bp4uVlvtjaa+TWTuur+2QjE/4OzcRaJLiYk3/QE7lQ5DnbKBjg+bnGG9xH1TDM7F+vmg wWW6GB5zwf7XbmkbZ5Q6x8p0Bzr8mLW2cSdgmIFErHWBZUUaxK+gyPpbeKgE6JCGbFkM O+0w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708382100; x=1708986900; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Dm08qHNGM2q4SeLkv5aeyRrRYY/ISTOBJCodG60HdvI=; b=p3AG2O3reVrj/jh31QBrH7OOlrjZf9XeBmMfJDdHDh93aum+8xQEX6qXyUpCsm6hpL 8PRsdCkQkygrnpsOdrEph8T4HFkai1Fpe1upkN6t8MKhihgUCufEsO1s6cWF0iWgdNEY 9Y+5kyGZb7cy7V1lb2jtoIgS/rXOHSjGMyQiEkGpkO0gewUI8pZ7J+6vgzKvm/cCzTDd p95Hnd/LshP2GLTWfvatSEWSfGvkPd5sty5jgVCQGkTbE2oZnuYA9xNE2Js/rfqJvu+w 4fIAUUR6SiGJ3zD/juMgkdY0CWZNwlowTM4UQVfxfFyzEJE8SE1cVcEgMaqA4QPJDacW GtFw== X-Forwarded-Encrypted: i=1; AJvYcCWrEn25zstQ6FkSyGKKxXWTndh6y4I4Lt91Oz3+vVQNy081J3k2e+U/zGcwWhXo00WthHIYqCqmEhD/Yk54aQ2PoKX+LWAXey8YwpwU X-Gm-Message-State: AOJu0YzUpHElcjetQaq1YIVztaeNrpnkFVzAxYERWOcn8FNRQZj4oXjj 7LICLIb06qBVy52aowmEv3ijf8hlhQb9iuADXJlVeKMVOxoJRA2YkVDCT7DtjGY= X-Received: by 2002:a9d:7a93:0:b0:6e2:ec3d:f141 with SMTP id l19-20020a9d7a93000000b006e2ec3df141mr13211177otn.16.1708382100186; Mon, 19 Feb 2024 14:35:00 -0800 (PST) Received: from freyr.lechnology.com (ip98-183-112-25.ok.ok.cox.net. [98.183.112.25]) by smtp.gmail.com with ESMTPSA id h25-20020a9d6419000000b006e45a5f0a70sm171776otl.49.2024.02.19.14.34.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Feb 2024 14:34:59 -0800 (PST) From: David Lechner To: Mark Brown Cc: David Lechner , Martin Sperl , David Jander , Jonathan Cameron , Michael Hennerich , =?utf-8?q?Nuno_S=C3=A1?= , Alain Volmat , Maxime Coquelin , Alexandre Torgue , linux-spi@vger.kernel.org, linux-kernel@vger.kernel.org, linux-stm32@st-md-mailman.stormreply.com, linux-arm-kernel@lists.infradead.org, linux-iio@vger.kernel.org, Julien Stephan Subject: [PATCH v2 4/5] spi: axi-spi-engine: move message compile to optimize_message Date: Mon, 19 Feb 2024 16:33:21 -0600 Message-ID: <20240219-mainline-spi-precook-message-v2-4-4a762c6701b9@baylibre.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240219-mainline-spi-precook-message-v2-0-4a762c6701b9@baylibre.com> References: <20240219-mainline-spi-precook-message-v2-0-4a762c6701b9@baylibre.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Mailer: b4 0.12.4 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791368661845266166 X-GMAIL-MSGID: 1791368661845266166 In the AXI SPI Engine driver, compiling the message is an expensive operation. Previously, it was done per message transfer in the prepare_message hook. This patch moves the message compile to the optimize_message hook so that it is only done once per message in cases where the peripheral driver calls spi_optimize_message(). This can be a significant performance improvement for some peripherals. For example, the ad7380 driver saw a 13% improvement in throughput when using the AXI SPI Engine driver with this patch. Since we now need two message states, one for the optimization stage that doesn't change for the lifetime of the message and one that is reset on each transfer for managing the current transfer state, the old msg->state is split into msg->opt_state and spi_engine->msg_state. The latter is included in the driver struct now since there is only one current message at a time that can ever use it and it is in a hot path so avoiding allocating a new one on each message transfer saves a few cpu cycles and lets us get rid of the prepare_message callback. Signed-off-by: David Lechner Reviewed-by: Nuno Sa Reviewed-by: Jonathan Cameron --- v2 changes: none drivers/spi/spi-axi-spi-engine.c | 40 +++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/drivers/spi/spi-axi-spi-engine.c b/drivers/spi/spi-axi-spi-engine.c index ca66d202f0e2..6177c1a8d56e 100644 --- a/drivers/spi/spi-axi-spi-engine.c +++ b/drivers/spi/spi-axi-spi-engine.c @@ -109,6 +109,7 @@ struct spi_engine { spinlock_t lock; void __iomem *base; + struct spi_engine_message_state msg_state; struct completion msg_complete; unsigned int int_enable; }; @@ -499,17 +500,11 @@ static irqreturn_t spi_engine_irq(int irq, void *devid) return IRQ_HANDLED; } -static int spi_engine_prepare_message(struct spi_controller *host, - struct spi_message *msg) +static int spi_engine_optimize_message(struct spi_message *msg) { struct spi_engine_program p_dry, *p; - struct spi_engine_message_state *st; size_t size; - st = kzalloc(sizeof(*st), GFP_KERNEL); - if (!st) - return -ENOMEM; - spi_engine_precompile_message(msg); p_dry.length = 0; @@ -517,31 +512,22 @@ static int spi_engine_prepare_message(struct spi_controller *host, size = sizeof(*p->instructions) * (p_dry.length + 1); p = kzalloc(sizeof(*p) + size, GFP_KERNEL); - if (!p) { - kfree(st); + if (!p) return -ENOMEM; - } spi_engine_compile_message(msg, false, p); spi_engine_program_add_cmd(p, false, SPI_ENGINE_CMD_SYNC( AXI_SPI_ENGINE_CUR_MSG_SYNC_ID)); - st->p = p; - st->cmd_buf = p->instructions; - st->cmd_length = p->length; - msg->state = st; + msg->opt_state = p; return 0; } -static int spi_engine_unprepare_message(struct spi_controller *host, - struct spi_message *msg) +static int spi_engine_unoptimize_message(struct spi_message *msg) { - struct spi_engine_message_state *st = msg->state; - - kfree(st->p); - kfree(st); + kfree(msg->opt_state); return 0; } @@ -550,10 +536,18 @@ static int spi_engine_transfer_one_message(struct spi_controller *host, struct spi_message *msg) { struct spi_engine *spi_engine = spi_controller_get_devdata(host); - struct spi_engine_message_state *st = msg->state; + struct spi_engine_message_state *st = &spi_engine->msg_state; + struct spi_engine_program *p = msg->opt_state; unsigned int int_enable = 0; unsigned long flags; + /* reinitialize message state for this transfer */ + memset(st, 0, sizeof(*st)); + st->p = p; + st->cmd_buf = p->instructions; + st->cmd_length = p->length; + msg->state = st; + reinit_completion(&spi_engine->msg_complete); spin_lock_irqsave(&spi_engine->lock, flags); @@ -658,8 +652,8 @@ static int spi_engine_probe(struct platform_device *pdev) host->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32); host->max_speed_hz = clk_get_rate(spi_engine->ref_clk) / 2; host->transfer_one_message = spi_engine_transfer_one_message; - host->prepare_message = spi_engine_prepare_message; - host->unprepare_message = spi_engine_unprepare_message; + host->optimize_message = spi_engine_optimize_message; + host->unoptimize_message = spi_engine_unoptimize_message; host->num_chipselect = 8; if (host->max_speed_hz == 0) From patchwork Mon Feb 19 22:33:22 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Lechner X-Patchwork-Id: 203298 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp73555dyc; Mon, 19 Feb 2024 14:36:27 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVZM1zmp/3GyKJa+4BDFI+RXF6yykDccwDfc9ROmHQZ2YTnzv5Ta9bQaHz6COj3da5QS34XL3b5tcBrpyANIFivCNR/Ag== X-Google-Smtp-Source: AGHT+IFmfTpbsgJgui4Z6b1ZhswKNQa9TWpt0Qgpypzr2i1d1zeu49JEFz/MuIJkf9yFTFOABccd X-Received: by 2002:a05:6871:5b1d:b0:21e:aca4:883 with SMTP id op29-20020a0568715b1d00b0021eaca40883mr7475512oac.15.1708382187611; Mon, 19 Feb 2024 14:36:27 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708382187; cv=pass; d=google.com; s=arc-20160816; b=IxtccEOxvZYBQhcOLT9sUxXcdMs89ylsTA9yzJXrT9wDSB8aQBazBR0ShfPBy9cJCD q7+4j3YFwVjRjcjX5o4mGEOAjNyvlBpgiIdXXJaAk4KR2X8+nu5fX9UpJin1j3b70SSM 5JjG0fKPrPebH3HZYIk/3hmta2S3sra8doIXAsSsB0bif9HEiy0AxvqgX8nTnDDXLylg HpLJNOEz0CVo4UgH3liu5D2zzZV7Dqx2SJVaNeMsTG5sHDF7mGUjFl7DcnHsfrkV58dC sWFMExEnHjL92XfS8LVdX/+WzK9hlV7CI+Nfp4lyaINp2Ah8k30jlhKgPXKtM66S8gl/ QJtA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=nguAE7f5YcWI/h5umYjj7eHdUyvOtkK6QQpK6mXXbx4=; fh=lpXf5WDUaM+taEW73K4OeYJaMH5rS9UfAC0RXnjf/mY=; b=M9jRTmPYM7kLB6jPbQxQ4zrzL5JEf8uCBcBHAHOnAI4VukBLVafXH2Px36PhhaDmUY fjSJ2ENmfUzlvs0BMRzq+E94YAnvEF07DPOAK3oadJeMKnDN+90C7lmwt1b3hK8Rl5fk Lu6ATlP/g4tIUg4X/RqFmpf8FkY6Q0A17kCU8XwUDGNxAJrAqqNrKVWN1j5Csjw7IeNm nbt2oW6Hv5vy4s7SkMfaDURPmUDQHYM9VvpzpeVZigeGPXiR9Q03nFtCxtn2O1EGDokz yBZPbNWdVnOdmbnpijyQjmTw1ApEH49d631EhjNMymTFeKWrMiEVwsGDWp2EO5Cf21dO W9Pw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@baylibre-com.20230601.gappssmtp.com header.s=20230601 header.b=Autx+eBN; arc=pass (i=1 spf=pass spfdomain=baylibre.com dkim=pass dkdomain=baylibre-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-72069-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-72069-ouuuleilei=gmail.com@vger.kernel.org" Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id dr15-20020a056a020fcf00b005dc8a58a865si5023456pgb.631.2024.02.19.14.36.27 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Feb 2024 14:36:27 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-72069-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=pass header.i=@baylibre-com.20230601.gappssmtp.com header.s=20230601 header.b=Autx+eBN; arc=pass (i=1 spf=pass spfdomain=baylibre.com dkim=pass dkdomain=baylibre-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-72069-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-72069-ouuuleilei=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 657142814E0 for ; Mon, 19 Feb 2024 22:36:27 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 01C2F55E75; Mon, 19 Feb 2024 22:35:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="Autx+eBN" Received: from mail-ot1-f44.google.com (mail-ot1-f44.google.com [209.85.210.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0061556B6D for ; Mon, 19 Feb 2024 22:35:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708382103; cv=none; b=QJg13ljBJyUKEB00J1no+6Fecm6L9KMRxbqgS3a1z3zotIYSdjp0IjPT2Rk37DDaW1G8TmUMkc7DkqxJsdPImI2PBVJaYpZE0aQMKyUOUgs1MKAHWSmetSChCnUC/yOQNiMppRfw1RZlg7rJNF2W6NijzJQj+pSXhEKAgFbwnCY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708382103; c=relaxed/simple; bh=QsqGSd0YrGNt4iO+QSuv/owuo4NT7KSX8p+xqtQcChY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=WBnnvmEbSrEJyRba0xi0eJpx2iB5SIEmVSFs6MSdKO5kFXGcsQCgwk8qKT66cb8B6fmS3cUsPgaXjHXXHBnz9lcHPwGfUOoEeuPM6pIqIBOKT2FMRSrDZ20P3+QPk/9tS8B5dPtErWQKxSvoYpuw55gZcLlIjZT+2H0kGsDUvK4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=Autx+eBN; arc=none smtp.client-ip=209.85.210.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Received: by mail-ot1-f44.google.com with SMTP id 46e09a7af769-6e2dfc98664so2665119a34.2 for ; Mon, 19 Feb 2024 14:35:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1708382101; x=1708986901; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=nguAE7f5YcWI/h5umYjj7eHdUyvOtkK6QQpK6mXXbx4=; b=Autx+eBNNCqYL35ZC0xL0rB31mo/p6dR/xOEzOklq4eWf1IMK5AUmGKdW+3Rdtwb28 nwoTuLyfFwdGa8MpIaPz0vXjZ8OfkYRTlxUAqKALXi+BTqQ3kBGZ4+eMwu2STVqEwiEd mEoiAQvbPLzOrDVtkAWW7QgArX44AkDUf1n9M1+QV63ZQ5PjqYMgZP+YIfYpMIWftduw XYlwSzfT0AbVnB9Y8LY7Ji3SME0nA8eGib5w4aI/R9mQ+lbLMj9W9jbPSnm9KUbHNw90 hgweH1prQEyn6/GeWokXEREudiWr5uZDA2xL41m+xSIfk5vZerGAgBY9zIXkrLOHqWeH iEtA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708382101; x=1708986901; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=nguAE7f5YcWI/h5umYjj7eHdUyvOtkK6QQpK6mXXbx4=; b=llhlMZ06ruvh3NoAmu9PCKZ+sGzOR1ZbyOqblW1rVnQie4Nsh3fKmUc5L03wT7d+l7 A1pi60V1hfZWRtVeHS3OTqyIse1jDLyNbcUtwQblSWMuFXaQpOard98X8OPSK349Thrw etDH8MfV//GvSOYDQB2WMfRZaMZY4vMiEX8CHpprv0MjjS3Uh1X7Hbbo67N8IcJhVM3D fSFR4qGYhFBJWWJfizvTRNntN9WAfqyG0tqyCZ/pNwjQaFwjvIcUnFNBQLxvZZqEnE8L hcRw2zPPEQNIGIkHi2pPsXm0Wj8kUz21WQWV/PT9fdPV1bcjQ9fHqoUiiMyGjhYbye0A SeBA== X-Forwarded-Encrypted: i=1; AJvYcCXH4att8qe0JQpVP8I42EWgBQ2UaPqe9MdXr8qEHLyrD8M68B9L250kfN31sokh7hwjssGubUED4BCNp6CuIK2Y0ONRoMdWFLunux3D X-Gm-Message-State: AOJu0YzcoWNmQE7WGGPURXyl7OOoYQR37xU+/zZ0OTNgcIGeVjW9blxq TsOzJqVM0vmzqbN8Xvm9G44e0kGZv50PGLWLx8bK3fTG7B8gg+yUr3A5l2WHd3w= X-Received: by 2002:a9d:7494:0:b0:6e2:e5a3:1732 with SMTP id t20-20020a9d7494000000b006e2e5a31732mr3221609otk.37.1708382101127; Mon, 19 Feb 2024 14:35:01 -0800 (PST) Received: from freyr.lechnology.com (ip98-183-112-25.ok.ok.cox.net. [98.183.112.25]) by smtp.gmail.com with ESMTPSA id h25-20020a9d6419000000b006e45a5f0a70sm171776otl.49.2024.02.19.14.35.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 19 Feb 2024 14:35:00 -0800 (PST) From: David Lechner To: Mark Brown Cc: David Lechner , Martin Sperl , David Jander , Jonathan Cameron , Michael Hennerich , =?utf-8?q?Nuno_S=C3=A1?= , Alain Volmat , Maxime Coquelin , Alexandre Torgue , linux-spi@vger.kernel.org, linux-kernel@vger.kernel.org, linux-stm32@st-md-mailman.stormreply.com, linux-arm-kernel@lists.infradead.org, linux-iio@vger.kernel.org, Julien Stephan Subject: [PATCH v2 5/5] iio: adc: ad7380: use spi_optimize_message() Date: Mon, 19 Feb 2024 16:33:22 -0600 Message-ID: <20240219-mainline-spi-precook-message-v2-5-4a762c6701b9@baylibre.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240219-mainline-spi-precook-message-v2-0-4a762c6701b9@baylibre.com> References: <20240219-mainline-spi-precook-message-v2-0-4a762c6701b9@baylibre.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Mailer: b4 0.12.4 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791368561187339872 X-GMAIL-MSGID: 1791368561187339872 This modifies the ad7380 ADC driver to use spi_optimize_message() to optimize the SPI message for the buffered read operation. Since buffered reads reuse the same SPI message for each read, this can improve performance by reducing the overhead of setting up some parts the SPI message in each spi_sync() call. Signed-off-by: David Lechner Reviewed-by: Nuno Sa --- v2 changes: - Removed dynamic allocation of spi xfer/msg - Moved spi message optimization to probe function - Dropped buffer pre/post callbacks drivers/iio/adc/ad7380.c | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/drivers/iio/adc/ad7380.c b/drivers/iio/adc/ad7380.c index abd746aef868..6b3fd20c8f1f 100644 --- a/drivers/iio/adc/ad7380.c +++ b/drivers/iio/adc/ad7380.c @@ -133,6 +133,9 @@ struct ad7380_state { struct spi_device *spi; struct regulator *vref; struct regmap *regmap; + /* xfer and msg for buffer reads */ + struct spi_transfer xfer; + struct spi_message msg; /* * DMA (thus cache coherency maintenance) requires the * transfer buffers to live in their own cache lines. @@ -236,14 +239,9 @@ static irqreturn_t ad7380_trigger_handler(int irq, void *p) struct iio_poll_func *pf = p; struct iio_dev *indio_dev = pf->indio_dev; struct ad7380_state *st = iio_priv(indio_dev); - struct spi_transfer xfer = { - .bits_per_word = st->chip_info->channels[0].scan_type.realbits, - .len = 4, - .rx_buf = st->scan_data.raw, - }; int ret; - ret = spi_sync_transfer(st->spi, &xfer, 1); + ret = spi_sync(st->spi, &st->msg); if (ret) goto out; @@ -335,6 +333,28 @@ static const struct iio_info ad7380_info = { .debugfs_reg_access = &ad7380_debugfs_reg_access, }; +static void ad7380_unoptimize_spi_msg(void *msg) +{ + spi_unoptimize_message(msg); +} + +static int devm_ad7380_setup_spi_msg(struct device *dev, struct ad7380_state *st) +{ + int ret; + + st->xfer.bits_per_word = st->chip_info->channels[0].scan_type.realbits; + st->xfer.len = 4; + st->xfer.rx_buf = st->scan_data.raw; + + spi_message_init_with_transfers(&st->msg, &st->xfer, 1); + + ret = spi_optimize_message(st->spi, &st->msg); + if (ret) + return dev_err_probe(dev, ret, "failed to optimize message\n"); + + return devm_add_action_or_reset(dev, ad7380_unoptimize_spi_msg, &st->msg); +} + static int ad7380_init(struct ad7380_state *st) { int ret; @@ -411,6 +431,10 @@ static int ad7380_probe(struct spi_device *spi) return dev_err_probe(&spi->dev, PTR_ERR(st->regmap), "failed to allocate register map\n"); + ret = devm_ad7380_setup_spi_msg(&spi->dev, st); + if (ret) + return ret; + indio_dev->channels = st->chip_info->channels; indio_dev->num_channels = st->chip_info->num_channels; indio_dev->name = st->chip_info->name;