| 1 | /* |
| 2 | * SCLP Support |
| 3 | * |
| 4 | * Copyright IBM, Corp. 2012 |
| 5 | * |
| 6 | * Authors: |
| 7 | * Christian Borntraeger <borntraeger@de.ibm.com> |
| 8 | * Heinz Graalfs <graalfs@linux.vnet.ibm.com> |
| 9 | * |
| 10 | * This work is licensed under the terms of the GNU GPL, version 2 or (at your |
| 11 | * option) any later version. See the COPYING file in the top-level directory. |
| 12 | * |
| 13 | */ |
| 14 | |
| 15 | #include "qemu/osdep.h" |
| 16 | #include "qemu/units.h" |
| 17 | #include "qapi/error.h" |
| 18 | #include "hw/core/boards.h" |
| 19 | #include "system/memory.h" |
| 20 | #include "hw/s390x/sclp.h" |
| 21 | #include "hw/s390x/event-facility.h" |
| 22 | #include "hw/s390x/s390-pci-bus.h" |
| 23 | #include "hw/s390x/ipl.h" |
| 24 | #include "hw/s390x/cpu-topology.h" |
| 25 | #include "hw/s390x/s390-virtio-ccw.h" |
| 26 | |
| 27 | static SCLPDevice *get_sclp_device(void) |
| 28 | { |
| 29 | static SCLPDevice *sclp; |
| 30 | |
| 31 | if (!sclp) { |
| 32 | sclp = S390_CCW_MACHINE(qdev_get_machine())->sclp; |
| 33 | } |
| 34 | return sclp; |
| 35 | } |
| 36 | |
| 37 | static inline bool sclp_command_code_valid(uint32_t code) |
| 38 | { |
| 39 | switch (code & SCLP_CMD_CODE_MASK) { |
| 40 | case SCLP_CMDW_READ_SCP_INFO: |
| 41 | case SCLP_CMDW_READ_SCP_INFO_FORCED: |
| 42 | case SCLP_CMDW_READ_CPU_INFO: |
| 43 | case SCLP_CMDW_CONFIGURE_IOA: |
| 44 | case SCLP_CMDW_DECONFIGURE_IOA: |
| 45 | case SCLP_CMD_READ_EVENT_DATA: |
| 46 | case SCLP_CMD_WRITE_EVENT_DATA: |
| 47 | case SCLP_CMD_WRITE_EVENT_MASK: |
| 48 | return true; |
| 49 | } |
| 50 | return false; |
| 51 | } |
| 52 | |
| 53 | static bool sccb_verify_boundary(uint64_t sccb_addr, uint16_t sccb_len, |
| 54 | uint32_t code) |
| 55 | { |
| 56 | uint64_t sccb_max_addr = sccb_addr + sccb_len - 1; |
| 57 | uint64_t sccb_boundary = (sccb_addr & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE; |
| 58 | |
| 59 | switch (code & SCLP_CMD_CODE_MASK) { |
| 60 | case SCLP_CMDW_READ_SCP_INFO: |
| 61 | case SCLP_CMDW_READ_SCP_INFO_FORCED: |
| 62 | case SCLP_CMDW_READ_CPU_INFO: |
| 63 | /* |
| 64 | * An extended-length SCCB is only allowed for Read SCP/CPU Info and |
| 65 | * is allowed to exceed the 4k boundary. The respective commands will |
| 66 | * set the length field to the required length if an insufficient |
| 67 | * SCCB length is provided. |
| 68 | */ |
| 69 | if (s390_has_feat(S390_FEAT_EXTENDED_LENGTH_SCCB)) { |
| 70 | return true; |
| 71 | } |
| 72 | /* fallthrough */ |
| 73 | default: |
| 74 | if (sccb_max_addr < sccb_boundary) { |
| 75 | return true; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | static void prepare_cpu_entries(MachineState *ms, CPUEntry *entry, int *count) |
| 83 | { |
| 84 | uint8_t features[SCCB_CPU_FEATURE_LEN] = { 0 }; |
| 85 | int i; |
| 86 | |
| 87 | s390_get_feat_block(S390_FEAT_TYPE_SCLP_CPU, features); |
| 88 | for (i = 0, *count = 0; i < ms->possible_cpus->len; i++) { |
| 89 | if (!ms->possible_cpus->cpus[i].cpu) { |
| 90 | continue; |
| 91 | } |
| 92 | entry[*count].address = ms->possible_cpus->cpus[i].arch_id; |
| 93 | entry[*count].type = 0; |
| 94 | memcpy(entry[*count].features, features, sizeof(features)); |
| 95 | (*count)++; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | #define SCCB_REQ_LEN(s, max_cpus) (sizeof(s) + max_cpus * sizeof(CPUEntry)) |
| 100 | |
| 101 | static inline bool ext_len_sccb_supported(SCCBHeader header) |
| 102 | { |
| 103 | return s390_has_feat(S390_FEAT_EXTENDED_LENGTH_SCCB) && |
| 104 | header.control_mask[2] & SCLP_VARIABLE_LENGTH_RESPONSE; |
| 105 | } |
| 106 | |
| 107 | /* Provide information about the configuration, CPUs and storage */ |
| 108 | static void read_SCP_info(SCLPDevice *sclp, SCCB *sccb) |
| 109 | { |
| 110 | ReadInfo *read_info = (ReadInfo *) sccb; |
| 111 | MachineState *machine = MACHINE(qdev_get_machine()); |
| 112 | int cpu_count; |
| 113 | int rnmax; |
| 114 | int required_len = SCCB_REQ_LEN(ReadInfo, machine->possible_cpus->len); |
| 115 | int offset_cpu = s390_has_feat(S390_FEAT_EXTENDED_LENGTH_SCCB) ? |
| 116 | offsetof(ReadInfo, entries) : |
| 117 | SCLP_READ_SCP_INFO_FIXED_CPU_OFFSET; |
| 118 | CPUEntry *entries_start = (void *)sccb + offset_cpu; |
| 119 | |
| 120 | if (be16_to_cpu(sccb->h.length) < required_len) { |
| 121 | if (ext_len_sccb_supported(sccb->h)) { |
| 122 | sccb->h.length = cpu_to_be16(required_len); |
| 123 | } |
| 124 | sccb->h.response_code = cpu_to_be16(SCLP_RC_INSUFFICIENT_SCCB_LENGTH); |
| 125 | return; |
| 126 | } |
| 127 | |
| 128 | if (s390_has_topology()) { |
| 129 | read_info->stsi_parm = SCLP_READ_SCP_INFO_MNEST; |
| 130 | } |
| 131 | |
| 132 | /* CPU information */ |
| 133 | prepare_cpu_entries(machine, entries_start, &cpu_count); |
| 134 | read_info->entries_cpu = cpu_to_be16(cpu_count); |
| 135 | read_info->offset_cpu = cpu_to_be16(offset_cpu); |
| 136 | read_info->highest_cpu = cpu_to_be16(machine->smp.max_cpus - 1); |
| 137 | |
| 138 | read_info->ibc_val = cpu_to_be32(s390_get_ibc_val()); |
| 139 | |
| 140 | /* Configuration Characteristic (Extension) */ |
| 141 | s390_get_feat_block(S390_FEAT_TYPE_SCLP_CONF_CHAR, |
| 142 | read_info->conf_char); |
| 143 | s390_get_feat_block(S390_FEAT_TYPE_SCLP_CONF_CHAR_EXT, |
| 144 | read_info->conf_char_ext); |
| 145 | |
| 146 | if (s390_has_feat(S390_FEAT_EXTENDED_LENGTH_SCCB)) { |
| 147 | s390_get_feat_block(S390_FEAT_TYPE_SCLP_FAC134, |
| 148 | &read_info->fac134); |
| 149 | s390_get_feat_block(S390_FEAT_TYPE_SCLP_FAC_IPL, |
| 150 | read_info->fac_ipl); |
| 151 | s390_get_feat_block(S390_FEAT_TYPE_SCLP_FAC139, |
| 152 | &read_info->fac139); |
| 153 | } |
| 154 | |
| 155 | read_info->facilities = cpu_to_be64(SCLP_HAS_CPU_INFO | |
| 156 | SCLP_HAS_IOA_RECONFIG); |
| 157 | |
| 158 | read_info->mha_pow = s390_get_mha_pow(); |
| 159 | read_info->hmfai = cpu_to_be32(s390_get_hmfai()); |
| 160 | read_info->rnsize = 1; |
| 161 | |
| 162 | /* |
| 163 | * We don't support standby memory. maxram_size is used for sizing the |
| 164 | * memory device region, which is not exposed through SCLP but through |
| 165 | * diag500. |
| 166 | */ |
| 167 | rnmax = machine->ram_size >> 20; |
| 168 | if (rnmax < 0x10000) { |
| 169 | read_info->rnmax = cpu_to_be16(rnmax); |
| 170 | } else { |
| 171 | read_info->rnmax = cpu_to_be16(0); |
| 172 | read_info->rnmax2 = cpu_to_be64(rnmax); |
| 173 | } |
| 174 | |
| 175 | s390_ipl_convert_loadparm((char *)S390_CCW_MACHINE(machine)->loadparm, |
| 176 | read_info->loadparm); |
| 177 | |
| 178 | sccb->h.response_code = cpu_to_be16(SCLP_RC_NORMAL_READ_COMPLETION); |
| 179 | } |
| 180 | |
| 181 | /* Provide information about the CPU */ |
| 182 | static void sclp_read_cpu_info(SCLPDevice *sclp, SCCB *sccb) |
| 183 | { |
| 184 | MachineState *machine = MACHINE(qdev_get_machine()); |
| 185 | ReadCpuInfo *cpu_info = (ReadCpuInfo *) sccb; |
| 186 | int cpu_count; |
| 187 | int required_len = SCCB_REQ_LEN(ReadCpuInfo, machine->possible_cpus->len); |
| 188 | |
| 189 | if (be16_to_cpu(sccb->h.length) < required_len) { |
| 190 | if (ext_len_sccb_supported(sccb->h)) { |
| 191 | sccb->h.length = cpu_to_be16(required_len); |
| 192 | } |
| 193 | sccb->h.response_code = cpu_to_be16(SCLP_RC_INSUFFICIENT_SCCB_LENGTH); |
| 194 | return; |
| 195 | } |
| 196 | |
| 197 | prepare_cpu_entries(machine, cpu_info->entries, &cpu_count); |
| 198 | cpu_info->nr_configured = cpu_to_be16(cpu_count); |
| 199 | cpu_info->offset_configured = cpu_to_be16(offsetof(ReadCpuInfo, entries)); |
| 200 | cpu_info->nr_standby = cpu_to_be16(0); |
| 201 | |
| 202 | /* The standby offset is 16-byte for each CPU */ |
| 203 | cpu_info->offset_standby = cpu_to_be16(cpu_info->offset_configured |
| 204 | + cpu_info->nr_configured*sizeof(CPUEntry)); |
| 205 | |
| 206 | |
| 207 | sccb->h.response_code = cpu_to_be16(SCLP_RC_NORMAL_READ_COMPLETION); |
| 208 | } |
| 209 | |
| 210 | static void sclp_configure_io_adapter(SCLPDevice *sclp, SCCB *sccb, |
| 211 | bool configure) |
| 212 | { |
| 213 | int rc; |
| 214 | |
| 215 | if (be16_to_cpu(sccb->h.length) < 16) { |
| 216 | rc = SCLP_RC_INSUFFICIENT_SCCB_LENGTH; |
| 217 | goto out_err; |
| 218 | } |
| 219 | |
| 220 | switch (((IoaCfgSccb *)sccb)->atype) { |
| 221 | case SCLP_RECONFIG_PCI_ATYPE: |
| 222 | if (s390_has_feat(S390_FEAT_ZPCI)) { |
| 223 | if (configure) { |
| 224 | s390_pci_sclp_configure(sccb); |
| 225 | } else { |
| 226 | s390_pci_sclp_deconfigure(sccb); |
| 227 | } |
| 228 | return; |
| 229 | } |
| 230 | /* fallthrough */ |
| 231 | default: |
| 232 | rc = SCLP_RC_ADAPTER_TYPE_NOT_RECOGNIZED; |
| 233 | } |
| 234 | |
| 235 | out_err: |
| 236 | sccb->h.response_code = cpu_to_be16(rc); |
| 237 | } |
| 238 | |
| 239 | static void sclp_execute(SCLPDevice *sclp, SCCB *sccb, uint32_t code) |
| 240 | { |
| 241 | SCLPDeviceClass *sclp_c = SCLP_GET_CLASS(sclp); |
| 242 | SCLPEventFacility *ef = sclp->event_facility; |
| 243 | SCLPEventFacilityClass *efc = EVENT_FACILITY_GET_CLASS(ef); |
| 244 | |
| 245 | switch (code & SCLP_CMD_CODE_MASK) { |
| 246 | case SCLP_CMDW_READ_SCP_INFO: |
| 247 | case SCLP_CMDW_READ_SCP_INFO_FORCED: |
| 248 | sclp_c->read_SCP_info(sclp, sccb); |
| 249 | break; |
| 250 | case SCLP_CMDW_READ_CPU_INFO: |
| 251 | sclp_c->read_cpu_info(sclp, sccb); |
| 252 | break; |
| 253 | case SCLP_CMDW_CONFIGURE_IOA: |
| 254 | sclp_configure_io_adapter(sclp, sccb, true); |
| 255 | break; |
| 256 | case SCLP_CMDW_DECONFIGURE_IOA: |
| 257 | sclp_configure_io_adapter(sclp, sccb, false); |
| 258 | break; |
| 259 | default: |
| 260 | efc->command_handler(ef, sccb, code); |
| 261 | break; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | /* |
| 266 | * We only need the address to have something valid for the |
| 267 | * service_interrupt call. |
| 268 | */ |
| 269 | #define SCLP_PV_DUMMY_ADDR 0x4000 |
| 270 | int sclp_service_call_protected(S390CPU *cpu, uint64_t sccb, uint32_t code) |
| 271 | { |
| 272 | CPUS390XState *env = &cpu->env; |
| 273 | SCLPDevice *sclp = get_sclp_device(); |
| 274 | SCLPDeviceClass *sclp_c = SCLP_GET_CLASS(sclp); |
| 275 | SCCBHeader header; |
| 276 | g_autofree SCCB *work_sccb = NULL; |
| 277 | |
| 278 | s390_cpu_pv_mem_read(env_archcpu(env), 0, &header, sizeof(SCCBHeader)); |
| 279 | |
| 280 | work_sccb = g_malloc0(be16_to_cpu(header.length)); |
| 281 | s390_cpu_pv_mem_read(env_archcpu(env), 0, work_sccb, |
| 282 | be16_to_cpu(header.length)); |
| 283 | |
| 284 | if (!sclp_command_code_valid(code)) { |
| 285 | work_sccb->h.response_code = cpu_to_be16(SCLP_RC_INVALID_SCLP_COMMAND); |
| 286 | goto out_write; |
| 287 | } |
| 288 | |
| 289 | sclp_c->execute(sclp, work_sccb, code); |
| 290 | out_write: |
| 291 | s390_cpu_pv_mem_write(env_archcpu(env), 0, work_sccb, |
| 292 | be16_to_cpu(header.length)); |
| 293 | sclp_c->service_interrupt(sclp, SCLP_PV_DUMMY_ADDR); |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | int sclp_service_call(S390CPU *cpu, uint64_t sccb, uint32_t code) |
| 298 | { |
| 299 | CPUS390XState *env = &cpu->env; |
| 300 | SCLPDevice *sclp = get_sclp_device(); |
| 301 | SCLPDeviceClass *sclp_c = SCLP_GET_CLASS(sclp); |
| 302 | SCCBHeader header; |
| 303 | g_autofree SCCB *work_sccb = NULL; |
| 304 | AddressSpace *as = CPU(cpu)->as; |
| 305 | const MemTxAttrs attrs = MEMTXATTRS_UNSPECIFIED; |
| 306 | MemTxResult ret; |
| 307 | |
| 308 | /* first some basic checks on program checks */ |
| 309 | if (env->psw.mask & PSW_MASK_PSTATE) { |
| 310 | return -PGM_PRIVILEGED; |
| 311 | } |
| 312 | if (address_space_is_io(CPU(cpu)->as, sccb)) { |
| 313 | return -PGM_ADDRESSING; |
| 314 | } |
| 315 | if ((sccb & ~0x1fffUL) == 0 || (sccb & ~0x1fffUL) == env->psa |
| 316 | || (sccb & ~0x7ffffff8UL) != 0) { |
| 317 | return -PGM_SPECIFICATION; |
| 318 | } |
| 319 | |
| 320 | /* the header contains the actual length of the sccb */ |
| 321 | ret = address_space_read(as, sccb, attrs, &header, sizeof(SCCBHeader)); |
| 322 | if (ret != MEMTX_OK) { |
| 323 | return -PGM_ADDRESSING; |
| 324 | } |
| 325 | |
| 326 | /* Valid sccb sizes */ |
| 327 | if (be16_to_cpu(header.length) < sizeof(SCCBHeader)) { |
| 328 | return -PGM_SPECIFICATION; |
| 329 | } |
| 330 | |
| 331 | /* |
| 332 | * we want to work on a private copy of the sccb, to prevent guests |
| 333 | * from playing dirty tricks by modifying the memory content after |
| 334 | * the host has checked the values. |
| 335 | * Reuse the previously fetched header |
| 336 | */ |
| 337 | work_sccb = g_malloc0(be16_to_cpu(header.length)); |
| 338 | ret = address_space_read(as, sccb, attrs, |
| 339 | work_sccb, be16_to_cpu(header.length)); |
| 340 | if (ret != MEMTX_OK) { |
| 341 | return -PGM_ADDRESSING; |
| 342 | } |
| 343 | work_sccb->h = header; |
| 344 | |
| 345 | if (!sclp_command_code_valid(code)) { |
| 346 | work_sccb->h.response_code = cpu_to_be16(SCLP_RC_INVALID_SCLP_COMMAND); |
| 347 | goto out_write; |
| 348 | } |
| 349 | |
| 350 | if (!sccb_verify_boundary(sccb, be16_to_cpu(work_sccb->h.length), code)) { |
| 351 | work_sccb->h.response_code = cpu_to_be16(SCLP_RC_SCCB_BOUNDARY_VIOLATION); |
| 352 | goto out_write; |
| 353 | } |
| 354 | |
| 355 | sclp_c->execute(sclp, work_sccb, code); |
| 356 | out_write: |
| 357 | ret = address_space_write(as, sccb, attrs, |
| 358 | work_sccb, be16_to_cpu(header.length)); |
| 359 | if (ret != MEMTX_OK) { |
| 360 | return -PGM_PROTECTION; |
| 361 | } |
| 362 | |
| 363 | sclp_c->service_interrupt(sclp, sccb); |
| 364 | |
| 365 | return 0; |
| 366 | } |
| 367 | |
| 368 | static void service_interrupt(SCLPDevice *sclp, uint32_t sccb) |
| 369 | { |
| 370 | SCLPEventFacility *ef = sclp->event_facility; |
| 371 | SCLPEventFacilityClass *efc = EVENT_FACILITY_GET_CLASS(ef); |
| 372 | |
| 373 | uint32_t param = sccb & ~3; |
| 374 | |
| 375 | /* Indicate whether an event is still pending */ |
| 376 | param |= efc->event_pending(ef) ? 1 : 0; |
| 377 | |
| 378 | if (!param) { |
| 379 | /* No need to send an interrupt, there's nothing to be notified about */ |
| 380 | return; |
| 381 | } |
| 382 | s390_sclp_extint(param); |
| 383 | } |
| 384 | |
| 385 | void sclp_service_interrupt(uint32_t sccb) |
| 386 | { |
| 387 | SCLPDevice *sclp = get_sclp_device(); |
| 388 | SCLPDeviceClass *sclp_c = SCLP_GET_CLASS(sclp); |
| 389 | |
| 390 | sclp_c->service_interrupt(sclp, sccb); |
| 391 | } |
| 392 | |
| 393 | /* qemu object creation and initialization functions */ |
| 394 | static void sclp_realize(DeviceState *dev, Error **errp) |
| 395 | { |
| 396 | SCLPDevice *sclp = SCLP(dev); |
| 397 | |
| 398 | /* |
| 399 | * qdev_device_add searches the sysbus for TYPE_SCLP_EVENTS_BUS. As long |
| 400 | * as we can't find a fitting bus via the qom tree, we have to add the |
| 401 | * event facility to the sysbus, so e.g. a sclp console can be created. |
| 402 | */ |
| 403 | if (!sysbus_realize(SYS_BUS_DEVICE(sclp->event_facility), errp)) { |
| 404 | return; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | static void sclp_init(Object *obj) |
| 409 | { |
| 410 | SCLPDevice *sclp = SCLP(obj); |
| 411 | Object *new; |
| 412 | |
| 413 | new = object_new(TYPE_SCLP_EVENT_FACILITY); |
| 414 | object_property_add_child(obj, TYPE_SCLP_EVENT_FACILITY, new); |
| 415 | object_unref(new); |
| 416 | sclp->event_facility = EVENT_FACILITY(new); |
| 417 | } |
| 418 | |
| 419 | static void sclp_class_init(ObjectClass *oc, const void *data) |
| 420 | { |
| 421 | SCLPDeviceClass *sc = SCLP_CLASS(oc); |
| 422 | DeviceClass *dc = DEVICE_CLASS(oc); |
| 423 | |
| 424 | dc->desc = "SCLP (Service-Call Logical Processor)"; |
| 425 | dc->realize = sclp_realize; |
| 426 | dc->hotpluggable = false; |
| 427 | set_bit(DEVICE_CATEGORY_MISC, dc->categories); |
| 428 | /* |
| 429 | * Reason: Creates TYPE_SCLP_EVENT_FACILITY in sclp_init |
| 430 | * which is a non-pluggable sysbus device |
| 431 | */ |
| 432 | dc->user_creatable = false; |
| 433 | |
| 434 | sc->read_SCP_info = read_SCP_info; |
| 435 | sc->read_cpu_info = sclp_read_cpu_info; |
| 436 | sc->execute = sclp_execute; |
| 437 | sc->service_interrupt = service_interrupt; |
| 438 | } |
| 439 | |
| 440 | static const TypeInfo sclp_info = { |
| 441 | .name = TYPE_SCLP, |
| 442 | .parent = TYPE_DEVICE, |
| 443 | .instance_init = sclp_init, |
| 444 | .instance_size = sizeof(SCLPDevice), |
| 445 | .class_init = sclp_class_init, |
| 446 | .class_size = sizeof(SCLPDeviceClass), |
| 447 | }; |
| 448 | |
| 449 | static void register_types(void) |
| 450 | { |
| 451 | type_register_static(&sclp_info); |
| 452 | } |
| 453 | type_init(register_types); |