[PATCH] of: calculate masks of the device based on dma-range size

From: Murali Karicheri
Date: Fri Feb 06 2015 - 15:16:28 EST


This patch update of_dma_configure() API to calculate the
masks (dma_mask and coherent_dma_mask) based on the dma-range
values set in DT for the device. Also limit the mask to lower
of the default mask and mask calculated.

Cc: Joerg Roedel <joro@xxxxxxxxxx>
Cc: Grant Likely <grant.likely@xxxxxxxxxx>
Cc: Rob Herring <robh+dt@xxxxxxxxxx>
Cc: Bjorn Helgaas <bhelgaas@xxxxxxxxxx>
Cc: Will Deacon <will.deacon@xxxxxxx>
Cc: Russell King <linux@xxxxxxxxxxxxxxxx>
Cc: Arnd Bergmann <arnd@xxxxxxxx>
Cc: Suravee Suthikulpanit <Suravee.Suthikulpanit@xxxxxxx>

Signed-off-by: Murali Karicheri <m-karicheri2@xxxxxx>
---
drivers/of/device.c | 27 ++++++++++++++++++++-------
1 file changed, 20 insertions(+), 7 deletions(-)

diff --git a/drivers/of/device.c b/drivers/of/device.c
index 314c8a9..44209fa 100644
--- a/drivers/of/device.c
+++ b/drivers/of/device.c
@@ -83,17 +83,18 @@ int of_device_add(struct platform_device *ofdev)
*/
void of_dma_configure(struct device *dev, struct device_node *np)
{
- u64 dma_addr, paddr, size;
+ u64 dma_addr = 0, paddr, size;
int ret;
bool coherent;
- unsigned long offset;
+ unsigned long offset = 0;
struct iommu_ops *iommu;

/*
* Set default dma-mask to 32 bit. Drivers are expected to setup
* the correct supported dma_mask.
*/
- dev->coherent_dma_mask = DMA_BIT_MASK(32);
+ if (!dev->coherent_dma_mask)
+ dev->coherent_dma_mask = DMA_BIT_MASK(32);

/*
* Set it to coherent_dma_mask by default if the architecture
@@ -102,11 +103,14 @@ void of_dma_configure(struct device *dev, struct device_node *np)
if (!dev->dma_mask)
dev->dma_mask = &dev->coherent_dma_mask;

+ /*
+ * Set default size to cover the 32-bit. Drivers are expected to setup
+ * the correct size and dma_mask.
+ */
+ size = 1ULL << 32;
+
ret = of_dma_get_range(np, &dma_addr, &paddr, &size);
- if (ret < 0) {
- dma_addr = offset = 0;
- size = dev->coherent_dma_mask + 1;
- } else {
+ if (!ret) {
offset = PFN_DOWN(paddr - dma_addr);

/*
@@ -128,6 +132,15 @@ void of_dma_configure(struct device *dev, struct device_node *np)

dev->dma_pfn_offset = offset;

+ /*
+ * Limit coherent and dma mask based on size and default mask
+ * set by the driver.
+ */
+ dev->coherent_dma_mask = min(dev->coherent_dma_mask,
+ DMA_BIT_MASK(ilog2(dma_addr + size)));
+ *dev->dma_mask = min((*dev->dma_mask),
+ DMA_BIT_MASK(ilog2(dma_addr + size)));
+
coherent = of_dma_is_coherent(np);
dev_dbg(dev, "device is%sdma coherent\n",
coherent ? " " : " not ");
--
1.7.9.5

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/