From nobody Mon Feb 9 19:42:17 2026 Received: from mail-ot1-f50.google.com (mail-ot1-f50.google.com [209.85.210.50]) (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 8CB3A22912F for ; Wed, 11 Dec 2024 20:55:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733950506; cv=none; b=lE8Lau83V+w47tVhgX//99hltQrmFoQqMqCEN2kCWdVi6Pjq/2rwkDsain4G7/qHkGODJHALHXiWzKPp8p3iCX8NVEasYDvUvTI+3Lr0+9UVNCzRh8Djxxee79+jcf3767kNthoyfxEyBFU8UjZ2XlMeH1HZq0WM2eJbsr3CqaE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733950506; c=relaxed/simple; bh=zdncvfZ0Q50oYp1R7RHAvey85jbOGhAbt2b/jbzQSKQ=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=I+dkdLL/3pQprAHG2afz1bsP3PCJ///jjWqwg27EiCi6Ycyq/X2n8PgbSW28W+C3k811F26WGlvIfRiNBuzVoSoS6MPmgqvN4NcmZBR9kKjnKpzKaLXFO/3UuseDgf8W/y9/FpxTSDQm1V5xnsRW3IzVeffkhuzFDg/onuDGxAk= 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=csJI4HNP; arc=none smtp.client-ip=209.85.210.50 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="csJI4HNP" Received: by mail-ot1-f50.google.com with SMTP id 46e09a7af769-71e181fb288so807383a34.2 for ; Wed, 11 Dec 2024 12:55:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1733950504; x=1734555304; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=vouqSSWMwE0i1CMHfrBToPiWRjTEnBfzG9E3I/smeiU=; b=csJI4HNPhzHiuPZlBZaE7+ln7Ng0r5BDh7vNMonX4/vA3NaYgOW5qdh29vyhEkOsIB nS1O4pwa1MlmQMDjk8aRytFOWwYbmGZ3brRImuTgPUg8vSl4gpBB8LnYLsWdyBcQQydL D9nVysl5Lby8JreBZgN1FXu+VeS4gxsJG7MlVOFd/ZXXWB4SJWtajhQ08U7JpmgAYrMI PZ5sysElFndGZhPmps6q2xlLfyPdBmJr7E+nFR87jHkWGcxDo37rxY1PtLCC6S3Mva00 hAsxoof7X61tCJ2u8M08OVYqbQ1qxI7zxU0gRfhqvhrLFNKI6MWk/F05ymsmrQaIYIyR MS9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1733950504; x=1734555304; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=vouqSSWMwE0i1CMHfrBToPiWRjTEnBfzG9E3I/smeiU=; b=Q6Hd5aBTOPkzlKh5FbPmGbKUt38qvCgxS7t+oMsqO3Aor9gQIAq2L5CMgZ+w/rXxt4 clI/wHVnmjEUIVjiuX+6erZxYvt/wKWEf4PwXrGpwn6MLQdKJz4N4Dw4wD3Mm2aK+Rh6 aiOfF+DKjcdJeHsr4ZJ0JAVf1F9ezlt49/H2eLMqn2AAkmGlem6qnrKKZ/Eh7s7kNhv1 7B7kYYVdLsigYdRzLFqC40UU30pU2/Mvn/B/4zUxeYwPAbEuGjDbSLROxbjcubUzTIlq DFMvK7omWwxdECkfSqlZ89JqZ0TwCP+zP09c47D0e3oSF2NXb5H0vIS0Nzj0+RrZA60A nNew== X-Forwarded-Encrypted: i=1; AJvYcCWyCY3TuzTpxSRbHX4jGorOSTGECM5zk03dVNKw6lKjS3rkMLm/V+Oq3IW0+AdwpJavuuJdAgz58CZB1Xo=@vger.kernel.org X-Gm-Message-State: AOJu0YyDt+BgI8+lTkuon4QLHZ1H5bJl5A48dszBRg7U1UH6H+7OzXx4 WrDBD8kUWfdGnVTI3flztDMJuIfUoIWU4ZFiBeJ8I8H/1BBf9IE27j/CZnekbts= X-Gm-Gg: ASbGnctULdRjwqLIvYpm2Gq/zHC44peQPFgTcW5OdjDvC00906b/eJES96LtXPesmwJ T59l1kZ11IsH2eyZR5ltKcr1BVyhOnhK+FJZ5u7ARLtloNj81sxcByVp3GloZixam8U34M0cxuv b/qks6ltNyHYfFjfKbIkpbhKqeSAGwf3uHoRmD7SYS1XNe+Y8vSkxKg4FTObvFUKWsUCz4IUtdm bjHqALuZ8FY76t4GL62MVO0zyVTugk72rPINl4phoxI/yjt2Z5/LOt+2EWDtHRXK6ucChlxcfDj 3EtVsF6efw== X-Google-Smtp-Source: AGHT+IGjdtY9SJoSuhMzq6sD9FjC+U2bj5zrP1DIdEBADIpRixwTBYhxCGTjehUjJtVHBExWPH0lGQ== X-Received: by 2002:a05:6830:6d09:b0:717:f666:9559 with SMTP id 46e09a7af769-71e29c5a609mr595769a34.9.1733950503638; Wed, 11 Dec 2024 12:55:03 -0800 (PST) Received: from [127.0.1.1] (ip98-183-112-25.ok.ok.cox.net. [98.183.112.25]) by smtp.gmail.com with ESMTPSA id 46e09a7af769-71def651fb2sm1888288a34.27.2024.12.11.12.55.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Dec 2024 12:55:03 -0800 (PST) From: David Lechner Date: Wed, 11 Dec 2024 14:54:42 -0600 Subject: [PATCH v6 05/17] spi: add offload TX/RX streaming APIs 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" Content-Transfer-Encoding: quoted-printable Message-Id: <20241211-dlech-mainline-spi-engine-offload-2-v6-5-88ee574d5d03@baylibre.com> References: <20241211-dlech-mainline-spi-engine-offload-2-v6-0-88ee574d5d03@baylibre.com> In-Reply-To: <20241211-dlech-mainline-spi-engine-offload-2-v6-0-88ee574d5d03@baylibre.com> To: Mark Brown , Jonathan Cameron , Rob Herring , Krzysztof Kozlowski , Conor Dooley , =?utf-8?q?Nuno_S=C3=A1?= Cc: =?utf-8?q?Uwe_Kleine-K=C3=B6nig?= , Michael Hennerich , Lars-Peter Clausen , David Jander , Martin Sperl , linux-spi@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-iio@vger.kernel.org, linux-pwm@vger.kernel.org, David Lechner X-Mailer: b4 0.14.2 Most configuration of SPI offloads is handled opaquely using the offload pointer that is passed to the various offload functions. However, there are some offload features that need to be controlled on a per transfer basis. This patch adds a flag field to struct spi_transfer to allow specifying such features. The first feature to be added is the ability to stream data to/from a hardware sink/source rather than using a tx or rx buffer. Additional flags can be added in the future as needed. A flags field is also added to the offload struct for providers to indicate which flags are supported. This allows for generic checking of offload capabilities during __spi_validate() so that each offload provider doesn't have to implement their own validation. As a first users of this streaming capability, getter functions are added to get a DMA channel that is directly connected to the offload. Peripheral drivers will use this to get a DMA channel and configure it to suit their needs. Signed-off-by: David Lechner Reviewed-by: Jonathan Cameron Reviewed-by: Nuno Sa --- v6 changes: * Update for header file split. * Fix wrong kernel-doc comments. v5 change: * Remove incorrect comment about caller needing to release DMA channels. v4 changes: * DMA API's now automatically release DMA channels instead of leaving it up to the caller. v3 changes: * Added spi_offload_{tx,rx}_stream_get_dma_chan() functions. v2 changes: * This is also split out from "spi: add core support for controllers with offload capabilities". * In the previous version, we were using (void *)-1 as a sentinel value that could be assigned, e.g. to rx_buf. But this was naive since there is core code that would try to dereference this pointer. So instead, we've added a new flags field to the spi_transfer structure for this sort of thing. This also has the advantage of being able to be used in the future for other arbitrary features. --- drivers/spi/spi-offload.c | 70 ++++++++++++++++++++++++++++++++= ++++ drivers/spi/spi.c | 10 ++++++ include/linux/spi/offload/consumer.h | 5 +++ include/linux/spi/offload/types.h | 19 ++++++++++ include/linux/spi/spi.h | 3 ++ 5 files changed, 107 insertions(+) diff --git a/drivers/spi/spi-offload.c b/drivers/spi/spi-offload.c index 43582e50e279c4b1b958765fec556aaa91180e55..df5e963d5ee29d3783355959553= 6a460c530bc81 100644 --- a/drivers/spi/spi-offload.c +++ b/drivers/spi/spi-offload.c @@ -18,6 +18,7 @@ =20 #include #include +#include #include #include #include @@ -332,6 +333,75 @@ void spi_offload_trigger_disable(struct spi_offload *o= ffload, } EXPORT_SYMBOL_GPL(spi_offload_trigger_disable); =20 +static void spi_offload_release_dma_chan(void *chan) +{ + dma_release_channel(chan); +} + +/** + * devm_spi_offload_tx_stream_request_dma_chan - Get the DMA channel info = for the TX stream + * @dev: Device for devm purposes. + * @offload: Offload instance + * + * This is the DMA channel that will provide data to transfers that use the + * %SPI_OFFLOAD_XFER_TX_STREAM offload flag. + * + * Return: Pointer to DMA channel info, or negative error code + */ +struct dma_chan +*devm_spi_offload_tx_stream_request_dma_chan(struct device *dev, + struct spi_offload *offload) +{ + struct dma_chan *chan; + int ret; + + if (!offload->ops || !offload->ops->tx_stream_request_dma_chan) + return ERR_PTR(-EOPNOTSUPP); + + chan =3D offload->ops->tx_stream_request_dma_chan(offload); + if (IS_ERR(chan)) + return chan; + + ret =3D devm_add_action_or_reset(dev, spi_offload_release_dma_chan, chan); + if (ret) + return ERR_PTR(ret); + + return chan; +} +EXPORT_SYMBOL_GPL(devm_spi_offload_tx_stream_request_dma_chan); + +/** + * devm_spi_offload_rx_stream_request_dma_chan - Get the DMA channel info = for the RX stream + * @dev: Device for devm purposes. + * @offload: Offload instance + * + * This is the DMA channel that will receive data from transfers that use = the + * %SPI_OFFLOAD_XFER_RX_STREAM offload flag. + * + * Return: Pointer to DMA channel info, or negative error code + */ +struct dma_chan +*devm_spi_offload_rx_stream_request_dma_chan(struct device *dev, + struct spi_offload *offload) +{ + struct dma_chan *chan; + int ret; + + if (!offload->ops || !offload->ops->rx_stream_request_dma_chan) + return ERR_PTR(-EOPNOTSUPP); + + chan =3D offload->ops->rx_stream_request_dma_chan(offload); + if (IS_ERR(chan)) + return chan; + + ret =3D devm_add_action_or_reset(dev, spi_offload_release_dma_chan, chan); + if (ret) + return ERR_PTR(ret); + + return chan; +} +EXPORT_SYMBOL_GPL(devm_spi_offload_rx_stream_request_dma_chan); + /* Triggers providers */ =20 static void spi_offload_trigger_unregister(void *data) diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c index ff1add2ecb91f18cf82e6f1e9595584c11adf9d8..4a871db9ee636aba64c866ebdd8= bb1dbf82e0f42 100644 --- a/drivers/spi/spi.c +++ b/drivers/spi/spi.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -4163,6 +4164,15 @@ static int __spi_validate(struct spi_device *spi, st= ruct spi_message *message) =20 if (_spi_xfer_word_delay_update(xfer, spi)) return -EINVAL; + + /* make sure controller supports required offload features */ + if (xfer->offload_flags) { + if (!message->offload) + return -EINVAL; + + if (xfer->offload_flags & ~message->offload->xfer_flags) + return -EINVAL; + } } =20 message->status =3D -EINPROGRESS; diff --git a/include/linux/spi/offload/consumer.h b/include/linux/spi/offlo= ad/consumer.h index 5a0ec5303d600728959594bcdbd0cb2baeba7c77..cd7d5daa21e69b61c16eba6c10c= 855345a4f3297 100644 --- a/include/linux/spi/offload/consumer.h +++ b/include/linux/spi/offload/consumer.h @@ -31,4 +31,9 @@ int spi_offload_trigger_enable(struct spi_offload *offloa= d, void spi_offload_trigger_disable(struct spi_offload *offload, struct spi_offload_trigger *trigger); =20 +struct dma_chan *devm_spi_offload_tx_stream_request_dma_chan(struct device= *dev, + struct spi_offload *offload); +struct dma_chan *devm_spi_offload_rx_stream_request_dma_chan(struct device= *dev, + struct spi_offload *offload); + #endif /* __LINUX_SPI_OFFLOAD_CONSUMER_H */ diff --git a/include/linux/spi/offload/types.h b/include/linux/spi/offload/= types.h index 7476f2073b02ee0f9edd3ae75e587b075746fa92..86d0e8cb9495bb43e177378b204= 1067de8ea8786 100644 --- a/include/linux/spi/offload/types.h +++ b/include/linux/spi/offload/types.h @@ -11,6 +11,11 @@ =20 struct device; =20 +/* This is write xfer but TX uses external data stream rather than tx_buf.= */ +#define SPI_OFFLOAD_XFER_TX_STREAM BIT(0) +/* This is read xfer but RX uses external data stream rather than rx_buf. = */ +#define SPI_OFFLOAD_XFER_RX_STREAM BIT(1) + /* Offload can be triggered by external hardware event. */ #define SPI_OFFLOAD_CAP_TRIGGER BIT(0) /* Offload can record and then play back TX data when triggered. */ @@ -40,6 +45,8 @@ struct spi_offload { void *priv; /** @ops: callbacks for offload support */ const struct spi_offload_ops *ops; + /** @xfer_flags: %SPI_OFFLOAD_XFER_* flags supported by provider */ + u32 xfer_flags; }; =20 enum spi_offload_trigger_type { @@ -75,6 +82,18 @@ struct spi_offload_ops { * given offload instance. */ void (*trigger_disable)(struct spi_offload *offload); + /** + * @tx_stream_request_dma_chan: Optional callback for controllers that + * have an offload where the TX data stream is connected directly to a + * DMA channel. + */ + struct dma_chan *(*tx_stream_request_dma_chan)(struct spi_offload *offloa= d); + /** + * @rx_stream_request_dma_chan: Optional callback for controllers that + * have an offload where the RX data stream is connected directly to a + * DMA channel. + */ + struct dma_chan *(*rx_stream_request_dma_chan)(struct spi_offload *offloa= d); }; =20 #endif /* __LINUX_SPI_OFFLOAD_TYPES_H */ diff --git a/include/linux/spi/spi.h b/include/linux/spi/spi.h index 98bdc8c16c20521c0a94e5f72f5e71c4f6d7d11e..4c087009cf974595f23036b1b7a= 030a45913420c 100644 --- a/include/linux/spi/spi.h +++ b/include/linux/spi/spi.h @@ -1093,6 +1093,9 @@ struct spi_transfer { =20 u32 effective_speed_hz; =20 + /* Use %SPI_OFFLOAD_XFER_* from spi-offload.h */ + unsigned int offload_flags; + unsigned int ptp_sts_word_pre; unsigned int ptp_sts_word_post; =20 --=20 2.43.0