ttm_memory.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /**************************************************************************
  2. *
  3. * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., 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. #ifndef TTM_MEMORY_H
  28. #define TTM_MEMORY_H
  29. #include <linux/workqueue.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/wait.h>
  32. #include <linux/errno.h>
  33. /**
  34. * struct ttm_mem_shrink - callback to shrink TTM memory usage.
  35. *
  36. * @do_shrink: The callback function.
  37. *
  38. * Arguments to the do_shrink functions are intended to be passed using
  39. * inheritance. That is, the argument class derives from struct ttm_mem_srink,
  40. * and can be accessed using container_of().
  41. */
  42. struct ttm_mem_shrink {
  43. int (*do_shrink) (struct ttm_mem_shrink *);
  44. };
  45. /**
  46. * struct ttm_mem_global - Global memory accounting structure.
  47. *
  48. * @shrink: A single callback to shrink TTM memory usage. Extend this
  49. * to a linked list to be able to handle multiple callbacks when needed.
  50. * @swap_queue: A workqueue to handle shrinking in low memory situations. We
  51. * need a separate workqueue since it will spend a lot of time waiting
  52. * for the GPU, and this will otherwise block other workqueue tasks(?)
  53. * At this point we use only a single-threaded workqueue.
  54. * @work: The workqueue callback for the shrink queue.
  55. * @queue: Wait queue for processes suspended waiting for memory.
  56. * @lock: Lock to protect the @shrink - and the memory accounting members,
  57. * that is, essentially the whole structure with some exceptions.
  58. * @emer_memory: Lowmem memory limit available for root.
  59. * @max_memory: Lowmem memory limit available for non-root.
  60. * @swap_limit: Lowmem memory limit where the shrink workqueue kicks in.
  61. * @used_memory: Currently used lowmem memory.
  62. * @used_total_memory: Currently used total (lowmem + highmem) memory.
  63. * @total_memory_swap_limit: Total memory limit where the shrink workqueue
  64. * kicks in.
  65. * @max_total_memory: Total memory available to non-root processes.
  66. * @emer_total_memory: Total memory available to root processes.
  67. *
  68. * Note that this structure is not per device. It should be global for all
  69. * graphics devices.
  70. */
  71. struct ttm_mem_global {
  72. struct ttm_mem_shrink *shrink;
  73. struct workqueue_struct *swap_queue;
  74. struct work_struct work;
  75. wait_queue_head_t queue;
  76. spinlock_t lock;
  77. uint64_t emer_memory;
  78. uint64_t max_memory;
  79. uint64_t swap_limit;
  80. uint64_t used_memory;
  81. uint64_t used_total_memory;
  82. uint64_t total_memory_swap_limit;
  83. uint64_t max_total_memory;
  84. uint64_t emer_total_memory;
  85. };
  86. /**
  87. * ttm_mem_init_shrink - initialize a struct ttm_mem_shrink object
  88. *
  89. * @shrink: The object to initialize.
  90. * @func: The callback function.
  91. */
  92. static inline void ttm_mem_init_shrink(struct ttm_mem_shrink *shrink,
  93. int (*func) (struct ttm_mem_shrink *))
  94. {
  95. shrink->do_shrink = func;
  96. }
  97. /**
  98. * ttm_mem_register_shrink - register a struct ttm_mem_shrink object.
  99. *
  100. * @glob: The struct ttm_mem_global object to register with.
  101. * @shrink: An initialized struct ttm_mem_shrink object to register.
  102. *
  103. * Returns:
  104. * -EBUSY: There's already a callback registered. (May change).
  105. */
  106. static inline int ttm_mem_register_shrink(struct ttm_mem_global *glob,
  107. struct ttm_mem_shrink *shrink)
  108. {
  109. spin_lock(&glob->lock);
  110. if (glob->shrink != NULL) {
  111. spin_unlock(&glob->lock);
  112. return -EBUSY;
  113. }
  114. glob->shrink = shrink;
  115. spin_unlock(&glob->lock);
  116. return 0;
  117. }
  118. /**
  119. * ttm_mem_unregister_shrink - unregister a struct ttm_mem_shrink object.
  120. *
  121. * @glob: The struct ttm_mem_global object to unregister from.
  122. * @shrink: A previously registert struct ttm_mem_shrink object.
  123. *
  124. */
  125. static inline void ttm_mem_unregister_shrink(struct ttm_mem_global *glob,
  126. struct ttm_mem_shrink *shrink)
  127. {
  128. spin_lock(&glob->lock);
  129. BUG_ON(glob->shrink != shrink);
  130. glob->shrink = NULL;
  131. spin_unlock(&glob->lock);
  132. }
  133. extern int ttm_mem_global_init(struct ttm_mem_global *glob);
  134. extern void ttm_mem_global_release(struct ttm_mem_global *glob);
  135. extern int ttm_mem_global_alloc(struct ttm_mem_global *glob, uint64_t memory,
  136. bool no_wait, bool interruptible, bool himem);
  137. extern void ttm_mem_global_free(struct ttm_mem_global *glob,
  138. uint64_t amount, bool himem);
  139. extern size_t ttm_round_pot(size_t size);
  140. #endif