From: Christoph Hellwig <hch@lst.de>
To: Jianxiong Gao <jxgao@google.com>
Cc: axboe@fb.com, heikki.krogerus@linux.intel.com, sagi@grimberg.me,
saravanak@google.com, konrad.wilk@oracle.com, marcorr@google.com,
gregkh@linuxfoundation.org, rafael.j.wysocki@intel.com,
linux-kernel@vger.kernel.org, linux-nvme@lists.infradead.org,
kbusch@kernel.org, bgolaszewski@baylibre.com,
iommu@lists.linux-foundation.org, jroedel@suse.de,
dan.j.williams@intel.com, andriy.shevchenko@linux.intel.com,
robin.murphy@arm.com, hch@lst.de
Subject: Re: [PATCH 2/3] Add swiotlb offset preserving mapping when dma_dma_parameters->page_offset_mask is non zero.
Date: Thu, 28 Jan 2021 19:16:27 +0100 [thread overview]
Message-ID: <20210128181627.GB11790@lst.de> (raw)
In-Reply-To: <20210128003829.1892018-3-jxgao@google.com>
On Wed, Jan 27, 2021 at 04:38:28PM -0800, Jianxiong Gao wrote:
> For devices that need to preserve address offset on mapping through
> swiotlb, this patch adds offset preserving based on page_offset_mask
> and keeps the offset if the mask is non zero. This is needed for
> device drivers like NVMe.
>
> Signed-off-by: Jianxiong Gao <jxgao@google.com>
> ---
> kernel/dma/swiotlb.c | 25 ++++++++++++++++++++++---
> 1 file changed, 22 insertions(+), 3 deletions(-)
>
> diff --git a/kernel/dma/swiotlb.c b/kernel/dma/swiotlb.c
> index 7c42df6e6100..4cab35f2c9bc 100644
> --- a/kernel/dma/swiotlb.c
> +++ b/kernel/dma/swiotlb.c
> @@ -468,7 +468,7 @@ phys_addr_t swiotlb_tbl_map_single(struct device *hwdev, phys_addr_t orig_addr,
> dma_addr_t tbl_dma_addr = phys_to_dma_unencrypted(hwdev, io_tlb_start);
> unsigned long flags;
> phys_addr_t tlb_addr;
> - unsigned int nslots, stride, index, wrap;
> + unsigned int nslots, stride, index, wrap, page_offset_mask, page_offset;
> int i;
> unsigned long mask;
> unsigned long offset_slots;
> @@ -500,12 +500,16 @@ phys_addr_t swiotlb_tbl_map_single(struct device *hwdev, phys_addr_t orig_addr,
> ? ALIGN(mask + 1, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT
> : 1UL << (BITS_PER_LONG - IO_TLB_SHIFT);
>
> + page_offset_mask = dma_get_page_offset_mask(hwdev);
> + page_offset = orig_addr & page_offset_mask;
> + alloc_size += page_offset;
> +
> /*
> * For mappings greater than or equal to a page, we limit the stride
> * (and hence alignment) to a page size.
> */
> nslots = ALIGN(alloc_size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
> - if (alloc_size >= PAGE_SIZE)
> + if ((alloc_size >= PAGE_SIZE) || (page_offset_mask > (1 << IO_TLB_SHIFT)))
> stride = (1 << (PAGE_SHIFT - IO_TLB_SHIFT));
> else
> stride = 1;
> @@ -583,6 +587,11 @@ phys_addr_t swiotlb_tbl_map_single(struct device *hwdev, phys_addr_t orig_addr,
> */
> for (i = 0; i < nslots; i++)
> io_tlb_orig_addr[index+i] = orig_addr + (i << IO_TLB_SHIFT);
> + /*
> + * When keeping the offset of the original data, we need to advance
> + * the tlb_addr by the offset of orig_addr.
> + */
> + tlb_addr += page_offset;
> if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC) &&
> (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL))
> swiotlb_bounce(orig_addr, tlb_addr, mapping_size, DMA_TO_DEVICE);
> @@ -598,7 +607,9 @@ void swiotlb_tbl_unmap_single(struct device *hwdev, phys_addr_t tlb_addr,
> enum dma_data_direction dir, unsigned long attrs)
> {
> unsigned long flags;
> - int i, count, nslots = ALIGN(alloc_size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
> + unsigned int num_page_offset_slabs, page_offset_mask = dma_get_page_offset_mask(hwdev);
Yikes, please avoid these crazy long lines.
> + num_page_offset_slabs = (tlb_addr & page_offset_mask) / (1 << IO_TLB_SHIFT);
also a double whitespace here.
_______________________________________________
iommu mailing list
iommu@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/iommu
next prev parent reply other threads:[~2021-01-28 18:16 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-01-28 0:38 [PATCH 0/3] Adding offset keeping option when mapping data via SWIOTLB.* Jianxiong Gao via iommu
2021-01-28 0:38 ` [PATCH 1/3] Adding page_offset_mask to device_dma_parameters Jianxiong Gao via iommu
2021-01-28 17:27 ` Robin Murphy
2021-01-28 18:15 ` Christoph Hellwig
2021-01-28 0:38 ` [PATCH 2/3] Add swiotlb offset preserving mapping when dma_dma_parameters->page_offset_mask is non zero Jianxiong Gao via iommu
2021-01-28 17:15 ` Konrad Rzeszutek Wilk
2021-01-28 17:34 ` Keith Busch
2021-01-28 18:06 ` Jianxiong Gao via iommu
2021-01-28 18:16 ` Christoph Hellwig [this message]
2021-01-28 0:38 ` [PATCH 3/3] Adding device_dma_parameters->offset_preserve_mask to NVMe driver Jianxiong Gao via iommu
2021-01-28 18:00 ` Robin Murphy
2021-01-28 18:18 ` Christoph Hellwig
2021-01-28 18:28 ` Jianxiong Gao via iommu
2021-01-28 8:04 ` [PATCH 0/3] Adding offset keeping option when mapping data via SWIOTLB.* Greg KH
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210128181627.GB11790@lst.de \
--to=hch@lst.de \
--cc=andriy.shevchenko@linux.intel.com \
--cc=axboe@fb.com \
--cc=bgolaszewski@baylibre.com \
--cc=dan.j.williams@intel.com \
--cc=gregkh@linuxfoundation.org \
--cc=heikki.krogerus@linux.intel.com \
--cc=iommu@lists.linux-foundation.org \
--cc=jroedel@suse.de \
--cc=jxgao@google.com \
--cc=kbusch@kernel.org \
--cc=konrad.wilk@oracle.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-nvme@lists.infradead.org \
--cc=marcorr@google.com \
--cc=rafael.j.wysocki@intel.com \
--cc=robin.murphy@arm.com \
--cc=sagi@grimberg.me \
--cc=saravanak@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).