gunyah_vm_mgr.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
  4. */
  5. #ifndef _GUNYAH_VM_MGR_H
  6. #define _GUNYAH_VM_MGR_H
  7. #include <linux/compiler_types.h>
  8. #include <linux/gunyah.h>
  9. #include <linux/gunyah_rsc_mgr.h>
  10. #include <linux/list.h>
  11. #include <linux/mod_devicetable.h>
  12. #include <linux/notifier.h>
  13. #include <uapi/linux/gunyah.h>
  14. struct gh_vm;
  15. int __must_check gh_vm_get(struct gh_vm *ghvm);
  16. void gh_vm_put(struct gh_vm *ghvm);
  17. struct gh_vm_function_instance;
  18. /**
  19. * struct gh_vm_function - Represents a function type
  20. * @type: value from &enum gh_fn_type
  21. * @name: friendly name for debug purposes
  22. * @mod: owner of the function type
  23. * @bind: Called when a new function of this type has been allocated.
  24. * @unbind: Called when the function instance is being destroyed.
  25. * @compare: Compare function instance @f's argument to the provided arg.
  26. * Return true if they are equivalent. Used on GH_VM_REMOVE_FUNCTION.
  27. */
  28. struct gh_vm_function {
  29. u32 type;
  30. const char *name;
  31. struct module *mod;
  32. long (*bind)(struct gh_vm_function_instance *f);
  33. void (*unbind)(struct gh_vm_function_instance *f);
  34. bool (*compare)(const struct gh_vm_function_instance *f, const void *arg, size_t size);
  35. };
  36. /**
  37. * struct gh_vm_function_instance - Represents one function instance
  38. * @arg_size: size of user argument
  39. * @argp: pointer to user argument
  40. * @ghvm: Pointer to VM instance
  41. * @rm: Pointer to resource manager for the VM instance
  42. * @fn: The ops for the function
  43. * @data: Private data for function
  44. * @vm_list: for gh_vm's functions list
  45. * @fn_list: for gh_vm_function's instances list
  46. */
  47. struct gh_vm_function_instance {
  48. size_t arg_size;
  49. void *argp;
  50. struct gh_vm *ghvm;
  51. struct gh_rm *rm;
  52. struct gh_vm_function *fn;
  53. void *data;
  54. struct list_head vm_list;
  55. };
  56. int gh_vm_function_register(struct gh_vm_function *f);
  57. void gh_vm_function_unregister(struct gh_vm_function *f);
  58. /* Since the function identifiers were setup in a uapi header as an
  59. * enum and we do no want to change that, the user must supply the expanded
  60. * constant as well and the compiler checks they are the same.
  61. * See also MODULE_ALIAS_RDMA_NETLINK.
  62. */
  63. #define MODULE_ALIAS_GH_VM_FUNCTION(_type, _idx) \
  64. static inline void __maybe_unused __chk##_idx(void) \
  65. { \
  66. BUILD_BUG_ON(_type != _idx); \
  67. } \
  68. MODULE_ALIAS("ghfunc:" __stringify(_idx))
  69. #define DECLARE_GH_VM_FUNCTION(_name, _type, _bind, _unbind, _compare) \
  70. static struct gh_vm_function _name = { \
  71. .type = _type, \
  72. .name = __stringify(_name), \
  73. .mod = THIS_MODULE, \
  74. .bind = _bind, \
  75. .unbind = _unbind, \
  76. .compare = _compare, \
  77. }
  78. #define module_gh_vm_function(__gf) \
  79. module_driver(__gf, gh_vm_function_register, gh_vm_function_unregister)
  80. #define DECLARE_GH_VM_FUNCTION_INIT(_name, _type, _idx, _bind, _unbind, _compare) \
  81. DECLARE_GH_VM_FUNCTION(_name, _type, _bind, _unbind, _compare); \
  82. module_gh_vm_function(_name); \
  83. MODULE_ALIAS_GH_VM_FUNCTION(_type, _idx)
  84. /**
  85. * struct gh_vm_resource_ticket - Represents a ticket to reserve exclusive access to VM resource(s)
  86. * @vm_list: for @gh_vm->resource_tickets
  87. * @resources: List of resource(s) associated with this ticket(members are from @gh_resource->list)
  88. * @resource_type: Type of resource this ticket reserves
  89. * @label: Label of the resource from resource manager this ticket reserves.
  90. * @owner: owner of the ticket
  91. * @populate: callback provided by the ticket owner and called when a resource is found that
  92. * matches @resource_type and @label. Note that this callback could be called
  93. * multiple times if userspace created mutliple resources with the same type/label.
  94. * This callback may also have significant delay after gh_vm_add_resource_ticket()
  95. * since gh_vm_add_resource_ticket() could be called before the VM starts.
  96. * @unpopulate: callback provided by the ticket owner and called when the ticket owner should no
  97. * no longer use the resource provided in the argument. When unpopulate() returns,
  98. * the ticket owner should not be able to use the resource any more as the resource
  99. * might being freed.
  100. */
  101. struct gh_vm_resource_ticket {
  102. struct list_head vm_list;
  103. struct list_head resources;
  104. enum gh_resource_type resource_type;
  105. u32 label;
  106. struct module *owner;
  107. bool (*populate)(struct gh_vm_resource_ticket *ticket, struct gh_resource *ghrsc);
  108. void (*unpopulate)(struct gh_vm_resource_ticket *ticket, struct gh_resource *ghrsc);
  109. };
  110. int gh_vm_add_resource_ticket(struct gh_vm *ghvm, struct gh_vm_resource_ticket *ticket);
  111. void gh_vm_remove_resource_ticket(struct gh_vm *ghvm, struct gh_vm_resource_ticket *ticket);
  112. /*
  113. * gh_vm_io_handler contains the info about an io device and its associated
  114. * addr and the ops associated with the io device.
  115. */
  116. struct gh_vm_io_handler {
  117. struct rb_node node;
  118. u64 addr;
  119. bool datamatch;
  120. u8 len;
  121. u64 data;
  122. struct gh_vm_io_handler_ops *ops;
  123. };
  124. /*
  125. * gh_vm_io_handler_ops contains function pointers associated with an iodevice.
  126. */
  127. struct gh_vm_io_handler_ops {
  128. int (*read)(struct gh_vm_io_handler *io_dev, u64 addr, u32 len, u64 data);
  129. int (*write)(struct gh_vm_io_handler *io_dev, u64 addr, u32 len, u64 data);
  130. };
  131. int gh_vm_add_io_handler(struct gh_vm *ghvm, struct gh_vm_io_handler *io_dev);
  132. void gh_vm_remove_io_handler(struct gh_vm *ghvm, struct gh_vm_io_handler *io_dev);
  133. #endif