ivshmem.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (c) 2020 Intel Corporation
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef ZEPHYR_INCLUDE_DRIVERS_VIRTUALIZATION_IVSHMEM_H_
  7. #define ZEPHYR_INCLUDE_DRIVERS_VIRTUALIZATION_IVSHMEM_H_
  8. /**
  9. * @brief ivshmem reference API
  10. * @defgroup ivshmem ivshmem reference API
  11. * @{
  12. */
  13. #include <zephyr/types.h>
  14. #include <stddef.h>
  15. #include <device.h>
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. typedef size_t (*ivshmem_get_mem_f)(const struct device *dev,
  20. uintptr_t *memmap);
  21. typedef uint32_t (*ivshmem_get_id_f)(const struct device *dev);
  22. typedef uint16_t (*ivshmem_get_vectors_f)(const struct device *dev);
  23. typedef int (*ivshmem_int_peer_f)(const struct device *dev,
  24. uint32_t peer_id, uint16_t vector);
  25. typedef int (*ivshmem_register_handler_f)(const struct device *dev,
  26. struct k_poll_signal *signal,
  27. uint16_t vector);
  28. __subsystem struct ivshmem_driver_api {
  29. ivshmem_get_mem_f get_mem;
  30. ivshmem_get_id_f get_id;
  31. ivshmem_get_vectors_f get_vectors;
  32. ivshmem_int_peer_f int_peer;
  33. ivshmem_register_handler_f register_handler;
  34. };
  35. /**
  36. * @brief Get the inter-VM shared memory
  37. *
  38. * @param dev Pointer to the device structure for the driver instance
  39. * @param memmap A pointer to fill in with the memory address
  40. *
  41. * @return the size of the memory mapped, or 0
  42. */
  43. __syscall size_t ivshmem_get_mem(const struct device *dev,
  44. uintptr_t *memmap);
  45. static inline size_t z_impl_ivshmem_get_mem(const struct device *dev,
  46. uintptr_t *memmap)
  47. {
  48. const struct ivshmem_driver_api *api =
  49. (const struct ivshmem_driver_api *)dev->api;
  50. return api->get_mem(dev, memmap);
  51. }
  52. /**
  53. * @brief Get our VM ID
  54. *
  55. * @param dev Pointer to the device structure for the driver instance
  56. *
  57. * @return our VM ID or 0 if we are not running on doorbell version
  58. */
  59. __syscall uint32_t ivshmem_get_id(const struct device *dev);
  60. static inline uint32_t z_impl_ivshmem_get_id(const struct device *dev)
  61. {
  62. const struct ivshmem_driver_api *api =
  63. (const struct ivshmem_driver_api *)dev->api;
  64. return api->get_id(dev);
  65. }
  66. /**
  67. * @brief Get the number of interrupt vectors we can use
  68. *
  69. * @param dev Pointer to the device structure for the driver instance
  70. *
  71. * @return the number of available interrupt vectors
  72. */
  73. __syscall uint16_t ivshmem_get_vectors(const struct device *dev);
  74. static inline uint16_t z_impl_ivshmem_get_vectors(const struct device *dev)
  75. {
  76. const struct ivshmem_driver_api *api =
  77. (const struct ivshmem_driver_api *)dev->api;
  78. return api->get_vectors(dev);
  79. }
  80. /**
  81. * @brief Interrupt another VM
  82. *
  83. * @param dev Pointer to the device structure for the driver instance
  84. * @param peer_id The VM ID to interrupt
  85. * @param vector The interrupt vector to use
  86. *
  87. * @return 0 on success, a negative errno otherwise
  88. */
  89. __syscall int ivshmem_int_peer(const struct device *dev,
  90. uint32_t peer_id, uint16_t vector);
  91. static inline int z_impl_ivshmem_int_peer(const struct device *dev,
  92. uint32_t peer_id, uint16_t vector)
  93. {
  94. const struct ivshmem_driver_api *api =
  95. (const struct ivshmem_driver_api *)dev->api;
  96. return api->int_peer(dev, peer_id, vector);
  97. }
  98. /**
  99. * @brief Register a vector notification (interrupt) handler
  100. *
  101. * @param dev Pointer to the device structure for the driver instance
  102. * @param signal A pointer to a valid and ready to be signaled
  103. * struct k_poll_signal. Or NULL to unregister any handler
  104. * registered for the given vector.
  105. * @param vector The interrupt vector to get notification from
  106. *
  107. * Note: The returned status, if positive, to a raised signal is the vector
  108. * that generated the signal. This lets the possibility to the user
  109. * to have one signal for all vectors, or one per-vector.
  110. *
  111. * @return 0 on success, a negative errno otherwise
  112. */
  113. __syscall int ivshmem_register_handler(const struct device *dev,
  114. struct k_poll_signal *signal,
  115. uint16_t vector);
  116. static inline int z_impl_ivshmem_register_handler(const struct device *dev,
  117. struct k_poll_signal *signal,
  118. uint16_t vector)
  119. {
  120. const struct ivshmem_driver_api *api =
  121. (const struct ivshmem_driver_api *)dev->api;
  122. return api->register_handler(dev, signal, vector);
  123. }
  124. #ifdef __cplusplus
  125. }
  126. #endif
  127. /**
  128. * @}
  129. */
  130. #include <syscalls/ivshmem.h>
  131. #endif /* ZEPHYR_INCLUDE_DRIVERS_VIRTUALIZATION_IVSHMEM_H_ */