xref: /linux/mm/cma.h (revision 4886cec0b283ff89c87c7b92f22e328439bf7b1f)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __MM_CMA_H__
3 #define __MM_CMA_H__
4 
5 #include <linux/atomic.h>
6 #include <linux/cma.h>
7 #include <linux/list.h>
8 #include <linux/mutex.h>
9 #include <linux/spinlock.h>
10 
11 #ifdef CONFIG_CMA_DEBUGFS
12 #include <linux/debugfs.h>
13 #endif
14 
15 #ifdef CONFIG_CMA_SYSFS
16 #include <linux/kobject.h>
17 
18 struct cma_kobject {
19 	struct kobject kobj;
20 	struct cma *cma;
21 };
22 #endif
23 
24 /*
25  * Multi-range support. This can be useful if the size of the allocation
26  * is not expected to be larger than the alignment (like with hugetlb_cma),
27  * and the total amount of memory requested, while smaller than the total
28  * amount of memory available, is large enough that it doesn't fit in a
29  * single physical memory range because of memory holes.
30  *
31  * Fields:
32  *   @base_pfn: physical address of range
33  *   @early_pfn: first PFN not reserved through cma_reserve_early
34  *   @count: size of range
35  *   @bitmap: bitmap of allocated (1 << order_per_bit)-sized chunks.
36  */
37 struct cma_memrange {
38 	unsigned long base_pfn;
39 	unsigned long count;
40 	union {
41 		unsigned long early_pfn;
42 		unsigned long *bitmap;
43 	};
44 #ifdef CONFIG_CMA_DEBUGFS
45 	struct debugfs_u32_array dfs_bitmap;
46 #endif
47 };
48 #define CMA_MAX_RANGES 8
49 
50 struct cma {
51 	unsigned long count;
52 	unsigned long available_count;
53 	unsigned int order_per_bit; /* Order of pages represented by one bit */
54 	spinlock_t lock;
55 	struct mutex alloc_mutex;
56 #ifdef CONFIG_CMA_DEBUGFS
57 	struct hlist_head mem_head;
58 	spinlock_t mem_head_lock;
59 #endif
60 	char name[CMA_MAX_NAME];
61 	int nranges;
62 	struct cma_memrange ranges[CMA_MAX_RANGES];
63 #ifdef CONFIG_CMA_SYSFS
64 	/* the number of CMA page successful allocations */
65 	atomic64_t nr_pages_succeeded;
66 	/* the number of CMA page allocation failures */
67 	atomic64_t nr_pages_failed;
68 	/* the number of CMA page released */
69 	atomic64_t nr_pages_released;
70 	/* kobject requires dynamic object */
71 	struct cma_kobject *cma_kobj;
72 #endif
73 	unsigned long flags;
74 	/* NUMA node (NUMA_NO_NODE if unspecified) */
75 	int nid;
76 };
77 
78 enum cma_flags {
79 	CMA_RESERVE_PAGES_ON_ERROR,
80 	CMA_ZONES_VALID,
81 	CMA_ZONES_INVALID,
82 	CMA_ACTIVATED,
83 };
84 
85 extern struct cma cma_areas[MAX_CMA_AREAS];
86 extern unsigned int cma_area_count;
87 
88 static inline unsigned long cma_bitmap_maxno(struct cma *cma,
89 		struct cma_memrange *cmr)
90 {
91 	return cmr->count >> cma->order_per_bit;
92 }
93 
94 #ifdef CONFIG_CMA_SYSFS
95 void cma_sysfs_account_success_pages(struct cma *cma, unsigned long nr_pages);
96 void cma_sysfs_account_fail_pages(struct cma *cma, unsigned long nr_pages);
97 void cma_sysfs_account_release_pages(struct cma *cma, unsigned long nr_pages);
98 #else
99 static inline void cma_sysfs_account_success_pages(struct cma *cma,
100 						   unsigned long nr_pages) {}
101 static inline void cma_sysfs_account_fail_pages(struct cma *cma,
102 						unsigned long nr_pages) {}
103 static inline void cma_sysfs_account_release_pages(struct cma *cma,
104 						   unsigned long nr_pages) {}
105 #endif
106 
107 #endif
108