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