Skip to content

Commit 05ee774

Browse files
Petr TesarikChristoph Hellwig
authored andcommitted
swiotlb: make io_tlb_default_mem local to swiotlb.c
SWIOTLB implementation details should not be exposed to the rest of the kernel. This will allow to make changes to the implementation without modifying non-swiotlb code. To avoid breaking existing users, provide helper functions for the few required fields. As a bonus, using a helper function to initialize struct device allows to get rid of an #ifdef in driver core. Signed-off-by: Petr Tesarik <petr.tesarik.ext@huawei.com> Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Signed-off-by: Christoph Hellwig <hch@lst.de>
1 parent 0c6874a commit 05ee774

File tree

6 files changed

+67
-10
lines changed

6 files changed

+67
-10
lines changed

arch/mips/pci/pci-octeon.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ static int __init octeon_pci_setup(void)
664664

665665
/* BAR1 movable regions contiguous to cover the swiotlb */
666666
octeon_bar1_pci_phys =
667-
io_tlb_default_mem.start & ~((1ull << 22) - 1);
667+
default_swiotlb_base() & ~((1ull << 22) - 1);
668668

669669
for (index = 0; index < 32; index++) {
670670
union cvmx_pci_bar1_indexx bar1_index;

drivers/base/core.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3108,9 +3108,7 @@ void device_initialize(struct device *dev)
31083108
defined(CONFIG_ARCH_HAS_SYNC_DMA_FOR_CPU_ALL)
31093109
dev->dma_coherent = dma_default_coherent;
31103110
#endif
3111-
#ifdef CONFIG_SWIOTLB
3112-
dev->dma_io_tlb_mem = &io_tlb_default_mem;
3113-
#endif
3111+
swiotlb_dev_init(dev);
31143112
}
31153113
EXPORT_SYMBOL_GPL(device_initialize);
31163114

