Skip to contentSkip to conversation

ASoC: omap-pcm: Lower the dma coherent mask to 32bits

PATCH In review Peter Ujfalusi 3 participants 18 messages Dec 5, 2013 – Dec 13, 2013

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ASoC: omap-pcm: Lower the dma coherent mask to 32bits

The underlying API dma_coerce_mask_and_coherent() checks the requested bits mask against the size of dma_addr_t which is 32bits on OMAP. Because of the previously used 64bits mask audio was not probing after commit c9bd5e6 (and 4dcfa6007).
32bits for the DMA_BIT_MASK looks to be the correct one to use.

---
Hi Mark,

This is the fix for OMAP audio for 3.13-rc2+. The dma coherent changes went in between -rc1 and -rc2 which broke things because omap-pcm was using 64bits for dma coherent mask.

Regards,
Peter

 sound/soc/omap/omap-pcm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
sound/soc/omap/omap-pcm.c +1 -1
index b8fa986..01d59d0 100644
@@ -202,7 +202,7 @@ static int omap_pcm_new(struct snd_soc_pcm_runtime *rtd)
 	struct snd_pcm *pcm = rtd->pcm;
 	int ret;
 
-	ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(64));
+	ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(32));
 	if (ret)
 		return ret;
 
Signature · 1 line
1.8.4.5
#1 lore raw
Quoted 6 lines · Peter Ujfalusi

The underlying API dma_coerce_mask_and_coherent() checks the requested bits mask against the size of dma_addr_t which is 32bits on OMAP. Because of the previously used 64bits mask audio was not probing after commit c9bd5e6 (and 4dcfa6007).
32bits for the DMA_BIT_MASK looks to be the correct one to use.

Quoted 7 lines

---
Hi Mark,

This is the fix for OMAP audio for 3.13-rc2+. The dma coherent changes went in between -rc1 and -rc2 which broke things because omap-pcm was using 64bits for dma coherent mask.

Can you please try to understand _why_ it broke and post an explanation. This breakage wasn't expected and shouldn't have happened.

#2 lore raw
Quoted 11 lines · Russell King - ARM Linux
Quoted 10 lines · Peter Ujfalusi

The underlying API dma_coerce_mask_and_coherent() checks the requested bits mask against the size of dma_addr_t which is 32bits on OMAP. Because of the previously used 64bits mask audio was not probing after commit c9bd5e6 (and 4dcfa6007).
32bits for the DMA_BIT_MASK looks to be the correct one to use.

---
Hi Mark,

Quoted 6 lines
Quoted 3 lines

This is the fix for OMAP audio for 3.13-rc2+. The dma coherent changes went in between -rc1 and -rc2 which broke things because omap-pcm was using 64bits for dma coherent mask.

Can you please try to understand _why_ it broke and post an explanation. This breakage wasn't expected and shouldn't have happened.

I'm not that familiar with this part of the code (mm, dma-mapping) but so far this is what I found:

ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(64)); is successful, no failure to set the mask to 64.

later on when requesting the dma channel however:
arm_dma_alloc() -> get_coherent_dma_mask() fails.

As far I can see we have different checks in case of dma_coerce_mask_and_coherent() and arm_dma_alloc():

[1] dma_coerce_mask_and_coherent() -> dma_supported()

if (sizeof(mask) != sizeof(dma_addr_t) &&
mask > (dma_addr_t)~0 &&
dma_to_pfn(dev, ~0) > arm_dma_pfn_limit) /* !!! */
return 0;

[2] arm_dma_alloc() -> get_coherent_dma_mask()

if (sizeof(mask) != sizeof(dma_addr_t) &&
mask > (dma_addr_t)~0 &&
dma_to_pfn(dev, ~0) > min(max_pfn, arm_dma_pfn_limit)) /* !!! */
return 0;

On omap4:
max_pfn: 0xc0000, arm_dma_pfn_limit: 0xfffff

Not sure which check is the correct one but I think in both cases we should have the same check in this way we can catch the issue at dma_coerce_mask_and_coherent() time and try to figure out what to do. In case of omap-pcm we request for 64 bit because of future SoCs, but I think it would be fine to try first 64 and in case it is not supported fall back to 32.

Signature · 1 line
Péter
#3 lore raw
Quoted 27 lines · Peter Ujfalusi

I'm not that familiar with this part of the code (mm, dma-mapping) but so far this is what I found:

ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(64)); is successful, no failure to set the mask to 64.

later on when requesting the dma channel however:
arm_dma_alloc() -> get_coherent_dma_mask() fails.

As far I can see we have different checks in case of dma_coerce_mask_and_coherent() and arm_dma_alloc():

[1] dma_coerce_mask_and_coherent() -> dma_supported()

(quoted diff) +0 -0
 	if (sizeof(mask) != sizeof(dma_addr_t) &&
 	    mask > (dma_addr_t)~0 &&
 	    dma_to_pfn(dev, ~0) > arm_dma_pfn_limit)  /* !!! */
 		return 0;

[2] arm_dma_alloc() -> get_coherent_dma_mask()

