radeon_mem.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 = kzalloc(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. (*heap)->file_priv = (struct drm_file *) - 1;
  135. (*heap)->next = (*heap)->prev = blocks;
  136. return 0;
  137. }
  138. /* Free all blocks associated with the releasing file.
  139. */
  140. void radeon_mem_release(struct drm_file *file_priv, struct mem_block *heap)
  141. {
  142. struct mem_block *p;
  143. if (!heap || !heap->next)
  144. return;
  145. list_for_each(p, heap) {
  146. if (p->file_priv == file_priv)
  147. p->file_priv = NULL;
  148. }
  149. /* Assumes a single contiguous range. Needs a special file_priv in
  150. * 'heap' to stop it being subsumed.
  151. */
  152. list_for_each(p, heap) {
  153. while (p->file_priv == NULL && p->next->file_priv == NULL) {
  154. struct mem_block *q = p->next;
  155. p->size += q->size;
  156. p->next = q->next;
  157. p->next->prev = p;
  158. kfree(q);
  159. }
  160. }
  161. }
  162. /* Shutdown.
  163. */
  164. void radeon_mem_takedown(struct mem_block **heap)
  165. {
  166. struct mem_block *p;
  167. if (!*heap)
  168. return;
  169. for (p = (*heap)->next; p != *heap;) {
  170. struct mem_block *q = p;
  171. p = p->next;
  172. kfree(q);
  173. }
  174. kfree(*heap);
  175. *heap = NULL;
  176. }
  177. /* IOCTL HANDLERS */
  178. static struct mem_block **get_heap(drm_radeon_private_t * dev_priv, int region)
  179. {
  180. switch (region) {
  181. case RADEON_MEM_REGION_GART:
  182. return &dev_priv->gart_heap;
  183. case RADEON_MEM_REGION_FB:
  184. return &dev_priv->fb_heap;
  185. default:
  186. return NULL;
  187. }
  188. }
  189. int radeon_mem_alloc(struct drm_device *dev, void *data, struct drm_file *file_priv)
  190. {
  191. drm_radeon_private_t *dev_priv = dev->dev_private;
  192. drm_radeon_mem_alloc_t *alloc = data;
  193. struct mem_block *block, **heap;
  194. if (!dev_priv) {
  195. DRM_ERROR("called with no initialization\n");
  196. return -EINVAL;
  197. }
  198. heap = get_heap(dev_priv, alloc->region);
  199. if (!heap || !*heap)
  200. return -EFAULT;
  201. /* Make things easier on ourselves: all allocations at least
  202. * 4k aligned.
  203. */
  204. if (alloc->alignment < 12)
  205. alloc->alignment = 12;
  206. block = alloc_block(*heap, alloc->size, alloc->alignment, file_priv);
  207. if (!block)
  208. return -ENOMEM;
  209. if (DRM_COPY_TO_USER(alloc->region_offset, &block->start,
  210. sizeof(int))) {
  211. DRM_ERROR("copy_to_user\n");
  212. return -EFAULT;
  213. }
  214. return 0;
  215. }
  216. int radeon_mem_free(struct drm_device *dev, void *data, struct drm_file *file_priv)
  217. {
  218. drm_radeon_private_t *dev_priv = dev->dev_private;
  219. drm_radeon_mem_free_t *memfree = data;
  220. struct mem_block *block, **heap;
  221. if (!dev_priv) {
  222. DRM_ERROR("called with no initialization\n");
  223. return -EINVAL;
  224. }
  225. heap = get_heap(dev_priv, memfree->region);
  226. if (!heap || !*heap)
  227. return -EFAULT;
  228. block = find_block(*heap, memfree->region_offset);
  229. if (!block)
  230. return -EFAULT;
  231. if (block->file_priv != file_priv)
  232. return -EPERM;
  233. free_block(block);
  234. return 0;
  235. }
  236. int radeon_mem_init_heap(struct drm_device *dev, void *data, struct drm_file *file_priv)
  237. {
  238. drm_radeon_private_t *dev_priv = dev->dev_private;
  239. drm_radeon_mem_init_heap_t *initheap = data;
  240. struct mem_block **heap;
  241. if (!dev_priv) {
  242. DRM_ERROR("called with no initialization\n");
  243. return -EINVAL;
  244. }
  245. heap = get_heap(dev_priv, initheap->region);
  246. if (!heap)
  247. return -EFAULT;
  248. if (*heap) {
  249. DRM_ERROR("heap already initialized?");
  250. return -EFAULT;
  251. }
  252. return init_heap(heap, initheap->start, initheap->size);
  253. }