xref: /kvm-unit-tests/s390x/topology.c (revision 16bff5dbf92897f7ea756b5bda2bb79a5b6f28ce)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * CPU Topology
4  *
5  * Copyright IBM Corp. 2022
6  *
7  * Authors:
8  *  Pierre Morel <pmorel@linux.ibm.com>
9  */
10 
11 #include <libcflat.h>
12 #include <asm/page.h>
13 #include <asm/asm-offsets.h>
14 #include <asm/interrupt.h>
15 #include <asm/facility.h>
16 #include <asm/barrier.h>
17 #include <smp.h>
18 #include <sclp.h>
19 #include <s390x/hardware.h>
20 #include <s390x/stsi.h>
21 
22 static uint8_t pagebuf[PAGE_SIZE] __attribute__((aligned(PAGE_SIZE)));
23 
24 static int max_nested_lvl;
25 static int number_of_cpus;
26 static int cpus_in_masks;
27 static int max_cpus;
28 
29 /*
30  * Topology level as defined by architecture, all levels exists with
31  * a single container unless overwritten by the QEMU -smp parameter.
32  */
33 static int expected_topo_lvl[CPU_TOPOLOGY_MAX_LEVEL] = { 1, 1, 1, 1, 1, 1 };
34 
35 #define PTF_REQ_HORIZONTAL	0
36 #define PTF_REQ_VERTICAL	1
37 #define PTF_CHECK		2
38 
39 #define PTF_ERR_NO_REASON	0
40 #define PTF_ERR_ALRDY_POLARIZED	1
41 #define PTF_ERR_IN_PROGRESS	2
42 
43 extern int diag308_load_reset(u64);
44 
45 static int ptf(unsigned long fc, unsigned long *rc)
46 {
47 	int cc;
48 
49 	asm volatile(
50 		"	ptf	%1	\n"
51 		"       ipm     %0	\n"
52 		"       srl     %0,28	\n"
53 		: "=d" (cc), "+d" (fc)
54 		:
55 		: "cc");
56 
57 	*rc = fc >> 8;
58 	return cc;
59 }
60 
61 static void check_privilege(int fc)
62 {
63 	unsigned long rc;
64 
65 	report_prefix_pushf("Privileged fc %d", fc);
66 	enter_pstate();
67 	expect_pgm_int();
68 	ptf(fc, &rc);
69 	check_pgm_int_code(PGM_INT_CODE_PRIVILEGED_OPERATION);
70 	report_prefix_pop();
71 }
72 
73 static void check_specifications(void)
74 {
75 	unsigned long error = 0;
76 	unsigned long ptf_bits;
77 	unsigned long rc;
78 	int i;
79 
80 	report_prefix_push("Specifications");
81 
82 	/* Function codes above 3 are undefined */
83 	for (i = 4; i < 255; i++) {
84 		expect_pgm_int();
85 		ptf(i, &rc);
86 		if (clear_pgm_int() != PGM_INT_CODE_SPECIFICATION) {
87 			report_fail("FC %d did not yield specification exception", i);
88 			error = 1;
89 		}
90 	}
91 	report(!error, "Undefined function codes");
92 
93 	/* Reserved bits must be 0 */
94 	for (i = 8, error = 0; i < 64; i++) {
95 		ptf_bits = 0x01UL << i;
96 		expect_pgm_int();
97 		ptf(ptf_bits, &rc);
98 		if (clear_pgm_int() != PGM_INT_CODE_SPECIFICATION) {
99 			report_fail("Reserved bit %d did not yield specification exception", i);
100 			error = 1;
101 		}
102 	}
103 
104 	report(!error, "Reserved bits");
105 
106 	report_prefix_pop();
107 }
108 
109 static void check_polarization_change(void)
110 {
111 	unsigned long rc;
112 	int cc;
113 
114 	report_prefix_push("Polarization change");
115 
116 	/* We expect a clean state through reset */
117 	report(diag308_load_reset(1), "load normal reset done");
118 
119 	/*
120 	 * Set vertical polarization to verify that RESET sets
121 	 * horizontal polarization back.
122 	 */
123 	cc = ptf(PTF_REQ_VERTICAL, &rc);
124 	report(cc == 0, "Set vertical polarization.");
125 
126 	report(diag308_load_reset(1), "load normal reset done");
127 
128 	cc = ptf(PTF_CHECK, &rc);
129 	report(cc == 0, "Reset should clear topology report");
130 
131 	cc = ptf(PTF_REQ_HORIZONTAL, &rc);
132 	report(cc == 2 && rc == PTF_ERR_ALRDY_POLARIZED,
133 	       "After RESET polarization is horizontal");
134 
135 	/* Flip between vertical and horizontal polarization */
136 	cc = ptf(PTF_REQ_VERTICAL, &rc);
137 	report(cc == 0, "Change to vertical");
138 
139 	cc = ptf(PTF_CHECK, &rc);
140 	report(cc == 1, "Should report");
141 
142 	cc = ptf(PTF_REQ_VERTICAL, &rc);
143 	report(cc == 2 && rc == PTF_ERR_ALRDY_POLARIZED, "Double change to vertical");
144 
145 	cc = ptf(PTF_CHECK, &rc);
146 	report(cc == 0, "Should not report");
147 
148 	cc = ptf(PTF_REQ_HORIZONTAL, &rc);
149 	report(cc == 0, "Change to horizontal");
150 
151 	cc = ptf(PTF_CHECK, &rc);
152 	report(cc == 1, "Should Report");
153 
154 	cc = ptf(PTF_REQ_HORIZONTAL, &rc);
155 	report(cc == 2 && rc == PTF_ERR_ALRDY_POLARIZED, "Double change to horizontal");
156 
157 	cc = ptf(PTF_CHECK, &rc);
158 	report(cc == 0, "Should not report");
159 
160 	report_prefix_pop();
161 }
162 
163 static void test_ptf(void)
164 {
165 	check_privilege(PTF_REQ_HORIZONTAL);
166 	check_privilege(PTF_REQ_VERTICAL);
167 	check_privilege(PTF_CHECK);
168 	check_specifications();
169 	check_polarization_change();
170 }
171 
172 /*
173  * stsi_check_maxcpus
174  * @info: Pointer to the stsi information
175  *
176  * The product of the numbers of containers per level
177  * is the maximum number of CPU allowed by the machine.
178  */
179 static void stsi_check_maxcpus(struct sysinfo_15_1_x *info)
180 {
181 	int n, i;
182 
183 	for (i = 0, n = 1; i < CPU_TOPOLOGY_MAX_LEVEL; i++)
184 		n *= info->mag[i] ?: 1;
185 
186 	report(n == max_cpus, "Calculated max CPUs: %d", n);
187 }
188 
189 /*
190  * stsi_check_mag
191  * @info: Pointer to the stsi information
192  *
193  * MAG field should match the architecture defined containers
194  * when MNEST as returned by SCLP matches MNEST of the SYSIB.
195  */
196 static void stsi_check_mag(struct sysinfo_15_1_x *info)
197 {
198 	int i;
199 
200 	report_prefix_push("MAG");
201 
202 	stsi_check_maxcpus(info);
203 
204 	/*
205 	 * It is not clear how the MAG fields are calculated when mnest
206 	 * in the SYSIB 15.x is different from the maximum nested level
207 	 * in the SCLP info, so we skip here for now.
208 	 */
209 	if (max_nested_lvl != info->mnest) {
210 		report_skip("No specification on layer aggregation");
211 		goto done;
212 	}
213 
214 	/*
215 	 * MAG up to max_nested_lvl must match the architecture
216 	 * defined containers.
217 	 */
218 	for (i = 0; i < max_nested_lvl; i++)
219 		report(info->mag[CPU_TOPOLOGY_MAX_LEVEL - i - 1] == expected_topo_lvl[i],
220 		       "MAG %d field match %d == %d",
221 		       i + 1,
222 		       info->mag[CPU_TOPOLOGY_MAX_LEVEL - i - 1],
223 		       expected_topo_lvl[i]);
224 
225 	/* Above max_nested_lvl the MAG field must be null */
226 	for (; i < CPU_TOPOLOGY_MAX_LEVEL; i++)
227 		report(info->mag[CPU_TOPOLOGY_MAX_LEVEL - i - 1] == 0,
228 		       "MAG %d field match %d == %d", i + 1,
229 		       info->mag[CPU_TOPOLOGY_MAX_LEVEL - i - 1], 0);
230 
231 done:
232 	report_prefix_pop();
233 }
234 
235 /**
236  * check_tle:
237  * @tc: pointer to first TLE
238  *
239  * Recursively check the containers TLEs until we
240  * find a CPU TLE.
241  */
242 static uint8_t *check_tle(void *tc)
243 {
244 	struct topology_container *container = tc;
245 	struct topology_core *cpus;
246 	int n;
247 
248 	if (container->nl) {
249 		report_info("NL: %d id: %d", container->nl, container->id);
250 
251 		report(!(*(uint64_t *)tc & CONTAINER_TLE_RES_BITS),
252 		       "reserved bits %016lx",
253 		       *(uint64_t *)tc & CONTAINER_TLE_RES_BITS);
254 
255 		return check_tle(tc + sizeof(*container));
256 	}
257 
258 	report_info("NL: %d", container->nl);
259 	cpus = tc;
260 
261 	report(!(*(uint64_t *)tc & CPUS_TLE_RES_BITS), "reserved bits %016lx",
262 	       *(uint64_t *)tc & CPUS_TLE_RES_BITS);
263 
264 	report(cpus->type == 0x03, "type IFL");
265 
266 	report_info("origin: %d", cpus->origin);
267 	report_info("mask: %016lx", cpus->mask);
268 	report_info("dedicated: %d entitlement: %d", cpus->d, cpus->pp);
269 
270 	n = __builtin_popcountl(cpus->mask);
271 	report(n <= expected_topo_lvl[0], "CPUs per mask: %d out of max %d",
272 	       n, expected_topo_lvl[0]);
273 	cpus_in_masks += n;
274 
275 	if (!cpus->d)
276 		report_skip("Not dedicated");
277 	else
278 		report(cpus->pp == 3 || cpus->pp == 0, "Dedicated CPUs are either vertically polarized or have high entitlement");
279 
280 	return tc + sizeof(*cpus);
281 }
282 
283 /**
284  * stsi_check_tle_coherency:
285  * @info: Pointer to the stsi information
286  *
287  * We verify that we get the expected number of Topology List Entry
288  * containers for a specific level.
289  */
290 static void stsi_check_tle_coherency(struct sysinfo_15_1_x *info)
291 {
292 	void *tc, *end;
293 
294 	report_prefix_push("TLE");
295 	cpus_in_masks = 0;
296 
297 	tc = info->tle;
298 	end = (void *)info + info->length;
299 
300 	while (tc < end)
301 		tc = check_tle(tc);
302 
303 	report(cpus_in_masks == number_of_cpus, "CPUs in mask %d",
304 	       cpus_in_masks);
305 
306 	report_prefix_pop();
307 }
308 
309 /**
310  * stsi_get_sysib:
311  * @info: pointer to the STSI info structure
312  * @sel2: the selector giving the topology level to check
313  *
314  * Fill the sysinfo_15_1_x info structure and check the
315  * SYSIB header.
316  *
317  * Returns instruction validity.
318  */
319 static int stsi_get_sysib(struct sysinfo_15_1_x *info, int sel2)
320 {
321 	int ret;
322 
323 	report_prefix_pushf("SYSIB");
324 
325 	ret = stsi(pagebuf, 15, 1, sel2);
326 
327 	if (max_nested_lvl >= sel2) {
328 		report(!ret, "Valid instruction");
329 		report(sel2 == info->mnest, "Valid mnest");
330 	} else {
331 		report(ret, "Invalid instruction");
332 	}
333 
334 	report_prefix_pop();
335 
336 	return ret;
337 }
338 
339 /**
340  * check_sysinfo_15_1_x:
341  * @info: pointer to the STSI info structure
342  * @sel2: the selector giving the topology level to check
343  *
344  * Check if the validity of the STSI instruction and then
345  * calls specific checks on the information buffer.
346  */
347 static void check_sysinfo_15_1_x(struct sysinfo_15_1_x *info, int sel2)
348 {
349 	int ret;
350 	int cc;
351 	unsigned long rc;
352 
353 	report_prefix_pushf("15_1_%d", sel2);
354 
355 	ret = stsi_get_sysib(info, sel2);
356 	if (ret) {
357 		report_skip("Selector 2 not supported by architecture");
358 		goto end;
359 	}
360 
361 	report_prefix_pushf("H");
362 	cc = ptf(PTF_REQ_HORIZONTAL, &rc);
363 	if (cc != 0 && rc != PTF_ERR_ALRDY_POLARIZED) {
364 		report_fail("Unable to set horizontal polarization");
365 		goto vertical;
366 	}
367 
368 	stsi_check_mag(info);
369 	stsi_check_tle_coherency(info);
370 
371 vertical:
372 	report_prefix_pop();
373 	report_prefix_pushf("V");
374 
375 	cc = ptf(PTF_REQ_VERTICAL, &rc);
376 	if (cc != 0 && rc != PTF_ERR_ALRDY_POLARIZED) {
377 		report_fail("Unable to set vertical polarization");
378 		goto end;
379 	}
380 
381 	stsi_check_mag(info);
382 	stsi_check_tle_coherency(info);
383 	report_prefix_pop();
384 
385 end:
386 	report_prefix_pop();
387 }
388 
389 /*
390  * The Maximum Nested level is given by SCLP READ_SCP_INFO if the MNEST facility
391  * is available.
392  * If the MNEST facility is not available, sclp_get_stsi_mnest  returns 0 and the
393  * Maximum Nested level is 2
394  */
395 #define S390_DEFAULT_MNEST	2
396 static int sclp_get_mnest(void)
397 {
398 	return sclp_get_stsi_mnest() ?: S390_DEFAULT_MNEST;
399 }
400 
401 static int expected_num_cpus(void)
402 {
403 	int i;
404 	int ncpus = 1;
405 
406 	for (i = 0; i < CPU_TOPOLOGY_MAX_LEVEL; i++)
407 		ncpus *= expected_topo_lvl[i] ?: 1;
408 
409 	return ncpus;
410 }
411 
412 /**
413  * test_stsi:
414  *
415  * Retrieves the maximum nested topology level supported by the architecture
416  * and the number of CPUs.
417  * Calls the checking for the STSI instruction in sel2 reverse level order
418  * from 6 (CPU_TOPOLOGY_MAX_LEVEL) to 2 to have the most interesting level,
419  * the one triggering a topology-change-report-pending condition, level 2,
420  * at the end of the report.
421  *
422  */
423 static void test_stsi(void)
424 {
425 	int sel2;
426 
427 	max_cpus = expected_num_cpus();
428 	report_info("Architecture max CPUs: %d", max_cpus);
429 
430 	max_nested_lvl = sclp_get_mnest();
431 	report_info("SCLP maximum nested level : %d", max_nested_lvl);
432 
433 	number_of_cpus = sclp_get_cpu_num();
434 	report_info("SCLP number of CPU: %d", number_of_cpus);
435 
436 	/* STSI selector 2 can takes values between 2 and 6 */
437 	for (sel2 = 6; sel2 >= 2; sel2--)
438 		check_sysinfo_15_1_x((struct sysinfo_15_1_x *)pagebuf, sel2);
439 }
440 
441 /**
442  * parse_topology_args:
443  * @argc: number of arguments
444  * @argv: argument array
445  *
446  * This function initialize the architecture topology levels
447  * which should be the same as the one provided by the hypervisor.
448  *
449  * We use the current names found in IBM/Z literature, Linux and QEMU:
450  * cores, sockets/packages, books, drawers and nodes to facilitate the
451  * human machine interface but store the result in a machine abstract
452  * array of architecture topology levels.
453  * Note that when QEMU uses socket as a name for the topology level 1
454  * Linux uses package or physical_package.
455  */
456 static void parse_topology_args(int argc, char **argv)
457 {
458 	int i;
459 	static const char * const levels[] = { "cores", "sockets",
460 					       "books", "drawers" };
461 
462 	for (i = 1; i < argc; i++) {
463 		char *flag = argv[i];
464 		int level;
465 
466 		if (flag[0] != '-')
467 			report_abort("Argument is expected to begin with '-'");
468 		flag++;
469 		for (level = 0; ARRAY_SIZE(levels); level++) {
470 			if (!strcmp(levels[level], flag))
471 				break;
472 		}
473 		if (level == ARRAY_SIZE(levels))
474 			report_abort("Unknown parameter %s", flag);
475 
476 		expected_topo_lvl[level] = atol(argv[++i]);
477 		report_info("%s: %d", levels[level], expected_topo_lvl[level]);
478 	}
479 }
480 
481 static struct {
482 	const char *name;
483 	void (*func)(void);
484 } tests[] = {
485 	{ "PTF", test_ptf },
486 	{ "STSI", test_stsi },
487 	{ NULL, NULL }
488 };
489 
490 int main(int argc, char *argv[])
491 {
492 	int i;
493 
494 	report_prefix_push("CPU Topology");
495 
496 	parse_topology_args(argc, argv);
497 
498 	if (!test_facility(11)) {
499 		report_skip("Topology facility not present");
500 		goto end;
501 	}
502 
503 	report_info("Virtual machine level %ld", stsi_get_fc());
504 
505 	for (i = 0; tests[i].name; i++) {
506 		report_prefix_push(tests[i].name);
507 		tests[i].func();
508 		report_prefix_pop();
509 	}
510 
511 end:
512 	report_prefix_pop();
513 	return report_summary();
514 }
515