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