drivers/xen/swiotlb-xen.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ xen_swiotlb_sync_sg_for_device(struct device *dev, struct scatterlist *sgl,
381381
static int
382382
xen_swiotlb_dma_supported(struct device *hwdev, u64 mask)
383383
{
384-
return xen_phys_to_dma(hwdev, io_tlb_default_mem.end - 1) <= mask;
384+
return xen_phys_to_dma(hwdev, default_swiotlb_limit()) <= mask;
385385
}
386386

387387
const struct dma_map_ops xen_swiotlb_dma_ops = {

include/linux/swiotlb.h

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ struct io_tlb_mem {
110110
atomic_long_t used_hiwater;
111111
#endif
112112
};
113-
extern struct io_tlb_mem io_tlb_default_mem;
114113

115114
static inline bool is_swiotlb_buffer(struct device *dev, phys_addr_t paddr)
116115
{
@@ -128,13 +127,22 @@ static inline bool is_swiotlb_force_bounce(struct device *dev)
128127

129128
void swiotlb_init(bool addressing_limited, unsigned int flags);
130129
void __init swiotlb_exit(void);
130+
void swiotlb_dev_init(struct device *dev);
131131
size_t swiotlb_max_mapping_size(struct device *dev);
132+
bool is_swiotlb_allocated(void);
132133
bool is_swiotlb_active(struct device *dev);
133134
void __init swiotlb_adjust_size(unsigned long size);
135+
phys_addr_t default_swiotlb_base(void);
136+
phys_addr_t default_swiotlb_limit(void);
134137
#else
135138
static inline void swiotlb_init(bool addressing_limited, unsigned int flags)
136139
{
137140
}
141+
142+
static inline void swiotlb_dev_init(struct device *dev)
143+
{
144+
}
145+
138146
static inline bool is_swiotlb_buffer(struct device *dev, phys_addr_t paddr)
139147
{
140148
return false;
@@ -151,6 +159,11 @@ static inline size_t swiotlb_max_mapping_size(struct device *dev)
151159
return SIZE_MAX;
152160
}
153161

162+
static inline bool is_swiotlb_allocated(void)
163+
{
164+
return false;
165+
}
166+
154167
static inline bool is_swiotlb_active(struct device *dev)
155168
{
156169
return false;
@@ -159,6 +172,16 @@ static inline bool is_swiotlb_active(struct device *dev)
159172
static inline void swiotlb_adjust_size(unsigned long size)
160173
{
161174
}
175+
176+
static inline phys_addr_t default_swiotlb_base(void)
177+
{
178+
return 0;
179+
}
180+
181+
static inline phys_addr_t default_swiotlb_limit(void)
182+
{
183+
return 0;
184+
}
162185
#endif /* CONFIG_SWIOTLB */
163186

164187
extern void swiotlb_print_info(void);

kernel/dma/swiotlb.c

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ struct io_tlb_slot {
7171
static bool swiotlb_force_bounce;
7272
static bool swiotlb_force_disable;
7373

74-
struct io_tlb_mem io_tlb_default_mem;
74+
static struct io_tlb_mem io_tlb_default_mem;
7575

7676
static unsigned long default_nslabs = IO_TLB_DEFAULT_SIZE >> IO_TLB_SHIFT;
7777
static unsigned long default_nareas;
@@ -489,6 +489,15 @@ void __init swiotlb_exit(void)
489489
memset(mem, 0, sizeof(*mem));
490490
}
491491

492+
/**
493+
* swiotlb_dev_init() - initialize swiotlb fields in &struct device
494+
* @dev: Device to be initialized.
495+
*/
496+
void swiotlb_dev_init(struct device *dev)
497+
{
498+
dev->dma_io_tlb_mem = &io_tlb_default_mem;
499+
}
500+
492501
/*
493502
* Return the offset into a iotlb slot required to keep the device happy.
494503
*/
@@ -953,13 +962,41 @@ size_t swiotlb_max_mapping_size(struct device *dev)
953962
return ((size_t)IO_TLB_SIZE) * IO_TLB_SEGSIZE - min_align;
954963
}
955964

965+
/**
966+
* is_swiotlb_allocated() - check if the default software IO TLB is initialized
967+
*/
968+
bool is_swiotlb_allocated(void)
969+
{
970+
return io_tlb_default_mem.nslabs;
971+
}
972+
956973
bool is_swiotlb_active(struct device *dev)
957974
{
958975
struct io_tlb_mem *mem = dev->dma_io_tlb_mem;
959976

960977
return mem && mem->nslabs;
961978
}
962979

980+
/**
981+
* default_swiotlb_base() - get the base address of the default SWIOTLB
982+
*
983+
* Get the lowest physical address used by the default software IO TLB pool.
984+
*/
985+
phys_addr_t default_swiotlb_base(void)
986+
{
987+
return io_tlb_default_mem.start;
988+
}
989+
990+
/**
991+
* default_swiotlb_limit() - get the address limit of the default SWIOTLB
992+
*
993+
* Get the highest physical address used by the default software IO TLB pool.
994+
*/
995+
phys_addr_t default_swiotlb_limit(void)
996+
{
997+
return io_tlb_default_mem.end - 1;
998+
}
999+
9631000
#ifdef CONFIG_DEBUG_FS
9641001

9651002
static int io_tlb_used_get(void *data, u64 *val)

mm/slab_common.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -864,10 +864,9 @@ void __init setup_kmalloc_cache_index_table(void)
864864

865865
static unsigned int __kmalloc_minalign(void)
866866
{
867-
#ifdef CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC
868-
if (io_tlb_default_mem.nslabs)
867+
if (IS_ENABLED(CONFIG_DMA_BOUNCE_UNALIGNED_KMALLOC) &&
868+
is_swiotlb_allocated())
869869
return ARCH_KMALLOC_MINALIGN;
870-
#endif
871870
return dma_get_cache_alignment();
872871
}
873872

0 commit comments

Comments
 (0)