radeon_mem.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /* radeon_mem.c -- Simple GART/fb memory manager for radeon -*- linux-c -*- */
  2. /*
  3. * Copyright (C) The Weather Channel, Inc. 2002. All Rights Reserved.
  4. *
  5. * The Weather Channel (TM) funded Tungsten Graphics to develop the
  6. * initial release of the Radeon 8500 driver under the XFree86 license.
  7. * This notice must be preserved.
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a
  10. * copy of this software and associated documentation files (the "Software"),
  11. * to deal in the Software without restriction, including without limitation
  12. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  13. * and/or sell copies of the Software, and to permit persons to whom the
  14. * Software is furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice (including the next
  17. * paragraph) shall be included in all copies or substantial portions of the
  18. * Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  23. * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  24. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  25. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  26. * DEALINGS IN THE SOFTWARE.
  27. *
  28. * Authors:
  29. * Keith Whitwell <keith@tungstengraphics.com>
  30. */
  31. #include "drmP.h"
  32. #include "drm.h"
  33. #include "radeon_drm.h"
  34. #include "radeon_drv.h"
  35. /* Very simple allocator for GART memory, working on a static range
  36. * already mapped into each client's address space.
  37. */
  38. static struct mem_block *split_block(struct mem_block *p, int start, int size,
  39. struct drm_file *file_priv)
  40. {
  41. /* Maybe cut off the start of an existing block */
  42. if (start > p->start) {
  43. struct mem_block *newblock = kmalloc(sizeof(*newblock),
  44. GFP_KERNEL);
  45. if (!newblock)
  46. goto out;
  47. newblock->start = start;
  48. newblock->size = p->size - (start - p->start);
  49. newblock->file_priv = NULL;
  50. newblock->next = p->next;
  51. newblock->prev = p;
  52. p->next->prev = newblock;
  53. p->next = newblock;
  54. p->size -= newblock->size;
  55. p = newblock;
  56. }
  57. /* Maybe cut off the end of an existing block */
  58. if (size < p->size) {
  59. struct mem_block *newblock = kmalloc(sizeof(*newblock),
  60. GFP_KERNEL);
  61. if (!newblock)
  62. goto out;
  63. newblock->start = start + size;
  64. newblock->size = p->size - size;
  65. newblock->file_priv = NULL;
  66. newblock->next = p->next;
  67. newblock->prev = p;
  68. p->next->prev = newblock;
  69. p->next = newblock;
  70. p->size = size;
  71. }
  72. out:
  73. /* Our block is in the middle */
  74. p->file_priv = file_priv;
  75. return p;
  76. }
  77. static struct mem_block *alloc_block(struct mem_block *heap, int size,
  78. int align2, struct drm_file *file_priv)
  79. {
  80. struct mem_block *p;
  81. int mask = (1 << align2) - 1;
  82. list_for_each(p, heap) {
  83. int start = (p->start + mask) & ~mask;
  84. if (p->file_priv == NULL && start + size <= p->start + p->size)
  85. return split_block(p, start, size, file_priv);
  86. }
  87. return NULL;
  88. }
  89. static struct mem_block *find_block(struct mem_block *heap, int start)
  90. {
  91. struct mem_block *p;
  92. list_for_each(p, heap)
  93. if (p->start == start)
  94. return p;
  95. return NULL;
  96. }
  97. static void free_block(struct mem_block *p)
  98. {
  99. p->file_priv = NULL;
  100. /* Assumes a single contiguous range. Needs a special file_priv in
  101. * 'heap' to stop it being subsumed.
  102. */
  103. if (p->next->file_priv == NULL) {
  104. struct mem_block *q = p->next;
  105. p->size += q->size;
  106. p->next = q->next;
  107. p->next->prev = p;
  108. kfree(q);
  109. }
  110. if (p->prev->file_priv == NULL) {
  111. struct mem_block *q = p->prev;
  112. q->size += p->size;
  113. q->next = p->next;
  114. q->next->prev = q;
  115. kfree(p);
  116. }
  117. }
  118. /* Initialize. How to check for an uninitialized heap?
  119. */
  120. static int init_heap(struct mem_block **heap, int start, int size)
  121. {
  122. struct mem_block *blocks = kmalloc(sizeof(*blocks), GFP_KERNEL);
  123. if (!blocks)
  124. return -ENOMEM;
  125. *heap = kmalloc(sizeof(**heap), GFP_KERNEL);
  126. if (!*heap) {
  127. kfree(blocks);
  128. return -ENOMEM;
  129. }
  130. blocks->start = start;
  131. blocks->size = size;
  132. blocks->file_priv = NULL;
  133. blocks->next = blocks->prev = *heap;
  134. memset(*heap, 0, sizeof(**heap));
  135. (*heap)->file_priv = (struct drm_file *) - 1;
  136. (*heap)->next = (*heap)->prev = blocks;
  137. return 0;
  138. }
  139. /* Free all blocks associated with the releasing file.
  140. */
  141. void radeon_mem_release(struct drm_file *file_priv, struct mem_block *heap)
  142. {
  143. struct mem_block *p;
  144. if (!heap || !heap->next)
  145. return;
  146. list_for_each(p, heap) {
  147. if (p->file_priv == file_priv)
  148. p->file_priv = NULL;
  149. }
  150. /* Assumes a single contiguous range. Needs a special file_priv in
  151. * 'heap' to stop it being subsumed.
  152. */
  153. list_for_each(p, heap) {
  154. while (p->file_priv == NULL && p->next->file_priv == NULL) {
  155. struct mem_block *q = p->next;
  156. p->size += q->size;
  157. p->next = q->next;
  158. p->next->prev = p;
  159. kfree(q);
  160. }
  161. }
  162. }
  163. /* Shutdown.
  164. */
  165. void radeon_mem_takedown(struct mem_block **heap)
  166. {
  167. struct mem_block *p;
  168. if (!*heap)
  169. return;
  170. for (p = (*heap)->next; p != *heap;) {
  171. struct mem_block *q = p;
  172. p = p->next;
  173. kfree(q);
  174. }
  175. kfree(*heap);
  176. *heap = NULL;
  177. }
  178. /* IOCTL HANDLERS */
  179. static struct mem_block **get_heap(drm_radeon_private_t * dev_priv, int region)
  180. {
  181. switch (region) {
  182. case RADEON_MEM_REGION_GART:
  183. return &dev_priv->gart_heap;
  184. case RADEON_MEM_REGION_FB:
  185. return &dev_priv->fb_heap;
  186. default:
  187. return NULL;
  188. }
  189. }
  190. int radeon_mem_alloc(struct drm_device *dev, void *data, struct drm_file *file_priv)
  191. {
  192. drm_radeon_private_t *dev_priv = dev->dev_private;
  193. drm_radeon_mem_alloc_t *alloc = data;
  194. struct mem_block *block, **heap;
  195. if (!dev_priv) {
  196. DRM_ERROR("called with no initialization\n");
  197. return -EINVAL;
  198. }
  199. heap = get_heap(dev_priv, alloc->region);
  200. if (!heap || !*heap)
  201. return -EFAULT;
  202. /* Make things easier on ourselves: all allocations at least
  203. * 4k aligned.
  204. */
  205. if (alloc->alignment < 12)
  206. alloc->alignment = 12;
  207. block = alloc_block(*heap, alloc->size, alloc->alignment, file_priv);
  208. if (!block)
  209. return -ENOMEM;
  210. if (DRM_COPY_TO_USER(alloc->region_offset, &block->start,
  211. sizeof(int))) {
  212. DRM_ERROR("copy_to_user\n");
  213. return -EFAULT;
  214. }
  215. return 0;
  216. }
  217. int radeon_mem_free(struct drm_device *dev, void *data, struct drm_file *file_priv)
  218. {
  219. drm_radeon_private_t *dev_priv = dev->dev_private;
  220. drm_radeon_mem_free_t *memfree = data;
  221. struct mem_block *block, **heap;
  222. if (!dev_priv) {
  223. DRM_ERROR("called with no initialization\n");
  224. return -EINVAL;
  225. }
  226. heap = get_heap(dev_priv, memfree->region);
  227. if (!heap || !*heap)
  228. return -EFAULT;
  229. block = find_block(*heap, memfree->region_offset);
  230. if (!block)
  231. return -EFAULT;
  232. if (block->file_priv != file_priv)
  233. return -EPERM;
  234. free_block(block);
  235. return 0;
  236. }
  237. int radeon_mem_init_heap(struct drm_device *dev, void *data, struct drm_file *file_priv)
  238. {
  239. drm_radeon_private_t *dev_priv = dev->dev_private;
  240. drm_radeon_mem_init_heap_t *initheap = data;
  241. struct mem_block **heap;
  242. if (!dev_priv) {
  243. DRM_ERROR("called with no initialization\n");
  244. return -EINVAL;
  245. }
  246. heap = get_heap(dev_priv, initheap->region);
  247. if (!heap)
  248. return -EFAULT;
  249. if (*heap) {
  250. DRM_ERROR("heap already initialized?");
  251. return -EFAULT;
  252. }
  253. return init_heap(heap, initheap->start, initheap->size);
  254. }