2019-05-22 15:51:44 +08:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
2011-12-29 20:09:51 +08:00
|
|
|
#ifndef __LINUX_CMA_H
|
|
|
|
#define __LINUX_CMA_H
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Contiguous Memory Allocator for DMA mapping framework
|
|
|
|
* Copyright (c) 2010-2011 by Samsung Electronics.
|
|
|
|
* Written by:
|
|
|
|
* Marek Szyprowski <m.szyprowski@samsung.com>
|
|
|
|
* Michal Nazarewicz <mina86@mina86.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Contiguous Memory Allocator
|
|
|
|
*
|
|
|
|
* The Contiguous Memory Allocator (CMA) makes it possible to
|
|
|
|
* allocate big contiguous chunks of memory after the system has
|
|
|
|
* booted.
|
|
|
|
*
|
|
|
|
* Why is it needed?
|
|
|
|
*
|
|
|
|
* Various devices on embedded systems have no scatter-getter and/or
|
|
|
|
* IO map support and require contiguous blocks of memory to
|
|
|
|
* operate. They include devices such as cameras, hardware video
|
|
|
|
* coders, etc.
|
|
|
|
*
|
|
|
|
* Such devices often require big memory buffers (a full HD frame
|
|
|
|
* is, for instance, more then 2 mega pixels large, i.e. more than 6
|
|
|
|
* MB of memory), which makes mechanisms such as kmalloc() or
|
|
|
|
* alloc_page() ineffective.
|
|
|
|
*
|
|
|
|
* At the same time, a solution where a big memory region is
|
|
|
|
* reserved for a device is suboptimal since often more memory is
|
|
|
|
* reserved then strictly required and, moreover, the memory is
|
|
|
|
* inaccessible to page system even if device drivers don't use it.
|
|
|
|
*
|
|
|
|
* CMA tries to solve this issue by operating on memory regions
|
|
|
|
* where only movable pages can be allocated from. This way, kernel
|
|
|
|
* can use the memory for pagecache and when device driver requests
|
|
|
|
* it, allocated pages can be migrated.
|
|
|
|
*
|
|
|
|
* Driver usage
|
|
|
|
*
|
|
|
|
* CMA should not be used by the device drivers directly. It is
|
|
|
|
* only a helper framework for dma-mapping subsystem.
|
|
|
|
*
|
2018-06-13 01:01:45 +08:00
|
|
|
* For more information, see kernel-docs in kernel/dma/contiguous.c
|
2011-12-29 20:09:51 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef __KERNEL__
|
|
|
|
|
2014-08-07 07:05:25 +08:00
|
|
|
#include <linux/device.h>
|
2019-05-30 08:54:25 +08:00
|
|
|
#include <linux/mm.h>
|
2014-08-07 07:05:25 +08:00
|
|
|
|
2011-12-29 20:09:51 +08:00
|
|
|
struct cma;
|
|
|
|
struct page;
|
|
|
|
|
2013-07-02 13:45:15 +08:00
|
|
|
#ifdef CONFIG_DMA_CMA
|
2011-12-29 20:09:51 +08:00
|
|
|
|
|
|
|
extern struct cma *dma_contiguous_default_area;
|
|
|
|
|
2013-07-29 20:31:45 +08:00
|
|
|
static inline struct cma *dev_get_cma_area(struct device *dev)
|
|
|
|
{
|
|
|
|
if (dev && dev->cma_area)
|
|
|
|
return dev->cma_area;
|
|
|
|
return dma_contiguous_default_area;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void dev_set_cma_area(struct device *dev, struct cma *cma)
|
|
|
|
{
|
|
|
|
if (dev)
|
|
|
|
dev->cma_area = cma;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void dma_contiguous_set_default(struct cma *cma)
|
|
|
|
{
|
|
|
|
dma_contiguous_default_area = cma;
|
|
|
|
}
|
|
|
|
|
2011-12-29 20:09:51 +08:00
|
|
|
void dma_contiguous_reserve(phys_addr_t addr_limit);
|
2013-07-29 20:31:45 +08:00
|
|
|
|
|
|
|
int __init dma_contiguous_reserve_area(phys_addr_t size, phys_addr_t base,
|
2014-06-05 07:06:54 +08:00
|
|
|
phys_addr_t limit, struct cma **res_cma,
|
|
|
|
bool fixed);
|
2013-07-29 20:31:45 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* dma_declare_contiguous() - reserve area for contiguous memory handling
|
|
|
|
* for particular device
|
|
|
|
* @dev: Pointer to device structure.
|
|
|
|
* @size: Size of the reserved memory.
|
|
|
|
* @base: Start address of the reserved memory (optional, 0 for any).
|
|
|
|
* @limit: End address of the reserved memory (optional, 0 for any).
|
|
|
|
*
|
|
|
|
* This function reserves memory for specified device. It should be
|
|
|
|
* called by board specific code when early allocator (memblock or bootmem)
|
|
|
|
* is still activate.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static inline int dma_declare_contiguous(struct device *dev, phys_addr_t size,
|
|
|
|
phys_addr_t base, phys_addr_t limit)
|
|
|
|
{
|
|
|
|
struct cma *cma;
|
|
|
|
int ret;
|
2014-06-05 07:06:54 +08:00
|
|
|
ret = dma_contiguous_reserve_area(size, base, limit, &cma, true);
|
2013-07-29 20:31:45 +08:00
|
|
|
if (ret == 0)
|
|
|
|
dev_set_cma_area(dev, cma);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2011-12-29 20:09:51 +08:00
|
|
|
|
2015-10-23 04:32:11 +08:00
|
|
|
struct page *dma_alloc_from_contiguous(struct device *dev, size_t count,
|
2018-08-18 06:49:00 +08:00
|
|
|
unsigned int order, bool no_warn);
|
2011-12-29 20:09:51 +08:00
|
|
|
bool dma_release_from_contiguous(struct device *dev, struct page *pages,
|
|
|
|
int count);
|
dma-contiguous: add dma_{alloc,free}_contiguous() helpers
Both dma_alloc_from_contiguous() and dma_release_from_contiguous() are
very simply implemented, but requiring callers to pass certain
parameters like count and align, and taking a boolean parameter to check
__GFP_NOWARN in the allocation flags. So every function call duplicates
similar work:
unsigned long order = get_order(size);
size_t count = size >> PAGE_SHIFT;
page = dma_alloc_from_contiguous(dev, count, order,
gfp & __GFP_NOWARN);
[...]
dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT);
Additionally, as CMA can be used only in the context which permits
sleeping, most of callers do a gfpflags_allow_blocking() check and a
corresponding fallback allocation of normal pages upon any false result:
if (gfpflags_allow_blocking(flag))
page = dma_alloc_from_contiguous();
if (!page)
page = alloc_pages();
[...]
if (!dma_release_from_contiguous(dev, page, count))
__free_pages(page, get_order(size));
So this patch simplifies those function calls by abstracting these
operations into the two new functions: dma_{alloc,free}_contiguous.
As some callers of dma_{alloc,release}_from_contiguous() might be
complicated, this patch just implements these two new functions to
kernel/dma/direct.c only as an initial step.
Suggested-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Nicolin Chen <nicoleotsuka@gmail.com>
Tested-by: dann frazier <dann.frazier@canonical.com>
Signed-off-by: Christoph Hellwig <hch@lst.de>
2019-05-24 12:06:32 +08:00
|
|
|
struct page *dma_alloc_contiguous(struct device *dev, size_t size, gfp_t gfp);
|
|
|
|
void dma_free_contiguous(struct device *dev, struct page *page, size_t size);
|
2011-12-29 20:09:51 +08:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
2013-07-29 20:31:45 +08:00
|
|
|
static inline struct cma *dev_get_cma_area(struct device *dev)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void dev_set_cma_area(struct device *dev, struct cma *cma) { }
|
|
|
|
|
|
|
|
static inline void dma_contiguous_set_default(struct cma *cma) { }
|
|
|
|
|
2011-12-29 20:09:51 +08:00
|
|
|
static inline void dma_contiguous_reserve(phys_addr_t limit) { }
|
|
|
|
|
2013-07-29 20:31:45 +08:00
|
|
|
static inline int dma_contiguous_reserve_area(phys_addr_t size, phys_addr_t base,
|
2014-06-05 07:06:54 +08:00
|
|
|
phys_addr_t limit, struct cma **res_cma,
|
|
|
|
bool fixed)
|
|
|
|
{
|
2013-07-29 20:31:45 +08:00
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
2011-12-29 20:09:51 +08:00
|
|
|
static inline
|
2012-12-05 22:29:25 +08:00
|
|
|
int dma_declare_contiguous(struct device *dev, phys_addr_t size,
|
2011-12-29 20:09:51 +08:00
|
|
|
phys_addr_t base, phys_addr_t limit)
|
|
|
|
{
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline
|
2015-10-23 04:32:11 +08:00
|
|
|
struct page *dma_alloc_from_contiguous(struct device *dev, size_t count,
|
2018-08-18 06:49:00 +08:00
|
|
|
unsigned int order, bool no_warn)
|
2011-12-29 20:09:51 +08:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline
|
|
|
|
bool dma_release_from_contiguous(struct device *dev, struct page *pages,
|
|
|
|
int count)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-30 08:54:25 +08:00
|
|
|
/* Use fallback alloc() and free() when CONFIG_DMA_CMA=n */
|
dma-contiguous: add dma_{alloc,free}_contiguous() helpers
Both dma_alloc_from_contiguous() and dma_release_from_contiguous() are
very simply implemented, but requiring callers to pass certain
parameters like count and align, and taking a boolean parameter to check
__GFP_NOWARN in the allocation flags. So every function call duplicates
similar work:
unsigned long order = get_order(size);
size_t count = size >> PAGE_SHIFT;
page = dma_alloc_from_contiguous(dev, count, order,
gfp & __GFP_NOWARN);
[...]
dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT);
Additionally, as CMA can be used only in the context which permits
sleeping, most of callers do a gfpflags_allow_blocking() check and a
corresponding fallback allocation of normal pages upon any false result:
if (gfpflags_allow_blocking(flag))
page = dma_alloc_from_contiguous();
if (!page)
page = alloc_pages();
[...]
if (!dma_release_from_contiguous(dev, page, count))
__free_pages(page, get_order(size));
So this patch simplifies those function calls by abstracting these
operations into the two new functions: dma_{alloc,free}_contiguous.
As some callers of dma_{alloc,release}_from_contiguous() might be
complicated, this patch just implements these two new functions to
kernel/dma/direct.c only as an initial step.
Suggested-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Nicolin Chen <nicoleotsuka@gmail.com>
Tested-by: dann frazier <dann.frazier@canonical.com>
Signed-off-by: Christoph Hellwig <hch@lst.de>
2019-05-24 12:06:32 +08:00
|
|
|
static inline struct page *dma_alloc_contiguous(struct device *dev, size_t size,
|
|
|
|
gfp_t gfp)
|
|
|
|
{
|
2019-08-20 10:45:49 +08:00
|
|
|
return NULL;
|
dma-contiguous: add dma_{alloc,free}_contiguous() helpers
Both dma_alloc_from_contiguous() and dma_release_from_contiguous() are
very simply implemented, but requiring callers to pass certain
parameters like count and align, and taking a boolean parameter to check
__GFP_NOWARN in the allocation flags. So every function call duplicates
similar work:
unsigned long order = get_order(size);
size_t count = size >> PAGE_SHIFT;
page = dma_alloc_from_contiguous(dev, count, order,
gfp & __GFP_NOWARN);
[...]
dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT);
Additionally, as CMA can be used only in the context which permits
sleeping, most of callers do a gfpflags_allow_blocking() check and a
corresponding fallback allocation of normal pages upon any false result:
if (gfpflags_allow_blocking(flag))
page = dma_alloc_from_contiguous();
if (!page)
page = alloc_pages();
[...]
if (!dma_release_from_contiguous(dev, page, count))
__free_pages(page, get_order(size));
So this patch simplifies those function calls by abstracting these
operations into the two new functions: dma_{alloc,free}_contiguous.
As some callers of dma_{alloc,release}_from_contiguous() might be
complicated, this patch just implements these two new functions to
kernel/dma/direct.c only as an initial step.
Suggested-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Nicolin Chen <nicoleotsuka@gmail.com>
Tested-by: dann frazier <dann.frazier@canonical.com>
Signed-off-by: Christoph Hellwig <hch@lst.de>
2019-05-24 12:06:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void dma_free_contiguous(struct device *dev, struct page *page,
|
|
|
|
size_t size)
|
|
|
|
{
|
2019-05-30 08:54:25 +08:00
|
|
|
__free_pages(page, get_order(size));
|
dma-contiguous: add dma_{alloc,free}_contiguous() helpers
Both dma_alloc_from_contiguous() and dma_release_from_contiguous() are
very simply implemented, but requiring callers to pass certain
parameters like count and align, and taking a boolean parameter to check
__GFP_NOWARN in the allocation flags. So every function call duplicates
similar work:
unsigned long order = get_order(size);
size_t count = size >> PAGE_SHIFT;
page = dma_alloc_from_contiguous(dev, count, order,
gfp & __GFP_NOWARN);
[...]
dma_release_from_contiguous(dev, page, size >> PAGE_SHIFT);
Additionally, as CMA can be used only in the context which permits
sleeping, most of callers do a gfpflags_allow_blocking() check and a
corresponding fallback allocation of normal pages upon any false result:
if (gfpflags_allow_blocking(flag))
page = dma_alloc_from_contiguous();
if (!page)
page = alloc_pages();
[...]
if (!dma_release_from_contiguous(dev, page, count))
__free_pages(page, get_order(size));
So this patch simplifies those function calls by abstracting these
operations into the two new functions: dma_{alloc,free}_contiguous.
As some callers of dma_{alloc,release}_from_contiguous() might be
complicated, this patch just implements these two new functions to
kernel/dma/direct.c only as an initial step.
Suggested-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Nicolin Chen <nicoleotsuka@gmail.com>
Tested-by: dann frazier <dann.frazier@canonical.com>
Signed-off-by: Christoph Hellwig <hch@lst.de>
2019-05-24 12:06:32 +08:00
|
|
|
}
|
|
|
|
|
2011-12-29 20:09:51 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|