(quoted diff) +0 -0
 	if (sizeof(mask) != sizeof(dma_addr_t) &&
 	    mask > (dma_addr_t)~0 &&
 	    dma_to_pfn(dev, ~0) > min(max_pfn, arm_dma_pfn_limit)) /* !!! */
 		return 0;
Quoted 2 lines

On omap4:
max_pfn: 0xc0000, arm_dma_pfn_limit: 0xfffff

Obviously, we should not be saying that the mask is okay, and then failing with that same mask.

I've thinking about what the right fix here is - it's been quite a while since I devised those tests and the knowledge I had back then has been swapped out...

The point of these tests are to deal with masks which are larger than dma_addr_t allows, and to only deny if we have sufficient system memory that we may overflow dma_addr_t.

So...

	sizeof(u64) != sizeof(dma_addr_t)

detects when we have non-64 bit dma_addr_t.

	mask > (dma_addr_t)~0

detects if the mask is larger than 4GiB.

	dma_to_pfn(dev, ~0)

gives us the very last PFN which the bus associated with dev is able to address. Hmm - so I got the test wrong on both twice - they should both be:

if (sizeof(mask) != sizeof(dma_addr_t) &&
mask > (dma_addr_t)~0 &&
dma_to_pfn(dev, ~0) < min(max_pfn, arm_dma_pfn_limit))

		return 0;

since we want to fail if we have _more_ memory than the bus will allow. Can you try the above in both locations please?

#4 lore raw

At Thu, 5 Dec 2013 19:28:53 +0000,

Quoted 64 lines · Russell King - ARM Linux
Quoted 27 lines · Peter Ujfalusi

I'm not that familiar with this part of the code (mm, dma-mapping) but so far this is what I found:

ret = dma_coerce_mask_and_coherent(card->dev, DMA_BIT_MASK(64)); is successful, no failure to set the mask to 64.

later on when requesting the dma channel however:
arm_dma_alloc() -> get_coherent_dma_mask() fails.

As far I can see we have different checks in case of dma_coerce_mask_and_coherent() and arm_dma_alloc():

[1] dma_coerce_mask_and_coherent() -> dma_supported()

(quoted diff) +0 -0
 	if (sizeof(mask) != sizeof(dma_addr_t) &&
 	    mask > (dma_addr_t)~0 &&
 	    dma_to_pfn(dev, ~0) > arm_dma_pfn_limit)  /* !!! */
 		return 0;

[2] arm_dma_alloc() -> get_coherent_dma_mask()

(quoted diff) +0 -0
 	if (sizeof(mask) != sizeof(dma_addr_t) &&
 	    mask > (dma_addr_t)~0 &&
 	    dma_to_pfn(dev, ~0) > min(max_pfn, arm_dma_pfn_limit)) /* !!! */
 		return 0;
Quoted 2 lines

On omap4:
max_pfn: 0xc0000, arm_dma_pfn_limit: 0xfffff

Obviously, we should not be saying that the mask is okay, and then failing with that same mask.

I've thinking about what the right fix here is - it's been quite a while since I devised those tests and the knowledge I had back then has been swapped out...

The point of these tests are to deal with masks which are larger than dma_addr_t allows, and to only deny if we have sufficient system memory that we may overflow dma_addr_t.

So...

	sizeof(u64) != sizeof(dma_addr_t)

detects when we have non-64 bit dma_addr_t.

	mask > (dma_addr_t)~0

detects if the mask is larger than 4GiB.

	dma_to_pfn(dev, ~0)

gives us the very last PFN which the bus associated with dev is able to address. Hmm - so I got the test wrong on both twice - they should both be:

(quoted diff) +0 -0
 	if (sizeof(mask) != sizeof(dma_addr_t) &&
 	    mask > (dma_addr_t)~0 &&
 	    dma_to_pfn(dev, ~0) < min(max_pfn, arm_dma_pfn_limit))
		return 0;
Quoted 2 lines

since we want to fail if we have _more_ memory than the bus will allow. Can you try the above in both locations please?

