i915_mem.c 9.4 KB

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