Skip to content

Commit 25ed882

Browse files
Jianxiong Gaogregkh
authored andcommitted
swiotlb: refactor swiotlb_tbl_map_single
commit: 26a7e09 Split out a bunch of a self-contained helpers to make the function easier to follow. Signed-off-by: Christoph Hellwig <hch@lst.de> Acked-by: Jianxiong Gao <jxgao@google.com> Tested-by: Jianxiong Gao <jxgao@google.com> Signed-off-by: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com> Signed-off-by: Jianxiong Gao <jxgao@google.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 9efd5df commit 25ed882

1 file changed

Lines changed: 89 additions & 90 deletions

File tree

‎kernel/dma/swiotlb.c‎

Lines changed: 89 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -452,134 +452,133 @@ static void swiotlb_bounce(phys_addr_t orig_addr, phys_addr_t tlb_addr,
452452
}
453453
}
454454

455-
phys_addr_t swiotlb_tbl_map_single(struct device *hwdev, phys_addr_t orig_addr,
456-
size_t mapping_size, size_t alloc_size,
457-
enum dma_data_direction dir, unsigned long attrs)
458-
{
459-
dma_addr_t tbl_dma_addr = phys_to_dma_unencrypted(hwdev, io_tlb_start);
460-
unsigned long flags;
461-
phys_addr_t tlb_addr;
462-
unsigned int nslots, stride, index, wrap;
463-
int i;
464-
unsigned long mask;
465-
unsigned long offset_slots;
466-
unsigned long max_slots;
467-
unsigned long tmp_io_tlb_used;
468-
469-
if (no_iotlb_memory)
470-
panic("Can not allocate SWIOTLB buffer earlier and can't now provide you with the DMA bounce buffer");
471-
472-
if (mem_encrypt_active())
473-
pr_warn_once("Memory encryption is active and system is using DMA bounce buffers\n");
455+
#define slot_addr(start, idx) ((start) + ((idx) << IO_TLB_SHIFT))
474456

475-
if (mapping_size > alloc_size) {
476-
dev_warn_once(hwdev, "Invalid sizes (mapping: %zd bytes, alloc: %zd bytes)",
477-
mapping_size, alloc_size);
478-
return (phys_addr_t)DMA_MAPPING_ERROR;
479-
}
480-
481-
mask = dma_get_seg_boundary(hwdev);
457+
/*
458+
* Carefully handle integer overflow which can occur when boundary_mask == ~0UL.
459+
*/
460+
static inline unsigned long get_max_slots(unsigned long boundary_mask)
461+
{
462+
if (boundary_mask == ~0UL)
463+
return 1UL << (BITS_PER_LONG - IO_TLB_SHIFT);
464+
return nr_slots(boundary_mask + 1);
465+
}
482466

483-
tbl_dma_addr &= mask;
467+
static unsigned int wrap_index(unsigned int index)
468+
{
469+
if (index >= io_tlb_nslabs)
470+
return 0;
471+
return index;
472+
}
484473

485-
offset_slots = nr_slots(tbl_dma_addr);
474+
/*
475+
* Find a suitable number of IO TLB entries size that will fit this request and
476+
* allocate a buffer from that IO TLB pool.
477+
*/
478+
static int find_slots(struct device *dev, size_t alloc_size)
479+
{
480+
unsigned long boundary_mask = dma_get_seg_boundary(dev);
481+
dma_addr_t tbl_dma_addr =
482+
phys_to_dma_unencrypted(dev, io_tlb_start) & boundary_mask;
483+
unsigned long max_slots = get_max_slots(boundary_mask);
484+
unsigned int nslots = nr_slots(alloc_size), stride = 1;
485+
unsigned int index, wrap, count = 0, i;
486+
unsigned long flags;
486487

487-
/*
488-
* Carefully handle integer overflow which can occur when mask == ~0UL.
489-
*/
490-
max_slots = mask + 1
491-
? nr_slots(mask + 1)
492-
: 1UL << (BITS_PER_LONG - IO_TLB_SHIFT);
488+
BUG_ON(!nslots);
493489

494490
/*
495491
* For mappings greater than or equal to a page, we limit the stride
496492
* (and hence alignment) to a page size.
497493
*/
498-
nslots = nr_slots(alloc_size);
499494
if (alloc_size >= PAGE_SIZE)
500-
stride = (1 << (PAGE_SHIFT - IO_TLB_SHIFT));
501-
else
502-
stride = 1;
495+
stride <<= (PAGE_SHIFT - IO_TLB_SHIFT);
503496

504-
BUG_ON(!nslots);
505-
506-
/*
507-
* Find suitable number of IO TLB entries size that will fit this
508-
* request and allocate a buffer from that IO TLB pool.
509-
*/
510497
spin_lock_irqsave(&io_tlb_lock, flags);
511-
512498
if (unlikely(nslots > io_tlb_nslabs - io_tlb_used))
513499
goto not_found;
514500

515-
index = ALIGN(io_tlb_index, stride);
516-
if (index >= io_tlb_nslabs)
517-
index = 0;
518-
wrap = index;
519-
501+
index = wrap = wrap_index(ALIGN(io_tlb_index, stride));
520502
do {
521-
while (iommu_is_span_boundary(index, nslots, offset_slots,
522-
max_slots)) {
523-
index += stride;
524-
if (index >= io_tlb_nslabs)
525-
index = 0;
526-
if (index == wrap)
527-
goto not_found;
528-
}
529-
530503
/*
531504
* If we find a slot that indicates we have 'nslots' number of
532505
* contiguous buffers, we allocate the buffers from that slot
533506
* and mark the entries as '0' indicating unavailable.
534507
*/
535-
if (io_tlb_list[index] >= nslots) {
536-
int count = 0;
537-
538-
for (i = index; i < (int) (index + nslots); i++)
539-
io_tlb_list[i] = 0;
540-
for (i = index - 1;
541-
io_tlb_offset(i) != IO_TLB_SEGSIZE - 1 &&
542-
io_tlb_list[i]; i--)
543-
io_tlb_list[i] = ++count;
544-
tlb_addr = io_tlb_start + (index << IO_TLB_SHIFT);
545-
546-
/*
547-
* Update the indices to avoid searching in the next
548-
* round.
549-
*/
550-
io_tlb_index = ((index + nslots) < io_tlb_nslabs
551-
? (index + nslots) : 0);
552-
553-
goto found;
508+
if (!iommu_is_span_boundary(index, nslots,
509+
nr_slots(tbl_dma_addr),
510+
max_slots)) {
511+
if (io_tlb_list[index] >= nslots)
512+
goto found;
554513
}
555-
index += stride;
556-
if (index >= io_tlb_nslabs)
557-
index = 0;
514+
index = wrap_index(index + stride);
558515
} while (index != wrap);
559516

560517
not_found:
561-
tmp_io_tlb_used = io_tlb_used;
562-
563518
spin_unlock_irqrestore(&io_tlb_lock, flags);
564-
if (!(attrs & DMA_ATTR_NO_WARN) && printk_ratelimit())
565-
dev_warn(hwdev, "swiotlb buffer is full (sz: %zd bytes), total %lu (slots), used %lu (slots)\n",
566-
alloc_size, io_tlb_nslabs, tmp_io_tlb_used);
567-
return (phys_addr_t)DMA_MAPPING_ERROR;
519+
return -1;
520+
568521
found:
522+
for (i = index; i < index + nslots; i++)
523+
io_tlb_list[i] = 0;
524+
for (i = index - 1;
525+
io_tlb_offset(i) != IO_TLB_SEGSIZE - 1 &&
526+
io_tlb_list[i]; i--)
527+
io_tlb_list[i] = ++count;
528+
529+
/*
530+
* Update the indices to avoid searching in the next round.
531+
*/
532+
if (index + nslots < io_tlb_nslabs)
533+
io_tlb_index = index + nslots;
534+
else
535+
io_tlb_index = 0;
569536
io_tlb_used += nslots;
537+
570538
spin_unlock_irqrestore(&io_tlb_lock, flags);
539+
return index;
540+
}
541+
542+
phys_addr_t swiotlb_tbl_map_single(struct device *dev, phys_addr_t orig_addr,
543+
size_t mapping_size, size_t alloc_size,
544+
enum dma_data_direction dir, unsigned long attrs)
545+
{
546+
unsigned int index, i;
547+
phys_addr_t tlb_addr;
548+
549+
if (no_iotlb_memory)
550+
panic("Can not allocate SWIOTLB buffer earlier and can't now provide you with the DMA bounce buffer");
551+
552+
if (mem_encrypt_active())
553+
pr_warn_once("Memory encryption is active and system is using DMA bounce buffers\n");
554+
555+
if (mapping_size > alloc_size) {
556+
dev_warn_once(dev, "Invalid sizes (mapping: %zd bytes, alloc: %zd bytes)",
557+
mapping_size, alloc_size);
558+
return (phys_addr_t)DMA_MAPPING_ERROR;
559+
}
560+
561+
index = find_slots(dev, alloc_size);
562+
if (index == -1) {
563+
if (!(attrs & DMA_ATTR_NO_WARN))
564+
dev_warn_ratelimited(dev,
565+
"swiotlb buffer is full (sz: %zd bytes), total %lu (slots), used %lu (slots)\n",
566+
alloc_size, io_tlb_nslabs, io_tlb_used);
567+
return (phys_addr_t)DMA_MAPPING_ERROR;
568+
}
571569

572570
/*
573571
* Save away the mapping from the original address to the DMA address.
574572
* This is needed when we sync the memory. Then we sync the buffer if
575573
* needed.
576574
*/
577-
for (i = 0; i < nslots; i++)
578-
io_tlb_orig_addr[index+i] = orig_addr + (i << IO_TLB_SHIFT);
575+
for (i = 0; i < nr_slots(alloc_size); i++)
576+
io_tlb_orig_addr[index + i] = slot_addr(orig_addr, i);
577+
578+
tlb_addr = slot_addr(io_tlb_start, index);
579579
if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC) &&
580580
(dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL))
581581
swiotlb_bounce(orig_addr, tlb_addr, mapping_size, DMA_TO_DEVICE);
582-
583582
return tlb_addr;
584583
}
585584

0 commit comments

Comments
 (0)