Daniel Vetter | 76aaf22 | 2010-11-05 22:23:30 +0100 | [diff] [blame] | 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 | |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 25 | #include <drm/drmP.h> |
| 26 | #include <drm/i915_drm.h> |
Daniel Vetter | 76aaf22 | 2010-11-05 22:23:30 +0100 | [diff] [blame] | 27 | #include "i915_drv.h" |
| 28 | #include "i915_trace.h" |
| 29 | #include "intel_drv.h" |
| 30 | |
Ben Widawsky | f61c060 | 2012-10-22 11:44:43 -0700 | [diff] [blame] | 31 | typedef uint32_t gtt_pte_t; |
| 32 | |
Ben Widawsky | 54d1252 | 2012-09-24 16:44:32 -0700 | [diff] [blame^] | 33 | static inline gtt_pte_t pte_encode(struct drm_device *dev, |
| 34 | dma_addr_t addr, |
| 35 | gtt_pte_t cache_bits) |
| 36 | { |
| 37 | gtt_pte_t pte = GEN6_PTE_VALID; |
| 38 | pte |= GEN6_PTE_ADDR_ENCODE(addr); |
| 39 | pte |= cache_bits; |
| 40 | |
| 41 | return pte; |
| 42 | } |
| 43 | |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 44 | /* PPGTT support for Sandybdrige/Gen6 and later */ |
| 45 | static void i915_ppgtt_clear_range(struct i915_hw_ppgtt *ppgtt, |
| 46 | unsigned first_entry, |
| 47 | unsigned num_entries) |
| 48 | { |
Ben Widawsky | f61c060 | 2012-10-22 11:44:43 -0700 | [diff] [blame] | 49 | gtt_pte_t *pt_vaddr; |
| 50 | gtt_pte_t scratch_pte; |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 51 | unsigned act_pd = first_entry / I915_PPGTT_PT_ENTRIES; |
| 52 | unsigned first_pte = first_entry % I915_PPGTT_PT_ENTRIES; |
| 53 | unsigned last_pte, i; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 54 | |
Ben Widawsky | 54d1252 | 2012-09-24 16:44:32 -0700 | [diff] [blame^] | 55 | scratch_pte = pte_encode(ppgtt->dev, ppgtt->scratch_page_dma_addr, |
| 56 | GEN6_PTE_CACHE_LLC); |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 57 | |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 58 | while (num_entries) { |
| 59 | last_pte = first_pte + num_entries; |
| 60 | if (last_pte > I915_PPGTT_PT_ENTRIES) |
| 61 | last_pte = I915_PPGTT_PT_ENTRIES; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 62 | |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 63 | pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pd]); |
| 64 | |
| 65 | for (i = first_pte; i < last_pte; i++) |
| 66 | pt_vaddr[i] = scratch_pte; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 67 | |
| 68 | kunmap_atomic(pt_vaddr); |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 69 | |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 70 | num_entries -= last_pte - first_pte; |
| 71 | first_pte = 0; |
| 72 | act_pd++; |
| 73 | } |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | int i915_gem_init_aliasing_ppgtt(struct drm_device *dev) |
| 77 | { |
| 78 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 79 | struct i915_hw_ppgtt *ppgtt; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 80 | unsigned first_pd_entry_in_global_pt; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 81 | int i; |
| 82 | int ret = -ENOMEM; |
| 83 | |
| 84 | /* ppgtt PDEs reside in the global gtt pagetable, which has 512*1024 |
| 85 | * entries. For aliasing ppgtt support we just steal them at the end for |
| 86 | * now. */ |
Chris Wilson | 9a0f938 | 2012-08-24 09:12:22 +0100 | [diff] [blame] | 87 | first_pd_entry_in_global_pt = dev_priv->mm.gtt->gtt_total_entries - I915_PPGTT_PD_ENTRIES; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 88 | |
| 89 | ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL); |
| 90 | if (!ppgtt) |
| 91 | return ret; |
| 92 | |
Ben Widawsky | 8f2c59f | 2012-09-24 08:55:51 -0700 | [diff] [blame] | 93 | ppgtt->dev = dev; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 94 | ppgtt->num_pd_entries = I915_PPGTT_PD_ENTRIES; |
| 95 | ppgtt->pt_pages = kzalloc(sizeof(struct page *)*ppgtt->num_pd_entries, |
| 96 | GFP_KERNEL); |
| 97 | if (!ppgtt->pt_pages) |
| 98 | goto err_ppgtt; |
| 99 | |
| 100 | for (i = 0; i < ppgtt->num_pd_entries; i++) { |
| 101 | ppgtt->pt_pages[i] = alloc_page(GFP_KERNEL); |
| 102 | if (!ppgtt->pt_pages[i]) |
| 103 | goto err_pt_alloc; |
| 104 | } |
| 105 | |
| 106 | if (dev_priv->mm.gtt->needs_dmar) { |
| 107 | ppgtt->pt_dma_addr = kzalloc(sizeof(dma_addr_t) |
| 108 | *ppgtt->num_pd_entries, |
| 109 | GFP_KERNEL); |
| 110 | if (!ppgtt->pt_dma_addr) |
| 111 | goto err_pt_alloc; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 112 | |
Daniel Vetter | 211c568 | 2012-04-10 17:29:17 +0200 | [diff] [blame] | 113 | for (i = 0; i < ppgtt->num_pd_entries; i++) { |
| 114 | dma_addr_t pt_addr; |
| 115 | |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 116 | pt_addr = pci_map_page(dev->pdev, ppgtt->pt_pages[i], |
| 117 | 0, 4096, |
| 118 | PCI_DMA_BIDIRECTIONAL); |
| 119 | |
| 120 | if (pci_dma_mapping_error(dev->pdev, |
| 121 | pt_addr)) { |
| 122 | ret = -EIO; |
| 123 | goto err_pd_pin; |
| 124 | |
| 125 | } |
| 126 | ppgtt->pt_dma_addr[i] = pt_addr; |
Daniel Vetter | 211c568 | 2012-04-10 17:29:17 +0200 | [diff] [blame] | 127 | } |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 128 | } |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 129 | |
| 130 | ppgtt->scratch_page_dma_addr = dev_priv->mm.gtt->scratch_page_dma; |
| 131 | |
| 132 | i915_ppgtt_clear_range(ppgtt, 0, |
| 133 | ppgtt->num_pd_entries*I915_PPGTT_PT_ENTRIES); |
| 134 | |
Ben Widawsky | f61c060 | 2012-10-22 11:44:43 -0700 | [diff] [blame] | 135 | ppgtt->pd_offset = (first_pd_entry_in_global_pt)*sizeof(gtt_pte_t); |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 136 | |
| 137 | dev_priv->mm.aliasing_ppgtt = ppgtt; |
| 138 | |
| 139 | return 0; |
| 140 | |
| 141 | err_pd_pin: |
| 142 | if (ppgtt->pt_dma_addr) { |
| 143 | for (i--; i >= 0; i--) |
| 144 | pci_unmap_page(dev->pdev, ppgtt->pt_dma_addr[i], |
| 145 | 4096, PCI_DMA_BIDIRECTIONAL); |
| 146 | } |
| 147 | err_pt_alloc: |
| 148 | kfree(ppgtt->pt_dma_addr); |
| 149 | for (i = 0; i < ppgtt->num_pd_entries; i++) { |
| 150 | if (ppgtt->pt_pages[i]) |
| 151 | __free_page(ppgtt->pt_pages[i]); |
| 152 | } |
| 153 | kfree(ppgtt->pt_pages); |
| 154 | err_ppgtt: |
| 155 | kfree(ppgtt); |
| 156 | |
| 157 | return ret; |
| 158 | } |
| 159 | |
| 160 | void i915_gem_cleanup_aliasing_ppgtt(struct drm_device *dev) |
| 161 | { |
| 162 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 163 | struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt; |
| 164 | int i; |
| 165 | |
| 166 | if (!ppgtt) |
| 167 | return; |
| 168 | |
| 169 | if (ppgtt->pt_dma_addr) { |
| 170 | for (i = 0; i < ppgtt->num_pd_entries; i++) |
| 171 | pci_unmap_page(dev->pdev, ppgtt->pt_dma_addr[i], |
| 172 | 4096, PCI_DMA_BIDIRECTIONAL); |
| 173 | } |
| 174 | |
| 175 | kfree(ppgtt->pt_dma_addr); |
| 176 | for (i = 0; i < ppgtt->num_pd_entries; i++) |
| 177 | __free_page(ppgtt->pt_pages[i]); |
| 178 | kfree(ppgtt->pt_pages); |
| 179 | kfree(ppgtt); |
| 180 | } |
| 181 | |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 182 | static void i915_ppgtt_insert_sg_entries(struct i915_hw_ppgtt *ppgtt, |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 183 | const struct sg_table *pages, |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 184 | unsigned first_entry, |
Ben Widawsky | f61c060 | 2012-10-22 11:44:43 -0700 | [diff] [blame] | 185 | gtt_pte_t pte_flags) |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 186 | { |
Ben Widawsky | 54d1252 | 2012-09-24 16:44:32 -0700 | [diff] [blame^] | 187 | gtt_pte_t *pt_vaddr; |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 188 | unsigned act_pd = first_entry / I915_PPGTT_PT_ENTRIES; |
| 189 | unsigned first_pte = first_entry % I915_PPGTT_PT_ENTRIES; |
| 190 | unsigned i, j, m, segment_len; |
| 191 | dma_addr_t page_addr; |
| 192 | struct scatterlist *sg; |
| 193 | |
| 194 | /* init sg walking */ |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 195 | sg = pages->sgl; |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 196 | i = 0; |
| 197 | segment_len = sg_dma_len(sg) >> PAGE_SHIFT; |
| 198 | m = 0; |
| 199 | |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 200 | while (i < pages->nents) { |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 201 | pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pd]); |
| 202 | |
| 203 | for (j = first_pte; j < I915_PPGTT_PT_ENTRIES; j++) { |
| 204 | page_addr = sg_dma_address(sg) + (m << PAGE_SHIFT); |
Ben Widawsky | 54d1252 | 2012-09-24 16:44:32 -0700 | [diff] [blame^] | 205 | pt_vaddr[j] = pte_encode(ppgtt->dev, page_addr, |
| 206 | pte_flags); |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 207 | |
| 208 | /* grab the next page */ |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 209 | if (++m == segment_len) { |
| 210 | if (++i == pages->nents) |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 211 | break; |
| 212 | |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 213 | sg = sg_next(sg); |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 214 | segment_len = sg_dma_len(sg) >> PAGE_SHIFT; |
| 215 | m = 0; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | kunmap_atomic(pt_vaddr); |
| 220 | |
| 221 | first_pte = 0; |
| 222 | act_pd++; |
| 223 | } |
| 224 | } |
| 225 | |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 226 | void i915_ppgtt_bind_object(struct i915_hw_ppgtt *ppgtt, |
| 227 | struct drm_i915_gem_object *obj, |
| 228 | enum i915_cache_level cache_level) |
| 229 | { |
Ben Widawsky | f61c060 | 2012-10-22 11:44:43 -0700 | [diff] [blame] | 230 | gtt_pte_t pte_flags = GEN6_PTE_VALID; |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 231 | |
| 232 | switch (cache_level) { |
| 233 | case I915_CACHE_LLC_MLC: |
Ben Widawsky | 8693607 | 2012-09-21 16:54:14 -0700 | [diff] [blame] | 234 | /* Haswell doesn't set L3 this way */ |
Ben Widawsky | 8f2c59f | 2012-09-24 08:55:51 -0700 | [diff] [blame] | 235 | if (IS_HASWELL(ppgtt->dev)) |
Ben Widawsky | 8693607 | 2012-09-21 16:54:14 -0700 | [diff] [blame] | 236 | pte_flags |= GEN6_PTE_CACHE_LLC; |
| 237 | else |
| 238 | pte_flags |= GEN6_PTE_CACHE_LLC_MLC; |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 239 | break; |
| 240 | case I915_CACHE_LLC: |
| 241 | pte_flags |= GEN6_PTE_CACHE_LLC; |
| 242 | break; |
| 243 | case I915_CACHE_NONE: |
Ben Widawsky | 8f2c59f | 2012-09-24 08:55:51 -0700 | [diff] [blame] | 244 | if (IS_HASWELL(ppgtt->dev)) |
Daniel Vetter | a843af1 | 2012-08-14 11:42:14 -0300 | [diff] [blame] | 245 | pte_flags |= HSW_PTE_UNCACHED; |
| 246 | else |
| 247 | pte_flags |= GEN6_PTE_UNCACHED; |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 248 | break; |
| 249 | default: |
| 250 | BUG(); |
| 251 | } |
| 252 | |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 253 | i915_ppgtt_insert_sg_entries(ppgtt, |
Chris Wilson | 2f745ad | 2012-09-04 21:02:58 +0100 | [diff] [blame] | 254 | obj->pages, |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 255 | obj->gtt_space->start >> PAGE_SHIFT, |
| 256 | pte_flags); |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | void i915_ppgtt_unbind_object(struct i915_hw_ppgtt *ppgtt, |
| 260 | struct drm_i915_gem_object *obj) |
| 261 | { |
| 262 | i915_ppgtt_clear_range(ppgtt, |
| 263 | obj->gtt_space->start >> PAGE_SHIFT, |
| 264 | obj->base.size >> PAGE_SHIFT); |
| 265 | } |
| 266 | |
Chris Wilson | 93dfb40 | 2011-03-29 16:59:50 -0700 | [diff] [blame] | 267 | /* XXX kill agp_type! */ |
| 268 | static unsigned int cache_level_to_agp_type(struct drm_device *dev, |
| 269 | enum i915_cache_level cache_level) |
| 270 | { |
| 271 | switch (cache_level) { |
| 272 | case I915_CACHE_LLC_MLC: |
Chris Wilson | 93dfb40 | 2011-03-29 16:59:50 -0700 | [diff] [blame] | 273 | /* Older chipsets do not have this extra level of CPU |
| 274 | * cacheing, so fallthrough and request the PTE simply |
| 275 | * as cached. |
| 276 | */ |
Ben Widawsky | 8693607 | 2012-09-21 16:54:14 -0700 | [diff] [blame] | 277 | if (INTEL_INFO(dev)->gen >= 6 && !IS_HASWELL(dev)) |
| 278 | return AGP_USER_CACHED_MEMORY_LLC_MLC; |
Chris Wilson | 93dfb40 | 2011-03-29 16:59:50 -0700 | [diff] [blame] | 279 | case I915_CACHE_LLC: |
| 280 | return AGP_USER_CACHED_MEMORY; |
| 281 | default: |
| 282 | case I915_CACHE_NONE: |
| 283 | return AGP_USER_MEMORY; |
| 284 | } |
| 285 | } |
| 286 | |
Ben Widawsky | 5c04228 | 2011-10-17 15:51:55 -0700 | [diff] [blame] | 287 | static bool do_idling(struct drm_i915_private *dev_priv) |
| 288 | { |
| 289 | bool ret = dev_priv->mm.interruptible; |
| 290 | |
| 291 | if (unlikely(dev_priv->mm.gtt->do_idle_maps)) { |
| 292 | dev_priv->mm.interruptible = false; |
Ben Widawsky | b2da9fe | 2012-04-26 16:02:58 -0700 | [diff] [blame] | 293 | if (i915_gpu_idle(dev_priv->dev)) { |
Ben Widawsky | 5c04228 | 2011-10-17 15:51:55 -0700 | [diff] [blame] | 294 | DRM_ERROR("Couldn't idle GPU\n"); |
| 295 | /* Wait a bit, in hopes it avoids the hang */ |
| 296 | udelay(10); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | return ret; |
| 301 | } |
| 302 | |
| 303 | static void undo_idling(struct drm_i915_private *dev_priv, bool interruptible) |
| 304 | { |
| 305 | if (unlikely(dev_priv->mm.gtt->do_idle_maps)) |
| 306 | dev_priv->mm.interruptible = interruptible; |
| 307 | } |
| 308 | |
Daniel Vetter | 76aaf22 | 2010-11-05 22:23:30 +0100 | [diff] [blame] | 309 | void i915_gem_restore_gtt_mappings(struct drm_device *dev) |
| 310 | { |
| 311 | struct drm_i915_private *dev_priv = dev->dev_private; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 312 | struct drm_i915_gem_object *obj; |
Daniel Vetter | 76aaf22 | 2010-11-05 22:23:30 +0100 | [diff] [blame] | 313 | |
Chris Wilson | bee4a18 | 2011-01-21 10:54:32 +0000 | [diff] [blame] | 314 | /* First fill our portion of the GTT with scratch pages */ |
| 315 | intel_gtt_clear_range(dev_priv->mm.gtt_start / PAGE_SIZE, |
| 316 | (dev_priv->mm.gtt_end - dev_priv->mm.gtt_start) / PAGE_SIZE); |
| 317 | |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 318 | list_for_each_entry(obj, &dev_priv->mm.bound_list, gtt_list) { |
Chris Wilson | a8e9312 | 2010-12-08 14:28:54 +0000 | [diff] [blame] | 319 | i915_gem_clflush_object(obj); |
Daniel Vetter | 7416390 | 2012-02-15 23:50:21 +0100 | [diff] [blame] | 320 | i915_gem_gtt_bind_object(obj, obj->cache_level); |
Daniel Vetter | 76aaf22 | 2010-11-05 22:23:30 +0100 | [diff] [blame] | 321 | } |
| 322 | |
Daniel Vetter | 76aaf22 | 2010-11-05 22:23:30 +0100 | [diff] [blame] | 323 | intel_gtt_chipset_flush(); |
| 324 | } |
Daniel Vetter | 7c2e6fd | 2010-11-06 10:10:47 +0100 | [diff] [blame] | 325 | |
Daniel Vetter | 7416390 | 2012-02-15 23:50:21 +0100 | [diff] [blame] | 326 | int i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj) |
Daniel Vetter | 7c2e6fd | 2010-11-06 10:10:47 +0100 | [diff] [blame] | 327 | { |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 328 | if (obj->has_dma_mapping) |
Daniel Vetter | 7416390 | 2012-02-15 23:50:21 +0100 | [diff] [blame] | 329 | return 0; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 330 | |
| 331 | if (!dma_map_sg(&obj->base.dev->pdev->dev, |
| 332 | obj->pages->sgl, obj->pages->nents, |
| 333 | PCI_DMA_BIDIRECTIONAL)) |
| 334 | return -ENOSPC; |
| 335 | |
| 336 | return 0; |
Daniel Vetter | 7c2e6fd | 2010-11-06 10:10:47 +0100 | [diff] [blame] | 337 | } |
| 338 | |
Daniel Vetter | 7416390 | 2012-02-15 23:50:21 +0100 | [diff] [blame] | 339 | void i915_gem_gtt_bind_object(struct drm_i915_gem_object *obj, |
| 340 | enum i915_cache_level cache_level) |
Chris Wilson | d5bd144 | 2011-04-14 06:48:26 +0100 | [diff] [blame] | 341 | { |
| 342 | struct drm_device *dev = obj->base.dev; |
Chris Wilson | d5bd144 | 2011-04-14 06:48:26 +0100 | [diff] [blame] | 343 | unsigned int agp_type = cache_level_to_agp_type(dev, cache_level); |
| 344 | |
Chris Wilson | 2f745ad | 2012-09-04 21:02:58 +0100 | [diff] [blame] | 345 | intel_gtt_insert_sg_entries(obj->pages, |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 346 | obj->gtt_space->start >> PAGE_SHIFT, |
| 347 | agp_type); |
Daniel Vetter | 74898d7 | 2012-02-15 23:50:22 +0100 | [diff] [blame] | 348 | obj->has_global_gtt_mapping = 1; |
Chris Wilson | d5bd144 | 2011-04-14 06:48:26 +0100 | [diff] [blame] | 349 | } |
| 350 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 351 | void i915_gem_gtt_unbind_object(struct drm_i915_gem_object *obj) |
Daniel Vetter | 7c2e6fd | 2010-11-06 10:10:47 +0100 | [diff] [blame] | 352 | { |
Daniel Vetter | 7416390 | 2012-02-15 23:50:21 +0100 | [diff] [blame] | 353 | intel_gtt_clear_range(obj->gtt_space->start >> PAGE_SHIFT, |
| 354 | obj->base.size >> PAGE_SHIFT); |
Daniel Vetter | 74898d7 | 2012-02-15 23:50:22 +0100 | [diff] [blame] | 355 | |
| 356 | obj->has_global_gtt_mapping = 0; |
Daniel Vetter | 7416390 | 2012-02-15 23:50:21 +0100 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj) |
| 360 | { |
Ben Widawsky | 5c04228 | 2011-10-17 15:51:55 -0700 | [diff] [blame] | 361 | struct drm_device *dev = obj->base.dev; |
| 362 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 363 | bool interruptible; |
| 364 | |
| 365 | interruptible = do_idling(dev_priv); |
| 366 | |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 367 | if (!obj->has_dma_mapping) |
| 368 | dma_unmap_sg(&dev->pdev->dev, |
| 369 | obj->pages->sgl, obj->pages->nents, |
| 370 | PCI_DMA_BIDIRECTIONAL); |
Ben Widawsky | 5c04228 | 2011-10-17 15:51:55 -0700 | [diff] [blame] | 371 | |
| 372 | undo_idling(dev_priv, interruptible); |
Daniel Vetter | 7c2e6fd | 2010-11-06 10:10:47 +0100 | [diff] [blame] | 373 | } |
Daniel Vetter | 644ec02 | 2012-03-26 09:45:40 +0200 | [diff] [blame] | 374 | |
Chris Wilson | 42d6ab4 | 2012-07-26 11:49:32 +0100 | [diff] [blame] | 375 | static void i915_gtt_color_adjust(struct drm_mm_node *node, |
| 376 | unsigned long color, |
| 377 | unsigned long *start, |
| 378 | unsigned long *end) |
| 379 | { |
| 380 | if (node->color != color) |
| 381 | *start += 4096; |
| 382 | |
| 383 | if (!list_empty(&node->node_list)) { |
| 384 | node = list_entry(node->node_list.next, |
| 385 | struct drm_mm_node, |
| 386 | node_list); |
| 387 | if (node->allocated && node->color != color) |
| 388 | *end -= 4096; |
| 389 | } |
| 390 | } |
| 391 | |
Daniel Vetter | 644ec02 | 2012-03-26 09:45:40 +0200 | [diff] [blame] | 392 | void i915_gem_init_global_gtt(struct drm_device *dev, |
| 393 | unsigned long start, |
| 394 | unsigned long mappable_end, |
| 395 | unsigned long end) |
| 396 | { |
| 397 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 398 | |
Daniel Vetter | d1dd20a | 2012-03-26 09:45:42 +0200 | [diff] [blame] | 399 | /* Substract the guard page ... */ |
| 400 | drm_mm_init(&dev_priv->mm.gtt_space, start, end - start - PAGE_SIZE); |
Chris Wilson | 42d6ab4 | 2012-07-26 11:49:32 +0100 | [diff] [blame] | 401 | if (!HAS_LLC(dev)) |
| 402 | dev_priv->mm.gtt_space.color_adjust = i915_gtt_color_adjust; |
Daniel Vetter | 644ec02 | 2012-03-26 09:45:40 +0200 | [diff] [blame] | 403 | |
| 404 | dev_priv->mm.gtt_start = start; |
| 405 | dev_priv->mm.gtt_mappable_end = mappable_end; |
| 406 | dev_priv->mm.gtt_end = end; |
| 407 | dev_priv->mm.gtt_total = end - start; |
| 408 | dev_priv->mm.mappable_gtt_total = min(end, mappable_end) - start; |
| 409 | |
Daniel Vetter | d1dd20a | 2012-03-26 09:45:42 +0200 | [diff] [blame] | 410 | /* ... but ensure that we clear the entire range. */ |
Daniel Vetter | 644ec02 | 2012-03-26 09:45:40 +0200 | [diff] [blame] | 411 | intel_gtt_clear_range(start / PAGE_SIZE, (end-start) / PAGE_SIZE); |
| 412 | } |