via_mm.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Copyright 2006 Tungsten Graphics Inc., Bismarck, ND., USA.
  3. * All rights reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sub license,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the
  13. * next paragraph) shall be included in all copies or substantial portions
  14. * of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
  20. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. * DEALINGS IN THE SOFTWARE.
  23. */
  24. /*
  25. * Authors: Thomas Hellström <thomas-at-tungstengraphics-dot-com>
  26. */
  27. #include "drmP.h"
  28. #include "via_drm.h"
  29. #include "via_drv.h"
  30. #define VIA_MM_ALIGN_SHIFT 4
  31. #define VIA_MM_ALIGN_MASK ((1 << VIA_MM_ALIGN_SHIFT) - 1)
  32. struct via_memblock {
  33. struct drm_mm_node mm_node;
  34. struct list_head owner_list;
  35. };
  36. int via_agp_init(struct drm_device *dev, void *data, struct drm_file *file_priv)
  37. {
  38. drm_via_agp_t *agp = data;
  39. drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
  40. mutex_lock(&dev->struct_mutex);
  41. drm_mm_init(&dev_priv->agp_mm, 0, agp->size >> VIA_MM_ALIGN_SHIFT);
  42. dev_priv->agp_initialized = 1;
  43. dev_priv->agp_offset = agp->offset;
  44. mutex_unlock(&dev->struct_mutex);
  45. DRM_DEBUG("offset = %u, size = %u\n", agp->offset, agp->size);
  46. return 0;
  47. }
  48. int via_fb_init(struct drm_device *dev, void *data, struct drm_file *file_priv)
  49. {
  50. drm_via_fb_t *fb = data;
  51. drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
  52. mutex_lock(&dev->struct_mutex);
  53. drm_mm_init(&dev_priv->vram_mm, 0, fb->size >> VIA_MM_ALIGN_SHIFT);
  54. dev_priv->vram_initialized = 1;
  55. dev_priv->vram_offset = fb->offset;
  56. mutex_unlock(&dev->struct_mutex);
  57. DRM_DEBUG("offset = %u, size = %u\n", fb->offset, fb->size);
  58. return 0;
  59. }
  60. int via_final_context(struct drm_device *dev, int context)
  61. {
  62. drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
  63. via_release_futex(dev_priv, context);
  64. /* Linux specific until context tracking code gets ported to BSD */
  65. /* Last context, perform cleanup */
  66. if (dev->ctx_count == 1 && dev->dev_private) {
  67. DRM_DEBUG("Last Context\n");
  68. drm_irq_uninstall(dev);
  69. via_cleanup_futex(dev_priv);
  70. via_do_cleanup_map(dev);
  71. }
  72. return 1;
  73. }
  74. void via_lastclose(struct drm_device *dev)
  75. {
  76. drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
  77. if (!dev_priv)
  78. return;
  79. mutex_lock(&dev->struct_mutex);
  80. if (dev_priv->vram_initialized) {
  81. drm_mm_takedown(&dev_priv->vram_mm);
  82. dev_priv->vram_initialized = 0;
  83. }
  84. if (dev_priv->agp_initialized) {
  85. drm_mm_takedown(&dev_priv->agp_mm);
  86. dev_priv->agp_initialized = 0;
  87. }
  88. mutex_unlock(&dev->struct_mutex);
  89. }
  90. int via_mem_alloc(struct drm_device *dev, void *data,
  91. struct drm_file *file)
  92. {
  93. drm_via_mem_t *mem = data;
  94. int retval = 0, user_key;
  95. struct via_memblock *item;
  96. drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
  97. struct via_file_private *file_priv = file->driver_priv;
  98. unsigned long tmpSize;
  99. if (mem->type > VIA_MEM_AGP) {
  100. DRM_ERROR("Unknown memory type allocation\n");
  101. return -EINVAL;
  102. }
  103. mutex_lock(&dev->struct_mutex);
  104. if (0 == ((mem->type == VIA_MEM_VIDEO) ? dev_priv->vram_initialized :
  105. dev_priv->agp_initialized)) {
  106. DRM_ERROR
  107. ("Attempt to allocate from uninitialized memory manager.\n");
  108. mutex_unlock(&dev->struct_mutex);
  109. return -EINVAL;
  110. }
  111. item = kzalloc(sizeof(*item), GFP_KERNEL);
  112. if (!item) {
  113. retval = -ENOMEM;
  114. goto fail_alloc;
  115. }
  116. tmpSize = (mem->size + VIA_MM_ALIGN_MASK) >> VIA_MM_ALIGN_SHIFT;
  117. if (mem->type == VIA_MEM_AGP)
  118. retval = drm_mm_insert_node(&dev_priv->agp_mm,
  119. &item->mm_node,
  120. tmpSize, 0);
  121. else
  122. retval = drm_mm_insert_node(&dev_priv->vram_mm,
  123. &item->mm_node,
  124. tmpSize, 0);
  125. if (retval)
  126. goto fail_alloc;
  127. again:
  128. if (idr_pre_get(&dev_priv->object_idr, GFP_KERNEL) == 0) {
  129. retval = -ENOMEM;
  130. goto fail_idr;
  131. }
  132. retval = idr_get_new_above(&dev_priv->object_idr, item, 1, &user_key);
  133. if (retval == -EAGAIN)
  134. goto again;
  135. if (retval)
  136. goto fail_idr;
  137. list_add(&item->owner_list, &file_priv->obj_list);
  138. mutex_unlock(&dev->struct_mutex);
  139. mem->offset = ((mem->type == VIA_MEM_VIDEO) ?
  140. dev_priv->vram_offset : dev_priv->agp_offset) +
  141. ((item->mm_node.start) << VIA_MM_ALIGN_SHIFT);
  142. mem->index = user_key;
  143. return 0;
  144. fail_idr:
  145. drm_mm_remove_node(&item->mm_node);
  146. fail_alloc:
  147. kfree(item);
  148. mutex_unlock(&dev->struct_mutex);
  149. mem->offset = 0;
  150. mem->size = 0;
  151. mem->index = 0;
  152. DRM_DEBUG("Video memory allocation failed\n");
  153. return retval;
  154. }
  155. int via_mem_free(struct drm_device *dev, void *data, struct drm_file *file_priv)
  156. {
  157. drm_via_private_t *dev_priv = dev->dev_private;
  158. drm_via_mem_t *mem = data;
  159. struct via_memblock *obj;
  160. mutex_lock(&dev->struct_mutex);
  161. obj = idr_find(&dev_priv->object_idr, mem->index);
  162. if (obj == NULL) {
  163. mutex_unlock(&dev->struct_mutex);
  164. return -EINVAL;
  165. }
  166. idr_remove(&dev_priv->object_idr, mem->index);
  167. list_del(&obj->owner_list);
  168. drm_mm_remove_node(&obj->mm_node);
  169. kfree(obj);
  170. mutex_unlock(&dev->struct_mutex);
  171. DRM_DEBUG("free = 0x%lx\n", mem->index);
  172. return 0;
  173. }
  174. void via_reclaim_buffers_locked(struct drm_device *dev,
  175. struct drm_file *file)
  176. {
  177. struct via_file_private *file_priv = file->driver_priv;
  178. struct via_memblock *entry, *next;
  179. mutex_lock(&dev->struct_mutex);
  180. if (list_empty(&file_priv->obj_list)) {
  181. mutex_unlock(&dev->struct_mutex);
  182. return;
  183. }
  184. if (dev->driver->dma_quiescent)
  185. dev->driver->dma_quiescent(dev);
  186. list_for_each_entry_safe(entry, next, &file_priv->obj_list,
  187. owner_list) {
  188. list_del(&entry->owner_list);
  189. drm_mm_remove_node(&entry->mm_node);
  190. kfree(entry);
  191. }
  192. mutex_unlock(&dev->struct_mutex);
  193. return;
  194. }