swiotlb: make the swiotlb_init interface more useful

Pass a boolean flag to indicate if swiotlb needs to be enabled based on
the addressing needs, and replace the verbose argument with a set of
flags, including one to force enable bounce buffering.

Note that this patch removes the possibility to force xen-swiotlb use
with the swiotlb=force parameter on the command line on x86 (arm and
arm64 never supported that), but this interface will be restored shortly.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
Tested-by: Boris Ostrovsky <boris.ostrovsky@oracle.com>
This commit is contained in:
Christoph Hellwig 2022-03-29 17:27:33 +02:00
parent a3e2309267
commit c6af2aa9ff
15 changed files with 55 additions and 82 deletions

View File

@ -271,11 +271,7 @@ static void __init free_highpages(void)
void __init mem_init(void) void __init mem_init(void)
{ {
#ifdef CONFIG_ARM_LPAE #ifdef CONFIG_ARM_LPAE
if (swiotlb_force == SWIOTLB_FORCE || swiotlb_init(max_pfn > arm_dma_pfn_limit, SWIOTLB_VERBOSE);
max_pfn > arm_dma_pfn_limit)
swiotlb_init(1);
else
swiotlb_force = SWIOTLB_NO_FORCE;
#endif #endif
set_max_mapnr(pfn_to_page(max_pfn) - mem_map); set_max_mapnr(pfn_to_page(max_pfn) - mem_map);

View File

@ -398,11 +398,7 @@ void __init bootmem_init(void)
*/ */
void __init mem_init(void) void __init mem_init(void)
{ {
if (swiotlb_force == SWIOTLB_FORCE || swiotlb_init(max_pfn > PFN_DOWN(arm64_dma_phys_limit), SWIOTLB_VERBOSE);
max_pfn > PFN_DOWN(arm64_dma_phys_limit))
swiotlb_init(1);
else if (!xen_swiotlb_detect())
swiotlb_force = SWIOTLB_NO_FORCE;
/* this will put all unused low memory onto the freelists */ /* this will put all unused low memory onto the freelists */
memblock_free_all(); memblock_free_all();

View File

@ -437,9 +437,7 @@ mem_init (void)
if (iommu_detected) if (iommu_detected)
break; break;
#endif #endif
#ifdef CONFIG_SWIOTLB swiotlb_init(true, SWIOTLB_VERBOSE);
swiotlb_init(1);
#endif
} while (0); } while (0);
#ifdef CONFIG_FLATMEM #ifdef CONFIG_FLATMEM

View File

@ -235,5 +235,5 @@ void __init plat_swiotlb_setup(void)
#endif #endif
swiotlb_adjust_size(swiotlbsize); swiotlb_adjust_size(swiotlbsize);
swiotlb_init(1); swiotlb_init(true, SWIOTLB_VERBOSE);
} }

View File

@ -24,5 +24,5 @@ phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr)
void __init plat_swiotlb_setup(void) void __init plat_swiotlb_setup(void)
{ {
swiotlb_init(1); swiotlb_init(true, SWIOTLB_VERBOSE);
} }

View File

@ -10,5 +10,5 @@
void __init plat_swiotlb_setup(void) void __init plat_swiotlb_setup(void)
{ {
swiotlb_init(1); swiotlb_init(true, SWIOTLB_VERBOSE);
} }

View File

@ -17,6 +17,7 @@
#include <linux/suspend.h> #include <linux/suspend.h>
#include <linux/dma-direct.h> #include <linux/dma-direct.h>
#include <asm/swiotlb.h>
#include <asm/machdep.h> #include <asm/machdep.h>
#include <asm/rtas.h> #include <asm/rtas.h>
#include <asm/kasan.h> #include <asm/kasan.h>
@ -251,7 +252,7 @@ void __init mem_init(void)
if (is_secure_guest()) if (is_secure_guest())
svm_swiotlb_init(); svm_swiotlb_init();
else else
swiotlb_init(0); swiotlb_init(ppc_swiotlb_enable, 0);
#endif #endif
high_memory = (void *) __va(max_low_pfn * PAGE_SIZE); high_memory = (void *) __va(max_low_pfn * PAGE_SIZE);

