xrp_alloc.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (c) 2016 - 2017 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. * Alternatively you can use and distribute this file under the terms of
  24. * the GNU General Public License version 2 or later.
  25. */
  26. #ifndef XRP_ALLOC_H
  27. #define XRP_ALLOC_H
  28. #ifndef __KERNEL__
  29. #include <stdint.h>
  30. #include <xrp_atomic.h>
  31. #include <xrp_thread_impl.h>
  32. typedef uint32_t u32;
  33. typedef uint32_t phys_addr_t;
  34. typedef _Atomic uint32_t atomic_t;
  35. struct mutex {
  36. xrp_mutex o;
  37. };
  38. static inline void atomic_inc(atomic_t *v)
  39. {
  40. ++*(volatile atomic_t *)v;
  41. }
  42. static inline int atomic_dec_and_test(atomic_t *v)
  43. {
  44. return --*(volatile atomic_t *)v == 0;
  45. }
  46. #endif
  47. struct xrp_allocation_pool;
  48. struct xrp_allocation;
  49. struct xrp_allocation_ops {
  50. long (*alloc)(struct xrp_allocation_pool *allocation_pool,
  51. u32 size, u32 align, struct xrp_allocation **alloc);
  52. void (*free)(struct xrp_allocation *allocation);
  53. void (*free_pool)(struct xrp_allocation_pool *allocation_pool);
  54. phys_addr_t (*offset)(const struct xrp_allocation *allocation);
  55. };
  56. struct xrp_allocation_pool {
  57. const struct xrp_allocation_ops *ops;
  58. };
  59. struct xrp_allocation {
  60. struct xrp_allocation_pool *pool;
  61. struct xrp_allocation *next;
  62. phys_addr_t start;
  63. u32 size;
  64. atomic_t ref;
  65. };
  66. static inline void xrp_free_pool(struct xrp_allocation_pool *allocation_pool)
  67. {
  68. allocation_pool->ops->free_pool(allocation_pool);
  69. }
  70. static inline void xrp_free(struct xrp_allocation *allocation)
  71. {
  72. return allocation->pool->ops->free(allocation);
  73. }
  74. static inline long xrp_allocate(struct xrp_allocation_pool *allocation_pool,
  75. u32 size, u32 align,
  76. struct xrp_allocation **alloc)
  77. {
  78. return allocation_pool->ops->alloc(allocation_pool,
  79. size, align, alloc);
  80. }
  81. static inline void xrp_allocation_get(struct xrp_allocation *xrp_allocation)
  82. {
  83. atomic_inc(&xrp_allocation->ref);
  84. }
  85. static inline void xrp_allocation_put(struct xrp_allocation *xrp_allocation)
  86. {
  87. if (atomic_dec_and_test(&xrp_allocation->ref))
  88. xrp_allocation->pool->ops->free(xrp_allocation);
  89. }
  90. static inline phys_addr_t xrp_allocation_offset(const struct xrp_allocation *allocation)
  91. {
  92. return allocation->pool->ops->offset(allocation);
  93. }
  94. #endif