1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include "qemu/osdep.h"
16#include "qemu/units.h"
17#include "qapi/error.h"
18#include "cpu.h"
19#include "sysemu/sysemu.h"
20#include "hw/boards.h"
21#include "hw/s390x/sclp.h"
22#include "hw/s390x/event-facility.h"
23#include "hw/s390x/s390-pci-bus.h"
24#include "hw/s390x/ipl.h"
25
26static inline SCLPDevice *get_sclp_device(void)
27{
28 static SCLPDevice *sclp;
29
30 if (!sclp) {
31 sclp = SCLP(object_resolve_path_type("", TYPE_SCLP, NULL));
32 }
33 return sclp;
34}
35
36static inline bool sclp_command_code_valid(uint32_t code)
37{
38 switch (code & SCLP_CMD_CODE_MASK) {
39 case SCLP_CMDW_READ_SCP_INFO:
40 case SCLP_CMDW_READ_SCP_INFO_FORCED:
41 case SCLP_CMDW_READ_CPU_INFO:
42 case SCLP_CMDW_CONFIGURE_IOA:
43 case SCLP_CMDW_DECONFIGURE_IOA:
44 case SCLP_CMD_READ_EVENT_DATA:
45 case SCLP_CMD_WRITE_EVENT_DATA:
46 case SCLP_CMD_WRITE_EVENT_MASK:
47 return true;
48 }
49 return false;
50}
51
52static void prepare_cpu_entries(SCLPDevice *sclp, CPUEntry *entry, int *count)
53{
54 MachineState *ms = MACHINE(qdev_get_machine());
55 uint8_t features[SCCB_CPU_FEATURE_LEN] = { 0 };
56 int i;
57
58 s390_get_feat_block(S390_FEAT_TYPE_SCLP_CPU, features);
59 for (i = 0, *count = 0; i < ms->possible_cpus->len; i++) {
60 if (!ms->possible_cpus->cpus[i].cpu) {
61 continue;
62 }
63 entry[*count].address = ms->possible_cpus->cpus[i].arch_id;
64 entry[*count].type = 0;
65 memcpy(entry[*count].features, features, sizeof(features));
66 (*count)++;
67 }
68}
69
70
71static void read_SCP_info(SCLPDevice *sclp, SCCB *sccb)
72{
73 ReadInfo *read_info = (ReadInfo *) sccb;
74 MachineState *machine = MACHINE(qdev_get_machine());
75 int cpu_count;
76 int rnsize, rnmax;
77 IplParameterBlock *ipib = s390_ipl_get_iplb();
78
79
80 prepare_cpu_entries(sclp, read_info->entries, &cpu_count);
81 read_info->entries_cpu = cpu_to_be16(cpu_count);
82 read_info->offset_cpu = cpu_to_be16(offsetof(ReadInfo, entries));
83 read_info->highest_cpu = cpu_to_be16(machine->smp.max_cpus - 1);
84
85 read_info->ibc_val = cpu_to_be32(s390_get_ibc_val());
86
87 if (be16_to_cpu(sccb->h.length) <
88 (sizeof(ReadInfo) + cpu_count * sizeof(CPUEntry))) {
89 sccb->h.response_code = cpu_to_be16(SCLP_RC_INSUFFICIENT_SCCB_LENGTH);
90 return;
91 }
92
93
94 s390_get_feat_block(S390_FEAT_TYPE_SCLP_CONF_CHAR,
95 read_info->conf_char);
96 s390_get_feat_block(S390_FEAT_TYPE_SCLP_CONF_CHAR_EXT,
97 read_info->conf_char_ext);
98
99 read_info->facilities = cpu_to_be64(SCLP_HAS_CPU_INFO |
100 SCLP_HAS_IOA_RECONFIG);
101
102 read_info->mha_pow = s390_get_mha_pow();
103 read_info->hmfai = cpu_to_be32(s390_get_hmfai());
104
105 rnsize = 1 << (sclp->increment_size - 20);
106 if (rnsize <= 128) {
107 read_info->rnsize = rnsize;
108 } else {
109 read_info->rnsize = 0;
110 read_info->rnsize2 = cpu_to_be32(rnsize);
111 }
112
113
114 rnmax = machine->ram_size >> sclp->increment_size;
115 if (rnmax < 0x10000) {
116 read_info->rnmax = cpu_to_be16(rnmax);
117 } else {
118 read_info->rnmax = cpu_to_be16(0);
119 read_info->rnmax2 = cpu_to_be64(rnmax);
120 }
121
122 if (ipib && ipib->flags & DIAG308_FLAGS_LP_VALID) {
123 memcpy(&read_info->loadparm, &ipib->loadparm,
124 sizeof(read_info->loadparm));
125 } else {
126 s390_ipl_set_loadparm(read_info->loadparm);
127 }
128
129 sccb->h.response_code = cpu_to_be16(SCLP_RC_NORMAL_READ_COMPLETION);
130}
131
132
133static void sclp_read_cpu_info(SCLPDevice *sclp, SCCB *sccb)
134{
135 ReadCpuInfo *cpu_info = (ReadCpuInfo *) sccb;
136 int cpu_count;
137
138 prepare_cpu_entries(sclp, cpu_info->entries, &cpu_count);
139 cpu_info->nr_configured = cpu_to_be16(cpu_count);
140 cpu_info->offset_configured = cpu_to_be16(offsetof(ReadCpuInfo, entries));
141 cpu_info->nr_standby = cpu_to_be16(0);
142
143 if (be16_to_cpu(sccb->h.length) <
144 (sizeof(ReadCpuInfo) + cpu_count * sizeof(CPUEntry))) {
145 sccb->h.response_code = cpu_to_be16(SCLP_RC_INSUFFICIENT_SCCB_LENGTH);
146 return;
147 }
148
149
150 cpu_info->offset_standby = cpu_to_be16(cpu_info->offset_configured
151 + cpu_info->nr_configured*sizeof(CPUEntry));
152
153
154 sccb->h.response_code = cpu_to_be16(SCLP_RC_NORMAL_READ_COMPLETION);
155}
156
157static void sclp_configure_io_adapter(SCLPDevice *sclp, SCCB *sccb,
158 bool configure)
159{
160 int rc;
161
162 if (be16_to_cpu(sccb->h.length) < 16) {
163 rc = SCLP_RC_INSUFFICIENT_SCCB_LENGTH;
164 goto out_err;
165 }
166
167 switch (((IoaCfgSccb *)sccb)->atype) {
168 case SCLP_RECONFIG_PCI_ATYPE:
169 if (s390_has_feat(S390_FEAT_ZPCI)) {
170 if (configure) {
171 s390_pci_sclp_configure(sccb);
172 } else {
173 s390_pci_sclp_deconfigure(sccb);
174 }
175 return;
176 }
177
178 default:
179 rc = SCLP_RC_ADAPTER_TYPE_NOT_RECOGNIZED;
180 }
181
182 out_err:
183 sccb->h.response_code = cpu_to_be16(rc);
184}
185
186static void sclp_execute(SCLPDevice *sclp, SCCB *sccb, uint32_t code)
187{
188 SCLPDeviceClass *sclp_c = SCLP_GET_CLASS(sclp);
189 SCLPEventFacility *ef = sclp->event_facility;
190 SCLPEventFacilityClass *efc = EVENT_FACILITY_GET_CLASS(ef);
191
192 switch (code & SCLP_CMD_CODE_MASK) {
193 case SCLP_CMDW_READ_SCP_INFO:
194 case SCLP_CMDW_READ_SCP_INFO_FORCED:
195 sclp_c->read_SCP_info(sclp, sccb);
196 break;
197 case SCLP_CMDW_READ_CPU_INFO:
198 sclp_c->read_cpu_info(sclp, sccb);
199 break;
200 case SCLP_CMDW_CONFIGURE_IOA:
201 sclp_configure_io_adapter(sclp, sccb, true);
202 break;
203 case SCLP_CMDW_DECONFIGURE_IOA:
204 sclp_configure_io_adapter(sclp, sccb, false);
205 break;
206 default:
207 efc->command_handler(ef, sccb, code);
208 break;
209 }
210}
211
212
213
214
215
216#define SCLP_PV_DUMMY_ADDR 0x4000
217int sclp_service_call_protected(CPUS390XState *env, uint64_t sccb,
218 uint32_t code)
219{
220 SCLPDevice *sclp = get_sclp_device();
221 SCLPDeviceClass *sclp_c = SCLP_GET_CLASS(sclp);
222 SCCB work_sccb;
223 hwaddr sccb_len = sizeof(SCCB);
224
225 s390_cpu_pv_mem_read(env_archcpu(env), 0, &work_sccb, sccb_len);
226
227 if (!sclp_command_code_valid(code)) {
228 work_sccb.h.response_code = cpu_to_be16(SCLP_RC_INVALID_SCLP_COMMAND);
229 goto out_write;
230 }
231
232 sclp_c->execute(sclp, &work_sccb, code);
233out_write:
234 s390_cpu_pv_mem_write(env_archcpu(env), 0, &work_sccb,
235 be16_to_cpu(work_sccb.h.length));
236 sclp_c->service_interrupt(sclp, SCLP_PV_DUMMY_ADDR);
237 return 0;
238}
239
240int sclp_service_call(CPUS390XState *env, uint64_t sccb, uint32_t code)
241{
242 SCLPDevice *sclp = get_sclp_device();
243 SCLPDeviceClass *sclp_c = SCLP_GET_CLASS(sclp);
244 SCCB work_sccb;
245
246 hwaddr sccb_len = sizeof(SCCB);
247
248
249 if (env->psw.mask & PSW_MASK_PSTATE) {
250 return -PGM_PRIVILEGED;
251 }
252 if (cpu_physical_memory_is_io(sccb)) {
253 return -PGM_ADDRESSING;
254 }
255 if ((sccb & ~0x1fffUL) == 0 || (sccb & ~0x1fffUL) == env->psa
256 || (sccb & ~0x7ffffff8UL) != 0) {
257 return -PGM_SPECIFICATION;
258 }
259
260
261
262
263
264
265 cpu_physical_memory_read(sccb, &work_sccb, sccb_len);
266
267
268 if (be16_to_cpu(work_sccb.h.length) < sizeof(SCCBHeader)) {
269 return -PGM_SPECIFICATION;
270 }
271
272 if (!sclp_command_code_valid(code)) {
273 work_sccb.h.response_code = cpu_to_be16(SCLP_RC_INVALID_SCLP_COMMAND);
274 goto out_write;
275 }
276
277 if ((sccb + be16_to_cpu(work_sccb.h.length)) > ((sccb & PAGE_MASK) + PAGE_SIZE)) {
278 work_sccb.h.response_code = cpu_to_be16(SCLP_RC_SCCB_BOUNDARY_VIOLATION);
279 goto out_write;
280 }
281
282 sclp_c->execute(sclp, &work_sccb, code);
283out_write:
284 cpu_physical_memory_write(sccb, &work_sccb,
285 be16_to_cpu(work_sccb.h.length));
286
287 sclp_c->service_interrupt(sclp, sccb);
288
289 return 0;
290}
291
292static void service_interrupt(SCLPDevice *sclp, uint32_t sccb)
293{
294 SCLPEventFacility *ef = sclp->event_facility;
295 SCLPEventFacilityClass *efc = EVENT_FACILITY_GET_CLASS(ef);
296
297 uint32_t param = sccb & ~3;
298
299
300 param |= efc->event_pending(ef) ? 1 : 0;
301
302 if (!param) {
303
304 return;
305 }
306 s390_sclp_extint(param);
307}
308
309void sclp_service_interrupt(uint32_t sccb)
310{
311 SCLPDevice *sclp = get_sclp_device();
312 SCLPDeviceClass *sclp_c = SCLP_GET_CLASS(sclp);
313
314 sclp_c->service_interrupt(sclp, sccb);
315}
316
317
318
319void s390_sclp_init(void)
320{
321 Object *new = object_new(TYPE_SCLP);
322
323 object_property_add_child(qdev_get_machine(), TYPE_SCLP, new);
324 object_unref(new);
325 qdev_realize(DEVICE(new), NULL, &error_fatal);
326}
327
328static void sclp_realize(DeviceState *dev, Error **errp)
329{
330 MachineState *machine = MACHINE(qdev_get_machine());
331 SCLPDevice *sclp = SCLP(dev);
332 uint64_t hw_limit;
333 int ret;
334
335
336
337
338
339
340 if (!sysbus_realize(SYS_BUS_DEVICE(sclp->event_facility), errp)) {
341 return;
342 }
343
344 ret = s390_set_memory_limit(machine->maxram_size, &hw_limit);
345 if (ret == -E2BIG) {
346 error_setg(errp, "host supports a maximum of %" PRIu64 " GB",
347 hw_limit / GiB);
348 } else if (ret) {
349 error_setg(errp, "setting the guest size failed");
350 }
351}
352
353static void sclp_memory_init(SCLPDevice *sclp)
354{
355 MachineState *machine = MACHINE(qdev_get_machine());
356 MachineClass *machine_class = MACHINE_GET_CLASS(qdev_get_machine());
357 ram_addr_t initial_mem = machine->ram_size;
358 int increment_size = 20;
359
360
361
362
363
364
365 while (machine_class->fixup_ram_size != NULL &&
366 (initial_mem >> increment_size) > MAX_STORAGE_INCREMENTS) {
367 increment_size++;
368 }
369 sclp->increment_size = increment_size;
370}
371
372static void sclp_init(Object *obj)
373{
374 SCLPDevice *sclp = SCLP(obj);
375 Object *new;
376
377 new = object_new(TYPE_SCLP_EVENT_FACILITY);
378 object_property_add_child(obj, TYPE_SCLP_EVENT_FACILITY, new);
379 object_unref(new);
380 sclp->event_facility = EVENT_FACILITY(new);
381
382 sclp_memory_init(sclp);
383}
384
385static void sclp_class_init(ObjectClass *oc, void *data)
386{
387 SCLPDeviceClass *sc = SCLP_CLASS(oc);
388 DeviceClass *dc = DEVICE_CLASS(oc);
389
390 dc->desc = "SCLP (Service-Call Logical Processor)";
391 dc->realize = sclp_realize;
392 dc->hotpluggable = false;
393 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
394
395
396
397
398 dc->user_creatable = false;
399
400 sc->read_SCP_info = read_SCP_info;
401 sc->read_cpu_info = sclp_read_cpu_info;
402 sc->execute = sclp_execute;
403 sc->service_interrupt = service_interrupt;
404}
405
406static TypeInfo sclp_info = {
407 .name = TYPE_SCLP,
408 .parent = TYPE_DEVICE,
409 .instance_init = sclp_init,
410 .instance_size = sizeof(SCLPDevice),
411 .class_init = sclp_class_init,
412 .class_size = sizeof(SCLPDeviceClass),
413};
414
415static void register_types(void)
416{
417 type_register_static(&sclp_info);
418}
419type_init(register_types);
420