xref: /dflybsd-src/sys/dev/drm/include/linux/gfp.h (revision 3f2dd94a569761201b5b0a18b2f697f97fe1b9dc)
1f43360b9SFrançois Tigeot /*
2183d4b4bSFrançois Tigeot  * Copyright (c) 2015-2020 François Tigeot <ftigeot@wolfpond.org>
3f43360b9SFrançois Tigeot  * All rights reserved.
4f43360b9SFrançois Tigeot  *
5f43360b9SFrançois Tigeot  * Redistribution and use in source and binary forms, with or without
6f43360b9SFrançois Tigeot  * modification, are permitted provided that the following conditions
7f43360b9SFrançois Tigeot  * are met:
8f43360b9SFrançois Tigeot  * 1. Redistributions of source code must retain the above copyright
9f43360b9SFrançois Tigeot  *    notice unmodified, this list of conditions, and the following
10f43360b9SFrançois Tigeot  *    disclaimer.
11f43360b9SFrançois Tigeot  * 2. Redistributions in binary form must reproduce the above copyright
12f43360b9SFrançois Tigeot  *    notice, this list of conditions and the following disclaimer in the
13f43360b9SFrançois Tigeot  *    documentation and/or other materials provided with the distribution.
14f43360b9SFrançois Tigeot  *
15f43360b9SFrançois Tigeot  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16f43360b9SFrançois Tigeot  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17f43360b9SFrançois Tigeot  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18f43360b9SFrançois Tigeot  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19f43360b9SFrançois Tigeot  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20f43360b9SFrançois Tigeot  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21f43360b9SFrançois Tigeot  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22f43360b9SFrançois Tigeot  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23f43360b9SFrançois Tigeot  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24f43360b9SFrançois Tigeot  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25f43360b9SFrançois Tigeot  */
26f43360b9SFrançois Tigeot 
27f43360b9SFrançois Tigeot #ifndef _LINUX_GFP_H_
28f43360b9SFrançois Tigeot #define _LINUX_GFP_H_
29f43360b9SFrançois Tigeot 
30f0bba3d1SFrançois Tigeot #include <linux/mmdebug.h>
31f0bba3d1SFrançois Tigeot #include <linux/mmzone.h>
32f0bba3d1SFrançois Tigeot #include <linux/stddef.h>
33f0bba3d1SFrançois Tigeot 
348b44ed4dSzrj #include <sys/malloc.h>
359db5a7ceSFrançois Tigeot #include <vm/vm_page.h>
36a5da6ec1SFrançois Tigeot #include <machine/bus_dma.h>
379db5a7ceSFrançois Tigeot 
38a85cb24fSFrançois Tigeot #define GFP_NOWAIT	(M_NOWAIT | M_CACHEALIGN)
39a85cb24fSFrançois Tigeot #define GFP_ATOMIC	(M_NOWAIT | M_CACHEALIGN)
40a85cb24fSFrançois Tigeot #define GFP_KERNEL	(M_WAITOK | M_CACHEALIGN)
41cfe46a34SFrançois Tigeot #define GFP_TEMPORARY	GFP_KERNEL
42cfe46a34SFrançois Tigeot #define GFP_USER	GFP_KERNEL
43cfe46a34SFrançois Tigeot #define GFP_HIGHUSER	GFP_KERNEL
449e08aba5SFrançois Tigeot 
45a5da6ec1SFrançois Tigeot #define __GFP_ZERO		M_ZERO
469d1b0c59SFrançois Tigeot #define __GFP_NORETRY		M_NULLOK
47*3f2dd94aSFrançois Tigeot #define __GFP_RETRY_MAYFAIL	M_NULLOK
48a5da6ec1SFrançois Tigeot 
49183d4b4bSFrançois Tigeot #define __GFP_HIGHMEM		0u	/* No particular meaning on DragonFly */
50a85cb24fSFrançois Tigeot #define __GFP_IO		0u	/* No particular meaning on DragonFly */
51a85cb24fSFrançois Tigeot #define __GFP_RECLAIM		0u
52183d4b4bSFrançois Tigeot #define __GFP_RECLAIMABLE	0u
53183d4b4bSFrançois Tigeot #define __GFP_NOWARN		0u
54*3f2dd94aSFrançois Tigeot #define __GFP_NOFAIL		0u
55183d4b4bSFrançois Tigeot 
56183d4b4bSFrançois Tigeot #define __GFP_DMA32	0x10000u	/* XXX: MUST NOT collide with the M_XXX definitions */
57183d4b4bSFrançois Tigeot #define GFP_DMA32	__GFP_DMA32
58f43360b9SFrançois Tigeot 
__free_page(struct page * page)59f0bba3d1SFrançois Tigeot static inline void __free_page(struct page *page)
609db5a7ceSFrançois Tigeot {
61f0bba3d1SFrançois Tigeot 	vm_page_free_contig((struct vm_page *)page, PAGE_SIZE);
629db5a7ceSFrançois Tigeot }
639db5a7ceSFrançois Tigeot 
alloc_page(int flags)64f0bba3d1SFrançois Tigeot static inline struct page * alloc_page(int flags)
65a5da6ec1SFrançois Tigeot {
66a5da6ec1SFrançois Tigeot 	vm_paddr_t high = ~0LLU;
67a5da6ec1SFrançois Tigeot 
68a5da6ec1SFrançois Tigeot 	if (flags & GFP_DMA32)
69a5da6ec1SFrançois Tigeot 		high = BUS_SPACE_MAXADDR_32BIT;
70a5da6ec1SFrançois Tigeot 
71f0bba3d1SFrançois Tigeot 	return (struct page *)vm_page_alloc_contig(0LLU, ~0LLU,
72a5da6ec1SFrançois Tigeot 			PAGE_SIZE, PAGE_SIZE, PAGE_SIZE,
73a5da6ec1SFrançois Tigeot 			VM_MEMATTR_DEFAULT);
74a5da6ec1SFrançois Tigeot }
75a5da6ec1SFrançois Tigeot 
76183d4b4bSFrançois Tigeot static inline bool
gfpflags_allow_blocking(const gfp_t flags)77183d4b4bSFrançois Tigeot gfpflags_allow_blocking(const gfp_t flags)
78183d4b4bSFrançois Tigeot {
79183d4b4bSFrançois Tigeot 	return (flags & M_WAITOK);
80183d4b4bSFrançois Tigeot }
81183d4b4bSFrançois Tigeot 
829d1b0c59SFrançois Tigeot /*
839d1b0c59SFrançois Tigeot  * Allocate multiple contiguous pages. The DragonFly code can only do
849d1b0c59SFrançois Tigeot  * multiple allocations via the free page reserve.  Linux does not appear
859d1b0c59SFrançois Tigeot  * to restrict the address space, so neither do we.
869d1b0c59SFrançois Tigeot  */
879d1b0c59SFrançois Tigeot static inline struct page *
alloc_pages(gfp_t gfp_mask,unsigned int order)889d1b0c59SFrançois Tigeot alloc_pages(gfp_t gfp_mask, unsigned int order)
899d1b0c59SFrançois Tigeot {
909d1b0c59SFrançois Tigeot 	size_t bytes = PAGE_SIZE << order;
919d1b0c59SFrançois Tigeot 	struct vm_page *pgs;
929d1b0c59SFrançois Tigeot 
939d1b0c59SFrançois Tigeot 	pgs = vm_page_alloc_contig(0LLU, ~0LLU, bytes, bytes, bytes,
949d1b0c59SFrançois Tigeot 				   VM_MEMATTR_DEFAULT);
959d1b0c59SFrançois Tigeot 
969d1b0c59SFrançois Tigeot 	return (struct page*)pgs;
979d1b0c59SFrançois Tigeot }
989d1b0c59SFrançois Tigeot 
999d1b0c59SFrançois Tigeot /*
1009d1b0c59SFrançois Tigeot  * Free multiple contiguous pages
1019d1b0c59SFrançois Tigeot  */
1029d1b0c59SFrançois Tigeot static inline void
__free_pages(struct page * pgs,unsigned int order)1039d1b0c59SFrançois Tigeot __free_pages(struct page *pgs, unsigned int order)
1049d1b0c59SFrançois Tigeot {
1059d1b0c59SFrançois Tigeot 	size_t bytes = PAGE_SIZE << order;
1069d1b0c59SFrançois Tigeot 
1079d1b0c59SFrançois Tigeot 	vm_page_free_contig((struct vm_page *)pgs, bytes);
1089d1b0c59SFrançois Tigeot }
1099d1b0c59SFrançois Tigeot 
110f43360b9SFrançois Tigeot #endif	/* _LINUX_GFP_H_ */
111