xref: /dflybsd-src/sys/dev/drm/include/linux/gfp.h (revision a85cb24f18e3804e75ab8bcda7692564d0563317)
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 
38*a85cb24fSFrançois Tigeot #define GFP_NOWAIT	(M_NOWAIT | M_CACHEALIGN)
39*a85cb24fSFrançois Tigeot #define GFP_ATOMIC	(M_NOWAIT | M_CACHEALIGN)
40*a85cb24fSFranç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
47a5da6ec1SFrançois Tigeot 
48183d4b4bSFrançois Tigeot #define __GFP_HIGHMEM		0u	/* No particular meaning on DragonFly */
49*a85cb24fSFrançois Tigeot #define __GFP_IO		0u	/* No particular meaning on DragonFly */
50*a85cb24fSFrançois Tigeot #define __GFP_RECLAIM		0u
51183d4b4bSFrançois Tigeot #define __GFP_RECLAIMABLE	0u
52183d4b4bSFrançois Tigeot #define __GFP_NOWARN		0u
53183d4b4bSFrançois Tigeot 
54183d4b4bSFrançois Tigeot #define __GFP_DMA32	0x10000u	/* XXX: MUST NOT collide with the M_XXX definitions */
55183d4b4bSFrançois Tigeot #define GFP_DMA32	__GFP_DMA32
56f43360b9SFrançois Tigeot 
57f0bba3d1SFrançois Tigeot static inline void __free_page(struct page *page)
589db5a7ceSFrançois Tigeot {
59f0bba3d1SFrançois Tigeot 	vm_page_free_contig((struct vm_page *)page, PAGE_SIZE);
609db5a7ceSFrançois Tigeot }
619db5a7ceSFrançois Tigeot 
62f0bba3d1SFrançois Tigeot static inline struct page * alloc_page(int flags)
63a5da6ec1SFrançois Tigeot {
64a5da6ec1SFrançois Tigeot 	vm_paddr_t high = ~0LLU;
65a5da6ec1SFrançois Tigeot 
66a5da6ec1SFrançois Tigeot 	if (flags & GFP_DMA32)
67a5da6ec1SFrançois Tigeot 		high = BUS_SPACE_MAXADDR_32BIT;
68a5da6ec1SFrançois Tigeot 
69f0bba3d1SFrançois Tigeot 	return (struct page *)vm_page_alloc_contig(0LLU, ~0LLU,
70a5da6ec1SFrançois Tigeot 			PAGE_SIZE, PAGE_SIZE, PAGE_SIZE,
71a5da6ec1SFrançois Tigeot 			VM_MEMATTR_DEFAULT);
72a5da6ec1SFrançois Tigeot }
73a5da6ec1SFrançois Tigeot 
74183d4b4bSFrançois Tigeot static inline bool
75183d4b4bSFrançois Tigeot gfpflags_allow_blocking(const gfp_t flags)
76183d4b4bSFrançois Tigeot {
77183d4b4bSFrançois Tigeot 	return (flags & M_WAITOK);
78183d4b4bSFrançois Tigeot }
79183d4b4bSFrançois Tigeot 
809d1b0c59SFrançois Tigeot /*
819d1b0c59SFrançois Tigeot  * Allocate multiple contiguous pages. The DragonFly code can only do
829d1b0c59SFrançois Tigeot  * multiple allocations via the free page reserve.  Linux does not appear
839d1b0c59SFrançois Tigeot  * to restrict the address space, so neither do we.
849d1b0c59SFrançois Tigeot  */
859d1b0c59SFrançois Tigeot static inline struct page *
869d1b0c59SFrançois Tigeot alloc_pages(gfp_t gfp_mask, unsigned int order)
879d1b0c59SFrançois Tigeot {
889d1b0c59SFrançois Tigeot 	size_t bytes = PAGE_SIZE << order;
899d1b0c59SFrançois Tigeot 	struct vm_page *pgs;
909d1b0c59SFrançois Tigeot 
919d1b0c59SFrançois Tigeot 	pgs = vm_page_alloc_contig(0LLU, ~0LLU, bytes, bytes, bytes,
929d1b0c59SFrançois Tigeot 				   VM_MEMATTR_DEFAULT);
939d1b0c59SFrançois Tigeot 
949d1b0c59SFrançois Tigeot 	return (struct page*)pgs;
959d1b0c59SFrançois Tigeot }
969d1b0c59SFrançois Tigeot 
979d1b0c59SFrançois Tigeot /*
989d1b0c59SFrançois Tigeot  * Free multiple contiguous pages
999d1b0c59SFrançois Tigeot  */
1009d1b0c59SFrançois Tigeot static inline void
1019d1b0c59SFrançois Tigeot __free_pages(struct page *pgs, unsigned int order)
1029d1b0c59SFrançois Tigeot {
1039d1b0c59SFrançois Tigeot 	size_t bytes = PAGE_SIZE << order;
1049d1b0c59SFrançois Tigeot 
1059d1b0c59SFrançois Tigeot 	vm_page_free_contig((struct vm_page *)pgs, bytes);
1069d1b0c59SFrançois Tigeot }
1079d1b0c59SFrançois Tigeot 
108f43360b9SFrançois Tigeot #endif	/* _LINUX_GFP_H_ */
109