Skip to content

Commit bf2ee8d

Browse files
lvjianmin-loongsonrafaeljw
authored andcommitted
ACPI: scan: Support multiple DMA windows with different offsets
In DT systems configurations, of_dma_get_range() returns struct bus_dma_region DMA regions; they are used to set-up devices DMA windows with different offset available for translation between DMA address and CPU address. In ACPI systems configuration, acpi_dma_get_range() does not return DMA regions yet and that precludes setting up the dev->dma_range_map pointer and therefore DMA regions with multiple offsets. Update acpi_dma_get_range() to return struct bus_dma_region DMA regions like of_dma_get_range() does. After updating acpi_dma_get_range(), acpi_arch_dma_setup() is changed for ARM64, where the original dma_addr and size are removed as these arguments are now redundant, and pass 0 and U64_MAX for dma_base and size of arch_setup_dma_ops; this is a simplification consistent with what other ACPI architectures also pass to iommu_setup_dma_ops(). Reviewed-by: Robin Murphy <[email protected]> Signed-off-by: Jianmin Lv <[email protected]> Reviewed-by: Lorenzo Pieralisi <[email protected]> Signed-off-by: Rafael J. Wysocki <[email protected]>
1 parent 521a547 commit bf2ee8d

File tree

4 files changed

+44
-47
lines changed

4 files changed

+44
-47
lines changed

drivers/acpi/arm64/dma.c

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
#include <linux/device.h>
55
#include <linux/dma-direct.h>
66

7-
void acpi_arch_dma_setup(struct device *dev, u64 *dma_addr, u64 *dma_size)
7+
void acpi_arch_dma_setup(struct device *dev)
88
{
99
int ret;
1010
u64 end, mask;
11-
u64 dmaaddr = 0, size = 0, offset = 0;
11+
u64 size = 0;
12+
const struct bus_dma_region *map = NULL;
1213

1314
/*
1415
* If @dev is expected to be DMA-capable then the bus code that created
@@ -26,25 +27,30 @@ void acpi_arch_dma_setup(struct device *dev, u64 *dma_addr, u64 *dma_size)
2627
else
2728
size = 1ULL << 32;
2829

29-
ret = acpi_dma_get_range(dev, &dmaaddr, &offset, &size);
30+
ret = acpi_dma_get_range(dev, &map);
31+
if (!ret && map) {
32+
const struct bus_dma_region *r = map;
33+
34+
for (end = 0; r->size; r++) {
35+
if (r->dma_start + r->size - 1 > end)
36+
end = r->dma_start + r->size - 1;
37+
}
38+
39+
size = end + 1;
40+
dev->dma_range_map = map;
41+
}
42+
3043
if (ret == -ENODEV)
3144
ret = iort_dma_get_ranges(dev, &size);
3245
if (!ret) {
3346
/*
3447
* Limit coherent and dma mask based on size retrieved from
3548
* firmware.
3649
*/
37-
end = dmaaddr + size - 1;
50+
end = size - 1;
3851
mask = DMA_BIT_MASK(ilog2(end) + 1);
3952
dev->bus_dma_limit = end;
4053
dev->coherent_dma_mask = min(dev->coherent_dma_mask, mask);
4154
*dev->dma_mask = min(*dev->dma_mask, mask);
4255
}
43-
44-
*dma_addr = dmaaddr;
45-
*dma_size = size;
46-
47-
ret = dma_direct_set_offset(dev, dmaaddr + offset, dmaaddr, size);
48-
49-
dev_dbg(dev, "dma_offset(%#08llx)%s\n", offset, ret ? " failed!" : "");
5056
}

drivers/acpi/scan.c

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <linux/platform_data/x86/apple.h>
2121
#include <linux/pgtable.h>
2222
#include <linux/crc32.h>
23+
#include <linux/dma-direct.h>
2324

2425
#include "internal.h"
2526

