Skip to content

Commit c64be2b

Browse files
committed
drivers: add Contiguous Memory Allocator
The Contiguous Memory Allocator is a set of helper functions for DMA mapping framework that improves allocations of contiguous memory chunks. CMA grabs memory on system boot, marks it with MIGRATE_CMA migrate type and gives back to the system. Kernel is allowed to allocate only movable pages within CMA's managed memory so that it can be used for example for page cache when DMA mapping do not use it. On dma_alloc_from_contiguous() request such pages are migrated out of CMA area to free required contiguous block and fulfill the request. This allows to allocate large contiguous chunks of memory at any time assuming that there is enough free memory available in the system. This code is heavily based on earlier works by Michal Nazarewicz. Signed-off-by: Marek Szyprowski <m.szyprowski@samsung.com> Signed-off-by: Kyungmin Park <kyungmin.park@samsung.com> Signed-off-by: Michal Nazarewicz <mina86@mina86.com> Acked-by: Arnd Bergmann <arnd@arndb.de> Tested-by: Rob Clark <rob.clark@linaro.org> Tested-by: Ohad Ben-Cohen <ohad@wizery.com> Tested-by: Benjamin Gaignard <benjamin.gaignard@linaro.org> Tested-by: Robert Nelson <robertcnelson@gmail.com> Tested-by: Barry Song <Baohua.Song@csr.com>
1 parent 49f223a commit c64be2b

8 files changed

Lines changed: 641 additions & 0 deletions

File tree

‎Documentation/kernel-parameters.txt‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,11 @@ bytes respectively. Such letter suffixes can also be entirely omitted.
508508
Also note the kernel might malfunction if you disable
509509
some critical bits.
510510

511+
cma=nn[MG] [ARM,KNL]
512+
Sets the size of kernel global memory area for contiguous
513+
memory allocations. For more information, see
514+
include/linux/dma-contiguous.h
515+
511516
cmo_free_hint= [PPC] Format: { yes | no }
512517
Specify whether pages are marked as being inactive
513518
when they are freed. This is used in CMO environments

‎arch/Kconfig‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ config HAVE_ARCH_TRACEHOOK
142142
config HAVE_DMA_ATTRS
143143
bool
144144

145+
config HAVE_DMA_CONTIGUOUS
146+
bool
147+
145148
config USE_GENERIC_SMP_HELPERS
146149
bool
147150

‎drivers/base/Kconfig‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,93 @@ config DMA_SHARED_BUFFER
192192
APIs extension; the file's descriptor can then be passed on to other
193193
driver.
194194

195+
config CMA
196+
bool "Contiguous Memory Allocator (EXPERIMENTAL)"
197+
depends on HAVE_DMA_CONTIGUOUS && HAVE_MEMBLOCK && EXPERIMENTAL
198+
select MIGRATION
199+
help
200+
This enables the Contiguous Memory Allocator which allows drivers
201+
to allocate big physically-contiguous blocks of memory for use with
202+
hardware components that do not support I/O map nor scatter-gather.
203+
204+
For more information see <include/linux/dma-contiguous.h>.
205+
If unsure, say "n".
206+
207+
if CMA
208+
209+
config CMA_DEBUG
210+
bool "CMA debug messages (DEVELOPMENT)"
211+
depends on DEBUG_KERNEL
212+
help
213+
Turns on debug messages in CMA. This produces KERN_DEBUG
214+
messages for every CMA call as well as various messages while
215+
processing calls such as dma_alloc_from_contiguous().
216+
This option does not affect warning and error messages.
217+
218+
comment "Default contiguous memory area size:"
219+
220+
config CMA_SIZE_MBYTES
221+
int "Size in Mega Bytes"
222+
depends on !CMA_SIZE_SEL_PERCENTAGE
223+
default 16
224+
help
225+
Defines the size (in MiB) of the default memory area for Contiguous
226+
Memory Allocator.
227+
228+
config CMA_SIZE_PERCENTAGE
229+
int "Percentage of total memory"
230+
depends on !CMA_SIZE_SEL_MBYTES
231+
default 10
232+
help
233+
Defines the size of the default memory area for Contiguous Memory
234+
Allocator as a percentage of the total memory in the system.
235+
236+
choice
237+
prompt "Selected region size"
238+
default CMA_SIZE_SEL_ABSOLUTE
239+
240+
config CMA_SIZE_SEL_MBYTES
241+
bool "Use mega bytes value only"
242+
243+
config CMA_SIZE_SEL_PERCENTAGE
244+
bool "Use percentage value only"
245+
246+
config CMA_SIZE_SEL_MIN
247+
bool "Use lower value (minimum)"
248+
249+
config CMA_SIZE_SEL_MAX
250+
bool "Use higher value (maximum)"
251+
252+
endchoice
253+
254+
config CMA_ALIGNMENT
255+
int "Maximum PAGE_SIZE order of alignment for contiguous buffers"
256+
range 4 9
257+
default 8
258+
help
259+
DMA mapping framework by default aligns all buffers to the smallest
260+
PAGE_SIZE order which is greater than or equal to the requested buffer
261+
size. This works well for buffers up to a few hundreds kilobytes, but
262+
for larger buffers it just a memory waste. With this parameter you can
263+
specify the maximum PAGE_SIZE order for contiguous buffers. Larger
264+
buffers will be aligned only to this specified order. The order is
265+
expressed as a power of two multiplied by the PAGE_SIZE.
266+
267+
For example, if your system defaults to 4KiB pages, the order value
268+
of 8 means that the buffers will be aligned up to 1MiB only.
269+
270+
If unsure, leave the default value "8".
271+
272+
config CMA_AREAS
273+
int "Maximum count of the CMA device-private areas"
274+
default 7
275+
help
276+
CMA allows to create CMA areas for particular devices. This parameter
277+
sets the maximum number of such device private CMA areas in the
278+
system.
279+
280+
If unsure, leave the default value "7".
281+
282+
endif
283+
195284
endmenu

‎drivers/base/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ obj-y := core.o bus.o dd.o syscore.o \
66
attribute_container.o transport_class.o \
77
topology.o
88
obj-$(CONFIG_DEVTMPFS) += devtmpfs.o
9+
obj-$(CONFIG_CMA) += dma-contiguous.o
910
obj-y += power/
1011
obj-$(CONFIG_HAS_DMA) += dma-mapping.o
1112
obj-$(CONFIG_HAVE_GENERIC_DMA_COHERENT) += dma-coherent.o

0 commit comments

Comments
 (0)