i915_gem_gtt.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright © 2010 Daniel Vetter
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. */
  24. #include "drmP.h"
  25. #include "drm.h"
  26. #include "i915_drm.h"
  27. #include "i915_drv.h"
  28. #include "i915_trace.h"
  29. #include "intel_drv.h"
  30. /* XXX kill agp_type! */
  31. static unsigned int cache_level_to_agp_type(struct drm_device *dev,
  32. enum i915_cache_level cache_level)
  33. {
  34. switch (cache_level) {
  35. case I915_CACHE_LLC_MLC:
  36. if (INTEL_INFO(dev)->gen >= 6)
  37. return AGP_USER_CACHED_MEMORY_LLC_MLC;
  38. /* Older chipsets do not have this extra level of CPU
  39. * cacheing, so fallthrough and request the PTE simply
  40. * as cached.
  41. */
  42. case I915_CACHE_LLC:
  43. return AGP_USER_CACHED_MEMORY;
  44. default:
  45. case I915_CACHE_NONE:
  46. return AGP_USER_MEMORY;
  47. }
  48. }
  49. void i915_gem_restore_gtt_mappings(struct drm_device *dev)
  50. {
  51. struct drm_i915_private *dev_priv = dev->dev_private;
  52. struct drm_i915_gem_object *obj;
  53. /* First fill our portion of the GTT with scratch pages */
  54. intel_gtt_clear_range(dev_priv->mm.gtt_start / PAGE_SIZE,
  55. (dev_priv->mm.gtt_end - dev_priv->mm.gtt_start) / PAGE_SIZE);
  56. list_for_each_entry(obj, &dev_priv->mm.gtt_list, gtt_list) {
  57. i915_gem_clflush_object(obj);
  58. i915_gem_gtt_rebind_object(obj, obj->cache_level);
  59. }
  60. intel_gtt_chipset_flush();
  61. }
  62. int i915_gem_gtt_bind_object(struct drm_i915_gem_object *obj)
  63. {
  64. struct drm_device *dev = obj->base.dev;
  65. struct drm_i915_private *dev_priv = dev->dev_private;
  66. unsigned int agp_type = cache_level_to_agp_type(dev, obj->cache_level);
  67. int ret;
  68. if (dev_priv->mm.gtt->needs_dmar) {
  69. ret = intel_gtt_map_memory(obj->pages,
  70. obj->base.size >> PAGE_SHIFT,
  71. &obj->sg_list,
  72. &obj->num_sg);
  73. if (ret != 0)
  74. return ret;
  75. intel_gtt_insert_sg_entries(obj->sg_list,
  76. obj->num_sg,
  77. obj->gtt_space->start >> PAGE_SHIFT,
  78. agp_type);
  79. } else
  80. intel_gtt_insert_pages(obj->gtt_space->start >> PAGE_SHIFT,
  81. obj->base.size >> PAGE_SHIFT,
  82. obj->pages,
  83. agp_type);
  84. return 0;
  85. }
  86. void i915_gem_gtt_rebind_object(struct drm_i915_gem_object *obj,
  87. enum i915_cache_level cache_level)
  88. {
  89. struct drm_device *dev = obj->base.dev;
  90. struct drm_i915_private *dev_priv = dev->dev_private;
  91. unsigned int agp_type = cache_level_to_agp_type(dev, cache_level);
  92. if (dev_priv->mm.gtt->needs_dmar) {
  93. BUG_ON(!obj->sg_list);
  94. intel_gtt_insert_sg_entries(obj->sg_list,
  95. obj->num_sg,
  96. obj->gtt_space->start >> PAGE_SHIFT,
  97. agp_type);
  98. } else
  99. intel_gtt_insert_pages(obj->gtt_space->start >> PAGE_SHIFT,
  100. obj->base.size >> PAGE_SHIFT,
  101. obj->pages,
  102. agp_type);
  103. }
  104. void i915_gem_gtt_unbind_object(struct drm_i915_gem_object *obj)
  105. {
  106. intel_gtt_clear_range(obj->gtt_space->start >> PAGE_SHIFT,
  107. obj->base.size >> PAGE_SHIFT);
  108. if (obj->sg_list) {
  109. intel_gtt_unmap_memory(obj->sg_list, obj->num_sg);
  110. obj->sg_list = NULL;
  111. }
  112. }