radeon_mem.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 <drm/drmP.h>
  32. #include <drm/radeon_drm.h>
  33. #include "radeon_drv.h"
  34. /* Very simple allocator for GART memory, working on a static range
  35. * already mapped into each client's address space.
  36. */
  37. static struct mem_block *split_block(struct mem_block *p, int start, int size,
  38. struct drm_file *file_priv)
  39. {
  40. /* Maybe cut off the start of an existing block */
  41. if (start > p->start) {
  42. struct mem_block *newblock = kmalloc(sizeof(*newblock),
  43. GFP_KERNEL);
  44. if (!newblock)
  45. goto out;
  46. newblock->start = start;
  47. newblock->size = p->size - (start - p->start);
  48. newblock->file_priv = NULL;
  49. newblock->next = p->next;
  50. newblock->prev = p;
  51. p->next->prev = newblock;
  52. p->next = newblock;
  53. p->size -= newblock->size;
  54. p = newblock;
  55. }
  56. /* Maybe cut off the end of an existing block */
  57. if (size < p->size) {
  58. struct mem_block *newblock = kmalloc(sizeof(*newblock),
  59. GFP_KERNEL);
  60. if (!newblock)
  61. goto out;
  62. newblock->start = start + size;
  63. newblock->size = p->size - size;
  64. newblock->file_priv = NULL;
  65. newblock->next = p->next;
  66. newblock->prev = p;
  67. p->next->prev = newblock;
  68. p->next = newblock;
  69. p->size = size;
  70. }
  71. out:
  72. /* Our block is in the middle */
  73. p->file_priv = file_priv;
  74. return p;
  75. }
  76. static struct mem_block *alloc_block(struct mem_block *heap, int size,
  77. int align2, struct drm_file *file_priv)
  78. {
  79. struct mem_block *p;
  80. int mask = (1 << align2) - 1;
  81. list_for_each(p, heap) {
  82. int start = (p->start + mask) & ~mask;
  83. if (p->file_priv == NULL && start + size <= p->start + p->size)
  84. return split_block(p, start, size, file_priv);
  85. }
  86. return NULL;
  87. }
  88. static struct mem_block *find_block(struct mem_block *heap, int start)
  89. {
  90. struct mem_block *p;
  91. list_for_each(p, heap)
  92. if (p->start == start)
  93. return p;
  94. return NULL;
  95. }
  96. static void free_block(struct mem_block *p)
  97. {
  98. p->file_priv = NULL;
  99. /* Assumes a single contiguous range. Needs a special file_priv in
  100. * 'heap' to stop it being subsumed.
  101. */
  102. if (p->next->file_priv == NULL) {
  103. struct mem_block *q = p->next;
  104. p->size += q->size;
  105. p->next = q->next;
  106. p->next->prev = p;
  107. kfree(q);
  108. }
  109. if (p->prev->file_priv == NULL) {
  110. struct mem_block *q = p->prev;
  111. q->size += p->size;
  112. q->next = p->next;
  113. q->next->prev = q;
  114. kfree(p);
  115. }
  116. }
  117. /* Initialize. How to check for an uninitialized heap?
  118. */
  119. static int init_heap(struct mem_block **heap, int start, int size)
  120. {
  121. struct mem_block *blocks = kmalloc(sizeof(*blocks), GFP_KERNEL);
  122. if (!blocks)
  123. return -ENOMEM;
  124. *heap = kzalloc(sizeof(**heap), GFP_KERNEL);
  125. if (!*heap) {
  126. kfree(blocks);
  127. return -ENOMEM;
  128. }
  129. blocks->start = start;
  130. blocks->size = size;
  131. blocks->file_priv = NULL;
  132. blocks->next = blocks->prev = *heap;
  133. (*heap)->file_priv = (struct drm_file *) - 1;
  134. (*heap)->next = (*heap)->prev = blocks;
  135. return 0;
  136. }
  137. /* Free all blocks associated with the releasing file.
  138. */
  139. void radeon_mem_release(struct drm_file *file_priv, struct mem_block *heap)
  140. {
  141. struct mem_block *p;
  142. if (!heap || !heap->next)
  143. return;
  144. list_for_each(p, heap) {
  145. if (p->file_priv == file_priv)
  146. p->file_priv = NULL;
  147. }
  148. /* Assumes a single contiguous range. Needs a special file_priv in
  149. * 'heap' to stop it being subsumed.
  150. */
  151. list_for_each(p, heap) {
  152. while (p->file_priv == NULL && p->next->file_priv == NULL) {
  153. struct mem_block *q = p->next;
  154. p->size += q->size;
  155. p->next = q->next;
  156. p->next->prev = p;
  157. kfree(q);
  158. }
  159. }
  160. }
  161. /* Shutdown.
  162. */
  163. void radeon_mem_takedown(struct mem_block **heap)
  164. {
  165. struct mem_block *p;
  166. if (!*heap)
  167. return;
  168. for (p = (*heap)->next; p != *heap;) {
  169. struct mem_block *q = p;
  170. p = p->next;
  171. kfree(q);
  172. }
  173. kfree(*heap);
  174. *heap = NULL;
  175. }
  176. /* IOCTL HANDLERS */
  177. static struct mem_block **get_heap(drm_radeon_private_t * dev_priv, int region)
  178. {
  179. switch (region) {
  180. case RADEON_MEM_REGION_GART:
  181. return &dev_priv->gart_heap;
  182. case RADEON_MEM_REGION_FB:
  183. return &dev_priv->fb_heap;
  184. default:
  185. return NULL;
  186. }
  187. }
  188. int radeon_mem_alloc(struct drm_device *dev, void *data, struct drm_file *file_priv)
  189. {
  190. drm_radeon_private_t *dev_priv = dev->dev_private;
  191. drm_radeon_mem_alloc_t *alloc = data;
  192. struct mem_block *block, **heap;
  193. if (!dev_priv) {
  194. DRM_ERROR("called with no initialization\n");
  195. return -EINVAL;
  196. }
  197. heap = get_heap(dev_priv, alloc->region);
  198. if (!heap || !*heap)
  199. return -EFAULT;
  200. /* Make things easier on ourselves: all allocations at least
  201. * 4k aligned.
  202. */
  203. if (alloc->alignment < 12)
  204. alloc->alignment = 12;
  205. block = alloc_block(*heap, alloc->size, alloc->alignment, file_priv);
  206. if (!block)
  207. return -ENOMEM;
  208. if (DRM_COPY_TO_USER(alloc->region_offset, &block->start,
  209. sizeof(int))) {
  210. DRM_ERROR("copy_to_user\n");
  211. return -EFAULT;
  212. }
  213. return 0;
  214. }
  215. int radeon_mem_free(struct drm_device *dev, void *data, struct drm_file *file_priv)
  216. {
  217. drm_radeon_private_t *dev_priv = dev->dev_private;
  218. drm_radeon_mem_free_t *memfree = data;
  219. struct mem_block *block, **heap;
  220. if (!dev_priv) {
  221. DRM_ERROR("called with no initialization\n");
  222. return -EINVAL;
  223. }
  224. heap = get_heap(dev_priv, memfree->region);
  225. if (!heap || !*heap)
  226. return -EFAULT;
  227. block = find_block(*heap, memfree->region_offset);
  228. if (!block)
  229. return -EFAULT;
  230. if (block->file_priv != file_priv)
  231. return -EPERM;
  232. free_block(block);
  233. return 0;
  234. }
  235. int radeon_mem_init_heap(struct drm_device *dev, void *data, struct drm_file *file_priv)
  236. {
  237. drm_radeon_private_t *dev_priv = dev->dev_private;
  238. drm_radeon_mem_init_heap_t *initheap = data;
  239. struct mem_block **heap;
  240. if (!dev_priv) {
  241. DRM_ERROR("called with no initialization\n");
  242. return -EINVAL;
  243. }
  244. heap = get_heap(dev_priv, initheap->region);
  245. if (!heap)
  246. return -EFAULT;
  247. if (*heap) {
  248. DRM_ERROR("heap already initialized?");
  249. return -EFAULT;
  250. }
  251. return init_heap(heap, initheap->start, initheap->size);
  252. }