Release 4.14 arch/powerpc/kernel/dma.c
/*
* Copyright (C) 2006 Benjamin Herrenschmidt, IBM Corporation
*
* Provide default implementations of the DMA mapping callbacks for
* directly mapped busses.
*/
#include <linux/device.h>
#include <linux/dma-mapping.h>
#include <linux/dma-debug.h>
#include <linux/gfp.h>
#include <linux/memblock.h>
#include <linux/export.h>
#include <linux/pci.h>
#include <asm/vio.h>
#include <asm/bug.h>
#include <asm/machdep.h>
#include <asm/swiotlb.h>
#include <asm/iommu.h>
/*
* Generic direct DMA implementation
*
* This implementation supports a per-device offset that can be applied if
* the address at which memory is visible to devices is not 0. Platform code
* can set archdata.dma_data to an unsigned long holding the offset. By
* default the offset is PCI_DRAM_OFFSET.
*/
static u64 __maybe_unused get_pfn_limit(struct device *dev)
{
u64 pfn = (dev->coherent_dma_mask >> PAGE_SHIFT) + 1;
struct dev_archdata __maybe_unused *sd = &dev->archdata;
#ifdef CONFIG_SWIOTLB
if (sd->max_direct_dma_addr && dev->dma_ops == &swiotlb_dma_ops)
pfn = min_t(u64, pfn, sd->max_direct_dma_addr >> PAGE_SHIFT);
#endif
return pfn;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Scott Wood | 71 | 98.61% | 1 | 50.00% |
Bart Van Assche | 1 | 1.39% | 1 | 50.00% |
Total | 72 | 100.00% | 2 | 100.00% |
static int dma_direct_dma_supported(struct device *dev, u64 mask)
{
#ifdef CONFIG_PPC64
u64 limit = get_dma_offset(dev) + (memblock_end_of_DRAM() - 1);
/* Limit fits in the mask, we are good */
if (mask >= limit)
return 1;
#ifdef CONFIG_FSL_SOC
/* Freescale gets another chance via ZONE_DMA/ZONE_DMA32, however
* that will have to be refined if/when they support iommus
*/
return 1;
#endif
/* Sorry ... */
return 0;
#else
return 1;
#endif
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Benjamin Herrenschmidt | 62 | 100.00% | 1 | 100.00% |
Total | 62 | 100.00% | 1 | 100.00% |
void *__dma_direct_alloc_coherent(struct device *dev, size_t size,
dma_addr_t *dma_handle, gfp_t flag,
unsigned long attrs)
{
void *ret;
#ifdef CONFIG_NOT_COHERENT_CACHE
ret = __dma_alloc_coherent(dev, size, dma_handle, flag);
if (ret == NULL)
return NULL;
*dma_handle += get_dma_offset(dev);
return ret;
#else
struct page *page;
int node = dev_to_node(dev);
#ifdef CONFIG_FSL_SOC
u64 pfn = get_pfn_limit(dev);
int zone;
/*
* This code should be OK on other platforms, but we have drivers that
* don't set coherent_dma_mask. As a workaround we just ifdef it. This
* whole routine needs some serious cleanup.
*/
zone = dma_pfn_limit_to_zone(pfn);
if (zone < 0) {
dev_err(dev, "%s: No suitable zone for pfn %#llx\n",
__func__, pfn);
return NULL;
}
switch (zone) {
case ZONE_DMA:
flag |= GFP_DMA;
break;
#ifdef CONFIG_ZONE_DMA32
case ZONE_DMA32:
flag |= GFP_DMA32;
break;
#endif
};
#endif /* CONFIG_FSL_SOC */
/* ignore region specifiers */
flag &= ~(__GFP_HIGHMEM);
page = alloc_pages_node(node, flag, get_order(size));
if (page == NULL)
return NULL;
ret = page_address(page);
memset(ret, 0, size);
*dma_handle = __pa(ret) + get_dma_offset(dev);
return ret;
#endif
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Benjamin Herrenschmidt | 88 | 40.00% | 5 | 29.41% |
Scott Wood | 68 | 30.91% | 2 | 11.76% |
Becky Bruce | 29 | 13.18% | 3 | 17.65% |
Andrew Morton | 16 | 7.27% | 1 | 5.88% |
Michael Ellerman | 12 | 5.45% | 3 | 17.65% |
Jeremy Kerr | 3 | 1.36% | 1 | 5.88% |
Krzysztof Kozlowski | 2 | 0.91% | 1 | 5.88% |
Andrzej Pietrasiewicz | 2 | 0.91% | 1 | 5.88% |
Total | 220 | 100.00% | 17 | 100.00% |
void __dma_direct_free_coherent(struct device *dev, size_t size,
void *vaddr, dma_addr_t dma_handle,
unsigned long attrs)
{
#ifdef CONFIG_NOT_COHERENT_CACHE
__dma_free_coherent(size, vaddr);
#else
free_pages((unsigned long)vaddr, get_order(size));
#endif
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Benjamin Herrenschmidt | 31 | 59.62% | 2 | 33.33% |
Becky Bruce | 14 | 26.92% | 1 | 16.67% |
Andrew Morton | 3 | 5.77% | 1 | 16.67% |
Andrzej Pietrasiewicz | 2 | 3.85% | 1 | 16.67% |
Krzysztof Kozlowski | 2 | 3.85% | 1 | 16.67% |
Total | 52 | 100.00% | 6 | 100.00% |
static void *dma_direct_alloc_coherent(struct device *dev, size_t size,
dma_addr_t *dma_handle, gfp_t flag,
unsigned long attrs)
{
struct iommu_table *iommu;
/* The coherent mask may be smaller than the real mask, check if
* we can really use the direct ops
*/
if (dma_direct_dma_supported(dev, dev->coherent_dma_mask))
return __dma_direct_alloc_coherent(dev, size, dma_handle,
flag, attrs);
/* Ok we can't ... do we have an iommu ? If not, fail */
iommu = get_iommu_table_base(dev);
if (!iommu)
return NULL;
/* Try to use the iommu */
return iommu_alloc_coherent(dev, iommu, size, dma_handle,
dev->coherent_dma_mask, flag,
dev_to_node(dev));
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Benjamin Herrenschmidt | 95 | 97.94% | 1 | 50.00% |
Krzysztof Kozlowski | 2 | 2.06% | 1 | 50.00% |
Total | 97 | 100.00% | 2 | 100.00% |
static void dma_direct_free_coherent(struct device *dev, size_t size,
void *vaddr, dma_addr_t dma_handle,
unsigned long attrs)
{
struct iommu_table *iommu;
/* See comments in dma_direct_alloc_coherent() */
if (dma_direct_dma_supported(dev, dev->coherent_dma_mask))
return __dma_direct_free_coherent(dev, size, vaddr, dma_handle,
attrs);
/* Maybe we used an iommu ... */
iommu = get_iommu_table_base(dev);
/* If we hit that we should have never allocated in the first
* place so how come we are freeing ?
*/
if (WARN_ON(!iommu))
return;
iommu_free_coherent(iommu, size, vaddr, dma_handle);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Benjamin Herrenschmidt | 83 | 97.65% | 1 | 50.00% |
Krzysztof Kozlowski | 2 | 2.35% | 1 | 50.00% |
Total | 85 | 100.00% | 2 | 100.00% |
int dma_direct_mmap_coherent(struct device *dev, struct vm_area_struct *vma,
void *cpu_addr, dma_addr_t handle, size_t size,
unsigned long attrs)
{
unsigned long pfn;
#ifdef CONFIG_NOT_COHERENT_CACHE
vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
pfn = __dma_get_coherent_pfn((unsigned long)cpu_addr);
#else
pfn = page_to_pfn(virt_to_page(cpu_addr));
#endif
return remap_pfn_range(vma, vma->vm_start,
pfn + vma->vm_pgoff,
vma->vm_end - vma->vm_start,
vma->vm_page_prot);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Marek Szyprowski | 98 | 98.00% | 1 | 50.00% |
Krzysztof Kozlowski | 2 | 2.00% | 1 | 50.00% |
Total | 100 | 100.00% | 2 | 100.00% |
static int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl,
int nents, enum dma_data_direction direction,
unsigned long attrs)
{
struct scatterlist *sg;
int i;
for_each_sg(sgl, sg, nents, i) {
sg->dma_address = sg_phys(sg) + get_dma_offset(dev);
sg->dma_length = sg->length;
if (attrs & DMA_ATTR_SKIP_CPU_SYNC)
continue;
__dma_sync_page(sg_page(sg), sg->offset, sg->length, direction);
}
return nents;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Benjamin Herrenschmidt | 42 | 43.30% | 2 | 16.67% |
Andrew Morton | 17 | 17.53% | 1 | 8.33% |
Jens Axboe | 17 | 17.53% | 2 | 16.67% |
Alexander Duyck | 7 | 7.22% | 1 | 8.33% |
Michael Ellerman | 4 | 4.12% | 1 | 8.33% |
Jeremy Kerr | 3 | 3.09% | 1 | 8.33% |
Stephen Rothwell | 2 | 2.06% | 1 | 8.33% |
Krzysztof Kozlowski | 2 | 2.06% | 1 | 8.33% |
Mark Nelson | 2 | 2.06% | 1 | 8.33% |
Becky Bruce | 1 | 1.03% | 1 | 8.33% |
Total | 97 | 100.00% | 12 | 100.00% |
static void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sg,
int nents, enum dma_data_direction direction,
unsigned long attrs)
{
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Benjamin Herrenschmidt | 19 | 73.08% | 1 | 25.00% |
Andrew Morton | 3 | 11.54% | 1 | 25.00% |
Mark Nelson | 2 | 7.69% | 1 | 25.00% |
Krzysztof Kozlowski | 2 | 7.69% | 1 | 25.00% |
Total | 26 | 100.00% | 4 | 100.00% |
static u64 dma_direct_get_required_mask(struct device *dev)
{
u64 end, mask;
end = memblock_end_of_DRAM() + get_dma_offset(dev);
mask = 1ULL << (fls64(end) - 1);
mask += mask - 1;
return mask;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Milton D. Miller II | 48 | 100.00% | 1 | 100.00% |
Total | 48 | 100.00% | 1 | 100.00% |
static inline dma_addr_t dma_direct_map_page(struct device *dev,
struct page *page,
unsigned long offset,
size_t size,
enum dma_data_direction dir,
unsigned long attrs)
{
BUG_ON(dir == DMA_NONE);
if (!(attrs & DMA_ATTR_SKIP_CPU_SYNC))
__dma_sync_page(page, offset, size, dir);
return page_to_phys(page) + offset + get_dma_offset(dev);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Becky Bruce | 61 | 84.72% | 2 | 50.00% |
Alexander Duyck | 9 | 12.50% | 1 | 25.00% |
Krzysztof Kozlowski | 2 | 2.78% | 1 | 25.00% |
Total | 72 | 100.00% | 4 | 100.00% |
static inline void dma_direct_unmap_page(struct device *dev,
dma_addr_t dma_address,
size_t size,
enum dma_data_direction direction,
unsigned long attrs)
{
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Becky Bruce | 23 | 92.00% | 1 | 50.00% |
Krzysztof Kozlowski | 2 | 8.00% | 1 | 50.00% |
Total | 25 | 100.00% | 2 | 100.00% |
#ifdef CONFIG_NOT_COHERENT_CACHE
static inline void dma_direct_sync_sg(struct device *dev,
struct scatterlist *sgl, int nents,
enum dma_data_direction direction)
{
struct scatterlist *sg;
int i;
for_each_sg(sgl, sg, nents, i)
__dma_sync_page(sg_page(sg), sg->offset, sg->length, direction);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Becky Bruce | 60 | 100.00% | 1 | 100.00% |
Total | 60 | 100.00% | 1 | 100.00% |
static inline void dma_direct_sync_single(struct device *dev,
dma_addr_t dma_handle, size_t size,
enum dma_data_direction direction)
{
__dma_sync(bus_to_virt(dma_handle), size, direction);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Becky Bruce | 33 | 97.06% | 1 | 50.00% |
FUJITA Tomonori | 1 | 2.94% | 1 | 50.00% |
Total | 34 | 100.00% | 2 | 100.00% |
#endif
const struct dma_map_ops dma_direct_ops = {
.alloc = dma_direct_alloc_coherent,
.free = dma_direct_free_coherent,
.mmap = dma_direct_mmap_coherent,
.map_sg = dma_direct_map_sg,
.unmap_sg = dma_direct_unmap_sg,
.dma_supported = dma_direct_dma_supported,
.map_page = dma_direct_map_page,
.unmap_page = dma_direct_unmap_page,
.get_required_mask = dma_direct_get_required_mask,
#ifdef CONFIG_NOT_COHERENT_CACHE
.sync_single_for_cpu = dma_direct_sync_single,
.sync_single_for_device = dma_direct_sync_single,
.sync_sg_for_cpu = dma_direct_sync_sg,
.sync_sg_for_device = dma_direct_sync_sg,
#endif
};
EXPORT_SYMBOL(dma_direct_ops);
int dma_set_coherent_mask(struct device *dev, u64 mask)
{
if (!dma_supported(dev, mask)) {
/*
* We need to special case the direct DMA ops which can
* support a fallback for coherent allocations. There
* is no dma_op->set_coherent_mask() so we have to do
* things the hard way:
*/
if (get_dma_ops(dev) != &dma_direct_ops ||
get_iommu_table_base(dev) == NULL ||
!dma_iommu_dma_supported(dev, mask))
return -EIO;
}
dev->coherent_dma_mask = mask;
return 0;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Benjamin Herrenschmidt | 64 | 100.00% | 1 | 100.00% |
Total | 64 | 100.00% | 1 | 100.00% |
EXPORT_SYMBOL(dma_set_coherent_mask);
#define PREALLOC_DMA_DEBUG_ENTRIES (1 << 16)
int dma_set_mask(struct device *dev, u64 dma_mask)
{
if (ppc_md.dma_set_mask)
return ppc_md.dma_set_mask(dev, dma_mask);
if (dev_is_pci(dev)) {
struct pci_dev *pdev = to_pci_dev(dev);
struct pci_controller *phb = pci_bus_to_host(pdev->bus);
if (phb->controller_ops.dma_set_mask)
return phb->controller_ops.dma_set_mask(pdev, dma_mask);
}
if (!dev->dma_mask || !dma_supported(dev, dma_mask))
return -EIO;
*dev->dma_mask = dma_mask;
return 0;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Daniel Axtens | 51 | 46.79% | 1 | 33.33% |
Benjamin Herrenschmidt | 35 | 32.11% | 1 | 33.33% |
Christoph Hellwig | 23 | 21.10% | 1 | 33.33% |
Total | 109 | 100.00% | 3 | 100.00% |
EXPORT_SYMBOL(dma_set_mask);
u64 __dma_get_required_mask(struct device *dev)
{
const struct dma_map_ops *dma_ops = get_dma_ops(dev);
if (unlikely(dma_ops == NULL))
return 0;
if (dma_ops->get_required_mask)
return dma_ops->get_required_mask(dev);
return DMA_BIT_MASK(8 * sizeof(dma_addr_t));
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Milton D. Miller II | 56 | 96.55% | 2 | 50.00% |
Bart Van Assche | 1 | 1.72% | 1 | 25.00% |
Gavin Shan | 1 | 1.72% | 1 | 25.00% |
Total | 58 | 100.00% | 4 | 100.00% |
u64 dma_get_required_mask(struct device *dev)
{
if (ppc_md.dma_get_required_mask)
return ppc_md.dma_get_required_mask(dev);
if (dev_is_pci(dev)) {
struct pci_dev *pdev = to_pci_dev(dev);
struct pci_controller *phb = pci_bus_to_host(pdev->bus);
if (phb->controller_ops.dma_get_required_mask)
return phb->controller_ops.dma_get_required_mask(pdev);
}
return __dma_get_required_mask(dev);
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Andrew Donnellan | 49 | 62.03% | 1 | 50.00% |
Gavin Shan | 30 | 37.97% | 1 | 50.00% |
Total | 79 | 100.00% | 2 | 100.00% |
EXPORT_SYMBOL_GPL(dma_get_required_mask);
static int __init dma_init(void)
{
dma_debug_init(PREALLOC_DMA_DEBUG_ENTRIES);
#ifdef CONFIG_PCI
dma_debug_add_bus(&pci_bus_type);
#endif
#ifdef CONFIG_IBMVIO
dma_debug_add_bus(&vio_bus_type);
#endif
return 0;
}
Contributors
Person | Tokens | Prop | Commits | CommitProp |
Anton Blanchard | 22 | 56.41% | 1 | 50.00% |
FUJITA Tomonori | 17 | 43.59% | 1 | 50.00% |
Total | 39 | 100.00% | 2 | 100.00% |
fs_initcall(dma_init);
Overall Contributors
Person | Tokens | Prop | Commits | CommitProp |
Benjamin Herrenschmidt | 564 | 36.41% | 10 | 22.22% |
Becky Bruce | 261 | 16.85% | 5 | 11.11% |
Scott Wood | 142 | 9.17% | 2 | 4.44% |
Milton D. Miller II | 114 | 7.36% | 2 | 4.44% |
Marek Szyprowski | 103 | 6.65% | 1 | 2.22% |
Andrew Morton | 53 | 3.42% | 1 | 2.22% |
Daniel Axtens | 51 | 3.29% | 1 | 2.22% |
Andrew Donnellan | 49 | 3.16% | 1 | 2.22% |
FUJITA Tomonori | 35 | 2.26% | 3 | 6.67% |
Gavin Shan | 31 | 2.00% | 1 | 2.22% |
Anton Blanchard | 28 | 1.81% | 1 | 2.22% |
Christoph Hellwig | 23 | 1.48% | 1 | 2.22% |
Krzysztof Kozlowski | 18 | 1.16% | 1 | 2.22% |
Jens Axboe | 17 | 1.10% | 2 | 4.44% |
Michael Ellerman | 16 | 1.03% | 3 | 6.67% |
Alexander Duyck | 16 | 1.03% | 1 | 2.22% |
Jeremy Kerr | 6 | 0.39% | 1 | 2.22% |
Andrzej Pietrasiewicz | 6 | 0.39% | 1 | 2.22% |
Mark Nelson | 4 | 0.26% | 1 | 2.22% |
Bart Van Assche | 3 | 0.19% | 2 | 4.44% |
Tejun Heo | 3 | 0.19% | 1 | 2.22% |
Paul Gortmaker | 3 | 0.19% | 1 | 2.22% |
Stephen Rothwell | 2 | 0.13% | 1 | 2.22% |
Yinghai Lu | 1 | 0.06% | 1 | 2.22% |
Total | 1549 | 100.00% | 45 | 100.00% |
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.