gunyah_ioeventfd.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
  4. */
  5. #include <linux/eventfd.h>
  6. #include <linux/file.h>
  7. #include <linux/fs.h>
  8. #include <linux/gunyah.h>
  9. #include <linux/gunyah_vm_mgr.h>
  10. #include <linux/module.h>
  11. #include <linux/printk.h>
  12. #include <uapi/linux/gunyah.h>
  13. struct gh_ioeventfd {
  14. struct gh_vm_function_instance *f;
  15. struct gh_vm_io_handler io_handler;
  16. struct eventfd_ctx *ctx;
  17. };
  18. static int gh_write_ioeventfd(struct gh_vm_io_handler *io_dev, u64 addr, u32 len, u64 data)
  19. {
  20. struct gh_ioeventfd *iofd = container_of(io_dev, struct gh_ioeventfd, io_handler);
  21. eventfd_signal(iofd->ctx, 1);
  22. return 0;
  23. }
  24. static struct gh_vm_io_handler_ops io_ops = {
  25. .write = gh_write_ioeventfd,
  26. };
  27. static long gh_ioeventfd_bind(struct gh_vm_function_instance *f)
  28. {
  29. const struct gh_fn_ioeventfd_arg *args = f->argp;
  30. struct gh_ioeventfd *iofd;
  31. struct eventfd_ctx *ctx;
  32. int ret;
  33. if (f->arg_size != sizeof(*args))
  34. return -EINVAL;
  35. /* All other flag bits are reserved for future use */
  36. if (args->flags & ~GH_IOEVENTFD_FLAGS_DATAMATCH)
  37. return -EINVAL;
  38. /* must be natural-word sized, or 0 to ignore length */
  39. switch (args->len) {
  40. case 0:
  41. case 1:
  42. case 2:
  43. case 4:
  44. case 8:
  45. break;
  46. default:
  47. return -EINVAL;
  48. }
  49. /* check for range overflow */
  50. if (overflows_type(args->addr + args->len, u64))
  51. return -EINVAL;
  52. /* ioeventfd with no length can't be combined with DATAMATCH */
  53. if (!args->len && (args->flags & GH_IOEVENTFD_FLAGS_DATAMATCH))
  54. return -EINVAL;
  55. ctx = eventfd_ctx_fdget(args->fd);
  56. if (IS_ERR(ctx))
  57. return PTR_ERR(ctx);
  58. iofd = kzalloc(sizeof(*iofd), GFP_KERNEL);
  59. if (!iofd) {
  60. ret = -ENOMEM;
  61. goto err_eventfd;
  62. }
  63. f->data = iofd;
  64. iofd->f = f;
  65. iofd->ctx = ctx;
  66. if (args->flags & GH_IOEVENTFD_FLAGS_DATAMATCH) {
  67. iofd->io_handler.datamatch = true;
  68. iofd->io_handler.len = args->len;
  69. iofd->io_handler.data = args->datamatch;
  70. }
  71. iofd->io_handler.addr = args->addr;
  72. iofd->io_handler.ops = &io_ops;
  73. ret = gh_vm_add_io_handler(f->ghvm, &iofd->io_handler);
  74. if (ret)
  75. goto err_io_dev_add;
  76. return 0;
  77. err_io_dev_add:
  78. kfree(iofd);
  79. err_eventfd:
  80. eventfd_ctx_put(ctx);
  81. return ret;
  82. }
  83. static void gh_ioevent_unbind(struct gh_vm_function_instance *f)
  84. {
  85. struct gh_ioeventfd *iofd = f->data;
  86. eventfd_ctx_put(iofd->ctx);
  87. gh_vm_remove_io_handler(iofd->f->ghvm, &iofd->io_handler);
  88. kfree(iofd);
  89. }
  90. static bool gh_ioevent_compare(const struct gh_vm_function_instance *f,
  91. const void *arg, size_t size)
  92. {
  93. const struct gh_fn_ioeventfd_arg *instance = f->argp,
  94. *other = arg;
  95. if (sizeof(*other) != size)
  96. return false;
  97. return instance->addr == other->addr;
  98. }
  99. DECLARE_GH_VM_FUNCTION_INIT(ioeventfd, GH_FN_IOEVENTFD, 3,
  100. gh_ioeventfd_bind, gh_ioevent_unbind,
  101. gh_ioevent_compare);
  102. MODULE_DESCRIPTION("Gunyah ioeventfd VM Function");
  103. MODULE_LICENSE("GPL");