1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1992, 1993
5 *	The Regents of the University of California.  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 * 3. Neither the name of the University nor the names of its contributors
16 *    may be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 *	@(#)vm_extern.h	8.2 (Berkeley) 1/12/94
32 * $FreeBSD$
33 */
34
35#ifndef _VM_EXTERN_H_
36#define	_VM_EXTERN_H_
37
38/*
39 * Is pa a multiple of alignment, which is a power-of-two?
40 */
41static inline bool
42vm_addr_align_ok(vm_paddr_t pa, u_long alignment)
43{
44	KASSERT(powerof2(alignment), ("%s: alignment is not a power of 2: %#lx",
45		__func__, alignment));
46	return ((pa & (alignment - 1)) == 0);
47}
48
49/*
50 * Do the first and last addresses of a range match in all bits except the ones
51 * in -boundary (a power-of-two)?  For boundary == 0, all addresses match.
52 */
53static inline bool
54vm_addr_bound_ok(vm_paddr_t pa, vm_paddr_t size, vm_paddr_t boundary)
55{
56	KASSERT(powerof2(boundary), ("%s: boundary is not a power of 2: %#jx",
57		__func__, (uintmax_t)boundary));
58	return (((pa ^ (pa + size - 1)) & -boundary) == 0);
59}
60
61static inline bool
62vm_addr_ok(vm_paddr_t pa, vm_paddr_t size, u_long alignment,
63	vm_paddr_t boundary)
64{
65	return (vm_addr_align_ok(pa, alignment) &&
66		vm_addr_bound_ok(pa, size, boundary));
67}
68
69#endif				/* !_VM_EXTERN_H_ */
70