2
0

s390_flic.c 14 KB

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