Does the check of dma_to_pfn(dev, ~0) is really necessary? In get_coherent_dma_mask(), it checks further

		if (dma_to_pfn(dev, mask) < max_dma_pfn) {

and since mask > ~0, this check should suffice, I think.

And this made me wonder why can we simply use dma_supported() for the check in get_coherent_dma_mask(). If the check in get_coherent_mask() must be different, it'd be helpful if you comment on it there, too.

thanks,

Takashi

#5 lore raw
Quoted 1 line · Takashi Iwai

Does the check of dma_to_pfn(dev, ~0) is really necessary?

Of course.

Quoted 5 lines

In get_coherent_dma_mask(), it checks further

		if (dma_to_pfn(dev, mask) < max_dma_pfn) {

and since mask > ~0, this check should suffice, I think.

dma_to_pfn() takes a dma_addr_t as the second argument. At this point, we've ascertained that 'mask' is larger than dma_addr_t, so the above will truncate the mask.

The whole point of this is to protect the following code which does exactly that from this truncation.

#6 lore raw

At Thu, 5 Dec 2013 21:07:07 +0000,

Quoted 8 lines · Russell King - ARM Linux
Quoted 1 line · Takashi Iwai

Does the check of dma_to_pfn(dev, ~0) is really necessary?

Of course.

Quoted 2 lines

In get_coherent_dma_mask(), it checks further

Quoted 7 lines
Quoted 3 lines
		if (dma_to_pfn(dev, mask) < max_dma_pfn) {

and since mask > ~0, this check should suffice, I think.

dma_to_pfn() takes a dma_addr_t as the second argument. At this point, we've ascertained that 'mask' is larger than dma_addr_t, so the above will truncate the mask.

Ah, I missed that point...

Quoted 2 lines

The whole point of this is to protect the following code which does exactly that from this truncation.

OK, I understand that part. Thanks for clarification!

But, it's still unclear why only get_coherent_dma_mask() takes max_pfn into account for the check of dma_to_pfn(dev, mask).

In dma_supported(),

	unsigned long limit;
	limit = dma_to_pfn(dev, mask);
	if (limit < arm_dma_pfn_limit)
		return 0;

while in get_coherent_dma_mask(),

	unsigned long max_dma_pfn;
	max_dma_pfn = min(max_pfn, arm_dma_pfn_limit);
	if (dma_to_pfn(dev, mask) < max_dma_pfn)
		return 0;

So, the current code looks to me that the results from dma_set_coherent_mask() and the actual allocation may conflict.

If adding warnings is the purpose of the open code, we can create a function like __dma_supported(struct device *dev, u64 mask, bool warning) and call it from the both places.

Takashi

#7 lore raw
Quoted 12 lines · Takashi Iwai

But, it's still unclear why only get_coherent_dma_mask() takes max_pfn into account for the check of dma_to_pfn(dev, mask).

In dma_supported(),

	unsigned long limit;
	limit = dma_to_pfn(dev, mask);
	if (limit < arm_dma_pfn_limit)
		return 0;

while in get_coherent_dma_mask(),

Quoted 7 lines
	unsigned long max_dma_pfn;
	max_dma_pfn = min(max_pfn, arm_dma_pfn_limit);
	if (dma_to_pfn(dev, mask) < max_dma_pfn)
		return 0;

So, the current code looks to me that the results from dma_set_coherent_mask() and the actual allocation may conflict.

I did ask Peter to replace *both* with the same thing.

#8 lore raw

At Fri, 6 Dec 2013 12:25:43 +0000,

Quoted 19 lines · Russell King - ARM Linux
Quoted 17 lines · Takashi Iwai

But, it's still unclear why only get_coherent_dma_mask() takes max_pfn into account for the check of dma_to_pfn(dev, mask).

In dma_supported(),

	unsigned long limit;
	limit = dma_to_pfn(dev, mask);
	if (limit < arm_dma_pfn_limit)
		return 0;

while in get_coherent_dma_mask(),

	unsigned long max_dma_pfn;
	max_dma_pfn = min(max_pfn, arm_dma_pfn_limit);
	if (dma_to_pfn(dev, mask) < max_dma_pfn)
		return 0;
Quoted 4 lines
Quoted 2 lines

So, the current code looks to me that the results from dma_set_coherent_mask() and the actual allocation may conflict.

I did ask Peter to replace *both* with the same thing.

Well, I'm looking at different points. You requested Peter to test both functions to take:

if (sizeof(mask) != sizeof(dma_addr_t) &&
mask > (dma_addr_t)~0 &&
dma_to_pfn(dev, ~0) < min(max_pfn, arm_dma_pfn_limit))

		return 0;

But, after that line, dma_supported() has another check:

if (dma_to_pfn(dev, mask) < arm_dma_pfn_limit)
return 0;

and get_coherent_dma_mask() has a different check:

if (dma_to_pfn(dev, mask) < min(max_pfn, arm_dma_pfn_limit))
return 0;

(the expressions here are expanded for easier comparison)

This is what I'm wondering.

Takashi

#9 lore raw
Quoted 44 lines · Takashi Iwai

At Fri, 6 Dec 2013 12:25:43 +0000,

Quoted 19 lines · Russell King - ARM Linux
Quoted 17 lines · Takashi Iwai

But, it's still unclear why only get_coherent_dma_mask() takes max_pfn into account for the check of dma_to_pfn(dev, mask).

In dma_supported(),

	unsigned long limit;
	limit = dma_to_pfn(dev, mask);
	if (limit < arm_dma_pfn_limit)
		return 0;

while in get_coherent_dma_mask(),

	unsigned long max_dma_pfn;
	max_dma_pfn = min(max_pfn, arm_dma_pfn_limit);
	if (dma_to_pfn(dev, mask) < max_dma_pfn)
		return 0;
Quoted 4 lines
Quoted 2 lines

So, the current code looks to me that the results from dma_set_coherent_mask() and the actual allocation may conflict.

I did ask Peter to replace *both* with the same thing.

Well, I'm looking at different points. You requested Peter to test both functions to take:

(quoted diff) +0 -0
 	if (sizeof(mask) != sizeof(dma_addr_t) &&
 	    mask > (dma_addr_t)~0 &&
 	    dma_to_pfn(dev, ~0) < min(max_pfn, arm_dma_pfn_limit))
		return 0;

But, after that line, dma_supported() has another check:

(quoted diff) +0 -0
 	if (dma_to_pfn(dev, mask) < arm_dma_pfn_limit)
 		return 0;

and get_coherent_dma_mask() has a different check:

(quoted diff) +0 -0
 	if (dma_to_pfn(dev, mask) < min(max_pfn, arm_dma_pfn_limit))
 		return 0;
Quoted 3 lines

(the expressions here are expanded for easier comparison)

This is what I'm wondering.

The tests in get_coherent_dma_mask() and dma_supported() should be identical. Fixing the dma_supported() checks and calling dma_supported() from get_coherent_dma_mask() instead of doing the same test coded locally there should be the preferred solution.

I think dma_supported() should be like this:

int dma_supported(struct device *dev, u64 mask)

{
	unsigned long max_dma_pfn;

	/*
	 * If the mask allows for more memory than we can address,
	 * and we actually have that much memory, then we must
	 * indicate that DMA to this device is not supported.
	 */
	max_dma_pfn = min(max_pfn, arm_dma_pfn_limit);
	if (sizeof(mask) != sizeof(dma_addr_t) &&
	    mask > (dma_addr_t)~0 &&
	    dma_to_pfn(dev, ~0) < max_dma_pfn)
		return 0;

	/*
	 * Translate the device's DMA mask to a PFN limit.  This
	 * PFN number includes the page which we can DMA to.
	 */
	if (dma_to_pfn(dev, mask) < max_dma_pfn)
		return 0;

	return 1;
}
Signature · 1 line
Péter
#10 lore raw
In reply to Takashi Iwai
Quoted 12 lines · Takashi Iwai

But, it's still unclear why only get_coherent_dma_mask() takes max_pfn into account for the check of dma_to_pfn(dev, mask).

In dma_supported(),

	unsigned long limit;
	limit = dma_to_pfn(dev, mask);
	if (limit < arm_dma_pfn_limit)
		return 0;

while in get_coherent_dma_mask(),

Quoted 7 lines
	unsigned long max_dma_pfn;
	max_dma_pfn = min(max_pfn, arm_dma_pfn_limit);
	if (dma_to_pfn(dev, mask) < max_dma_pfn)
		return 0;

So, the current code looks to me that the results from dma_set_coherent_mask() and the actual allocation may conflict.

Yes, that's also wrong. Both should be the same, and both should take max_pfn into account.

The reason why max_pfn needs to be taken into account is that is the top of memory - arm_dma_pfn_limit is hard-coded to 4GiB if there's no DMA zone. With some buses (eg, ONAP) this results in a failure as dma_to_pfn(dev, 32-bit) is less than 4GiB.

Quoted 3 lines

If adding warnings is the purpose of the open code, we can create a function like __dma_supported(struct device *dev, u64 mask, bool warning) and call it from the both places.

Yes, that's a good idea. Revised version of the patch. Peter, can you retest please, thanks.

arch/arm/mm/dma-mapping.c +40 -49
index f6b6bfa88ecf..86c564e52ea7 100644
@@ -158,6 +158,44 @@ struct dma_map_ops arm_coherent_dma_ops = {
 };
 EXPORT_SYMBOL(arm_coherent_dma_ops);
 
+static int __dma_supported(struct device *dev, u64 mask, bool warn)
+{
+	unsigned long max_dma_pfn;
+
+	/*
+	 * If the mask allows for more memory than we can address,
+	 * and we actually have that much memory, then we must
+	 * indicate that DMA to this device is not supported.
+	 */
+	if (sizeof(mask) != sizeof(dma_addr_t) &&
+	    mask > (dma_addr_t)~0 &&
+	    dma_to_pfn(dev, ~0) < max_pfn) {
+		if (warn) {
+			dev_warn(dev, "Coherent DMA mask %#llx is larger than dma_addr_t allows\n",
+				 mask);
+			dev_warn(dev, "Driver did not use or check the return value from dma_set_coherent_mask()?\n");
+		}
+		return 0;
+	}
+
+	max_dma_pfn = min(max_pfn, arm_dma_pfn_limit);
+
+	/*
+	 * Translate the device's DMA mask to a PFN limit.  This
+	 * PFN number includes the page which we can DMA to.
+	 */
+	if (dma_to_pfn(dev, mask) < max_dma_pfn) {
+		if (warn)
+			dev_warn(dev, "Coherent DMA mask %#llx (pfn %#lx-%#lx) covers a smaller range of system memory than the DMA zone pfn 0x0-%#lx\n",
+				 mask,
+				 dma_to_pfn(dev, 0), dma_to_pfn(dev, mask) + 1,
+				 max_dma_pfn + 1);
+		return 0;
+	}
+
+	return 1;
+}
+
 static u64 get_coherent_dma_mask(struct device *dev)
 {
 	u64 mask = (u64)DMA_BIT_MASK(32);
@@ -176,34 +214,8 @@ static u64 get_coherent_dma_mask(struct device *dev)
 			return 0;
 		}
 
-		max_dma_pfn = min(max_pfn, arm_dma_pfn_limit);
-
-		/*
-		 * If the mask allows for more memory than we can address,
-		 * and we actually have that much memory, then fail the
-		 * allocation.
-		 */
-		if (sizeof(mask) != sizeof(dma_addr_t) &&
-		    mask > (dma_addr_t)~0 &&
-		    dma_to_pfn(dev, ~0) > max_dma_pfn) {
-			dev_warn(dev, "Coherent DMA mask %#llx is larger than dma_addr_t allows\n",
-				 mask);
-			dev_warn(dev, "Driver did not use or check the return value from dma_set_coherent_mask()?\n");
-			return 0;
-		}
-
-		/*
-		 * Now check that the mask, when translated to a PFN,
-		 * fits within the allowable addresses which we can
-		 * allocate.
-		 */
-		if (dma_to_pfn(dev, mask) < max_dma_pfn) {
-			dev_warn(dev, "Coherent DMA mask %#llx (pfn %#lx-%#lx) covers a smaller range of system memory than the DMA zone pfn 0x0-%#lx\n",
-				 mask,
-				 dma_to_pfn(dev, 0), dma_to_pfn(dev, mask) + 1,
-				 arm_dma_pfn_limit + 1);
+		if (!__dma_supported(dev, mask, true))
 			return 0;
-		}
 	}
 
 	return mask;
@@ -1032,28 +1044,7 @@ void arm_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  */
 int dma_supported(struct device *dev, u64 mask)
 {
-	unsigned long limit;
-
-	/*
-	 * If the mask allows for more memory than we can address,
-	 * and we actually have that much memory, then we must
-	 * indicate that DMA to this device is not supported.
-	 */
-	if (sizeof(mask) != sizeof(dma_addr_t) &&
-	    mask > (dma_addr_t)~0 &&
-	    dma_to_pfn(dev, ~0) > arm_dma_pfn_limit)
-		return 0;
-
-	/*
-	 * Translate the device's DMA mask to a PFN limit.  This
-	 * PFN number includes the page which we can DMA to.
-	 */
-	limit = dma_to_pfn(dev, mask);
-
-	if (limit < arm_dma_pfn_limit)
-		return 0;
-
-	return 1;
+	return __dma_supported(dev, mask, false);
 }
 EXPORT_SYMBOL(dma_supported);
 
author Tested-by
#11 lore raw

Hi Russell,

Quoted 2 lines · Russell King - ARM Linux

Yes, that's a good idea. Revised version of the patch. Peter, can you retest please, thanks.

With this patch PandaES and BeagleXM works fine on top of today's mainline.

Thank you Russell and Takashi!

Quoted 116 lines
arch/arm/mm/dma-mapping.c +40 -49
index f6b6bfa88ecf..86c564e52ea7 100644
@@ -158,6 +158,44 @@ struct dma_map_ops arm_coherent_dma_ops = {
 };
 EXPORT_SYMBOL(arm_coherent_dma_ops);
 
+static int __dma_supported(struct device *dev, u64 mask, bool warn)
+{
+	unsigned long max_dma_pfn;
+
+	/*
+	 * If the mask allows for more memory than we can address,
+	 * and we actually have that much memory, then we must
+	 * indicate that DMA to this device is not supported.
+	 */
+	if (sizeof(mask) != sizeof(dma_addr_t) &&
+	    mask > (dma_addr_t)~0 &&
+	    dma_to_pfn(dev, ~0) < max_pfn) {
+		if (warn) {
+			dev_warn(dev, "Coherent DMA mask %#llx is larger than dma_addr_t allows\n",
+				 mask);
+			dev_warn(dev, "Driver did not use or check the return value from dma_set_coherent_mask()?\n");
+		}
+		return 0;
+	}
+
+	max_dma_pfn = min(max_pfn, arm_dma_pfn_limit);
+
+	/*
+	 * Translate the device's DMA mask to a PFN limit.  This
+	 * PFN number includes the page which we can DMA to.
+	 */
+	if (dma_to_pfn(dev, mask) < max_dma_pfn) {
+		if (warn)
+			dev_warn(dev, "Coherent DMA mask %#llx (pfn %#lx-%#lx) covers a smaller range of system memory than the DMA zone pfn 0x0-%#lx\n",
+				 mask,
+				 dma_to_pfn(dev, 0), dma_to_pfn(dev, mask) + 1,
+				 max_dma_pfn + 1);
+		return 0;
+	}
+
+	return 1;
+}
+
 static u64 get_coherent_dma_mask(struct device *dev)
 {
 	u64 mask = (u64)DMA_BIT_MASK(32);
@@ -176,34 +214,8 @@ static u64 get_coherent_dma_mask(struct device *dev)
 			return 0;
 		}
 
-		max_dma_pfn = min(max_pfn, arm_dma_pfn_limit);
-
-		/*
-		 * If the mask allows for more memory than we can address,
-		 * and we actually have that much memory, then fail the
-		 * allocation.
-		 */
-		if (sizeof(mask) != sizeof(dma_addr_t) &&
-		    mask > (dma_addr_t)~0 &&
-		    dma_to_pfn(dev, ~0) > max_dma_pfn) {
-			dev_warn(dev, "Coherent DMA mask %#llx is larger than dma_addr_t allows\n",
-				 mask);
-			dev_warn(dev, "Driver did not use or check the return value from dma_set_coherent_mask()?\n");
-			return 0;
-		}
-
-		/*
-		 * Now check that the mask, when translated to a PFN,
-		 * fits within the allowable addresses which we can
-		 * allocate.
-		 */
-		if (dma_to_pfn(dev, mask) < max_dma_pfn) {
-			dev_warn(dev, "Coherent DMA mask %#llx (pfn %#lx-%#lx) covers a smaller range of system memory than the DMA zone pfn 0x0-%#lx\n",
-				 mask,
-				 dma_to_pfn(dev, 0), dma_to_pfn(dev, mask) + 1,
-				 arm_dma_pfn_limit + 1);
+		if (!__dma_supported(dev, mask, true))
 			return 0;
-		}
 	}
 
 	return mask;
@@ -1032,28 +1044,7 @@ void arm_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  */
 int dma_supported(struct device *dev, u64 mask)
 {
-	unsigned long limit;
-
-	/*
-	 * If the mask allows for more memory than we can address,
-	 * and we actually have that much memory, then we must
-	 * indicate that DMA to this device is not supported.
-	 */
-	if (sizeof(mask) != sizeof(dma_addr_t) &&
-	    mask > (dma_addr_t)~0 &&
-	    dma_to_pfn(dev, ~0) > arm_dma_pfn_limit)
-		return 0;
-
-	/*
-	 * Translate the device's DMA mask to a PFN limit.  This
-	 * PFN number includes the page which we can DMA to.
-	 */
-	limit = dma_to_pfn(dev, mask);
-
-	if (limit < arm_dma_pfn_limit)
-		return 0;
-
-	return 1;
+	return __dma_supported(dev, mask, false);
 }
 EXPORT_SYMBOL(dma_supported);
 
Signature · 1 line
Péter
#12 lore raw

One note for the patch:

Quoted 55 lines · Peter Ujfalusi

Hi Russell,

Quoted 2 lines · Russell King - ARM Linux

Yes, that's a good idea. Revised version of the patch. Peter, can you retest please, thanks.

With this patch PandaES and BeagleXM works fine on top of today's mainline.

Thank you Russell and Takashi!

Quoted 41 lines
arch/arm/mm/dma-mapping.c +33 -0
index f6b6bfa88ecf..86c564e52ea7 100644
@@ -158,6 +158,44 @@ struct dma_map_ops arm_coherent_dma_ops = {
 };
 EXPORT_SYMBOL(arm_coherent_dma_ops);
 
+static int __dma_supported(struct device *dev, u64 mask, bool warn)
+{
+	unsigned long max_dma_pfn;
+
+	/*
+	 * If the mask allows for more memory than we can address,
+	 * and we actually have that much memory, then we must
+	 * indicate that DMA to this device is not supported.
+	 */
+	if (sizeof(mask) != sizeof(dma_addr_t) &&
+	    mask > (dma_addr_t)~0 &&
+	    dma_to_pfn(dev, ~0) < max_pfn) {
+		if (warn) {
+			dev_warn(dev, "Coherent DMA mask %#llx is larger than dma_addr_t allows\n",
+				 mask);
+			dev_warn(dev, "Driver did not use or check the return value from dma_set_coherent_mask()?\n");
+		}
+		return 0;
+	}
+
+	max_dma_pfn = min(max_pfn, arm_dma_pfn_limit);
+
+	/*
+	 * Translate the device's DMA mask to a PFN limit.  This
+	 * PFN number includes the page which we can DMA to.
+	 */
+	if (dma_to_pfn(dev, mask) < max_dma_pfn) {
+		if (warn)
+			dev_warn(dev, "Coherent DMA mask %#llx (pfn %#lx-%#lx) covers a smaller range of system memory than the DMA zone pfn 0x0-%#lx\n",
+				 mask,
+				 dma_to_pfn(dev, 0), dma_to_pfn(dev, mask) + 1,
+				 max_dma_pfn + 1);
+		return 0;
Quoted 8 lines

arch/arm/mm/dma-mapping.c struct dma_map_ops arm_coherent_dma_ops = { · line 201

Quoted 8 lines
arch/arm/mm/dma-mapping.c +5 -0
+	}
+
+	return 1;
+}
+
 static u64 get_coherent_dma_mask(struct device *dev)
 {
 	u64 mask = (u64)DMA_BIT_MASK(32);

CC arch/arm/mach-omap2/control.o
arch/arm/mm/dma-mapping.c: In function ‘get_coherent_dma_mask’:
arch/arm/mm/dma-mapping.c:204:17: warning: unused variable ‘max_dma_pfn’ [-Wunused-variable]

   unsigned long max_dma_pfn;
                 ^
Quoted 69 lines
Quoted 67 lines
arch/arm/mm/dma-mapping.c +2 -49
@@ -176,34 +214,8 @@ static u64 get_coherent_dma_mask(struct device *dev)
 			return 0;
 		}
 
-		max_dma_pfn = min(max_pfn, arm_dma_pfn_limit);
-
-		/*
-		 * If the mask allows for more memory than we can address,
-		 * and we actually have that much memory, then fail the
-		 * allocation.
-		 */
-		if (sizeof(mask) != sizeof(dma_addr_t) &&
-		    mask > (dma_addr_t)~0 &&
-		    dma_to_pfn(dev, ~0) > max_dma_pfn) {
-			dev_warn(dev, "Coherent DMA mask %#llx is larger than dma_addr_t allows\n",
-				 mask);
-			dev_warn(dev, "Driver did not use or check the return value from dma_set_coherent_mask()?\n");
-			return 0;
-		}
-
-		/*
-		 * Now check that the mask, when translated to a PFN,
-		 * fits within the allowable addresses which we can
-		 * allocate.
-		 */
-		if (dma_to_pfn(dev, mask) < max_dma_pfn) {
-			dev_warn(dev, "Coherent DMA mask %#llx (pfn %#lx-%#lx) covers a smaller range of system memory than the DMA zone pfn 0x0-%#lx\n",
-				 mask,
-				 dma_to_pfn(dev, 0), dma_to_pfn(dev, mask) + 1,
-				 arm_dma_pfn_limit + 1);
+		if (!__dma_supported(dev, mask, true))
 			return 0;
-		}
 	}
 
 	return mask;
@@ -1032,28 +1044,7 @@ void arm_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  */
 int dma_supported(struct device *dev, u64 mask)
 {
-	unsigned long limit;
-
-	/*
-	 * If the mask allows for more memory than we can address,
-	 * and we actually have that much memory, then we must
-	 * indicate that DMA to this device is not supported.
-	 */
-	if (sizeof(mask) != sizeof(dma_addr_t) &&
-	    mask > (dma_addr_t)~0 &&
-	    dma_to_pfn(dev, ~0) > arm_dma_pfn_limit)
-		return 0;
-
-	/*
-	 * Translate the device's DMA mask to a PFN limit.  This
-	 * PFN number includes the page which we can DMA to.
-	 */
-	limit = dma_to_pfn(dev, mask);
-
-	if (limit < arm_dma_pfn_limit)
-		return 0;
-
-	return 1;
+	return __dma_supported(dev, mask, false);
 }
 EXPORT_SYMBOL(dma_supported);
 
Signature · 1 line
Péter
#13 lore raw
In reply to Peter Ujfalusi
Quoted 11 lines · Peter Ujfalusi

Hi Russell,

Quoted 2 lines · Russell King - ARM Linux

Yes, that's a good idea. Revised version of the patch. Peter, can you retest please, thanks.

With this patch PandaES and BeagleXM works fine on top of today's mainline.

Thank you Russell and Takashi!

Russell,

are you planning to send this for 3.13? Without the patch all OMAP audio fails to probe in 3.13 kernel.

Signature · 1 line
Péter
Quoted 119 lines
Quoted 117 lines
arch/arm/mm/dma-mapping.c +40 -49
index f6b6bfa88ecf..86c564e52ea7 100644
@@ -158,6 +158,44 @@ struct dma_map_ops arm_coherent_dma_ops = {
 };
 EXPORT_SYMBOL(arm_coherent_dma_ops);
 
+static int __dma_supported(struct device *dev, u64 mask, bool warn)
+{
+	unsigned long max_dma_pfn;
+
+	/*
+	 * If the mask allows for more memory than we can address,
+	 * and we actually have that much memory, then we must
+	 * indicate that DMA to this device is not supported.
+	 */
+	if (sizeof(mask) != sizeof(dma_addr_t) &&
+	    mask > (dma_addr_t)~0 &&
+	    dma_to_pfn(dev, ~0) < max_pfn) {
+		if (warn) {
+			dev_warn(dev, "Coherent DMA mask %#llx is larger than dma_addr_t allows\n",
+				 mask);
+			dev_warn(dev, "Driver did not use or check the return value from dma_set_coherent_mask()?\n");
+		}
+		return 0;
+	}
+
+	max_dma_pfn = min(max_pfn, arm_dma_pfn_limit);
+
+	/*
+	 * Translate the device's DMA mask to a PFN limit.  This
+	 * PFN number includes the page which we can DMA to.
+	 */
+	if (dma_to_pfn(dev, mask) < max_dma_pfn) {
+		if (warn)
+			dev_warn(dev, "Coherent DMA mask %#llx (pfn %#lx-%#lx) covers a smaller range of system memory than the DMA zone pfn 0x0-%#lx\n",
+				 mask,
+				 dma_to_pfn(dev, 0), dma_to_pfn(dev, mask) + 1,
+				 max_dma_pfn + 1);
+		return 0;
+	}
+
+	return 1;
+}
+
 static u64 get_coherent_dma_mask(struct device *dev)
 {
 	u64 mask = (u64)DMA_BIT_MASK(32);
@@ -176,34 +214,8 @@ static u64 get_coherent_dma_mask(struct device *dev)
 			return 0;
 		}
 
-		max_dma_pfn = min(max_pfn, arm_dma_pfn_limit);
-
-		/*
-		 * If the mask allows for more memory than we can address,
-		 * and we actually have that much memory, then fail the
-		 * allocation.
-		 */
-		if (sizeof(mask) != sizeof(dma_addr_t) &&
-		    mask > (dma_addr_t)~0 &&
-		    dma_to_pfn(dev, ~0) > max_dma_pfn) {
-			dev_warn(dev, "Coherent DMA mask %#llx is larger than dma_addr_t allows\n",
-				 mask);
-			dev_warn(dev, "Driver did not use or check the return value from dma_set_coherent_mask()?\n");
-			return 0;
-		}
-
-		/*
-		 * Now check that the mask, when translated to a PFN,
-		 * fits within the allowable addresses which we can
-		 * allocate.
-		 */
-		if (dma_to_pfn(dev, mask) < max_dma_pfn) {
-			dev_warn(dev, "Coherent DMA mask %#llx (pfn %#lx-%#lx) covers a smaller range of system memory than the DMA zone pfn 0x0-%#lx\n",
-				 mask,
-				 dma_to_pfn(dev, 0), dma_to_pfn(dev, mask) + 1,
-				 arm_dma_pfn_limit + 1);
+		if (!__dma_supported(dev, mask, true))
 			return 0;
-		}
 	}
 
 	return mask;
