dm: Respect dma-ranges size

Rework dev_phys_to_bus() and dev_bus_to_phys() to respect the size
of the area specified in dma-ranges DT property. The area outside
of ranges is remapped 1:1, while the area in the ranges is remapped
according to the description in the dma-ranges property.

Adjust the test to test the area within the remapped range, not area
outside the remapped range, which was incorrect.

Signed-off-by: Marek Vasut <marek.vasut+renesas@mailbox.org>
Reviewed-by: Markus Schneider-Pargmann <msp@baylibre.com>
This commit is contained in:
Marek Vasut
2026-03-16 00:57:23 +01:00
committed by Tom Rini
parent 6201fd7c1f
commit a383c96892
5 changed files with 25 additions and 20 deletions

View File

@@ -473,7 +473,11 @@ static int device_get_dma_constraints(struct udevice *dev)
return ret;
}
dev_set_dma_offset(dev, cpu - bus);
#if CONFIG_IS_ENABLED(DM_DMA)
dev->dma_cpu = cpu;
dev->dma_bus = bus;
dev->dma_size = size;
#endif
return 0;
}

View File

@@ -166,8 +166,9 @@ enum {
* When CONFIG_DEVRES is enabled, devm_kmalloc() and friends will
* add to this list. Memory so-allocated will be freed
* automatically when the device is removed / unbound
* @dma_offset: Offset between the physical address space (CPU's) and the
* device's bus address space
* @dma_cpu: DMA physical address space (CPU's)
* @dma_bus: DMA device's bus address space
* @dma_size: DMA window size
* @iommu: IOMMU device associated with this device
*/
struct udevice {
@@ -196,7 +197,9 @@ struct udevice {
struct list_head devres_head;
#endif
#if CONFIG_IS_ENABLED(DM_DMA)
ulong dma_offset;
phys_addr_t dma_cpu;
dma_addr_t dma_bus;
u64 dma_size;
#endif
#if CONFIG_IS_ENABLED(IOMMU)
struct udevice *iommu;
@@ -272,14 +275,6 @@ static inline __attribute_const__ ofnode dev_ofnode(const struct udevice *dev)
/* Returns non-zero if the device is active (probed and not removed) */
#define device_active(dev) (dev_get_flags(dev) & DM_FLAG_ACTIVATED)
#if CONFIG_IS_ENABLED(DM_DMA)
#define dev_set_dma_offset(_dev, _offset) _dev->dma_offset = _offset
#define dev_get_dma_offset(_dev) _dev->dma_offset
#else
#define dev_set_dma_offset(_dev, _offset)
#define dev_get_dma_offset(_dev) 0
#endif
static inline __attribute_const__ int dev_of_offset(const struct udevice *dev)
{
#if CONFIG_IS_ENABLED(OF_REAL)

View File

@@ -21,17 +21,23 @@ static inline unsigned long bus_to_phys(unsigned long bus)
}
#endif
#if CONFIG_IS_ENABLED(DM)
#if CONFIG_IS_ENABLED(DM_DMA)
#include <dm/device.h>
static inline dma_addr_t dev_phys_to_bus(struct udevice *dev, phys_addr_t phys)
{
return phys - dev_get_dma_offset(dev);
/* If the PA is in the remap range, apply remap. */
if (phys >= dev->dma_cpu && phys < dev->dma_cpu + dev->dma_size)
phys -= dev->dma_cpu - dev->dma_bus;
return phys;
}
static inline phys_addr_t dev_bus_to_phys(struct udevice *dev, dma_addr_t bus)
{
return bus + dev_get_dma_offset(dev);
/* If the DA is in the remap range, apply remap. */
if (bus >= dev->dma_bus && bus < dev->dma_bus + dev->dma_size)
bus += dev->dma_cpu - dev->dma_bus;
return bus;
}
#else
#define dev_phys_to_bus(_, _addr) _addr

View File

@@ -1248,13 +1248,13 @@ static int dm_test_dma_offset(struct unit_test_state *uts)
node = ofnode_path("/mmio-bus@0");
ut_assert(ofnode_valid(node));
ut_assertok(uclass_get_device_by_ofnode(UCLASS_TEST_BUS, node, &dev));
ut_asserteq_64(0, dev->dma_offset);
ut_asserteq_64(0, dev->dma_cpu - dev->dma_bus);
/* Device behind a bus with dma-ranges */
node = ofnode_path("/mmio-bus@0/subnode@0");
ut_assert(ofnode_valid(node));
ut_assertok(uclass_get_device_by_ofnode(UCLASS_TEST_FDT, node, &dev));
ut_asserteq_64(-0x10000000ULL, dev->dma_offset);
ut_asserteq_64(-0x10000000ULL, dev->dma_cpu - dev->dma_bus);
/* This one has no dma-ranges */
node = ofnode_path("/mmio-bus@1");
@@ -1263,7 +1263,7 @@ static int dm_test_dma_offset(struct unit_test_state *uts)
node = ofnode_path("/mmio-bus@1/subnode@0");
ut_assert(ofnode_valid(node));
ut_assertok(uclass_get_device_by_ofnode(UCLASS_TEST_FDT, node, &dev));
ut_asserteq_64(0, dev->dma_offset);
ut_asserteq_64(0, dev->dma_cpu - dev->dma_bus);
return 0;
}

View File

@@ -28,8 +28,8 @@ static int dm_test_phys_to_bus(struct unit_test_state *uts)
node = ofnode_path("/mmio-bus@0/subnode@0");
ut_assert(ofnode_valid(node));
ut_assertok(uclass_get_device_by_ofnode(UCLASS_TEST_FDT, node, &dev));
ut_asserteq_addr((void*)0x100fffffULL, (void*)dev_phys_to_bus(dev, 0xfffff));
ut_asserteq_addr((void*)0xfffffULL, (void*)(ulong)dev_bus_to_phys(dev, 0x100fffff));
ut_asserteq_addr((void*)0x1003ffffULL, (void*)dev_phys_to_bus(dev, 0x3ffff));
ut_asserteq_addr((void*)0x3ffffULL, (void*)(ulong)dev_bus_to_phys(dev, 0x1003ffff));
return 0;
}