1 /* 2 * QEMU S390x floating interrupt controller (flic) 3 * 4 * Copyright 2014 IBM Corp. 5 * Author(s): Jens Freimann <jfrei@linux.vnet.ibm.com> 6 * Cornelia Huck <cornelia.huck@de.ibm.com> 7 * 8 * This work is licensed under the terms of the GNU GPL, version 2 or (at 9 * your option) any later version. See the COPYING file in the top-level 10 * directory. 11 */ 12 13 #include "qemu/osdep.h" 14 #include "qemu/error-report.h" 15 #include "hw/sysbus.h" 16 #include "hw/s390x/ioinst.h" 17 #include "hw/s390x/s390_flic.h" 18 #include "hw/s390x/css.h" 19 #include "trace.h" 20 #include "cpu.h" 21 #include "hw/qdev.h" 22 #include "qapi/error.h" 23 #include "hw/s390x/s390-virtio-ccw.h" 24 25 S390FLICState *s390_get_flic(void) 26 { 27 static S390FLICState *fs; 28 29 if (!fs) { 30 fs = S390_FLIC_COMMON(object_resolve_path_type("", 31 TYPE_S390_FLIC_COMMON, 32 NULL)); 33 } 34 return fs; 35 } 36 37 void s390_flic_init(void) 38 { 39 DeviceState *dev; 40 41 if (kvm_enabled()) { 42 dev = qdev_create(NULL, TYPE_KVM_S390_FLIC); 43 object_property_add_child(qdev_get_machine(), TYPE_KVM_S390_FLIC, 44 OBJECT(dev), NULL); 45 } else { 46 dev = qdev_create(NULL, TYPE_QEMU_S390_FLIC); 47 object_property_add_child(qdev_get_machine(), TYPE_QEMU_S390_FLIC, 48 OBJECT(dev), NULL); 49 } 50 qdev_init_nofail(dev); 51 } 52 53 static int qemu_s390_register_io_adapter(S390FLICState *fs, uint32_t id, 54 uint8_t isc, bool swap, 55 bool is_maskable, uint8_t flags) 56 { 57 /* nothing to do */ 58 return 0; 59 } 60 61 static int qemu_s390_io_adapter_map(S390FLICState *fs, uint32_t id, 62 uint64_t map_addr, bool do_map) 63 { 64 /* nothing to do */ 65 return 0; 66 } 67 68 static int qemu_s390_add_adapter_routes(S390FLICState *fs, 69 AdapterRoutes *routes) 70 { 71 return -ENOSYS; 72 } 73 74 static void qemu_s390_release_adapter_routes(S390FLICState *fs, 75 AdapterRoutes *routes) 76 { 77 } 78 79 static int qemu_s390_clear_io_flic(S390FLICState *fs, uint16_t subchannel_id, 80 uint16_t subchannel_nr) 81 { 82 QEMUS390FLICState *flic = QEMU_S390_FLIC(fs); 83 QEMUS390FlicIO *cur, *next; 84 uint8_t isc; 85 86 g_assert(qemu_mutex_iothread_locked()); 87 if (!(flic->pending & FLIC_PENDING_IO)) { 88 return 0; 89 } 90 91 /* check all iscs */ 92 for (isc = 0; isc < 8; isc++) { 93 if (QLIST_EMPTY(&flic->io[isc])) { 94 continue; 95 } 96 97 /* search and delete any matching one */ 98 QLIST_FOREACH_SAFE(cur, &flic->io[isc], next, next) { 99 if (cur->id == subchannel_id && cur->nr == subchannel_nr) { 100 QLIST_REMOVE(cur, next); 101 g_free(cur); 102 } 103 } 104 105 /* update our indicator bit */ 106 if (QLIST_EMPTY(&flic->io[isc])) { 107 flic->pending &= ~ISC_TO_PENDING_IO(isc); 108 } 109 } 110 return 0; 111 } 112 113 static int qemu_s390_modify_ais_mode(S390FLICState *fs, uint8_t isc, 114 uint16_t mode) 115 { 116 QEMUS390FLICState *flic = QEMU_S390_FLIC(fs); 117 118 switch (mode) { 119 case SIC_IRQ_MODE_ALL: 120 flic->simm &= ~AIS_MODE_MASK(isc); 121 flic->nimm &= ~AIS_MODE_MASK(isc); 122 break; 123 case SIC_IRQ_MODE_SINGLE: 124 flic->simm |= AIS_MODE_MASK(isc); 125 flic->nimm &= ~AIS_MODE_MASK(isc); 126 break; 127 default: 128 return -EINVAL; 129 } 130 131 return 0; 132 } 133 134 static int qemu_s390_inject_airq(S390FLICState *fs, uint8_t type, 135 uint8_t isc, uint8_t flags) 136 { 137 QEMUS390FLICState *flic = QEMU_S390_FLIC(fs); 138 S390FLICStateClass *fsc = S390_FLIC_COMMON_GET_CLASS(fs); 139 bool flag = flags & S390_ADAPTER_SUPPRESSIBLE; 140 uint32_t io_int_word = (isc << 27) | IO_INT_WORD_AI; 141 142 if (flag && (flic->nimm & AIS_MODE_MASK(isc))) { 143 trace_qemu_s390_airq_suppressed(type, isc); 144 return 0; 145 } 146 147 fsc->inject_io(fs, 0, 0, 0, io_int_word); 148 149 if (flag && (flic->simm & AIS_MODE_MASK(isc))) { 150 flic->nimm |= AIS_MODE_MASK(isc); 151 trace_qemu_s390_suppress_airq(isc, "Single-Interruption Mode", 152 "NO-Interruptions Mode"); 153 } 154 155 return 0; 156 } 157 158 static void qemu_s390_flic_notify(uint32_t type) 159 { 160 CPUState *cs; 161 162 /* 163 * We have to make all CPUs see CPU_INTERRUPT_HARD, so they might 164 * consider it. We will kick all running CPUs and only relevant 165 * sleeping ones. 166 */ 167 CPU_FOREACH(cs) { 168 S390CPU *cpu = S390_CPU(cs); 169 170 cs->interrupt_request |= CPU_INTERRUPT_HARD; 171 172 /* ignore CPUs that are not sleeping */ 173 if (s390_cpu_get_state(cpu) != CPU_STATE_OPERATING && 174 s390_cpu_get_state(cpu) != CPU_STATE_LOAD) { 175 continue; 176 } 177 178 /* we always kick running CPUs for now, this is tricky */ 179 if (cs->halted) { 180 /* don't check for subclasses, CPUs double check when waking up */ 181 if (type & FLIC_PENDING_SERVICE) { 182 if (!(cpu->env.psw.mask & PSW_MASK_EXT)) { 183 continue; 184 } 185 } else if (type & FLIC_PENDING_IO) { 186 if (!(cpu->env.psw.mask & PSW_MASK_IO)) { 187 continue; 188 } 189 } else if (type & FLIC_PENDING_MCHK_CR) { 190 if (!(cpu->env.psw.mask & PSW_MASK_MCHECK)) { 191 continue; 192 } 193 } 194 } 195 cpu_interrupt(cs, CPU_INTERRUPT_HARD); 196 } 197 } 198 199 uint32_t qemu_s390_flic_dequeue_service(QEMUS390FLICState *flic) 200 { 201 uint32_t tmp; 202 203 g_assert(qemu_mutex_iothread_locked()); 204 g_assert(flic->pending & FLIC_PENDING_SERVICE); 205 tmp = flic->service_param; 206 flic->service_param = 0; 207 flic->pending &= ~FLIC_PENDING_SERVICE; 208 209 return tmp; 210 } 211 212 /* caller has to free the returned object */ 213 QEMUS390FlicIO *qemu_s390_flic_dequeue_io(QEMUS390FLICState *flic, uint64_t cr6) 214 { 215 QEMUS390FlicIO *io; 216 uint8_t isc; 217 218 g_assert(qemu_mutex_iothread_locked()); 219 if (!(flic->pending & CR6_TO_PENDING_IO(cr6))) { 220 return NULL; 221 } 222 223 for (isc = 0; isc < 8; isc++) { 224 if (QLIST_EMPTY(&flic->io[isc]) || !(cr6 & ISC_TO_ISC_BITS(isc))) { 225 continue; 226 } 227 io = QLIST_FIRST(&flic->io[isc]); 228 QLIST_REMOVE(io, next); 229 230 /* update our indicator bit */ 231 if (QLIST_EMPTY(&flic->io[isc])) { 232 flic->pending &= ~ISC_TO_PENDING_IO(isc); 233 } 234 return io; 235 } 236 237 return NULL; 238 } 239 240 void qemu_s390_flic_dequeue_crw_mchk(QEMUS390FLICState *flic) 241 { 242 g_assert(qemu_mutex_iothread_locked()); 243 g_assert(flic->pending & FLIC_PENDING_MCHK_CR); 244 flic->pending &= ~FLIC_PENDING_MCHK_CR; 245 } 246 247 static void qemu_s390_inject_service(S390FLICState *fs, uint32_t parm) 248 { 249 QEMUS390FLICState *flic = QEMU_S390_FLIC(fs); 250 251 g_assert(qemu_mutex_iothread_locked()); 252 /* multiplexing is good enough for sclp - kvm does it internally as well */ 253 flic->service_param |= parm; 254 flic->pending |= FLIC_PENDING_SERVICE; 255 256 qemu_s390_flic_notify(FLIC_PENDING_SERVICE); 257 } 258 259 static void qemu_s390_inject_io(S390FLICState *fs, uint16_t subchannel_id, 260 uint16_t subchannel_nr, uint32_t io_int_parm, 261 uint32_t io_int_word) 262 { 263 const uint8_t isc = IO_INT_WORD_ISC(io_int_word); 264 QEMUS390FLICState *flic = QEMU_S390_FLIC(fs); 265 QEMUS390FlicIO *io; 266 267 g_assert(qemu_mutex_iothread_locked()); 268 io = g_new0(QEMUS390FlicIO, 1); 269 io->id = subchannel_id; 270 io->nr = subchannel_nr; 271 io->parm = io_int_parm; 272 io->word = io_int_word; 273 274 QLIST_INSERT_HEAD(&flic->io[isc], io, next); 275 flic->pending |= ISC_TO_PENDING_IO(isc); 276 277 qemu_s390_flic_notify(ISC_TO_PENDING_IO(isc)); 278 } 279 280 static void qemu_s390_inject_crw_mchk(S390FLICState *fs) 281 { 282 QEMUS390FLICState *flic = QEMU_S390_FLIC(fs); 283 284 g_assert(qemu_mutex_iothread_locked()); 285 flic->pending |= FLIC_PENDING_MCHK_CR; 286 287 qemu_s390_flic_notify(FLIC_PENDING_MCHK_CR); 288 } 289 290 bool qemu_s390_flic_has_service(QEMUS390FLICState *flic) 291 { 292 /* called without lock via cc->has_work, will be validated under lock */ 293 return !!(flic->pending & FLIC_PENDING_SERVICE); 294 } 295 296 bool qemu_s390_flic_has_io(QEMUS390FLICState *flic, uint64_t cr6) 297 { 298 /* called without lock via cc->has_work, will be validated under lock */ 299 return !!(flic->pending & CR6_TO_PENDING_IO(cr6)); 300 } 301 302 bool qemu_s390_flic_has_crw_mchk(QEMUS390FLICState *flic) 303 { 304 /* called without lock via cc->has_work, will be validated under lock */ 305 return !!(flic->pending & FLIC_PENDING_MCHK_CR); 306 } 307 308 bool qemu_s390_flic_has_any(QEMUS390FLICState *flic) 309 { 310 g_assert(qemu_mutex_iothread_locked()); 311 return !!flic->pending; 312 } 313 314 static void qemu_s390_flic_reset(DeviceState *dev) 315 { 316 QEMUS390FLICState *flic = QEMU_S390_FLIC(dev); 317 QEMUS390FlicIO *cur, *next; 318 int isc; 319 320 g_assert(qemu_mutex_iothread_locked()); 321 flic->simm = 0; 322 flic->nimm = 0; 323 flic->pending = 0; 324 325 /* remove all pending io interrupts */ 326 for (isc = 0; isc < 8; isc++) { 327 QLIST_FOREACH_SAFE(cur, &flic->io[isc], next, next) { 328 QLIST_REMOVE(cur, next); 329 g_free(cur); 330 } 331 } 332 } 333 334 bool ais_needed(void *opaque) 335 { 336 S390FLICState *s = opaque; 337 338 return s->ais_supported; 339 } 340 341 static const VMStateDescription qemu_s390_flic_vmstate = { 342 .name = "qemu-s390-flic", 343 .version_id = 1, 344 .minimum_version_id = 1, 345 .needed = ais_needed, 346 .fields = (VMStateField[]) { 347 VMSTATE_UINT8(simm, QEMUS390FLICState), 348 VMSTATE_UINT8(nimm, QEMUS390FLICState), 349 VMSTATE_END_OF_LIST() 350 } 351 }; 352 353 static void qemu_s390_flic_instance_init(Object *obj) 354 { 355 QEMUS390FLICState *flic = QEMU_S390_FLIC(obj); 356 int isc; 357 358 for (isc = 0; isc < 8; isc++) { 359 QLIST_INIT(&flic->io[isc]); 360 } 361 } 362 363 static void qemu_s390_flic_class_init(ObjectClass *oc, void *data) 364 { 365 DeviceClass *dc = DEVICE_CLASS(oc); 366 S390FLICStateClass *fsc = S390_FLIC_COMMON_CLASS(oc); 367 368 dc->reset = qemu_s390_flic_reset; 369 dc->vmsd = &qemu_s390_flic_vmstate; 370 fsc->register_io_adapter = qemu_s390_register_io_adapter; 371 fsc->io_adapter_map = qemu_s390_io_adapter_map; 372 fsc->add_adapter_routes = qemu_s390_add_adapter_routes; 373 fsc->release_adapter_routes = qemu_s390_release_adapter_routes; 374 fsc->clear_io_irq = qemu_s390_clear_io_flic; 375 fsc->modify_ais_mode = qemu_s390_modify_ais_mode; 376 fsc->inject_airq = qemu_s390_inject_airq; 377 fsc->inject_service = qemu_s390_inject_service; 378 fsc->inject_io = qemu_s390_inject_io; 379 fsc->inject_crw_mchk = qemu_s390_inject_crw_mchk; 380 } 381 382 static Property s390_flic_common_properties[] = { 383 DEFINE_PROP_UINT32("adapter_routes_max_batch", S390FLICState, 384 adapter_routes_max_batch, ADAPTER_ROUTES_MAX_GSI), 385 DEFINE_PROP_END_OF_LIST(), 386 }; 387 388 static void s390_flic_common_realize(DeviceState *dev, Error **errp) 389 { 390 S390FLICState *fs = S390_FLIC_COMMON(dev); 391 uint32_t max_batch = fs->adapter_routes_max_batch; 392 393 if (max_batch > ADAPTER_ROUTES_MAX_GSI) { 394 error_setg(errp, "flic property adapter_routes_max_batch too big" 395 " (%d > %d)", max_batch, ADAPTER_ROUTES_MAX_GSI); 396 return; 397 } 398 399 fs->ais_supported = s390_has_feat(S390_FEAT_ADAPTER_INT_SUPPRESSION); 400 } 401 402 static void s390_flic_class_init(ObjectClass *oc, void *data) 403 { 404 DeviceClass *dc = DEVICE_CLASS(oc); 405 406 dc->props = s390_flic_common_properties; 407 dc->realize = s390_flic_common_realize; 408 } 409 410 static const TypeInfo qemu_s390_flic_info = { 411 .name = TYPE_QEMU_S390_FLIC, 412 .parent = TYPE_S390_FLIC_COMMON, 413 .instance_size = sizeof(QEMUS390FLICState), 414 .instance_init = qemu_s390_flic_instance_init, 415 .class_init = qemu_s390_flic_class_init, 416 }; 417 418 419 static const TypeInfo s390_flic_common_info = { 420 .name = TYPE_S390_FLIC_COMMON, 421 .parent = TYPE_SYS_BUS_DEVICE, 422 .instance_size = sizeof(S390FLICState), 423 .class_init = s390_flic_class_init, 424 .class_size = sizeof(S390FLICStateClass), 425 }; 426 427 static void qemu_s390_flic_register_types(void) 428 { 429 type_register_static(&s390_flic_common_info); 430 type_register_static(&qemu_s390_flic_info); 431 } 432 433 type_init(qemu_s390_flic_register_types) 434 435 static bool adapter_info_so_needed(void *opaque) 436 { 437 return css_migration_enabled(); 438 } 439 440 const VMStateDescription vmstate_adapter_info_so = { 441 .name = "s390_adapter_info/summary_offset", 442 .version_id = 1, 443 .minimum_version_id = 1, 444 .needed = adapter_info_so_needed, 445 .fields = (VMStateField[]) { 446 VMSTATE_UINT32(summary_offset, AdapterInfo), 447 VMSTATE_END_OF_LIST() 448 } 449 }; 450 451 const VMStateDescription vmstate_adapter_info = { 452 .name = "s390_adapter_info", 453 .version_id = 1, 454 .minimum_version_id = 1, 455 .fields = (VMStateField[]) { 456 VMSTATE_UINT64(ind_offset, AdapterInfo), 457 /* 458 * We do not have to migrate neither the id nor the addresses. 459 * The id is set by css_register_io_adapter and the addresses 460 * are set based on the IndAddr objects after those get mapped. 461 */ 462 VMSTATE_END_OF_LIST() 463 }, 464 .subsections = (const VMStateDescription * []) { 465 &vmstate_adapter_info_so, 466 NULL 467 } 468 }; 469 470 const VMStateDescription vmstate_adapter_routes = { 471 472 .name = "s390_adapter_routes", 473 .version_id = 1, 474 .minimum_version_id = 1, 475 .fields = (VMStateField[]) { 476 VMSTATE_STRUCT(adapter, AdapterRoutes, 1, vmstate_adapter_info, 477 AdapterInfo), 478 VMSTATE_END_OF_LIST() 479 } 480 }; 481