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