blob: c040aad0cca6652f64ae1198e2ce8868a9e300ea [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
David Howells760285e2012-10-02 18:01:07 +010025#include <drm/drmP.h>
26#include <drm/i915_drm.h>
Daniel Vetter76aaf222010-11-05 22:23:30 +010027#include "i915_drv.h"
28#include "i915_trace.h"
29#include "intel_drv.h"
30
Daniel Vetter1d2a3142012-02-09 17:15:46 +010031/* PPGTT support for Sandybdrige/Gen6 and later */
32static void i915_ppgtt_clear_range(struct i915_hw_ppgtt *ppgtt,
33 unsigned first_entry,
34 unsigned num_entries)
35{
Daniel Vetter1d2a3142012-02-09 17:15:46 +010036 uint32_t *pt_vaddr;
37 uint32_t scratch_pte;
Daniel Vetter7bddb012012-02-09 17:15:47 +010038 unsigned act_pd = first_entry / I915_PPGTT_PT_ENTRIES;
39 unsigned first_pte = first_entry % I915_PPGTT_PT_ENTRIES;
40 unsigned last_pte, i;
Daniel Vetter1d2a3142012-02-09 17:15:46 +010041
42 scratch_pte = GEN6_PTE_ADDR_ENCODE(ppgtt->scratch_page_dma_addr);
43 scratch_pte |= GEN6_PTE_VALID | GEN6_PTE_CACHE_LLC;
44
Daniel Vetter7bddb012012-02-09 17:15:47 +010045 while (num_entries) {
46 last_pte = first_pte + num_entries;
47 if (last_pte > I915_PPGTT_PT_ENTRIES)
48 last_pte = I915_PPGTT_PT_ENTRIES;
Daniel Vetter1d2a3142012-02-09 17:15:46 +010049
Daniel Vetter7bddb012012-02-09 17:15:47 +010050 pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pd]);
51
52 for (i = first_pte; i < last_pte; i++)
53 pt_vaddr[i] = scratch_pte;
Daniel Vetter1d2a3142012-02-09 17:15:46 +010054
55 kunmap_atomic(pt_vaddr);
Daniel Vetter1d2a3142012-02-09 17:15:46 +010056
Daniel Vetter7bddb012012-02-09 17:15:47 +010057 num_entries -= last_pte - first_pte;
58 first_pte = 0;
59 act_pd++;
60 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +010061}
62
63int i915_gem_init_aliasing_ppgtt(struct drm_device *dev)
64{
65 struct drm_i915_private *dev_priv = dev->dev_private;
66 struct i915_hw_ppgtt *ppgtt;
Daniel Vetter1d2a3142012-02-09 17:15:46 +010067 unsigned first_pd_entry_in_global_pt;
Daniel Vetter1d2a3142012-02-09 17:15:46 +010068 int i;
69 int ret = -ENOMEM;
70
71 /* ppgtt PDEs reside in the global gtt pagetable, which has 512*1024
72 * entries. For aliasing ppgtt support we just steal them at the end for
73 * now. */
Chris Wilson9a0f9382012-08-24 09:12:22 +010074 first_pd_entry_in_global_pt = dev_priv->mm.gtt->gtt_total_entries - I915_PPGTT_PD_ENTRIES;
Daniel Vetter1d2a3142012-02-09 17:15:46 +010075
76 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
77 if (!ppgtt)
78 return ret;
79
80 ppgtt->num_pd_entries = I915_PPGTT_PD_ENTRIES;
81 ppgtt->pt_pages = kzalloc(sizeof(struct page *)*ppgtt->num_pd_entries,
82 GFP_KERNEL);
83 if (!ppgtt->pt_pages)
84 goto err_ppgtt;
85
86 for (i = 0; i < ppgtt->num_pd_entries; i++) {
87 ppgtt->pt_pages[i] = alloc_page(GFP_KERNEL);
88 if (!ppgtt->pt_pages[i])
89 goto err_pt_alloc;
90 }
91
92 if (dev_priv->mm.gtt->needs_dmar) {
93 ppgtt->pt_dma_addr = kzalloc(sizeof(dma_addr_t)
94 *ppgtt->num_pd_entries,
95 GFP_KERNEL);
96 if (!ppgtt->pt_dma_addr)
97 goto err_pt_alloc;
Daniel Vetter1d2a3142012-02-09 17:15:46 +010098
Daniel Vetter211c5682012-04-10 17:29:17 +020099 for (i = 0; i < ppgtt->num_pd_entries; i++) {
100 dma_addr_t pt_addr;
101
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100102 pt_addr = pci_map_page(dev->pdev, ppgtt->pt_pages[i],
103 0, 4096,
104 PCI_DMA_BIDIRECTIONAL);
105
106 if (pci_dma_mapping_error(dev->pdev,
107 pt_addr)) {
108 ret = -EIO;
109 goto err_pd_pin;
110
111 }
112 ppgtt->pt_dma_addr[i] = pt_addr;
Daniel Vetter211c5682012-04-10 17:29:17 +0200113 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100114 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100115
116 ppgtt->scratch_page_dma_addr = dev_priv->mm.gtt->scratch_page_dma;
117
118 i915_ppgtt_clear_range(ppgtt, 0,
119 ppgtt->num_pd_entries*I915_PPGTT_PT_ENTRIES);
120
121 ppgtt->pd_offset = (first_pd_entry_in_global_pt)*sizeof(uint32_t);
122
123 dev_priv->mm.aliasing_ppgtt = ppgtt;
124
125 return 0;
126
127err_pd_pin:
128 if (ppgtt->pt_dma_addr) {
129 for (i--; i >= 0; i--)
130 pci_unmap_page(dev->pdev, ppgtt->pt_dma_addr[i],
131 4096, PCI_DMA_BIDIRECTIONAL);
132 }
133err_pt_alloc:
134 kfree(ppgtt->pt_dma_addr);
135 for (i = 0; i < ppgtt->num_pd_entries; i++) {
136 if (ppgtt->pt_pages[i])
137 __free_page(ppgtt->pt_pages[i]);
138 }
139 kfree(ppgtt->pt_pages);
140err_ppgtt:
141 kfree(ppgtt);
142
143 return ret;
144}
145
146void i915_gem_cleanup_aliasing_ppgtt(struct drm_device *dev)
147{
148 struct drm_i915_private *dev_priv = dev->dev_private;
149 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
150 int i;
151
152 if (!ppgtt)
153 return;
154
155 if (ppgtt->pt_dma_addr) {
156 for (i = 0; i < ppgtt->num_pd_entries; i++)
157 pci_unmap_page(dev->pdev, ppgtt->pt_dma_addr[i],
158 4096, PCI_DMA_BIDIRECTIONAL);
159 }
160
161 kfree(ppgtt->pt_dma_addr);
162 for (i = 0; i < ppgtt->num_pd_entries; i++)
163 __free_page(ppgtt->pt_pages[i]);
164 kfree(ppgtt->pt_pages);
165 kfree(ppgtt);
166}
167
Daniel Vetter7bddb012012-02-09 17:15:47 +0100168static void i915_ppgtt_insert_sg_entries(struct i915_hw_ppgtt *ppgtt,
Chris Wilson9da3da62012-06-01 15:20:22 +0100169 const struct sg_table *pages,
Daniel Vetter7bddb012012-02-09 17:15:47 +0100170 unsigned first_entry,
171 uint32_t pte_flags)
172{
173 uint32_t *pt_vaddr, pte;
174 unsigned act_pd = first_entry / I915_PPGTT_PT_ENTRIES;
175 unsigned first_pte = first_entry % I915_PPGTT_PT_ENTRIES;
176 unsigned i, j, m, segment_len;
177 dma_addr_t page_addr;
178 struct scatterlist *sg;
179
180 /* init sg walking */
Chris Wilson9da3da62012-06-01 15:20:22 +0100181 sg = pages->sgl;
Daniel Vetter7bddb012012-02-09 17:15:47 +0100182 i = 0;
183 segment_len = sg_dma_len(sg) >> PAGE_SHIFT;
184 m = 0;
185
Chris Wilson9da3da62012-06-01 15:20:22 +0100186 while (i < pages->nents) {
Daniel Vetter7bddb012012-02-09 17:15:47 +0100187 pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pd]);
188
189 for (j = first_pte; j < I915_PPGTT_PT_ENTRIES; j++) {
190 page_addr = sg_dma_address(sg) + (m << PAGE_SHIFT);
191 pte = GEN6_PTE_ADDR_ENCODE(page_addr);
192 pt_vaddr[j] = pte | pte_flags;
193
194 /* grab the next page */
Chris Wilson9da3da62012-06-01 15:20:22 +0100195 if (++m == segment_len) {
196 if (++i == pages->nents)
Daniel Vetter7bddb012012-02-09 17:15:47 +0100197 break;
198
Chris Wilson9da3da62012-06-01 15:20:22 +0100199 sg = sg_next(sg);
Daniel Vetter7bddb012012-02-09 17:15:47 +0100200 segment_len = sg_dma_len(sg) >> PAGE_SHIFT;
201 m = 0;
202 }
203 }
204
205 kunmap_atomic(pt_vaddr);
206
207 first_pte = 0;
208 act_pd++;
209 }
210}
211
Daniel Vetter7bddb012012-02-09 17:15:47 +0100212void i915_ppgtt_bind_object(struct i915_hw_ppgtt *ppgtt,
213 struct drm_i915_gem_object *obj,
214 enum i915_cache_level cache_level)
215{
Daniel Vetter7bddb012012-02-09 17:15:47 +0100216 uint32_t pte_flags = GEN6_PTE_VALID;
217
218 switch (cache_level) {
219 case I915_CACHE_LLC_MLC:
Ben Widawsky86936072012-09-21 16:54:14 -0700220 /* Haswell doesn't set L3 this way */
221 if (IS_HASWELL(obj->base.dev))
222 pte_flags |= GEN6_PTE_CACHE_LLC;
223 else
224 pte_flags |= GEN6_PTE_CACHE_LLC_MLC;
Daniel Vetter7bddb012012-02-09 17:15:47 +0100225 break;
226 case I915_CACHE_LLC:
227 pte_flags |= GEN6_PTE_CACHE_LLC;
228 break;
229 case I915_CACHE_NONE:
Chris Wilson9da3da62012-06-01 15:20:22 +0100230 if (IS_HASWELL(obj->base.dev))
Daniel Vettera843af12012-08-14 11:42:14 -0300231 pte_flags |= HSW_PTE_UNCACHED;
232 else
233 pte_flags |= GEN6_PTE_UNCACHED;
Daniel Vetter7bddb012012-02-09 17:15:47 +0100234 break;
235 default:
236 BUG();
237 }
238
Chris Wilson9da3da62012-06-01 15:20:22 +0100239 i915_ppgtt_insert_sg_entries(ppgtt,
Chris Wilson2f745ad2012-09-04 21:02:58 +0100240 obj->pages,
Chris Wilson9da3da62012-06-01 15:20:22 +0100241 obj->gtt_space->start >> PAGE_SHIFT,
242 pte_flags);
Daniel Vetter7bddb012012-02-09 17:15:47 +0100243}
244
245void i915_ppgtt_unbind_object(struct i915_hw_ppgtt *ppgtt,
246 struct drm_i915_gem_object *obj)
247{
248 i915_ppgtt_clear_range(ppgtt,
249 obj->gtt_space->start >> PAGE_SHIFT,
250 obj->base.size >> PAGE_SHIFT);
251}
252
Chris Wilson93dfb402011-03-29 16:59:50 -0700253/* XXX kill agp_type! */
254static unsigned int cache_level_to_agp_type(struct drm_device *dev,
255 enum i915_cache_level cache_level)
256{
257 switch (cache_level) {
258 case I915_CACHE_LLC_MLC:
Chris Wilson93dfb402011-03-29 16:59:50 -0700259 /* Older chipsets do not have this extra level of CPU
260 * cacheing, so fallthrough and request the PTE simply
261 * as cached.
262 */
Ben Widawsky86936072012-09-21 16:54:14 -0700263 if (INTEL_INFO(dev)->gen >= 6 && !IS_HASWELL(dev))
264 return AGP_USER_CACHED_MEMORY_LLC_MLC;
Chris Wilson93dfb402011-03-29 16:59:50 -0700265 case I915_CACHE_LLC:
266 return AGP_USER_CACHED_MEMORY;
267 default:
268 case I915_CACHE_NONE:
269 return AGP_USER_MEMORY;
270 }
271}
272
Ben Widawsky5c042282011-10-17 15:51:55 -0700273static bool do_idling(struct drm_i915_private *dev_priv)
274{
275 bool ret = dev_priv->mm.interruptible;
276
277 if (unlikely(dev_priv->mm.gtt->do_idle_maps)) {
278 dev_priv->mm.interruptible = false;
Ben Widawskyb2da9fe2012-04-26 16:02:58 -0700279 if (i915_gpu_idle(dev_priv->dev)) {
Ben Widawsky5c042282011-10-17 15:51:55 -0700280 DRM_ERROR("Couldn't idle GPU\n");
281 /* Wait a bit, in hopes it avoids the hang */
282 udelay(10);
283 }
284 }
285
286 return ret;
287}
288
289static void undo_idling(struct drm_i915_private *dev_priv, bool interruptible)
290{
291 if (unlikely(dev_priv->mm.gtt->do_idle_maps))
292 dev_priv->mm.interruptible = interruptible;
293}
294
Daniel Vetter76aaf222010-11-05 22:23:30 +0100295void i915_gem_restore_gtt_mappings(struct drm_device *dev)
296{
297 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +0000298 struct drm_i915_gem_object *obj;
Daniel Vetter76aaf222010-11-05 22:23:30 +0100299
Chris Wilsonbee4a182011-01-21 10:54:32 +0000300 /* First fill our portion of the GTT with scratch pages */
301 intel_gtt_clear_range(dev_priv->mm.gtt_start / PAGE_SIZE,
302 (dev_priv->mm.gtt_end - dev_priv->mm.gtt_start) / PAGE_SIZE);
303
Chris Wilson6c085a72012-08-20 11:40:46 +0200304 list_for_each_entry(obj, &dev_priv->mm.bound_list, gtt_list) {
Chris Wilsona8e93122010-12-08 14:28:54 +0000305 i915_gem_clflush_object(obj);
Daniel Vetter74163902012-02-15 23:50:21 +0100306 i915_gem_gtt_bind_object(obj, obj->cache_level);
Daniel Vetter76aaf222010-11-05 22:23:30 +0100307 }
308
Daniel Vetter76aaf222010-11-05 22:23:30 +0100309 intel_gtt_chipset_flush();
310}
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +0100311
Daniel Vetter74163902012-02-15 23:50:21 +0100312int i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +0100313{
Chris Wilson9da3da62012-06-01 15:20:22 +0100314 if (obj->has_dma_mapping)
Daniel Vetter74163902012-02-15 23:50:21 +0100315 return 0;
Chris Wilson9da3da62012-06-01 15:20:22 +0100316
317 if (!dma_map_sg(&obj->base.dev->pdev->dev,
318 obj->pages->sgl, obj->pages->nents,
319 PCI_DMA_BIDIRECTIONAL))
320 return -ENOSPC;
321
322 return 0;
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +0100323}
324
Daniel Vetter74163902012-02-15 23:50:21 +0100325void i915_gem_gtt_bind_object(struct drm_i915_gem_object *obj,
326 enum i915_cache_level cache_level)
Chris Wilsond5bd1442011-04-14 06:48:26 +0100327{
328 struct drm_device *dev = obj->base.dev;
Chris Wilsond5bd1442011-04-14 06:48:26 +0100329 unsigned int agp_type = cache_level_to_agp_type(dev, cache_level);
330
Chris Wilson2f745ad2012-09-04 21:02:58 +0100331 intel_gtt_insert_sg_entries(obj->pages,
Chris Wilson9da3da62012-06-01 15:20:22 +0100332 obj->gtt_space->start >> PAGE_SHIFT,
333 agp_type);
Daniel Vetter74898d72012-02-15 23:50:22 +0100334 obj->has_global_gtt_mapping = 1;
Chris Wilsond5bd1442011-04-14 06:48:26 +0100335}
336
Chris Wilson05394f32010-11-08 19:18:58 +0000337void i915_gem_gtt_unbind_object(struct drm_i915_gem_object *obj)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +0100338{
Daniel Vetter74163902012-02-15 23:50:21 +0100339 intel_gtt_clear_range(obj->gtt_space->start >> PAGE_SHIFT,
340 obj->base.size >> PAGE_SHIFT);
Daniel Vetter74898d72012-02-15 23:50:22 +0100341
342 obj->has_global_gtt_mapping = 0;
Daniel Vetter74163902012-02-15 23:50:21 +0100343}
344
345void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj)
346{
Ben Widawsky5c042282011-10-17 15:51:55 -0700347 struct drm_device *dev = obj->base.dev;
348 struct drm_i915_private *dev_priv = dev->dev_private;
349 bool interruptible;
350
351 interruptible = do_idling(dev_priv);
352
Chris Wilson9da3da62012-06-01 15:20:22 +0100353 if (!obj->has_dma_mapping)
354 dma_unmap_sg(&dev->pdev->dev,
355 obj->pages->sgl, obj->pages->nents,
356 PCI_DMA_BIDIRECTIONAL);
Ben Widawsky5c042282011-10-17 15:51:55 -0700357
358 undo_idling(dev_priv, interruptible);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +0100359}
Daniel Vetter644ec022012-03-26 09:45:40 +0200360
Chris Wilson42d6ab42012-07-26 11:49:32 +0100361static void i915_gtt_color_adjust(struct drm_mm_node *node,
362 unsigned long color,
363 unsigned long *start,
364 unsigned long *end)
365{
366 if (node->color != color)
367 *start += 4096;
368
369 if (!list_empty(&node->node_list)) {
370 node = list_entry(node->node_list.next,
371 struct drm_mm_node,
372 node_list);
373 if (node->allocated && node->color != color)
374 *end -= 4096;
375 }
376}
377
Daniel Vetter644ec022012-03-26 09:45:40 +0200378void i915_gem_init_global_gtt(struct drm_device *dev,
379 unsigned long start,
380 unsigned long mappable_end,
381 unsigned long end)
382{
383 drm_i915_private_t *dev_priv = dev->dev_private;
384
Daniel Vetterd1dd20a2012-03-26 09:45:42 +0200385 /* Substract the guard page ... */
386 drm_mm_init(&dev_priv->mm.gtt_space, start, end - start - PAGE_SIZE);
Chris Wilson42d6ab42012-07-26 11:49:32 +0100387 if (!HAS_LLC(dev))
388 dev_priv->mm.gtt_space.color_adjust = i915_gtt_color_adjust;
Daniel Vetter644ec022012-03-26 09:45:40 +0200389
390 dev_priv->mm.gtt_start = start;
391 dev_priv->mm.gtt_mappable_end = mappable_end;
392 dev_priv->mm.gtt_end = end;
393 dev_priv->mm.gtt_total = end - start;
394 dev_priv->mm.mappable_gtt_total = min(end, mappable_end) - start;
395
Daniel Vetterd1dd20a2012-03-26 09:45:42 +0200396 /* ... but ensure that we clear the entire range. */
Daniel Vetter644ec022012-03-26 09:45:40 +0200397 intel_gtt_clear_range(start / PAGE_SIZE, (end-start) / PAGE_SIZE);
398}