radeon_mem.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. * ------------------------ This file is DEPRECATED! -------------------------
  32. */
  33. #include <drm/drmP.h>
  34. #include <drm/radeon_drm.h>
  35. #include "radeon_drv.h"
  36. /* Very simple allocator for GART memory, working on a static range
  37. * already mapped into each client's address space.
  38. */
  39. static struct mem_block *split_block(struct mem_block *p, int start, int size,
  40. struct drm_file *file_priv)
  41. {
  42. /* Maybe cut off the start of an existing block */
  43. if (start > p->start) {
  44. struct mem_block *newblock = kmalloc(sizeof(*newblock),
  45. GFP_KERNEL);
  46. if (!newblock)
  47. goto out;
  48. newblock->start = start;
  49. newblock->size = p->size - (start - p->start);
  50. newblock->file_priv = NULL;
  51. newblock->next = p->next;
  52. newblock->prev = p;
  53. p->next->prev = newblock;
  54. p->next = newblock;
  55. p->size -= newblock->size;
  56. p = newblock;
  57. }
  58. /* Maybe cut off the end of an existing block */
  59. if (size < p->size) {
  60. struct mem_block *newblock = kmalloc(sizeof(*newblock),
  61. GFP_KERNEL);
  62. if (!newblock)
  63. goto out;
  64. newblock->start = start + size;
  65. newblock->size = p->size - size;
  66. newblock->file_priv = NULL;
  67. newblock->next = p->next;
  68. newblock->prev = p;
  69. p->next->prev = newblock;
  70. p->next = newblock;
  71. p->size = size;
  72. }
  73. out:
  74. /* Our block is in the middle */
  75. p->file_priv = file_priv;
  76. return p;
  77. }
  78. static struct mem_block *alloc_block(struct mem_block *heap, int size,
  79. int align2, struct drm_file *file_priv)
  80. {
  81. struct mem_block *p;
  82. int mask = (1 << align2) - 1;
  83. list_for_each(p, heap) {
  84. int start = (p->start + mask) & ~mask;
  85. if (p->file_priv == NULL && start + size <= p->start + p->size)
  86. return split_block(p, start, size, file_priv);
  87. }
  88. return NULL;
  89. }
  90. static struct mem_block *find_block(struct mem_block *heap, int start)
  91. {
  92. struct mem_block *p;
  93. list_for_each(p, heap)
  94. if (p->start == start)
  95. return p;
  96. return NULL;
  97. }
  98. static void free_block(struct mem_block *p)
  99. {
  100. p->file_priv = NULL;
  101. /* Assumes a single contiguous range. Needs a special file_priv in
  102. * 'heap' to stop it being subsumed.
  103. */
  104. if (p->next->file_priv == NULL) {
  105. struct mem_block *q = p->next;
  106. p->size += q->size;
  107. p->next = q->next;
  108. p->next->prev = p;
  109. kfree(q);
  110. }
  111. if (p->prev->file_priv == NULL) {
  112. struct mem_block *q = p->prev;
  113. q->size += p->size;
  114. q->next = p->next;
  115. q->next->prev = q;
  116. kfree(p);
  117. }
  118. }
  119. /* Initialize. How to check for an uninitialized heap?
  120. */
  121. static int init_heap(struct mem_block **heap, int start, int size)
  122. {
  123. struct mem_block *blocks = kmalloc(sizeof(*blocks), GFP_KERNEL);
  124. if (!blocks)
  125. return -ENOMEM;
  126. *heap = kzalloc(sizeof(**heap), GFP_KERNEL);
  127. if (!*heap) {
  128. kfree(blocks);
  129. return -ENOMEM;
  130. }
  131. blocks->start = start;
  132. blocks->size = size;
  133. blocks->file_priv = NULL;
  134. blocks->next = blocks->prev = *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 (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. }