via_mm.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 <drm/drmP.h>
  28. #include <drm/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 (list_is_singular(&dev->ctxlist)) {
  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, DRM_MM_SEARCH_DEFAULT);
  121. else
  122. retval = drm_mm_insert_node(&dev_priv->vram_mm,
  123. &item->mm_node,
  124. tmpSize, 0, DRM_MM_SEARCH_DEFAULT);
  125. if (retval)
  126. goto fail_alloc;
  127. retval = idr_alloc(&dev_priv->object_idr, item, 1, 0, GFP_KERNEL);
  128. if (retval < 0)
  129. goto fail_idr;
  130. user_key = retval;
  131. list_add(&item->owner_list, &file_priv->obj_list);
  132. mutex_unlock(&dev->struct_mutex);
  133. mem->offset = ((mem->type == VIA_MEM_VIDEO) ?
  134. dev_priv->vram_offset : dev_priv->agp_offset) +
  135. ((item->mm_node.start) << VIA_MM_ALIGN_SHIFT);
  136. mem->index = user_key;
  137. return 0;
  138. fail_idr:
  139. drm_mm_remove_node(&item->mm_node);
  140. fail_alloc:
  141. kfree(item);
  142. mutex_unlock(&dev->struct_mutex);
  143. mem->offset = 0;
  144. mem->size = 0;
  145. mem->index = 0;
  146. DRM_DEBUG("Video memory allocation failed\n");
  147. return retval;
  148. }
  149. int via_mem_free(struct drm_device *dev, void *data, struct drm_file *file_priv)
  150. {
  151. drm_via_private_t *dev_priv = dev->dev_private;
  152. drm_via_mem_t *mem = data;
  153. struct via_memblock *obj;
  154. mutex_lock(&dev->struct_mutex);
  155. obj = idr_find(&dev_priv->object_idr, mem->index);
  156. if (obj == NULL) {
  157. mutex_unlock(&dev->struct_mutex);
  158. return -EINVAL;
  159. }
  160. idr_remove(&dev_priv->object_idr, mem->index);
  161. list_del(&obj->owner_list);
  162. drm_mm_remove_node(&obj->mm_node);
  163. kfree(obj);
  164. mutex_unlock(&dev->struct_mutex);
  165. DRM_DEBUG("free = 0x%lx\n", mem->index);
  166. return 0;
  167. }
  168. void via_reclaim_buffers_locked(struct drm_device *dev,
  169. struct drm_file *file)
  170. {
  171. struct via_file_private *file_priv = file->driver_priv;
  172. struct via_memblock *entry, *next;
  173. if (!(file->minor->master && file->master->lock.hw_lock))
  174. return;
  175. drm_legacy_idlelock_take(&file->master->lock);
  176. mutex_lock(&dev->struct_mutex);
  177. if (list_empty(&file_priv->obj_list)) {
  178. mutex_unlock(&dev->struct_mutex);
  179. drm_legacy_idlelock_release(&file->master->lock);
  180. return;
  181. }
  182. via_driver_dma_quiescent(dev);
  183. list_for_each_entry_safe(entry, next, &file_priv->obj_list,
  184. owner_list) {
  185. list_del(&entry->owner_list);
  186. drm_mm_remove_node(&entry->mm_node);
  187. kfree(entry);
  188. }
  189. mutex_unlock(&dev->struct_mutex);
  190. drm_legacy_idlelock_release(&file->master->lock);
  191. return;
  192. }