blob: 06202fd6dbdd0e6cd5bfd523ad1e73989d3aad5a [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
Ben Widawskyf61c0602012-10-22 11:44:43 -070031typedef uint32_t gtt_pte_t;
32
Ben Widawsky54d12522012-09-24 16:44:32 -070033static inline gtt_pte_t pte_encode(struct drm_device *dev,
34 dma_addr_t addr,
Ben Widawskye7210c32012-10-19 09:33:22 -070035 enum i915_cache_level level)
Ben Widawsky54d12522012-09-24 16:44:32 -070036{
37 gtt_pte_t pte = GEN6_PTE_VALID;
38 pte |= GEN6_PTE_ADDR_ENCODE(addr);
Ben Widawskye7210c32012-10-19 09:33:22 -070039
40 switch (level) {
41 case I915_CACHE_LLC_MLC:
42 /* Haswell doesn't set L3 this way */
43 if (IS_HASWELL(dev))
44 pte |= GEN6_PTE_CACHE_LLC;
45 else
46 pte |= GEN6_PTE_CACHE_LLC_MLC;
47 break;
48 case I915_CACHE_LLC:
49 pte |= GEN6_PTE_CACHE_LLC;
50 break;
51 case I915_CACHE_NONE:
52 if (IS_HASWELL(dev))
53 pte |= HSW_PTE_UNCACHED;
54 else
55 pte |= GEN6_PTE_UNCACHED;
56 break;
57 default:
58 BUG();
59 }
60
Ben Widawsky54d12522012-09-24 16:44:32 -070061
62 return pte;
63}
64
Daniel Vetter1d2a3142012-02-09 17:15:46 +010065/* PPGTT support for Sandybdrige/Gen6 and later */
66static void i915_ppgtt_clear_range(struct i915_hw_ppgtt *ppgtt,
67 unsigned first_entry,
68 unsigned num_entries)
69{
Ben Widawskyf61c0602012-10-22 11:44:43 -070070 gtt_pte_t *pt_vaddr;
71 gtt_pte_t scratch_pte;
Daniel Vetter7bddb012012-02-09 17:15:47 +010072 unsigned act_pd = first_entry / I915_PPGTT_PT_ENTRIES;
73 unsigned first_pte = first_entry % I915_PPGTT_PT_ENTRIES;
74 unsigned last_pte, i;
Daniel Vetter1d2a3142012-02-09 17:15:46 +010075
Ben Widawsky54d12522012-09-24 16:44:32 -070076 scratch_pte = pte_encode(ppgtt->dev, ppgtt->scratch_page_dma_addr,
Ben Widawskye7210c32012-10-19 09:33:22 -070077 I915_CACHE_LLC);
Daniel Vetter1d2a3142012-02-09 17:15:46 +010078
Daniel Vetter7bddb012012-02-09 17:15:47 +010079 while (num_entries) {
80 last_pte = first_pte + num_entries;
81 if (last_pte > I915_PPGTT_PT_ENTRIES)
82 last_pte = I915_PPGTT_PT_ENTRIES;
Daniel Vetter1d2a3142012-02-09 17:15:46 +010083
Daniel Vetter7bddb012012-02-09 17:15:47 +010084 pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pd]);
85
86 for (i = first_pte; i < last_pte; i++)
87 pt_vaddr[i] = scratch_pte;
Daniel Vetter1d2a3142012-02-09 17:15:46 +010088
89 kunmap_atomic(pt_vaddr);
Daniel Vetter1d2a3142012-02-09 17:15:46 +010090
Daniel Vetter7bddb012012-02-09 17:15:47 +010091 num_entries -= last_pte - first_pte;
92 first_pte = 0;
93 act_pd++;
94 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +010095}
96
97int i915_gem_init_aliasing_ppgtt(struct drm_device *dev)
98{
99 struct drm_i915_private *dev_priv = dev->dev_private;
100 struct i915_hw_ppgtt *ppgtt;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100101 unsigned first_pd_entry_in_global_pt;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100102 int i;
103 int ret = -ENOMEM;
104
105 /* ppgtt PDEs reside in the global gtt pagetable, which has 512*1024
106 * entries. For aliasing ppgtt support we just steal them at the end for
107 * now. */
Chris Wilson9a0f9382012-08-24 09:12:22 +0100108 first_pd_entry_in_global_pt = dev_priv->mm.gtt->gtt_total_entries - I915_PPGTT_PD_ENTRIES;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100109
110 ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
111 if (!ppgtt)
112 return ret;
113
Ben Widawsky8f2c59f2012-09-24 08:55:51 -0700114 ppgtt->dev = dev;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100115 ppgtt->num_pd_entries = I915_PPGTT_PD_ENTRIES;
116 ppgtt->pt_pages = kzalloc(sizeof(struct page *)*ppgtt->num_pd_entries,
117 GFP_KERNEL);
118 if (!ppgtt->pt_pages)
119 goto err_ppgtt;
120
121 for (i = 0; i < ppgtt->num_pd_entries; i++) {
122 ppgtt->pt_pages[i] = alloc_page(GFP_KERNEL);
123 if (!ppgtt->pt_pages[i])
124 goto err_pt_alloc;
125 }
126
127 if (dev_priv->mm.gtt->needs_dmar) {
128 ppgtt->pt_dma_addr = kzalloc(sizeof(dma_addr_t)
129 *ppgtt->num_pd_entries,
130 GFP_KERNEL);
131 if (!ppgtt->pt_dma_addr)
132 goto err_pt_alloc;
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100133
Daniel Vetter211c5682012-04-10 17:29:17 +0200134 for (i = 0; i < ppgtt->num_pd_entries; i++) {
135 dma_addr_t pt_addr;
136
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100137 pt_addr = pci_map_page(dev->pdev, ppgtt->pt_pages[i],
138 0, 4096,
139 PCI_DMA_BIDIRECTIONAL);
140
141 if (pci_dma_mapping_error(dev->pdev,
142 pt_addr)) {
143 ret = -EIO;
144 goto err_pd_pin;
145
146 }
147 ppgtt->pt_dma_addr[i] = pt_addr;
Daniel Vetter211c5682012-04-10 17:29:17 +0200148 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100149 }
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100150
151 ppgtt->scratch_page_dma_addr = dev_priv->mm.gtt->scratch_page_dma;
152
153 i915_ppgtt_clear_range(ppgtt, 0,
154 ppgtt->num_pd_entries*I915_PPGTT_PT_ENTRIES);
155
Ben Widawskyf61c0602012-10-22 11:44:43 -0700156 ppgtt->pd_offset = (first_pd_entry_in_global_pt)*sizeof(gtt_pte_t);
Daniel Vetter1d2a3142012-02-09 17:15:46 +0100157
158 dev_priv->mm.aliasing_ppgtt = ppgtt;
159
160 return 0;
161
162err_pd_pin:
163 if (ppgtt->pt_dma_addr) {
164 for (i--; i >= 0; i--)
165 pci_unmap_page(dev->pdev, ppgtt->pt_dma_addr[i],
166 4096, PCI_DMA_BIDIRECTIONAL);
167 }
168err_pt_alloc:
169 kfree(ppgtt->pt_dma_addr);
170 for (i = 0; i < ppgtt->num_pd_entries; i++) {
171 if (ppgtt->pt_pages[i])
172 __free_page(ppgtt->pt_pages[i]);
173 }
174 kfree(ppgtt->pt_pages);
175err_ppgtt:
176 kfree(ppgtt);
177
178 return ret;
179}
180
181void i915_gem_cleanup_aliasing_ppgtt(struct drm_device *dev)
182{
183 struct drm_i915_private *dev_priv = dev->dev_private;
184 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
185 int i;
186
187 if (!ppgtt)
188 return;
189
190 if (ppgtt->pt_dma_addr) {
191 for (i = 0; i < ppgtt->num_pd_entries; i++)
192 pci_unmap_page(dev->pdev, ppgtt->pt_dma_addr[i],
193 4096, PCI_DMA_BIDIRECTIONAL);
194 }
195
196 kfree(ppgtt->pt_dma_addr);
197 for (i = 0; i < ppgtt->num_pd_entries; i++)
198 __free_page(ppgtt->pt_pages[i]);
199 kfree(ppgtt->pt_pages);
200 kfree(ppgtt);
201}
202
Daniel Vetter7bddb012012-02-09 17:15:47 +0100203static void i915_ppgtt_insert_sg_entries(struct i915_hw_ppgtt *ppgtt,
Chris Wilson9da3da62012-06-01 15:20:22 +0100204 const struct sg_table *pages,
Daniel Vetter7bddb012012-02-09 17:15:47 +0100205 unsigned first_entry,
Ben Widawskye7210c32012-10-19 09:33:22 -0700206 enum i915_cache_level cache_level)
Daniel Vetter7bddb012012-02-09 17:15:47 +0100207{
Ben Widawsky54d12522012-09-24 16:44:32 -0700208 gtt_pte_t *pt_vaddr;
Daniel Vetter7bddb012012-02-09 17:15:47 +0100209 unsigned act_pd = first_entry / I915_PPGTT_PT_ENTRIES;
210 unsigned first_pte = first_entry % I915_PPGTT_PT_ENTRIES;
211 unsigned i, j, m, segment_len;
212 dma_addr_t page_addr;
213 struct scatterlist *sg;
214
215 /* init sg walking */
Chris Wilson9da3da62012-06-01 15:20:22 +0100216 sg = pages->sgl;
Daniel Vetter7bddb012012-02-09 17:15:47 +0100217 i = 0;
218 segment_len = sg_dma_len(sg) >> PAGE_SHIFT;
219 m = 0;
220
Chris Wilson9da3da62012-06-01 15:20:22 +0100221 while (i < pages->nents) {
Daniel Vetter7bddb012012-02-09 17:15:47 +0100222 pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pd]);
223
224 for (j = first_pte; j < I915_PPGTT_PT_ENTRIES; j++) {
225 page_addr = sg_dma_address(sg) + (m << PAGE_SHIFT);
Ben Widawsky54d12522012-09-24 16:44:32 -0700226 pt_vaddr[j] = pte_encode(ppgtt->dev, page_addr,
Ben Widawskye7210c32012-10-19 09:33:22 -0700227 cache_level);
Daniel Vetter7bddb012012-02-09 17:15:47 +0100228
229 /* grab the next page */
Chris Wilson9da3da62012-06-01 15:20:22 +0100230 if (++m == segment_len) {
231 if (++i == pages->nents)
Daniel Vetter7bddb012012-02-09 17:15:47 +0100232 break;
233
Chris Wilson9da3da62012-06-01 15:20:22 +0100234 sg = sg_next(sg);
Daniel Vetter7bddb012012-02-09 17:15:47 +0100235 segment_len = sg_dma_len(sg) >> PAGE_SHIFT;
236 m = 0;
237 }
238 }
239
240 kunmap_atomic(pt_vaddr);
241
242 first_pte = 0;
243 act_pd++;
244 }
245}
246
Daniel Vetter7bddb012012-02-09 17:15:47 +0100247void i915_ppgtt_bind_object(struct i915_hw_ppgtt *ppgtt,
248 struct drm_i915_gem_object *obj,
249 enum i915_cache_level cache_level)
250{
Chris Wilson9da3da62012-06-01 15:20:22 +0100251 i915_ppgtt_insert_sg_entries(ppgtt,
Chris Wilson2f745ad2012-09-04 21:02:58 +0100252 obj->pages,
Chris Wilson9da3da62012-06-01 15:20:22 +0100253 obj->gtt_space->start >> PAGE_SHIFT,
Ben Widawskye7210c32012-10-19 09:33:22 -0700254 cache_level);
Daniel Vetter7bddb012012-02-09 17:15:47 +0100255}
256
257void i915_ppgtt_unbind_object(struct i915_hw_ppgtt *ppgtt,
258 struct drm_i915_gem_object *obj)
259{
260 i915_ppgtt_clear_range(ppgtt,
261 obj->gtt_space->start >> PAGE_SHIFT,
262 obj->base.size >> PAGE_SHIFT);
263}
264
Chris Wilson93dfb402011-03-29 16:59:50 -0700265/* XXX kill agp_type! */
266static unsigned int cache_level_to_agp_type(struct drm_device *dev,
267 enum i915_cache_level cache_level)
268{
269 switch (cache_level) {
270 case I915_CACHE_LLC_MLC:
Chris Wilson93dfb402011-03-29 16:59:50 -0700271 /* Older chipsets do not have this extra level of CPU
272 * cacheing, so fallthrough and request the PTE simply
273 * as cached.
274 */
Ben Widawsky86936072012-09-21 16:54:14 -0700275 if (INTEL_INFO(dev)->gen >= 6 && !IS_HASWELL(dev))
276 return AGP_USER_CACHED_MEMORY_LLC_MLC;
Chris Wilson93dfb402011-03-29 16:59:50 -0700277 case I915_CACHE_LLC:
278 return AGP_USER_CACHED_MEMORY;
279 default:
280 case I915_CACHE_NONE:
281 return AGP_USER_MEMORY;
282 }
283}
284
Ben Widawsky5c042282011-10-17 15:51:55 -0700285static bool do_idling(struct drm_i915_private *dev_priv)
286{
287 bool ret = dev_priv->mm.interruptible;
288
289 if (unlikely(dev_priv->mm.gtt->do_idle_maps)) {
290 dev_priv->mm.interruptible = false;
Ben Widawskyb2da9fe2012-04-26 16:02:58 -0700291 if (i915_gpu_idle(dev_priv->dev)) {
Ben Widawsky5c042282011-10-17 15:51:55 -0700292 DRM_ERROR("Couldn't idle GPU\n");
293 /* Wait a bit, in hopes it avoids the hang */
294 udelay(10);
295 }
296 }
297
298 return ret;
299}
300
301static void undo_idling(struct drm_i915_private *dev_priv, bool interruptible)
302{
303 if (unlikely(dev_priv->mm.gtt->do_idle_maps))
304 dev_priv->mm.interruptible = interruptible;
305}
306
Daniel Vetter76aaf222010-11-05 22:23:30 +0100307void i915_gem_restore_gtt_mappings(struct drm_device *dev)
308{
309 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson05394f32010-11-08 19:18:58 +0000310 struct drm_i915_gem_object *obj;
Daniel Vetter76aaf222010-11-05 22:23:30 +0100311
Chris Wilsonbee4a182011-01-21 10:54:32 +0000312 /* First fill our portion of the GTT with scratch pages */
313 intel_gtt_clear_range(dev_priv->mm.gtt_start / PAGE_SIZE,
314 (dev_priv->mm.gtt_end - dev_priv->mm.gtt_start) / PAGE_SIZE);
315
Chris Wilson6c085a72012-08-20 11:40:46 +0200316 list_for_each_entry(obj, &dev_priv->mm.bound_list, gtt_list) {
Chris Wilsona8e93122010-12-08 14:28:54 +0000317 i915_gem_clflush_object(obj);
Daniel Vetter74163902012-02-15 23:50:21 +0100318 i915_gem_gtt_bind_object(obj, obj->cache_level);
Daniel Vetter76aaf222010-11-05 22:23:30 +0100319 }
320
Daniel Vetter76aaf222010-11-05 22:23:30 +0100321 intel_gtt_chipset_flush();
322}
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +0100323
Daniel Vetter74163902012-02-15 23:50:21 +0100324int i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +0100325{
Chris Wilson9da3da62012-06-01 15:20:22 +0100326 if (obj->has_dma_mapping)
Daniel Vetter74163902012-02-15 23:50:21 +0100327 return 0;
Chris Wilson9da3da62012-06-01 15:20:22 +0100328
329 if (!dma_map_sg(&obj->base.dev->pdev->dev,
330 obj->pages->sgl, obj->pages->nents,
331 PCI_DMA_BIDIRECTIONAL))
332 return -ENOSPC;
333
334 return 0;
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +0100335}
336
Daniel Vetter74163902012-02-15 23:50:21 +0100337void i915_gem_gtt_bind_object(struct drm_i915_gem_object *obj,
338 enum i915_cache_level cache_level)
Chris Wilsond5bd1442011-04-14 06:48:26 +0100339{
340 struct drm_device *dev = obj->base.dev;
Chris Wilsond5bd1442011-04-14 06:48:26 +0100341 unsigned int agp_type = cache_level_to_agp_type(dev, cache_level);
342
Chris Wilson2f745ad2012-09-04 21:02:58 +0100343 intel_gtt_insert_sg_entries(obj->pages,
Chris Wilson9da3da62012-06-01 15:20:22 +0100344 obj->gtt_space->start >> PAGE_SHIFT,
345 agp_type);
Daniel Vetter74898d72012-02-15 23:50:22 +0100346 obj->has_global_gtt_mapping = 1;
Chris Wilsond5bd1442011-04-14 06:48:26 +0100347}
348
Chris Wilson05394f32010-11-08 19:18:58 +0000349void i915_gem_gtt_unbind_object(struct drm_i915_gem_object *obj)
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +0100350{
Daniel Vetter74163902012-02-15 23:50:21 +0100351 intel_gtt_clear_range(obj->gtt_space->start >> PAGE_SHIFT,
352 obj->base.size >> PAGE_SHIFT);
Daniel Vetter74898d72012-02-15 23:50:22 +0100353
354 obj->has_global_gtt_mapping = 0;
Daniel Vetter74163902012-02-15 23:50:21 +0100355}
356
357void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj)
358{
Ben Widawsky5c042282011-10-17 15:51:55 -0700359 struct drm_device *dev = obj->base.dev;
360 struct drm_i915_private *dev_priv = dev->dev_private;
361 bool interruptible;
362
363 interruptible = do_idling(dev_priv);
364
Chris Wilson9da3da62012-06-01 15:20:22 +0100365 if (!obj->has_dma_mapping)
366 dma_unmap_sg(&dev->pdev->dev,
367 obj->pages->sgl, obj->pages->nents,
368 PCI_DMA_BIDIRECTIONAL);
Ben Widawsky5c042282011-10-17 15:51:55 -0700369
370 undo_idling(dev_priv, interruptible);
Daniel Vetter7c2e6fd2010-11-06 10:10:47 +0100371}
Daniel Vetter644ec022012-03-26 09:45:40 +0200372
Chris Wilson42d6ab42012-07-26 11:49:32 +0100373static void i915_gtt_color_adjust(struct drm_mm_node *node,
374 unsigned long color,
375 unsigned long *start,
376 unsigned long *end)
377{
378 if (node->color != color)
379 *start += 4096;
380
381 if (!list_empty(&node->node_list)) {
382 node = list_entry(node->node_list.next,
383 struct drm_mm_node,
384 node_list);
385 if (node->allocated && node->color != color)
386 *end -= 4096;
387 }
388}
389
Daniel Vetter644ec022012-03-26 09:45:40 +0200390void i915_gem_init_global_gtt(struct drm_device *dev,
391 unsigned long start,
392 unsigned long mappable_end,
393 unsigned long end)
394{
395 drm_i915_private_t *dev_priv = dev->dev_private;
396
Daniel Vetterd1dd20a2012-03-26 09:45:42 +0200397 /* Substract the guard page ... */
398 drm_mm_init(&dev_priv->mm.gtt_space, start, end - start - PAGE_SIZE);
Chris Wilson42d6ab42012-07-26 11:49:32 +0100399 if (!HAS_LLC(dev))
400 dev_priv->mm.gtt_space.color_adjust = i915_gtt_color_adjust;
Daniel Vetter644ec022012-03-26 09:45:40 +0200401
402 dev_priv->mm.gtt_start = start;
403 dev_priv->mm.gtt_mappable_end = mappable_end;
404 dev_priv->mm.gtt_end = end;
405 dev_priv->mm.gtt_total = end - start;
406 dev_priv->mm.mappable_gtt_total = min(end, mappable_end) - start;
407
Daniel Vetterd1dd20a2012-03-26 09:45:42 +0200408 /* ... but ensure that we clear the entire range. */
Daniel Vetter644ec022012-03-26 09:45:40 +0200409 intel_gtt_clear_range(start / PAGE_SIZE, (end-start) / PAGE_SIZE);
410}