i915_mem.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 =
  88. drm_alloc(sizeof(*newblock), DRM_MEM_BUFLISTS);
  89. if (!newblock)
  90. goto out;
  91. newblock->start = start;
  92. newblock->size = p->size - (start - p->start);
  93. newblock->filp = NULL;
  94. newblock->next = p->next;
  95. newblock->prev = p;
  96. p->next->prev = newblock;
  97. p->next = newblock;
  98. p->size -= newblock->size;
  99. p = newblock;
  100. }
  101. /* Maybe cut off the end of an existing block */
  102. if (size < p->size) {
  103. struct mem_block *newblock =
  104. drm_alloc(sizeof(*newblock), DRM_MEM_BUFLISTS);
  105. if (!newblock)
  106. goto out;
  107. newblock->start = start + size;
  108. newblock->size = p->size - size;
  109. newblock->filp = NULL;
  110. newblock->next = p->next;
  111. newblock->prev = p;
  112. p->next->prev = newblock;
  113. p->next = newblock;
  114. p->size = size;
  115. }
  116. out:
  117. /* Our block is in the middle */
  118. p->filp = filp;
  119. return p;
  120. }
  121. static struct mem_block *alloc_block(struct mem_block *heap, int size,
  122. int align2, DRMFILE filp)
  123. {
  124. struct mem_block *p;
  125. int mask = (1 << align2) - 1;
  126. for (p = heap->next; p != heap; p = p->next) {
  127. int start = (p->start + mask) & ~mask;
  128. if (p->filp == NULL && start + size <= p->start + p->size)
  129. return split_block(p, start, size, filp);
  130. }
  131. return NULL;
  132. }
  133. static struct mem_block *find_block(struct mem_block *heap, int start)
  134. {
  135. struct mem_block *p;
  136. for (p = heap->next; p != heap; p = p->next)
  137. if (p->start == start)
  138. return p;
  139. return NULL;
  140. }
  141. static void free_block(struct mem_block *p)
  142. {
  143. p->filp = NULL;
  144. /* Assumes a single contiguous range. Needs a special filp in
  145. * 'heap' to stop it being subsumed.
  146. */
  147. if (p->next->filp == NULL) {
  148. struct mem_block *q = p->next;
  149. p->size += q->size;
  150. p->next = q->next;
  151. p->next->prev = p;
  152. drm_free(q, sizeof(*q), DRM_MEM_BUFLISTS);
  153. }
  154. if (p->prev->filp == NULL) {
  155. struct mem_block *q = p->prev;
  156. q->size += p->size;
  157. q->next = p->next;
  158. q->next->prev = q;
  159. drm_free(p, sizeof(*q), DRM_MEM_BUFLISTS);
  160. }
  161. }
  162. /* Initialize. How to check for an uninitialized heap?
  163. */
  164. static int init_heap(struct mem_block **heap, int start, int size)
  165. {
  166. struct mem_block *blocks = drm_alloc(sizeof(*blocks), DRM_MEM_BUFLISTS);
  167. if (!blocks)
  168. return -ENOMEM;
  169. *heap = drm_alloc(sizeof(**heap), DRM_MEM_BUFLISTS);
  170. if (!*heap) {
  171. drm_free(blocks, sizeof(*blocks), DRM_MEM_BUFLISTS);
  172. return -ENOMEM;
  173. }
  174. blocks->start = start;
  175. blocks->size = size;
  176. blocks->filp = NULL;
  177. blocks->next = blocks->prev = *heap;
  178. memset(*heap, 0, sizeof(**heap));
  179. (*heap)->filp = (DRMFILE) - 1;
  180. (*heap)->next = (*heap)->prev = blocks;
  181. return 0;
  182. }
  183. /* Free all blocks associated with the releasing file.
  184. */
  185. void i915_mem_release(drm_device_t * dev, DRMFILE filp, struct mem_block *heap)
  186. {
  187. struct mem_block *p;
  188. if (!heap || !heap->next)
  189. return;
  190. for (p = heap->next; p != heap; p = p->next) {
  191. if (p->filp == filp) {
  192. p->filp = NULL;
  193. mark_block(dev, p, 0);
  194. }
  195. }
  196. /* Assumes a single contiguous range. Needs a special filp in
  197. * 'heap' to stop it being subsumed.
  198. */
  199. for (p = heap->next; p != heap; p = p->next) {
  200. while (p->filp == NULL && p->next->filp == NULL) {
  201. struct mem_block *q = p->next;
  202. p->size += q->size;
  203. p->next = q->next;
  204. p->next->prev = p;
  205. drm_free(q, sizeof(*q), DRM_MEM_BUFLISTS);
  206. }
  207. }
  208. }
  209. /* Shutdown.
  210. */
  211. void i915_mem_takedown(struct mem_block **heap)
  212. {
  213. struct mem_block *p;
  214. if (!*heap)
  215. return;
  216. for (p = (*heap)->next; p != *heap;) {
  217. struct mem_block *q = p;
  218. p = p->next;
  219. drm_free(q, sizeof(*q), DRM_MEM_BUFLISTS);
  220. }
  221. drm_free(*heap, sizeof(**heap), DRM_MEM_BUFLISTS);
  222. *heap = NULL;
  223. }
  224. static struct mem_block **get_heap(drm_i915_private_t * dev_priv, int region)
  225. {
  226. switch (region) {
  227. case I915_MEM_REGION_AGP:
  228. return &dev_priv->agp_heap;
  229. default:
  230. return NULL;
  231. }
  232. }
  233. /* IOCTL HANDLERS */
  234. int i915_mem_alloc(DRM_IOCTL_ARGS)
  235. {
  236. DRM_DEVICE;
  237. drm_i915_private_t *dev_priv = dev->dev_private;
  238. drm_i915_mem_alloc_t alloc;
  239. struct mem_block *block, **heap;
  240. if (!dev_priv) {
  241. DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
  242. return DRM_ERR(EINVAL);
  243. }
  244. DRM_COPY_FROM_USER_IOCTL(alloc, (drm_i915_mem_alloc_t __user *) data,
  245. sizeof(alloc));
  246. heap = get_heap(dev_priv, alloc.region);
  247. if (!heap || !*heap)
  248. return DRM_ERR(EFAULT);
  249. /* Make things easier on ourselves: all allocations at least
  250. * 4k aligned.
  251. */
  252. if (alloc.alignment < 12)
  253. alloc.alignment = 12;
  254. block = alloc_block(*heap, alloc.size, alloc.alignment, filp);
  255. if (!block)
  256. return DRM_ERR(ENOMEM);
  257. mark_block(dev, block, 1);
  258. if (DRM_COPY_TO_USER(alloc.region_offset, &block->start, sizeof(int))) {
  259. DRM_ERROR("copy_to_user\n");
  260. return DRM_ERR(EFAULT);
  261. }
  262. return 0;
  263. }
  264. int i915_mem_free(DRM_IOCTL_ARGS)
  265. {
  266. DRM_DEVICE;
  267. drm_i915_private_t *dev_priv = dev->dev_private;
  268. drm_i915_mem_free_t memfree;
  269. struct mem_block *block, **heap;
  270. if (!dev_priv) {
  271. DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
  272. return DRM_ERR(EINVAL);
  273. }
  274. DRM_COPY_FROM_USER_IOCTL(memfree, (drm_i915_mem_free_t __user *) data,
  275. sizeof(memfree));
  276. heap = get_heap(dev_priv, memfree.region);
  277. if (!heap || !*heap)
  278. return DRM_ERR(EFAULT);
  279. block = find_block(*heap, memfree.region_offset);
  280. if (!block)
  281. return DRM_ERR(EFAULT);
  282. if (block->filp != filp)
  283. return DRM_ERR(EPERM);
  284. mark_block(dev, block, 0);
  285. free_block(block);
  286. return 0;
  287. }
  288. int i915_mem_init_heap(DRM_IOCTL_ARGS)
  289. {
  290. DRM_DEVICE;
  291. drm_i915_private_t *dev_priv = dev->dev_private;
  292. drm_i915_mem_init_heap_t initheap;
  293. struct mem_block **heap;
  294. if (!dev_priv) {
  295. DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
  296. return DRM_ERR(EINVAL);
  297. }
  298. DRM_COPY_FROM_USER_IOCTL(initheap,
  299. (drm_i915_mem_init_heap_t __user *) data,
  300. sizeof(initheap));
  301. heap = get_heap(dev_priv, initheap.region);
  302. if (!heap)
  303. return DRM_ERR(EFAULT);
  304. if (*heap) {
  305. DRM_ERROR("heap already initialized?");
  306. return DRM_ERR(EFAULT);
  307. }
  308. return init_heap(heap, initheap.start, initheap.size);
  309. }