Skip to content

Commit 1471c51

Browse files
Gaurav Batramaddy-kerneldev
authored andcommitted
powerpc/iommu: bypass DMA APIs for coherent allocations for pre-mapped memory
Leverage ARCH_HAS_DMA_MAP_DIRECT config option for coherent allocations as well. This will bypass DMA ops for memory allocations that have been pre-mapped. Always set device bus_dma_limit when memory is pre-mapped. In some architectures, like PowerPC, pmemory can be converted to regular memory via daxctl command. This will gate the coherent allocations to pre-mapped RAM only, by dma_coherent_ok(). Signed-off-by: Gaurav Batra <gbatra@linux.ibm.com> Signed-off-by: Madhavan Srinivasan <maddy@linux.ibm.com> Link: https://patch.msgid.link/20251107161105.85999-1-gbatra@linux.ibm.com
1 parent 20ab1d1 commit 1471c51

4 files changed

Lines changed: 28 additions & 16 deletions

File tree

arch/powerpc/kernel/dma-iommu.c

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,21 @@ bool arch_dma_unmap_sg_direct(struct device *dev, struct scatterlist *sg,
6565

6666
return true;
6767
}
68+
bool arch_dma_alloc_direct(struct device *dev)
69+
{
70+
if (dev->dma_ops_bypass)
71+
return true;
72+
73+
return false;
74+
}
75+
76+
bool arch_dma_free_direct(struct device *dev, dma_addr_t dma_handle)
77+
{
78+
if (!dev->dma_ops_bypass)
79+
return false;
80+
81+
return is_direct_handle(dev, dma_handle);
82+
}
6883
#endif /* CONFIG_ARCH_HAS_DMA_MAP_DIRECT */
6984

7085
/*
@@ -146,17 +161,12 @@ int dma_iommu_dma_supported(struct device *dev, u64 mask)
146161

147162
if (dev_is_pci(dev) && dma_iommu_bypass_supported(dev, mask)) {
148163
/*
149-
* dma_iommu_bypass_supported() sets dma_max when there is
150-
* 1:1 mapping but it is somehow limited.
151-
* ibm,pmemory is one example.
164+
* fixed ops will be used for RAM. This is limited by
165+
* bus_dma_limit which is set when RAM is pre-mapped.
152166
*/
153-
dev->dma_ops_bypass = dev->bus_dma_limit == 0;
154-
if (!dev->dma_ops_bypass)
155-
dev_warn(dev,
156-
"iommu: 64-bit OK but direct DMA is limited by %llx\n",
157-
dev->bus_dma_limit);
158-
else
159-
dev_dbg(dev, "iommu: 64-bit OK, using fixed ops\n");
167+
dev->dma_ops_bypass = true;
168+
dev_info(dev, "iommu: 64-bit OK but direct DMA is limited by %llx\n",
169+
dev->bus_dma_limit);
160170
return 1;
161171
}
162172

arch/powerpc/platforms/pseries/iommu.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1769,10 +1769,8 @@ static bool enable_ddw(struct pci_dev *dev, struct device_node *pdn, u64 dma_mas
17691769
out_unlock:
17701770
mutex_unlock(&dma_win_init_mutex);
17711771

1772-
/* If we have persistent memory and the window size is not big enough
1773-
* to directly map both RAM and vPMEM, then we need to set DMA limit.
1774-
*/
1775-
if (pmem_present && direct_mapping && len != MAX_PHYSMEM_BITS)
1772+
/* For pre-mapped memory, set bus_dma_limit to the max RAM */
1773+
if (direct_mapping)
17761774
dev->dev.bus_dma_limit = dev->dev.archdata.dma_offset +
17771775
(1ULL << max_ram_len);
17781776

include/linux/dma-map-ops.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,11 +395,15 @@ bool arch_dma_map_sg_direct(struct device *dev, struct scatterlist *sg,
395395
int nents);
396396
bool arch_dma_unmap_sg_direct(struct device *dev, struct scatterlist *sg,
397397
int nents);
398+
bool arch_dma_alloc_direct(struct device *dev);
399+
bool arch_dma_free_direct(struct device *dev, dma_addr_t dma_handle);
398400
#else
399401
#define arch_dma_map_phys_direct(d, a) (false)
400402
#define arch_dma_unmap_phys_direct(d, a) (false)
401403
#define arch_dma_map_sg_direct(d, s, n) (false)
402404
#define arch_dma_unmap_sg_direct(d, s, n) (false)
405+
#define arch_dma_alloc_direct(d) (false)
406+
#define arch_dma_free_direct(d, a) (false)
403407
#endif
404408

405409
#ifdef CONFIG_ARCH_HAS_SETUP_DMA_OPS

kernel/dma/mapping.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ void *dma_alloc_attrs(struct device *dev, size_t size, dma_addr_t *dma_handle,
638638
/* let the implementation decide on the zone to allocate from: */
639639
flag &= ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM);
640640

641-
if (dma_alloc_direct(dev, ops)) {
641+
if (dma_alloc_direct(dev, ops) || arch_dma_alloc_direct(dev)) {
642642
cpu_addr = dma_direct_alloc(dev, size, dma_handle, flag, attrs);
643643
} else if (use_dma_iommu(dev)) {
644644
cpu_addr = iommu_dma_alloc(dev, size, dma_handle, flag, attrs);
@@ -679,7 +679,7 @@ void dma_free_attrs(struct device *dev, size_t size, void *cpu_addr,
679679
return;
680680

681681
debug_dma_free_coherent(dev, size, cpu_addr, dma_handle);
682-
if (dma_alloc_direct(dev, ops))
682+
if (dma_alloc_direct(dev, ops) || arch_dma_free_direct(dev, dma_handle))
683683
dma_direct_free(dev, size, cpu_addr, dma_handle, attrs);
684684
else if (use_dma_iommu(dev))
685685
iommu_dma_free(dev, size, cpu_addr, dma_handle, attrs);

0 commit comments

Comments
 (0)