From patchwork Thu Feb 2 11:00:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xuan Zhuo X-Patchwork-Id: 13125756 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 89EADC636D6 for ; Thu, 2 Feb 2023 11:01:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232024AbjBBLBi (ORCPT ); Thu, 2 Feb 2023 06:01:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58502 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232435AbjBBLBW (ORCPT ); Thu, 2 Feb 2023 06:01:22 -0500 Received: from out30-112.freemail.mail.aliyun.com (out30-112.freemail.mail.aliyun.com [115.124.30.112]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F2A6E885CD; Thu, 2 Feb 2023 03:01:15 -0800 (PST) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R291e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018046056;MF=xuanzhuo@linux.alibaba.com;NM=1;PH=DS;RN=21;SR=0;TI=SMTPD_---0VaktKMa_1675335668; Received: from localhost(mailfrom:xuanzhuo@linux.alibaba.com fp:SMTPD_---0VaktKMa_1675335668) by smtp.aliyun-inc.com; Thu, 02 Feb 2023 19:01:09 +0800 From: Xuan Zhuo To: netdev@vger.kernel.org Cc: "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , "Michael S. Tsirkin" , Jason Wang , =?utf-8?b?QmrDtnJuIFTDtnBlbA==?= , Magnus Karlsson , Maciej Fijalkowski , Jonathan Lemon , Alexei Starovoitov , Daniel Borkmann , Jesper Dangaard Brouer , John Fastabend , Sebastian Andrzej Siewior , Menglong Dong , Kuniyuki Iwashima , Petr Machata , virtualization@lists.linux-foundation.org, bpf@vger.kernel.org Subject: [PATCH 08/33] virtio_ring: introduce dma sync api for virtio Date: Thu, 2 Feb 2023 19:00:33 +0800 Message-Id: <20230202110058.130695-9-xuanzhuo@linux.alibaba.com> X-Mailer: git-send-email 2.32.0.3.g01195cf9f In-Reply-To: <20230202110058.130695-1-xuanzhuo@linux.alibaba.com> References: <20230202110058.130695-1-xuanzhuo@linux.alibaba.com> MIME-Version: 1.0 X-Git-Hash: d7589ab6ea10 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org In the process of dma sync, we involved whether virtio uses dma api. On the other hand, it is also necessary to read vdev->dev.parent. So these API has been introduced. Signed-off-by: Xuan Zhuo --- drivers/virtio/virtio_ring.c | 61 ++++++++++++++++++++++++++++++++++++ include/linux/virtio.h | 8 +++++ 2 files changed, 69 insertions(+) diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c index 67eda7bc23ea..7b393133fd27 100644 --- a/drivers/virtio/virtio_ring.c +++ b/drivers/virtio/virtio_ring.c @@ -3102,4 +3102,65 @@ void virtio_dma_unmap(struct device *dev, dma_addr_t dma, unsigned int length, } EXPORT_SYMBOL_GPL(virtio_dma_unmap); +/** + * virtio_dma_need_sync - check dma address need sync + * @dev: virtio device + * @addr: DMA address + */ +bool virtio_dma_need_sync(struct device *dev, dma_addr_t addr) +{ + struct virtio_device *vdev = dev_to_virtio(dev); + + if (!vring_use_dma_api(vdev)) + return 0; + + return dma_need_sync(vdev->dev.parent, addr); +} +EXPORT_SYMBOL_GPL(virtio_dma_need_sync); + +/** + * virtio_dma_sync_signle_range_for_cpu - dma sync for cpu + * @dev: virtio device + * @addr: DMA address + * @offset: DMA address offset + * @size: mem size for sync + * @dir: DMA direction + * + * Before calling this function, use virtio_dma_need_sync() to confirm that the + * DMA address really needs to be synchronized + */ +void virtio_dma_sync_signle_range_for_cpu(struct device *dev, dma_addr_t addr, + unsigned long offset, size_t size, + enum dma_data_direction dir) +{ + struct virtio_device *vdev = dev_to_virtio(dev); + + dma_sync_single_range_for_cpu(vdev->dev.parent, addr, offset, + size, DMA_BIDIRECTIONAL); +} +EXPORT_SYMBOL_GPL(virtio_dma_sync_signle_range_for_cpu); + +/** + * virtio_dma_sync_signle_range_for_device - dma sync for device + * @dev: virtio device + * @addr: DMA address + * @offset: DMA address offset + * @size: mem size for sync + * @dir: DMA direction + * + * Before calling this function, use virtio_dma_need_sync() to confirm that the + * DMA address really needs to be synchronized + */ +void virtio_dma_sync_signle_range_for_device(struct device *dev, + dma_addr_t addr, + unsigned long offset, size_t size, + enum dma_data_direction dir) +{ + struct virtio_device *vdev = dev_to_virtio(dev); + + dma_sync_single_range_for_device(vdev->dev.parent, addr, offset, + size, DMA_BIDIRECTIONAL); +} +EXPORT_SYMBOL_GPL(virtio_dma_sync_signle_range_for_device); + MODULE_LICENSE("GPL"); diff --git a/include/linux/virtio.h b/include/linux/virtio.h index ce89126becc5..8c2fae318b0c 100644 --- a/include/linux/virtio.h +++ b/include/linux/virtio.h @@ -227,4 +227,12 @@ dma_addr_t virtio_dma_map(struct device *dev, void *addr, unsigned int length, int virtio_dma_mapping_error(struct device *dev, dma_addr_t addr); void virtio_dma_unmap(struct device *dev, dma_addr_t dma, unsigned int length, enum dma_data_direction dir); +bool virtio_dma_need_sync(struct device *dev, dma_addr_t addr); +void virtio_dma_sync_signle_range_for_cpu(struct device *dev, dma_addr_t addr, + unsigned long offset, size_t size, + enum dma_data_direction dir); +void virtio_dma_sync_signle_range_for_device(struct device *dev, + dma_addr_t addr, + unsigned long offset, size_t size, + enum dma_data_direction dir); #endif /* _LINUX_VIRTIO_H */