All of lore.kernel.org
 help / color / mirror / Atom feed
* Proper way to allocate DMA buffer within a single 4GB block?
@ 2017-09-20 22:18 Timur Tabi
  2017-09-24 14:20 ` Chuck Ebbert
  0 siblings, 1 reply; 2+ messages in thread
From: Timur Tabi @ 2017-09-20 22:18 UTC (permalink / raw)
  To: linux-kernel

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?

static void *dma_alloc_4gb_block(struct device *dev, size_t size,
				   dma_addr_t *dma_handle)
{
	dma_addr_t start, start2, end;
	void *v, *v2;

	v = dma_zalloc_coherent(dev, size, &start, GFP_KERNEL);
	if (!v)
		return NULL;

	/* Test to see if the entire buffer is within one 4G block */
	end = start + size - 1;
	if (upper_32_bits(start) == upper_32_bits(end)) {
		*dma_handle = start;
		return v;
	}

	/* The DMA buffer spanned a 32-bit boundry!  Allocate another
	 * block and try again.  Assuming this block as adjacent to
	 * the first one, it should be valid.
	 */
	v2 = dma_zalloc_coherent(dev, size, &start2, GFP_KERNEL);
	dma_free_coherent(dev, size, v, start);
	if (!v2) {
		return NULL;
	}

	/* Test to see if the entire buffer is within one 4G block */
	end = start2 + size - 1;
	if (upper_32_bits(start2) == upper_32_bits(end)) {
		*dma_handle = start2;
		return v2;
	}

	/* We tried twice and still failed, so just give up. */
	dma_free_coherent(dev, size, v2, start2);
	return NULL;
}

-- 
Qualcomm Datacenter Technologies, Inc. as an affiliate of Qualcomm
Technologies, Inc.  Qualcomm Technologies, Inc. is a member of the
Code Aurora Forum, a Linux Foundation Collaborative Project.

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Proper way to allocate DMA buffer within a single 4GB block?
  2017-09-20 22:18 Proper way to allocate DMA buffer within a single 4GB block? Timur Tabi
@ 2017-09-24 14:20 ` Chuck Ebbert
  0 siblings, 0 replies; 2+ messages in thread
From: Chuck Ebbert @ 2017-09-24 14:20 UTC (permalink / raw)
  To: Timur Tabi; +Cc: linux-kernel

On Wed, 20 Sep 2017 17:18:09 -0500
Timur Tabi <timur@codeaurora.org> 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.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2017-09-24 14:20 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-09-20 22:18 Proper way to allocate DMA buffer within a single 4GB block? Timur Tabi
2017-09-24 14:20 ` Chuck Ebbert

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.