[U-Boot] [PATCH] mmc:dcache: Cache line size aligned internal MMC buffers

Anton Staaf
2011-08-23T21:48:50Z

On Tue, Aug 23, 2011 at 2:32 PM, Mike Frysinger <vapier@gentoo.org> wrote:
> On Tuesday, August 23, 2011 17:09:37 Wolfgang Denk wrote:
>> Mike Frysinger wrote:
>> > On Tuesday, August 23, 2011 16:27:26 Anton Staaf wrote:
>> > > So then, to guide our efforts, what is a more suitable solution?
>> > > Would you prefer we stick with the existing path of calling memalign
>> > > and passing it the cache size by directly calling
>> > > get_dcache_line_size? ?Or would you prefer something more like a
>> > > dma_buffer_malloc function that allocates on the heap a cache line
>> > > size aligned buffer and returns it?
>> >
>> > memalign() is simply a malloc() with offset fudging, so
>> > dma_buffer_malloc() is the way to go imo. ?anything that involves end
>> > code having to figure out how to align things itself is asking for pain.
>>
>> I would like to avoid using any malloc code here. ?We have to keep in
>> mind that such code changes will spread, and will be copied into
>> driver code, file systems, etc. which might be used (and even
>> required, for example for NAND or SDCard booting systems) before
>> relocation - but malloc becomes available only after relocation.
>>
>> Why cannot we define a macro that declares a (sufficiently sized)
>> buffer on the stack and provides and a pointer to a (correctly
>> aligned) address in this buffer?
>
> isnt that what i already posted and you NAK-ed ? :)
>
> DMA_DECLARE_BUFFER(...)

I wasn't going to say it. :) How about something like this, which is
very similar to what you had Mike, but doesn't define the array in the
macro. It's a bit clearer what is going on, but also requires a bit
more work at each use.

#define DCACHE_RESIZE(size) ((size) + get_dcache_line_size() - 1)
#define DCACHE_ALIGN(buffer) ((buffer) + get_dcache_line_size() - 1) &
~(get_dcache_line_size() - 1)

char buffer[DCACHE_RESIZE(100)];
char * aligned = DCACHE_ALIGN(buffer);

It would be awesome if the idea below worked, but it can't because the
array is popped when the ({...}) scope is exited I believe.

#define allocate_dma_buffer_on_stack(size) \
({ \
char _dma_buffer[(size) + get_dcache_line_size() - 1]; \
\
_dma_buffer & ~(get_dcache_line_size() - 1); \
})

And you could use it like:

char * buffer = allocate_dma_buffer_on_stack(100);

If anyone can think of a way to make this work that would be excellent...

Thanks,
Anton

> -mike
>