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