How to allocate 4-8 MB aligned DMA Linux

I am using Fedora 14 and I am creating a driver for a PCI switch. for this switch I need to allocate 8 MB of DMA memory (I can handle 2 * 4 MB), but I need it to be aligned in memory size, for example, if it's 8 MB, then the first 23 bits of the base address should be 0.

how to do it?

+4
source share
2 answers

__get_free_pages can allocate up to 4 MB, distributed by distribution size.
Thus, you can select two 4 MB blocks, each of which is aligned to 4 MB.

You cannot align 8MB in this way.
At boot time, if you allocate 3 * 4 MB, it is very likely that the blocks will be contiguous, so either the first, second, or second and third will be aligned at 8 MB. But I would not rely on it.

Please note that you must do this earlier at boot time, anyway. Otherwise, it is very likely that the memory is fragmented and you cannot get your memory.

+2
source

__get_free_pages gave me a maximum of 4 MB, but it was aligned only with the page size (4k).

I managed to allocate 16 MB of DMA using pci_alloc_consistent and at least 8 MB is aligned with 8 MB. This solved my problem.

0
source

Source: https://habr.com/ru/post/1398611/


All Articles