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