i915_mem.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /* i915_mem.c -- Simple agp/fb memory manager for i915 -*- linux-c -*-
  2. */
  3. /**************************************************************************
  4. *
  5. * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
  6. * All Rights Reserved.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a
  9. * copy of this software and associated documentation files (the
  10. * "Software"), to deal in the Software without restriction, including
  11. * without limitation the rights to use, copy, modify, merge, publish,
  12. * distribute, sub license, and/or sell copies of the Software, and to
  13. * permit persons to whom the Software is furnished to do so, subject to
  14. * the following conditions:
  15. *
  16. * The above copyright notice and this permission notice (including the
  17. * next paragraph) shall be included in all copies or substantial portions
  18. * of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  21. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
  23. * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
  24. * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  25. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  26. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. *
  28. **************************************************************************/
  29. #include "drmP.h"
  30. #include "drm.h"
  31. #include "i915_drm.h"
  32. #include "i915_drv.h"
  33. /* This memory manager is integrated into the global/local lru
  34. * mechanisms used by the clients. Specifically, it operates by
  35. * setting the 'in_use' fields of the global LRU to indicate whether
  36. * this region is privately allocated to a client.
  37. *
  38. * This does require the client to actually respect that field.
  39. *
  40. * Currently no effort is made to allocate 'private' memory in any
  41. * clever way - the LRU information isn't used to determine which
  42. * block to allocate, and the ring is drained prior to allocations --
  43. * in other words allocation is expensive.
  44. */
  45. static void mark_block(drm_device_t * dev, struct mem_block *p, int in_use)
  46. {
  47. drm_i915_private_t *dev_priv = dev->dev_private;
  48. drm_i915_sarea_t *sarea_priv = dev_priv->sarea_priv;
  49. drm_tex_region_t *list;
  50. unsigned shift, nr;
  51. unsigned start;
  52. unsigned end;
  53. unsigned i;
  54. int age;
  55. shift = dev_priv->tex_lru_log_granularity;
  56. nr = I915_NR_TEX_REGIONS;
  57. start = p->start >> shift;
  58. end = (p->start + p->size - 1) >> shift;
  59. age = ++sarea_priv->texAge;
  60. list = sarea_priv->texList;
  61. /* Mark the regions with the new flag and update their age. Move
  62. * them to head of list to preserve LRU semantics.
  63. */
  64. for (i = start; i <= end; i++) {
  65. list[i].in_use = in_use;
  66. list[i].age = age;
  67. /* remove_from_list(i)
  68. */
  69. list[(unsigned)list[i].next].prev = list[i].prev;
  70. list[(unsigned)list[i].prev].next = list[i].next;
  71. /* insert_at_head(list, i)
  72. */
  73. list[i].prev = nr;
  74. list[i].next = list[nr].next;
  75. list[(unsigned)list[nr].next].prev = i;
  76. list[nr].next = i;
  77. }
  78. }
  79. /* Very simple allocator for agp memory, working on a static range
  80. * already mapped into each client's address space.
  81. */
  82. static struct mem_block *split_block(struct mem_block *p, int start, int size,
  83. DRMFILE filp)
  84. {
  85. /* Maybe cut off the start of an existing block */
  86. if (start > p->start) {
  87. struct mem_block *newblock = drm_alloc(sizeof(*newblock), DRM_MEM_BUFLISTS);
  88. if (!newblock)
  89. goto out;
  90. newblock->start = start;
  91. newblock->size = p->size - (start - p->start);
  92. newblock->filp = NULL;
  93. newblock->next = p->next;
  94. newblock->prev = p;
  95. p->next->prev = newblock;
  96. p->next = newblock;
  97. p->size -= newblock->size;
  98. p = newblock;
  99. }
  100. /* Maybe cut off the end of an existing block */
  101. if (size < p->size) {
  102. struct mem_block *newblock = drm_alloc(sizeof(*newblock), DRM_MEM_BUFLISTS);
  103. if (!newblock)
  104. goto out;
  105. newblock->start = start + size;
  106. newblock->size = p->size - size;
  107. newblock->filp = NULL;
  108. newblock->next = p->next;
  109. newblock->prev = p;
  110. p->next->prev = newblock;
  111. p->next = newblock;
  112. p->size = size;
  113. }
  114. out:
  115. /* Our block is in the middle */
  116. p->filp = filp;
  117. return p;
  118. }
  119. static struct mem_block *alloc_block(struct mem_block *heap, int size,
  120. int align2, DRMFILE filp)
  121. {
  122. struct mem_block *p;
  123. int mask = (1 << align2) - 1;
  124. for (p = heap->next; p != heap; p = p->next) {
  125. int start = (p->start + mask) & ~mask;
  126. if (p->filp == NULL && start + size <= p->start + p->size)
  127. return split_block(p, start, size, filp);
  128. }
  129. return NULL;
  130. }
  131. static struct mem_block *find_block(struct mem_block *heap, int start)
  132. {
  133. struct mem_block *p;
  134. for (p = heap->next; p != heap; p = p->next)
  135. if (p->start == start)
  136. return p;
  137. return NULL;
  138. }
  139. static void free_block(struct mem_block *p)
  140. {
  141. p->filp = NULL;
  142. /* Assumes a single contiguous range. Needs a special filp in
  143. * 'heap' to stop it being subsumed.
  144. */
  145. if (p->next->filp == NULL) {
  146. struct mem_block *q = p->next;
  147. p->size += q->size;
  148. p->next = q->next;
  149. p->next->prev = p;
  150. drm_free(q, sizeof(*q), DRM_MEM_BUFLISTS);
  151. }
  152. if (p->prev->filp == NULL) {
  153. struct mem_block *q = p->prev;
  154. q->size += p->size;
  155. q->next = p->next;
  156. q->next->prev = q;
  157. drm_free(p, sizeof(*q), DRM_MEM_BUFLISTS);
  158. }
  159. }
  160. /* Initialize. How to check for an uninitialized heap?
  161. */
  162. static int init_heap(struct mem_block **heap, int start, int size)
  163. {
  164. struct mem_block *blocks = drm_alloc(sizeof(*blocks), DRM_MEM_BUFLISTS);
  165. if (!blocks)
  166. return -ENOMEM;
  167. *heap = drm_alloc(sizeof(**heap), DRM_MEM_BUFLISTS);
  168. if (!*heap) {
  169. drm_free(blocks, sizeof(*blocks), DRM_MEM_BUFLISTS);
  170. return -ENOMEM;
  171. }
  172. blocks->start = start;
  173. blocks->size = size;
  174. blocks->filp = NULL;
  175. blocks->next = blocks->prev = *heap;
  176. memset(*heap, 0, sizeof(**heap));
  177. (*heap)->filp = (DRMFILE) - 1;
  178. (*heap)->next = (*heap)->prev = blocks;
  179. return 0;
  180. }
  181. /* Free all blocks associated with the releasing file.
  182. */
  183. void i915_mem_release(drm_device_t * dev, DRMFILE filp, struct mem_block *heap)
  184. {
  185. struct mem_block *p;
  186. if (!heap || !heap->next)
  187. return;
  188. for (p = heap->next; p != heap; p = p->next) {
  189. if (p->filp == filp) {
  190. p->filp = NULL;
  191. mark_block(dev, p, 0);
  192. }
  193. }
  194. /* Assumes a single contiguous range. Needs a special filp in
  195. * 'heap' to stop it being subsumed.
  196. */
  197. for (p = heap->next; p != heap; p = p->next) {
  198. while (p->filp == NULL && p->next->filp == NULL) {
  199. struct mem_block *q = p->next;
  200. p->size += q->size;
  201. p->next = q->next;
  202. p->next->prev = p;
  203. drm_free(q, sizeof(*q), DRM_MEM_BUFLISTS);
  204. }
  205. }
  206. }
  207. /* Shutdown.
  208. */
  209. void i915_mem_takedown(struct mem_block **heap)
  210. {
  211. struct mem_block *p;
  212. if (!*heap)
  213. return;
  214. for (p = (*heap)->next; p != *heap;) {
  215. struct mem_block *q = p;
  216. p = p->next;
  217. drm_free(q, sizeof(*q), DRM_MEM_BUFLISTS);
  218. }
  219. drm_free(*heap, sizeof(**heap), DRM_MEM_BUFLISTS);
  220. *heap = NULL;
  221. }
  222. static struct mem_block **get_heap(drm_i915_private_t * dev_priv, int region)
  223. {
  224. switch (region) {
  225. case I915_MEM_REGION_AGP:
  226. return &dev_priv->agp_heap;
  227. default:
  228. return NULL;
  229. }
  230. }
  231. /* IOCTL HANDLERS */
  232. int i915_mem_alloc(DRM_IOCTL_ARGS)
  233. {
  234. DRM_DEVICE;
  235. drm_i915_private_t *dev_priv = dev->dev_private;
  236. drm_i915_mem_alloc_t alloc;
  237. struct mem_block *block, **heap;
  238. if (!dev_priv) {
  239. DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
  240. return DRM_ERR(EINVAL);
  241. }
  242. DRM_COPY_FROM_USER_IOCTL(alloc, (drm_i915_mem_alloc_t __user *) data,
  243. sizeof(alloc));
  244. heap = get_heap(dev_priv, alloc.region);
  245. if (!heap || !*heap)
  246. return DRM_ERR(EFAULT);
  247. /* Make things easier on ourselves: all allocations at least
  248. * 4k aligned.
  249. */
  250. if (alloc.alignment < 12)
  251. alloc.alignment = 12;
  252. block = alloc_block(*heap, alloc.size, alloc.alignment, filp);
  253. if (!block)
  254. return DRM_ERR(ENOMEM);
  255. mark_block(dev, block, 1);
  256. if (DRM_COPY_TO_USER(alloc.region_offset, &block->start, sizeof(int))) {
  257. DRM_ERROR("copy_to_user\n");
  258. return DRM_ERR(EFAULT);
  259. }
  260. return 0;
  261. }
  262. int i915_mem_free(DRM_IOCTL_ARGS)
  263. {
  264. DRM_DEVICE;
  265. drm_i915_private_t *dev_priv = dev->dev_private;
  266. drm_i915_mem_free_t memfree;
  267. struct mem_block *block, **heap;
  268. if (!dev_priv) {
  269. DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
  270. return DRM_ERR(EINVAL);
  271. }
  272. DRM_COPY_FROM_USER_IOCTL(memfree, (drm_i915_mem_free_t __user *) data,
  273. sizeof(memfree));
  274. heap = get_heap(dev_priv, memfree.region);
  275. if (!heap || !*heap)
  276. return DRM_ERR(EFAULT);
  277. block = find_block(*heap, memfree.region_offset);
  278. if (!block)
  279. return DRM_ERR(EFAULT);
  280. if (block->filp != filp)
  281. return DRM_ERR(EPERM);
  282. mark_block(dev, block, 0);
  283. free_block(block);
  284. return 0;
  285. }
  286. int i915_mem_init_heap(DRM_IOCTL_ARGS)
  287. {
  288. DRM_DEVICE;
  289. drm_i915_private_t *dev_priv = dev->dev_private;
  290. drm_i915_mem_init_heap_t initheap;
  291. struct mem_block **heap;
  292. if (!dev_priv) {
  293. DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
  294. return DRM_ERR(EINVAL);
  295. }
  296. DRM_COPY_FROM_USER_IOCTL(initheap,
  297. (drm_i915_mem_init_heap_t __user *) data,
  298. sizeof(initheap));
  299. heap = get_heap(dev_priv, initheap.region);
  300. if (!heap)
  301. return DRM_ERR(EFAULT);
  302. if (*heap) {
  303. DRM_ERROR("heap already initialized?");
  304. return DRM_ERR(EFAULT);
  305. }
  306. return init_heap(heap, initheap.start, initheap.size);
  307. }