From: Jianxiong Gao <jxgao@google.com>
To: jxgao@google.com, erdemaktas@google.com, marcorr@google.com,
hch@lst.de, m.szyprowski@samsung.com, robin.murphy@arm.com,
gregkh@linuxfoundation.org, saravanak@google.com,
heikki.krogerus@linux.intel.com, rafael.j.wysocki@intel.com,
andriy.shevchenko@linux.intel.com, dan.j.williams@intel.com,
bgolaszewski@baylibre.com, jroedel@suse.de,
iommu@lists.linux-foundation.org, konrad.wilk@oracle.com,
kbusch@kernel.org, axboe@fb.com, sagi@grimberg.me,
linux-nvme@lists.infradead.org, linux-kernel@vger.kernel.org
Subject: [PATCH 2/3] Add swiotlb offset preserving mapping when dma_dma_parameters->page_offset_mask is non zero.
Date: Wed, 27 Jan 2021 16:38:28 -0800 [thread overview]
Message-ID: <20210128003829.1892018-3-jxgao@google.com> (raw)
In-Reply-To: <20210128003829.1892018-1-jxgao@google.com>
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);
+ int i, count;
+ int nslots = ALIGN(alloc_size + tlb_addr & page_offset_mask, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
int index = (tlb_addr - io_tlb_start) >> IO_TLB_SHIFT;
phys_addr_t orig_addr = io_tlb_orig_addr[index];
@@ -610,6 +621,14 @@ void swiotlb_tbl_unmap_single(struct device *hwdev, phys_addr_t tlb_addr,
((dir == DMA_FROM_DEVICE) || (dir == DMA_BIDIRECTIONAL)))
swiotlb_bounce(orig_addr, tlb_addr, mapping_size, DMA_FROM_DEVICE);
+ /*
+ * When dma_get_page_offset_mask is used, we may have padded more slabs
+ * when padding exceeds one slab. We need to move index back to the
+ * beginning of the padding.
+ */
+ num_page_offset_slabs = (tlb_addr & page_offset_mask) / (1 << IO_TLB_SHIFT);
+ index -= num_page_offset_slabs;
+
/*
* Return the buffer to the free list by setting the corresponding
* entries to indicate the number of contiguous entries available.
--
2.27.0
_______________________________________________
Linux-nvme mailing list
Linux-nvme@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-nvme
next prev parent reply other threads:[~2021-01-28 0:39 UTC|newest]
Thread overview: 12+ 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
2021-01-28 0:38 ` [PATCH 1/3] Adding page_offset_mask to device_dma_parameters Jianxiong Gao
2021-01-28 17:27 ` Robin Murphy
2021-01-28 18:15 ` Christoph Hellwig
2021-01-28 0:38 ` Jianxiong Gao [this message]
2021-01-28 17:15 ` [PATCH 2/3] Add swiotlb offset preserving mapping when dma_dma_parameters->page_offset_mask is non zero Konrad Rzeszutek Wilk
2021-01-28 17:34 ` Keith Busch
2021-01-28 18:16 ` Christoph Hellwig
2021-01-28 0:38 ` [PATCH 3/3] Adding device_dma_parameters->offset_preserve_mask to NVMe driver Jianxiong Gao
2021-01-28 18:00 ` Robin Murphy
2021-01-28 18:18 ` Christoph Hellwig
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=20210128003829.1892018-3-jxgao@google.com \
--to=jxgao@google.com \
--cc=andriy.shevchenko@linux.intel.com \
--cc=axboe@fb.com \
--cc=bgolaszewski@baylibre.com \
--cc=dan.j.williams@intel.com \
--cc=erdemaktas@google.com \
--cc=gregkh@linuxfoundation.org \
--cc=hch@lst.de \
--cc=heikki.krogerus@linux.intel.com \
--cc=iommu@lists.linux-foundation.org \
--cc=jroedel@suse.de \
--cc=kbusch@kernel.org \
--cc=konrad.wilk@oracle.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-nvme@lists.infradead.org \
--cc=m.szyprowski@samsung.com \
--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