View File

@ -849,9 +849,6 @@ static void __init pSeries_setup_arch(void)
} }
ppc_md.pcibios_root_bridge_prepare = pseries_root_bridge_prepare; ppc_md.pcibios_root_bridge_prepare = pseries_root_bridge_prepare;
if (swiotlb_force == SWIOTLB_FORCE)
ppc_swiotlb_enable = 1;
} }
static void pseries_panic(char *str) static void pseries_panic(char *str)

View File

@ -120,13 +120,7 @@ void __init mem_init(void)
BUG_ON(!mem_map); BUG_ON(!mem_map);
#endif /* CONFIG_FLATMEM */ #endif /* CONFIG_FLATMEM */
#ifdef CONFIG_SWIOTLB swiotlb_init(max_pfn > PFN_DOWN(dma32_phys_limit), SWIOTLB_VERBOSE);
if (swiotlb_force == SWIOTLB_FORCE ||
max_pfn > PFN_DOWN(dma32_phys_limit))
swiotlb_init(1);
else
swiotlb_force = SWIOTLB_NO_FORCE;
#endif
memblock_free_all(); memblock_free_all();
print_vm_layout(); print_vm_layout();

View File

@ -185,8 +185,7 @@ static void pv_init(void)
return; return;
/* make sure bounce buffers are shared */ /* make sure bounce buffers are shared */
swiotlb_force = SWIOTLB_FORCE; swiotlb_init(true, SWIOTLB_FORCE | SWIOTLB_VERBOSE);
swiotlb_init(1);
swiotlb_update_mem_attributes(); swiotlb_update_mem_attributes();
} }

View File

