xref: /openbmc/linux/drivers/s390/char/sclp_early.c (revision 12eb4683)
1 /*
2  * SCLP early driver
3  *
4  * Copyright IBM Corp. 2013
5  */
6 
7 #define KMSG_COMPONENT "sclp_early"
8 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
9 
10 #include <asm/ctl_reg.h>
11 #include <asm/sclp.h>
12 #include <asm/ipl.h>
13 #include "sclp_sdias.h"
14 #include "sclp.h"
15 
16 #define SCLP_CMDW_READ_SCP_INFO		0x00020001
17 #define SCLP_CMDW_READ_SCP_INFO_FORCED	0x00120001
18 
19 struct read_info_sccb {
20 	struct	sccb_header header;	/* 0-7 */
21 	u16	rnmax;			/* 8-9 */
22 	u8	rnsize;			/* 10 */
23 	u8	_reserved0[24 - 11];	/* 11-15 */
24 	u8	loadparm[8];		/* 24-31 */
25 	u8	_reserved1[48 - 32];	/* 32-47 */
26 	u64	facilities;		/* 48-55 */
27 	u8	_reserved2[84 - 56];	/* 56-83 */
28 	u8	fac84;			/* 84 */
29 	u8	fac85;			/* 85 */
30 	u8	_reserved3[91 - 86];	/* 86-90 */
31 	u8	flags;			/* 91 */
32 	u8	_reserved4[100 - 92];	/* 92-99 */
33 	u32	rnsize2;		/* 100-103 */
34 	u64	rnmax2;			/* 104-111 */
35 	u8	_reserved5[4096 - 112];	/* 112-4095 */
36 } __packed __aligned(PAGE_SIZE);
37 
38 static __initdata struct read_info_sccb early_read_info_sccb;
39 static __initdata char sccb_early[PAGE_SIZE] __aligned(PAGE_SIZE);
40 static unsigned long sclp_hsa_size;
41 
42 __initdata int sclp_early_read_info_sccb_valid;
43 u64 sclp_facilities;
44 u8 sclp_fac84;
45 unsigned long long sclp_rzm;
46 unsigned long long sclp_rnmax;
47 
48 static int __init sclp_cmd_sync_early(sclp_cmdw_t cmd, void *sccb)
49 {
50 	int rc;
51 
52 	__ctl_set_bit(0, 9);
53 	rc = sclp_service_call(cmd, sccb);
54 	if (rc)
55 		goto out;
56 	__load_psw_mask(PSW_DEFAULT_KEY | PSW_MASK_BASE | PSW_MASK_EA |
57 			PSW_MASK_BA | PSW_MASK_EXT | PSW_MASK_WAIT);
58 	local_irq_disable();
59 out:
60 	/* Contents of the sccb might have changed. */
61 	barrier();
62 	__ctl_clear_bit(0, 9);
63 	return rc;
64 }
65 
66 static void __init sclp_read_info_early(void)
67 {
68 	int rc;
69 	int i;
70 	struct read_info_sccb *sccb;
71 	sclp_cmdw_t commands[] = {SCLP_CMDW_READ_SCP_INFO_FORCED,
72 				  SCLP_CMDW_READ_SCP_INFO};
73 
74 	sccb = &early_read_info_sccb;
75 	for (i = 0; i < ARRAY_SIZE(commands); i++) {
76 		do {
77 			memset(sccb, 0, sizeof(*sccb));
78 			sccb->header.length = sizeof(*sccb);
79 			sccb->header.function_code = 0x80;
80 			sccb->header.control_mask[2] = 0x80;
81 			rc = sclp_cmd_sync_early(commands[i], sccb);
82 		} while (rc == -EBUSY);
83 
84 		if (rc)
85 			break;
86 		if (sccb->header.response_code == 0x10) {
87 			sclp_early_read_info_sccb_valid = 1;
88 			break;
89 		}
90 		if (sccb->header.response_code != 0x1f0)
91 			break;
92 	}
93 }
94 
95 static void __init sclp_facilities_detect(void)
96 {
97 	struct read_info_sccb *sccb;
98 
99 	sclp_read_info_early();
100 	if (!sclp_early_read_info_sccb_valid)
101 		return;
102 
103 	sccb = &early_read_info_sccb;
104 	sclp_facilities = sccb->facilities;
105 	sclp_fac84 = sccb->fac84;
106 	if (sccb->fac85 & 0x02)
107 		S390_lowcore.machine_flags |= MACHINE_FLAG_ESOP;
108 	sclp_rnmax = sccb->rnmax ? sccb->rnmax : sccb->rnmax2;
109 	sclp_rzm = sccb->rnsize ? sccb->rnsize : sccb->rnsize2;
110 	sclp_rzm <<= 20;
111 }
112 
113 bool __init sclp_has_linemode(void)
114 {
115 	struct init_sccb *sccb = (void *) &sccb_early;
116 
117 	if (sccb->header.response_code != 0x20)
118 		return 0;
119 	if (!(sccb->sclp_send_mask & (EVTYP_OPCMD_MASK | EVTYP_PMSGCMD_MASK)))
120 		return 0;
121 	if (!(sccb->sclp_receive_mask & (EVTYP_MSG_MASK | EVTYP_PMSGCMD_MASK)))
122 		return 0;
123 	return 1;
124 }
125 
126 bool __init sclp_has_vt220(void)
127 {
128 	struct init_sccb *sccb = (void *) &sccb_early;
129 
130 	if (sccb->header.response_code != 0x20)
131 		return 0;
132 	if (sccb->sclp_send_mask & EVTYP_VT220MSG_MASK)
133 		return 1;
134 	return 0;
135 }
136 
137 unsigned long long sclp_get_rnmax(void)
138 {
139 	return sclp_rnmax;
140 }
141 
142 unsigned long long sclp_get_rzm(void)
143 {
144 	return sclp_rzm;
145 }
146 
147 /*
148  * This function will be called after sclp_facilities_detect(), which gets
149  * called from early.c code. Therefore the sccb should have valid contents.
150  */
151 void __init sclp_get_ipl_info(struct sclp_ipl_info *info)
152 {
153 	struct read_info_sccb *sccb;
154 
155 	if (!sclp_early_read_info_sccb_valid)
156 		return;
157 	sccb = &early_read_info_sccb;
158 	info->is_valid = 1;
159 	if (sccb->flags & 0x2)
160 		info->has_dump = 1;
161 	memcpy(&info->loadparm, &sccb->loadparm, LOADPARM_LEN);
162 }
163 
164 static int __init sclp_cmd_early(sclp_cmdw_t cmd, void *sccb)
165 {
166 	int rc;
167 
168 	do {
169 		rc = sclp_cmd_sync_early(cmd, sccb);
170 	} while (rc == -EBUSY);
171 
172 	if (rc)
173 		return -EIO;
174 	if (((struct sccb_header *) sccb)->response_code != 0x0020)
175 		return -EIO;
176 	return 0;
177 }
178 
179 static void __init sccb_init_eq_size(struct sdias_sccb *sccb)
180 {
181 	memset(sccb, 0, sizeof(*sccb));
182 
183 	sccb->hdr.length = sizeof(*sccb);
184 	sccb->evbuf.hdr.length = sizeof(struct sdias_evbuf);
185 	sccb->evbuf.hdr.type = EVTYP_SDIAS;
186 	sccb->evbuf.event_qual = SDIAS_EQ_SIZE;
187 	sccb->evbuf.data_id = SDIAS_DI_FCP_DUMP;
188 	sccb->evbuf.event_id = 4712;
189 	sccb->evbuf.dbs = 1;
190 }
191 
192 static int __init sclp_set_event_mask(unsigned long receive_mask,
193 				      unsigned long send_mask)
194 {
195 	struct init_sccb *sccb = (void *) &sccb_early;
196 
197 	memset(sccb, 0, sizeof(*sccb));
198 	sccb->header.length = sizeof(*sccb);
199 	sccb->mask_length = sizeof(sccb_mask_t);
200 	sccb->receive_mask = receive_mask;
201 	sccb->send_mask = send_mask;
202 	return sclp_cmd_early(SCLP_CMDW_WRITE_EVENT_MASK, sccb);
203 }
204 
205 static long __init sclp_hsa_size_init(void)
206 {
207 	struct sdias_sccb *sccb = (void *) &sccb_early;
208 
209 	sccb_init_eq_size(sccb);
210 	if (sclp_cmd_early(SCLP_CMDW_WRITE_EVENT_DATA, sccb))
211 		return -EIO;
212 	if (sccb->evbuf.blk_cnt != 0)
213 		return (sccb->evbuf.blk_cnt - 1) * PAGE_SIZE;
214 	return 0;
215 }
216 
217 static long __init sclp_hsa_copy_wait(void)
218 {
219 	struct sccb_header *sccb = (void *) &sccb_early;
220 
221 	memset(sccb, 0, PAGE_SIZE);
222 	sccb->length = PAGE_SIZE;
223 	if (sclp_cmd_early(SCLP_CMDW_READ_EVENT_DATA, sccb))
224 		return -EIO;
225 	return (((struct sdias_sccb *) sccb)->evbuf.blk_cnt - 1) * PAGE_SIZE;
226 }
227 
228 unsigned long sclp_get_hsa_size(void)
229 {
230 	return sclp_hsa_size;
231 }
232 
233 static void __init sclp_hsa_size_detect(void)
234 {
235 	long size;
236 
237 	/* First try synchronous interface (LPAR) */
238 	if (sclp_set_event_mask(0, 0x40000010))
239 		return;
240 	size = sclp_hsa_size_init();
241 	if (size < 0)
242 		return;
243 	if (size != 0)
244 		goto out;
245 	/* Then try asynchronous interface (z/VM) */
246 	if (sclp_set_event_mask(0x00000010, 0x40000010))
247 		return;
248 	size = sclp_hsa_size_init();
249 	if (size < 0)
250 		return;
251 	size = sclp_hsa_copy_wait();
252 	if (size < 0)
253 		return;
254 out:
255 	sclp_hsa_size = size;
256 }
257 
258 void __init sclp_early_detect(void)
259 {
260 	sclp_facilities_detect();
261 	sclp_hsa_size_detect();
262 	sclp_set_event_mask(0, 0);
263 }
264