ttm_memory.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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/bug.h>
  32. #include <linux/wait.h>
  33. #include <linux/errno.h>
  34. #include <linux/kobject.h>
  35. #include <linux/mm.h>
  36. /**
  37. * struct ttm_mem_shrink - callback to shrink TTM memory usage.
  38. *
  39. * @do_shrink: The callback function.
  40. *
  41. * Arguments to the do_shrink functions are intended to be passed using
  42. * inheritance. That is, the argument class derives from struct ttm_mem_shrink,
  43. * and can be accessed using container_of().
  44. */
  45. struct ttm_mem_shrink {
  46. int (*do_shrink) (struct ttm_mem_shrink *);
  47. };
  48. /**
  49. * struct ttm_mem_global - Global memory accounting structure.
  50. *
  51. * @shrink: A single callback to shrink TTM memory usage. Extend this
  52. * to a linked list to be able to handle multiple callbacks when needed.
  53. * @swap_queue: A workqueue to handle shrinking in low memory situations. We
  54. * need a separate workqueue since it will spend a lot of time waiting
  55. * for the GPU, and this will otherwise block other workqueue tasks(?)
  56. * At this point we use only a single-threaded workqueue.
  57. * @work: The workqueue callback for the shrink queue.
  58. * @queue: Wait queue for processes suspended waiting for memory.
  59. * @lock: Lock to protect the @shrink - and the memory accounting members,
  60. * that is, essentially the whole structure with some exceptions.
  61. * @zones: Array of pointers to accounting zones.
  62. * @num_zones: Number of populated entries in the @zones array.
  63. * @zone_kernel: Pointer to the kernel zone.
  64. * @zone_highmem: Pointer to the highmem zone if there is one.
  65. * @zone_dma32: Pointer to the dma32 zone if there is one.
  66. *
  67. * Note that this structure is not per device. It should be global for all
  68. * graphics devices.
  69. */
  70. #define TTM_MEM_MAX_ZONES 2
  71. struct ttm_mem_zone;
  72. struct ttm_mem_global {
  73. struct kobject kobj;
  74. struct ttm_mem_shrink *shrink;
  75. struct workqueue_struct *swap_queue;
  76. struct work_struct work;
  77. wait_queue_head_t queue;
  78. spinlock_t lock;
  79. struct ttm_mem_zone *zones[TTM_MEM_MAX_ZONES];
  80. unsigned int num_zones;
  81. struct ttm_mem_zone *zone_kernel;
  82. #ifdef CONFIG_HIGHMEM
  83. struct ttm_mem_zone *zone_highmem;
  84. #else
  85. struct ttm_mem_zone *zone_dma32;
  86. #endif
  87. };
  88. /**
  89. * ttm_mem_init_shrink - initialize a struct ttm_mem_shrink object
  90. *
  91. * @shrink: The object to initialize.
  92. * @func: The callback function.
  93. */
  94. static inline void ttm_mem_init_shrink(struct ttm_mem_shrink *shrink,
  95. int (*func) (struct ttm_mem_shrink *))
  96. {
  97. shrink->do_shrink = func;
  98. }
  99. /**
  100. * ttm_mem_register_shrink - register a struct ttm_mem_shrink object.
  101. *
  102. * @glob: The struct ttm_mem_global object to register with.
  103. * @shrink: An initialized struct ttm_mem_shrink object to register.
  104. *
  105. * Returns:
  106. * -EBUSY: There's already a callback registered. (May change).
  107. */
  108. static inline int ttm_mem_register_shrink(struct ttm_mem_global *glob,
  109. struct ttm_mem_shrink *shrink)
  110. {
  111. spin_lock(&glob->lock);
  112. if (glob->shrink != NULL) {
  113. spin_unlock(&glob->lock);
  114. return -EBUSY;
  115. }
  116. glob->shrink = shrink;
  117. spin_unlock(&glob->lock);
  118. return 0;
  119. }
  120. /**
  121. * ttm_mem_unregister_shrink - unregister a struct ttm_mem_shrink object.
  122. *
  123. * @glob: The struct ttm_mem_global object to unregister from.
  124. * @shrink: A previously registert struct ttm_mem_shrink object.
  125. *
  126. */
  127. static inline void ttm_mem_unregister_shrink(struct ttm_mem_global *glob,
  128. struct ttm_mem_shrink *shrink)
  129. {
  130. spin_lock(&glob->lock);
  131. BUG_ON(glob->shrink != shrink);
  132. glob->shrink = NULL;
  133. spin_unlock(&glob->lock);
  134. }
  135. extern int ttm_mem_global_init(struct ttm_mem_global *glob);
  136. extern void ttm_mem_global_release(struct ttm_mem_global *glob);
  137. extern int ttm_mem_global_alloc(struct ttm_mem_global *glob, uint64_t memory,
  138. bool no_wait, bool interruptible);
  139. extern void ttm_mem_global_free(struct ttm_mem_global *glob,
  140. uint64_t amount);
  141. extern int ttm_mem_global_alloc_page(struct ttm_mem_global *glob,
  142. struct page *page,
  143. bool no_wait, bool interruptible);
  144. extern void ttm_mem_global_free_page(struct ttm_mem_global *glob,
  145. struct page *page);
  146. extern size_t ttm_round_pot(size_t size);
  147. #endif