1 /* Copyright 2019 IBM Corp.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * 	http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12  * implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <skiboot.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <stdbool.h>
21 
22 /* Override this for testing. */
23 #define is_rodata(p) fake_is_rodata(p)
24 
25 char __rodata_start[16];
26 #define __rodata_end (__rodata_start + sizeof(__rodata_start))
27 
fake_is_rodata(const void * p)28 static inline bool fake_is_rodata(const void *p)
29 {
30 	return ((char *)p >= __rodata_start && (char *)p < __rodata_end);
31 }
32 
33 #define zalloc(bytes) calloc((bytes), 1)
34 
35 #include "../device.c"
36 #include <assert.h>
37 #include "../../test/dt_common.c"
38 
39 #define __TEST__
40 
41 static inline unsigned long mfspr(unsigned int spr);
42 
43 #include <ccan/str/str.c>
44 
45 #include "../cpufeatures.c"
46 
47 static unsigned long fake_pvr = PVR_TYPE_P8;
48 
mfspr(unsigned int spr)49 static inline unsigned long mfspr(unsigned int spr)
50 {
51 	assert(spr == SPR_PVR);
52 	return fake_pvr;
53 }
54 
main(void)55 int main(void)
56 {
57 	struct dt_node *dt_root;
58 
59 	dt_root = dt_new_root("");
60 	dt_add_cpufeatures(dt_root);
61 	dump_dt(dt_root, 0, true);
62 	dt_free(dt_root);
63 
64 	fake_pvr = (PVR_TYPE_P8E << 16) | 0x100; // P8E DD1.0
65 	dt_root = dt_new_root("");
66 	dt_add_cpufeatures(dt_root);
67 	dump_dt(dt_root, 0, false);
68 	assert(dt_find_by_path(dt_root, "cpus/ibm,powerpc-cpu-features/mmu-radix") == 0);
69 	assert(dt_find_by_path(dt_root, "cpus/ibm,powerpc-cpu-features/tm-suspend-hypervisor-assist") == 0);
70 	assert(dt_find_by_path(dt_root, "cpus/ibm,powerpc-cpu-features/tm-suspend-xer-so-bug") == 0);
71 	dt_free(dt_root);
72 
73 	fake_pvr = (PVR_TYPE_P8E << 16) | 0x200; // P8E DD2.0
74 	dt_root = dt_new_root("");
75 	dt_add_cpufeatures(dt_root);
76 	dump_dt(dt_root, 0, false);
77 	assert(dt_find_by_path(dt_root, "cpus/ibm,powerpc-cpu-features/mmu-radix") == 0);
78 	assert(dt_find_by_path(dt_root, "cpus/ibm,powerpc-cpu-features/tm-suspend-hypervisor-assist") == 0);
79 	assert(dt_find_by_path(dt_root, "cpus/ibm,powerpc-cpu-features/tm-suspend-xer-so-bug") == 0);
80 	dt_free(dt_root);
81 
82 	fake_pvr = (PVR_TYPE_P8 << 16) | 0x100; // P8 DD1.0
83 	dt_root = dt_new_root("");
84 	dt_add_cpufeatures(dt_root);
85 	dump_dt(dt_root, 0, false);
86 	assert(dt_find_by_path(dt_root, "cpus/ibm,powerpc-cpu-features/mmu-radix") == 0);
87 	assert(dt_find_by_path(dt_root, "cpus/ibm,powerpc-cpu-features/tm-suspend-hypervisor-assist") == 0);
88 	assert(dt_find_by_path(dt_root, "cpus/ibm,powerpc-cpu-features/tm-suspend-xer-so-bug") == 0);
89 	dt_free(dt_root);
90 
91 	fake_pvr = (PVR_TYPE_P8 << 16) | 0x200; // P8 DD2.0
92 	dt_root = dt_new_root("");
93 	dt_add_cpufeatures(dt_root);
94 	dump_dt(dt_root, 0, false);
95 	assert(dt_find_by_path(dt_root, "cpus/ibm,powerpc-cpu-features/mmu-radix") == 0);
96 	assert(dt_find_by_path(dt_root, "cpus/ibm,powerpc-cpu-features/tm-suspend-hypervisor-assist") == 0);
97 	assert(dt_find_by_path(dt_root, "cpus/ibm,powerpc-cpu-features/tm-suspend-xer-so-bug") == 0);
98 	dt_free(dt_root);
99 
100 	fake_pvr = (PVR_TYPE_P8NVL << 16) | 0x100; // P8NVL DD1.0
101 	dt_root = dt_new_root("");
102 	dt_add_cpufeatures(dt_root);
103 	dump_dt(dt_root, 0, false);
104 	assert(dt_find_by_path(dt_root, "cpus/ibm,powerpc-cpu-features/mmu-radix") == 0);
105 	assert(dt_find_by_path(dt_root, "cpus/ibm,powerpc-cpu-features/tm-suspend-hypervisor-assist") == 0);
106 	assert(dt_find_by_path(dt_root, "cpus/ibm,powerpc-cpu-features/tm-suspend-xer-so-bug") == 0);
107 	dt_free(dt_root);
108 
109 	fake_pvr = (PVR_TYPE_P9 << 16) | 0x200; // P9 DD2.0
110 	dt_root = dt_new_root("");
111 	dt_add_cpufeatures(dt_root);
112 	dump_dt(dt_root, 0, false);
113 	assert(dt_find_by_path(dt_root, "cpus/ibm,powerpc-cpu-features/mmu-radix"));
114 	assert(dt_find_by_path(dt_root, "cpus/ibm,powerpc-cpu-features/tm-suspend-hypervisor-assist") == 0);
115 	assert(dt_find_by_path(dt_root, "cpus/ibm,powerpc-cpu-features/tm-suspend-xer-so-bug") == 0);
116 	dt_free(dt_root);
117 
118 	fake_pvr = (PVR_TYPE_P9 << 16) | 0x201; // P9 DD2.1
119 	dt_root = dt_new_root("");
120 	dt_add_cpufeatures(dt_root);
121 	dump_dt(dt_root, 0, false);
122 	assert(dt_find_by_path(dt_root, "cpus/ibm,powerpc-cpu-features/mmu-radix"));
123 	assert(dt_find_by_path(dt_root, "cpus/ibm,powerpc-cpu-features/tm-suspend-hypervisor-assist") == 0);
124 	assert(dt_find_by_path(dt_root, "cpus/ibm,powerpc-cpu-features/tm-suspend-xer-so-bug") == 0);
125 	dt_free(dt_root);
126 
127 	fake_pvr = (PVR_TYPE_P9 << 16) | 0x202; // P9 DD2.2
128 	dt_root = dt_new_root("");
129 	dt_add_cpufeatures(dt_root);
130 	dump_dt(dt_root, 0, false);
131 	assert(dt_find_by_path(dt_root, "cpus/ibm,powerpc-cpu-features/mmu-radix"));
132 	assert(dt_find_by_path(dt_root, "cpus/ibm,powerpc-cpu-features/tm-suspend-hypervisor-assist") != 0);
133 	assert(dt_find_by_path(dt_root, "cpus/ibm,powerpc-cpu-features/tm-suspend-xer-so-bug") != 0);
134 	dt_free(dt_root);
135 
136 	fake_pvr = (PVR_TYPE_P9 << 16) | 0x203; // P9 DD2.3
137 	dt_root = dt_new_root("");
138 	dt_add_cpufeatures(dt_root);
139 	dump_dt(dt_root, 0, false);
140 	assert(dt_find_by_path(dt_root, "cpus/ibm,powerpc-cpu-features/mmu-radix"));
141 	assert(dt_find_by_path(dt_root, "cpus/ibm,powerpc-cpu-features/tm-suspend-hypervisor-assist") != 0);
142 	assert(dt_find_by_path(dt_root, "cpus/ibm,powerpc-cpu-features/tm-suspend-xer-so-bug") == 0);
143 	dt_free(dt_root);
144 
145 	fake_pvr = (PVR_TYPE_P9P << 16) | 0x100; // P9P DD1.0
146 	dt_root = dt_new_root("");
147 	dt_add_cpufeatures(dt_root);
148 	dump_dt(dt_root, 0, false);
149 	assert(dt_find_by_path(dt_root, "cpus/ibm,powerpc-cpu-features/mmu-radix"));
150 	assert(dt_find_by_path(dt_root, "cpus/ibm,powerpc-cpu-features/tm-suspend-hypervisor-assist") != 0);
151 	assert(dt_find_by_path(dt_root, "cpus/ibm,powerpc-cpu-features/tm-suspend-xer-so-bug") == 0);
152 	dt_free(dt_root);
153 
154 	exit(EXIT_SUCCESS);
155 }
156