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 | e7c2b58 | 2013-04-08 18:43:48 -0700 | [diff] [blame^] | 31 | typedef uint32_t gen6_gtt_pte_t; |
Ben Widawsky | f61c060 | 2012-10-22 11:44:43 -0700 | [diff] [blame] | 32 | |
Ben Widawsky | 26b1ff3 | 2012-11-04 09:21:31 -0800 | [diff] [blame] | 33 | /* PPGTT stuff */ |
| 34 | #define GEN6_GTT_ADDR_ENCODE(addr) ((addr) | (((addr) >> 28) & 0xff0)) |
| 35 | |
| 36 | #define GEN6_PDE_VALID (1 << 0) |
| 37 | /* gen6+ has bit 11-4 for physical addr bit 39-32 */ |
| 38 | #define GEN6_PDE_ADDR_ENCODE(addr) GEN6_GTT_ADDR_ENCODE(addr) |
| 39 | |
| 40 | #define GEN6_PTE_VALID (1 << 0) |
| 41 | #define GEN6_PTE_UNCACHED (1 << 1) |
| 42 | #define HSW_PTE_UNCACHED (0) |
| 43 | #define GEN6_PTE_CACHE_LLC (2 << 1) |
| 44 | #define GEN6_PTE_CACHE_LLC_MLC (3 << 1) |
| 45 | #define GEN6_PTE_ADDR_ENCODE(addr) GEN6_GTT_ADDR_ENCODE(addr) |
| 46 | |
Ben Widawsky | e7c2b58 | 2013-04-08 18:43:48 -0700 | [diff] [blame^] | 47 | static inline gen6_gtt_pte_t gen6_pte_encode(struct drm_device *dev, |
Daniel Vetter | 960e3e4 | 2013-01-24 14:44:57 -0800 | [diff] [blame] | 48 | dma_addr_t addr, |
| 49 | enum i915_cache_level level) |
Ben Widawsky | 54d1252 | 2012-09-24 16:44:32 -0700 | [diff] [blame] | 50 | { |
Ben Widawsky | e7c2b58 | 2013-04-08 18:43:48 -0700 | [diff] [blame^] | 51 | gen6_gtt_pte_t pte = GEN6_PTE_VALID; |
Ben Widawsky | 54d1252 | 2012-09-24 16:44:32 -0700 | [diff] [blame] | 52 | pte |= GEN6_PTE_ADDR_ENCODE(addr); |
Ben Widawsky | e7210c3 | 2012-10-19 09:33:22 -0700 | [diff] [blame] | 53 | |
| 54 | switch (level) { |
| 55 | case I915_CACHE_LLC_MLC: |
| 56 | /* Haswell doesn't set L3 this way */ |
| 57 | if (IS_HASWELL(dev)) |
| 58 | pte |= GEN6_PTE_CACHE_LLC; |
| 59 | else |
| 60 | pte |= GEN6_PTE_CACHE_LLC_MLC; |
| 61 | break; |
| 62 | case I915_CACHE_LLC: |
| 63 | pte |= GEN6_PTE_CACHE_LLC; |
| 64 | break; |
| 65 | case I915_CACHE_NONE: |
| 66 | if (IS_HASWELL(dev)) |
| 67 | pte |= HSW_PTE_UNCACHED; |
| 68 | else |
| 69 | pte |= GEN6_PTE_UNCACHED; |
| 70 | break; |
| 71 | default: |
| 72 | BUG(); |
| 73 | } |
| 74 | |
Ben Widawsky | 54d1252 | 2012-09-24 16:44:32 -0700 | [diff] [blame] | 75 | return pte; |
| 76 | } |
| 77 | |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 78 | /* PPGTT support for Sandybdrige/Gen6 and later */ |
Daniel Vetter | def886c | 2013-01-24 14:44:56 -0800 | [diff] [blame] | 79 | static void gen6_ppgtt_clear_range(struct i915_hw_ppgtt *ppgtt, |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 80 | unsigned first_entry, |
| 81 | unsigned num_entries) |
| 82 | { |
Ben Widawsky | e7c2b58 | 2013-04-08 18:43:48 -0700 | [diff] [blame^] | 83 | gen6_gtt_pte_t *pt_vaddr, scratch_pte; |
Daniel Vetter | a15326a | 2013-03-19 23:48:39 +0100 | [diff] [blame] | 84 | unsigned act_pt = first_entry / I915_PPGTT_PT_ENTRIES; |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 85 | unsigned first_pte = first_entry % I915_PPGTT_PT_ENTRIES; |
| 86 | unsigned last_pte, i; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 87 | |
Daniel Vetter | 960e3e4 | 2013-01-24 14:44:57 -0800 | [diff] [blame] | 88 | scratch_pte = gen6_pte_encode(ppgtt->dev, |
| 89 | ppgtt->scratch_page_dma_addr, |
| 90 | I915_CACHE_LLC); |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 91 | |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 92 | while (num_entries) { |
| 93 | last_pte = first_pte + num_entries; |
| 94 | if (last_pte > I915_PPGTT_PT_ENTRIES) |
| 95 | last_pte = I915_PPGTT_PT_ENTRIES; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 96 | |
Daniel Vetter | a15326a | 2013-03-19 23:48:39 +0100 | [diff] [blame] | 97 | pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]); |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 98 | |
| 99 | for (i = first_pte; i < last_pte; i++) |
| 100 | pt_vaddr[i] = scratch_pte; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 101 | |
| 102 | kunmap_atomic(pt_vaddr); |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 103 | |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 104 | num_entries -= last_pte - first_pte; |
| 105 | first_pte = 0; |
Daniel Vetter | a15326a | 2013-03-19 23:48:39 +0100 | [diff] [blame] | 106 | act_pt++; |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 107 | } |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 108 | } |
| 109 | |
Daniel Vetter | def886c | 2013-01-24 14:44:56 -0800 | [diff] [blame] | 110 | static void gen6_ppgtt_insert_entries(struct i915_hw_ppgtt *ppgtt, |
| 111 | struct sg_table *pages, |
| 112 | unsigned first_entry, |
| 113 | enum i915_cache_level cache_level) |
| 114 | { |
Ben Widawsky | e7c2b58 | 2013-04-08 18:43:48 -0700 | [diff] [blame^] | 115 | gen6_gtt_pte_t *pt_vaddr; |
Daniel Vetter | a15326a | 2013-03-19 23:48:39 +0100 | [diff] [blame] | 116 | unsigned act_pt = first_entry / I915_PPGTT_PT_ENTRIES; |
Imre Deak | 6e995e2 | 2013-02-18 19:28:04 +0200 | [diff] [blame] | 117 | unsigned act_pte = first_entry % I915_PPGTT_PT_ENTRIES; |
| 118 | struct sg_page_iter sg_iter; |
Daniel Vetter | def886c | 2013-01-24 14:44:56 -0800 | [diff] [blame] | 119 | |
Daniel Vetter | a15326a | 2013-03-19 23:48:39 +0100 | [diff] [blame] | 120 | pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]); |
Imre Deak | 6e995e2 | 2013-02-18 19:28:04 +0200 | [diff] [blame] | 121 | for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) { |
| 122 | dma_addr_t page_addr; |
Daniel Vetter | def886c | 2013-01-24 14:44:56 -0800 | [diff] [blame] | 123 | |
Imre Deak | 2db76d7 | 2013-03-26 15:14:18 +0200 | [diff] [blame] | 124 | page_addr = sg_page_iter_dma_address(&sg_iter); |
Imre Deak | 6e995e2 | 2013-02-18 19:28:04 +0200 | [diff] [blame] | 125 | pt_vaddr[act_pte] = gen6_pte_encode(ppgtt->dev, page_addr, |
Daniel Vetter | 6ddc4fc | 2013-03-19 23:37:08 +0100 | [diff] [blame] | 126 | cache_level); |
Imre Deak | 6e995e2 | 2013-02-18 19:28:04 +0200 | [diff] [blame] | 127 | if (++act_pte == I915_PPGTT_PT_ENTRIES) { |
| 128 | kunmap_atomic(pt_vaddr); |
Daniel Vetter | a15326a | 2013-03-19 23:48:39 +0100 | [diff] [blame] | 129 | act_pt++; |
| 130 | pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]); |
Imre Deak | 6e995e2 | 2013-02-18 19:28:04 +0200 | [diff] [blame] | 131 | act_pte = 0; |
Daniel Vetter | def886c | 2013-01-24 14:44:56 -0800 | [diff] [blame] | 132 | |
Daniel Vetter | def886c | 2013-01-24 14:44:56 -0800 | [diff] [blame] | 133 | } |
Daniel Vetter | def886c | 2013-01-24 14:44:56 -0800 | [diff] [blame] | 134 | } |
Imre Deak | 6e995e2 | 2013-02-18 19:28:04 +0200 | [diff] [blame] | 135 | kunmap_atomic(pt_vaddr); |
Daniel Vetter | def886c | 2013-01-24 14:44:56 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Daniel Vetter | 3440d26 | 2013-01-24 13:49:56 -0800 | [diff] [blame] | 138 | static void gen6_ppgtt_cleanup(struct i915_hw_ppgtt *ppgtt) |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 139 | { |
Daniel Vetter | 3440d26 | 2013-01-24 13:49:56 -0800 | [diff] [blame] | 140 | int i; |
| 141 | |
| 142 | if (ppgtt->pt_dma_addr) { |
| 143 | for (i = 0; i < ppgtt->num_pd_entries; i++) |
| 144 | pci_unmap_page(ppgtt->dev->pdev, |
| 145 | ppgtt->pt_dma_addr[i], |
| 146 | 4096, PCI_DMA_BIDIRECTIONAL); |
| 147 | } |
| 148 | |
| 149 | kfree(ppgtt->pt_dma_addr); |
| 150 | for (i = 0; i < ppgtt->num_pd_entries; i++) |
| 151 | __free_page(ppgtt->pt_pages[i]); |
| 152 | kfree(ppgtt->pt_pages); |
| 153 | kfree(ppgtt); |
| 154 | } |
| 155 | |
| 156 | static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt) |
| 157 | { |
| 158 | struct drm_device *dev = ppgtt->dev; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 159 | struct drm_i915_private *dev_priv = dev->dev_private; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 160 | unsigned first_pd_entry_in_global_pt; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 161 | int i; |
| 162 | int ret = -ENOMEM; |
| 163 | |
| 164 | /* ppgtt PDEs reside in the global gtt pagetable, which has 512*1024 |
| 165 | * entries. For aliasing ppgtt support we just steal them at the end for |
| 166 | * now. */ |
Ben Widawsky | a54c0c2 | 2013-01-24 14:45:00 -0800 | [diff] [blame] | 167 | first_pd_entry_in_global_pt = |
| 168 | gtt_total_entries(dev_priv->gtt) - I915_PPGTT_PD_ENTRIES; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 169 | |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 170 | ppgtt->num_pd_entries = I915_PPGTT_PD_ENTRIES; |
Daniel Vetter | def886c | 2013-01-24 14:44:56 -0800 | [diff] [blame] | 171 | ppgtt->clear_range = gen6_ppgtt_clear_range; |
| 172 | ppgtt->insert_entries = gen6_ppgtt_insert_entries; |
Daniel Vetter | 3440d26 | 2013-01-24 13:49:56 -0800 | [diff] [blame] | 173 | ppgtt->cleanup = gen6_ppgtt_cleanup; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 174 | ppgtt->pt_pages = kzalloc(sizeof(struct page *)*ppgtt->num_pd_entries, |
| 175 | GFP_KERNEL); |
| 176 | if (!ppgtt->pt_pages) |
Daniel Vetter | 3440d26 | 2013-01-24 13:49:56 -0800 | [diff] [blame] | 177 | return -ENOMEM; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 178 | |
| 179 | for (i = 0; i < ppgtt->num_pd_entries; i++) { |
| 180 | ppgtt->pt_pages[i] = alloc_page(GFP_KERNEL); |
| 181 | if (!ppgtt->pt_pages[i]) |
| 182 | goto err_pt_alloc; |
| 183 | } |
| 184 | |
Ben Widawsky | 8d2e630 | 2013-01-18 12:30:33 -0800 | [diff] [blame] | 185 | ppgtt->pt_dma_addr = kzalloc(sizeof(dma_addr_t) *ppgtt->num_pd_entries, |
| 186 | GFP_KERNEL); |
| 187 | if (!ppgtt->pt_dma_addr) |
| 188 | goto err_pt_alloc; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 189 | |
Ben Widawsky | 8d2e630 | 2013-01-18 12:30:33 -0800 | [diff] [blame] | 190 | for (i = 0; i < ppgtt->num_pd_entries; i++) { |
| 191 | dma_addr_t pt_addr; |
Daniel Vetter | 211c568 | 2012-04-10 17:29:17 +0200 | [diff] [blame] | 192 | |
Ben Widawsky | 8d2e630 | 2013-01-18 12:30:33 -0800 | [diff] [blame] | 193 | pt_addr = pci_map_page(dev->pdev, ppgtt->pt_pages[i], 0, 4096, |
| 194 | PCI_DMA_BIDIRECTIONAL); |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 195 | |
Ben Widawsky | 8d2e630 | 2013-01-18 12:30:33 -0800 | [diff] [blame] | 196 | if (pci_dma_mapping_error(dev->pdev, pt_addr)) { |
| 197 | ret = -EIO; |
| 198 | goto err_pd_pin; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 199 | |
Daniel Vetter | 211c568 | 2012-04-10 17:29:17 +0200 | [diff] [blame] | 200 | } |
Ben Widawsky | 8d2e630 | 2013-01-18 12:30:33 -0800 | [diff] [blame] | 201 | ppgtt->pt_dma_addr[i] = pt_addr; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 202 | } |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 203 | |
Ben Widawsky | 9c61a32 | 2013-01-18 12:30:32 -0800 | [diff] [blame] | 204 | ppgtt->scratch_page_dma_addr = dev_priv->gtt.scratch_page_dma; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 205 | |
Daniel Vetter | def886c | 2013-01-24 14:44:56 -0800 | [diff] [blame] | 206 | ppgtt->clear_range(ppgtt, 0, |
| 207 | ppgtt->num_pd_entries*I915_PPGTT_PT_ENTRIES); |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 208 | |
Ben Widawsky | e7c2b58 | 2013-04-08 18:43:48 -0700 | [diff] [blame^] | 209 | ppgtt->pd_offset = first_pd_entry_in_global_pt * sizeof(gen6_gtt_pte_t); |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 210 | |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 211 | return 0; |
| 212 | |
| 213 | err_pd_pin: |
| 214 | if (ppgtt->pt_dma_addr) { |
| 215 | for (i--; i >= 0; i--) |
| 216 | pci_unmap_page(dev->pdev, ppgtt->pt_dma_addr[i], |
| 217 | 4096, PCI_DMA_BIDIRECTIONAL); |
| 218 | } |
| 219 | err_pt_alloc: |
| 220 | kfree(ppgtt->pt_dma_addr); |
| 221 | for (i = 0; i < ppgtt->num_pd_entries; i++) { |
| 222 | if (ppgtt->pt_pages[i]) |
| 223 | __free_page(ppgtt->pt_pages[i]); |
| 224 | } |
| 225 | kfree(ppgtt->pt_pages); |
Daniel Vetter | 3440d26 | 2013-01-24 13:49:56 -0800 | [diff] [blame] | 226 | |
| 227 | return ret; |
| 228 | } |
| 229 | |
| 230 | static int i915_gem_init_aliasing_ppgtt(struct drm_device *dev) |
| 231 | { |
| 232 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 233 | struct i915_hw_ppgtt *ppgtt; |
| 234 | int ret; |
| 235 | |
| 236 | ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL); |
| 237 | if (!ppgtt) |
| 238 | return -ENOMEM; |
| 239 | |
| 240 | ppgtt->dev = dev; |
| 241 | |
| 242 | ret = gen6_ppgtt_init(ppgtt); |
| 243 | if (ret) |
| 244 | kfree(ppgtt); |
| 245 | else |
| 246 | dev_priv->mm.aliasing_ppgtt = ppgtt; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 247 | |
| 248 | return ret; |
| 249 | } |
| 250 | |
| 251 | void i915_gem_cleanup_aliasing_ppgtt(struct drm_device *dev) |
| 252 | { |
| 253 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 254 | struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 255 | |
| 256 | if (!ppgtt) |
| 257 | return; |
| 258 | |
Daniel Vetter | 3440d26 | 2013-01-24 13:49:56 -0800 | [diff] [blame] | 259 | ppgtt->cleanup(ppgtt); |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 260 | } |
| 261 | |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 262 | void i915_ppgtt_bind_object(struct i915_hw_ppgtt *ppgtt, |
| 263 | struct drm_i915_gem_object *obj, |
| 264 | enum i915_cache_level cache_level) |
| 265 | { |
Daniel Vetter | def886c | 2013-01-24 14:44:56 -0800 | [diff] [blame] | 266 | ppgtt->insert_entries(ppgtt, obj->pages, |
| 267 | obj->gtt_space->start >> PAGE_SHIFT, |
| 268 | cache_level); |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | void i915_ppgtt_unbind_object(struct i915_hw_ppgtt *ppgtt, |
| 272 | struct drm_i915_gem_object *obj) |
| 273 | { |
Daniel Vetter | def886c | 2013-01-24 14:44:56 -0800 | [diff] [blame] | 274 | ppgtt->clear_range(ppgtt, |
| 275 | obj->gtt_space->start >> PAGE_SHIFT, |
| 276 | obj->base.size >> PAGE_SHIFT); |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 277 | } |
| 278 | |
Ben Widawsky | 26b1ff3 | 2012-11-04 09:21:31 -0800 | [diff] [blame] | 279 | void i915_gem_init_ppgtt(struct drm_device *dev) |
| 280 | { |
| 281 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 282 | uint32_t pd_offset; |
| 283 | struct intel_ring_buffer *ring; |
| 284 | struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt; |
Ben Widawsky | e7c2b58 | 2013-04-08 18:43:48 -0700 | [diff] [blame^] | 285 | gen6_gtt_pte_t __iomem *pd_addr; |
Ben Widawsky | 26b1ff3 | 2012-11-04 09:21:31 -0800 | [diff] [blame] | 286 | uint32_t pd_entry; |
| 287 | int i; |
| 288 | |
| 289 | if (!dev_priv->mm.aliasing_ppgtt) |
| 290 | return; |
| 291 | |
| 292 | |
Ben Widawsky | e7c2b58 | 2013-04-08 18:43:48 -0700 | [diff] [blame^] | 293 | pd_addr = (gen6_gtt_pte_t __iomem *)dev_priv->gtt.gsm + |
| 294 | ppgtt->pd_offset / sizeof(gen6_gtt_pte_t); |
Ben Widawsky | 26b1ff3 | 2012-11-04 09:21:31 -0800 | [diff] [blame] | 295 | for (i = 0; i < ppgtt->num_pd_entries; i++) { |
| 296 | dma_addr_t pt_addr; |
| 297 | |
Ben Widawsky | 8d2e630 | 2013-01-18 12:30:33 -0800 | [diff] [blame] | 298 | pt_addr = ppgtt->pt_dma_addr[i]; |
Ben Widawsky | 26b1ff3 | 2012-11-04 09:21:31 -0800 | [diff] [blame] | 299 | pd_entry = GEN6_PDE_ADDR_ENCODE(pt_addr); |
| 300 | pd_entry |= GEN6_PDE_VALID; |
| 301 | |
| 302 | writel(pd_entry, pd_addr + i); |
| 303 | } |
| 304 | readl(pd_addr); |
| 305 | |
| 306 | pd_offset = ppgtt->pd_offset; |
| 307 | pd_offset /= 64; /* in cachelines, */ |
| 308 | pd_offset <<= 16; |
| 309 | |
| 310 | if (INTEL_INFO(dev)->gen == 6) { |
| 311 | uint32_t ecochk, gab_ctl, ecobits; |
| 312 | |
| 313 | ecobits = I915_READ(GAC_ECO_BITS); |
| 314 | I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B); |
| 315 | |
| 316 | gab_ctl = I915_READ(GAB_CTL); |
| 317 | I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT); |
| 318 | |
| 319 | ecochk = I915_READ(GAM_ECOCHK); |
| 320 | I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | |
| 321 | ECOCHK_PPGTT_CACHE64B); |
| 322 | I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE)); |
| 323 | } else if (INTEL_INFO(dev)->gen >= 7) { |
| 324 | I915_WRITE(GAM_ECOCHK, ECOCHK_PPGTT_CACHE64B); |
| 325 | /* GFX_MODE is per-ring on gen7+ */ |
| 326 | } |
| 327 | |
| 328 | for_each_ring(ring, dev_priv, i) { |
| 329 | if (INTEL_INFO(dev)->gen >= 7) |
| 330 | I915_WRITE(RING_MODE_GEN7(ring), |
| 331 | _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE)); |
| 332 | |
| 333 | I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G); |
| 334 | I915_WRITE(RING_PP_DIR_BASE(ring), pd_offset); |
| 335 | } |
| 336 | } |
| 337 | |
Ben Widawsky | a81cc00 | 2013-01-18 12:30:31 -0800 | [diff] [blame] | 338 | extern int intel_iommu_gfx_mapped; |
| 339 | /* Certain Gen5 chipsets require require idling the GPU before |
| 340 | * unmapping anything from the GTT when VT-d is enabled. |
| 341 | */ |
| 342 | static inline bool needs_idle_maps(struct drm_device *dev) |
| 343 | { |
| 344 | #ifdef CONFIG_INTEL_IOMMU |
| 345 | /* Query intel_iommu to see if we need the workaround. Presumably that |
| 346 | * was loaded first. |
| 347 | */ |
| 348 | if (IS_GEN5(dev) && IS_MOBILE(dev) && intel_iommu_gfx_mapped) |
| 349 | return true; |
| 350 | #endif |
| 351 | return false; |
| 352 | } |
| 353 | |
Ben Widawsky | 5c04228 | 2011-10-17 15:51:55 -0700 | [diff] [blame] | 354 | static bool do_idling(struct drm_i915_private *dev_priv) |
| 355 | { |
| 356 | bool ret = dev_priv->mm.interruptible; |
| 357 | |
Ben Widawsky | a81cc00 | 2013-01-18 12:30:31 -0800 | [diff] [blame] | 358 | if (unlikely(dev_priv->gtt.do_idle_maps)) { |
Ben Widawsky | 5c04228 | 2011-10-17 15:51:55 -0700 | [diff] [blame] | 359 | dev_priv->mm.interruptible = false; |
Ben Widawsky | b2da9fe | 2012-04-26 16:02:58 -0700 | [diff] [blame] | 360 | if (i915_gpu_idle(dev_priv->dev)) { |
Ben Widawsky | 5c04228 | 2011-10-17 15:51:55 -0700 | [diff] [blame] | 361 | DRM_ERROR("Couldn't idle GPU\n"); |
| 362 | /* Wait a bit, in hopes it avoids the hang */ |
| 363 | udelay(10); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | return ret; |
| 368 | } |
| 369 | |
| 370 | static void undo_idling(struct drm_i915_private *dev_priv, bool interruptible) |
| 371 | { |
Ben Widawsky | a81cc00 | 2013-01-18 12:30:31 -0800 | [diff] [blame] | 372 | if (unlikely(dev_priv->gtt.do_idle_maps)) |
Ben Widawsky | 5c04228 | 2011-10-17 15:51:55 -0700 | [diff] [blame] | 373 | dev_priv->mm.interruptible = interruptible; |
| 374 | } |
| 375 | |
Daniel Vetter | 76aaf22 | 2010-11-05 22:23:30 +0100 | [diff] [blame] | 376 | void i915_gem_restore_gtt_mappings(struct drm_device *dev) |
| 377 | { |
| 378 | struct drm_i915_private *dev_priv = dev->dev_private; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 379 | struct drm_i915_gem_object *obj; |
Daniel Vetter | 76aaf22 | 2010-11-05 22:23:30 +0100 | [diff] [blame] | 380 | |
Chris Wilson | bee4a18 | 2011-01-21 10:54:32 +0000 | [diff] [blame] | 381 | /* First fill our portion of the GTT with scratch pages */ |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 382 | dev_priv->gtt.gtt_clear_range(dev, dev_priv->gtt.start / PAGE_SIZE, |
| 383 | dev_priv->gtt.total / PAGE_SIZE); |
Chris Wilson | bee4a18 | 2011-01-21 10:54:32 +0000 | [diff] [blame] | 384 | |
Chris Wilson | 6c085a7 | 2012-08-20 11:40:46 +0200 | [diff] [blame] | 385 | list_for_each_entry(obj, &dev_priv->mm.bound_list, gtt_list) { |
Chris Wilson | a8e9312 | 2010-12-08 14:28:54 +0000 | [diff] [blame] | 386 | i915_gem_clflush_object(obj); |
Daniel Vetter | 7416390 | 2012-02-15 23:50:21 +0100 | [diff] [blame] | 387 | i915_gem_gtt_bind_object(obj, obj->cache_level); |
Daniel Vetter | 76aaf22 | 2010-11-05 22:23:30 +0100 | [diff] [blame] | 388 | } |
| 389 | |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 390 | i915_gem_chipset_flush(dev); |
Daniel Vetter | 76aaf22 | 2010-11-05 22:23:30 +0100 | [diff] [blame] | 391 | } |
Daniel Vetter | 7c2e6fd | 2010-11-06 10:10:47 +0100 | [diff] [blame] | 392 | |
Daniel Vetter | 7416390 | 2012-02-15 23:50:21 +0100 | [diff] [blame] | 393 | int i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj) |
Daniel Vetter | 7c2e6fd | 2010-11-06 10:10:47 +0100 | [diff] [blame] | 394 | { |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 395 | if (obj->has_dma_mapping) |
Daniel Vetter | 7416390 | 2012-02-15 23:50:21 +0100 | [diff] [blame] | 396 | return 0; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 397 | |
| 398 | if (!dma_map_sg(&obj->base.dev->pdev->dev, |
| 399 | obj->pages->sgl, obj->pages->nents, |
| 400 | PCI_DMA_BIDIRECTIONAL)) |
| 401 | return -ENOSPC; |
| 402 | |
| 403 | return 0; |
Daniel Vetter | 7c2e6fd | 2010-11-06 10:10:47 +0100 | [diff] [blame] | 404 | } |
| 405 | |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 406 | /* |
| 407 | * Binds an object into the global gtt with the specified cache level. The object |
| 408 | * will be accessible to the GPU via commands whose operands reference offsets |
| 409 | * within the global GTT as well as accessible by the GPU through the GMADR |
| 410 | * mapped BAR (dev_priv->mm.gtt->gtt). |
| 411 | */ |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 412 | static void gen6_ggtt_insert_entries(struct drm_device *dev, |
| 413 | struct sg_table *st, |
| 414 | unsigned int first_entry, |
| 415 | enum i915_cache_level level) |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 416 | { |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 417 | struct drm_i915_private *dev_priv = dev->dev_private; |
Ben Widawsky | e7c2b58 | 2013-04-08 18:43:48 -0700 | [diff] [blame^] | 418 | gen6_gtt_pte_t __iomem *gtt_entries = |
| 419 | (gen6_gtt_pte_t __iomem *)dev_priv->gtt.gsm + first_entry; |
Imre Deak | 6e995e2 | 2013-02-18 19:28:04 +0200 | [diff] [blame] | 420 | int i = 0; |
| 421 | struct sg_page_iter sg_iter; |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 422 | dma_addr_t addr; |
| 423 | |
Imre Deak | 6e995e2 | 2013-02-18 19:28:04 +0200 | [diff] [blame] | 424 | for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) { |
Imre Deak | 2db76d7 | 2013-03-26 15:14:18 +0200 | [diff] [blame] | 425 | addr = sg_page_iter_dma_address(&sg_iter); |
Imre Deak | 6e995e2 | 2013-02-18 19:28:04 +0200 | [diff] [blame] | 426 | iowrite32(gen6_pte_encode(dev, addr, level), >t_entries[i]); |
| 427 | i++; |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 428 | } |
| 429 | |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 430 | /* XXX: This serves as a posting read to make sure that the PTE has |
| 431 | * actually been updated. There is some concern that even though |
| 432 | * registers and PTEs are within the same BAR that they are potentially |
| 433 | * of NUMA access patterns. Therefore, even with the way we assume |
| 434 | * hardware should work, we must keep this posting read for paranoia. |
| 435 | */ |
| 436 | if (i != 0) |
Daniel Vetter | 960e3e4 | 2013-01-24 14:44:57 -0800 | [diff] [blame] | 437 | WARN_ON(readl(>t_entries[i-1]) |
| 438 | != gen6_pte_encode(dev, addr, level)); |
Ben Widawsky | 0f9b91c | 2012-11-04 09:21:30 -0800 | [diff] [blame] | 439 | |
| 440 | /* This next bit makes the above posting read even more important. We |
| 441 | * want to flush the TLBs only after we're certain all the PTE updates |
| 442 | * have finished. |
| 443 | */ |
| 444 | I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN); |
| 445 | POSTING_READ(GFX_FLSH_CNTL_GEN6); |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 446 | } |
| 447 | |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 448 | static void gen6_ggtt_clear_range(struct drm_device *dev, |
| 449 | unsigned int first_entry, |
| 450 | unsigned int num_entries) |
| 451 | { |
| 452 | struct drm_i915_private *dev_priv = dev->dev_private; |
Ben Widawsky | e7c2b58 | 2013-04-08 18:43:48 -0700 | [diff] [blame^] | 453 | gen6_gtt_pte_t scratch_pte, __iomem *gtt_base = |
| 454 | (gen6_gtt_pte_t __iomem *) dev_priv->gtt.gsm + first_entry; |
Ben Widawsky | a54c0c2 | 2013-01-24 14:45:00 -0800 | [diff] [blame] | 455 | const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry; |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 456 | int i; |
| 457 | |
| 458 | if (WARN(num_entries > max_entries, |
| 459 | "First entry = %d; Num entries = %d (max=%d)\n", |
| 460 | first_entry, num_entries, max_entries)) |
| 461 | num_entries = max_entries; |
| 462 | |
Daniel Vetter | 960e3e4 | 2013-01-24 14:44:57 -0800 | [diff] [blame] | 463 | scratch_pte = gen6_pte_encode(dev, dev_priv->gtt.scratch_page_dma, |
| 464 | I915_CACHE_LLC); |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 465 | for (i = 0; i < num_entries; i++) |
| 466 | iowrite32(scratch_pte, >t_base[i]); |
| 467 | readl(gtt_base); |
| 468 | } |
| 469 | |
| 470 | |
| 471 | static void i915_ggtt_insert_entries(struct drm_device *dev, |
| 472 | struct sg_table *st, |
| 473 | unsigned int pg_start, |
| 474 | enum i915_cache_level cache_level) |
| 475 | { |
| 476 | unsigned int flags = (cache_level == I915_CACHE_NONE) ? |
| 477 | AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY; |
| 478 | |
| 479 | intel_gtt_insert_sg_entries(st, pg_start, flags); |
| 480 | |
| 481 | } |
| 482 | |
| 483 | static void i915_ggtt_clear_range(struct drm_device *dev, |
| 484 | unsigned int first_entry, |
| 485 | unsigned int num_entries) |
| 486 | { |
| 487 | intel_gtt_clear_range(first_entry, num_entries); |
| 488 | } |
| 489 | |
| 490 | |
Daniel Vetter | 7416390 | 2012-02-15 23:50:21 +0100 | [diff] [blame] | 491 | void i915_gem_gtt_bind_object(struct drm_i915_gem_object *obj, |
| 492 | enum i915_cache_level cache_level) |
Chris Wilson | d5bd144 | 2011-04-14 06:48:26 +0100 | [diff] [blame] | 493 | { |
| 494 | struct drm_device *dev = obj->base.dev; |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 495 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 496 | |
| 497 | dev_priv->gtt.gtt_insert_entries(dev, obj->pages, |
| 498 | obj->gtt_space->start >> PAGE_SHIFT, |
| 499 | cache_level); |
Chris Wilson | d5bd144 | 2011-04-14 06:48:26 +0100 | [diff] [blame] | 500 | |
Daniel Vetter | 74898d7 | 2012-02-15 23:50:22 +0100 | [diff] [blame] | 501 | obj->has_global_gtt_mapping = 1; |
Chris Wilson | d5bd144 | 2011-04-14 06:48:26 +0100 | [diff] [blame] | 502 | } |
| 503 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 504 | void i915_gem_gtt_unbind_object(struct drm_i915_gem_object *obj) |
Daniel Vetter | 7c2e6fd | 2010-11-06 10:10:47 +0100 | [diff] [blame] | 505 | { |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 506 | struct drm_device *dev = obj->base.dev; |
| 507 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 508 | |
| 509 | dev_priv->gtt.gtt_clear_range(obj->base.dev, |
| 510 | obj->gtt_space->start >> PAGE_SHIFT, |
| 511 | obj->base.size >> PAGE_SHIFT); |
Daniel Vetter | 74898d7 | 2012-02-15 23:50:22 +0100 | [diff] [blame] | 512 | |
| 513 | obj->has_global_gtt_mapping = 0; |
Daniel Vetter | 7416390 | 2012-02-15 23:50:21 +0100 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj) |
| 517 | { |
Ben Widawsky | 5c04228 | 2011-10-17 15:51:55 -0700 | [diff] [blame] | 518 | struct drm_device *dev = obj->base.dev; |
| 519 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 520 | bool interruptible; |
| 521 | |
| 522 | interruptible = do_idling(dev_priv); |
| 523 | |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 524 | if (!obj->has_dma_mapping) |
| 525 | dma_unmap_sg(&dev->pdev->dev, |
| 526 | obj->pages->sgl, obj->pages->nents, |
| 527 | PCI_DMA_BIDIRECTIONAL); |
Ben Widawsky | 5c04228 | 2011-10-17 15:51:55 -0700 | [diff] [blame] | 528 | |
| 529 | undo_idling(dev_priv, interruptible); |
Daniel Vetter | 7c2e6fd | 2010-11-06 10:10:47 +0100 | [diff] [blame] | 530 | } |
Daniel Vetter | 644ec02 | 2012-03-26 09:45:40 +0200 | [diff] [blame] | 531 | |
Chris Wilson | 42d6ab4 | 2012-07-26 11:49:32 +0100 | [diff] [blame] | 532 | static void i915_gtt_color_adjust(struct drm_mm_node *node, |
| 533 | unsigned long color, |
| 534 | unsigned long *start, |
| 535 | unsigned long *end) |
| 536 | { |
| 537 | if (node->color != color) |
| 538 | *start += 4096; |
| 539 | |
| 540 | if (!list_empty(&node->node_list)) { |
| 541 | node = list_entry(node->node_list.next, |
| 542 | struct drm_mm_node, |
| 543 | node_list); |
| 544 | if (node->allocated && node->color != color) |
| 545 | *end -= 4096; |
| 546 | } |
| 547 | } |
Ben Widawsky | d7e5008 | 2012-12-18 10:31:25 -0800 | [diff] [blame] | 548 | void i915_gem_setup_global_gtt(struct drm_device *dev, |
| 549 | unsigned long start, |
| 550 | unsigned long mappable_end, |
| 551 | unsigned long end) |
Daniel Vetter | 644ec02 | 2012-03-26 09:45:40 +0200 | [diff] [blame] | 552 | { |
Ben Widawsky | e78891c | 2013-01-25 16:41:04 -0800 | [diff] [blame] | 553 | /* Let GEM Manage all of the aperture. |
| 554 | * |
| 555 | * However, leave one page at the end still bound to the scratch page. |
| 556 | * There are a number of places where the hardware apparently prefetches |
| 557 | * past the end of the object, and we've seen multiple hangs with the |
| 558 | * GPU head pointer stuck in a batchbuffer bound at the last page of the |
| 559 | * aperture. One page should be enough to keep any prefetching inside |
| 560 | * of the aperture. |
| 561 | */ |
Daniel Vetter | 644ec02 | 2012-03-26 09:45:40 +0200 | [diff] [blame] | 562 | drm_i915_private_t *dev_priv = dev->dev_private; |
Chris Wilson | ed2f345 | 2012-11-15 11:32:19 +0000 | [diff] [blame] | 563 | struct drm_mm_node *entry; |
| 564 | struct drm_i915_gem_object *obj; |
| 565 | unsigned long hole_start, hole_end; |
Daniel Vetter | 644ec02 | 2012-03-26 09:45:40 +0200 | [diff] [blame] | 566 | |
Ben Widawsky | 35451cb | 2013-01-17 12:45:13 -0800 | [diff] [blame] | 567 | BUG_ON(mappable_end > end); |
| 568 | |
Chris Wilson | ed2f345 | 2012-11-15 11:32:19 +0000 | [diff] [blame] | 569 | /* Subtract the guard page ... */ |
Daniel Vetter | d1dd20a | 2012-03-26 09:45:42 +0200 | [diff] [blame] | 570 | 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] | 571 | if (!HAS_LLC(dev)) |
| 572 | dev_priv->mm.gtt_space.color_adjust = i915_gtt_color_adjust; |
Daniel Vetter | 644ec02 | 2012-03-26 09:45:40 +0200 | [diff] [blame] | 573 | |
Chris Wilson | ed2f345 | 2012-11-15 11:32:19 +0000 | [diff] [blame] | 574 | /* Mark any preallocated objects as occupied */ |
| 575 | list_for_each_entry(obj, &dev_priv->mm.bound_list, gtt_list) { |
| 576 | DRM_DEBUG_KMS("reserving preallocated space: %x + %zx\n", |
| 577 | obj->gtt_offset, obj->base.size); |
| 578 | |
| 579 | BUG_ON(obj->gtt_space != I915_GTT_RESERVED); |
| 580 | obj->gtt_space = drm_mm_create_block(&dev_priv->mm.gtt_space, |
| 581 | obj->gtt_offset, |
| 582 | obj->base.size, |
| 583 | false); |
| 584 | obj->has_global_gtt_mapping = 1; |
| 585 | } |
| 586 | |
Ben Widawsky | 5d4545a | 2013-01-17 12:45:15 -0800 | [diff] [blame] | 587 | dev_priv->gtt.start = start; |
Ben Widawsky | 5d4545a | 2013-01-17 12:45:15 -0800 | [diff] [blame] | 588 | dev_priv->gtt.total = end - start; |
Daniel Vetter | 644ec02 | 2012-03-26 09:45:40 +0200 | [diff] [blame] | 589 | |
Chris Wilson | ed2f345 | 2012-11-15 11:32:19 +0000 | [diff] [blame] | 590 | /* Clear any non-preallocated blocks */ |
| 591 | drm_mm_for_each_hole(entry, &dev_priv->mm.gtt_space, |
| 592 | hole_start, hole_end) { |
| 593 | DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n", |
| 594 | hole_start, hole_end); |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 595 | dev_priv->gtt.gtt_clear_range(dev, hole_start / PAGE_SIZE, |
| 596 | (hole_end-hole_start) / PAGE_SIZE); |
Chris Wilson | ed2f345 | 2012-11-15 11:32:19 +0000 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | /* And finally clear the reserved guard page */ |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 600 | dev_priv->gtt.gtt_clear_range(dev, end / PAGE_SIZE - 1, 1); |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 601 | } |
| 602 | |
Ben Widawsky | d7e5008 | 2012-12-18 10:31:25 -0800 | [diff] [blame] | 603 | static bool |
| 604 | intel_enable_ppgtt(struct drm_device *dev) |
| 605 | { |
| 606 | if (i915_enable_ppgtt >= 0) |
| 607 | return i915_enable_ppgtt; |
| 608 | |
| 609 | #ifdef CONFIG_INTEL_IOMMU |
| 610 | /* Disable ppgtt on SNB if VT-d is on. */ |
| 611 | if (INTEL_INFO(dev)->gen == 6 && intel_iommu_gfx_mapped) |
| 612 | return false; |
| 613 | #endif |
| 614 | |
| 615 | return true; |
| 616 | } |
| 617 | |
| 618 | void i915_gem_init_global_gtt(struct drm_device *dev) |
| 619 | { |
| 620 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 621 | unsigned long gtt_size, mappable_size; |
Ben Widawsky | d7e5008 | 2012-12-18 10:31:25 -0800 | [diff] [blame] | 622 | |
Ben Widawsky | a54c0c2 | 2013-01-24 14:45:00 -0800 | [diff] [blame] | 623 | gtt_size = dev_priv->gtt.total; |
Ben Widawsky | 93d1879 | 2013-01-17 12:45:17 -0800 | [diff] [blame] | 624 | mappable_size = dev_priv->gtt.mappable_end; |
Ben Widawsky | d7e5008 | 2012-12-18 10:31:25 -0800 | [diff] [blame] | 625 | |
| 626 | if (intel_enable_ppgtt(dev) && HAS_ALIASING_PPGTT(dev)) { |
Ben Widawsky | e78891c | 2013-01-25 16:41:04 -0800 | [diff] [blame] | 627 | int ret; |
Ben Widawsky | d7e5008 | 2012-12-18 10:31:25 -0800 | [diff] [blame] | 628 | /* PPGTT pdes are stolen from global gtt ptes, so shrink the |
| 629 | * aperture accordingly when using aliasing ppgtt. */ |
| 630 | gtt_size -= I915_PPGTT_PD_ENTRIES*PAGE_SIZE; |
| 631 | |
| 632 | i915_gem_setup_global_gtt(dev, 0, mappable_size, gtt_size); |
| 633 | |
| 634 | ret = i915_gem_init_aliasing_ppgtt(dev); |
Ben Widawsky | e78891c | 2013-01-25 16:41:04 -0800 | [diff] [blame] | 635 | if (!ret) |
Ben Widawsky | d7e5008 | 2012-12-18 10:31:25 -0800 | [diff] [blame] | 636 | return; |
Ben Widawsky | e78891c | 2013-01-25 16:41:04 -0800 | [diff] [blame] | 637 | |
| 638 | DRM_ERROR("Aliased PPGTT setup failed %d\n", ret); |
| 639 | drm_mm_takedown(&dev_priv->mm.gtt_space); |
| 640 | gtt_size += I915_PPGTT_PD_ENTRIES*PAGE_SIZE; |
Ben Widawsky | d7e5008 | 2012-12-18 10:31:25 -0800 | [diff] [blame] | 641 | } |
Ben Widawsky | e78891c | 2013-01-25 16:41:04 -0800 | [diff] [blame] | 642 | i915_gem_setup_global_gtt(dev, 0, mappable_size, gtt_size); |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | static int setup_scratch_page(struct drm_device *dev) |
| 646 | { |
| 647 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 648 | struct page *page; |
| 649 | dma_addr_t dma_addr; |
| 650 | |
| 651 | page = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO); |
| 652 | if (page == NULL) |
| 653 | return -ENOMEM; |
| 654 | get_page(page); |
| 655 | set_pages_uc(page, 1); |
| 656 | |
| 657 | #ifdef CONFIG_INTEL_IOMMU |
| 658 | dma_addr = pci_map_page(dev->pdev, page, 0, PAGE_SIZE, |
| 659 | PCI_DMA_BIDIRECTIONAL); |
| 660 | if (pci_dma_mapping_error(dev->pdev, dma_addr)) |
| 661 | return -EINVAL; |
| 662 | #else |
| 663 | dma_addr = page_to_phys(page); |
| 664 | #endif |
Ben Widawsky | 9c61a32 | 2013-01-18 12:30:32 -0800 | [diff] [blame] | 665 | dev_priv->gtt.scratch_page = page; |
| 666 | dev_priv->gtt.scratch_page_dma = dma_addr; |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 667 | |
| 668 | return 0; |
| 669 | } |
| 670 | |
| 671 | static void teardown_scratch_page(struct drm_device *dev) |
| 672 | { |
| 673 | struct drm_i915_private *dev_priv = dev->dev_private; |
Ben Widawsky | 9c61a32 | 2013-01-18 12:30:32 -0800 | [diff] [blame] | 674 | set_pages_wb(dev_priv->gtt.scratch_page, 1); |
| 675 | pci_unmap_page(dev->pdev, dev_priv->gtt.scratch_page_dma, |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 676 | PAGE_SIZE, PCI_DMA_BIDIRECTIONAL); |
Ben Widawsky | 9c61a32 | 2013-01-18 12:30:32 -0800 | [diff] [blame] | 677 | put_page(dev_priv->gtt.scratch_page); |
| 678 | __free_page(dev_priv->gtt.scratch_page); |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | static inline unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl) |
| 682 | { |
| 683 | snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT; |
| 684 | snb_gmch_ctl &= SNB_GMCH_GGMS_MASK; |
| 685 | return snb_gmch_ctl << 20; |
| 686 | } |
| 687 | |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 688 | static inline size_t gen6_get_stolen_size(u16 snb_gmch_ctl) |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 689 | { |
| 690 | snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT; |
| 691 | snb_gmch_ctl &= SNB_GMCH_GMS_MASK; |
| 692 | return snb_gmch_ctl << 25; /* 32 MB units */ |
| 693 | } |
| 694 | |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 695 | static inline size_t gen7_get_stolen_size(u16 snb_gmch_ctl) |
Ben Widawsky | 03752f5 | 2012-11-04 09:21:28 -0800 | [diff] [blame] | 696 | { |
| 697 | static const int stolen_decoder[] = { |
| 698 | 0, 0, 0, 0, 0, 32, 48, 64, 128, 256, 96, 160, 224, 352}; |
| 699 | snb_gmch_ctl >>= IVB_GMCH_GMS_SHIFT; |
| 700 | snb_gmch_ctl &= IVB_GMCH_GMS_MASK; |
| 701 | return stolen_decoder[snb_gmch_ctl] << 20; |
| 702 | } |
| 703 | |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 704 | static int gen6_gmch_probe(struct drm_device *dev, |
| 705 | size_t *gtt_total, |
Ben Widawsky | 41907dd | 2013-02-08 11:32:47 -0800 | [diff] [blame] | 706 | size_t *stolen, |
| 707 | phys_addr_t *mappable_base, |
| 708 | unsigned long *mappable_end) |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 709 | { |
| 710 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 711 | phys_addr_t gtt_bus_addr; |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 712 | unsigned int gtt_size; |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 713 | u16 snb_gmch_ctl; |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 714 | int ret; |
| 715 | |
Ben Widawsky | 41907dd | 2013-02-08 11:32:47 -0800 | [diff] [blame] | 716 | *mappable_base = pci_resource_start(dev->pdev, 2); |
| 717 | *mappable_end = pci_resource_len(dev->pdev, 2); |
| 718 | |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 719 | /* 64/512MB is the current min/max we actually know of, but this is just |
| 720 | * a coarse sanity check. |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 721 | */ |
Ben Widawsky | 41907dd | 2013-02-08 11:32:47 -0800 | [diff] [blame] | 722 | if ((*mappable_end < (64<<20) || (*mappable_end > (512<<20)))) { |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 723 | DRM_ERROR("Unknown GMADR size (%lx)\n", |
| 724 | dev_priv->gtt.mappable_end); |
| 725 | return -ENXIO; |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 726 | } |
| 727 | |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 728 | if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(40))) |
| 729 | pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(40)); |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 730 | pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl); |
| 731 | gtt_size = gen6_get_total_gtt_size(snb_gmch_ctl); |
| 732 | |
Jesse Barnes | 086ddcc | 2013-03-01 14:08:29 -0800 | [diff] [blame] | 733 | if (IS_GEN7(dev) && !IS_VALLEYVIEW(dev)) |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 734 | *stolen = gen7_get_stolen_size(snb_gmch_ctl); |
| 735 | else |
| 736 | *stolen = gen6_get_stolen_size(snb_gmch_ctl); |
| 737 | |
Ben Widawsky | e7c2b58 | 2013-04-08 18:43:48 -0700 | [diff] [blame^] | 738 | *gtt_total = (gtt_size / sizeof(gen6_gtt_pte_t)) << PAGE_SHIFT; |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 739 | |
Ben Widawsky | a93e416 | 2013-04-08 18:43:47 -0700 | [diff] [blame] | 740 | /* For Modern GENs the PTEs and register space are split in the BAR */ |
| 741 | gtt_bus_addr = pci_resource_start(dev->pdev, 0) + |
| 742 | (pci_resource_len(dev->pdev, 0) / 2); |
| 743 | |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 744 | dev_priv->gtt.gsm = ioremap_wc(gtt_bus_addr, gtt_size); |
| 745 | if (!dev_priv->gtt.gsm) { |
| 746 | DRM_ERROR("Failed to map the gtt page table\n"); |
| 747 | return -ENOMEM; |
| 748 | } |
| 749 | |
| 750 | ret = setup_scratch_page(dev); |
| 751 | if (ret) |
| 752 | DRM_ERROR("Scratch setup failed\n"); |
| 753 | |
| 754 | dev_priv->gtt.gtt_clear_range = gen6_ggtt_clear_range; |
| 755 | dev_priv->gtt.gtt_insert_entries = gen6_ggtt_insert_entries; |
| 756 | |
| 757 | return ret; |
| 758 | } |
| 759 | |
Changlong Xie | d93c623 | 2013-01-31 11:32:50 +0800 | [diff] [blame] | 760 | static void gen6_gmch_remove(struct drm_device *dev) |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 761 | { |
| 762 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 763 | iounmap(dev_priv->gtt.gsm); |
| 764 | teardown_scratch_page(dev_priv->dev); |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | static int i915_gmch_probe(struct drm_device *dev, |
| 768 | size_t *gtt_total, |
Ben Widawsky | 41907dd | 2013-02-08 11:32:47 -0800 | [diff] [blame] | 769 | size_t *stolen, |
| 770 | phys_addr_t *mappable_base, |
| 771 | unsigned long *mappable_end) |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 772 | { |
| 773 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 774 | int ret; |
| 775 | |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 776 | ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->dev->pdev, NULL); |
| 777 | if (!ret) { |
| 778 | DRM_ERROR("failed to set up gmch\n"); |
| 779 | return -EIO; |
| 780 | } |
| 781 | |
Ben Widawsky | 41907dd | 2013-02-08 11:32:47 -0800 | [diff] [blame] | 782 | intel_gtt_get(gtt_total, stolen, mappable_base, mappable_end); |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 783 | |
| 784 | dev_priv->gtt.do_idle_maps = needs_idle_maps(dev_priv->dev); |
| 785 | dev_priv->gtt.gtt_clear_range = i915_ggtt_clear_range; |
| 786 | dev_priv->gtt.gtt_insert_entries = i915_ggtt_insert_entries; |
| 787 | |
| 788 | return 0; |
| 789 | } |
| 790 | |
| 791 | static void i915_gmch_remove(struct drm_device *dev) |
| 792 | { |
| 793 | intel_gmch_remove(); |
| 794 | } |
| 795 | |
| 796 | int i915_gem_gtt_init(struct drm_device *dev) |
| 797 | { |
| 798 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 799 | struct i915_gtt *gtt = &dev_priv->gtt; |
| 800 | unsigned long gtt_size; |
| 801 | int ret; |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 802 | |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 803 | if (INTEL_INFO(dev)->gen <= 5) { |
| 804 | dev_priv->gtt.gtt_probe = i915_gmch_probe; |
| 805 | dev_priv->gtt.gtt_remove = i915_gmch_remove; |
| 806 | } else { |
| 807 | dev_priv->gtt.gtt_probe = gen6_gmch_probe; |
| 808 | dev_priv->gtt.gtt_remove = gen6_gmch_remove; |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 809 | } |
| 810 | |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 811 | ret = dev_priv->gtt.gtt_probe(dev, &dev_priv->gtt.total, |
Ben Widawsky | 41907dd | 2013-02-08 11:32:47 -0800 | [diff] [blame] | 812 | &dev_priv->gtt.stolen_size, |
| 813 | >t->mappable_base, |
| 814 | >t->mappable_end); |
Ben Widawsky | a54c0c2 | 2013-01-24 14:45:00 -0800 | [diff] [blame] | 815 | if (ret) |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 816 | return ret; |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 817 | |
Ben Widawsky | e7c2b58 | 2013-04-08 18:43:48 -0700 | [diff] [blame^] | 818 | gtt_size = (dev_priv->gtt.total >> PAGE_SHIFT) * sizeof(gen6_gtt_pte_t); |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 819 | |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 820 | /* GMADR is the PCI mmio aperture into the global GTT. */ |
| 821 | DRM_INFO("Memory usable by graphics device = %zdM\n", |
| 822 | dev_priv->gtt.total >> 20); |
| 823 | DRM_DEBUG_DRIVER("GMADR size = %ldM\n", |
| 824 | dev_priv->gtt.mappable_end >> 20); |
| 825 | DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", |
| 826 | dev_priv->gtt.stolen_size >> 20); |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 827 | |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 828 | return 0; |
Daniel Vetter | 644ec02 | 2012-03-26 09:45:40 +0200 | [diff] [blame] | 829 | } |