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