via_mm.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. if (dev->irq)
  80. drm_irq_uninstall(dev);
  81. via_cleanup_futex(dev_priv);
  82. via_do_cleanup_map(dev);
  83. }
  84. return 1;
  85. }
  86. void via_lastclose(struct drm_device *dev)
  87. {
  88. drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
  89. if (!dev_priv)
  90. return;
  91. mutex_lock(&dev->struct_mutex);
  92. drm_sman_cleanup(&dev_priv->sman);
  93. dev_priv->vram_initialized = 0;
  94. dev_priv->agp_initialized = 0;
  95. mutex_unlock(&dev->struct_mutex);
  96. }
  97. int via_mem_alloc(struct drm_device *dev, void *data,
  98. struct drm_file *file_priv)
  99. {
  100. drm_via_mem_t *mem = data;
  101. int retval = 0;
  102. struct drm_memblock_item *item;
  103. drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
  104. unsigned long tmpSize;
  105. if (mem->type > VIA_MEM_AGP) {
  106. DRM_ERROR("Unknown memory type allocation\n");
  107. return -EINVAL;
  108. }
  109. mutex_lock(&dev->struct_mutex);
  110. if (0 == ((mem->type == VIA_MEM_VIDEO) ? dev_priv->vram_initialized :
  111. dev_priv->agp_initialized)) {
  112. DRM_ERROR
  113. ("Attempt to allocate from uninitialized memory manager.\n");
  114. mutex_unlock(&dev->struct_mutex);
  115. return -EINVAL;
  116. }
  117. tmpSize = (mem->size + VIA_MM_ALIGN_MASK) >> VIA_MM_ALIGN_SHIFT;
  118. item = drm_sman_alloc(&dev_priv->sman, mem->type, tmpSize, 0,
  119. (unsigned long)file_priv);
  120. mutex_unlock(&dev->struct_mutex);
  121. if (item) {
  122. mem->offset = ((mem->type == VIA_MEM_VIDEO) ?
  123. dev_priv->vram_offset : dev_priv->agp_offset) +
  124. (item->mm->
  125. offset(item->mm, item->mm_info) << VIA_MM_ALIGN_SHIFT);
  126. mem->index = item->user_hash.key;
  127. } else {
  128. mem->offset = 0;
  129. mem->size = 0;
  130. mem->index = 0;
  131. DRM_DEBUG("Video memory allocation failed\n");
  132. retval = -ENOMEM;
  133. }
  134. return retval;
  135. }
  136. int via_mem_free(struct drm_device *dev, void *data, struct drm_file *file_priv)
  137. {
  138. drm_via_private_t *dev_priv = dev->dev_private;
  139. drm_via_mem_t *mem = data;
  140. int ret;
  141. mutex_lock(&dev->struct_mutex);
  142. ret = drm_sman_free_key(&dev_priv->sman, mem->index);
  143. mutex_unlock(&dev->struct_mutex);
  144. DRM_DEBUG("free = 0x%lx\n", mem->index);
  145. return ret;
  146. }
  147. void via_reclaim_buffers_locked(struct drm_device * dev,
  148. struct drm_file *file_priv)
  149. {
  150. drm_via_private_t *dev_priv = dev->dev_private;
  151. mutex_lock(&dev->struct_mutex);
  152. if (drm_sman_owner_clean(&dev_priv->sman, (unsigned long)file_priv)) {
  153. mutex_unlock(&dev->struct_mutex);
  154. return;
  155. }
  156. if (dev->driver->dma_quiescent) {
  157. dev->driver->dma_quiescent(dev);
  158. }
  159. drm_sman_owner_cleanup(&dev_priv->sman, (unsigned long)file_priv);
  160. mutex_unlock(&dev->struct_mutex);
  161. return;
  162. }