drm_buddy.h 4.4 KB

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