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 	struct mutex alloc_mutex;
43 #ifdef CONFIG_CMA_DEBUGFS
44 	struct hlist_head mem_head;
45 	spinlock_t mem_head_lock;
46 #endif
47 	char name[CMA_MAX_NAME];
48 	int nranges;
49 	struct cma_memrange ranges[CMA_MAX_RANGES];
50 #ifdef CONFIG_CMA_SYSFS
51 	/* the number of CMA page successful allocations */
52 	atomic64_t nr_pages_succeeded;
53 	/* the number of CMA page allocation failures */
54 	atomic64_t nr_pages_failed;
55 	/* the number of CMA page released */
56 	atomic64_t nr_pages_released;
57 	/* kobject requires dynamic object */
58 	struct cma_kobject *cma_kobj;
59 #endif
60 	unsigned long flags;
61 	/* NUMA node (NUMA_NO_NODE if unspecified) */
62 	int nid;
63 };
64 
65 enum cma_flags {
66 	CMA_RESERVE_PAGES_ON_ERROR,
67 	CMA_ZONES_VALID,
68 	CMA_ZONES_INVALID,
69 	CMA_ACTIVATED,
70 };
71 
72 extern struct cma cma_areas[MAX_CMA_AREAS];
73 extern unsigned int cma_area_count;
74 
cma_bitmap_maxno(struct cma * cma,struct cma_memrange * cmr)75 static inline unsigned long cma_bitmap_maxno(struct cma *cma,
76 		struct cma_memrange *cmr)
77 {
78 	return cmr->count >> cma->order_per_bit;
79 }
80 
81 #ifdef CONFIG_CMA_SYSFS
82 void cma_sysfs_account_success_pages(struct cma *cma, unsigned long nr_pages);
83 void cma_sysfs_account_fail_pages(struct cma *cma, unsigned long nr_pages);
84 void cma_sysfs_account_release_pages(struct cma *cma, unsigned long nr_pages);
85 #else
cma_sysfs_account_success_pages(struct cma * cma,unsigned long nr_pages)86 static inline void cma_sysfs_account_success_pages(struct cma *cma,
87 						   unsigned long nr_pages) {};
cma_sysfs_account_fail_pages(struct cma * cma,unsigned long nr_pages)88 static inline void cma_sysfs_account_fail_pages(struct cma *cma,
89 						unsigned long nr_pages) {};
cma_sysfs_account_release_pages(struct cma * cma,unsigned long nr_pages)90 static inline void cma_sysfs_account_release_pages(struct cma *cma,
91 						   unsigned long nr_pages) {};
92 #endif
93 #endif
94