xref: /freebsd/sys/compat/linuxkpi/common/include/linux/gfp.h (revision e532a99901e26d3961f8f5c0e4969fce86f09456)
1 /*-
2  * Copyright (c) 2010 Isilon Systems, Inc.
3  * Copyright (c) 2010 iX Systems, Inc.
4  * Copyright (c) 2010 Panasas, Inc.
5  * Copyright (c) 2013-2017 Mellanox Technologies, Ltd.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice unmodified, this list of conditions, and the following
13  *    disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * $FreeBSD$
30  */
31 #ifndef	_LINUX_GFP_H_
32 #define	_LINUX_GFP_H_
33 
34 #include <sys/cdefs.h>
35 #include <sys/types.h>
36 #include <sys/systm.h>
37 #include <sys/malloc.h>
38 
39 #include <linux/page.h>
40 
41 #include <vm/vm_param.h>
42 #include <vm/vm_object.h>
43 #include <vm/vm_extern.h>
44 #include <vm/vm_kern.h>
45 
46 #define	__GFP_NOWARN	0
47 #define	__GFP_HIGHMEM	0
48 #define	__GFP_ZERO	M_ZERO
49 #define	__GFP_NORETRY	0
50 #define	__GFP_RECLAIM   0
51 #define	__GFP_RECLAIMABLE   0
52 #define	__GFP_RETRY_MAYFAIL 0
53 #define	__GFP_MOVABLE	0
54 #define	__GFP_COMP	0
55 #define	__GFP_KSWAPD_RECLAIM 0
56 
57 #define	__GFP_IO	0
58 #define	__GFP_NO_KSWAPD	0
59 #define	__GFP_WAIT	M_WAITOK
60 #define	__GFP_DMA32	(1U << 24) /* LinuxKPI only */
61 #if defined(LINUXKPI_VERSION) && LINUXKPI_VERSION == 50000
62 #define	__GFP_NOTWIRED	(1U << 25)
63 #endif
64 #define	__GFP_BITS_SHIFT 25
65 #define	__GFP_BITS_MASK	((1 << __GFP_BITS_SHIFT) - 1)
66 #define	__GFP_NOFAIL	M_WAITOK
67 
68 #define	GFP_NOWAIT	M_NOWAIT
69 #define	GFP_ATOMIC	(M_NOWAIT | M_USE_RESERVE)
70 #define	GFP_KERNEL	M_WAITOK
71 #define	GFP_USER	M_WAITOK
72 #define	GFP_HIGHUSER	M_WAITOK
73 #define	GFP_HIGHUSER_MOVABLE	M_WAITOK
74 #define	GFP_IOFS	M_NOWAIT
75 #define	GFP_NOIO	M_NOWAIT
76 #define	GFP_DMA32	__GFP_DMA32
77 #define	GFP_TEMPORARY	M_NOWAIT
78 #define	GFP_NATIVE_MASK	(M_NOWAIT | M_WAITOK | M_USE_RESERVE | M_ZERO)
79 #define	GFP_TRANSHUGE	0
80 #define	GFP_TRANSHUGE_LIGHT	0
81 
82 CTASSERT((__GFP_DMA32 & GFP_NATIVE_MASK) == 0);
83 CTASSERT((__GFP_BITS_MASK & GFP_NATIVE_MASK) == GFP_NATIVE_MASK);
84 
85 /*
86  * Resolve a page into a virtual address:
87  *
88  * NOTE: This function only works for pages allocated by the kernel.
89  */
90 extern void *linux_page_address(struct page *);
91 
92 #define	page_address(page) linux_page_address(page)
93 
94 /*
95  * Page management for unmapped pages:
96  */
97 extern vm_page_t linux_alloc_pages(gfp_t flags, unsigned int order);
98 extern void linux_free_pages(vm_page_t page, unsigned int order);
99 
100 static inline struct page *
101 alloc_page(gfp_t flags)
102 {
103 
104 #ifdef __GFP_NOTWIRED
105 	flags |= __GFP_NOTWIRED;
106 #endif
107 	return (linux_alloc_pages(flags, 0));
108 }
109 
110 static inline struct page *
111 alloc_pages(gfp_t flags, unsigned int order)
112 {
113 
114 #ifdef __GFP_NOTWIRED
115 	flags |= __GFP_NOTWIRED;
116 #endif
117 	return (linux_alloc_pages(flags, order));
118 }
119 
120 static inline struct page *
121 alloc_pages_node(int node_id, gfp_t flags, unsigned int order)
122 {
123 
124 #ifdef __GFP_NOTWIRED
125 	flags |= __GFP_NOTWIRED;
126 #endif
127 	return (linux_alloc_pages(flags, order));
128 }
129 
130 static inline void
131 __free_pages(struct page *page, unsigned int order)
132 {
133 
134 	linux_free_pages(page, order);
135 }
136 
137 static inline void
138 __free_page(struct page *page)
139 {
140 
141 	linux_free_pages(page, 0);
142 }
143 
144 /*
145  * Page management for mapped pages:
146  */
147 extern vm_offset_t linux_alloc_kmem(gfp_t flags, unsigned int order);
148 extern void linux_free_kmem(vm_offset_t, unsigned int order);
149 
150 static inline vm_offset_t
151 get_zeroed_page(gfp_t flags)
152 {
153 
154 	return (linux_alloc_kmem(flags | __GFP_ZERO, 0));
155 }
156 
157 static inline vm_offset_t
158 __get_free_page(gfp_t flags)
159 {
160 
161 	return (linux_alloc_kmem(flags, 0));
162 }
163 
164 static inline vm_offset_t
165 __get_free_pages(gfp_t flags, unsigned int order)
166 {
167 
168 	return (linux_alloc_kmem(flags, order));
169 }
170 
171 static inline void
172 free_pages(uintptr_t addr, unsigned int order)
173 {
174 	if (addr == 0)
175 		return;
176 
177 	linux_free_kmem(addr, order);
178 }
179 
180 static inline void
181 free_page(uintptr_t addr)
182 {
183 	if (addr == 0)
184 		return;
185 
186 	linux_free_kmem(addr, 0);
187 }
188 
189 static inline bool
190 gfpflags_allow_blocking(const gfp_t gfp_flags)
191 {
192 	return ((gfp_flags & (M_WAITOK | M_NOWAIT)) == M_WAITOK);
193 }
194 
195 #define	SetPageReserved(page)	do { } while (0)	/* NOP */
196 #define	ClearPageReserved(page)	do { } while (0)	/* NOP */
197 
198 #endif	/* _LINUX_GFP_H_ */
199