xref: /freebsd/sys/sys/vmem.h (revision 32c7dde816fd1d738a48af82bf490307cb7b4739)
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 /* $FreeBSD$ */
31 
32 #ifndef _SYS_VMEM_H_
33 #define	_SYS_VMEM_H_
34 
35 #include <sys/types.h>
36 
37 #ifdef _KERNEL
38 
39 typedef struct vmem vmem_t;
40 
41 typedef uintptr_t	vmem_addr_t;
42 typedef size_t		vmem_size_t;
43 
44 #define	VMEM_ADDR_MIN		0
45 #define	VMEM_ADDR_QCACHE_MIN	1
46 #define	VMEM_ADDR_MAX		(~(vmem_addr_t)0)
47 
48 typedef int (vmem_import_t)(void *, vmem_size_t, int, vmem_addr_t *);
49 typedef void (vmem_release_t)(void *, vmem_addr_t, vmem_size_t);
50 typedef void (vmem_reclaim_t)(vmem_t *, int);
51 
52 /*
53  * Create a vmem:
54  *	name		- Name of the region
55  *	base		- Initial span start (optional)
56  *	size		- Initial span size
57  *	quantum		- Natural unit of allocation (ie PAGE_SIZE, 1, etc)
58  *	qcache_max	- Maximum size to quantum cache.  This creates a UMA
59  *			  cache for each multiple of quantum up to qcache_max.
60  *	flags		- M_* flags
61  */
62 vmem_t *vmem_create(const char *name, vmem_addr_t base,
63     vmem_size_t size, vmem_size_t quantum, vmem_size_t qcache_max, int flags);
64 vmem_t *vmem_init(vmem_t *vm, const char *name, vmem_addr_t base,
65     vmem_size_t size, vmem_size_t quantum, vmem_size_t qcache_max, int flags);
66 void vmem_destroy(vmem_t *);
67 
68 /*
69  * Set callbacks for bringing in dynamic regions:
70  *	importfn	- Backing store import routine.
71  *	releasefn	- Backing store release routine.
72  *	arg		- Backing store argument
73  *	import_quantum	- Size to import from backing store
74  */
75 
76 void vmem_set_import(vmem_t *vm, vmem_import_t *importfn,
77     vmem_release_t *releasefn, void *arg, vmem_size_t import_quantum);
78 
79 /*
80  * Set a limit on the total size of a vmem.
81  */
82 
83 void vmem_set_limit(vmem_t *vm, vmem_size_t limit);
84 
85 /*
86  * Set a callback for reclaiming memory when space is exhausted:
87  */
88 void vmem_set_reclaim(vmem_t *vm, vmem_reclaim_t *reclaimfn);
89 
90 /*
91  * Allocate and free linear regions from a vmem.  Must specify
92  * BESTFIT or FIRSTFIT.  Free is non-blocking.  These routines
93  * respect the quantum caches.
94  */
95 int vmem_alloc(vmem_t *vm, vmem_size_t size, int flags, vmem_addr_t *addrp);
96 void vmem_free(vmem_t *vm, vmem_addr_t addr, vmem_size_t size);
97 
98 /*
99  * Constrained allocate and free routines.  These bypass the quantum cache.
100  *	size		- Size in units of 1, not quantum.
101  *	align		- Required alignment of the start of region
102  *	phase		- Offset from alignment
103  *	nocross		- Illegal boundary
104  *	minaddr		- Minimum allowed address for last byte
105  *	maxaddr		- Maximum allowed address for first byte
106  *	flags		- M_* flags
107  *	addrp		- result
108  */
109 int vmem_xalloc(vmem_t *vm, vmem_size_t size, vmem_size_t align,
110     vmem_size_t phase, vmem_size_t nocross, vmem_addr_t minaddr,
111     vmem_addr_t maxaddr, int flags, vmem_addr_t *addrp);
112 void vmem_xfree(vmem_t *vm, vmem_addr_t addr, vmem_size_t size);
113 
114 /*
115  * Add a static region to a vmem after create.  This won't be freed
116  * until the vmem is destroyed.
117  */
118 int vmem_add(vmem_t *vm, vmem_addr_t addr, vmem_size_t size, int flags);
119 
120 /*
121  * Given roundup size to the vmem's native quantum size.
122  */
123 vmem_size_t vmem_roundup_size(vmem_t *vm, vmem_size_t size);
124 
125 /*
126  * Report vmem utilization according to the requested type.
127  */
128 vmem_size_t vmem_size(vmem_t *vm, int typemask);
129 
130 void vmem_whatis(vmem_addr_t addr, int (*fn)(const char *, ...)
131     __printflike(1, 2));
132 void vmem_print(vmem_addr_t addr, const char *, int (*fn)(const char *, ...)
133     __printflike(1, 2));
134 void vmem_printall(const char *, int (*fn)(const char *, ...)
135     __printflike(1, 2));
136 void vmem_startup(void);
137 
138 /* vmem_size typemask */
139 #define VMEM_ALLOC	0x01
140 #define VMEM_FREE	0x02
141 #define VMEM_MAXFREE	0x10
142 
143 #endif /* _KERNEL */
144 
145 #endif /* !_SYS_VMEM_H_ */
146