Re: Proper way to allocate DMA buffer within a single 4GB block?

From: Chuck Ebbert
Date: Sun Sep 24 2017 - 10:20:56 EST


On Wed, 20 Sep 2017 17:18:09 -0500
Timur Tabi <timur@xxxxxxxxxxxxxx> wrote:

> I have a device that requires I allocated a few buffers for DMA. The
> problem is that this device has only one register for the upper 32
> bits of all of the buffers. That is, all of buffers must reside
> within the same 4GB block of memory. In order words,
>
> end = start + size - 1;
> if (upper_32_bits(start) != upper_32_bits(end))
> // Oh no, the buffer spans across a 4GB boundary!
>
> The buffer is typically less than 16KB in size, so we've never seen
> it actually span across a 4GB boundary. However, I want to ensure
> that it's impossible. I wrote this function that re-tries the
> allocation if the first one is invalid, but I suspect that it's too
> hackish. Is there a better way?
>

Allocate a buffer twice as big as what you really need. If the first
half doesn't work, the second half will.