ttm_memory.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. * @lock: Lock to protect the @shrink - and the memory accounting members,
  59. * that is, essentially the whole structure with some exceptions.
  60. * @zones: Array of pointers to accounting zones.
  61. * @num_zones: Number of populated entries in the @zones array.
  62. * @zone_kernel: Pointer to the kernel zone.
  63. * @zone_highmem: Pointer to the highmem zone if there is one.
  64. * @zone_dma32: Pointer to the dma32 zone if there is one.
  65. *
  66. * Note that this structure is not per device. It should be global for all
  67. * graphics devices.
  68. */
  69. #define TTM_MEM_MAX_ZONES 2
  70. struct ttm_mem_zone;
  71. struct ttm_mem_global {
  72. struct kobject kobj;
  73. struct ttm_mem_shrink *shrink;
  74. struct workqueue_struct *swap_queue;
  75. struct work_struct work;
  76. spinlock_t lock;
  77. struct ttm_mem_zone *zones[TTM_MEM_MAX_ZONES];
  78. unsigned int num_zones;
  79. struct ttm_mem_zone *zone_kernel;
  80. #ifdef CONFIG_HIGHMEM
  81. struct ttm_mem_zone *zone_highmem;
  82. #else
  83. struct ttm_mem_zone *zone_dma32;
  84. #endif
  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);
  137. extern void ttm_mem_global_free(struct ttm_mem_global *glob,
  138. uint64_t amount);
  139. extern int ttm_mem_global_alloc_page(struct ttm_mem_global *glob,
  140. struct page *page,
  141. bool no_wait, bool interruptible);
  142. extern void ttm_mem_global_free_page(struct ttm_mem_global *glob,
  143. struct page *page);
  144. extern size_t ttm_round_pot(size_t size);
  145. #endif