s390_flic.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * QEMU S390x KVM floating interrupt controller (flic)
  3. *
  4. * Copyright 2014 IBM Corp.
  5. * Author(s): Jens Freimann <jfrei@linux.vnet.ibm.com>
  6. *
  7. * This work is licensed under the terms of the GNU GPL, version 2 or (at
  8. * your option) any later version. See the COPYING file in the top-level
  9. * directory.
  10. */
  11. #include <sys/ioctl.h>
  12. #include "qemu/error-report.h"
  13. #include "hw/sysbus.h"
  14. #include "sysemu/kvm.h"
  15. #include "migration/qemu-file.h"
  16. #include "hw/s390x/s390_flic.h"
  17. #include "trace.h"
  18. #define FLIC_SAVE_INITIAL_SIZE getpagesize()
  19. #define FLIC_FAILED (-1UL)
  20. #define FLIC_SAVEVM_VERSION 1
  21. void s390_flic_init(void)
  22. {
  23. DeviceState *dev;
  24. int r;
  25. if (kvm_enabled()) {
  26. dev = qdev_create(NULL, "s390-flic");
  27. object_property_add_child(qdev_get_machine(), "s390-flic",
  28. OBJECT(dev), NULL);
  29. r = qdev_init(dev);
  30. if (r) {
  31. error_report("flic: couldn't create qdev");
  32. }
  33. }
  34. }
  35. /**
  36. * flic_get_all_irqs - store all pending irqs in buffer
  37. * @buf: pointer to buffer which is passed to kernel
  38. * @len: length of buffer
  39. * @flic: pointer to flic device state
  40. *
  41. * Returns: -ENOMEM if buffer is too small,
  42. * -EINVAL if attr.group is invalid,
  43. * -EFAULT if copying to userspace failed,
  44. * on success return number of stored interrupts
  45. */
  46. static int flic_get_all_irqs(KVMS390FLICState *flic,
  47. void *buf, int len)
  48. {
  49. struct kvm_device_attr attr = {
  50. .group = KVM_DEV_FLIC_GET_ALL_IRQS,
  51. .addr = (uint64_t) buf,
  52. .attr = len,
  53. };
  54. int rc;
  55. rc = ioctl(flic->fd, KVM_GET_DEVICE_ATTR, &attr);
  56. return rc == -1 ? -errno : rc;
  57. }
  58. static void flic_enable_pfault(KVMS390FLICState *flic)
  59. {
  60. struct kvm_device_attr attr = {
  61. .group = KVM_DEV_FLIC_APF_ENABLE,
  62. };
  63. int rc;
  64. rc = ioctl(flic->fd, KVM_SET_DEVICE_ATTR, &attr);
  65. if (rc) {
  66. fprintf(stderr, "flic: couldn't enable pfault\n");
  67. }
  68. }
  69. static void flic_disable_wait_pfault(KVMS390FLICState *flic)
  70. {
  71. struct kvm_device_attr attr = {
  72. .group = KVM_DEV_FLIC_APF_DISABLE_WAIT,
  73. };
  74. int rc;
  75. rc = ioctl(flic->fd, KVM_SET_DEVICE_ATTR, &attr);
  76. if (rc) {
  77. fprintf(stderr, "flic: couldn't disable pfault\n");
  78. }
  79. }
  80. /** flic_enqueue_irqs - returns 0 on success
  81. * @buf: pointer to buffer which is passed to kernel
  82. * @len: length of buffer
  83. * @flic: pointer to flic device state
  84. *
  85. * Returns: -EINVAL if attr.group is unknown
  86. */
  87. static int flic_enqueue_irqs(void *buf, uint64_t len,
  88. KVMS390FLICState *flic)
  89. {
  90. int rc;
  91. struct kvm_device_attr attr = {
  92. .group = KVM_DEV_FLIC_ENQUEUE,
  93. .addr = (uint64_t) buf,
  94. .attr = len,
  95. };
  96. rc = ioctl(flic->fd, KVM_SET_DEVICE_ATTR, &attr);
  97. return rc ? -errno : 0;
  98. }
  99. /**
  100. * __get_all_irqs - store all pending irqs in buffer
  101. * @flic: pointer to flic device state
  102. * @buf: pointer to pointer to a buffer
  103. * @len: length of buffer
  104. *
  105. * Returns: return value of flic_get_all_irqs
  106. * Note: Retry and increase buffer size until flic_get_all_irqs
  107. * either returns a value >= 0 or a negative error code.
  108. * -ENOMEM is an exception, which means the buffer is too small
  109. * and we should try again. Other negative error codes can be
  110. * -EFAULT and -EINVAL which we ignore at this point
  111. */
  112. static int __get_all_irqs(KVMS390FLICState *flic,
  113. void **buf, int len)
  114. {
  115. int r;
  116. do {
  117. /* returns -ENOMEM if buffer is too small and number
  118. * of queued interrupts on success */
  119. r = flic_get_all_irqs(flic, *buf, len);
  120. if (r >= 0) {
  121. break;
  122. }
  123. len *= 2;
  124. *buf = g_try_realloc(*buf, len);
  125. if (!buf) {
  126. return -ENOMEM;
  127. }
  128. } while (r == -ENOMEM && len <= KVM_S390_FLIC_MAX_BUFFER);
  129. return r;
  130. }
  131. /**
  132. * kvm_flic_save - Save pending floating interrupts
  133. * @f: QEMUFile containing migration state
  134. * @opaque: pointer to flic device state
  135. *
  136. * Note: Pass buf and len to kernel. Start with one page and
  137. * increase until buffer is sufficient or maxium size is
  138. * reached
  139. */
  140. static void kvm_flic_save(QEMUFile *f, void *opaque)
  141. {
  142. KVMS390FLICState *flic = opaque;
  143. int len = FLIC_SAVE_INITIAL_SIZE;
  144. void *buf;
  145. int count;
  146. flic_disable_wait_pfault((struct KVMS390FLICState *) opaque);
  147. buf = g_try_malloc0(len);
  148. if (!buf) {
  149. /* Storing FLIC_FAILED into the count field here will cause the
  150. * target system to fail when attempting to load irqs from the
  151. * migration state */
  152. error_report("flic: couldn't allocate memory");
  153. qemu_put_be64(f, FLIC_FAILED);
  154. return;
  155. }
  156. count = __get_all_irqs(flic, &buf, len);
  157. if (count < 0) {
  158. error_report("flic: couldn't retrieve irqs from kernel, rc %d",
  159. count);
  160. /* Storing FLIC_FAILED into the count field here will cause the
  161. * target system to fail when attempting to load irqs from the
  162. * migration state */
  163. qemu_put_be64(f, FLIC_FAILED);
  164. } else {
  165. qemu_put_be64(f, count);
  166. qemu_put_buffer(f, (uint8_t *) buf,
  167. count * sizeof(struct kvm_s390_irq));
  168. }
  169. g_free(buf);
  170. }
  171. /**
  172. * kvm_flic_load - Load pending floating interrupts
  173. * @f: QEMUFile containing migration state
  174. * @opaque: pointer to flic device state
  175. * @version_id: version id for migration
  176. *
  177. * Returns: value of flic_enqueue_irqs, -EINVAL on error
  178. * Note: Do nothing when no interrupts where stored
  179. * in QEMUFile
  180. */
  181. static int kvm_flic_load(QEMUFile *f, void *opaque, int version_id)
  182. {
  183. uint64_t len = 0;
  184. uint64_t count = 0;
  185. void *buf = NULL;
  186. int r = 0;
  187. if (version_id != FLIC_SAVEVM_VERSION) {
  188. r = -EINVAL;
  189. goto out;
  190. }
  191. flic_enable_pfault((struct KVMS390FLICState *) opaque);
  192. count = qemu_get_be64(f);
  193. len = count * sizeof(struct kvm_s390_irq);
  194. if (count == FLIC_FAILED) {
  195. r = -EINVAL;
  196. goto out;
  197. }
  198. if (count == 0) {
  199. r = 0;
  200. goto out;
  201. }
  202. buf = g_try_malloc0(len);
  203. if (!buf) {
  204. r = -ENOMEM;
  205. goto out;
  206. }
  207. if (qemu_get_buffer(f, (uint8_t *) buf, len) != len) {
  208. r = -EINVAL;
  209. goto out_free;
  210. }
  211. r = flic_enqueue_irqs(buf, len, (struct KVMS390FLICState *) opaque);
  212. out_free:
  213. g_free(buf);
  214. out:
  215. return r;
  216. }
  217. static void kvm_s390_flic_realize(DeviceState *dev, Error **errp)
  218. {
  219. KVMS390FLICState *flic_state = KVM_S390_FLIC(dev);
  220. struct kvm_create_device cd = {0};
  221. int ret;
  222. flic_state->fd = -1;
  223. if (!kvm_check_extension(kvm_state, KVM_CAP_DEVICE_CTRL)) {
  224. trace_flic_no_device_api(errno);
  225. return;
  226. }
  227. cd.type = KVM_DEV_TYPE_FLIC;
  228. ret = kvm_vm_ioctl(kvm_state, KVM_CREATE_DEVICE, &cd);
  229. if (ret < 0) {
  230. trace_flic_create_device(errno);
  231. return;
  232. }
  233. flic_state->fd = cd.fd;
  234. /* Register savevm handler for floating interrupts */
  235. register_savevm(NULL, "s390-flic", 0, 1, kvm_flic_save,
  236. kvm_flic_load, (void *) flic_state);
  237. }
  238. static void kvm_s390_flic_unrealize(DeviceState *dev, Error **errp)
  239. {
  240. KVMS390FLICState *flic_state = KVM_S390_FLIC(dev);
  241. unregister_savevm(DEVICE(flic_state), "s390-flic", flic_state);
  242. }
  243. static void kvm_s390_flic_reset(DeviceState *dev)
  244. {
  245. KVMS390FLICState *flic = KVM_S390_FLIC(dev);
  246. struct kvm_device_attr attr = {
  247. .group = KVM_DEV_FLIC_CLEAR_IRQS,
  248. };
  249. int rc = 0;
  250. if (flic->fd == -1) {
  251. return;
  252. }
  253. flic_disable_wait_pfault(flic);
  254. rc = ioctl(flic->fd, KVM_SET_DEVICE_ATTR, &attr);
  255. if (rc) {
  256. trace_flic_reset_failed(errno);
  257. }
  258. flic_enable_pfault(flic);
  259. }
  260. static void kvm_s390_flic_class_init(ObjectClass *oc, void *data)
  261. {
  262. DeviceClass *dc = DEVICE_CLASS(oc);
  263. dc->realize = kvm_s390_flic_realize;
  264. dc->unrealize = kvm_s390_flic_unrealize;
  265. dc->reset = kvm_s390_flic_reset;
  266. }
  267. static const TypeInfo kvm_s390_flic_info = {
  268. .name = TYPE_KVM_S390_FLIC,
  269. .parent = TYPE_SYS_BUS_DEVICE,
  270. .instance_size = sizeof(KVMS390FLICState),
  271. .class_init = kvm_s390_flic_class_init,
  272. };
  273. static void kvm_s390_flic_register_types(void)
  274. {
  275. type_register_static(&kvm_s390_flic_info);
  276. }
  277. type_init(kvm_s390_flic_register_types)