@ -39,6 +39,7 @@ int iommu_detected __read_mostly = 0;
#ifdef CONFIG_SWIOTLB #ifdef CONFIG_SWIOTLB
bool x86_swiotlb_enable; bool x86_swiotlb_enable;
static unsigned int x86_swiotlb_flags;
static void __init pci_swiotlb_detect(void) static void __init pci_swiotlb_detect(void)
{ {
@ -58,16 +59,16 @@ static void __init pci_swiotlb_detect(void)
* bounce buffers as the hypervisor can't access arbitrary VM memory * bounce buffers as the hypervisor can't access arbitrary VM memory
* that is not explicitly shared with it. * that is not explicitly shared with it.
*/ */
if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)) if (cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT)) {
swiotlb_force = SWIOTLB_FORCE;
if (swiotlb_force == SWIOTLB_FORCE)
x86_swiotlb_enable = true; x86_swiotlb_enable = true;
x86_swiotlb_flags |= SWIOTLB_FORCE;
}
} }
#else #else
static inline void __init pci_swiotlb_detect(void) static inline void __init pci_swiotlb_detect(void)
{ {
} }
#define x86_swiotlb_flags 0
#endif /* CONFIG_SWIOTLB */ #endif /* CONFIG_SWIOTLB */
#ifdef CONFIG_SWIOTLB_XEN #ifdef CONFIG_SWIOTLB_XEN
@ -75,8 +76,7 @@ static bool xen_swiotlb;
static void __init pci_xen_swiotlb_init(void) static void __init pci_xen_swiotlb_init(void)
{ {
if (!xen_initial_domain() && !x86_swiotlb_enable && if (!xen_initial_domain() && !x86_swiotlb_enable)
swiotlb_force != SWIOTLB_FORCE)
return; return;
x86_swiotlb_enable = true; x86_swiotlb_enable = true;
xen_swiotlb = true; xen_swiotlb = true;
@ -120,8 +120,7 @@ void __init pci_iommu_alloc(void)
gart_iommu_hole_init(); gart_iommu_hole_init();
amd_iommu_detect(); amd_iommu_detect();
detect_intel_iommu(); detect_intel_iommu();
if (x86_swiotlb_enable) swiotlb_init(x86_swiotlb_enable, x86_swiotlb_flags);
swiotlb_init(0);
} }
/* /*

View File

@ -251,7 +251,7 @@ retry:
panic("%s (rc:%d)", xen_swiotlb_error(XEN_SWIOTLB_EFIXUP), rc); panic("%s (rc:%d)", xen_swiotlb_error(XEN_SWIOTLB_EFIXUP), rc);
} }
if (swiotlb_init_with_tbl(start, nslabs, true)) if (swiotlb_init_with_tbl(start, nslabs, SWIOTLB_VERBOSE))
panic("Cannot allocate SWIOTLB buffer"); panic("Cannot allocate SWIOTLB buffer");
} }
#endif /* CONFIG_X86 */ #endif /* CONFIG_X86 */
@ -376,7 +376,7 @@ static dma_addr_t xen_swiotlb_map_page(struct device *dev, struct page *page,
/* /*
* Oh well, have to allocate and map a bounce buffer. * Oh well, have to allocate and map a bounce buffer.
*/ */
trace_swiotlb_bounced(dev, dev_addr, size, swiotlb_force); trace_swiotlb_bounced(dev, dev_addr, size);
map = swiotlb_tbl_map_single(dev, phys, size, size, 0, dir, attrs); map = swiotlb_tbl_map_single(dev, phys, size, size, 0, dir, attrs);
if (map == (phys_addr_t)DMA_MAPPING_ERROR) if (map == (phys_addr_t)DMA_MAPPING_ERROR)

View File

@ -13,11 +13,8 @@ struct device;
struct page; struct page;
struct scatterlist; struct scatterlist;
enum swiotlb_force { #define SWIOTLB_VERBOSE (1 << 0) /* verbose initialization */
SWIOTLB_NORMAL, /* Default - depending on HW DMA mask etc. */ #define SWIOTLB_FORCE (1 << 1) /* force bounce buffering */
SWIOTLB_FORCE, /* swiotlb=force */
SWIOTLB_NO_FORCE, /* swiotlb=noforce */
};
/* /*
* Maximum allowable number of contiguous slabs to map, * Maximum allowable number of contiguous slabs to map,
@ -36,8 +33,7 @@ enum swiotlb_force {
/* default to 64MB */ /* default to 64MB */
#define IO_TLB_DEFAULT_SIZE (64UL<<20) #define IO_TLB_DEFAULT_SIZE (64UL<<20)
extern void swiotlb_init(int verbose); int swiotlb_init_with_tbl(char *tlb, unsigned long nslabs, unsigned int flags);
int swiotlb_init_with_tbl(char *tlb, unsigned long nslabs, int verbose);
unsigned long swiotlb_size_or_default(void); unsigned long swiotlb_size_or_default(void);
extern int swiotlb_late_init_with_tbl(char *tlb, unsigned long nslabs); extern int swiotlb_late_init_with_tbl(char *tlb, unsigned long nslabs);
int swiotlb_init_late(size_t size); int swiotlb_init_late(size_t size);
@ -126,13 +122,16 @@ static inline bool is_swiotlb_force_bounce(struct device *dev)
return mem && mem->force_bounce; return mem && mem->force_bounce;
} }
void swiotlb_init(bool addressing_limited, unsigned int flags);
void __init swiotlb_exit(void); void __init swiotlb_exit(void);
unsigned int swiotlb_max_segment(void); unsigned int swiotlb_max_segment(void);
size_t swiotlb_max_mapping_size(struct device *dev); size_t swiotlb_max_mapping_size(struct device *dev);
bool is_swiotlb_active(struct device *dev); bool is_swiotlb_active(struct device *dev);
void __init swiotlb_adjust_size(unsigned long size); void __init swiotlb_adjust_size(unsigned long size);
#else #else
#define swiotlb_force SWIOTLB_NO_FORCE static inline void swiotlb_init(bool addressing_limited, unsigned int flags)
{
}
static inline bool is_swiotlb_buffer(struct device *dev, phys_addr_t paddr) static inline bool is_swiotlb_buffer(struct device *dev, phys_addr_t paddr)
{ {
return false; return false;

View File

@ -8,20 +8,15 @@
#include <linux/tracepoint.h> #include <linux/tracepoint.h>
TRACE_EVENT(swiotlb_bounced, TRACE_EVENT(swiotlb_bounced,
TP_PROTO(struct device *dev, dma_addr_t dev_addr, size_t size),
TP_PROTO(struct device *dev, TP_ARGS(dev, dev_addr, size),
dma_addr_t dev_addr,
size_t size,
enum swiotlb_force swiotlb_force),
TP_ARGS(dev, dev_addr, size, swiotlb_force),
TP_STRUCT__entry( TP_STRUCT__entry(
__string( dev_name, dev_name(dev) ) __string(dev_name, dev_name(dev))
__field( u64, dma_mask ) __field(u64, dma_mask)
__field( dma_addr_t, dev_addr ) __field(dma_addr_t, dev_addr)
__field( size_t, size ) __field(size_t, size)
__field( enum swiotlb_force, swiotlb_force ) __field(bool, force)
), ),
TP_fast_assign( TP_fast_assign(
@ -29,19 +24,15 @@ TRACE_EVENT(swiotlb_bounced,
__entry->dma_mask = (dev->dma_mask ? *dev->dma_mask : 0); __entry->dma_mask = (dev->dma_mask ? *dev->dma_mask : 0);
__entry->dev_addr = dev_addr; __entry->dev_addr = dev_addr;
__entry->size = size; __entry->size = size;
__entry->swiotlb_force = swiotlb_force; __entry->force = is_swiotlb_force_bounce(dev);
), ),
TP_printk("dev_name: %s dma_mask=%llx dev_addr=%llx " TP_printk("dev_name: %s dma_mask=%llx dev_addr=%llx size=%zu %s",
"size=%zu %s",
__get_str(dev_name), __get_str(dev_name),
__entry->dma_mask, __entry->dma_mask,
(unsigned long long)__entry->dev_addr, (unsigned long long)__entry->dev_addr,
__entry->size, __entry->size,
__print_symbolic(__entry->swiotlb_force, __entry->force ? "FORCE" : "NORMAL")
{ SWIOTLB_NORMAL, "NORMAL" },
{ SWIOTLB_FORCE, "FORCE" },
{ SWIOTLB_NO_FORCE, "NO_FORCE" }))
); );
#endif /* _TRACE_SWIOTLB_H */ #endif /* _TRACE_SWIOTLB_H */

View File

@ -62,7 +62,8 @@
#define INVALID_PHYS_ADDR (~(phys_addr_t)0) #define INVALID_PHYS_ADDR (~(phys_addr_t)0)
enum swiotlb_force swiotlb_force; static bool swiotlb_force_bounce;
static bool swiotlb_force_disable;
struct io_tlb_mem io_tlb_default_mem; struct io_tlb_mem io_tlb_default_mem;
@ -81,9 +82,9 @@ setup_io_tlb_npages(char *str)
if (*str == ',') if (*str == ',')
++str; ++str;
if (!strcmp(str, "force")) if (!strcmp(str, "force"))
swiotlb_force = SWIOTLB_FORCE; swiotlb_force_bounce = true;
else if (!strcmp(str, "noforce")) else if (!strcmp(str, "noforce"))
swiotlb_force = SWIOTLB_NO_FORCE; swiotlb_force_disable = true;
return 0; return 0;
} }
@ -202,7 +203,7 @@ static void swiotlb_init_io_tlb_mem(struct io_tlb_mem *mem, phys_addr_t start,
mem->index = 0; mem->index = 0;
mem->late_alloc = late_alloc; mem->late_alloc = late_alloc;
if (swiotlb_force == SWIOTLB_FORCE) if (swiotlb_force_bounce)
mem->force_bounce = true; mem->force_bounce = true;
spin_lock_init(&mem->lock); spin_lock_init(&mem->lock);
@ -224,12 +225,13 @@ static void swiotlb_init_io_tlb_mem(struct io_tlb_mem *mem, phys_addr_t start,
return; return;
} }
int __init swiotlb_init_with_tbl(char *tlb, unsigned long nslabs, int verbose) int __init swiotlb_init_with_tbl(char *tlb, unsigned long nslabs,
unsigned int flags)
{ {
struct io_tlb_mem *mem = &io_tlb_default_mem; struct io_tlb_mem *mem = &io_tlb_default_mem;
size_t alloc_size; size_t alloc_size;
if (swiotlb_force == SWIOTLB_NO_FORCE) if (swiotlb_force_disable)
return 0; return 0;
/* protect against double initialization */ /* protect against double initialization */
@ -243,8 +245,9 @@ int __init swiotlb_init_with_tbl(char *tlb, unsigned long nslabs, int verbose)
__func__, alloc_size, PAGE_SIZE); __func__, alloc_size, PAGE_SIZE);
swiotlb_init_io_tlb_mem(mem, __pa(tlb), nslabs, false); swiotlb_init_io_tlb_mem(mem, __pa(tlb), nslabs, false);
mem->force_bounce = flags & SWIOTLB_FORCE;
if (verbose) if (flags & SWIOTLB_VERBOSE)
swiotlb_print_info(); swiotlb_print_info();
return 0; return 0;
} }
@ -253,20 +256,21 @@ int __init swiotlb_init_with_tbl(char *tlb, unsigned long nslabs, int verbose)
* Statically reserve bounce buffer space and initialize bounce buffer data * Statically reserve bounce buffer space and initialize bounce buffer data
* structures for the software IO TLB used to implement the DMA API. * structures for the software IO TLB used to implement the DMA API.
*/ */
void __init void __init swiotlb_init(bool addressing_limit, unsigned int flags)
swiotlb_init(int verbose)
{ {
size_t bytes = PAGE_ALIGN(default_nslabs << IO_TLB_SHIFT); size_t bytes = PAGE_ALIGN(default_nslabs << IO_TLB_SHIFT);
void *tlb; void *tlb;
if (swiotlb_force == SWIOTLB_NO_FORCE) if (!addressing_limit && !swiotlb_force_bounce)
return;
if (swiotlb_force_disable)
return; return;
/* Get IO TLB memory from the low pages */ /* Get IO TLB memory from the low pages */
tlb = memblock_alloc_low(bytes, PAGE_SIZE); tlb = memblock_alloc_low(bytes, PAGE_SIZE);
if (!tlb) if (!tlb)
goto fail; goto fail;
if (swiotlb_init_with_tbl(tlb, default_nslabs, verbose)) if (swiotlb_init_with_tbl(tlb, default_nslabs, flags))
goto fail_free_mem; goto fail_free_mem;
return; return;
@ -289,7 +293,7 @@ int swiotlb_init_late(size_t size)
unsigned int order; unsigned int order;
int rc = 0; int rc = 0;
if (swiotlb_force == SWIOTLB_NO_FORCE) if (swiotlb_force_disable)
return 0; return 0;
/* /*
@ -328,7 +332,7 @@ swiotlb_late_init_with_tbl(char *tlb, unsigned long nslabs)
struct io_tlb_mem *mem = &io_tlb_default_mem; struct io_tlb_mem *mem = &io_tlb_default_mem;
unsigned long bytes = nslabs << IO_TLB_SHIFT; unsigned long bytes = nslabs << IO_TLB_SHIFT;
if (swiotlb_force == SWIOTLB_NO_FORCE) if (swiotlb_force_disable)
return 0; return 0;
/* protect against double initialization */ /* protect against double initialization */
@ -353,7 +357,7 @@ void __init swiotlb_exit(void)
unsigned long tbl_vaddr; unsigned long tbl_vaddr;
size_t tbl_size, slots_size; size_t tbl_size, slots_size;
if (swiotlb_force == SWIOTLB_FORCE) if (swiotlb_force_bounce)
return; return;
if (!mem->nslabs) if (!mem->nslabs)
@ -704,8 +708,7 @@ dma_addr_t swiotlb_map(struct device *dev, phys_addr_t paddr, size_t size,
phys_addr_t swiotlb_addr; phys_addr_t swiotlb_addr;
dma_addr_t dma_addr; dma_addr_t dma_addr;
trace_swiotlb_bounced(dev, phys_to_dma(dev, paddr), size, trace_swiotlb_bounced(dev, phys_to_dma(dev, paddr), size);
swiotlb_force);
swiotlb_addr = swiotlb_tbl_map_single(dev, paddr, size, size, 0, dir, swiotlb_addr = swiotlb_tbl_map_single(dev, paddr, size, size, 0, dir,
attrs); attrs);