drm_sman.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /**************************************************************************
  2. *
  3. * Copyright 2006 Tungsten Graphics, Inc., Bismarck., ND., USA.
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sub license, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  18. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  19. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  20. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * The above copyright notice and this permission notice (including the
  23. * next paragraph) shall be included in all copies or substantial portions
  24. * of the Software.
  25. *
  26. *
  27. **************************************************************************/
  28. /*
  29. * Simple memory manager interface that keeps track on allocate regions on a
  30. * per "owner" basis. All regions associated with an "owner" can be released
  31. * with a simple call. Typically if the "owner" exists. The owner is any
  32. * "unsigned long" identifier. Can typically be a pointer to a file private
  33. * struct or a context identifier.
  34. *
  35. * Authors:
  36. * Thomas Hellström <thomas-at-tungstengraphics-dot-com>
  37. */
  38. #include <linux/export.h>
  39. #include "drm_sman.h"
  40. struct drm_owner_item {
  41. struct drm_hash_item owner_hash;
  42. struct list_head sman_list;
  43. struct list_head mem_blocks;
  44. };
  45. void drm_sman_takedown(struct drm_sman * sman)
  46. {
  47. kfree(sman->mm);
  48. }
  49. EXPORT_SYMBOL(drm_sman_takedown);
  50. int
  51. drm_sman_init(struct drm_sman * sman, unsigned int num_managers,
  52. unsigned int user_order, unsigned int owner_order)
  53. {
  54. int ret = 0;
  55. sman->mm = kcalloc(num_managers, sizeof(*sman->mm), GFP_KERNEL);
  56. if (!sman->mm) {
  57. ret = -ENOMEM;
  58. return ret;
  59. }
  60. sman->num_managers = num_managers;
  61. return 0;
  62. }
  63. EXPORT_SYMBOL(drm_sman_init);
  64. static void *drm_sman_mm_allocate(void *private, unsigned long size,
  65. unsigned alignment)
  66. {
  67. struct drm_mm *mm = (struct drm_mm *) private;
  68. struct drm_mm_node *tmp;
  69. tmp = drm_mm_search_free(mm, size, alignment, 1);
  70. if (!tmp) {
  71. return NULL;
  72. }
  73. tmp = drm_mm_get_block(tmp, size, alignment);
  74. return tmp;
  75. }
  76. static void drm_sman_mm_free(void *private, void *ref)
  77. {
  78. struct drm_mm_node *node = (struct drm_mm_node *) ref;
  79. drm_mm_put_block(node);
  80. }
  81. static void drm_sman_mm_destroy(void *private)
  82. {
  83. struct drm_mm *mm = (struct drm_mm *) private;
  84. drm_mm_takedown(mm);
  85. kfree(mm);
  86. }
  87. static unsigned long drm_sman_mm_offset(void *private, void *ref)
  88. {
  89. struct drm_mm_node *node = (struct drm_mm_node *) ref;
  90. return node->start;
  91. }
  92. int
  93. drm_sman_set_range(struct drm_sman * sman, unsigned int manager,
  94. unsigned long start, unsigned long size)
  95. {
  96. struct drm_sman_mm *sman_mm;
  97. struct drm_mm *mm;
  98. int ret;
  99. BUG_ON(manager >= sman->num_managers);
  100. sman_mm = &sman->mm[manager];
  101. mm = kzalloc(sizeof(*mm), GFP_KERNEL);
  102. if (!mm) {
  103. return -ENOMEM;
  104. }
  105. sman_mm->private = mm;
  106. ret = drm_mm_init(mm, start, size);
  107. if (ret) {
  108. kfree(mm);
  109. return ret;
  110. }
  111. sman_mm->allocate = drm_sman_mm_allocate;
  112. sman_mm->free = drm_sman_mm_free;
  113. sman_mm->destroy = drm_sman_mm_destroy;
  114. sman_mm->offset = drm_sman_mm_offset;
  115. return 0;
  116. }
  117. EXPORT_SYMBOL(drm_sman_set_range);
  118. int
  119. drm_sman_set_manager(struct drm_sman * sman, unsigned int manager,
  120. struct drm_sman_mm * allocator)
  121. {
  122. BUG_ON(manager >= sman->num_managers);
  123. sman->mm[manager] = *allocator;
  124. return 0;
  125. }
  126. EXPORT_SYMBOL(drm_sman_set_manager);
  127. struct drm_memblock_item *drm_sman_alloc(struct drm_sman *sman, unsigned int manager,
  128. unsigned long size, unsigned alignment,
  129. unsigned long owner)
  130. {
  131. void *tmp;
  132. struct drm_sman_mm *sman_mm;
  133. struct drm_memblock_item *memblock;
  134. BUG_ON(manager >= sman->num_managers);
  135. sman_mm = &sman->mm[manager];
  136. tmp = sman_mm->allocate(sman_mm->private, size, alignment);
  137. if (!tmp) {
  138. return NULL;
  139. }
  140. memblock = kzalloc(sizeof(*memblock), GFP_KERNEL);
  141. if (!memblock)
  142. goto out;
  143. memblock->mm_info = tmp;
  144. memblock->mm = sman_mm;
  145. memblock->sman = sman;
  146. return memblock;
  147. out:
  148. sman_mm->free(sman_mm->private, tmp);
  149. return NULL;
  150. }
  151. EXPORT_SYMBOL(drm_sman_alloc);
  152. void drm_sman_free(struct drm_memblock_item *item)
  153. {
  154. list_del(&item->owner_list);
  155. item->mm->free(item->mm->private, item->mm_info);
  156. kfree(item);
  157. }
  158. EXPORT_SYMBOL(drm_sman_free);
  159. void drm_sman_cleanup(struct drm_sman *sman)
  160. {
  161. unsigned int i;
  162. struct drm_sman_mm *sman_mm;
  163. if (sman->mm) {
  164. for (i = 0; i < sman->num_managers; ++i) {
  165. sman_mm = &sman->mm[i];
  166. if (sman_mm->private) {
  167. sman_mm->destroy(sman_mm->private);
  168. sman_mm->private = NULL;
  169. }
  170. }
  171. }
  172. }
  173. EXPORT_SYMBOL(drm_sman_cleanup);