Skip to content

Commit a7ba70f

Browse files
Nicolas Saenz JulienneChristoph Hellwig
authored andcommitted
dma-mapping: treat dev->bus_dma_mask as a DMA limit
Using a mask to represent bus DMA constraints has a set of limitations. The biggest one being it can only hold a power of two (minus one). The DMA mapping code is already aware of this and treats dev->bus_dma_mask as a limit. This quirk is already used by some architectures although still rare. With the introduction of the Raspberry Pi 4 we've found a new contender for the use of bus DMA limits, as its PCIe bus can only address the lower 3GB of memory (of a total of 4GB). This is impossible to represent with a mask. To make things worse the device-tree code rounds non power of two bus DMA limits to the next power of two, which is unacceptable in this case. In the light of this, rename dev->bus_dma_mask to dev->bus_dma_limit all over the tree and treat it as such. Note that dev->bus_dma_limit should contain the higher accessible DMA address. Signed-off-by: Nicolas Saenz Julienne <nsaenzjulienne@suse.de> Reviewed-by: Robin Murphy <robin.murphy@arm.com> Signed-off-by: Christoph Hellwig <hch@lst.de>
1 parent d7293f7 commit a7ba70f

13 files changed

Lines changed: 46 additions & 53 deletions

File tree

‎arch/mips/pci/fixup-sb1250.c‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,22 @@ DECLARE_PCI_FIXUP_EARLY(PCI_VENDOR_ID_SIBYTE, PCI_DEVICE_ID_BCM1250_PCI,
2121

2222
/*
2323
* The BCM1250, etc. PCI host bridge does not support DAC on its 32-bit
24-
* bus, so we set the bus's DMA mask accordingly. However the HT link
24+
* bus, so we set the bus's DMA limit accordingly. However the HT link
2525
* down the artificial PCI-HT bridge supports 40-bit addressing and the
2626
* SP1011 HT-PCI bridge downstream supports both DAC and a 64-bit bus
2727
* width, so we record the PCI-HT bridge's secondary and subordinate bus
28-
* numbers and do not set the mask for devices present in the inclusive
28+
* numbers and do not set the limit for devices present in the inclusive
2929
* range of those.
3030
*/
31-
struct sb1250_bus_dma_mask_exclude {
31+
struct sb1250_bus_dma_limit_exclude {
3232
bool set;
3333
unsigned char start;
3434
unsigned char end;
3535
};
3636

37-
static int sb1250_bus_dma_mask(struct pci_dev *dev, void *data)
37+
static int sb1250_bus_dma_limit(struct pci_dev *dev, void *data)
3838
{
39-
struct sb1250_bus_dma_mask_exclude *exclude = data;
39+
struct sb1250_bus_dma_limit_exclude *exclude = data;
4040
bool exclude_this;
4141
bool ht_bridge;
4242

@@ -55,17 +55,17 @@ static int sb1250_bus_dma_mask(struct pci_dev *dev, void *data)
5555
exclude->start, exclude->end);
5656
} else {
5757
dev_dbg(&dev->dev, "disabling DAC for device");
58-
dev->dev.bus_dma_mask = DMA_BIT_MASK(32);
58+
dev->dev.bus_dma_limit = DMA_BIT_MASK(32);
5959
}
6060

6161
return 0;
6262
}
6363

6464
static void quirk_sb1250_pci_dac(struct pci_dev *dev)
6565
{
66-
struct sb1250_bus_dma_mask_exclude exclude = { .set = false };
66+
struct sb1250_bus_dma_limit_exclude exclude = { .set = false };
6767

68-
pci_walk_bus(dev->bus, sb1250_bus_dma_mask, &exclude);
68+
pci_walk_bus(dev->bus, sb1250_bus_dma_limit, &exclude);
6969
}
7070
DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_SIBYTE, PCI_DEVICE_ID_BCM1250_PCI,
7171
quirk_sb1250_pci_dac);