@@ -1032,28 +1044,7 @@ void arm_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  */
 int dma_supported(struct device *dev, u64 mask)
 {
-	unsigned long limit;
-
-	/*
-	 * If the mask allows for more memory than we can address,
-	 * and we actually have that much memory, then we must
-	 * indicate that DMA to this device is not supported.
-	 */
-	if (sizeof(mask) != sizeof(dma_addr_t) &&
-	    mask > (dma_addr_t)~0 &&
-	    dma_to_pfn(dev, ~0) > arm_dma_pfn_limit)
-		return 0;
-
-	/*
-	 * Translate the device's DMA mask to a PFN limit.  This
-	 * PFN number includes the page which we can DMA to.
-	 */
-	limit = dma_to_pfn(dev, mask);
-
-	if (limit < arm_dma_pfn_limit)
-		return 0;
-
-	return 1;
+	return __dma_supported(dev, mask, false);
 }
 EXPORT_SYMBOL(dma_supported);
 
#14 lore raw
Quoted 9 lines · Peter Ujfalusi
Quoted 8 lines · Peter Ujfalusi

Hi Russell,

Quoted 2 lines · Russell King - ARM Linux

Yes, that's a good idea. Revised version of the patch. Peter, can you retest please, thanks.

With this patch PandaES and BeagleXM works fine on top of today's mainline.

