xref: /openbmc/qemu/tests/qtest/cxl-test.c (revision d2dfe0b5)
1 /*
2  * QTest testcase for CXL
3  *
4  * This work is licensed under the terms of the GNU GPL, version 2 or later.
5  * See the COPYING file in the top-level directory.
6  */
7 
8 #include "qemu/osdep.h"
9 #include "libqtest-single.h"
10 
11 #define QEMU_PXB_CMD \
12     "-machine q35,cxl=on " \
13     "-device pxb-cxl,id=cxl.0,bus=pcie.0,bus_nr=52 " \
14     "-M cxl-fmw.0.targets.0=cxl.0,cxl-fmw.0.size=4G "
15 
16 #define QEMU_2PXB_CMD \
17     "-machine q35,cxl=on " \
18     "-device pxb-cxl,id=cxl.0,bus=pcie.0,bus_nr=52 " \
19     "-device pxb-cxl,id=cxl.1,bus=pcie.0,bus_nr=53 " \
20     "-M cxl-fmw.0.targets.0=cxl.0,cxl-fmw.0.targets.1=cxl.1,cxl-fmw.0.size=4G "
21 
22 #define QEMU_RP \
23     "-device cxl-rp,id=rp0,bus=cxl.0,chassis=0,slot=0 "
24 
25 /* Dual ports on first pxb */
26 #define QEMU_2RP \
27     "-device cxl-rp,id=rp0,bus=cxl.0,chassis=0,slot=0 " \
28     "-device cxl-rp,id=rp1,bus=cxl.0,chassis=0,slot=1 "
29 
30 /* Dual ports on each of the pxb instances */
31 #define QEMU_4RP \
32     "-device cxl-rp,id=rp0,bus=cxl.0,chassis=0,slot=0 " \
33     "-device cxl-rp,id=rp1,bus=cxl.0,chassis=0,slot=1 " \
34     "-device cxl-rp,id=rp2,bus=cxl.1,chassis=0,slot=2 " \
35     "-device cxl-rp,id=rp3,bus=cxl.1,chassis=0,slot=3 "
36 
37 #define QEMU_T3D_DEPRECATED \
38     "-object memory-backend-file,id=cxl-mem0,mem-path=%s,size=256M " \
39     "-object memory-backend-file,id=lsa0,mem-path=%s,size=256M " \
40     "-device cxl-type3,bus=rp0,memdev=cxl-mem0,lsa=lsa0,id=cxl-pmem0 "
41 
42 #define QEMU_T3D_PMEM \
43     "-object memory-backend-file,id=cxl-mem0,mem-path=%s,size=256M " \
44     "-object memory-backend-file,id=lsa0,mem-path=%s,size=256M " \
45     "-device cxl-type3,bus=rp0,persistent-memdev=cxl-mem0,lsa=lsa0,id=pmem0 "
46 
47 #define QEMU_T3D_VMEM \
48     "-object memory-backend-ram,id=cxl-mem0,size=256M " \
49     "-device cxl-type3,bus=rp0,volatile-memdev=cxl-mem0,id=mem0 "
50 
51 #define QEMU_T3D_VMEM_LSA \
52     "-object memory-backend-ram,id=cxl-mem0,size=256M " \
53     "-object memory-backend-file,id=lsa0,mem-path=%s,size=256M " \
54     "-device cxl-type3,bus=rp0,volatile-memdev=cxl-mem0,lsa=lsa0,id=mem0 "
55 
56 #define QEMU_2T3D \
57     "-object memory-backend-file,id=cxl-mem0,mem-path=%s,size=256M " \
58     "-object memory-backend-file,id=lsa0,mem-path=%s,size=256M " \
59     "-device cxl-type3,bus=rp0,persistent-memdev=cxl-mem0,lsa=lsa0,id=pmem0 " \
60     "-object memory-backend-file,id=cxl-mem1,mem-path=%s,size=256M " \
61     "-object memory-backend-file,id=lsa1,mem-path=%s,size=256M " \
62     "-device cxl-type3,bus=rp1,persistent-memdev=cxl-mem1,lsa=lsa1,id=pmem1 "
63 
64 #define QEMU_4T3D \
65     "-object memory-backend-file,id=cxl-mem0,mem-path=%s,size=256M " \
66     "-object memory-backend-file,id=lsa0,mem-path=%s,size=256M " \
67     "-device cxl-type3,bus=rp0,persistent-memdev=cxl-mem0,lsa=lsa0,id=pmem0 " \
68     "-object memory-backend-file,id=cxl-mem1,mem-path=%s,size=256M " \
69     "-object memory-backend-file,id=lsa1,mem-path=%s,size=256M " \
70     "-device cxl-type3,bus=rp1,persistent-memdev=cxl-mem1,lsa=lsa1,id=pmem1 " \
71     "-object memory-backend-file,id=cxl-mem2,mem-path=%s,size=256M " \
72     "-object memory-backend-file,id=lsa2,mem-path=%s,size=256M " \
73     "-device cxl-type3,bus=rp2,persistent-memdev=cxl-mem2,lsa=lsa2,id=pmem2 " \
74     "-object memory-backend-file,id=cxl-mem3,mem-path=%s,size=256M " \
75     "-object memory-backend-file,id=lsa3,mem-path=%s,size=256M " \
76     "-device cxl-type3,bus=rp3,persistent-memdev=cxl-mem3,lsa=lsa3,id=pmem3 "
77 
78 static void cxl_basic_hb(void)
79 {
80     qtest_start("-machine q35,cxl=on");
81     qtest_end();
82 }
83 
84 static void cxl_basic_pxb(void)
85 {
86     qtest_start("-machine q35,cxl=on -device pxb-cxl,bus=pcie.0");
87     qtest_end();
88 }
89 
90 static void cxl_pxb_with_window(void)
91 {
92     qtest_start(QEMU_PXB_CMD);
93     qtest_end();
94 }
95 
96 static void cxl_2pxb_with_window(void)
97 {
98     qtest_start(QEMU_2PXB_CMD);
99     qtest_end();
100 }
101 
102 static void cxl_root_port(void)
103 {
104     qtest_start(QEMU_PXB_CMD QEMU_RP);
105     qtest_end();
106 }
107 
108 static void cxl_2root_port(void)
109 {
110     qtest_start(QEMU_PXB_CMD QEMU_2RP);
111     qtest_end();
112 }
113 
114 #ifdef CONFIG_POSIX
115 static void cxl_t3d_deprecated(void)
116 {
117     g_autoptr(GString) cmdline = g_string_new(NULL);
118     g_autofree const char *tmpfs = NULL;
119 
120     tmpfs = g_dir_make_tmp("cxl-test-XXXXXX", NULL);
121 
122     g_string_printf(cmdline, QEMU_PXB_CMD QEMU_RP QEMU_T3D_DEPRECATED,
123                     tmpfs, tmpfs);
124 
125     qtest_start(cmdline->str);
126     qtest_end();
127 }
128 
129 static void cxl_t3d_persistent(void)
130 {
131     g_autoptr(GString) cmdline = g_string_new(NULL);
132     g_autofree const char *tmpfs = NULL;
133 
134     tmpfs = g_dir_make_tmp("cxl-test-XXXXXX", NULL);
135 
136     g_string_printf(cmdline, QEMU_PXB_CMD QEMU_RP QEMU_T3D_PMEM,
137                     tmpfs, tmpfs);
138 
139     qtest_start(cmdline->str);
140     qtest_end();
141 }
142 
143 static void cxl_t3d_volatile(void)
144 {
145     g_autoptr(GString) cmdline = g_string_new(NULL);
146 
147     g_string_printf(cmdline, QEMU_PXB_CMD QEMU_RP QEMU_T3D_VMEM);
148 
149     qtest_start(cmdline->str);
150     qtest_end();
151 }
152 
153 static void cxl_t3d_volatile_lsa(void)
154 {
155     g_autoptr(GString) cmdline = g_string_new(NULL);
156     g_autofree const char *tmpfs = NULL;
157 
158     tmpfs = g_dir_make_tmp("cxl-test-XXXXXX", NULL);
159 
160     g_string_printf(cmdline, QEMU_PXB_CMD QEMU_RP QEMU_T3D_VMEM_LSA,
161                     tmpfs);
162 
163     qtest_start(cmdline->str);
164     qtest_end();
165     rmdir(tmpfs);
166 }
167 
168 static void cxl_1pxb_2rp_2t3d(void)
169 {
170     g_autoptr(GString) cmdline = g_string_new(NULL);
171     g_autofree const char *tmpfs = NULL;
172 
173     tmpfs = g_dir_make_tmp("cxl-test-XXXXXX", NULL);
174 
175     g_string_printf(cmdline, QEMU_PXB_CMD QEMU_2RP QEMU_2T3D,
176                     tmpfs, tmpfs, tmpfs, tmpfs);
177 
178     qtest_start(cmdline->str);
179     qtest_end();
180     rmdir(tmpfs);
181 }
182 
183 static void cxl_2pxb_4rp_4t3d(void)
184 {
185     g_autoptr(GString) cmdline = g_string_new(NULL);
186     g_autofree const char *tmpfs = NULL;
187 
188     tmpfs = g_dir_make_tmp("cxl-test-XXXXXX", NULL);
189 
190     g_string_printf(cmdline, QEMU_2PXB_CMD QEMU_4RP QEMU_4T3D,
191                     tmpfs, tmpfs, tmpfs, tmpfs, tmpfs, tmpfs,
192                     tmpfs, tmpfs);
193 
194     qtest_start(cmdline->str);
195     qtest_end();
196     rmdir(tmpfs);
197 }
198 #endif /* CONFIG_POSIX */
199 
200 int main(int argc, char **argv)
201 {
202     g_test_init(&argc, &argv, NULL);
203 
204     qtest_add_func("/pci/cxl/basic_hostbridge", cxl_basic_hb);
205     qtest_add_func("/pci/cxl/basic_pxb", cxl_basic_pxb);
206     qtest_add_func("/pci/cxl/pxb_with_window", cxl_pxb_with_window);
207     qtest_add_func("/pci/cxl/pxb_x2_with_window", cxl_2pxb_with_window);
208     qtest_add_func("/pci/cxl/rp", cxl_root_port);
209     qtest_add_func("/pci/cxl/rp_x2", cxl_2root_port);
210 #ifdef CONFIG_POSIX
211     qtest_add_func("/pci/cxl/type3_device", cxl_t3d_deprecated);
212     qtest_add_func("/pci/cxl/type3_device_pmem", cxl_t3d_persistent);
213     qtest_add_func("/pci/cxl/type3_device_vmem", cxl_t3d_volatile);
214     qtest_add_func("/pci/cxl/type3_device_vmem_lsa", cxl_t3d_volatile_lsa);
215     qtest_add_func("/pci/cxl/rp_x2_type3_x2", cxl_1pxb_2rp_2t3d);
216     qtest_add_func("/pci/cxl/pxb_x2_root_port_x4_type3_x4", cxl_2pxb_4rp_4t3d);
217 #endif
218     return g_test_run();
219 }
220