‎arch/powerpc/sysdev/fsl_pci.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ static void pci_dma_dev_setup_swiotlb(struct pci_dev *pdev)
115115
{
116116
struct pci_controller *hose = pci_bus_to_host(pdev->bus);
117117

118-
pdev->dev.bus_dma_mask =
119-
hose->dma_window_base_cur + hose->dma_window_size;
118+
pdev->dev.bus_dma_limit =
119+
hose->dma_window_base_cur + hose->dma_window_size - 1;
120120
}
121121

122122
static void setup_swiotlb_ops(struct pci_controller *hose)
@@ -135,7 +135,7 @@ static void fsl_pci_dma_set_mask(struct device *dev, u64 dma_mask)
135135
* mapping that allows addressing any RAM address from across PCI.
136136
*/
137137
if (dev_is_pci(dev) && dma_mask >= pci64_dma_offset * 2 - 1) {
138-
dev->bus_dma_mask = 0;
138+
dev->bus_dma_limit = 0;
139139
dev->archdata.dma_offset = pci64_dma_offset;
140140
}
141141
}

‎arch/x86/kernel/pci-dma.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ rootfs_initcall(pci_iommu_init);
146146

147147
static int via_no_dac_cb(struct pci_dev *pdev, void *data)
148148
{
149-
pdev->dev.bus_dma_mask = DMA_BIT_MASK(32);
149+
pdev->dev.bus_dma_limit = DMA_BIT_MASK(32);
150150
return 0;
151151
}
152152

‎arch/x86/mm/mem_encrypt.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ bool force_dma_unencrypted(struct device *dev)
367367
if (sme_active()) {
368368
u64 dma_enc_mask = DMA_BIT_MASK(__ffs64(sme_me_mask));
369369
u64 dma_dev_mask = min_not_zero(dev->coherent_dma_mask,
370-
dev->bus_dma_mask);
370+
dev->bus_dma_limit);
371371

372372
if (dma_dev_mask <= dma_enc_mask)
373373
return true;

‎arch/x86/pci/sta2x11-fixup.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ static void sta2x11_map_ep(struct pci_dev *pdev)
143143

144144
dev->dma_pfn_offset = PFN_DOWN(-amba_base);
145145

146-
dev->bus_dma_mask = max_amba_addr;
146+
dev->bus_dma_limit = max_amba_addr;
147147
pci_set_consistent_dma_mask(pdev, max_amba_addr);
148148
pci_set_dma_mask(pdev, max_amba_addr);
149149

‎drivers/acpi/arm64/iort.c‎

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,8 +1057,8 @@ static int rc_dma_get_range(struct device *dev, u64 *size)
10571057
*/
10581058
void iort_dma_setup(struct device *dev, u64 *dma_addr, u64 *dma_size)
10591059
{
1060-
u64 mask, dmaaddr = 0, size = 0, offset = 0;
1061-
int ret, msb;
1060+
u64 end, mask, dmaaddr = 0, size = 0, offset = 0;
1061+
int ret;
10621062

10631063
/*
10641064
* If @dev is expected to be DMA-capable then the bus code that created
@@ -1085,19 +1085,13 @@ void iort_dma_setup(struct device *dev, u64 *dma_addr, u64 *dma_size)
10851085
}
10861086

10871087
if (!ret) {
1088-
msb = fls64(dmaaddr + size - 1);
10891088
/*
1090-
* Round-up to the power-of-two mask or set
1091-
* the mask to the whole 64-bit address space
1092-
* in case the DMA region covers the full
1093-
* memory window.
1089+
* Limit coherent and dma mask based on size retrieved from
1090+
* firmware.
10941091
*/
1095-
mask = msb == 64 ? U64_MAX : (1ULL << msb) - 1;
1096-
/*
1097-
* Limit coherent and dma mask based on size
1098-
* retrieved from firmware.
1099-
*/
1100-
dev->bus_dma_mask = mask;
1092+
end = dmaaddr + size - 1;
1093+
mask = DMA_BIT_MASK(ilog2(end) + 1);
1094+
dev->bus_dma_limit = end;
11011095
dev->coherent_dma_mask = mask;
11021096
*dev->dma_mask = mask;
11031097
}

