xref: /freebsd/tests/sys/kern/subr_physmem_test.c (revision 4e579ad047720775ab580b74192c7de8a3386fea)
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 #include <sys/param.h>
32 #include <sys/physmem.h>
33 
34 #include <atf-c.h>
35 
36 ATF_TC_WITHOUT_HEAD(hwregion);
37 ATF_TC_BODY(hwregion, tc)
38 {
39 	vm_paddr_t avail[4];
40 	size_t len;
41 
42 	physmem_hardware_region(2 * PAGE_SIZE, PAGE_SIZE);
43 
44 	len = physmem_avail(avail, 4);
45 	ATF_CHECK_EQ(len, 2);
46 	ATF_CHECK_EQ(avail[0], 2 * PAGE_SIZE);
47 	ATF_CHECK_EQ(avail[1], 3 * PAGE_SIZE);
48 
49 	/* Add an overlap */
50 	physmem_hardware_region(2 * PAGE_SIZE, 2 * PAGE_SIZE);
51 	len = physmem_avail(avail, 4);
52 	ATF_CHECK_EQ(len, 2);
53 	ATF_CHECK_EQ(avail[0], 2 * PAGE_SIZE);
54 	ATF_CHECK_EQ(avail[1], 4 * PAGE_SIZE);
55 
56 	/* Add a touching region */
57 	physmem_hardware_region(4 * PAGE_SIZE, PAGE_SIZE);
58 	len = physmem_avail(avail, 4);
59 	ATF_CHECK_EQ(len, 2);
60 	ATF_CHECK_EQ(avail[0], 2 * PAGE_SIZE);
61 	ATF_CHECK_EQ(avail[1], 5 * PAGE_SIZE);
62 
63 	/* Add a partial overlap */
64 	physmem_hardware_region(4 * PAGE_SIZE, 2 * PAGE_SIZE);
65 	len = physmem_avail(avail, 4);
66 	ATF_CHECK_EQ(len, 2);
67 	ATF_CHECK_EQ(avail[0], 2 * PAGE_SIZE);
68 	ATF_CHECK_EQ(avail[1], 6 * PAGE_SIZE);
69 
70 	/* Add a non-page aligned section */
71 	physmem_hardware_region(6 * PAGE_SIZE, PAGE_SIZE / 2);
72 	len = physmem_avail(avail, 4);
73 	ATF_CHECK_EQ(len, 2);
74 	ATF_CHECK_EQ(avail[0], 2 * PAGE_SIZE);
75 	ATF_CHECK_EQ(avail[1], 6 * PAGE_SIZE);
76 
77 	/* Add the remaining part of the page */
78 	physmem_hardware_region(6 * PAGE_SIZE + PAGE_SIZE / 2, PAGE_SIZE / 2);
79 	len = physmem_avail(avail, 4);
80 	ATF_CHECK_EQ(len, 2);
81 	ATF_CHECK_EQ(avail[0], 2 * PAGE_SIZE);
82 	ATF_CHECK_EQ(avail[1], 7 * PAGE_SIZE);
83 }
84 
85 ATF_TC_WITHOUT_HEAD(hwregion_exclude);
86 ATF_TC_BODY(hwregion_exclude, tc)
87 {
88 	vm_paddr_t avail[6];
89 	size_t len;
90 
91 	physmem_hardware_region(2 * PAGE_SIZE, 5 * PAGE_SIZE);
92 	physmem_exclude_region(4 * PAGE_SIZE, PAGE_SIZE, EXFLAG_NOALLOC);
93 
94 	len = physmem_avail(avail, 6);
95 	ATF_CHECK_EQ(len, 4);
96 	ATF_CHECK_EQ(avail[0], 2 * PAGE_SIZE);
97 	ATF_CHECK_EQ(avail[1], 4 * PAGE_SIZE);
98 	ATF_CHECK_EQ(avail[2], 5 * PAGE_SIZE);
99 	ATF_CHECK_EQ(avail[3], 7 * PAGE_SIZE);
100 
101 	/* Check mis-aligned/sized excluded regions work */
102 	physmem_exclude_region(4 * PAGE_SIZE - 1, PAGE_SIZE + 2,
103 	    EXFLAG_NOALLOC);
104 	len = physmem_avail(avail, 6);
105 	ATF_CHECK_EQ(len, 4);
106 	ATF_CHECK_EQ(avail[0], 2 * PAGE_SIZE);
107 	ATF_CHECK_EQ(avail[1], 3 * PAGE_SIZE);
108 	ATF_CHECK_EQ(avail[2], 6 * PAGE_SIZE);
109 	ATF_CHECK_EQ(avail[3], 7 * PAGE_SIZE);
110 }
111 
112 ATF_TC_WITHOUT_HEAD(hwregion_unordered);
113 ATF_TC_BODY(hwregion_unordered, tc)
114 {
115 	vm_paddr_t avail[4];
116 	size_t len;
117 
118 	/* Add a partial page */
119 	physmem_hardware_region(PAGE_SIZE, PAGE_SIZE / 2);
120 	/* Add a full page not touching the previous */
121 	physmem_hardware_region( 2 * PAGE_SIZE, PAGE_SIZE);
122 	/* Add the remainder of the first page */
123 	physmem_hardware_region(PAGE_SIZE + PAGE_SIZE / 2, PAGE_SIZE / 2);
124 
125 	len = physmem_avail(avail, 4);
126 	ATF_CHECK_EQ(len, 2);
127 	ATF_CHECK_EQ(avail[0], PAGE_SIZE);
128 	ATF_CHECK_EQ(avail[1], 3 * PAGE_SIZE);
129 }
130 
131 ATF_TP_ADD_TCS(tp)
132 {
133 
134 	ATF_TP_ADD_TC(tp, hwregion);
135 	ATF_TP_ADD_TC(tp, hwregion_exclude);
136 	ATF_TP_ADD_TC(tp, hwregion_unordered);
137 	return (atf_no_error());
138 }
139