Marek Szyprowski | c64be2b | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 1 | #ifndef __LINUX_CMA_H |
| 2 | #define __LINUX_CMA_H |
| 3 | |
| 4 | /* |
| 5 | * Contiguous Memory Allocator for DMA mapping framework |
| 6 | * Copyright (c) 2010-2011 by Samsung Electronics. |
| 7 | * Written by: |
| 8 | * Marek Szyprowski <m.szyprowski@samsung.com> |
| 9 | * Michal Nazarewicz <mina86@mina86.com> |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU General Public License as |
| 13 | * published by the Free Software Foundation; either version 2 of the |
| 14 | * License or (at your optional) any later version of the license. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * Contiguous Memory Allocator |
| 19 | * |
| 20 | * The Contiguous Memory Allocator (CMA) makes it possible to |
| 21 | * allocate big contiguous chunks of memory after the system has |
| 22 | * booted. |
| 23 | * |
| 24 | * Why is it needed? |
| 25 | * |
| 26 | * Various devices on embedded systems have no scatter-getter and/or |
| 27 | * IO map support and require contiguous blocks of memory to |
| 28 | * operate. They include devices such as cameras, hardware video |
| 29 | * coders, etc. |
| 30 | * |
| 31 | * Such devices often require big memory buffers (a full HD frame |
| 32 | * is, for instance, more then 2 mega pixels large, i.e. more than 6 |
| 33 | * MB of memory), which makes mechanisms such as kmalloc() or |
| 34 | * alloc_page() ineffective. |
| 35 | * |
| 36 | * At the same time, a solution where a big memory region is |
| 37 | * reserved for a device is suboptimal since often more memory is |
| 38 | * reserved then strictly required and, moreover, the memory is |
| 39 | * inaccessible to page system even if device drivers don't use it. |
| 40 | * |
| 41 | * CMA tries to solve this issue by operating on memory regions |
| 42 | * where only movable pages can be allocated from. This way, kernel |
| 43 | * can use the memory for pagecache and when device driver requests |
| 44 | * it, allocated pages can be migrated. |
| 45 | * |
| 46 | * Driver usage |
| 47 | * |
| 48 | * CMA should not be used by the device drivers directly. It is |
| 49 | * only a helper framework for dma-mapping subsystem. |
| 50 | * |
| 51 | * For more information, see kernel-docs in drivers/base/dma-contiguous.c |
| 52 | */ |
| 53 | |
| 54 | #ifdef __KERNEL__ |
| 55 | |
Joonsoo Kim | a254129 | 2014-08-06 16:05:25 -0700 | [diff] [blame] | 56 | #include <linux/device.h> |
| 57 | |
Marek Szyprowski | c64be2b | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 58 | struct cma; |
| 59 | struct page; |
Marek Szyprowski | c64be2b | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 60 | |
Aneesh Kumar K.V | f825c73 | 2013-07-02 11:15:15 +0530 | [diff] [blame] | 61 | #ifdef CONFIG_DMA_CMA |
Marek Szyprowski | c64be2b | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 62 | |
Marek Szyprowski | c64be2b | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 63 | extern struct cma *dma_contiguous_default_area; |
| 64 | |
Marek Szyprowski | a254738 | 2013-07-29 14:31:45 +0200 | [diff] [blame] | 65 | static inline struct cma *dev_get_cma_area(struct device *dev) |
| 66 | { |
| 67 | if (dev && dev->cma_area) |
| 68 | return dev->cma_area; |
| 69 | return dma_contiguous_default_area; |
| 70 | } |
| 71 | |
| 72 | static inline void dev_set_cma_area(struct device *dev, struct cma *cma) |
| 73 | { |
| 74 | if (dev) |
| 75 | dev->cma_area = cma; |
| 76 | } |
| 77 | |
| 78 | static inline void dma_contiguous_set_default(struct cma *cma) |
| 79 | { |
| 80 | dma_contiguous_default_area = cma; |
| 81 | } |
| 82 | |
Marek Szyprowski | c64be2b | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 83 | void dma_contiguous_reserve(phys_addr_t addr_limit); |
Marek Szyprowski | a254738 | 2013-07-29 14:31:45 +0200 | [diff] [blame] | 84 | |
| 85 | int __init dma_contiguous_reserve_area(phys_addr_t size, phys_addr_t base, |
Akinobu Mita | 5ea3b1b | 2014-06-04 16:06:54 -0700 | [diff] [blame] | 86 | phys_addr_t limit, struct cma **res_cma, |
| 87 | bool fixed); |
Marek Szyprowski | a254738 | 2013-07-29 14:31:45 +0200 | [diff] [blame] | 88 | |
| 89 | /** |
| 90 | * dma_declare_contiguous() - reserve area for contiguous memory handling |
| 91 | * for particular device |
| 92 | * @dev: Pointer to device structure. |
| 93 | * @size: Size of the reserved memory. |
| 94 | * @base: Start address of the reserved memory (optional, 0 for any). |
| 95 | * @limit: End address of the reserved memory (optional, 0 for any). |
| 96 | * |
| 97 | * This function reserves memory for specified device. It should be |
| 98 | * called by board specific code when early allocator (memblock or bootmem) |
| 99 | * is still activate. |
| 100 | */ |
| 101 | |
| 102 | static inline int dma_declare_contiguous(struct device *dev, phys_addr_t size, |
| 103 | phys_addr_t base, phys_addr_t limit) |
| 104 | { |
| 105 | struct cma *cma; |
| 106 | int ret; |
Akinobu Mita | 5ea3b1b | 2014-06-04 16:06:54 -0700 | [diff] [blame] | 107 | ret = dma_contiguous_reserve_area(size, base, limit, &cma, true); |
Marek Szyprowski | a254738 | 2013-07-29 14:31:45 +0200 | [diff] [blame] | 108 | if (ret == 0) |
| 109 | dev_set_cma_area(dev, cma); |
| 110 | |
| 111 | return ret; |
| 112 | } |
Marek Szyprowski | c64be2b | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 113 | |
| 114 | struct page *dma_alloc_from_contiguous(struct device *dev, int count, |
| 115 | unsigned int order); |
| 116 | bool dma_release_from_contiguous(struct device *dev, struct page *pages, |
| 117 | int count); |
| 118 | |
| 119 | #else |
| 120 | |
Marek Szyprowski | a254738 | 2013-07-29 14:31:45 +0200 | [diff] [blame] | 121 | static inline struct cma *dev_get_cma_area(struct device *dev) |
| 122 | { |
| 123 | return NULL; |
| 124 | } |
| 125 | |
| 126 | static inline void dev_set_cma_area(struct device *dev, struct cma *cma) { } |
| 127 | |
| 128 | static inline void dma_contiguous_set_default(struct cma *cma) { } |
| 129 | |
Marek Szyprowski | c64be2b | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 130 | static inline void dma_contiguous_reserve(phys_addr_t limit) { } |
| 131 | |
Marek Szyprowski | a254738 | 2013-07-29 14:31:45 +0200 | [diff] [blame] | 132 | static inline int dma_contiguous_reserve_area(phys_addr_t size, phys_addr_t base, |
Akinobu Mita | 5ea3b1b | 2014-06-04 16:06:54 -0700 | [diff] [blame] | 133 | phys_addr_t limit, struct cma **res_cma, |
| 134 | bool fixed) |
| 135 | { |
Marek Szyprowski | a254738 | 2013-07-29 14:31:45 +0200 | [diff] [blame] | 136 | return -ENOSYS; |
| 137 | } |
| 138 | |
Marek Szyprowski | c64be2b | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 139 | static inline |
Vitaly Andrianov | 4009793 | 2012-12-05 09:29:25 -0500 | [diff] [blame] | 140 | int dma_declare_contiguous(struct device *dev, phys_addr_t size, |
Marek Szyprowski | c64be2b | 2011-12-29 13:09:51 +0100 | [diff] [blame] | 141 | phys_addr_t base, phys_addr_t limit) |
| 142 | { |
| 143 | return -ENOSYS; |
| 144 | } |
| 145 | |
| 146 | static inline |
| 147 | struct page *dma_alloc_from_contiguous(struct device *dev, int count, |
| 148 | unsigned int order) |
| 149 | { |
| 150 | return NULL; |
| 151 | } |
| 152 | |
| 153 | static inline |
| 154 | bool dma_release_from_contiguous(struct device *dev, struct page *pages, |
| 155 | int count) |
| 156 | { |
| 157 | return false; |
| 158 | } |
| 159 | |
| 160 | #endif |
| 161 | |
| 162 | #endif |
| 163 | |
| 164 | #endif |