Skip to content

Commit f02ad36

Browse files
lsgunthChristoph Hellwig
authored andcommitted
dma-direct: support PCI P2PDMA pages in dma-direct map_sg
Add PCI P2PDMA support for dma_direct_map_sg() so that it can map PCI P2PDMA pages directly without a hack in the callers. This allows for heterogeneous SGLs that contain both P2PDMA and regular pages. A P2PDMA page may have three possible outcomes when being mapped: 1) If the data path between the two devices doesn't go through the root port, then it should be mapped with a PCI bus address 2) If the data path goes through the host bridge, it should be mapped normally, as though it were a CPU physical address 3) It is not possible for the two devices to communicate and thus the mapping operation should fail (and it will return -EREMOTEIO). SGL segments that contain PCI bus addresses are marked with sg_dma_mark_pci_p2pdma() and are ignored when unmapped. P2PDMA mappings are also failed if swiotlb needs to be used on the mapping. Signed-off-by: Logan Gunthorpe <logang@deltatee.com> Reviewed-by: Christoph Hellwig <hch@lst.de> Signed-off-by: Christoph Hellwig <hch@lst.de>
1 parent 7c2645a commit f02ad36

2 files changed

Lines changed: 44 additions & 7 deletions

File tree

kernel/dma/direct.c

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -454,37 +454,68 @@ void dma_direct_sync_sg_for_cpu(struct device *dev,
454454
arch_sync_dma_for_cpu_all();
455455
}
456456

457+
/*
458+
* Unmaps segments, except for ones marked as pci_p2pdma which do not
459+
* require any further action as they contain a bus address.
460+
*/
457461
void dma_direct_unmap_sg(struct device *dev, struct scatterlist *sgl,
458462
int nents, enum dma_data_direction dir, unsigned long attrs)
459463
{
460464
struct scatterlist *sg;
461465
int i;
462466

463-
for_each_sg(sgl, sg, nents, i)
464-
dma_direct_unmap_page(dev, sg->dma_address, sg_dma_len(sg), dir,
465-
attrs);
467+
for_each_sg(sgl, sg, nents, i) {
468+
if (sg_is_dma_bus_address(sg))
469+
sg_dma_unmark_bus_address(sg);
470+
else
471+
dma_direct_unmap_page(dev, sg->dma_address,
472+
sg_dma_len(sg), dir, attrs);
473+
}
466474
}
467475
#endif
468476

469477
int dma_direct_map_sg(struct device *dev, struct scatterlist *sgl, int nents,
470478
enum dma_data_direction dir, unsigned long attrs)
471479
{
472-
int i;
480+
struct pci_p2pdma_map_state p2pdma_state = {};
481+
enum pci_p2pdma_map_type map;
473482
struct scatterlist *sg;
483+
int i, ret;
474484

475485
for_each_sg(sgl, sg, nents, i) {
486+
if (is_pci_p2pdma_page(sg_page(sg))) {
487+
map = pci_p2pdma_map_segment(&p2pdma_state, dev, sg);
488+
switch (map) {
489+
case PCI_P2PDMA_MAP_BUS_ADDR:
490+
continue;
491+
case PCI_P2PDMA_MAP_THRU_HOST_BRIDGE:
492+
/*
493+
* Any P2P mapping that traverses the PCI
494+
* host bridge must be mapped with CPU physical
495+
* address and not PCI bus addresses. This is
496+
* done with dma_direct_map_page() below.
497+
*/
498+
break;
499+
default:
500+
ret = -EREMOTEIO;
501+
goto out_unmap;
502+
}
503+
}
504+
476505
sg->dma_address = dma_direct_map_page(dev, sg_page(sg),
477506
sg->offset, sg->length, dir, attrs);
478-
if (sg->dma_address == DMA_MAPPING_ERROR)
507+
if (sg->dma_address == DMA_MAPPING_ERROR) {
508+
ret = -EIO;
479509
goto out_unmap;
510+
}
480511
sg_dma_len(sg) = sg->length;
481512
}
482513

483514
return nents;
484515

485516
out_unmap:
486517
dma_direct_unmap_sg(dev, sgl, i, dir, attrs | DMA_ATTR_SKIP_CPU_SYNC);
487-
return -EIO;
518+
return ret;
488519
}
489520

490521
dma_addr_t dma_direct_map_resource(struct device *dev, phys_addr_t paddr,

kernel/dma/direct.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#define _KERNEL_DMA_DIRECT_H
99

1010
#include <linux/dma-direct.h>
11+
#include <linux/memremap.h>
1112

1213
int dma_direct_get_sgtable(struct device *dev, struct sg_table *sgt,
1314
void *cpu_addr, dma_addr_t dma_addr, size_t size,
@@ -87,10 +88,15 @@ static inline dma_addr_t dma_direct_map_page(struct device *dev,
8788
phys_addr_t phys = page_to_phys(page) + offset;
8889
dma_addr_t dma_addr = phys_to_dma(dev, phys);
8990

90-
if (is_swiotlb_force_bounce(dev))
91+
if (is_swiotlb_force_bounce(dev)) {
92+
if (is_pci_p2pdma_page(page))
93+
return DMA_MAPPING_ERROR;
9194
return swiotlb_map(dev, phys, size, dir, attrs);
95+
}
9296

9397
if (unlikely(!dma_capable(dev, dma_addr, size, true))) {
98+
if (is_pci_p2pdma_page(page))
99+
return DMA_MAPPING_ERROR;
94100
if (is_swiotlb_active(dev))
95101
return swiotlb_map(dev, phys, size, dir, attrs);
96102

0 commit comments

Comments
 (0)