i915_mem.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. struct drm_i915_master_private *master_priv = dev->primary->master->driver_priv;
  48. drm_i915_sarea_t *sarea_priv = master_priv->sarea_priv;
  49. struct drm_tex_region *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. struct drm_file *file_priv)
  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->file_priv = 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->file_priv = 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->file_priv = file_priv;
  119. return p;
  120. }
  121. static struct mem_block *alloc_block(struct mem_block *heap, int size,
  122. int align2, struct drm_file *file_priv)
  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->file_priv == NULL && start + size <= p->start + p->size)
  129. return split_block(p, start, size, file_priv);
  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->file_priv = NULL;
  144. /* Assumes a single contiguous range. Needs a special file_priv in
  145. * 'heap' to stop it being subsumed.
  146. */
  147. if (p->next->file_priv == 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->file_priv == 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->file_priv = NULL;
  177. blocks->next = blocks->prev = *heap;
  178. memset(*heap, 0, sizeof(**heap));
  179. (*heap)->file_priv = (struct drm_file *) - 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(struct drm_device * dev, struct drm_file *file_priv,
  186. struct mem_block *heap)
  187. {
  188. struct mem_block *p;
  189. if (!heap || !heap->next)
  190. return;
  191. for (p = heap->next; p != heap; p = p->next) {
  192. if (p->file_priv == file_priv) {
  193. p->file_priv = NULL;
  194. mark_block(dev, p, 0);
  195. }
  196. }
  197. /* Assumes a single contiguous range. Needs a special file_priv in
  198. * 'heap' to stop it being subsumed.
  199. */
  200. for (p = heap->next; p != heap; p = p->next) {
  201. while (p->file_priv == NULL && p->next->file_priv == NULL) {
  202. struct mem_block *q = p->next;
  203. p->size += q->size;
  204. p->next = q->next;
  205. p->next->prev = p;
  206. drm_free(q, sizeof(*q), DRM_MEM_BUFLISTS);
  207. }
  208. }
  209. }
  210. /* Shutdown.
  211. */
  212. void i915_mem_takedown(struct mem_block **heap)
  213. {
  214. struct mem_block *p;
  215. if (!*heap)
  216. return;
  217. for (p = (*heap)->next; p != *heap;) {
  218. struct mem_block *q = p;
  219. p = p->next;
  220. drm_free(q, sizeof(*q), DRM_MEM_BUFLISTS);
  221. }
  222. drm_free(*heap, sizeof(**heap), DRM_MEM_BUFLISTS);
  223. *heap = NULL;
  224. }
  225. static struct mem_block **get_heap(drm_i915_private_t * dev_priv, int region)
  226. {
  227. switch (region) {
  228. case I915_MEM_REGION_AGP:
  229. return &dev_priv->agp_heap;
  230. default:
  231. return NULL;
  232. }
  233. }
  234. /* IOCTL HANDLERS */
  235. int i915_mem_alloc(struct drm_device *dev, void *data,
  236. struct drm_file *file_priv)
  237. {
  238. drm_i915_private_t *dev_priv = dev->dev_private;
  239. drm_i915_mem_alloc_t *alloc = data;
  240. struct mem_block *block, **heap;
  241. if (!dev_priv) {
  242. DRM_ERROR("called with no initialization\n");
  243. return -EINVAL;
  244. }
  245. heap = get_heap(dev_priv, alloc->region);
  246. if (!heap || !*heap)
  247. return -EFAULT;
  248. /* Make things easier on ourselves: all allocations at least
  249. * 4k aligned.
  250. */
  251. if (alloc->alignment < 12)
  252. alloc->alignment = 12;
  253. block = alloc_block(*heap, alloc->size, alloc->alignment, file_priv);
  254. if (!block)
  255. return -ENOMEM;
  256. mark_block(dev, block, 1);
  257. if (DRM_COPY_TO_USER(alloc->region_offset, &block->start,
  258. sizeof(int))) {
  259. DRM_ERROR("copy_to_user\n");
  260. return -EFAULT;
  261. }
  262. return 0;
  263. }
  264. int i915_mem_free(struct drm_device *dev, void *data,
  265. struct drm_file *file_priv)
  266. {
  267. drm_i915_private_t *dev_priv = dev->dev_private;
  268. drm_i915_mem_free_t *memfree = data;
  269. struct mem_block *block, **heap;
  270. if (!dev_priv) {
  271. DRM_ERROR("called with no initialization\n");
  272. return -EINVAL;
  273. }
  274. heap = get_heap(dev_priv, memfree->region);
  275. if (!heap || !*heap)
  276. return -EFAULT;
  277. block = find_block(*heap, memfree->region_offset);
  278. if (!block)
  279. return -EFAULT;
  280. if (block->file_priv != file_priv)
  281. return -EPERM;
  282. mark_block(dev, block, 0);
  283. free_block(block);
  284. return 0;
  285. }
  286. int i915_mem_init_heap(struct drm_device *dev, void *data,
  287. struct drm_file *file_priv)
  288. {
  289. drm_i915_private_t *dev_priv = dev->dev_private;
  290. drm_i915_mem_init_heap_t *initheap = data;
  291. struct mem_block **heap;
  292. if (!dev_priv) {
  293. DRM_ERROR("called with no initialization\n");
  294. return -EINVAL;
  295. }
  296. heap = get_heap(dev_priv, initheap->region);
  297. if (!heap)
  298. return -EFAULT;
  299. if (*heap) {
  300. DRM_ERROR("heap already initialized?");
  301. return -EFAULT;
  302. }
  303. return init_heap(heap, initheap->start, initheap->size);
  304. }
  305. int i915_mem_destroy_heap( struct drm_device *dev, void *data,
  306. struct drm_file *file_priv )
  307. {
  308. drm_i915_private_t *dev_priv = dev->dev_private;
  309. drm_i915_mem_destroy_heap_t *destroyheap = data;
  310. struct mem_block **heap;
  311. if ( !dev_priv ) {
  312. DRM_ERROR( "called with no initialization\n" );
  313. return -EINVAL;
  314. }
  315. heap = get_heap( dev_priv, destroyheap->region );
  316. if (!heap) {
  317. DRM_ERROR("get_heap failed");
  318. return -EFAULT;
  319. }
  320. if (!*heap) {
  321. DRM_ERROR("heap not initialized?");
  322. return -EFAULT;
  323. }
  324. i915_mem_takedown( heap );
  325. return 0;
  326. }