xref: /openbmc/linux/arch/s390/boot/als.c (revision ebd09753)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *    Copyright IBM Corp. 2016
4  */
5 #include <linux/kernel.h>
6 #include <asm/processor.h>
7 #include <asm/facility.h>
8 #include <asm/lowcore.h>
9 #include <asm/sclp.h>
10 
11 /*
12  * The code within this file will be called very early. It may _not_
13  * access anything within the bss section, since that is not cleared
14  * yet and may contain data (e.g. initrd) that must be saved by other
15  * code.
16  * For temporary objects the stack (16k) should be used.
17  */
18 
19 static unsigned long als[] = { FACILITIES_ALS };
20 
21 static void u16_to_hex(char *str, u16 val)
22 {
23 	int i, num;
24 
25 	for (i = 1; i <= 4; i++) {
26 		num = (val >> (16 - 4 * i)) & 0xf;
27 		if (num >= 10)
28 			num += 7;
29 		*str++ = '0' + num;
30 	}
31 	*str = '\0';
32 }
33 
34 static void print_machine_type(void)
35 {
36 	static char mach_str[80] = "Detected machine-type number: ";
37 	char type_str[5];
38 	struct cpuid id;
39 
40 	get_cpu_id(&id);
41 	u16_to_hex(type_str, id.machine);
42 	strcat(mach_str, type_str);
43 	strcat(mach_str, "\n");
44 	sclp_early_printk(mach_str);
45 }
46 
47 static void u16_to_decimal(char *str, u16 val)
48 {
49 	int div = 1;
50 
51 	while (div * 10 <= val)
52 		div *= 10;
53 	while (div) {
54 		*str++ = '0' + val / div;
55 		val %= div;
56 		div /= 10;
57 	}
58 	*str = '\0';
59 }
60 
61 static void print_missing_facilities(void)
62 {
63 	static char als_str[80] = "Missing facilities: ";
64 	unsigned long val;
65 	char val_str[6];
66 	int i, j, first;
67 
68 	first = 1;
69 	for (i = 0; i < ARRAY_SIZE(als); i++) {
70 		val = ~S390_lowcore.stfle_fac_list[i] & als[i];
71 		for (j = 0; j < BITS_PER_LONG; j++) {
72 			if (!(val & (1UL << (BITS_PER_LONG - 1 - j))))
73 				continue;
74 			if (!first)
75 				strcat(als_str, ",");
76 			/*
77 			 * Make sure we stay within one line. Consider that
78 			 * each facility bit adds up to five characters and
79 			 * z/VM adds a four character prefix.
80 			 */
81 			if (strlen(als_str) > 70) {
82 				strcat(als_str, "\n");
83 				sclp_early_printk(als_str);
84 				*als_str = '\0';
85 			}
86 			u16_to_decimal(val_str, i * BITS_PER_LONG + j);
87 			strcat(als_str, val_str);
88 			first = 0;
89 		}
90 	}
91 	strcat(als_str, "\n");
92 	sclp_early_printk(als_str);
93 	sclp_early_printk("See Principles of Operations for facility bits\n");
94 }
95 
96 static void facility_mismatch(void)
97 {
98 	sclp_early_printk("The Linux kernel requires more recent processor hardware\n");
99 	print_machine_type();
100 	print_missing_facilities();
101 	disabled_wait(0x8badcccc);
102 }
103 
104 void verify_facilities(void)
105 {
106 	int i;
107 
108 	for (i = 0; i < ARRAY_SIZE(S390_lowcore.stfle_fac_list); i++)
109 		S390_lowcore.stfle_fac_list[i] = 0;
110 	asm volatile(
111 		"	stfl	0(0)\n"
112 		: "=m" (S390_lowcore.stfl_fac_list));
113 	S390_lowcore.stfle_fac_list[0] = (u64)S390_lowcore.stfl_fac_list << 32;
114 	if (S390_lowcore.stfl_fac_list & 0x01000000) {
115 		register unsigned long reg0 asm("0") = ARRAY_SIZE(als) - 1;
116 
117 		asm volatile(".insn s,0xb2b00000,0(%1)" /* stfle */
118 			     : "+d" (reg0)
119 			     : "a" (&S390_lowcore.stfle_fac_list)
120 			     : "memory", "cc");
121 	}
122 	for (i = 0; i < ARRAY_SIZE(als); i++) {
123 		if ((S390_lowcore.stfle_fac_list[i] & als[i]) != als[i])
124 			facility_mismatch();
125 	}
126 }
127