via_mm.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. #include "drm_sman.h"
  31. #define VIA_MM_ALIGN_SHIFT 4
  32. #define VIA_MM_ALIGN_MASK ( (1 << VIA_MM_ALIGN_SHIFT) - 1)
  33. int via_agp_init(struct drm_device *dev, void *data, struct drm_file *file_priv)
  34. {
  35. drm_via_agp_t *agp = data;
  36. drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
  37. int ret;
  38. mutex_lock(&dev->struct_mutex);
  39. ret = drm_sman_set_range(&dev_priv->sman, VIA_MEM_AGP, 0,
  40. agp->size >> VIA_MM_ALIGN_SHIFT);
  41. if (ret) {
  42. DRM_ERROR("AGP memory manager initialisation error\n");
  43. mutex_unlock(&dev->struct_mutex);
  44. return ret;
  45. }
  46. dev_priv->agp_initialized = 1;
  47. dev_priv->agp_offset = agp->offset;
  48. mutex_unlock(&dev->struct_mutex);
  49. DRM_DEBUG("offset = %u, size = %u\n", agp->offset, agp->size);
  50. return 0;
  51. }
  52. int via_fb_init(struct drm_device *dev, void *data, struct drm_file *file_priv)
  53. {
  54. drm_via_fb_t *fb = data;
  55. drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
  56. int ret;
  57. mutex_lock(&dev->struct_mutex);
  58. ret = drm_sman_set_range(&dev_priv->sman, VIA_MEM_VIDEO, 0,
  59. fb->size >> VIA_MM_ALIGN_SHIFT);
  60. if (ret) {
  61. DRM_ERROR("VRAM memory manager initialisation error\n");
  62. mutex_unlock(&dev->struct_mutex);
  63. return ret;
  64. }
  65. dev_priv->vram_initialized = 1;
  66. dev_priv->vram_offset = fb->offset;
  67. mutex_unlock(&dev->struct_mutex);
  68. DRM_DEBUG("offset = %u, size = %u\n", fb->offset, fb->size);
  69. return 0;
  70. }
  71. int via_final_context(struct drm_device *dev, int context)
  72. {
  73. drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
  74. via_release_futex(dev_priv, context);
  75. /* Linux specific until context tracking code gets ported to BSD */
  76. /* Last context, perform cleanup */
  77. if (dev->ctx_count == 1 && dev->dev_private) {
  78. DRM_DEBUG("Last Context\n");
  79. drm_irq_uninstall(dev);
  80. via_cleanup_futex(dev_priv);
  81. via_do_cleanup_map(dev);
  82. }
  83. return 1;
  84. }
  85. void via_lastclose(struct drm_device *dev)
  86. {
  87. drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
  88. if (!dev_priv)
  89. return;
  90. mutex_lock(&dev->struct_mutex);
  91. drm_sman_cleanup(&dev_priv->sman);
  92. dev_priv->vram_initialized = 0;
  93. dev_priv->agp_initialized = 0;
  94. mutex_unlock(&dev->struct_mutex);
  95. }
  96. int via_mem_alloc(struct drm_device *dev, void *data,
  97. struct drm_file *file_priv)
  98. {
  99. drm_via_mem_t *mem = data;
  100. int retval = 0;
  101. struct drm_memblock_item *item;
  102. drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
  103. unsigned long tmpSize;
  104. if (mem->type > VIA_MEM_AGP) {
  105. DRM_ERROR("Unknown memory type allocation\n");
  106. return -EINVAL;
  107. }
  108. mutex_lock(&dev->struct_mutex);
  109. if (0 == ((mem->type == VIA_MEM_VIDEO) ? dev_priv->vram_initialized :
  110. dev_priv->agp_initialized)) {
  111. DRM_ERROR
  112. ("Attempt to allocate from uninitialized memory manager.\n");
  113. mutex_unlock(&dev->struct_mutex);
  114. return -EINVAL;
  115. }
  116. tmpSize = (mem->size + VIA_MM_ALIGN_MASK) >> VIA_MM_ALIGN_SHIFT;
  117. item = drm_sman_alloc(&dev_priv->sman, mem->type, tmpSize, 0,
  118. (unsigned long)file_priv);
  119. mutex_unlock(&dev->struct_mutex);
  120. if (item) {
  121. mem->offset = ((mem->type == VIA_MEM_VIDEO) ?
  122. dev_priv->vram_offset : dev_priv->agp_offset) +
  123. (item->mm->
  124. offset(item->mm, item->mm_info) << VIA_MM_ALIGN_SHIFT);
  125. mem->index = item->user_hash.key;
  126. } else {
  127. mem->offset = 0;
  128. mem->size = 0;
  129. mem->index = 0;
  130. DRM_DEBUG("Video memory allocation failed\n");
  131. retval = -ENOMEM;
  132. }
  133. return retval;
  134. }
  135. int via_mem_free(struct drm_device *dev, void *data, struct drm_file *file_priv)
  136. {
  137. drm_via_private_t *dev_priv = dev->dev_private;
  138. drm_via_mem_t *mem = data;
  139. int ret;
  140. mutex_lock(&dev->struct_mutex);
  141. ret = drm_sman_free_key(&dev_priv->sman, mem->index);
  142. mutex_unlock(&dev->struct_mutex);
  143. DRM_DEBUG("free = 0x%lx\n", mem->index);
  144. return ret;
  145. }
  146. void via_reclaim_buffers_locked(struct drm_device * dev,
  147. struct drm_file *file_priv)
  148. {
  149. drm_via_private_t *dev_priv = dev->dev_private;
  150. mutex_lock(&dev->struct_mutex);
  151. if (drm_sman_owner_clean(&dev_priv->sman, (unsigned long)file_priv)) {
  152. mutex_unlock(&dev->struct_mutex);
  153. return;
  154. }
  155. if (dev->driver->dma_quiescent) {
  156. dev->driver->dma_quiescent(dev);
  157. }
  158. drm_sman_owner_cleanup(&dev_priv->sman, (unsigned long)file_priv);
  159. mutex_unlock(&dev->struct_mutex);
  160. return;
  161. }