Skip to content

Commit fec9b62

Browse files
Claire Changkonradwilk
authored andcommitted
of: Add plumbing for restricted DMA pool
If a device is not behind an IOMMU, we look up the device node and set up the restricted DMA when the restricted-dma-pool is presented. Signed-off-by: Claire Chang <[email protected]> Tested-by: Stefano Stabellini <[email protected]> Tested-by: Will Deacon <[email protected]> Signed-off-by: Konrad Rzeszutek Wilk <[email protected]>
1 parent b12fe99 commit fec9b62

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

drivers/of/address.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <linux/logic_pio.h>
99
#include <linux/module.h>
1010
#include <linux/of_address.h>
11+
#include <linux/of_reserved_mem.h>
1112
#include <linux/pci.h>
1213
#include <linux/pci_regs.h>
1314
#include <linux/sizes.h>
@@ -995,6 +996,38 @@ int of_dma_get_range(struct device_node *np, const struct bus_dma_region **map)
995996
of_node_put(node);
996997
return ret;
997998
}
999+
1000+
int of_dma_set_restricted_buffer(struct device *dev, struct device_node *np)
1001+
{
1002+
struct device_node *node, *of_node = dev->of_node;
1003+
int count, i;
1004+
1005+
count = of_property_count_elems_of_size(of_node, "memory-region",
1006+
sizeof(u32));
1007+
/*
1008+
* If dev->of_node doesn't exist or doesn't contain memory-region, try
1009+
* the OF node having DMA configuration.
1010+
*/
1011+
if (count <= 0) {
1012+
of_node = np;
1013+
count = of_property_count_elems_of_size(
1014+
of_node, "memory-region", sizeof(u32));
1015+
}
1016+
1017+
for (i = 0; i < count; i++) {
1018+
node = of_parse_phandle(of_node, "memory-region", i);
1019+
/*
1020+
* There might be multiple memory regions, but only one
1021+
* restricted-dma-pool region is allowed.
1022+
*/
1023+
if (of_device_is_compatible(node, "restricted-dma-pool") &&
1024+
of_device_is_available(node))
1025+
return of_reserved_mem_device_init_by_idx(dev, of_node,
1026+
i);
1027+
}
1028+
1029+
return 0;
1030+
}
9981031
#endif /* CONFIG_HAS_DMA */
9991032

10001033
/**

drivers/of/device.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,9 @@ int of_dma_configure_id(struct device *dev, struct device_node *np,
165165

166166
arch_setup_dma_ops(dev, dma_start, size, iommu, coherent);
167167

168+
if (!iommu)
169+
return of_dma_set_restricted_buffer(dev, np);
170+
168171
return 0;
169172
}
170173
EXPORT_SYMBOL_GPL(of_dma_configure_id);

drivers/of/of_private.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,18 @@ struct bus_dma_region;
163163
#if defined(CONFIG_OF_ADDRESS) && defined(CONFIG_HAS_DMA)
164164
int of_dma_get_range(struct device_node *np,
165165
const struct bus_dma_region **map);
166+
int of_dma_set_restricted_buffer(struct device *dev, struct device_node *np);
166167
#else
167168
static inline int of_dma_get_range(struct device_node *np,
168169
const struct bus_dma_region **map)
169170
{
170171
return -ENODEV;
171172
}
173+
static inline int of_dma_set_restricted_buffer(struct device *dev,
174+
struct device_node *np)
175+
{
176+
return -ENODEV;
177+
}
172178
#endif
173179

174180
void fdt_init_reserved_mem(void);

0 commit comments

Comments
 (0)