xref: /kvm-unit-tests/s390x/migration-cmm.c (revision e6e1a20143625acb08a3f8ae25ec06846356d891)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * CMM migration tests (ESSA)
4  *
5  * Copyright IBM Corp. 2022
6  *
7  * Authors:
8  *  Nico Boehr <nrb@linux.ibm.com>
9  */
10 
11 #include <libcflat.h>
12 #include <migrate.h>
13 #include <asm/interrupt.h>
14 #include <asm/page.h>
15 #include <asm/cmm.h>
16 #include <bitops.h>
17 
18 #define NUM_PAGES 128
19 static uint8_t pagebuf[NUM_PAGES][PAGE_SIZE] __attribute__((aligned(PAGE_SIZE)));
20 
test_migration(void)21 static void test_migration(void)
22 {
23 	int i, state_mask, actual_state;
24 	/*
25 	 * Maps ESSA actions to states the page is allowed to be in after the
26 	 * respective action was executed.
27 	 */
28 	int allowed_essa_state_masks[4] = {
29 		BIT(ESSA_USAGE_STABLE),					/* ESSA_SET_STABLE */
30 		BIT(ESSA_USAGE_UNUSED),					/* ESSA_SET_UNUSED */
31 		BIT(ESSA_USAGE_VOLATILE),				/* ESSA_SET_VOLATILE */
32 		BIT(ESSA_USAGE_VOLATILE) | BIT(ESSA_USAGE_POT_VOLATILE) /* ESSA_SET_POT_VOLATILE */
33 	};
34 
35 	assert(NUM_PAGES % 4 == 0);
36 	for (i = 0; i < NUM_PAGES; i += 4) {
37 		essa(ESSA_SET_STABLE, (unsigned long)pagebuf[i]);
38 		essa(ESSA_SET_UNUSED, (unsigned long)pagebuf[i + 1]);
39 		essa(ESSA_SET_VOLATILE, (unsigned long)pagebuf[i + 2]);
40 		essa(ESSA_SET_POT_VOLATILE, (unsigned long)pagebuf[i + 3]);
41 	}
42 
43 	migrate_once();
44 
45 	for (i = 0; i < NUM_PAGES; i++) {
46 		actual_state = essa(ESSA_GET_STATE, (unsigned long)pagebuf[i]);
47 		/* extract the usage state in bits 60 and 61 */
48 		actual_state = (actual_state >> 2) & 0x3;
49 		state_mask = allowed_essa_state_masks[i % ARRAY_SIZE(allowed_essa_state_masks)];
50 		report(BIT(actual_state) & state_mask, "page %d state: expected_mask=0x%x actual_mask=0x%lx", i, state_mask, BIT(actual_state));
51 	}
52 }
53 
main(void)54 int main(void)
55 {
56 	report_prefix_push("migration-cmm");
57 
58 	if (!check_essa_available()) {
59 		report_skip("ESSA is not available");
60 		migrate_skip();
61 	} else {
62 		test_migration();
63 	}
64 
65 	report_prefix_pop();
66 	return report_summary();
67 }
68