drm_sman.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 above copyright notice and this permission notice (including the
  15. * next paragraph) shall be included in all copies or substantial portions
  16. * of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  21. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  22. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  23. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  24. * USE OR OTHER DEALINGS IN 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. #ifndef DRM_SMAN_H
  39. #define DRM_SMAN_H
  40. #include "drmP.h"
  41. #include "drm_hashtab.h"
  42. /*
  43. * A class that is an abstration of a simple memory allocator.
  44. * The sman implementation provides a default such allocator
  45. * using the drm_mm.c implementation. But the user can replace it.
  46. * See the SiS implementation, which may use the SiS FB kernel module
  47. * for memory management.
  48. */
  49. struct drm_sman_mm {
  50. /* private info. If allocated, needs to be destroyed by the destroy
  51. function */
  52. void *private;
  53. /* Allocate a memory block with given size and alignment.
  54. Return an opaque reference to the memory block */
  55. void *(*allocate) (void *private, unsigned long size,
  56. unsigned alignment);
  57. /* Free a memory block. "ref" is the opaque reference that we got from
  58. the "alloc" function */
  59. void (*free) (void *private, void *ref);
  60. /* Free all resources associated with this allocator */
  61. void (*destroy) (void *private);
  62. /* Return a memory offset from the opaque reference returned from the
  63. "alloc" function */
  64. unsigned long (*offset) (void *private, void *ref);
  65. };
  66. struct drm_memblock_item {
  67. struct list_head owner_list;
  68. struct drm_hash_item user_hash;
  69. void *mm_info;
  70. struct drm_sman_mm *mm;
  71. struct drm_sman *sman;
  72. };
  73. struct drm_sman {
  74. struct drm_sman_mm *mm;
  75. int num_managers;
  76. struct drm_open_hash owner_hash_tab;
  77. struct drm_open_hash user_hash_tab;
  78. struct list_head owner_items;
  79. };
  80. /*
  81. * Take down a memory manager. This function should only be called after a
  82. * successful init and after a call to drm_sman_cleanup.
  83. */
  84. extern void drm_sman_takedown(struct drm_sman * sman);
  85. /*
  86. * Allocate structures for a manager.
  87. * num_managers are the number of memory pools to manage. (VRAM, AGP, ....)
  88. * user_order is the log2 of the number of buckets in the user hash table.
  89. * set this to approximately log2 of the max number of memory regions
  90. * that will be allocated for _all_ pools together.
  91. * owner_order is the log2 of the number of buckets in the owner hash table.
  92. * set this to approximately log2 of
  93. * the number of client file connections that will
  94. * be using the manager.
  95. *
  96. */
  97. extern int drm_sman_init(struct drm_sman * sman, unsigned int num_managers,
  98. unsigned int user_order, unsigned int owner_order);
  99. /*
  100. * Initialize a drm_mm.c allocator. Should be called only once for each
  101. * manager unless a customized allogator is used.
  102. */
  103. extern int drm_sman_set_range(struct drm_sman * sman, unsigned int manager,
  104. unsigned long start, unsigned long size);
  105. /*
  106. * Initialize a customized allocator for one of the managers.
  107. * (See the SiS module). The object pointed to by "allocator" is copied,
  108. * so it can be destroyed after this call.
  109. */
  110. extern int drm_sman_set_manager(struct drm_sman * sman, unsigned int mananger,
  111. struct drm_sman_mm * allocator);
  112. /*
  113. * Allocate a memory block. Aligment is not implemented yet.
  114. */
  115. extern struct drm_memblock_item *drm_sman_alloc(struct drm_sman * sman,
  116. unsigned int manager,
  117. unsigned long size,
  118. unsigned alignment,
  119. unsigned long owner);
  120. /*
  121. * Free a memory block identified by its user hash key.
  122. */
  123. extern int drm_sman_free_key(struct drm_sman * sman, unsigned int key);
  124. /*
  125. * returns 1 iff there are no stale memory blocks associated with this owner.
  126. * Typically called to determine if we need to idle the hardware and call
  127. * drm_sman_owner_cleanup. If there are no stale memory blocks, it removes all
  128. * resources associated with owner.
  129. */
  130. extern int drm_sman_owner_clean(struct drm_sman * sman, unsigned long owner);
  131. /*
  132. * Frees all stale memory blocks associated with this owner. Note that this
  133. * requires that the hardware is finished with all blocks, so the graphics engine
  134. * should be idled before this call is made. This function also frees
  135. * any resources associated with "owner" and should be called when owner
  136. * is not going to be referenced anymore.
  137. */
  138. extern void drm_sman_owner_cleanup(struct drm_sman * sman, unsigned long owner);
  139. /*
  140. * Frees all stale memory blocks associated with the memory manager.
  141. * See idling above.
  142. */
  143. extern void drm_sman_cleanup(struct drm_sman * sman);
  144. #endif