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