xref: /freebsd/tests/sys/kern/subr_physmem_test.c (revision ebacd8013fe5f7fdf9f6a5b286f6680dd2891036)
1 /*-
2  * Copyright (c) 2021 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by Andrew Turner under
6  * sponsorship from the FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/physmem.h>
35 
36 #include <atf-c.h>
37 
38 ATF_TC_WITHOUT_HEAD(hwregion);
39 ATF_TC_BODY(hwregion, tc)
40 {
41 	vm_paddr_t avail[4];
42 	size_t len;
43 
44 	physmem_hardware_region(2 * PAGE_SIZE, PAGE_SIZE);
45 
46 	len = physmem_avail(avail, 4);
47 	ATF_CHECK_EQ(len, 2);
48 	ATF_CHECK_EQ(avail[0], 2 * PAGE_SIZE);
49 	ATF_CHECK_EQ(avail[1], 3 * PAGE_SIZE);
50 
51 	/* Add an overlap */
52 	physmem_hardware_region(2 * PAGE_SIZE, 2 * PAGE_SIZE);
53 	len = physmem_avail(avail, 4);
54 	ATF_CHECK_EQ(len, 2);
55 	ATF_CHECK_EQ(avail[0], 2 * PAGE_SIZE);
56 	ATF_CHECK_EQ(avail[1], 4 * PAGE_SIZE);
57 
58 	/* Add a touching region */
59 	physmem_hardware_region(4 * PAGE_SIZE, PAGE_SIZE);
60 	len = physmem_avail(avail, 4);
61 	ATF_CHECK_EQ(len, 2);
62 	ATF_CHECK_EQ(avail[0], 2 * PAGE_SIZE);
63 	ATF_CHECK_EQ(avail[1], 5 * PAGE_SIZE);
64 
65 	/* Add a partial overlap */
66 	physmem_hardware_region(4 * PAGE_SIZE, 2 * PAGE_SIZE);
67 	len = physmem_avail(avail, 4);
68 	ATF_CHECK_EQ(len, 2);
69 	ATF_CHECK_EQ(avail[0], 2 * PAGE_SIZE);
70 	ATF_CHECK_EQ(avail[1], 6 * PAGE_SIZE);
71 
72 	/* Add a non-page aligned section */
73 	physmem_hardware_region(6 * PAGE_SIZE, PAGE_SIZE / 2);
74 	len = physmem_avail(avail, 4);
75 	ATF_CHECK_EQ(len, 2);
76 	ATF_CHECK_EQ(avail[0], 2 * PAGE_SIZE);
77 	ATF_CHECK_EQ(avail[1], 6 * PAGE_SIZE);
78 
79 	/* Add the remaining part of the page */
80 	physmem_hardware_region(6 * PAGE_SIZE + PAGE_SIZE / 2, PAGE_SIZE / 2);
81 	len = physmem_avail(avail, 4);
82 	ATF_CHECK_EQ(len, 2);
83 	ATF_CHECK_EQ(avail[0], 2 * PAGE_SIZE);
84 	ATF_CHECK_EQ(avail[1], 7 * PAGE_SIZE);
85 }
86 
87 ATF_TC_WITHOUT_HEAD(hwregion_exclude);
88 ATF_TC_BODY(hwregion_exclude, tc)
89 {
90 	vm_paddr_t avail[6];
91 	size_t len;
92 
93 	physmem_hardware_region(2 * PAGE_SIZE, 5 * PAGE_SIZE);
94 	physmem_exclude_region(4 * PAGE_SIZE, PAGE_SIZE, EXFLAG_NOALLOC);
95 
96 	len = physmem_avail(avail, 6);
97 	ATF_CHECK_EQ(len, 4);
98 	ATF_CHECK_EQ(avail[0], 2 * PAGE_SIZE);
99 	ATF_CHECK_EQ(avail[1], 4 * PAGE_SIZE);
100 	ATF_CHECK_EQ(avail[2], 5 * PAGE_SIZE);
101 	ATF_CHECK_EQ(avail[3], 7 * PAGE_SIZE);
102 
103 	/* Check mis-aligned/sized excluded regions work */
104 	physmem_exclude_region(4 * PAGE_SIZE - 1, PAGE_SIZE + 2,
105 	    EXFLAG_NOALLOC);
106 	len = physmem_avail(avail, 6);
107 	ATF_CHECK_EQ(len, 4);
108 	ATF_CHECK_EQ(avail[0], 2 * PAGE_SIZE);
109 	ATF_CHECK_EQ(avail[1], 3 * PAGE_SIZE);
110 	ATF_CHECK_EQ(avail[2], 6 * PAGE_SIZE);
111 	ATF_CHECK_EQ(avail[3], 7 * PAGE_SIZE);
112 }
113 
114 ATF_TC_WITHOUT_HEAD(hwregion_unordered);
115 ATF_TC_BODY(hwregion_unordered, tc)
116 {
117 	vm_paddr_t avail[4];
118 	size_t len;
119 
120 	/* Add a partial page */
121 	physmem_hardware_region(PAGE_SIZE, PAGE_SIZE / 2);
122 	/* Add a full page not touching the previous */
123 	physmem_hardware_region( 2 * PAGE_SIZE, PAGE_SIZE);
124 	/* Add the remainder of the first page */
125 	physmem_hardware_region(PAGE_SIZE + PAGE_SIZE / 2, PAGE_SIZE / 2);
126 
127 	len = physmem_avail(avail, 4);
128 	ATF_CHECK_EQ(len, 2);
129 	ATF_CHECK_EQ(avail[0], PAGE_SIZE);
130 	ATF_CHECK_EQ(avail[1], 3 * PAGE_SIZE);
131 }
132 
133 ATF_TP_ADD_TCS(tp)
134 {
135 
136 	ATF_TP_ADD_TC(tp, hwregion);
137 	ATF_TP_ADD_TC(tp, hwregion_exclude);
138 	ATF_TP_ADD_TC(tp, hwregion_unordered);
139 	return (atf_no_error());
140 }
141