From nobody Sun Sep 8 00:12:50 2024 Received: from mail-oa1-f41.google.com (mail-oa1-f41.google.com [209.85.160.41]) (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 B240D4F610 for ; Mon, 12 Feb 2024 23:27:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707780479; cv=none; b=cCJyfAlkiH5nn+5GIk6GG40KuqF+ocxXESfaDu8fXmasqcmO6abTx1bOBrr7yG+jOoQ1oZT8MGvQQsTRrTR/8diLZPCChZ9G4pX1wmKyU1UL37mHD7eNn+MThz8Yw4l4TWdLNqgoUhgDE8ZmrIuwR4KLfOS20FsLlOq6fzw/AlY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707780479; c=relaxed/simple; bh=okkDkG5qpHCKNPabVQCkxBBNkU9wU6t2QXMZXXs810o=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=VwsVFfaZNYBZx/XjZdshdyjBTHFkYEmf+a3LYFfT/mv1/LVXEGIpEEO/VAmP+GSqChZ9ynud3c33zxZdfQClDtWXMDEKCK+kx6rgUpttByLFhF7n4cI0fArP0CHSMGdGcmLzhQLvW8UZR3jXxWeenaU9n1rDWWIGTUUjE3QRNec= 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=E+JeLe/B; arc=none smtp.client-ip=209.85.160.41 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 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="E+JeLe/B" Received: by mail-oa1-f41.google.com with SMTP id 586e51a60fabf-218eea4f1a7so2158522fac.3 for ; Mon, 12 Feb 2024 15:27:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1707780475; x=1708385275; 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=y5nlvb7whNQtUeaLVoTHbqmwre6mZQsRGTnIbxan8M0=; b=E+JeLe/BFq89mnBMm/en0q1sT67EckzV3/VeyYrIgSgyrCk1QktfB/KHyDIiiPB9j1 zu7h6xuvgBSWEi/WkGzJgRLsgRywoSfyW6CzPOnX/FQ7xrZEEOmGsSLqdQkyaK0iDn2h /XxoWmt7izYbRp3Zp+UI9s6a0tayP1ZWosBeAGJGZko5iVWmtJkcv7+2hFqWWHh75Hyp t1z/Hd81USsLA00whRK/cOlRKxt5IaxfBm96YNbgsyYj1lgg2QvzXFGLr8RKlTgp8NZv nVtK0EcjHfpHrcHbk+coFk1NoCVzIP4VrEgmqjixuIsXT6W7CFF5RpW/nR3fw+VdKobY aBIw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707780475; x=1708385275; 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=y5nlvb7whNQtUeaLVoTHbqmwre6mZQsRGTnIbxan8M0=; b=JqncWgxx9euflOBNsTYxkD5hb2uVPxzfE3Cib395Jike4mHT3CorQ+ah5q429MZx7I Kv43dn4me1s2qFYz1V4aHzeX7z1w1Cv4IwaaDKpyUhmxQM/a7QMQfxQ4ul3Fh+yHLkTR LlsDydz/ufS4HuiCu8x9ciGKswmSGMkIZIJNXJEVYJIlO/ZmsuIqr2kSdUTg6lBw313G qlfbx6MrrEhp1KT5rRzMuzGaqU8o9KvH05f4DLhhlfWcDZg3UmsMmUAwyMQi9NuyGoc2 dCKWWIg4PVN9cLWT5+IzJua4L+MYRQOoQwxs3I4EyraJof1VgN8szHZ9qwU8U7QHkmUp gMgQ== X-Gm-Message-State: AOJu0YwaJEd5Mf6eqCDKEUyIuji35zpGWbwsoOaJM5g2RtI0At96jjDY tdNpbAKAgeva6ZZk+qSKMOiqdIpq1/SO5AIXqp3EqaE4aoA2N4ADdu71a4QmGticJk9ELfjitwH Y X-Google-Smtp-Source: AGHT+IGvxMcOWZ2Dv756kXDAUlvzBSoSeQYwyeWZHoJaRDJmyJNey3V4uJB8Ovv52qzdBiAy2PaaGQ== X-Received: by 2002:a05:6870:fb86:b0:21a:34e1:bfd3 with SMTP id kv6-20020a056870fb8600b0021a34e1bfd3mr7547301oab.25.1707780474781; Mon, 12 Feb 2024 15:27:54 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCVzhfZUuDWw4nNUdfIipHha2QvzEVJ76dXh2uwDGgQ+rFZOzT0PLSSkaWrB2H3bUw8a0n29HmiZAzDDSB0lhM/X5/S1OScQmG4U2gnkCboLBYHdwNUwIQTk9+8690fwmVyg1tAsEMX4cDQFTkDuOE7UNJLJdPZf779SrJwKwtsLLnq6HhsMwuV9HHlz81Re62ptX4dI5uPC1G0x3YZkgGPOU2DGQkeSEkjhN6fIR/a1GTScqced6k8YMphov6T9cfjsZPqf5L0ez1xk88FNiZbcC5jN+LKGW9lnk+8orNIDMCzU/tRBt4o5oVSLxngvZYiG0hlWfqbJfdUPau3lsnGT4GsZrDJVLsygXVR/Ogg8QyCzSHxg8BgxVuq/bpyQcp8ZpAW4iCvWiSwaw3e+1ILVtFO2TZN0tfNh75CSeNy/oZgnrvsKdlNzXmhFawU5t//LwrcguwbB13qIcxQMM3hLn1oRRdoicOlsuw1HCzyI6lksBbscW9Hnw0anWzfkZFG2ZOtAVCGCYwLMYIbjhvcAco81fmo= Received: from freyr.lechnology.com (ip98-183-112-25.ok.ok.cox.net. [98.183.112.25]) by smtp.gmail.com with ESMTPSA id pk17-20020a0568704c1100b0021a7a45e0b1sm330141oab.35.2024.02.12.15.27.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Feb 2024 15:27:54 -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 1/5] spi: add spi_optimize_message() APIs Date: Mon, 12 Feb 2024 17:26:41 -0600 Message-ID: <20240212-mainline-spi-precook-message-v1-1-a2373cd72d36@baylibre.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240212-mainline-spi-precook-message-v1-0-a2373cd72d36@baylibre.com> References: <20240212-mainline-spi-precook-message-v1-0-a2373cd72d36@baylibre.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" X-Mailer: b4 0.12.4 Content-Transfer-Encoding: quoted-printable 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-276188D2580= C@martin.sperl.org/ Signed-off-by: David Lechner --- drivers/spi/spi.c | 145 ++++++++++++++++++++++++++++++++++++++++++++= ++-- include/linux/spi/spi.h | 19 +++++++ 2 files changed, 160 insertions(+), 4 deletions(-) diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c index c2b10e2c75f0..5bac215d7009 100644 --- a/drivers/spi/spi.c +++ b/drivers/spi/spi.c @@ -2106,6 +2106,41 @@ struct spi_message *spi_get_next_queued_message(stru= ct spi_controller *ctlr) } EXPORT_SYMBOL_GPL(spi_get_next_queued_message); =20 +/** + * __spi_unoptimize_message - shared implementation of spi_unoptimize_mess= age() + * and spi_maybe_unoptimize_message() + * @msg: the message to unoptimize + * + * Periperhal drivers should use spi_unoptimize_message() and callers insi= de + * 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 optimiz= ed. + */ +static void __spi_unoptimize_message(struct spi_message *msg) +{ + struct spi_controller *ctlr =3D msg->spi->controller; + + if (ctlr->unoptimize_message) + ctlr->unoptimize_message(msg); + + msg->optimized =3D false; + msg->opt_state =3D NULL; +} + +/** + * spi_maybe_unoptimize_message - unoptimize msg not managed by a peripher= al + * @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_controll= er *ctlr) =20 mesg->prepared =3D false; =20 + 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,99 @@ static int __spi_validate(struct spi_device *spi, st= ruct spi_message *message) return 0; } =20 +/** + * __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 + * @pre_optimized: whether the message is considered pre-optimized or not + * + * Peripheral drivers will call spi_optimize_message() and the spi core wi= ll + * call spi_maybe_optimize_message() instead of calling this directly. + * + * It is not valid to call this on a message that has already been optimiz= ed. + * + * Return: zero on success, else a negative error code + */ +static int __spi_optimize_message(struct spi_device *spi, + struct spi_message *msg, + bool pre_optimized) +{ + struct spi_controller *ctlr =3D spi->controller; + int ret; + + ret =3D __spi_validate(spi, msg); + if (ret) + return ret; + + if (ctlr->optimize_message) { + ret =3D ctlr->optimize_message(msg); + if (ret) + return ret; + } + + msg->pre_optimized =3D pre_optimized; + msg->optimized =3D true; + + return 0; +} + +/** + * spi_maybe_optimize_message - optimize message if it isn't already pre-o= ptimized + * @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, false); +} + +/** + * spi_optimize_message - do any one-time validation and setup for a SPI m= essage + * @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 C= PU + * usage. + * + * Once a message has been optimized, it cannot be modified with the excep= tion + * of updating the contents of any xfer->tx_buf (the pointer can't be chan= ged, + * only the data in the memory it points to). + * + * Calls to this function must be balanced with calls to spi_unoptimize_me= ssage() + * 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) +{ + return __spi_optimize_message(spi, msg, true); +} +EXPORT_SYMBOL_GPL(spi_optimize_message); + +/** + * spi_unoptimize_message - releases any resources allocated by spi_optimi= ze_message() + * @msg: the message to unoptimize + * + * Calls to this function must be balanced with calls to spi_optimize_mess= age(). + * + * Context: can sleep + */ +void spi_unoptimize_message(struct spi_message *msg) +{ + __spi_unoptimize_message(msg); + msg->pre_optimized =3D false; +} +EXPORT_SYMBOL_GPL(spi_unoptimize_message); + static int __spi_async(struct spi_device *spi, struct spi_message *message) { struct spi_controller *ctlr =3D spi->controller; @@ -4258,8 +4388,8 @@ int spi_async(struct spi_device *spi, struct spi_mess= age *message) int ret; unsigned long flags; =20 - ret =3D __spi_validate(spi, message); - if (ret !=3D 0) + ret =3D spi_maybe_optimize_message(spi, message); + if (ret) return ret; =20 spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags); @@ -4271,6 +4401,8 @@ int spi_async(struct spi_device *spi, struct spi_mess= age *message) =20 spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags); =20 + spi_maybe_unoptimize_message(message); + return ret; } EXPORT_SYMBOL_GPL(spi_async); @@ -4331,10 +4463,15 @@ static int __spi_sync(struct spi_device *spi, struc= t spi_message *message) return -ESHUTDOWN; } =20 - status =3D __spi_validate(spi, message); - if (status !=3D 0) + status =3D spi_maybe_optimize_message(spi, message); + if (status) return status; =20 + /* + * NB: all return paths after this point must ensure that + * spi_finalize_current_message() is called to avoid leaking resources. + */ + SPI_STATISTICS_INCREMENT_FIELD(ctlr->pcpu_statistics, spi_sync); SPI_STATISTICS_INCREMENT_FIELD(spi->pcpu_statistics, spi_sync); =20 diff --git a/include/linux/spi/spi.h b/include/linux/spi/spi.h index 2b8e2746769a..f7a269f4956b 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(stru= ct 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; =20 + 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,7 @@ 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 + * @optimized: spi_optimize_message was called for the this message * @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 +1125,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 +1149,11 @@ struct spi_message { =20 unsigned is_dma_mapped:1; =20 + /* 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; =20 @@ -1172,6 +1183,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; =20 /* List of spi_res resources when the SPI message is processed */ struct list_head resources; @@ -1255,6 +1271,9 @@ static inline void spi_message_free(struct spi_messag= e *m) kfree(m); } =20 +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); --=20 2.43.0 From nobody Sun Sep 8 00:12:50 2024 Received: from mail-ot1-f41.google.com (mail-ot1-f41.google.com [209.85.210.41]) (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 AE4BF4F888 for ; Mon, 12 Feb 2024 23:27:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.41 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707780480; cv=none; b=lf4l4W1swE4nKdVFChWZiCf+mSnnAdEwjE1zqXxGX+7Cv6aBzhO9AQmteA26sqQlgOxW6bPiJtPkwZbwHHSRQQgtb9n0bduimncSMvP5kQRxHwlfBWrncO90lTD/s+nn/rXaTkK0B3rlsEuWsbpbBRFg2in9AJ8WDy+2NTfNt70= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707780480; c=relaxed/simple; bh=SOwuvsAaLR0dNkyt8XZ60TPrC9qAZLshwqZz0Hj2Yuc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=sLpbdaR6pTKqR3OVE/1AHeZ7YQwOV5SxjQsVuORhcTobRdR9/GC/yVARFHUjHEzKCrEghvfQBdWb67ksI4rbT3q0K0BfR4sjFqLTyK4zzOn1yYbFSuURsUnu7WlpCqr6fg94FvVmdrR7kcIYzS2nU5ZNjiltwr6af8TUvE8JqL4= 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=NQJR/Lu5; arc=none smtp.client-ip=209.85.210.41 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 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="NQJR/Lu5" Received: by mail-ot1-f41.google.com with SMTP id 46e09a7af769-6e2c45b81d8so1289683a34.0 for ; Mon, 12 Feb 2024 15:27:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1707780476; x=1708385276; 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=eLX74Fa9d90V9VkLwpCR7m/vmZr63zncboWNam9EPB0=; b=NQJR/Lu5t5bctPZ1Ck/9aqUV/gJHEuGv3KO/pmtJXImeWVYd5TATYphharej1Mhr3f fRfoD1OI/kIZcCNuFbtR6Hi3uGFJnrc9+lw/nA1ZOjc2gZDIeyHlXWpxb8IHwazFhLGz i6y80X8gz2m3SmrU79T6bAZqrxdGRrY1m7fhW9kejTh2xZegB3/kp3LoAQYlp9wJxNaQ NWwhh+6dnFnkJFvdNy2jcD5j1A11iIUW35IaRmbr4GBHTg+AyRs+jhVXzxdxikciWkq0 Dinwis8RnLgWbuQO8nUvoqkaHVUONcF89Mcqs/QarHRhpGnbG0nIPuYF8wdETXi1WqGZ /PSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707780476; x=1708385276; 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=eLX74Fa9d90V9VkLwpCR7m/vmZr63zncboWNam9EPB0=; b=D0O7GV/2gVvD1hSeDBwchnBpjFIz1FO0XuSFCB3sZbmMrvMYLRhmBmOBymxpvxvhA0 kupHmvMX4JM6PKlqI1bYGRKn89/jZA9rVv3yNiW78oRQJ5Qg8K24r/qYab85sC3Dtu+b 1tTTfBQ8OwqF5pG6OqAGp/JkOKn9cXEOkAn6eqHQu4RtvGtAtFGIz5avwCxuIBIynL5F HESiD5f02w1//LmpIMoaFREM8jSJv2EXuGwzjHP669UsX5Jh4NHy4/TM/WqFPz4/pnI7 57Lla0hZGbES35b12H8vNb0niYAFnVa33hJXLFyzXRckN9HlXhysDN55r9JAZpZ4vTA+ Ot3Q== X-Gm-Message-State: AOJu0Yy1+gwyJEQAvZXTpl6XBnB6r1i57DlWHfXudYjKH9cWRo4OzTW4 bqcjSAEfJrR5EJLbnNFegISi9h+SbB0ppOtznxFcaLnqxOdENfVvGMjLB5zKeuQ= X-Google-Smtp-Source: AGHT+IENcG0sbm+FxGSeKQ32xuud3iecYncceHk8taz1sm4Yg2pNVJ8oAzK4Wiliu88pFCNfDotmDQ== X-Received: by 2002:a05:6808:4396:b0:3c0:370c:69ea with SMTP id dz22-20020a056808439600b003c0370c69eamr307850oib.21.1707780475807; Mon, 12 Feb 2024 15:27:55 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCWi+s6AcBtZ0faJKmtDS0VKFmvby7nAC38w75GOULRX0y9EwEx7+1J4H9ouW6lZvgCfSXJzdPrc7yhgO3q+HjZI/gPHESEeDPCRuQ/REyCrDkMkJv6LRwKg8Uo1Z/VP7UB+maV2po+cRkt1zHUDomud4hHTEjSVKo1VkbhcHLieCmVmF2Kqam/A9G7dQDDYuHW3eZu0brg+uTKK5AOlNYmeZqIkCCWu/Ru3v84yl6XJsbYnZIzbE/3z1ezjezL0f3AVzKT0610T2R7bCmORiIn8vdONSIM4qGQPMyXP2+xGeKXDtJmU3qIhddqUByKFCCmhPITsVmEHZTV0ZSu8DVluKLVxz1yE5CfaNKZvbiXWUs23Kk2xX6o8l7x0L0Qd833OSHTRbjkeyEpqTy4qxYTtzlOeYpDvj2A4mdKBAdNI1R54Zt5oK87Z4zOTALUFtihDq2C6VC2RX68xgia+zTecW9/zY1uVG4ALoFCXhgqXknkJMDkVxR86w/jjgiv2Vy35XT8Hk8V4JXrFtvGU82GTiScVHa4= Received: from freyr.lechnology.com (ip98-183-112-25.ok.ok.cox.net. [98.183.112.25]) by smtp.gmail.com with ESMTPSA id pk17-20020a0568704c1100b0021a7a45e0b1sm330141oab.35.2024.02.12.15.27.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Feb 2024 15:27:55 -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 2/5] spi: move splitting transfers to spi_optimize_message() Date: Mon, 12 Feb 2024 17:26:42 -0600 Message-ID: <20240212-mainline-spi-precook-message-v1-2-a2373cd72d36@baylibre.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240212-mainline-spi-precook-message-v1-0-a2373cd72d36@baylibre.com> References: <20240212-mainline-spi-precook-message-v1-0-a2373cd72d36@baylibre.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" X-Mailer: b4 0.12.4 Content-Transfer-Encoding: quoted-printable 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. Signed-off-by: David Lechner Reviewed-by: Jonathan Cameron --- 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 5bac215d7009..8a21fa5bd4b9 100644 --- a/drivers/spi/spi.c +++ b/drivers/spi/spi.c @@ -1747,38 +1747,6 @@ static int __spi_pump_transfer_message(struct spi_co= ntroller *ctlr, =20 trace_spi_message_start(msg); =20 - /* - * 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 =3D spi_split_transfers_maxwords(ctlr, msg, 1); - if (ret) { - msg->status =3D 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 =3D 1; - } - } else { - ret =3D spi_split_transfers_maxsize(ctlr, msg, - spi_max_transfer_size(msg->spi)); - if (ret) { - msg->status =3D ret; - spi_finalize_current_message(ctlr); - return ret; - } - } - if (ctlr->prepare_message) { ret =3D ctlr->prepare_message(ctlr, msg); if (ret) { @@ -2124,6 +2092,8 @@ static void __spi_unoptimize_message(struct spi_messa= ge *msg) if (ctlr->unoptimize_message) ctlr->unoptimize_message(msg); =20 + spi_res_release(ctlr, msg); + msg->optimized =3D false; msg->opt_state =3D NULL; } @@ -2169,15 +2139,6 @@ void spi_finalize_current_message(struct spi_control= ler *ctlr) =20 spi_unmap_msg(ctlr, mesg); =20 - /* - * 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 =3D ctlr->unprepare_message(ctlr, mesg); if (ret) { @@ -3819,6 +3780,10 @@ static int __spi_split_transfer_maxsize(struct spi_c= ontroller *ctlr, * @msg: the @spi_message to transform * @maxsize: the maximum when to apply this * + * This function allocates resources that are automatically freed during t= he + * 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 t= he + * 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, st= ruct spi_message *message) return 0; } =20 +/** + * 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 =3D 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 =3D 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 =3D 1; + } + } else { + ret =3D 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() @@ -4256,10 +4276,16 @@ static int __spi_optimize_message(struct spi_device= *spi, if (ret) return ret; =20 + ret =3D spi_split_transfers(msg); + if (ret) + return ret; + if (ctlr->optimize_message) { ret =3D ctlr->optimize_message(msg); - if (ret) + if (ret) { + spi_res_release(ctlr, msg); return ret; + } } =20 msg->pre_optimized =3D pre_optimized; --=20 2.43.0 From nobody Sun Sep 8 00:12:50 2024 Received: from mail-oa1-f43.google.com (mail-oa1-f43.google.com [209.85.160.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 AE9204F895 for ; Mon, 12 Feb 2024 23:27:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707780479; cv=none; b=gQ5H46E+MT4S1Sl+r3C7+vbsPcUBIjE/E1y2u8kPklTH0qSwz+icQlJPmIes6pJ/mTmINuq2cySnPFbMVj+OfFhb0R97InKfCOO2EGruJ8dOwzzLBv8g+oXwgoajgQLPTaEqWnD1vTKWCPJNdld3Ug1E2d8SN+IcHt37TrjTdJ4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707780479; c=relaxed/simple; bh=gJ2uzCt6zEhXarHM9Z30NiaY+auYzoaf143QGmaQzOQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=NjcVR0udCSX3l3AFvfQJHhMlYwA/yh2dgoocf+/pwIWyajCgNvA3DZBSrWlhNtwTulzhWhT6XgEjfjLv06QhvVlsZK880Ax56Ge4eYcy3KPdMycZPYZNIKYI25hnwHRx76xD3kjjJaQ99+3pvFY4+tpWYiz2JE8BPV/BeD5+trE= 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=n2weUlzU; arc=none smtp.client-ip=209.85.160.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 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="n2weUlzU" Received: by mail-oa1-f43.google.com with SMTP id 586e51a60fabf-21432e87455so2433983fac.1 for ; Mon, 12 Feb 2024 15:27:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1707780477; x=1708385277; 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=Wgg1LuqsL+txmWYsX1bhtUmhJO0NDo1N/7wvtQZvjZE=; b=n2weUlzUesDczyqsWvOYbEhW44P0aDqv2zPmd+NrhziMBHOa7tEioDjvcmAXTrMefH WyxWAnUXkkRnJLA21/rDjN1KewOy5vuT335ZVi0dTZ1qnq5HhplLTWdPK3QBOphJrSVb nyWkLI9zifSilye7OOM1XMl99hWe+q0csxTqczgSzLXV7OB4L/lkC3RLJ3fNk9WGwQzu wMtLyRdpjE7hBiUZCz1gYkyQoo6C/jwONPyMPvuchkfrDazV5yRhbZsGcgiUdRzWcv8n MPHLDkSQKVgeLTws0yIkRso5xitohWtG68UmSO6PSr7KCYzKWvSXFXJvTA7RdPdSul6/ n4Bw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707780477; x=1708385277; 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=Wgg1LuqsL+txmWYsX1bhtUmhJO0NDo1N/7wvtQZvjZE=; b=skrU6RNZf0ld763f37cHHOgMj+hA7rzCjmEitEvgO9EGbsw2Nla0twC7zABwLKvTbv mrAlfvh7fVuGm43Z9L2vnZSgYzxmDZ9bC3190Hxavv4YO6H7Bcno38egr3NDvxXMQvf/ LkLJkDLCOkhrY2qCU9krZmu2YZKncPgKRPPeFcHbuAYyTt4TS+9IvE9EN5XalpULrdic BNfbed3etNGFcE8VyFoq+NXkFsOR2d+3VLNW/Pm7FEdawOQXB9V/lHdrxsMzJJX4fdHL Wvgz71tccVa/DCcXkjxptsnrsT2qV6IZOtS2XumWvwgIPSjULMyL6Zj1VjZdA7RNi1uw aIow== X-Gm-Message-State: AOJu0YwOnHmSBKvlzCELg/LFCQ0fS4BAJZtZfonOGYojUGgpoBF/jrmV E4BkD9CAAAO6Aku7AWXKHCLDC14yNeDJff6LiKglfPgfeQV0b6G+5CcEmmDI+ws= X-Google-Smtp-Source: AGHT+IELT5R44AE8dFtwqd5bl+J3kkdcmv8gPvy1/OPh7cRterQinbELctzOchigcD1RHaK44PYXvQ== X-Received: by 2002:a05:6871:20a:b0:219:77b5:efea with SMTP id t10-20020a056871020a00b0021977b5efeamr9731725oad.52.1707780476826; Mon, 12 Feb 2024 15:27:56 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCVC+/BMjkbKS/YbJkbW+yDdREhJQUURkROBdWkmvdgRrc4cmkOXx308t4GOolDYtSDjDM4HHnBMbUlxYSyyBsZcnfq1pLOJPF1oWxWk+zFOQY+ub9Z3iflpRl3q/QflYQh9UbrZtPKEPnnZMGKjsTCpXvqIH2flK1o/rPQYVBd3YfK7H7H8RABG1U8jTCfjhFL7DiRSQZNeelRZRZmhWBzsFEUhtOu1564EhwvNzr/gDVT5spJByD6wHVyvHex1sasvrnGLyi2jxwKPJgBwlUK2wXNw5vuat3K3S9Nk2vra0WgOf+JOXZndnhfTDdJgm51LYnCIPmwumgUzRD0jAlbH9vEkRHDdfw5S+8hrGkbFiIWX/SBpaUaaBJiL8cZ54jW8JcRGdKHmm5dQd76JHjX78uoX8NMgKNMpcTxmzHSk2zqGRh8/Xqeaa+fcMSfACkuZ9BVZjuGOUo0ZyHGGGAqhme0nqmaSTsP34XSiGFNloQm86nymrhYFEvhsUyYbchCU8/NnRS6yWt7hJiblyWzf44YQ/3Y= Received: from freyr.lechnology.com (ip98-183-112-25.ok.ok.cox.net. [98.183.112.25]) by smtp.gmail.com with ESMTPSA id pk17-20020a0568704c1100b0021a7a45e0b1sm330141oab.35.2024.02.12.15.27.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Feb 2024 15:27: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 3/5] spi: stm32: move splitting transfers to optimize_message Date: Mon, 12 Feb 2024 17:26:43 -0600 Message-ID: <20240212-mainline-spi-precook-message-v1-3-a2373cd72d36@baylibre.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240212-mainline-spi-precook-message-v1-0-a2373cd72d36@baylibre.com> References: <20240212-mainline-spi-precook-message-v1-0-a2373cd72d36@baylibre.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" X-Mailer: b4 0.12.4 Content-Transfer-Encoding: quoted-printable 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 --- 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, v= oid *dev_id) return IRQ_HANDLED; } =20 +static int stm32_spi_optimize_message(struct spi_message *msg) +{ + struct spi_controller *ctrl =3D msg->spi->controller; + struct stm32_spi *spi =3D 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 mess= age * @ctrl: controller interface @@ -1163,18 +1178,6 @@ static int stm32_spi_prepare_msg(struct spi_controll= er *ctrl, !!(spi_dev->mode & SPI_LSB_FIRST), !!(spi_dev->mode & SPI_CS_HIGH)); =20 - /* 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 =3D spi_split_transfers_maxwords(ctrl, msg, spi->t_size_max); - if (ret) - return ret; - } - spin_lock_irqsave(&spi->lock, flags); =20 /* CPOL, CPHA and LSB FIRST bits have common register */ @@ -2180,6 +2183,7 @@ static int stm32_spi_probe(struct platform_device *pd= ev) ctrl->max_speed_hz =3D spi->clk_rate / spi->cfg->baud_rate_div_min; ctrl->min_speed_hz =3D spi->clk_rate / spi->cfg->baud_rate_div_max; ctrl->use_gpio_descriptors =3D true; + ctrl->optimize_message =3D stm32_spi_optimize_message; ctrl->prepare_message =3D stm32_spi_prepare_msg; ctrl->transfer_one =3D stm32_spi_transfer_one; ctrl->unprepare_message =3D stm32_spi_unprepare_msg; --=20 2.43.0 From nobody Sun Sep 8 00:12:50 2024 Received: from mail-oa1-f43.google.com (mail-oa1-f43.google.com [209.85.160.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 99BA14F618 for ; Mon, 12 Feb 2024 23:27:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707780480; cv=none; b=u7ed47bABOUfJTupOsgmngXySBS0Ni5mrIN/5qpTTm4zY4+Ci4NcI63/KjrhbFZ/eW3ZGoI9xL1o1UOCQVKc46+acnzIES26ln6jNmfNR0DUMBaiiNXEkjTmPw1g/fdcJQhcBXBDQapwyuieUQ+1Hn5vA6z6/SaKdrqIs42/pvg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707780480; c=relaxed/simple; bh=pBodro6qaNPwfsk7dgSwBHvtNf3EfyqOH3QDD8d7KSA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=RKrI1AkDZ6AFtumRCwr10VdR9E/6gg/LvpZAA35f5HOBARSPY8vJfQvDfA/2km9V/25ZV5u1zLK4z3e6zgWP5YagkZVDNBXCpG8wHKiFakCugPpPr3hoiJqNrH10jQWtQ/r72oHvx54WPTVW0o+BA8xZrogL/r0P0YUAdVkL07U= 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=zHC3UF1u; arc=none smtp.client-ip=209.85.160.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 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="zHC3UF1u" Received: by mail-oa1-f43.google.com with SMTP id 586e51a60fabf-2046b2cd2d3so3049208fac.0 for ; Mon, 12 Feb 2024 15:27:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1707780477; x=1708385277; 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=/CDnTfe6nvQ55r7IK52xXS4oQavI5ZjAWN4vgbC1X28=; b=zHC3UF1utFRvV2eeuI5NDvbRyHfqNRzSFM2J5NeqV1z2TtEnv3xSfnoC7fAHWNmgTk j3viZjPMUYq85mIRDvQtSTXgbP7ov0P8BnO8rkJ6fZpAjbEq4a1jyCxrsXdwcRbwR9Pr 4D942dCs3wiTR5eqZCRqaftgXkZPylUYG2qqjOSD3cdmosQZoOPuO1eg5UX03pIqCce4 lD51Cq5aPHOk2efC+1DjBk8qwpug2P6x+uWw3tw9ZzH3ThhGwEmqlxaOD+5zMrKLzfQj Cv4UUXxv5UaKWycyDls4HheEHGOS9+92o9Ssd+ZybX0sNkMxurOUoUPRMPqVZHk7e0Uo Q08A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707780477; x=1708385277; 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=/CDnTfe6nvQ55r7IK52xXS4oQavI5ZjAWN4vgbC1X28=; b=Vbuw9SVPey6ktjJDXIx1rrb901wUgp1aTeIlg4J4acxOM7yHLIg0D7wBz5ydhzwsw1 1Ankn2hHFKxAQmP6Rs1v0ZmHcfVaHgNdbWLispG0VDA+rk/dRjzudeS2dGDoXbPm0hmv b3Kwm4Pimofu//STY8b3E5tQvZuGDxg+aPr2eH70HwhJiwVcjh/psa2Y/fBfiX1T9H83 T5iz6pqPBi7Us4c908b1HplIigUKwwtTf34MpR00zpj6VDz1uLea/1BtnKdM5HHQELAq emjRUCFaTexlVWP6ijt2R8bHGJi+eOh/9wrhYCd1q7qW6VYqcWOJKBPaYrxiUnEqyFy+ G0jQ== X-Forwarded-Encrypted: i=1; AJvYcCXVA52r0qO6jgBmH4xzbIw7mWxg48aHSnXD+hpCc4gm6SdspHasGdszUalbO+vhzNWela9knuGEkkOl59/lZ9qjQCm/iSZnBKOzH5Fk X-Gm-Message-State: AOJu0YyxyXuywjZjJqRRk7Yc4ExW5ppPbgDz+gZRQwxb35ioVJs8TYid yDyDuE7H3WzlNI1pwZ3euDu4uM63iYL/dxJPbTZSeI8SvwZbA1ImvYptTQ0qjf0= X-Google-Smtp-Source: AGHT+IFB5BMY94ESp7KJjpv5JdpAU/tpvbuYuXHw2+mNPbkI1Ukm6LHnu4zxTlzwR9ndeIJuVaW/DQ== X-Received: by 2002:a05:6870:c98e:b0:21a:47fc:4bcf with SMTP id hi14-20020a056870c98e00b0021a47fc4bcfmr8244258oab.29.1707780477681; Mon, 12 Feb 2024 15:27:57 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCXmAThv/8juXzf9mfLJyIKEgmDljh/Todw+KLIUx9gxl6haVS4PiqGhAXBBBtWjETVMr9PyQP32dLRyjAN/ELx2NsQX+J2CVBwF7rElKMmqRAGrSWTy0bpITdFHUKav9sX2g1m8B0PokQONqbek1aATCqIPG/6OFwYqjxh54DYpJRslItxMr0xXT4k+OBtPQbk2PgR2SVYRDiI51YKp6Jpnm5q2He9K2KJq+ige12IGcyYJeq+AwC2gZ+pPlZijbO9dCkgHWU2QaDWASE5mDCJgc7/+NAkeTE2Sg+nxaXR8yMtF3KBGjGwDYOPJNPzy3s6rFm79s5zxIUH/Hv/qqxwC4pPjC8ta645ynQAqa3XhikPFza9zF6ALx05/XuhBaHG34Nwq3bz2U88bl5uAqU5N00yJRy4KNq3i07oDEtSGUHhtHsSZngncZ99/PaGJxD/5JnvbkC19EZftclOQobnbO8MfFWmK1ghtKjQ7vnSJQ3jQvFbx/KMC+PNXlrmxOucutnfCsfbOmFY7bhwXgoZckU2oVao= Received: from freyr.lechnology.com (ip98-183-112-25.ok.ok.cox.net. [98.183.112.25]) by smtp.gmail.com with ESMTPSA id pk17-20020a0568704c1100b0021a7a45e0b1sm330141oab.35.2024.02.12.15.27.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Feb 2024 15:27: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 Subject: [PATCH 4/5] spi: axi-spi-engine: move message compile to optimize_message Date: Mon, 12 Feb 2024 17:26:44 -0600 Message-ID: <20240212-mainline-spi-precook-message-v1-4-a2373cd72d36@baylibre.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240212-mainline-spi-precook-message-v1-0-a2373cd72d36@baylibre.com> References: <20240212-mainline-spi-precook-message-v1-0-a2373cd72d36@baylibre.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" X-Mailer: b4 0.12.4 Content-Transfer-Encoding: quoted-printable 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 --- 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-eng= ine.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; =20 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 *devi= d) return IRQ_HANDLED; } =20 -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; =20 - st =3D kzalloc(sizeof(*st), GFP_KERNEL); - if (!st) - return -ENOMEM; - spi_engine_precompile_message(msg); =20 p_dry.length =3D 0; @@ -517,31 +512,22 @@ static int spi_engine_prepare_message(struct spi_cont= roller *host, =20 size =3D sizeof(*p->instructions) * (p_dry.length + 1); p =3D kzalloc(sizeof(*p) + size, GFP_KERNEL); - if (!p) { - kfree(st); + if (!p) return -ENOMEM; - } =20 spi_engine_compile_message(msg, false, p); =20 spi_engine_program_add_cmd(p, false, SPI_ENGINE_CMD_SYNC( AXI_SPI_ENGINE_CUR_MSG_SYNC_ID)); =20 - st->p =3D p; - st->cmd_buf =3D p->instructions; - st->cmd_length =3D p->length; - msg->state =3D st; + msg->opt_state =3D p; =20 return 0; } =20 -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 =3D msg->state; - - kfree(st->p); - kfree(st); + kfree(msg->opt_state); =20 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 =3D spi_controller_get_devdata(host); - struct spi_engine_message_state *st =3D msg->state; + struct spi_engine_message_state *st =3D &spi_engine->msg_state; + struct spi_engine_program *p =3D msg->opt_state; unsigned int int_enable =3D 0; unsigned long flags; =20 + /* reinitialize message state for this transfer */ + memset(st, 0, sizeof(*st)); + st->p =3D p; + st->cmd_buf =3D p->instructions; + st->cmd_length =3D p->length; + msg->state =3D st; + reinit_completion(&spi_engine->msg_complete); =20 spin_lock_irqsave(&spi_engine->lock, flags); @@ -658,8 +652,8 @@ static int spi_engine_probe(struct platform_device *pde= v) host->bits_per_word_mask =3D SPI_BPW_RANGE_MASK(1, 32); host->max_speed_hz =3D clk_get_rate(spi_engine->ref_clk) / 2; host->transfer_one_message =3D spi_engine_transfer_one_message; - host->prepare_message =3D spi_engine_prepare_message; - host->unprepare_message =3D spi_engine_unprepare_message; + host->optimize_message =3D spi_engine_optimize_message; + host->unoptimize_message =3D spi_engine_unoptimize_message; host->num_chipselect =3D 8; =20 if (host->max_speed_hz =3D=3D 0) --=20 2.43.0 From nobody Sun Sep 8 00:12:50 2024 Received: from mail-oa1-f48.google.com (mail-oa1-f48.google.com [209.85.160.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 6944750273 for ; Mon, 12 Feb 2024 23:27:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.48 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707780481; cv=none; b=RiIOyg0TI1dCGyAu6Yz96yMqGKOSCoMC6/j7mg1I5B6vBxFiAEDdHm2ypgO5FYoP+qCDJY1NNmtZrMpBuUoA0A3U/Jq+c2EF0JPPMz+JuPLJvQNNredHdMhj7QyM/5/Qgsn5egsfWcg1nSTkXQ5ZLPaU9cDjTZVk1+xP4wI8gCc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707780481; c=relaxed/simple; bh=TolsSfnf5ZNv9qUX//b4LDHC0qhXpGT9ROt+eluCm0Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=CPNA0wbi6Ib97GXEE4zrvvaZqNFkDZ3RsMtPtUWAwIm/yvZ9R+epr8DfmwHYldDEVnXjVWqDVMMvegS4MxcQpEnbHm7Rm+o3rZD/jr5axw2d+Hh8EtJaUWGdJG/Wkm8oM3sQcOLKXFuojoG13VwTPy6gt4FqX1Egn3OyS/E0t/Y= 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=a3/WL/Aq; arc=none smtp.client-ip=209.85.160.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 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="a3/WL/Aq" Received: by mail-oa1-f48.google.com with SMTP id 586e51a60fabf-21920f89441so2592889fac.2 for ; Mon, 12 Feb 2024 15:27:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1707780478; x=1708385278; 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=NPDb6hVtEhiljZHBpIuJ6/eQfkiQhhMGNrzlPsHzoEA=; b=a3/WL/AqH2ZWQLcLhsxTE0qiy6qY5KqxBIoUJg6PzfSP1C6ZGXcfOSIIoHH5kGnrb/ 4KR71zKRU3q3IkXaoZl7Q/iGoDbVGj1C5fE9paCxenVvRZ4Et0kjxCMyvpmy8YlUlp2M z+P5+1GzBEG/TV5oqSnkcL/HXoVTlvelmeitYxK/ZJfpN88bgbaSq2SJwyXkdJd/zvzw 8do58f56Y9dQto6zWftnPN0J7HRjrHlTOgggEcFISaAyckbV6ZK0xQHllRwS8Ww5v0hx KLFen3sgREBW1yeD069FfHIyekDaj+gnHnPwjQoVQN+DHzNzuTllfoe583/pTAwnZ99Z 3Qtw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707780478; x=1708385278; 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=NPDb6hVtEhiljZHBpIuJ6/eQfkiQhhMGNrzlPsHzoEA=; b=EBDO/JbSCA7tQMXa2+/VYzx3DyMI2E+q0+fz3i4ITRCqQz7+o59X638DkVrocnUqR4 4hPGKDEmlhjZOl+6JoTMQdoWcsT5OyX6uiKgJuqyANCJIzvTGWtwRcTayZ5CHpPL5mSx LeO+z/wW1IekZKtTOMmI1Zs6Gdz79wczUU+8Kio8F43j33Iko2EftAGniAg2x9mvzqsN RPuMIbRqfQKOx65zbyZ++K317R2QaJ0ewF9NT+hexFdjZE8Fr5MG+7Jaw2HNrQ9yi2Pa zFXVtvzaV6fpjjUO2Kcyq9GX0dZjO+JR/axudP1A+6NxUPi7zvp9syb2X+AX0P4HHXVd 0SEQ== X-Forwarded-Encrypted: i=1; AJvYcCXzzVrIKoJo2A2qCCIlN1BIuNN6xvPhU8P46f4p78kjzzW1i9zPL/4frXY5uYitgJVShOFjWfLnA2c8aJVvceI2uoUbLnItXSkgYguT X-Gm-Message-State: AOJu0YyJmp+HPITY4GWz5Tbhy7mmq3v+rgh9y/Ei55CMH/ygfI3fyN4i ZRtl0KZVA5rJb+H4ECTXWeMQyqeyG4bao7xtAMTwAvZc6pa8RZ3Eb0h9St/C6t0= X-Google-Smtp-Source: AGHT+IH9erOJCDaYbmQDWF2K81JN0GKgiq54irJeN5yLR/tg4FUJhNIMhrIkifdGOu4oAL5TJZcC0Q== X-Received: by 2002:a05:6871:5c48:b0:219:c8ea:ecfd with SMTP id os8-20020a0568715c4800b00219c8eaecfdmr10850888oac.15.1707780478627; Mon, 12 Feb 2024 15:27:58 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCU/sChhyo7/S+ePAw+fUTZa4Fa+c+iizdHVFXCth9k0UVOZs5C2WlgRtbyufn7EOEddQq5CkoNbtZtOlCdXpnzPXFGf760G/g1P4jT3D2BZM851R0PtrDuAkl06AR06eLUGEITPtV1UK0F65cIlm7ccxGgZLoQPiwMwsyuL5+SNLnL+D8pJj8ktyPxbGY2KEjPfVcWo3aru0369a6RMQJWdab/0ibpad8aRXTQjKE2CY2ZgZKHfkTlJl+JJ28mwL2pNejLLTSSa4B9k3zuc4nDciUgdcuwoMeHfxozDYXDiKfMYPfC3BEJVdK3sFLpvz5Z2Y6K087Zk6+hLDN3ceLH0IZuGlif8dpiVSs2aw4qqhk80aOpu703oSYX2fa2Zy04rdtzhykVvL9yVQLic9YrrrC+2SmVp0ygbRulhgWff+wuEJGaDvt46o+YtUGYq2F+yoofZRk2mrtpYLyyJ/fhAMTQctHXdcSryftOnYMqxIPwVdTitM4q3WhWgmLLWBe+3CCel/7DU5WydgyihQ23lXKvMWqg= Received: from freyr.lechnology.com (ip98-183-112-25.ok.ok.cox.net. [98.183.112.25]) by smtp.gmail.com with ESMTPSA id pk17-20020a0568704c1100b0021a7a45e0b1sm330141oab.35.2024.02.12.15.27.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Feb 2024 15:27: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 Subject: [PATCH 5/5] iio: adc: ad7380: use spi_optimize_message() Date: Mon, 12 Feb 2024 17:26:45 -0600 Message-ID: <20240212-mainline-spi-precook-message-v1-5-a2373cd72d36@baylibre.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240212-mainline-spi-precook-message-v1-0-a2373cd72d36@baylibre.com> References: <20240212-mainline-spi-precook-message-v1-0-a2373cd72d36@baylibre.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" X-Mailer: b4 0.12.4 Content-Transfer-Encoding: quoted-printable 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 --- drivers/iio/adc/ad7380.c | 52 +++++++++++++++++++++++++++++++++++++++++---= ---- 1 file changed, 45 insertions(+), 7 deletions(-) diff --git a/drivers/iio/adc/ad7380.c b/drivers/iio/adc/ad7380.c index abd746aef868..5c5d2642a474 100644 --- a/drivers/iio/adc/ad7380.c +++ b/drivers/iio/adc/ad7380.c @@ -133,6 +133,7 @@ struct ad7380_state { struct spi_device *spi; struct regulator *vref; struct regmap *regmap; + struct spi_message *msg; /* * DMA (thus cache coherency maintenance) requires the * transfer buffers to live in their own cache lines. @@ -231,19 +232,55 @@ static int ad7380_debugfs_reg_access(struct iio_dev *= indio_dev, u32 reg, return ret; } =20 +static int ad7380_buffer_preenable(struct iio_dev *indio_dev) +{ + struct ad7380_state *st =3D iio_priv(indio_dev); + struct spi_transfer *xfer; + int ret; + + st->msg =3D spi_message_alloc(1, GFP_KERNEL); + if (!st->msg) + return -ENOMEM; + + xfer =3D list_first_entry(&st->msg->transfers, struct spi_transfer, + transfer_list); + + xfer->bits_per_word =3D st->chip_info->channels[0].scan_type.realbits; + xfer->len =3D 4; + xfer->rx_buf =3D st->scan_data.raw; + + ret =3D spi_optimize_message(st->spi, st->msg); + if (ret) { + spi_message_free(st->msg); + return ret; + } + + return 0; +} + +static int ad7380_buffer_postdisable(struct iio_dev *indio_dev) +{ + struct ad7380_state *st =3D iio_priv(indio_dev); + + spi_unoptimize_message(st->msg); + spi_message_free(st->msg); + + return 0; +} + +static const struct iio_buffer_setup_ops ad7380_buffer_setup_ops =3D { + .preenable =3D ad7380_buffer_preenable, + .postdisable =3D ad7380_buffer_postdisable, +}; + static irqreturn_t ad7380_trigger_handler(int irq, void *p) { struct iio_poll_func *pf =3D p; struct iio_dev *indio_dev =3D pf->indio_dev; struct ad7380_state *st =3D iio_priv(indio_dev); - struct spi_transfer xfer =3D { - .bits_per_word =3D st->chip_info->channels[0].scan_type.realbits, - .len =3D 4, - .rx_buf =3D st->scan_data.raw, - }; int ret; =20 - ret =3D spi_sync_transfer(st->spi, &xfer, 1); + ret =3D spi_sync(st->spi, st->msg); if (ret) goto out; =20 @@ -420,7 +457,8 @@ static int ad7380_probe(struct spi_device *spi) =20 ret =3D devm_iio_triggered_buffer_setup(&spi->dev, indio_dev, iio_pollfunc_store_time, - ad7380_trigger_handler, NULL); + ad7380_trigger_handler, + &ad7380_buffer_setup_ops); if (ret) return ret; =20 --=20 2.43.0