Quoted 8 lines
Quoted 3 lines

Thank you Russell and Takashi!

Russell,

are you planning to send this for 3.13? Without the patch all OMAP audio fails to probe in 3.13 kernel.

It's queued up in -fixes, but there's a few other fixes in progress which I want to get together before I finally push it to Linus.

#15 lore raw
Quoted 14 lines · Russell King - ARM Linux
Quoted 13 lines · Peter Ujfalusi
Quoted 11 lines · Peter Ujfalusi

Hi Russell,

Quoted 2 lines · Russell King - ARM Linux

Yes, that's a good idea. Revised version of the patch. Peter, can you retest please, thanks.

With this patch PandaES and BeagleXM works fine on top of today's mainline.

Thank you Russell and Takashi!

Quoted 7 lines
Quoted 4 lines

Russell,

are you planning to send this for 3.13? Without the patch all OMAP audio fails to probe in 3.13 kernel.

It's queued up in -fixes, but there's a few other fixes in progress which I want to get together before I finally push it to Linus.

Sure, the important thing is that it is going.

Thank you,
Péter

author Tested-by
#16 lore raw
In reply to Russell King - ARM Linux
Quoted 11 lines · Russell King - ARM Linux

gives us the very last PFN which the bus associated with dev is able to address. Hmm - so I got the test wrong on both twice - they should both be:

(quoted diff) +0 -0
 	if (sizeof(mask) != sizeof(dma_addr_t) &&
 	    mask > (dma_addr_t)~0 &&
 	    dma_to_pfn(dev, ~0) < min(max_pfn, arm_dma_pfn_limit))
		return 0;

since we want to fail if we have _more_ memory than the bus will allow. Can you try the above in both locations please?

Thanks, works fine.
I will be out for a long weekend so you can add my:

to the final patch.

Signature · 1 line
Péter
#17 lore raw
Quoted 10 lines · Peter Ujfalusi
Quoted 9 lines · Russell King - ARM Linux

gives us the very last PFN which the bus associated with dev is able to address. Hmm - so I got the test wrong on both twice - they should both be:

(quoted diff) +0 -0
 	if (sizeof(mask) != sizeof(dma_addr_t) &&
 	    mask > (dma_addr_t)~0 &&
 	    dma_to_pfn(dev, ~0) < min(max_pfn, arm_dma_pfn_limit))
		return 0;
Quoted 7 lines
Quoted 2 lines

since we want to fail if we have _more_ memory than the bus will allow. Can you try the above in both locations please?

Thanks, works fine.
I will be out for a long weekend so you can add my:

Thanks Peter.