‎drivers/ata/ahci.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -897,7 +897,7 @@ static int ahci_configure_dma_masks(struct pci_dev *pdev, int using_dac)
897897
* value, don't extend it here. This happens on STA2X11, for example.
898898
*
899899
* XXX: manipulating the DMA mask from platform code is completely
900-
* bogus, platform code should use dev->bus_dma_mask instead..
900+
* bogus, platform code should use dev->bus_dma_limit instead..
901901
*/
902902
if (pdev->dma_mask && pdev->dma_mask < DMA_BIT_MASK(32))
903903
return 0;

‎drivers/iommu/dma-iommu.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,8 +405,7 @@ static dma_addr_t iommu_dma_alloc_iova(struct iommu_domain *domain,
405405
if (iova_len < (1 << (IOVA_RANGE_CACHE_MAX_SIZE - 1)))
406406
iova_len = roundup_pow_of_two(iova_len);
407407

408-
if (dev->bus_dma_mask)
409-
dma_limit &= dev->bus_dma_mask;
408+
dma_limit = min_not_zero(dma_limit, dev->bus_dma_limit);
410409

411410
if (domain->geometry.force_aperture)
412411
dma_limit = min(dma_limit, domain->geometry.aperture_end);

‎drivers/of/device.c‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ int of_dma_configure(struct device *dev, struct device_node *np, bool force_dma)
9393
bool coherent;
9494
unsigned long offset;
9595
const struct iommu_ops *iommu;
96-
u64 mask;
96+
u64 mask, end;
9797

9898
ret = of_dma_get_range(np, &dma_addr, &paddr, &size);
9999
if (ret < 0) {
@@ -148,12 +148,13 @@ int of_dma_configure(struct device *dev, struct device_node *np, bool force_dma)
148148
* Limit coherent and dma mask based on size and default mask
149149
* set by the driver.
150150
*/
151-
mask = DMA_BIT_MASK(ilog2(dma_addr + size - 1) + 1);
151+
end = dma_addr + size - 1;
152+
mask = DMA_BIT_MASK(ilog2(end) + 1);
152153
dev->coherent_dma_mask &= mask;
153154
*dev->dma_mask &= mask;
154-
/* ...but only set bus mask if we found valid dma-ranges earlier */
155+
/* ...but only set bus limit if we found valid dma-ranges earlier */
155156
if (!ret)
156-
dev->bus_dma_mask = mask;
157+
dev->bus_dma_limit = end;
157158

158159
coherent = of_dma_is_coherent(np);
159160
dev_dbg(dev, "device is%sdma coherent\n",

‎include/linux/device.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,8 +1186,8 @@ struct dev_links_info {
11861186
* @coherent_dma_mask: Like dma_mask, but for alloc_coherent mapping as not all
11871187
* hardware supports 64-bit addresses for consistent allocations
11881188
* such descriptors.
1189-
* @bus_dma_mask: Mask of an upstream bridge or bus which imposes a smaller DMA
1190-
* limit than the device itself supports.
1189+
* @bus_dma_limit: Limit of an upstream bridge or bus which imposes a smaller
1190+
* DMA limit than the device itself supports.
11911191
* @dma_pfn_offset: offset of DMA memory range relatively of RAM
11921192
* @dma_parms: A low level driver may set these to teach IOMMU code about
11931193
* segment limitations.
@@ -1270,7 +1270,7 @@ struct device {
12701270
not all hardware supports
12711271
64 bit addresses for consistent
12721272
allocations such descriptors. */
1273-
u64 bus_dma_mask; /* upstream dma_mask constraint */
1273+
u64 bus_dma_limit; /* upstream dma constraint */
12741274
unsigned long dma_pfn_offset;
12751275

12761276
struct device_dma_parameters *dma_parms;

0 commit comments

Comments
 (0)