@@ -1467,25 +1468,21 @@ enum dev_dma_attr acpi_get_dma_attr(struct acpi_device *adev)
14671468
* acpi_dma_get_range() - Get device DMA parameters.
14681469
*
14691470
* @dev: device to configure
1470-
* @dma_addr: pointer device DMA address result
1471-
* @offset: pointer to the DMA offset result
1472-
* @size: pointer to DMA range size result
1471+
* @map: pointer to DMA ranges result
14731472
*
1474-
* Evaluate DMA regions and return respectively DMA region start, offset
1475-
* and size in dma_addr, offset and size on parsing success; it does not
1476-
* update the passed in values on failure.
1473+
* Evaluate DMA regions and return pointer to DMA regions on
1474+
* parsing success; it does not update the passed in values on failure.
14771475
*
14781476
* Return 0 on success, < 0 on failure.
14791477
*/
1480-
int acpi_dma_get_range(struct device *dev, u64 *dma_addr, u64 *offset,
1481-
u64 *size)
1478+
int acpi_dma_get_range(struct device *dev, const struct bus_dma_region **map)
14821479
{
14831480
struct acpi_device *adev;
14841481
LIST_HEAD(list);
14851482
struct resource_entry *rentry;
14861483
int ret;
14871484
struct device *dma_dev = dev;
1488-
u64 len, dma_start = U64_MAX, dma_end = 0, dma_offset = 0;
1485+
struct bus_dma_region *r;
14891486

14901487
/*
14911488
* Walk the device tree chasing an ACPI companion with a _DMA
@@ -1510,31 +1507,28 @@ int acpi_dma_get_range(struct device *dev, u64 *dma_addr, u64 *offset,
15101507

15111508
ret = acpi_dev_get_dma_resources(adev, &list);
15121509
if (ret > 0) {
1510+
r = kcalloc(ret + 1, sizeof(*r), GFP_KERNEL);
1511+
if (!r) {
1512+
ret = -ENOMEM;
1513+
goto out;
1514+
}
1515+
15131516
list_for_each_entry(rentry, &list, node) {
1514-
if (dma_offset && rentry->offset != dma_offset) {
1517+
if (rentry->res->start >= rentry->res->end) {
1518+
kfree(r);
15151519
ret = -EINVAL;
1516-
dev_warn(dma_dev, "Can't handle multiple windows with different offsets\n");
1520+
dev_dbg(dma_dev, "Invalid DMA regions configuration\n");
15171521
goto out;
15181522
}
1519-
dma_offset = rentry->offset;
15201523

1521-
/* Take lower and upper limits */
1522-
if (rentry->res->start < dma_start)
1523-
dma_start = rentry->res->start;
1524-
if (rentry->res->end > dma_end)
1525-
dma_end = rentry->res->end;
1526-
}
1527-
1528-
if (dma_start >= dma_end) {
1529-
ret = -EINVAL;
1530-
dev_dbg(dma_dev, "Invalid DMA regions configuration\n");
1531-
goto out;
1524+
r->cpu_start = rentry->res->start;
1525+
r->dma_start = rentry->res->start - rentry->offset;
1526+
r->size = resource_size(rentry->res);
1527+
r->offset = rentry->offset;
1528+
r++;
15321529
}
15331530

1534-
*dma_addr = dma_start - dma_offset;
1535-
len = dma_end - dma_start;
1536-
*size = max(len, len + 1);
1537-
*offset = dma_offset;
1531+
*map = r;
15381532
}
15391533
out:
15401534
acpi_dev_free_resource_list(&list);
@@ -1624,20 +1618,19 @@ int acpi_dma_configure_id(struct device *dev, enum dev_dma_attr attr,
16241618
const u32 *input_id)
16251619
{
16261620
const struct iommu_ops *iommu;
1627-
u64 dma_addr = 0, size = 0;
16281621

16291622
if (attr == DEV_DMA_NOT_SUPPORTED) {
16301623
set_dma_ops(dev, &dma_dummy_ops);
16311624
return 0;
16321625
}
16331626

1634-
acpi_arch_dma_setup(dev, &dma_addr, &size);
1627+
acpi_arch_dma_setup(dev);
16351628

16361629
iommu = acpi_iommu_configure_id(dev, input_id);
16371630
if (PTR_ERR(iommu) == -EPROBE_DEFER)
16381631
return -EPROBE_DEFER;
16391632

1640-
arch_setup_dma_ops(dev, dma_addr, size,
1633+
arch_setup_dma_ops(dev, 0, U64_MAX,
16411634
iommu, attr == DEV_DMA_COHERENT);
16421635

16431636
return 0;

include/acpi/acpi_bus.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -613,8 +613,7 @@ enum dev_dma_attr acpi_get_dma_attr(struct acpi_device *adev);
613613
int acpi_iommu_fwspec_init(struct device *dev, u32 id,
614614
struct fwnode_handle *fwnode,
615615
const struct iommu_ops *ops);
616-
int acpi_dma_get_range(struct device *dev, u64 *dma_addr, u64 *offset,
617-
u64 *size);
616+
int acpi_dma_get_range(struct device *dev, const struct bus_dma_region **map);
618617
int acpi_dma_configure_id(struct device *dev, enum dev_dma_attr attr,
619618
const u32 *input_id);
620619
static inline int acpi_dma_configure(struct device *dev,

include/linux/acpi.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,12 +281,12 @@ void acpi_numa_x2apic_affinity_init(struct acpi_srat_x2apic_cpu_affinity *pa);
281281

282282
#ifdef CONFIG_ARM64
283283
void acpi_numa_gicc_affinity_init(struct acpi_srat_gicc_affinity *pa);
284-
void acpi_arch_dma_setup(struct device *dev, u64 *dma_addr, u64 *dma_size);
284+
void acpi_arch_dma_setup(struct device *dev);
285285
#else
286286
static inline void
287287
acpi_numa_gicc_affinity_init(struct acpi_srat_gicc_affinity *pa) { }
288288
static inline void
289-
acpi_arch_dma_setup(struct device *dev, u64 *dma_addr, u64 *dma_size) { }
289+
acpi_arch_dma_setup(struct device *dev) { }
290290
#endif
291291

292292
int acpi_numa_memory_affinity_init (struct acpi_srat_mem_affinity *ma);
@@ -977,8 +977,7 @@ static inline enum dev_dma_attr acpi_get_dma_attr(struct acpi_device *adev)
977977
return DEV_DMA_NOT_SUPPORTED;
978978
}
979979

980-
static inline int acpi_dma_get_range(struct device *dev, u64 *dma_addr,
981-
u64 *offset, u64 *size)
980+
static inline int acpi_dma_get_range(struct device *dev, const struct bus_dma_region **map)
982981
{
983982
return -ENODEV;
984983
}

0 commit comments

Comments
 (0)