blob: 11bddd5a5a6aa17abbd621c633f98b4ec0b3abc6 [file] [log] [blame]
Daniel Vetter76aaf222010-11-05 22:23:30 +01001/*
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
25#include "drmP.h"
26#include "drm.h"
27#include "i915_drm.h"
28#include "i915_drv.h"
29#include "i915_trace.h"
30#include "intel_drv.h"
31
Chris Wilson93dfb402011-03-29 16:59:50 -070032/* XXX kill agp_type! */
33static unsigned int cache_level_to_agp_type(struct drm_device *dev,
34 enum i915_cache_level cache_level)
35{
36 switch (cache_level) {
37 case I915_CACHE_LLC_MLC:
38 if (INTEL_INFO(dev)->gen >= 6)
39 return AGP_USER_CACHED_MEMORY_LLC_MLC;
40 /* Older chipsets do not have this extra level of CPU
41 * cacheing, so fallthrough and request the PTE simply
42 * as cached.
43 */
44 case I915_CACHE_LLC:
45 return AGP_USER_CACHED_MEMORY;
46 default:
47 case I915_CACHE_NONE:
48 return AGP_USER_MEMORY;
49 }
50}
51
Ben Widawsky5c042282011-10-17 15:51:55 -070052static bool do_idling(struct drm_i915_private *dev_priv)
53{
54 bool ret = dev_priv->mm.interruptible;
55
56 if (unlikely(dev_priv->mm.gtt->do_idle_maps)) {
57 dev_priv->mm.interruptible = false;
Ben Widawsky84364732012-01-24 20:36:15 -080058 if (i915_gpu_idle(dev_priv->dev, false)) {
Ben Widawsky5c042282011-10-17 15:51:55 -070059 DRM_ERROR("Couldn't idle GPU\n");
60 /* Wait a bit, in hopes it avoids the hang */
61 udelay(10);
62 }
63 }
64
65 return ret;
66}
67
68static void undo_idling(struct drm_i915_private *dev_priv, bool interruptible)
69{
70 if (unlikely(dev_priv->mm.gtt->do_idle_maps))
71 dev_priv->mm.interruptible = interruptible;
72}
73
Daniel Vetter76aaf222010-11-05 22:23:30 +010074void i915_gem_restore_gtt_mappings(struct drm_device *dev)
75{
76 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +000077 struct drm_i915_gem_object *obj;
Daniel Vetter76aaf222010-11-05 22:23:30 +010078
Chris Wilsonbee4a182011-01-21 10:54:32 +000079 /* First fill our portion of the GTT with scratch pages */
80 intel_gtt_clear_range(dev_priv->mm.gtt_start / PAGE_SIZE,
81 (dev_priv->mm.gtt_end - dev_priv->mm.gtt_start) / PAGE_SIZE);
82
Chris Wilson05394f32010-11-08 19:18:58 +000083 list_for_each_entry(obj, &dev_priv->mm.gtt_list, gtt_list) {
Chris Wilsona8e93122010-12-08 14:28:54 +000084 i915_gem_clflush_object(obj);
Chris Wilsond5bd1442011-04-14 06:48:26 +010085 i915_gem_gtt_rebind_object(obj, obj->cache_level);
Daniel Vetter76aaf222010-11-05 22:23:30 +010086 }
87
Daniel Vetter76aaf222010-11-05 22:23:30 +010088 intel_gtt_chipset_flush();
89}
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +010090
Chris Wilson05394f32010-11-08 19:18:58 +000091int i915_gem_gtt_bind_object(struct drm_i915_gem_object *obj)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +010092{
Chris Wilson05394f32010-11-08 19:18:58 +000093 struct drm_device *dev = obj->base.dev;
Daniel Vetter185cbcb2010-11-06 12:12:35 +010094 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson93dfb402011-03-29 16:59:50 -070095 unsigned int agp_type = cache_level_to_agp_type(dev, obj->cache_level);
Daniel Vetter185cbcb2010-11-06 12:12:35 +010096 int ret;
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +010097
Daniel Vetter185cbcb2010-11-06 12:12:35 +010098 if (dev_priv->mm.gtt->needs_dmar) {
Chris Wilson05394f32010-11-08 19:18:58 +000099 ret = intel_gtt_map_memory(obj->pages,
100 obj->base.size >> PAGE_SHIFT,
101 &obj->sg_list,
102 &obj->num_sg);
Daniel Vetter185cbcb2010-11-06 12:12:35 +0100103 if (ret != 0)
104 return ret;
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +0100105
Chris Wilson05394f32010-11-08 19:18:58 +0000106 intel_gtt_insert_sg_entries(obj->sg_list,
107 obj->num_sg,
108 obj->gtt_space->start >> PAGE_SHIFT,
Chris Wilson93dfb402011-03-29 16:59:50 -0700109 agp_type);
Daniel Vetter185cbcb2010-11-06 12:12:35 +0100110 } else
Chris Wilson05394f32010-11-08 19:18:58 +0000111 intel_gtt_insert_pages(obj->gtt_space->start >> PAGE_SHIFT,
112 obj->base.size >> PAGE_SHIFT,
113 obj->pages,
Chris Wilson93dfb402011-03-29 16:59:50 -0700114 agp_type);
Daniel Vetter185cbcb2010-11-06 12:12:35 +0100115
116 return 0;
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +0100117}
118
Chris Wilsone4ffd172011-04-04 09:44:39 +0100119void i915_gem_gtt_rebind_object(struct drm_i915_gem_object *obj,
120 enum i915_cache_level cache_level)
Chris Wilsond5bd1442011-04-14 06:48:26 +0100121{
122 struct drm_device *dev = obj->base.dev;
123 struct drm_i915_private *dev_priv = dev->dev_private;
124 unsigned int agp_type = cache_level_to_agp_type(dev, cache_level);
125
126 if (dev_priv->mm.gtt->needs_dmar) {
127 BUG_ON(!obj->sg_list);
128
129 intel_gtt_insert_sg_entries(obj->sg_list,
130 obj->num_sg,
131 obj->gtt_space->start >> PAGE_SHIFT,
132 agp_type);
133 } else
134 intel_gtt_insert_pages(obj->gtt_space->start >> PAGE_SHIFT,
135 obj->base.size >> PAGE_SHIFT,
136 obj->pages,
137 agp_type);
138}
139
Chris Wilson05394f32010-11-08 19:18:58 +0000140void i915_gem_gtt_unbind_object(struct drm_i915_gem_object *obj)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +0100141{
Ben Widawsky5c042282011-10-17 15:51:55 -0700142 struct drm_device *dev = obj->base.dev;
143 struct drm_i915_private *dev_priv = dev->dev_private;
144 bool interruptible;
145
146 interruptible = do_idling(dev_priv);
147
Chris Wilson05394f32010-11-08 19:18:58 +0000148 intel_gtt_clear_range(obj->gtt_space->start >> PAGE_SHIFT,
149 obj->base.size >> PAGE_SHIFT);
Chris Wilsond9126402011-01-11 11:07:54 +0000150
151 if (obj->sg_list) {
152 intel_gtt_unmap_memory(obj->sg_list, obj->num_sg);
153 obj->sg_list = NULL;
154 }
Ben Widawsky5c042282011-10-17 15:51:55 -0700155
156 undo_idling(dev_priv, interruptible);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +0100157}