xref: /freebsd/sys/sys/vmem.h (revision 1ecf01065b45018de3901c8bf89d703af737feeb)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c)2006 YAMAMOTO Takashi,
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 /* From	$NetBSD: vmem.h,v 1.20 2013/01/29 21:26:24 para Exp $	*/
29 
30 
31 #ifndef _SYS_VMEM_H_
32 #define	_SYS_VMEM_H_
33 
34 #include <sys/types.h>
35 
36 typedef struct vmem vmem_t;
37 
38 typedef uintptr_t	vmem_addr_t;
39 typedef size_t		vmem_size_t;
40 
41 #define	VMEM_ADDR_MIN		0
42 #define	VMEM_ADDR_QCACHE_MIN	1
43 #define	VMEM_ADDR_MAX		(~(vmem_addr_t)0)
44 
45 /* vmem_size typemask */
46 #define VMEM_ALLOC	0x01
47 #define VMEM_FREE	0x02
48 #define VMEM_MAXFREE	0x10
49 
50 typedef int (vmem_import_t)(void *, vmem_size_t, int, vmem_addr_t *);
51 typedef void (vmem_release_t)(void *, vmem_addr_t, vmem_size_t);
52 typedef void (vmem_reclaim_t)(vmem_t *, int);
53 
54 #ifndef _KERNEL
55 #define	M_NOWAIT	0x0800		/* userspace hack */
56 #define	M_FIRSTFIT	0x1000		/* only for vmem, fast fit */
57 #define	M_BESTFIT	0x2000		/* only for vmem, low fragmentation */
58 #define	M_NEXTFIT	0x8000		/* only for vmem, follow cursor */
59 #endif
60 
61 __BEGIN_DECLS
62 
63 /*
64  * Create a vmem:
65  *	name		- Name of the region
66  *	base		- Initial span start (optional)
67  *	size		- Initial span size
68  *	quantum		- Natural unit of allocation (ie PAGE_SIZE, 1, etc)
69  *	qcache_max	- Maximum size to quantum cache.  This creates a UMA
70  *			  cache for each multiple of quantum up to qcache_max.
71  *	flags		- M_* flags
72  */
73 vmem_t *vmem_create(const char *name, vmem_addr_t base,
74     vmem_size_t size, vmem_size_t quantum, vmem_size_t qcache_max, int flags);
75 vmem_t *vmem_init(vmem_t *vm, const char *name, vmem_addr_t base,
76     vmem_size_t size, vmem_size_t quantum, vmem_size_t qcache_max, int flags);
77 void vmem_destroy(vmem_t *);
78 
79 /*
80  * Set callbacks for bringing in dynamic regions:
81  *	importfn	- Backing store import routine.
82  *	releasefn	- Backing store release routine.
83  *	arg		- Backing store argument
84  *	import_quantum	- Size to import from backing store
85  */
86 
87 void vmem_set_import(vmem_t *vm, vmem_import_t *importfn,
88     vmem_release_t *releasefn, void *arg, vmem_size_t import_quantum);
89 
90 /*
91  * Set a limit on the total size of a vmem.
92  */
93 
94 void vmem_set_limit(vmem_t *vm, vmem_size_t limit);
95 
96 /*
97  * Set a callback for reclaiming memory when space is exhausted:
98  */
99 void vmem_set_reclaim(vmem_t *vm, vmem_reclaim_t *reclaimfn);
100 
101 /*
102  * Allocate and free linear regions from a vmem.  Must specify
103  * BESTFIT or FIRSTFIT.  Free is non-blocking.  These routines
104  * respect the quantum caches.
105  */
106 int vmem_alloc(vmem_t *vm, vmem_size_t size, int flags, vmem_addr_t *addrp);
107 void vmem_free(vmem_t *vm, vmem_addr_t addr, vmem_size_t size);
108 
109 /*
110  * Constrained allocate and free routines.  These bypass the quantum cache.
111  *	size		- Size in units of 1, not quantum.
112  *	align		- Required alignment of the start of region
113  *	phase		- Offset from alignment
114  *	nocross		- Illegal boundary
115  *	minaddr		- Minimum allowed address for last byte
116  *	maxaddr		- Maximum allowed address for first byte
117  *	flags		- M_* flags
118  *	addrp		- result
119  */
120 int vmem_xalloc(vmem_t *vm, vmem_size_t size, vmem_size_t align,
121     vmem_size_t phase, vmem_size_t nocross, vmem_addr_t minaddr,
122     vmem_addr_t maxaddr, int flags, vmem_addr_t *addrp);
123 void vmem_xfree(vmem_t *vm, vmem_addr_t addr, vmem_size_t size);
124 
125 /*
126  * Add a static region to a vmem after create.  This won't be freed
127  * until the vmem is destroyed.
128  */
129 int vmem_add(vmem_t *vm, vmem_addr_t addr, vmem_size_t size, int flags);
130 
131 /*
132  * Given roundup size to the vmem's native quantum size.
133  */
134 vmem_size_t vmem_roundup_size(vmem_t *vm, vmem_size_t size);
135 
136 /*
137  * Report vmem utilization according to the requested type.
138  */
139 vmem_size_t vmem_size(vmem_t *vm, int typemask);
140 
141 void vmem_whatis(vmem_addr_t addr, int (*fn)(const char *, ...)
142     __printflike(1, 2));
143 void vmem_print(vmem_addr_t addr, const char *, int (*fn)(const char *, ...)
144     __printflike(1, 2));
145 void vmem_printall(const char *, int (*fn)(const char *, ...)
146     __printflike(1, 2));
147 void vmem_startup(void);
148 
149 __END_DECLS
150 
151 #endif /* !_SYS_VMEM_H_ */
152