xrp_host_common.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (c) 2016 - 2018 Cadence Design Systems Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sublicense, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included
  13. * in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #ifndef _XRP_HOST_COMMON_H
  24. #define _XRP_HOST_COMMON_H
  25. #include <stdlib.h>
  26. #include <stdint.h>
  27. #include "xrp_atomic.h"
  28. #include "xrp_thread_impl.h"
  29. #include "xrp_host_impl.h"
  30. #include "xrp_report.h"
  31. struct xrp_refcounted {
  32. _Atomic unsigned long count;
  33. };
  34. struct xrp_device {
  35. struct xrp_refcounted ref;
  36. struct xrp_device_impl impl;
  37. };
  38. struct xrp_buffer {
  39. struct xrp_refcounted ref;
  40. struct xrp_device *device;
  41. enum {
  42. XRP_BUFFER_TYPE_HOST,
  43. XRP_BUFFER_TYPE_DEVICE,
  44. } type;
  45. void *ptr;
  46. uint64_t phy_addr;
  47. size_t size;
  48. _Atomic unsigned long map_count;
  49. enum xrp_access_flags map_flags;
  50. struct xrp_buffer_impl impl;
  51. };
  52. struct xrp_buffer_group_record {
  53. struct xrp_buffer *buffer;
  54. enum xrp_access_flags access_flags;
  55. };
  56. struct xrp_buffer_group {
  57. struct xrp_refcounted ref;
  58. xrp_mutex mutex;
  59. size_t n_buffers;
  60. size_t capacity;
  61. struct xrp_buffer_group_record *buffer;
  62. };
  63. struct xrp_queue {
  64. struct xrp_refcounted ref;
  65. struct xrp_device *device;
  66. int use_nsid;
  67. int priority;
  68. char nsid[XRP_NAMESPACE_ID_SIZE];
  69. struct xrp_queue_impl impl;
  70. };
  71. struct xrp_event_link {
  72. struct xrp_event *group;
  73. struct xrp_event_link *next, *prev;
  74. };
  75. struct xrp_event {
  76. struct xrp_refcounted ref;
  77. struct xrp_queue *queue;
  78. _Atomic enum xrp_status status;
  79. struct xrp_event_impl impl;
  80. struct xrp_event *group;
  81. struct xrp_event_link *link;
  82. };
  83. struct xrp_report{
  84. struct xrp_refcounted ref;
  85. struct xrp_device *device;
  86. struct xrp_report_list list;
  87. void *report_buf;
  88. int buf_size;
  89. };
  90. /* Helpers */
  91. static inline void set_status(enum xrp_status *status, enum xrp_status v)
  92. {
  93. if (status)
  94. *status = v;
  95. }
  96. static inline void *alloc_refcounted(size_t sz)
  97. {
  98. void *buf = calloc(1, sz);
  99. struct xrp_refcounted *ref = buf;
  100. if (ref)
  101. ref->count = 1;
  102. return buf;
  103. }
  104. static inline void retain_refcounted(void *buf)
  105. {
  106. struct xrp_refcounted *ref = buf;
  107. (void)++ref->count;
  108. }
  109. static inline int last_release_refcounted(void *buf)
  110. {
  111. struct xrp_refcounted *ref = buf;
  112. return --ref->count == 0;
  113. }
  114. #endif