drm_buddy.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* SPDX-License-Identifier: MIT */
  2. /*
  3. * Copyright © 2021 Intel Corporation
  4. */
  5. #ifndef __DRM_BUDDY_H__
  6. #define __DRM_BUDDY_H__
  7. #include <linux/bitops.h>
  8. #include <linux/list.h>
  9. #include <linux/slab.h>
  10. #include <linux/sched.h>
  11. #include <drm/drm_print.h>
  12. #define range_overflows(start, size, max) ({ \
  13. typeof(start) start__ = (start); \
  14. typeof(size) size__ = (size); \
  15. typeof(max) max__ = (max); \
  16. (void)(&start__ == &size__); \
  17. (void)(&start__ == &max__); \
  18. start__ >= max__ || size__ > max__ - start__; \
  19. })
  20. #define DRM_BUDDY_RANGE_ALLOCATION BIT(0)
  21. #define DRM_BUDDY_TOPDOWN_ALLOCATION BIT(1)
  22. #define DRM_BUDDY_CONTIGUOUS_ALLOCATION BIT(2)
  23. #define DRM_BUDDY_CLEAR_ALLOCATION BIT(3)
  24. #define DRM_BUDDY_CLEARED BIT(4)
  25. #define DRM_BUDDY_TRIM_DISABLE BIT(5)
  26. struct drm_buddy_block {
  27. #define DRM_BUDDY_HEADER_OFFSET GENMASK_ULL(63, 12)
  28. #define DRM_BUDDY_HEADER_STATE GENMASK_ULL(11, 10)
  29. #define DRM_BUDDY_ALLOCATED (1 << 10)
  30. #define DRM_BUDDY_FREE (2 << 10)
  31. #define DRM_BUDDY_SPLIT (3 << 10)
  32. #define DRM_BUDDY_HEADER_CLEAR GENMASK_ULL(9, 9)
  33. /* Free to be used, if needed in the future */
  34. #define DRM_BUDDY_HEADER_UNUSED GENMASK_ULL(8, 6)
  35. #define DRM_BUDDY_HEADER_ORDER GENMASK_ULL(5, 0)
  36. u64 header;
  37. struct drm_buddy_block *left;
  38. struct drm_buddy_block *right;
  39. struct drm_buddy_block *parent;
  40. void *private; /* owned by creator */
  41. /*
  42. * While the block is allocated by the user through drm_buddy_alloc*,
  43. * the user has ownership of the link, for example to maintain within
  44. * a list, if so desired. As soon as the block is freed with
  45. * drm_buddy_free* ownership is given back to the mm.
  46. */
  47. struct list_head link;
  48. struct list_head tmp_link;
  49. };
  50. /* Order-zero must be at least SZ_4K */
  51. #define DRM_BUDDY_MAX_ORDER (63 - 12)
  52. /*
  53. * Binary Buddy System.
  54. *
  55. * Locking should be handled by the user, a simple mutex around
  56. * drm_buddy_alloc* and drm_buddy_free* should suffice.
  57. */
  58. struct drm_buddy {
  59. /* Maintain a free list for each order. */
  60. struct list_head *free_list;
  61. /*
  62. * Maintain explicit binary tree(s) to track the allocation of the
  63. * address space. This gives us a simple way of finding a buddy block
  64. * and performing the potentially recursive merge step when freeing a
  65. * block. Nodes are either allocated or free, in which case they will
  66. * also exist on the respective free list.
  67. */
  68. struct drm_buddy_block **roots;
  69. /*
  70. * Anything from here is public, and remains static for the lifetime of
  71. * the mm. Everything above is considered do-not-touch.
  72. */
  73. unsigned int n_roots;
  74. unsigned int max_order;
  75. /* Must be at least SZ_4K */
  76. u64 chunk_size;
  77. u64 size;
  78. u64 avail;
  79. u64 clear_avail;
  80. };
  81. static inline u64
  82. drm_buddy_block_offset(struct drm_buddy_block *block)
  83. {
  84. return block->header & DRM_BUDDY_HEADER_OFFSET;
  85. }
  86. static inline unsigned int
  87. drm_buddy_block_order(struct drm_buddy_block *block)
  88. {
  89. return block->header & DRM_BUDDY_HEADER_ORDER;
  90. }
  91. static inline unsigned int
  92. drm_buddy_block_state(struct drm_buddy_block *block)
  93. {
  94. return block->header & DRM_BUDDY_HEADER_STATE;
  95. }
  96. static inline bool
  97. drm_buddy_block_is_allocated(struct drm_buddy_block *block)
  98. {
  99. return drm_buddy_block_state(block) == DRM_BUDDY_ALLOCATED;
  100. }
  101. static inline bool
  102. drm_buddy_block_is_clear(struct drm_buddy_block *block)
  103. {
  104. return block->header & DRM_BUDDY_HEADER_CLEAR;
  105. }
  106. static inline bool
  107. drm_buddy_block_is_free(struct drm_buddy_block *block)
  108. {
  109. return drm_buddy_block_state(block) == DRM_BUDDY_FREE;
  110. }
  111. static inline bool
  112. drm_buddy_block_is_split(struct drm_buddy_block *block)
  113. {
  114. return drm_buddy_block_state(block) == DRM_BUDDY_SPLIT;
  115. }
  116. static inline u64
  117. drm_buddy_block_size(struct drm_buddy *mm,
  118. struct drm_buddy_block *block)
  119. {
  120. return mm->chunk_size << drm_buddy_block_order(block);
  121. }
  122. int drm_buddy_init(struct drm_buddy *mm, u64 size, u64 chunk_size);
  123. void drm_buddy_fini(struct drm_buddy *mm);
  124. struct drm_buddy_block *
  125. drm_get_buddy(struct drm_buddy_block *block);
  126. int drm_buddy_alloc_blocks(struct drm_buddy *mm,
  127. u64 start, u64 end, u64 size,
  128. u64 min_page_size,
  129. struct list_head *blocks,
  130. unsigned long flags);
  131. int drm_buddy_block_trim(struct drm_buddy *mm,
  132. u64 *start,
  133. u64 new_size,
  134. struct list_head *blocks);
  135. void drm_buddy_reset_clear(struct drm_buddy *mm, bool is_clear);
  136. void drm_buddy_free_block(struct drm_buddy *mm, struct drm_buddy_block *block);
  137. void drm_buddy_free_list(struct drm_buddy *mm,
  138. struct list_head *objects,
  139. unsigned int flags);
  140. void drm_buddy_print(struct drm_buddy *mm, struct drm_printer *p);
  141. void drm_buddy_block_print(struct drm_buddy *mm,
  142. struct drm_buddy_block *block,
  143. struct drm_printer *p);
  144. #endif