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 "qapi/error.h" 17 #include "cpu.h" 18 #include "sysemu/sysemu.h" 19 #include "hw/boards.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 25 static inline SCLPDevice *get_sclp_device(void) 26 { 27 static SCLPDevice *sclp; 28 29 if (!sclp) { 30 sclp = SCLP(object_resolve_path_type("", TYPE_SCLP, NULL)); 31 } 32 return sclp; 33 } 34 35 static void prepare_cpu_entries(SCLPDevice *sclp, CPUEntry *entry, int *count) 36 { 37 MachineState *ms = MACHINE(qdev_get_machine()); 38 uint8_t features[SCCB_CPU_FEATURE_LEN] = { 0 }; 39 int i; 40 41 s390_get_feat_block(S390_FEAT_TYPE_SCLP_CPU, features); 42 for (i = 0, *count = 0; i < ms->possible_cpus->len; i++) { 43 if (!ms->possible_cpus->cpus[i].cpu) { 44 continue; 45 } 46 entry[*count].address = ms->possible_cpus->cpus[i].arch_id; 47 entry[*count].type = 0; 48 memcpy(entry[*count].features, features, sizeof(features)); 49 (*count)++; 50 } 51 } 52 53 /* Provide information about the configuration, CPUs and storage */ 54 static void read_SCP_info(SCLPDevice *sclp, SCCB *sccb) 55 { 56 ReadInfo *read_info = (ReadInfo *) sccb; 57 MachineState *machine = MACHINE(qdev_get_machine()); 58 int cpu_count; 59 int rnsize, rnmax; 60 IplParameterBlock *ipib = s390_ipl_get_iplb(); 61 62 /* CPU information */ 63 prepare_cpu_entries(sclp, read_info->entries, &cpu_count); 64 read_info->entries_cpu = cpu_to_be16(cpu_count); 65 read_info->offset_cpu = cpu_to_be16(offsetof(ReadInfo, entries)); 66 read_info->highest_cpu = cpu_to_be16(max_cpus - 1); 67 68 read_info->ibc_val = cpu_to_be32(s390_get_ibc_val()); 69 70 /* Configuration Characteristic (Extension) */ 71 s390_get_feat_block(S390_FEAT_TYPE_SCLP_CONF_CHAR, 72 read_info->conf_char); 73 s390_get_feat_block(S390_FEAT_TYPE_SCLP_CONF_CHAR_EXT, 74 read_info->conf_char_ext); 75 76 read_info->facilities = cpu_to_be64(SCLP_HAS_CPU_INFO | 77 SCLP_HAS_IOA_RECONFIG); 78 79 read_info->mha_pow = s390_get_mha_pow(); 80 read_info->hmfai = cpu_to_be32(s390_get_hmfai()); 81 82 rnsize = 1 << (sclp->increment_size - 20); 83 if (rnsize <= 128) { 84 read_info->rnsize = rnsize; 85 } else { 86 read_info->rnsize = 0; 87 read_info->rnsize2 = cpu_to_be32(rnsize); 88 } 89 90 /* we don't support standby memory, maxram_size is never exposed */ 91 rnmax = machine->ram_size >> sclp->increment_size; 92 if (rnmax < 0x10000) { 93 read_info->rnmax = cpu_to_be16(rnmax); 94 } else { 95 read_info->rnmax = cpu_to_be16(0); 96 read_info->rnmax2 = cpu_to_be64(rnmax); 97 } 98 99 if (ipib && ipib->flags & DIAG308_FLAGS_LP_VALID) { 100 memcpy(&read_info->loadparm, &ipib->loadparm, 101 sizeof(read_info->loadparm)); 102 } else { 103 s390_ipl_set_loadparm(read_info->loadparm); 104 } 105 106 sccb->h.response_code = cpu_to_be16(SCLP_RC_NORMAL_READ_COMPLETION); 107 } 108 109 /* Provide information about the CPU */ 110 static void sclp_read_cpu_info(SCLPDevice *sclp, SCCB *sccb) 111 { 112 ReadCpuInfo *cpu_info = (ReadCpuInfo *) sccb; 113 int cpu_count; 114 115 prepare_cpu_entries(sclp, cpu_info->entries, &cpu_count); 116 cpu_info->nr_configured = cpu_to_be16(cpu_count); 117 cpu_info->offset_configured = cpu_to_be16(offsetof(ReadCpuInfo, entries)); 118 cpu_info->nr_standby = cpu_to_be16(0); 119 120 /* The standby offset is 16-byte for each CPU */ 121 cpu_info->offset_standby = cpu_to_be16(cpu_info->offset_configured 122 + cpu_info->nr_configured*sizeof(CPUEntry)); 123 124 125 sccb->h.response_code = cpu_to_be16(SCLP_RC_NORMAL_READ_COMPLETION); 126 } 127 128 static void sclp_configure_io_adapter(SCLPDevice *sclp, SCCB *sccb, 129 bool configure) 130 { 131 int rc; 132 133 if (be16_to_cpu(sccb->h.length) < 16) { 134 rc = SCLP_RC_INSUFFICIENT_SCCB_LENGTH; 135 goto out_err; 136 } 137 138 switch (((IoaCfgSccb *)sccb)->atype) { 139 case SCLP_RECONFIG_PCI_ATYPE: 140 if (s390_has_feat(S390_FEAT_ZPCI)) { 141 if (configure) { 142 s390_pci_sclp_configure(sccb); 143 } else { 144 s390_pci_sclp_deconfigure(sccb); 145 } 146 return; 147 } 148 /* fallthrough */ 149 default: 150 rc = SCLP_RC_ADAPTER_TYPE_NOT_RECOGNIZED; 151 } 152 153 out_err: 154 sccb->h.response_code = cpu_to_be16(rc); 155 } 156 157 static void sclp_execute(SCLPDevice *sclp, SCCB *sccb, uint32_t code) 158 { 159 SCLPDeviceClass *sclp_c = SCLP_GET_CLASS(sclp); 160 SCLPEventFacility *ef = sclp->event_facility; 161 SCLPEventFacilityClass *efc = EVENT_FACILITY_GET_CLASS(ef); 162 163 switch (code & SCLP_CMD_CODE_MASK) { 164 case SCLP_CMDW_READ_SCP_INFO: 165 case SCLP_CMDW_READ_SCP_INFO_FORCED: 166 sclp_c->read_SCP_info(sclp, sccb); 167 break; 168 case SCLP_CMDW_READ_CPU_INFO: 169 sclp_c->read_cpu_info(sclp, sccb); 170 break; 171 case SCLP_CMDW_CONFIGURE_IOA: 172 sclp_configure_io_adapter(sclp, sccb, true); 173 break; 174 case SCLP_CMDW_DECONFIGURE_IOA: 175 sclp_configure_io_adapter(sclp, sccb, false); 176 break; 177 default: 178 efc->command_handler(ef, sccb, code); 179 break; 180 } 181 } 182 183 int sclp_service_call(CPUS390XState *env, uint64_t sccb, uint32_t code) 184 { 185 SCLPDevice *sclp = get_sclp_device(); 186 SCLPDeviceClass *sclp_c = SCLP_GET_CLASS(sclp); 187 int r = 0; 188 SCCB work_sccb; 189 190 hwaddr sccb_len = sizeof(SCCB); 191 192 /* first some basic checks on program checks */ 193 if (env->psw.mask & PSW_MASK_PSTATE) { 194 r = -PGM_PRIVILEGED; 195 goto out; 196 } 197 if (cpu_physical_memory_is_io(sccb)) { 198 r = -PGM_ADDRESSING; 199 goto out; 200 } 201 if ((sccb & ~0x1fffUL) == 0 || (sccb & ~0x1fffUL) == env->psa 202 || (sccb & ~0x7ffffff8UL) != 0) { 203 r = -PGM_SPECIFICATION; 204 goto out; 205 } 206 207 /* 208 * we want to work on a private copy of the sccb, to prevent guests 209 * from playing dirty tricks by modifying the memory content after 210 * the host has checked the values 211 */ 212 cpu_physical_memory_read(sccb, &work_sccb, sccb_len); 213 214 /* Valid sccb sizes */ 215 if (be16_to_cpu(work_sccb.h.length) < sizeof(SCCBHeader) || 216 be16_to_cpu(work_sccb.h.length) > SCCB_SIZE) { 217 r = -PGM_SPECIFICATION; 218 goto out; 219 } 220 221 sclp_c->execute(sclp, &work_sccb, code); 222 223 cpu_physical_memory_write(sccb, &work_sccb, 224 be16_to_cpu(work_sccb.h.length)); 225 226 sclp_c->service_interrupt(sclp, sccb); 227 228 out: 229 return r; 230 } 231 232 static void service_interrupt(SCLPDevice *sclp, uint32_t sccb) 233 { 234 SCLPEventFacility *ef = sclp->event_facility; 235 SCLPEventFacilityClass *efc = EVENT_FACILITY_GET_CLASS(ef); 236 237 uint32_t param = sccb & ~3; 238 239 /* Indicate whether an event is still pending */ 240 param |= efc->event_pending(ef) ? 1 : 0; 241 242 if (!param) { 243 /* No need to send an interrupt, there's nothing to be notified about */ 244 return; 245 } 246 s390_sclp_extint(param); 247 } 248 249 void sclp_service_interrupt(uint32_t sccb) 250 { 251 SCLPDevice *sclp = get_sclp_device(); 252 SCLPDeviceClass *sclp_c = SCLP_GET_CLASS(sclp); 253 254 sclp_c->service_interrupt(sclp, sccb); 255 } 256 257 /* qemu object creation and initialization functions */ 258 259 void s390_sclp_init(void) 260 { 261 Object *new = object_new(TYPE_SCLP); 262 263 object_property_add_child(qdev_get_machine(), TYPE_SCLP, new, 264 NULL); 265 object_unref(OBJECT(new)); 266 qdev_init_nofail(DEVICE(new)); 267 } 268 269 static void sclp_realize(DeviceState *dev, Error **errp) 270 { 271 MachineState *machine = MACHINE(qdev_get_machine()); 272 SCLPDevice *sclp = SCLP(dev); 273 Error *err = NULL; 274 uint64_t hw_limit; 275 int ret; 276 277 object_property_set_bool(OBJECT(sclp->event_facility), true, "realized", 278 &err); 279 if (err) { 280 goto out; 281 } 282 /* 283 * qdev_device_add searches the sysbus for TYPE_SCLP_EVENTS_BUS. As long 284 * as we can't find a fitting bus via the qom tree, we have to add the 285 * event facility to the sysbus, so e.g. a sclp console can be created. 286 */ 287 qdev_set_parent_bus(DEVICE(sclp->event_facility), sysbus_get_default()); 288 289 ret = s390_set_memory_limit(machine->maxram_size, &hw_limit); 290 if (ret == -E2BIG) { 291 error_setg(&err, "host supports a maximum of %" PRIu64 " GB", 292 hw_limit >> 30); 293 } else if (ret) { 294 error_setg(&err, "setting the guest size failed"); 295 } 296 297 out: 298 error_propagate(errp, err); 299 } 300 301 static void sclp_memory_init(SCLPDevice *sclp) 302 { 303 MachineState *machine = MACHINE(qdev_get_machine()); 304 ram_addr_t initial_mem = machine->ram_size; 305 int increment_size = 20; 306 307 /* The storage increment size is a multiple of 1M and is a power of 2. 308 * The number of storage increments must be MAX_STORAGE_INCREMENTS or fewer. 309 * The variable 'increment_size' is an exponent of 2 that can be 310 * used to calculate the size (in bytes) of an increment. */ 311 while ((initial_mem >> increment_size) > MAX_STORAGE_INCREMENTS) { 312 increment_size++; 313 } 314 sclp->increment_size = increment_size; 315 316 /* The core memory area needs to be aligned with the increment size. 317 * In effect, this can cause the user-specified memory size to be rounded 318 * down to align with the nearest increment boundary. */ 319 initial_mem = initial_mem >> increment_size << increment_size; 320 321 machine->ram_size = initial_mem; 322 /* let's propagate the changed ram size into the global variable. */ 323 ram_size = initial_mem; 324 } 325 326 static void sclp_init(Object *obj) 327 { 328 SCLPDevice *sclp = SCLP(obj); 329 Object *new; 330 331 new = object_new(TYPE_SCLP_EVENT_FACILITY); 332 object_property_add_child(obj, TYPE_SCLP_EVENT_FACILITY, new, NULL); 333 object_unref(new); 334 sclp->event_facility = EVENT_FACILITY(new); 335 336 sclp_memory_init(sclp); 337 } 338 339 static void sclp_class_init(ObjectClass *oc, void *data) 340 { 341 SCLPDeviceClass *sc = SCLP_CLASS(oc); 342 DeviceClass *dc = DEVICE_CLASS(oc); 343 344 dc->desc = "SCLP (Service-Call Logical Processor)"; 345 dc->realize = sclp_realize; 346 dc->hotpluggable = false; 347 set_bit(DEVICE_CATEGORY_MISC, dc->categories); 348 /* 349 * Reason: Creates TYPE_SCLP_EVENT_FACILITY in sclp_init 350 * which is a non-pluggable sysbus device 351 */ 352 dc->user_creatable = false; 353 354 sc->read_SCP_info = read_SCP_info; 355 sc->read_cpu_info = sclp_read_cpu_info; 356 sc->execute = sclp_execute; 357 sc->service_interrupt = service_interrupt; 358 } 359 360 static TypeInfo sclp_info = { 361 .name = TYPE_SCLP, 362 .parent = TYPE_DEVICE, 363 .instance_init = sclp_init, 364 .instance_size = sizeof(SCLPDevice), 365 .class_init = sclp_class_init, 366 .class_size = sizeof(SCLPDeviceClass), 367 }; 368 369 static void register_types(void) 370 { 371 type_register_static(&sclp_info); 372 } 373 type_init(register_types); 374