2
0

s390_flic.c 16 KB

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