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)) |
Ben Widawsky | d31eb10 | 2013-11-02 21:07:17 -0700 | [diff] [blame] | 33 | typedef uint64_t gen8_gtt_pte_t; |
Ben Widawsky | 6670a5a | 2013-06-27 16:30:04 -0700 | [diff] [blame] | 34 | |
Ben Widawsky | 26b1ff3 | 2012-11-04 09:21:31 -0800 | [diff] [blame] | 35 | /* PPGTT stuff */ |
| 36 | #define GEN6_GTT_ADDR_ENCODE(addr) ((addr) | (((addr) >> 28) & 0xff0)) |
Ben Widawsky | 0d8ff15 | 2013-07-04 11:02:03 -0700 | [diff] [blame] | 37 | #define HSW_GTT_ADDR_ENCODE(addr) ((addr) | (((addr) >> 28) & 0x7f0)) |
Ben Widawsky | 26b1ff3 | 2012-11-04 09:21:31 -0800 | [diff] [blame] | 38 | |
| 39 | #define GEN6_PDE_VALID (1 << 0) |
| 40 | /* gen6+ has bit 11-4 for physical addr bit 39-32 */ |
| 41 | #define GEN6_PDE_ADDR_ENCODE(addr) GEN6_GTT_ADDR_ENCODE(addr) |
| 42 | |
| 43 | #define GEN6_PTE_VALID (1 << 0) |
| 44 | #define GEN6_PTE_UNCACHED (1 << 1) |
| 45 | #define HSW_PTE_UNCACHED (0) |
| 46 | #define GEN6_PTE_CACHE_LLC (2 << 1) |
Chris Wilson | 350ec88 | 2013-08-06 13:17:02 +0100 | [diff] [blame] | 47 | #define GEN7_PTE_CACHE_L3_LLC (3 << 1) |
Ben Widawsky | 26b1ff3 | 2012-11-04 09:21:31 -0800 | [diff] [blame] | 48 | #define GEN6_PTE_ADDR_ENCODE(addr) GEN6_GTT_ADDR_ENCODE(addr) |
Ben Widawsky | 0d8ff15 | 2013-07-04 11:02:03 -0700 | [diff] [blame] | 49 | #define HSW_PTE_ADDR_ENCODE(addr) HSW_GTT_ADDR_ENCODE(addr) |
| 50 | |
| 51 | /* Cacheability Control is a 4-bit value. The low three bits are stored in * |
| 52 | * bits 3:1 of the PTE, while the fourth bit is stored in bit 11 of the PTE. |
| 53 | */ |
| 54 | #define HSW_CACHEABILITY_CONTROL(bits) ((((bits) & 0x7) << 1) | \ |
| 55 | (((bits) & 0x8) << (11 - 3))) |
Ben Widawsky | 87a6b68 | 2013-08-04 23:47:29 -0700 | [diff] [blame] | 56 | #define HSW_WB_LLC_AGE3 HSW_CACHEABILITY_CONTROL(0x2) |
Ben Widawsky | 0d8ff15 | 2013-07-04 11:02:03 -0700 | [diff] [blame] | 57 | #define HSW_WB_LLC_AGE0 HSW_CACHEABILITY_CONTROL(0x3) |
Ben Widawsky | 4d15c14 | 2013-07-04 11:02:06 -0700 | [diff] [blame] | 58 | #define HSW_WB_ELLC_LLC_AGE0 HSW_CACHEABILITY_CONTROL(0xb) |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 59 | #define HSW_WT_ELLC_LLC_AGE0 HSW_CACHEABILITY_CONTROL(0x6) |
Ben Widawsky | 26b1ff3 | 2012-11-04 09:21:31 -0800 | [diff] [blame] | 60 | |
Ben Widawsky | fbe5d36 | 2013-11-04 19:56:49 -0800 | [diff] [blame^] | 61 | #define PPAT_UNCACHED_INDEX (_PAGE_PWT | _PAGE_PCD) |
| 62 | #define PPAT_CACHED_PDE_INDEX 0 /* WB LLC */ |
| 63 | #define PPAT_CACHED_INDEX _PAGE_PAT /* WB LLCeLLC */ |
| 64 | #define PPAT_DISPLAY_ELLC_INDEX _PAGE_PCD /* WT eLLC */ |
| 65 | |
Ben Widawsky | 94ec8f6 | 2013-11-02 21:07:18 -0700 | [diff] [blame] | 66 | static inline gen8_gtt_pte_t gen8_pte_encode(dma_addr_t addr, |
| 67 | enum i915_cache_level level, |
| 68 | bool valid) |
| 69 | { |
| 70 | gen8_gtt_pte_t pte = valid ? _PAGE_PRESENT | _PAGE_RW : 0; |
| 71 | pte |= addr; |
Ben Widawsky | fbe5d36 | 2013-11-04 19:56:49 -0800 | [diff] [blame^] | 72 | if (level != I915_CACHE_NONE) |
| 73 | pte |= PPAT_CACHED_INDEX; |
| 74 | else |
| 75 | pte |= PPAT_UNCACHED_INDEX; |
Ben Widawsky | 94ec8f6 | 2013-11-02 21:07:18 -0700 | [diff] [blame] | 76 | return pte; |
| 77 | } |
| 78 | |
Chris Wilson | 350ec88 | 2013-08-06 13:17:02 +0100 | [diff] [blame] | 79 | static gen6_gtt_pte_t snb_pte_encode(dma_addr_t addr, |
Ben Widawsky | b35b380 | 2013-10-16 09:18:21 -0700 | [diff] [blame] | 80 | enum i915_cache_level level, |
| 81 | bool valid) |
Ben Widawsky | 54d1252 | 2012-09-24 16:44:32 -0700 | [diff] [blame] | 82 | { |
Ben Widawsky | b35b380 | 2013-10-16 09:18:21 -0700 | [diff] [blame] | 83 | gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0; |
Ben Widawsky | 54d1252 | 2012-09-24 16:44:32 -0700 | [diff] [blame] | 84 | pte |= GEN6_PTE_ADDR_ENCODE(addr); |
Ben Widawsky | e7210c3 | 2012-10-19 09:33:22 -0700 | [diff] [blame] | 85 | |
| 86 | switch (level) { |
Chris Wilson | 350ec88 | 2013-08-06 13:17:02 +0100 | [diff] [blame] | 87 | case I915_CACHE_L3_LLC: |
| 88 | case I915_CACHE_LLC: |
| 89 | pte |= GEN6_PTE_CACHE_LLC; |
| 90 | break; |
| 91 | case I915_CACHE_NONE: |
| 92 | pte |= GEN6_PTE_UNCACHED; |
| 93 | break; |
| 94 | default: |
| 95 | WARN_ON(1); |
| 96 | } |
| 97 | |
| 98 | return pte; |
| 99 | } |
| 100 | |
| 101 | static gen6_gtt_pte_t ivb_pte_encode(dma_addr_t addr, |
Ben Widawsky | b35b380 | 2013-10-16 09:18:21 -0700 | [diff] [blame] | 102 | enum i915_cache_level level, |
| 103 | bool valid) |
Chris Wilson | 350ec88 | 2013-08-06 13:17:02 +0100 | [diff] [blame] | 104 | { |
Ben Widawsky | b35b380 | 2013-10-16 09:18:21 -0700 | [diff] [blame] | 105 | gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0; |
Chris Wilson | 350ec88 | 2013-08-06 13:17:02 +0100 | [diff] [blame] | 106 | pte |= GEN6_PTE_ADDR_ENCODE(addr); |
| 107 | |
| 108 | switch (level) { |
| 109 | case I915_CACHE_L3_LLC: |
| 110 | pte |= GEN7_PTE_CACHE_L3_LLC; |
Ben Widawsky | e7210c3 | 2012-10-19 09:33:22 -0700 | [diff] [blame] | 111 | break; |
| 112 | case I915_CACHE_LLC: |
| 113 | pte |= GEN6_PTE_CACHE_LLC; |
| 114 | break; |
| 115 | case I915_CACHE_NONE: |
Kenneth Graunke | 9119708 | 2013-04-22 00:53:51 -0700 | [diff] [blame] | 116 | pte |= GEN6_PTE_UNCACHED; |
Ben Widawsky | e7210c3 | 2012-10-19 09:33:22 -0700 | [diff] [blame] | 117 | break; |
| 118 | default: |
Chris Wilson | 350ec88 | 2013-08-06 13:17:02 +0100 | [diff] [blame] | 119 | WARN_ON(1); |
Ben Widawsky | e7210c3 | 2012-10-19 09:33:22 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Ben Widawsky | 54d1252 | 2012-09-24 16:44:32 -0700 | [diff] [blame] | 122 | return pte; |
| 123 | } |
| 124 | |
Kenneth Graunke | 93c34e7 | 2013-04-22 00:53:50 -0700 | [diff] [blame] | 125 | #define BYT_PTE_WRITEABLE (1 << 1) |
| 126 | #define BYT_PTE_SNOOPED_BY_CPU_CACHES (1 << 2) |
| 127 | |
Ben Widawsky | 80a74f7 | 2013-06-27 16:30:19 -0700 | [diff] [blame] | 128 | static gen6_gtt_pte_t byt_pte_encode(dma_addr_t addr, |
Ben Widawsky | b35b380 | 2013-10-16 09:18:21 -0700 | [diff] [blame] | 129 | enum i915_cache_level level, |
| 130 | bool valid) |
Kenneth Graunke | 93c34e7 | 2013-04-22 00:53:50 -0700 | [diff] [blame] | 131 | { |
Ben Widawsky | b35b380 | 2013-10-16 09:18:21 -0700 | [diff] [blame] | 132 | gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0; |
Kenneth Graunke | 93c34e7 | 2013-04-22 00:53:50 -0700 | [diff] [blame] | 133 | pte |= GEN6_PTE_ADDR_ENCODE(addr); |
| 134 | |
| 135 | /* Mark the page as writeable. Other platforms don't have a |
| 136 | * setting for read-only/writable, so this matches that behavior. |
| 137 | */ |
| 138 | pte |= BYT_PTE_WRITEABLE; |
| 139 | |
| 140 | if (level != I915_CACHE_NONE) |
| 141 | pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES; |
| 142 | |
| 143 | return pte; |
| 144 | } |
| 145 | |
Ben Widawsky | 80a74f7 | 2013-06-27 16:30:19 -0700 | [diff] [blame] | 146 | static gen6_gtt_pte_t hsw_pte_encode(dma_addr_t addr, |
Ben Widawsky | b35b380 | 2013-10-16 09:18:21 -0700 | [diff] [blame] | 147 | enum i915_cache_level level, |
| 148 | bool valid) |
Kenneth Graunke | 9119708 | 2013-04-22 00:53:51 -0700 | [diff] [blame] | 149 | { |
Ben Widawsky | b35b380 | 2013-10-16 09:18:21 -0700 | [diff] [blame] | 150 | gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0; |
Ben Widawsky | 0d8ff15 | 2013-07-04 11:02:03 -0700 | [diff] [blame] | 151 | pte |= HSW_PTE_ADDR_ENCODE(addr); |
Kenneth Graunke | 9119708 | 2013-04-22 00:53:51 -0700 | [diff] [blame] | 152 | |
| 153 | if (level != I915_CACHE_NONE) |
Ben Widawsky | 87a6b68 | 2013-08-04 23:47:29 -0700 | [diff] [blame] | 154 | pte |= HSW_WB_LLC_AGE3; |
Kenneth Graunke | 9119708 | 2013-04-22 00:53:51 -0700 | [diff] [blame] | 155 | |
| 156 | return pte; |
| 157 | } |
| 158 | |
Ben Widawsky | 4d15c14 | 2013-07-04 11:02:06 -0700 | [diff] [blame] | 159 | static gen6_gtt_pte_t iris_pte_encode(dma_addr_t addr, |
Ben Widawsky | b35b380 | 2013-10-16 09:18:21 -0700 | [diff] [blame] | 160 | enum i915_cache_level level, |
| 161 | bool valid) |
Ben Widawsky | 4d15c14 | 2013-07-04 11:02:06 -0700 | [diff] [blame] | 162 | { |
Ben Widawsky | b35b380 | 2013-10-16 09:18:21 -0700 | [diff] [blame] | 163 | gen6_gtt_pte_t pte = valid ? GEN6_PTE_VALID : 0; |
Ben Widawsky | 4d15c14 | 2013-07-04 11:02:06 -0700 | [diff] [blame] | 164 | pte |= HSW_PTE_ADDR_ENCODE(addr); |
| 165 | |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 166 | switch (level) { |
| 167 | case I915_CACHE_NONE: |
| 168 | break; |
| 169 | case I915_CACHE_WT: |
| 170 | pte |= HSW_WT_ELLC_LLC_AGE0; |
| 171 | break; |
| 172 | default: |
Ben Widawsky | 4d15c14 | 2013-07-04 11:02:06 -0700 | [diff] [blame] | 173 | pte |= HSW_WB_ELLC_LLC_AGE0; |
Chris Wilson | 651d794 | 2013-08-08 14:41:10 +0100 | [diff] [blame] | 174 | break; |
| 175 | } |
Ben Widawsky | 4d15c14 | 2013-07-04 11:02:06 -0700 | [diff] [blame] | 176 | |
| 177 | return pte; |
| 178 | } |
| 179 | |
Ben Widawsky | 3e30254 | 2013-04-23 23:15:32 -0700 | [diff] [blame] | 180 | static void gen6_write_pdes(struct i915_hw_ppgtt *ppgtt) |
Ben Widawsky | 6197349 | 2013-04-08 18:43:54 -0700 | [diff] [blame] | 181 | { |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 182 | struct drm_i915_private *dev_priv = ppgtt->base.dev->dev_private; |
Ben Widawsky | 6197349 | 2013-04-08 18:43:54 -0700 | [diff] [blame] | 183 | gen6_gtt_pte_t __iomem *pd_addr; |
| 184 | uint32_t pd_entry; |
| 185 | int i; |
| 186 | |
Ben Widawsky | 0a73287 | 2013-04-23 23:15:30 -0700 | [diff] [blame] | 187 | WARN_ON(ppgtt->pd_offset & 0x3f); |
Ben Widawsky | 6197349 | 2013-04-08 18:43:54 -0700 | [diff] [blame] | 188 | pd_addr = (gen6_gtt_pte_t __iomem*)dev_priv->gtt.gsm + |
| 189 | ppgtt->pd_offset / sizeof(gen6_gtt_pte_t); |
| 190 | for (i = 0; i < ppgtt->num_pd_entries; i++) { |
| 191 | dma_addr_t pt_addr; |
| 192 | |
| 193 | pt_addr = ppgtt->pt_dma_addr[i]; |
| 194 | pd_entry = GEN6_PDE_ADDR_ENCODE(pt_addr); |
| 195 | pd_entry |= GEN6_PDE_VALID; |
| 196 | |
| 197 | writel(pd_entry, pd_addr + i); |
| 198 | } |
| 199 | readl(pd_addr); |
Ben Widawsky | 3e30254 | 2013-04-23 23:15:32 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | static int gen6_ppgtt_enable(struct drm_device *dev) |
| 203 | { |
| 204 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 205 | uint32_t pd_offset; |
| 206 | struct intel_ring_buffer *ring; |
| 207 | struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt; |
| 208 | int i; |
| 209 | |
| 210 | BUG_ON(ppgtt->pd_offset & 0x3f); |
| 211 | |
| 212 | gen6_write_pdes(ppgtt); |
Ben Widawsky | 6197349 | 2013-04-08 18:43:54 -0700 | [diff] [blame] | 213 | |
| 214 | pd_offset = ppgtt->pd_offset; |
| 215 | pd_offset /= 64; /* in cachelines, */ |
| 216 | pd_offset <<= 16; |
| 217 | |
| 218 | if (INTEL_INFO(dev)->gen == 6) { |
| 219 | uint32_t ecochk, gab_ctl, ecobits; |
| 220 | |
| 221 | ecobits = I915_READ(GAC_ECO_BITS); |
Ville Syrjälä | 3b9d788 | 2013-04-04 15:13:40 +0300 | [diff] [blame] | 222 | I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT | |
| 223 | ECOBITS_PPGTT_CACHE64B); |
Ben Widawsky | 6197349 | 2013-04-08 18:43:54 -0700 | [diff] [blame] | 224 | |
| 225 | gab_ctl = I915_READ(GAB_CTL); |
| 226 | I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT); |
| 227 | |
| 228 | ecochk = I915_READ(GAM_ECOCHK); |
| 229 | I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | |
| 230 | ECOCHK_PPGTT_CACHE64B); |
| 231 | I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE)); |
| 232 | } else if (INTEL_INFO(dev)->gen >= 7) { |
Ville Syrjälä | a6f429a | 2013-04-04 15:13:42 +0300 | [diff] [blame] | 233 | uint32_t ecochk, ecobits; |
Ville Syrjälä | a65c2fc | 2013-04-04 15:13:41 +0300 | [diff] [blame] | 234 | |
| 235 | ecobits = I915_READ(GAC_ECO_BITS); |
| 236 | I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B); |
| 237 | |
Ville Syrjälä | a6f429a | 2013-04-04 15:13:42 +0300 | [diff] [blame] | 238 | ecochk = I915_READ(GAM_ECOCHK); |
| 239 | if (IS_HASWELL(dev)) { |
| 240 | ecochk |= ECOCHK_PPGTT_WB_HSW; |
| 241 | } else { |
| 242 | ecochk |= ECOCHK_PPGTT_LLC_IVB; |
| 243 | ecochk &= ~ECOCHK_PPGTT_GFDT_IVB; |
| 244 | } |
| 245 | I915_WRITE(GAM_ECOCHK, ecochk); |
Ben Widawsky | 6197349 | 2013-04-08 18:43:54 -0700 | [diff] [blame] | 246 | /* GFX_MODE is per-ring on gen7+ */ |
| 247 | } |
| 248 | |
| 249 | for_each_ring(ring, dev_priv, i) { |
| 250 | if (INTEL_INFO(dev)->gen >= 7) |
| 251 | I915_WRITE(RING_MODE_GEN7(ring), |
| 252 | _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE)); |
| 253 | |
| 254 | I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G); |
| 255 | I915_WRITE(RING_PP_DIR_BASE(ring), pd_offset); |
| 256 | } |
Ben Widawsky | b7c36d2 | 2013-04-08 18:43:56 -0700 | [diff] [blame] | 257 | return 0; |
Ben Widawsky | 6197349 | 2013-04-08 18:43:54 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 260 | /* PPGTT support for Sandybdrige/Gen6 and later */ |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 261 | static void gen6_ppgtt_clear_range(struct i915_address_space *vm, |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 262 | unsigned first_entry, |
Ben Widawsky | 828c790 | 2013-10-16 09:21:30 -0700 | [diff] [blame] | 263 | unsigned num_entries, |
| 264 | bool use_scratch) |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 265 | { |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 266 | struct i915_hw_ppgtt *ppgtt = |
| 267 | container_of(vm, struct i915_hw_ppgtt, base); |
Ben Widawsky | e7c2b58 | 2013-04-08 18:43:48 -0700 | [diff] [blame] | 268 | gen6_gtt_pte_t *pt_vaddr, scratch_pte; |
Daniel Vetter | a15326a | 2013-03-19 23:48:39 +0100 | [diff] [blame] | 269 | unsigned act_pt = first_entry / I915_PPGTT_PT_ENTRIES; |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 270 | unsigned first_pte = first_entry % I915_PPGTT_PT_ENTRIES; |
| 271 | unsigned last_pte, i; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 272 | |
Ben Widawsky | b35b380 | 2013-10-16 09:18:21 -0700 | [diff] [blame] | 273 | scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true); |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 274 | |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 275 | while (num_entries) { |
| 276 | last_pte = first_pte + num_entries; |
| 277 | if (last_pte > I915_PPGTT_PT_ENTRIES) |
| 278 | last_pte = I915_PPGTT_PT_ENTRIES; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 279 | |
Daniel Vetter | a15326a | 2013-03-19 23:48:39 +0100 | [diff] [blame] | 280 | pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]); |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 281 | |
| 282 | for (i = first_pte; i < last_pte; i++) |
| 283 | pt_vaddr[i] = scratch_pte; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 284 | |
| 285 | kunmap_atomic(pt_vaddr); |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 286 | |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 287 | num_entries -= last_pte - first_pte; |
| 288 | first_pte = 0; |
Daniel Vetter | a15326a | 2013-03-19 23:48:39 +0100 | [diff] [blame] | 289 | act_pt++; |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 290 | } |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 291 | } |
| 292 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 293 | static void gen6_ppgtt_insert_entries(struct i915_address_space *vm, |
Daniel Vetter | def886c | 2013-01-24 14:44:56 -0800 | [diff] [blame] | 294 | struct sg_table *pages, |
| 295 | unsigned first_entry, |
| 296 | enum i915_cache_level cache_level) |
| 297 | { |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 298 | struct i915_hw_ppgtt *ppgtt = |
| 299 | container_of(vm, struct i915_hw_ppgtt, base); |
Ben Widawsky | e7c2b58 | 2013-04-08 18:43:48 -0700 | [diff] [blame] | 300 | gen6_gtt_pte_t *pt_vaddr; |
Daniel Vetter | a15326a | 2013-03-19 23:48:39 +0100 | [diff] [blame] | 301 | unsigned act_pt = first_entry / I915_PPGTT_PT_ENTRIES; |
Imre Deak | 6e995e2 | 2013-02-18 19:28:04 +0200 | [diff] [blame] | 302 | unsigned act_pte = first_entry % I915_PPGTT_PT_ENTRIES; |
| 303 | struct sg_page_iter sg_iter; |
Daniel Vetter | def886c | 2013-01-24 14:44:56 -0800 | [diff] [blame] | 304 | |
Daniel Vetter | a15326a | 2013-03-19 23:48:39 +0100 | [diff] [blame] | 305 | pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]); |
Imre Deak | 6e995e2 | 2013-02-18 19:28:04 +0200 | [diff] [blame] | 306 | for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) { |
| 307 | dma_addr_t page_addr; |
Daniel Vetter | def886c | 2013-01-24 14:44:56 -0800 | [diff] [blame] | 308 | |
Imre Deak | 2db76d7 | 2013-03-26 15:14:18 +0200 | [diff] [blame] | 309 | page_addr = sg_page_iter_dma_address(&sg_iter); |
Ben Widawsky | b35b380 | 2013-10-16 09:18:21 -0700 | [diff] [blame] | 310 | pt_vaddr[act_pte] = vm->pte_encode(page_addr, cache_level, true); |
Imre Deak | 6e995e2 | 2013-02-18 19:28:04 +0200 | [diff] [blame] | 311 | if (++act_pte == I915_PPGTT_PT_ENTRIES) { |
| 312 | kunmap_atomic(pt_vaddr); |
Daniel Vetter | a15326a | 2013-03-19 23:48:39 +0100 | [diff] [blame] | 313 | act_pt++; |
| 314 | pt_vaddr = kmap_atomic(ppgtt->pt_pages[act_pt]); |
Imre Deak | 6e995e2 | 2013-02-18 19:28:04 +0200 | [diff] [blame] | 315 | act_pte = 0; |
Daniel Vetter | def886c | 2013-01-24 14:44:56 -0800 | [diff] [blame] | 316 | |
Daniel Vetter | def886c | 2013-01-24 14:44:56 -0800 | [diff] [blame] | 317 | } |
Daniel Vetter | def886c | 2013-01-24 14:44:56 -0800 | [diff] [blame] | 318 | } |
Imre Deak | 6e995e2 | 2013-02-18 19:28:04 +0200 | [diff] [blame] | 319 | kunmap_atomic(pt_vaddr); |
Daniel Vetter | def886c | 2013-01-24 14:44:56 -0800 | [diff] [blame] | 320 | } |
| 321 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 322 | static void gen6_ppgtt_cleanup(struct i915_address_space *vm) |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 323 | { |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 324 | struct i915_hw_ppgtt *ppgtt = |
| 325 | container_of(vm, struct i915_hw_ppgtt, base); |
Daniel Vetter | 3440d26 | 2013-01-24 13:49:56 -0800 | [diff] [blame] | 326 | int i; |
| 327 | |
Ben Widawsky | 93bd864 | 2013-07-16 16:50:06 -0700 | [diff] [blame] | 328 | drm_mm_takedown(&ppgtt->base.mm); |
| 329 | |
Daniel Vetter | 3440d26 | 2013-01-24 13:49:56 -0800 | [diff] [blame] | 330 | if (ppgtt->pt_dma_addr) { |
| 331 | for (i = 0; i < ppgtt->num_pd_entries; i++) |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 332 | pci_unmap_page(ppgtt->base.dev->pdev, |
Daniel Vetter | 3440d26 | 2013-01-24 13:49:56 -0800 | [diff] [blame] | 333 | ppgtt->pt_dma_addr[i], |
| 334 | 4096, PCI_DMA_BIDIRECTIONAL); |
| 335 | } |
| 336 | |
| 337 | kfree(ppgtt->pt_dma_addr); |
| 338 | for (i = 0; i < ppgtt->num_pd_entries; i++) |
| 339 | __free_page(ppgtt->pt_pages[i]); |
| 340 | kfree(ppgtt->pt_pages); |
| 341 | kfree(ppgtt); |
| 342 | } |
| 343 | |
| 344 | static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt) |
| 345 | { |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 346 | struct drm_device *dev = ppgtt->base.dev; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 347 | struct drm_i915_private *dev_priv = dev->dev_private; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 348 | unsigned first_pd_entry_in_global_pt; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 349 | int i; |
| 350 | int ret = -ENOMEM; |
| 351 | |
| 352 | /* ppgtt PDEs reside in the global gtt pagetable, which has 512*1024 |
| 353 | * entries. For aliasing ppgtt support we just steal them at the end for |
| 354 | * now. */ |
Daniel Vetter | e1b73cb | 2013-05-21 09:52:16 +0200 | [diff] [blame] | 355 | first_pd_entry_in_global_pt = gtt_total_entries(dev_priv->gtt); |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 356 | |
Chris Wilson | 08c4526 | 2013-07-30 19:04:37 +0100 | [diff] [blame] | 357 | ppgtt->base.pte_encode = dev_priv->gtt.base.pte_encode; |
Ben Widawsky | 6670a5a | 2013-06-27 16:30:04 -0700 | [diff] [blame] | 358 | ppgtt->num_pd_entries = GEN6_PPGTT_PD_ENTRIES; |
Ben Widawsky | 6197349 | 2013-04-08 18:43:54 -0700 | [diff] [blame] | 359 | ppgtt->enable = gen6_ppgtt_enable; |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 360 | ppgtt->base.clear_range = gen6_ppgtt_clear_range; |
| 361 | ppgtt->base.insert_entries = gen6_ppgtt_insert_entries; |
| 362 | ppgtt->base.cleanup = gen6_ppgtt_cleanup; |
| 363 | ppgtt->base.scratch = dev_priv->gtt.base.scratch; |
Daniel Vetter | a1e2265 | 2013-09-21 00:35:38 +0200 | [diff] [blame] | 364 | ppgtt->pt_pages = kcalloc(ppgtt->num_pd_entries, sizeof(struct page *), |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 365 | GFP_KERNEL); |
| 366 | if (!ppgtt->pt_pages) |
Daniel Vetter | 3440d26 | 2013-01-24 13:49:56 -0800 | [diff] [blame] | 367 | return -ENOMEM; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 368 | |
| 369 | for (i = 0; i < ppgtt->num_pd_entries; i++) { |
| 370 | ppgtt->pt_pages[i] = alloc_page(GFP_KERNEL); |
| 371 | if (!ppgtt->pt_pages[i]) |
| 372 | goto err_pt_alloc; |
| 373 | } |
| 374 | |
Daniel Vetter | a1e2265 | 2013-09-21 00:35:38 +0200 | [diff] [blame] | 375 | ppgtt->pt_dma_addr = kcalloc(ppgtt->num_pd_entries, sizeof(dma_addr_t), |
Ben Widawsky | 8d2e630 | 2013-01-18 12:30:33 -0800 | [diff] [blame] | 376 | GFP_KERNEL); |
| 377 | if (!ppgtt->pt_dma_addr) |
| 378 | goto err_pt_alloc; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 379 | |
Ben Widawsky | 8d2e630 | 2013-01-18 12:30:33 -0800 | [diff] [blame] | 380 | for (i = 0; i < ppgtt->num_pd_entries; i++) { |
| 381 | dma_addr_t pt_addr; |
Daniel Vetter | 211c568 | 2012-04-10 17:29:17 +0200 | [diff] [blame] | 382 | |
Ben Widawsky | 8d2e630 | 2013-01-18 12:30:33 -0800 | [diff] [blame] | 383 | pt_addr = pci_map_page(dev->pdev, ppgtt->pt_pages[i], 0, 4096, |
| 384 | PCI_DMA_BIDIRECTIONAL); |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 385 | |
Ben Widawsky | 8d2e630 | 2013-01-18 12:30:33 -0800 | [diff] [blame] | 386 | if (pci_dma_mapping_error(dev->pdev, pt_addr)) { |
| 387 | ret = -EIO; |
| 388 | goto err_pd_pin; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 389 | |
Daniel Vetter | 211c568 | 2012-04-10 17:29:17 +0200 | [diff] [blame] | 390 | } |
Ben Widawsky | 8d2e630 | 2013-01-18 12:30:33 -0800 | [diff] [blame] | 391 | ppgtt->pt_dma_addr[i] = pt_addr; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 392 | } |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 393 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 394 | ppgtt->base.clear_range(&ppgtt->base, 0, |
Ben Widawsky | 828c790 | 2013-10-16 09:21:30 -0700 | [diff] [blame] | 395 | ppgtt->num_pd_entries * I915_PPGTT_PT_ENTRIES, true); |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 396 | |
Ben Widawsky | e7c2b58 | 2013-04-08 18:43:48 -0700 | [diff] [blame] | 397 | 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] | 398 | |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 399 | return 0; |
| 400 | |
| 401 | err_pd_pin: |
| 402 | if (ppgtt->pt_dma_addr) { |
| 403 | for (i--; i >= 0; i--) |
| 404 | pci_unmap_page(dev->pdev, ppgtt->pt_dma_addr[i], |
| 405 | 4096, PCI_DMA_BIDIRECTIONAL); |
| 406 | } |
| 407 | err_pt_alloc: |
| 408 | kfree(ppgtt->pt_dma_addr); |
| 409 | for (i = 0; i < ppgtt->num_pd_entries; i++) { |
| 410 | if (ppgtt->pt_pages[i]) |
| 411 | __free_page(ppgtt->pt_pages[i]); |
| 412 | } |
| 413 | kfree(ppgtt->pt_pages); |
Daniel Vetter | 3440d26 | 2013-01-24 13:49:56 -0800 | [diff] [blame] | 414 | |
| 415 | return ret; |
| 416 | } |
| 417 | |
| 418 | static int i915_gem_init_aliasing_ppgtt(struct drm_device *dev) |
| 419 | { |
| 420 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 421 | struct i915_hw_ppgtt *ppgtt; |
| 422 | int ret; |
| 423 | |
| 424 | ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL); |
| 425 | if (!ppgtt) |
| 426 | return -ENOMEM; |
| 427 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 428 | ppgtt->base.dev = dev; |
Daniel Vetter | 3440d26 | 2013-01-24 13:49:56 -0800 | [diff] [blame] | 429 | |
Ben Widawsky | 3ed124b | 2013-04-08 18:43:53 -0700 | [diff] [blame] | 430 | if (INTEL_INFO(dev)->gen < 8) |
| 431 | ret = gen6_ppgtt_init(ppgtt); |
Daniel Vetter | 8fe6bd2 | 2013-11-02 21:07:01 -0700 | [diff] [blame] | 432 | else if (IS_GEN8(dev)) |
| 433 | ret = -ENOSYS; |
Ben Widawsky | 3ed124b | 2013-04-08 18:43:53 -0700 | [diff] [blame] | 434 | else |
| 435 | BUG(); |
| 436 | |
Daniel Vetter | 3440d26 | 2013-01-24 13:49:56 -0800 | [diff] [blame] | 437 | if (ret) |
| 438 | kfree(ppgtt); |
Ben Widawsky | 93bd864 | 2013-07-16 16:50:06 -0700 | [diff] [blame] | 439 | else { |
Daniel Vetter | 3440d26 | 2013-01-24 13:49:56 -0800 | [diff] [blame] | 440 | dev_priv->mm.aliasing_ppgtt = ppgtt; |
Ben Widawsky | 93bd864 | 2013-07-16 16:50:06 -0700 | [diff] [blame] | 441 | drm_mm_init(&ppgtt->base.mm, ppgtt->base.start, |
| 442 | ppgtt->base.total); |
| 443 | } |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 444 | |
| 445 | return ret; |
| 446 | } |
| 447 | |
| 448 | void i915_gem_cleanup_aliasing_ppgtt(struct drm_device *dev) |
| 449 | { |
| 450 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 451 | struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 452 | |
| 453 | if (!ppgtt) |
| 454 | return; |
| 455 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 456 | ppgtt->base.cleanup(&ppgtt->base); |
Ben Widawsky | 5963cf0 | 2013-04-08 18:43:55 -0700 | [diff] [blame] | 457 | dev_priv->mm.aliasing_ppgtt = NULL; |
Daniel Vetter | 1d2a314 | 2012-02-09 17:15:46 +0100 | [diff] [blame] | 458 | } |
| 459 | |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 460 | void i915_ppgtt_bind_object(struct i915_hw_ppgtt *ppgtt, |
| 461 | struct drm_i915_gem_object *obj, |
| 462 | enum i915_cache_level cache_level) |
| 463 | { |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 464 | ppgtt->base.insert_entries(&ppgtt->base, obj->pages, |
| 465 | i915_gem_obj_ggtt_offset(obj) >> PAGE_SHIFT, |
| 466 | cache_level); |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | void i915_ppgtt_unbind_object(struct i915_hw_ppgtt *ppgtt, |
| 470 | struct drm_i915_gem_object *obj) |
| 471 | { |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 472 | ppgtt->base.clear_range(&ppgtt->base, |
| 473 | i915_gem_obj_ggtt_offset(obj) >> PAGE_SHIFT, |
Ben Widawsky | 828c790 | 2013-10-16 09:21:30 -0700 | [diff] [blame] | 474 | obj->base.size >> PAGE_SHIFT, |
| 475 | true); |
Daniel Vetter | 7bddb01 | 2012-02-09 17:15:47 +0100 | [diff] [blame] | 476 | } |
| 477 | |
Ben Widawsky | a81cc00 | 2013-01-18 12:30:31 -0800 | [diff] [blame] | 478 | extern int intel_iommu_gfx_mapped; |
| 479 | /* Certain Gen5 chipsets require require idling the GPU before |
| 480 | * unmapping anything from the GTT when VT-d is enabled. |
| 481 | */ |
| 482 | static inline bool needs_idle_maps(struct drm_device *dev) |
| 483 | { |
| 484 | #ifdef CONFIG_INTEL_IOMMU |
| 485 | /* Query intel_iommu to see if we need the workaround. Presumably that |
| 486 | * was loaded first. |
| 487 | */ |
| 488 | if (IS_GEN5(dev) && IS_MOBILE(dev) && intel_iommu_gfx_mapped) |
| 489 | return true; |
| 490 | #endif |
| 491 | return false; |
| 492 | } |
| 493 | |
Ben Widawsky | 5c04228 | 2011-10-17 15:51:55 -0700 | [diff] [blame] | 494 | static bool do_idling(struct drm_i915_private *dev_priv) |
| 495 | { |
| 496 | bool ret = dev_priv->mm.interruptible; |
| 497 | |
Ben Widawsky | a81cc00 | 2013-01-18 12:30:31 -0800 | [diff] [blame] | 498 | if (unlikely(dev_priv->gtt.do_idle_maps)) { |
Ben Widawsky | 5c04228 | 2011-10-17 15:51:55 -0700 | [diff] [blame] | 499 | dev_priv->mm.interruptible = false; |
Ben Widawsky | b2da9fe | 2012-04-26 16:02:58 -0700 | [diff] [blame] | 500 | if (i915_gpu_idle(dev_priv->dev)) { |
Ben Widawsky | 5c04228 | 2011-10-17 15:51:55 -0700 | [diff] [blame] | 501 | DRM_ERROR("Couldn't idle GPU\n"); |
| 502 | /* Wait a bit, in hopes it avoids the hang */ |
| 503 | udelay(10); |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | return ret; |
| 508 | } |
| 509 | |
| 510 | static void undo_idling(struct drm_i915_private *dev_priv, bool interruptible) |
| 511 | { |
Ben Widawsky | a81cc00 | 2013-01-18 12:30:31 -0800 | [diff] [blame] | 512 | if (unlikely(dev_priv->gtt.do_idle_maps)) |
Ben Widawsky | 5c04228 | 2011-10-17 15:51:55 -0700 | [diff] [blame] | 513 | dev_priv->mm.interruptible = interruptible; |
| 514 | } |
| 515 | |
Ben Widawsky | 828c790 | 2013-10-16 09:21:30 -0700 | [diff] [blame] | 516 | void i915_check_and_clear_faults(struct drm_device *dev) |
| 517 | { |
| 518 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 519 | struct intel_ring_buffer *ring; |
| 520 | int i; |
| 521 | |
| 522 | if (INTEL_INFO(dev)->gen < 6) |
| 523 | return; |
| 524 | |
| 525 | for_each_ring(ring, dev_priv, i) { |
| 526 | u32 fault_reg; |
| 527 | fault_reg = I915_READ(RING_FAULT_REG(ring)); |
| 528 | if (fault_reg & RING_FAULT_VALID) { |
| 529 | DRM_DEBUG_DRIVER("Unexpected fault\n" |
| 530 | "\tAddr: 0x%08lx\\n" |
| 531 | "\tAddress space: %s\n" |
| 532 | "\tSource ID: %d\n" |
| 533 | "\tType: %d\n", |
| 534 | fault_reg & PAGE_MASK, |
| 535 | fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT", |
| 536 | RING_FAULT_SRCID(fault_reg), |
| 537 | RING_FAULT_FAULT_TYPE(fault_reg)); |
| 538 | I915_WRITE(RING_FAULT_REG(ring), |
| 539 | fault_reg & ~RING_FAULT_VALID); |
| 540 | } |
| 541 | } |
| 542 | POSTING_READ(RING_FAULT_REG(&dev_priv->ring[RCS])); |
| 543 | } |
| 544 | |
| 545 | void i915_gem_suspend_gtt_mappings(struct drm_device *dev) |
| 546 | { |
| 547 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 548 | |
| 549 | /* Don't bother messing with faults pre GEN6 as we have little |
| 550 | * documentation supporting that it's a good idea. |
| 551 | */ |
| 552 | if (INTEL_INFO(dev)->gen < 6) |
| 553 | return; |
| 554 | |
| 555 | i915_check_and_clear_faults(dev); |
| 556 | |
| 557 | dev_priv->gtt.base.clear_range(&dev_priv->gtt.base, |
| 558 | dev_priv->gtt.base.start / PAGE_SIZE, |
| 559 | dev_priv->gtt.base.total / PAGE_SIZE, |
| 560 | false); |
| 561 | } |
| 562 | |
Daniel Vetter | 76aaf22 | 2010-11-05 22:23:30 +0100 | [diff] [blame] | 563 | void i915_gem_restore_gtt_mappings(struct drm_device *dev) |
| 564 | { |
| 565 | struct drm_i915_private *dev_priv = dev->dev_private; |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 566 | struct drm_i915_gem_object *obj; |
Daniel Vetter | 76aaf22 | 2010-11-05 22:23:30 +0100 | [diff] [blame] | 567 | |
Ben Widawsky | 828c790 | 2013-10-16 09:21:30 -0700 | [diff] [blame] | 568 | i915_check_and_clear_faults(dev); |
| 569 | |
Chris Wilson | bee4a18 | 2011-01-21 10:54:32 +0000 | [diff] [blame] | 570 | /* First fill our portion of the GTT with scratch pages */ |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 571 | dev_priv->gtt.base.clear_range(&dev_priv->gtt.base, |
| 572 | dev_priv->gtt.base.start / PAGE_SIZE, |
Ben Widawsky | 828c790 | 2013-10-16 09:21:30 -0700 | [diff] [blame] | 573 | dev_priv->gtt.base.total / PAGE_SIZE, |
| 574 | true); |
Chris Wilson | bee4a18 | 2011-01-21 10:54:32 +0000 | [diff] [blame] | 575 | |
Ben Widawsky | 35c20a6 | 2013-05-31 11:28:48 -0700 | [diff] [blame] | 576 | list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) { |
Chris Wilson | 2c22569 | 2013-08-09 12:26:45 +0100 | [diff] [blame] | 577 | i915_gem_clflush_object(obj, obj->pin_display); |
Daniel Vetter | 7416390 | 2012-02-15 23:50:21 +0100 | [diff] [blame] | 578 | i915_gem_gtt_bind_object(obj, obj->cache_level); |
Daniel Vetter | 76aaf22 | 2010-11-05 22:23:30 +0100 | [diff] [blame] | 579 | } |
| 580 | |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 581 | i915_gem_chipset_flush(dev); |
Daniel Vetter | 76aaf22 | 2010-11-05 22:23:30 +0100 | [diff] [blame] | 582 | } |
Daniel Vetter | 7c2e6fd | 2010-11-06 10:10:47 +0100 | [diff] [blame] | 583 | |
Daniel Vetter | 7416390 | 2012-02-15 23:50:21 +0100 | [diff] [blame] | 584 | int i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj) |
Daniel Vetter | 7c2e6fd | 2010-11-06 10:10:47 +0100 | [diff] [blame] | 585 | { |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 586 | if (obj->has_dma_mapping) |
Daniel Vetter | 7416390 | 2012-02-15 23:50:21 +0100 | [diff] [blame] | 587 | return 0; |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 588 | |
| 589 | if (!dma_map_sg(&obj->base.dev->pdev->dev, |
| 590 | obj->pages->sgl, obj->pages->nents, |
| 591 | PCI_DMA_BIDIRECTIONAL)) |
| 592 | return -ENOSPC; |
| 593 | |
| 594 | return 0; |
Daniel Vetter | 7c2e6fd | 2010-11-06 10:10:47 +0100 | [diff] [blame] | 595 | } |
| 596 | |
Ben Widawsky | 94ec8f6 | 2013-11-02 21:07:18 -0700 | [diff] [blame] | 597 | static inline void gen8_set_pte(void __iomem *addr, gen8_gtt_pte_t pte) |
| 598 | { |
| 599 | #ifdef writeq |
| 600 | writeq(pte, addr); |
| 601 | #else |
| 602 | iowrite32((u32)pte, addr); |
| 603 | iowrite32(pte >> 32, addr + 4); |
| 604 | #endif |
| 605 | } |
| 606 | |
| 607 | static void gen8_ggtt_insert_entries(struct i915_address_space *vm, |
| 608 | struct sg_table *st, |
| 609 | unsigned int first_entry, |
| 610 | enum i915_cache_level level) |
| 611 | { |
| 612 | struct drm_i915_private *dev_priv = vm->dev->dev_private; |
| 613 | gen8_gtt_pte_t __iomem *gtt_entries = |
| 614 | (gen8_gtt_pte_t __iomem *)dev_priv->gtt.gsm + first_entry; |
| 615 | int i = 0; |
| 616 | struct sg_page_iter sg_iter; |
| 617 | dma_addr_t addr; |
| 618 | |
| 619 | for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) { |
| 620 | addr = sg_dma_address(sg_iter.sg) + |
| 621 | (sg_iter.sg_pgoffset << PAGE_SHIFT); |
| 622 | gen8_set_pte(>t_entries[i], |
| 623 | gen8_pte_encode(addr, level, true)); |
| 624 | i++; |
| 625 | } |
| 626 | |
| 627 | /* |
| 628 | * XXX: This serves as a posting read to make sure that the PTE has |
| 629 | * actually been updated. There is some concern that even though |
| 630 | * registers and PTEs are within the same BAR that they are potentially |
| 631 | * of NUMA access patterns. Therefore, even with the way we assume |
| 632 | * hardware should work, we must keep this posting read for paranoia. |
| 633 | */ |
| 634 | if (i != 0) |
| 635 | WARN_ON(readq(>t_entries[i-1]) |
| 636 | != gen8_pte_encode(addr, level, true)); |
| 637 | |
| 638 | #if 0 /* TODO: Still needed on GEN8? */ |
| 639 | /* This next bit makes the above posting read even more important. We |
| 640 | * want to flush the TLBs only after we're certain all the PTE updates |
| 641 | * have finished. |
| 642 | */ |
| 643 | I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN); |
| 644 | POSTING_READ(GFX_FLSH_CNTL_GEN6); |
| 645 | #endif |
| 646 | } |
| 647 | |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 648 | /* |
| 649 | * Binds an object into the global gtt with the specified cache level. The object |
| 650 | * will be accessible to the GPU via commands whose operands reference offsets |
| 651 | * within the global GTT as well as accessible by the GPU through the GMADR |
| 652 | * mapped BAR (dev_priv->mm.gtt->gtt). |
| 653 | */ |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 654 | static void gen6_ggtt_insert_entries(struct i915_address_space *vm, |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 655 | struct sg_table *st, |
| 656 | unsigned int first_entry, |
| 657 | enum i915_cache_level level) |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 658 | { |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 659 | struct drm_i915_private *dev_priv = vm->dev->dev_private; |
Ben Widawsky | e7c2b58 | 2013-04-08 18:43:48 -0700 | [diff] [blame] | 660 | gen6_gtt_pte_t __iomem *gtt_entries = |
| 661 | (gen6_gtt_pte_t __iomem *)dev_priv->gtt.gsm + first_entry; |
Imre Deak | 6e995e2 | 2013-02-18 19:28:04 +0200 | [diff] [blame] | 662 | int i = 0; |
| 663 | struct sg_page_iter sg_iter; |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 664 | dma_addr_t addr; |
| 665 | |
Imre Deak | 6e995e2 | 2013-02-18 19:28:04 +0200 | [diff] [blame] | 666 | for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) { |
Imre Deak | 2db76d7 | 2013-03-26 15:14:18 +0200 | [diff] [blame] | 667 | addr = sg_page_iter_dma_address(&sg_iter); |
Ben Widawsky | b35b380 | 2013-10-16 09:18:21 -0700 | [diff] [blame] | 668 | iowrite32(vm->pte_encode(addr, level, true), >t_entries[i]); |
Imre Deak | 6e995e2 | 2013-02-18 19:28:04 +0200 | [diff] [blame] | 669 | i++; |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 670 | } |
| 671 | |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 672 | /* XXX: This serves as a posting read to make sure that the PTE has |
| 673 | * actually been updated. There is some concern that even though |
| 674 | * registers and PTEs are within the same BAR that they are potentially |
| 675 | * of NUMA access patterns. Therefore, even with the way we assume |
| 676 | * hardware should work, we must keep this posting read for paranoia. |
| 677 | */ |
| 678 | if (i != 0) |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 679 | WARN_ON(readl(>t_entries[i-1]) != |
Ben Widawsky | b35b380 | 2013-10-16 09:18:21 -0700 | [diff] [blame] | 680 | vm->pte_encode(addr, level, true)); |
Ben Widawsky | 0f9b91c | 2012-11-04 09:21:30 -0800 | [diff] [blame] | 681 | |
| 682 | /* This next bit makes the above posting read even more important. We |
| 683 | * want to flush the TLBs only after we're certain all the PTE updates |
| 684 | * have finished. |
| 685 | */ |
| 686 | I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN); |
| 687 | POSTING_READ(GFX_FLSH_CNTL_GEN6); |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 688 | } |
| 689 | |
Ben Widawsky | 94ec8f6 | 2013-11-02 21:07:18 -0700 | [diff] [blame] | 690 | static void gen8_ggtt_clear_range(struct i915_address_space *vm, |
| 691 | unsigned int first_entry, |
| 692 | unsigned int num_entries, |
| 693 | bool use_scratch) |
| 694 | { |
| 695 | struct drm_i915_private *dev_priv = vm->dev->dev_private; |
| 696 | gen8_gtt_pte_t scratch_pte, __iomem *gtt_base = |
| 697 | (gen8_gtt_pte_t __iomem *) dev_priv->gtt.gsm + first_entry; |
| 698 | const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry; |
| 699 | int i; |
| 700 | |
| 701 | if (WARN(num_entries > max_entries, |
| 702 | "First entry = %d; Num entries = %d (max=%d)\n", |
| 703 | first_entry, num_entries, max_entries)) |
| 704 | num_entries = max_entries; |
| 705 | |
| 706 | scratch_pte = gen8_pte_encode(vm->scratch.addr, |
| 707 | I915_CACHE_LLC, |
| 708 | use_scratch); |
| 709 | for (i = 0; i < num_entries; i++) |
| 710 | gen8_set_pte(>t_base[i], scratch_pte); |
| 711 | readl(gtt_base); |
| 712 | } |
| 713 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 714 | static void gen6_ggtt_clear_range(struct i915_address_space *vm, |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 715 | unsigned int first_entry, |
Ben Widawsky | 828c790 | 2013-10-16 09:21:30 -0700 | [diff] [blame] | 716 | unsigned int num_entries, |
| 717 | bool use_scratch) |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 718 | { |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 719 | struct drm_i915_private *dev_priv = vm->dev->dev_private; |
Ben Widawsky | e7c2b58 | 2013-04-08 18:43:48 -0700 | [diff] [blame] | 720 | gen6_gtt_pte_t scratch_pte, __iomem *gtt_base = |
| 721 | (gen6_gtt_pte_t __iomem *) dev_priv->gtt.gsm + first_entry; |
Ben Widawsky | a54c0c2 | 2013-01-24 14:45:00 -0800 | [diff] [blame] | 722 | const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry; |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 723 | int i; |
| 724 | |
| 725 | if (WARN(num_entries > max_entries, |
| 726 | "First entry = %d; Num entries = %d (max=%d)\n", |
| 727 | first_entry, num_entries, max_entries)) |
| 728 | num_entries = max_entries; |
| 729 | |
Ben Widawsky | 828c790 | 2013-10-16 09:21:30 -0700 | [diff] [blame] | 730 | scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, use_scratch); |
| 731 | |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 732 | for (i = 0; i < num_entries; i++) |
| 733 | iowrite32(scratch_pte, >t_base[i]); |
| 734 | readl(gtt_base); |
| 735 | } |
| 736 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 737 | static void i915_ggtt_insert_entries(struct i915_address_space *vm, |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 738 | struct sg_table *st, |
| 739 | unsigned int pg_start, |
| 740 | enum i915_cache_level cache_level) |
| 741 | { |
| 742 | unsigned int flags = (cache_level == I915_CACHE_NONE) ? |
| 743 | AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY; |
| 744 | |
| 745 | intel_gtt_insert_sg_entries(st, pg_start, flags); |
| 746 | |
| 747 | } |
| 748 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 749 | static void i915_ggtt_clear_range(struct i915_address_space *vm, |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 750 | unsigned int first_entry, |
Ben Widawsky | 828c790 | 2013-10-16 09:21:30 -0700 | [diff] [blame] | 751 | unsigned int num_entries, |
| 752 | bool unused) |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 753 | { |
| 754 | intel_gtt_clear_range(first_entry, num_entries); |
| 755 | } |
| 756 | |
| 757 | |
Daniel Vetter | 7416390 | 2012-02-15 23:50:21 +0100 | [diff] [blame] | 758 | void i915_gem_gtt_bind_object(struct drm_i915_gem_object *obj, |
| 759 | enum i915_cache_level cache_level) |
Chris Wilson | d5bd144 | 2011-04-14 06:48:26 +0100 | [diff] [blame] | 760 | { |
| 761 | struct drm_device *dev = obj->base.dev; |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 762 | struct drm_i915_private *dev_priv = dev->dev_private; |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 763 | const unsigned long entry = i915_gem_obj_ggtt_offset(obj) >> PAGE_SHIFT; |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 764 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 765 | dev_priv->gtt.base.insert_entries(&dev_priv->gtt.base, obj->pages, |
| 766 | entry, |
| 767 | cache_level); |
Chris Wilson | d5bd144 | 2011-04-14 06:48:26 +0100 | [diff] [blame] | 768 | |
Daniel Vetter | 74898d7 | 2012-02-15 23:50:22 +0100 | [diff] [blame] | 769 | obj->has_global_gtt_mapping = 1; |
Chris Wilson | d5bd144 | 2011-04-14 06:48:26 +0100 | [diff] [blame] | 770 | } |
| 771 | |
Chris Wilson | 05394f3 | 2010-11-08 19:18:58 +0000 | [diff] [blame] | 772 | void i915_gem_gtt_unbind_object(struct drm_i915_gem_object *obj) |
Daniel Vetter | 7c2e6fd | 2010-11-06 10:10:47 +0100 | [diff] [blame] | 773 | { |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 774 | struct drm_device *dev = obj->base.dev; |
| 775 | struct drm_i915_private *dev_priv = dev->dev_private; |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 776 | const unsigned long entry = i915_gem_obj_ggtt_offset(obj) >> PAGE_SHIFT; |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 777 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 778 | dev_priv->gtt.base.clear_range(&dev_priv->gtt.base, |
| 779 | entry, |
Ben Widawsky | 828c790 | 2013-10-16 09:21:30 -0700 | [diff] [blame] | 780 | obj->base.size >> PAGE_SHIFT, |
| 781 | true); |
Daniel Vetter | 74898d7 | 2012-02-15 23:50:22 +0100 | [diff] [blame] | 782 | |
| 783 | obj->has_global_gtt_mapping = 0; |
Daniel Vetter | 7416390 | 2012-02-15 23:50:21 +0100 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj) |
| 787 | { |
Ben Widawsky | 5c04228 | 2011-10-17 15:51:55 -0700 | [diff] [blame] | 788 | struct drm_device *dev = obj->base.dev; |
| 789 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 790 | bool interruptible; |
| 791 | |
| 792 | interruptible = do_idling(dev_priv); |
| 793 | |
Chris Wilson | 9da3da6 | 2012-06-01 15:20:22 +0100 | [diff] [blame] | 794 | if (!obj->has_dma_mapping) |
| 795 | dma_unmap_sg(&dev->pdev->dev, |
| 796 | obj->pages->sgl, obj->pages->nents, |
| 797 | PCI_DMA_BIDIRECTIONAL); |
Ben Widawsky | 5c04228 | 2011-10-17 15:51:55 -0700 | [diff] [blame] | 798 | |
| 799 | undo_idling(dev_priv, interruptible); |
Daniel Vetter | 7c2e6fd | 2010-11-06 10:10:47 +0100 | [diff] [blame] | 800 | } |
Daniel Vetter | 644ec02 | 2012-03-26 09:45:40 +0200 | [diff] [blame] | 801 | |
Chris Wilson | 42d6ab4 | 2012-07-26 11:49:32 +0100 | [diff] [blame] | 802 | static void i915_gtt_color_adjust(struct drm_mm_node *node, |
| 803 | unsigned long color, |
| 804 | unsigned long *start, |
| 805 | unsigned long *end) |
| 806 | { |
| 807 | if (node->color != color) |
| 808 | *start += 4096; |
| 809 | |
| 810 | if (!list_empty(&node->node_list)) { |
| 811 | node = list_entry(node->node_list.next, |
| 812 | struct drm_mm_node, |
| 813 | node_list); |
| 814 | if (node->allocated && node->color != color) |
| 815 | *end -= 4096; |
| 816 | } |
| 817 | } |
Ben Widawsky | fbe5d36 | 2013-11-04 19:56:49 -0800 | [diff] [blame^] | 818 | |
Ben Widawsky | d7e5008 | 2012-12-18 10:31:25 -0800 | [diff] [blame] | 819 | void i915_gem_setup_global_gtt(struct drm_device *dev, |
| 820 | unsigned long start, |
| 821 | unsigned long mappable_end, |
| 822 | unsigned long end) |
Daniel Vetter | 644ec02 | 2012-03-26 09:45:40 +0200 | [diff] [blame] | 823 | { |
Ben Widawsky | e78891c | 2013-01-25 16:41:04 -0800 | [diff] [blame] | 824 | /* Let GEM Manage all of the aperture. |
| 825 | * |
| 826 | * However, leave one page at the end still bound to the scratch page. |
| 827 | * There are a number of places where the hardware apparently prefetches |
| 828 | * past the end of the object, and we've seen multiple hangs with the |
| 829 | * GPU head pointer stuck in a batchbuffer bound at the last page of the |
| 830 | * aperture. One page should be enough to keep any prefetching inside |
| 831 | * of the aperture. |
| 832 | */ |
Ben Widawsky | 40d74980 | 2013-07-31 16:59:59 -0700 | [diff] [blame] | 833 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 834 | struct i915_address_space *ggtt_vm = &dev_priv->gtt.base; |
Chris Wilson | ed2f345 | 2012-11-15 11:32:19 +0000 | [diff] [blame] | 835 | struct drm_mm_node *entry; |
| 836 | struct drm_i915_gem_object *obj; |
| 837 | unsigned long hole_start, hole_end; |
Daniel Vetter | 644ec02 | 2012-03-26 09:45:40 +0200 | [diff] [blame] | 838 | |
Ben Widawsky | 35451cb | 2013-01-17 12:45:13 -0800 | [diff] [blame] | 839 | BUG_ON(mappable_end > end); |
| 840 | |
Chris Wilson | ed2f345 | 2012-11-15 11:32:19 +0000 | [diff] [blame] | 841 | /* Subtract the guard page ... */ |
Ben Widawsky | 40d74980 | 2013-07-31 16:59:59 -0700 | [diff] [blame] | 842 | drm_mm_init(&ggtt_vm->mm, start, end - start - PAGE_SIZE); |
Chris Wilson | 42d6ab4 | 2012-07-26 11:49:32 +0100 | [diff] [blame] | 843 | if (!HAS_LLC(dev)) |
Ben Widawsky | 93bd864 | 2013-07-16 16:50:06 -0700 | [diff] [blame] | 844 | dev_priv->gtt.base.mm.color_adjust = i915_gtt_color_adjust; |
Daniel Vetter | 644ec02 | 2012-03-26 09:45:40 +0200 | [diff] [blame] | 845 | |
Chris Wilson | ed2f345 | 2012-11-15 11:32:19 +0000 | [diff] [blame] | 846 | /* Mark any preallocated objects as occupied */ |
Ben Widawsky | 35c20a6 | 2013-05-31 11:28:48 -0700 | [diff] [blame] | 847 | list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) { |
Ben Widawsky | 40d74980 | 2013-07-31 16:59:59 -0700 | [diff] [blame] | 848 | struct i915_vma *vma = i915_gem_obj_to_vma(obj, ggtt_vm); |
Ben Widawsky | b3a070c | 2013-07-05 14:41:02 -0700 | [diff] [blame] | 849 | int ret; |
Ben Widawsky | edd41a8 | 2013-07-05 14:41:05 -0700 | [diff] [blame] | 850 | DRM_DEBUG_KMS("reserving preallocated space: %lx + %zx\n", |
Ben Widawsky | c6cfb32 | 2013-07-05 14:41:06 -0700 | [diff] [blame] | 851 | i915_gem_obj_ggtt_offset(obj), obj->base.size); |
Chris Wilson | ed2f345 | 2012-11-15 11:32:19 +0000 | [diff] [blame] | 852 | |
Ben Widawsky | c6cfb32 | 2013-07-05 14:41:06 -0700 | [diff] [blame] | 853 | WARN_ON(i915_gem_obj_ggtt_bound(obj)); |
Ben Widawsky | 40d74980 | 2013-07-31 16:59:59 -0700 | [diff] [blame] | 854 | ret = drm_mm_reserve_node(&ggtt_vm->mm, &vma->node); |
Ben Widawsky | c6cfb32 | 2013-07-05 14:41:06 -0700 | [diff] [blame] | 855 | if (ret) |
Ben Widawsky | b3a070c | 2013-07-05 14:41:02 -0700 | [diff] [blame] | 856 | DRM_DEBUG_KMS("Reservation failed\n"); |
Chris Wilson | ed2f345 | 2012-11-15 11:32:19 +0000 | [diff] [blame] | 857 | obj->has_global_gtt_mapping = 1; |
Ben Widawsky | 2f63315 | 2013-07-17 12:19:03 -0700 | [diff] [blame] | 858 | list_add(&vma->vma_link, &obj->vma_list); |
Chris Wilson | ed2f345 | 2012-11-15 11:32:19 +0000 | [diff] [blame] | 859 | } |
| 860 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 861 | dev_priv->gtt.base.start = start; |
| 862 | dev_priv->gtt.base.total = end - start; |
Daniel Vetter | 644ec02 | 2012-03-26 09:45:40 +0200 | [diff] [blame] | 863 | |
Chris Wilson | ed2f345 | 2012-11-15 11:32:19 +0000 | [diff] [blame] | 864 | /* Clear any non-preallocated blocks */ |
Ben Widawsky | 40d74980 | 2013-07-31 16:59:59 -0700 | [diff] [blame] | 865 | 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] | 866 | const unsigned long count = (hole_end - hole_start) / PAGE_SIZE; |
Chris Wilson | ed2f345 | 2012-11-15 11:32:19 +0000 | [diff] [blame] | 867 | DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n", |
| 868 | hole_start, hole_end); |
Ben Widawsky | 828c790 | 2013-10-16 09:21:30 -0700 | [diff] [blame] | 869 | ggtt_vm->clear_range(ggtt_vm, hole_start / PAGE_SIZE, count, true); |
Chris Wilson | ed2f345 | 2012-11-15 11:32:19 +0000 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | /* And finally clear the reserved guard page */ |
Ben Widawsky | 828c790 | 2013-10-16 09:21:30 -0700 | [diff] [blame] | 873 | ggtt_vm->clear_range(ggtt_vm, end / PAGE_SIZE - 1, 1, true); |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 874 | } |
| 875 | |
Ben Widawsky | d7e5008 | 2012-12-18 10:31:25 -0800 | [diff] [blame] | 876 | static bool |
| 877 | intel_enable_ppgtt(struct drm_device *dev) |
| 878 | { |
| 879 | if (i915_enable_ppgtt >= 0) |
| 880 | return i915_enable_ppgtt; |
| 881 | |
| 882 | #ifdef CONFIG_INTEL_IOMMU |
| 883 | /* Disable ppgtt on SNB if VT-d is on. */ |
| 884 | if (INTEL_INFO(dev)->gen == 6 && intel_iommu_gfx_mapped) |
| 885 | return false; |
| 886 | #endif |
| 887 | |
| 888 | return true; |
| 889 | } |
| 890 | |
| 891 | void i915_gem_init_global_gtt(struct drm_device *dev) |
| 892 | { |
| 893 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 894 | unsigned long gtt_size, mappable_size; |
Ben Widawsky | d7e5008 | 2012-12-18 10:31:25 -0800 | [diff] [blame] | 895 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 896 | gtt_size = dev_priv->gtt.base.total; |
Ben Widawsky | 93d1879 | 2013-01-17 12:45:17 -0800 | [diff] [blame] | 897 | mappable_size = dev_priv->gtt.mappable_end; |
Ben Widawsky | d7e5008 | 2012-12-18 10:31:25 -0800 | [diff] [blame] | 898 | |
| 899 | if (intel_enable_ppgtt(dev) && HAS_ALIASING_PPGTT(dev)) { |
Ben Widawsky | e78891c | 2013-01-25 16:41:04 -0800 | [diff] [blame] | 900 | int ret; |
Ben Widawsky | 3eb1c00 | 2013-04-08 18:43:52 -0700 | [diff] [blame] | 901 | |
| 902 | if (INTEL_INFO(dev)->gen <= 7) { |
| 903 | /* PPGTT pdes are stolen from global gtt ptes, so shrink the |
| 904 | * aperture accordingly when using aliasing ppgtt. */ |
Ben Widawsky | 6670a5a | 2013-06-27 16:30:04 -0700 | [diff] [blame] | 905 | gtt_size -= GEN6_PPGTT_PD_ENTRIES * PAGE_SIZE; |
Ben Widawsky | 3eb1c00 | 2013-04-08 18:43:52 -0700 | [diff] [blame] | 906 | } |
Ben Widawsky | d7e5008 | 2012-12-18 10:31:25 -0800 | [diff] [blame] | 907 | |
| 908 | i915_gem_setup_global_gtt(dev, 0, mappable_size, gtt_size); |
| 909 | |
| 910 | ret = i915_gem_init_aliasing_ppgtt(dev); |
Ben Widawsky | e78891c | 2013-01-25 16:41:04 -0800 | [diff] [blame] | 911 | if (!ret) |
Ben Widawsky | d7e5008 | 2012-12-18 10:31:25 -0800 | [diff] [blame] | 912 | return; |
Ben Widawsky | e78891c | 2013-01-25 16:41:04 -0800 | [diff] [blame] | 913 | |
| 914 | DRM_ERROR("Aliased PPGTT setup failed %d\n", ret); |
Ben Widawsky | 93bd864 | 2013-07-16 16:50:06 -0700 | [diff] [blame] | 915 | drm_mm_takedown(&dev_priv->gtt.base.mm); |
Ben Widawsky | 6670a5a | 2013-06-27 16:30:04 -0700 | [diff] [blame] | 916 | gtt_size += GEN6_PPGTT_PD_ENTRIES * PAGE_SIZE; |
Ben Widawsky | d7e5008 | 2012-12-18 10:31:25 -0800 | [diff] [blame] | 917 | } |
Ben Widawsky | e78891c | 2013-01-25 16:41:04 -0800 | [diff] [blame] | 918 | i915_gem_setup_global_gtt(dev, 0, mappable_size, gtt_size); |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 919 | } |
| 920 | |
| 921 | static int setup_scratch_page(struct drm_device *dev) |
| 922 | { |
| 923 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 924 | struct page *page; |
| 925 | dma_addr_t dma_addr; |
| 926 | |
| 927 | page = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO); |
| 928 | if (page == NULL) |
| 929 | return -ENOMEM; |
| 930 | get_page(page); |
| 931 | set_pages_uc(page, 1); |
| 932 | |
| 933 | #ifdef CONFIG_INTEL_IOMMU |
| 934 | dma_addr = pci_map_page(dev->pdev, page, 0, PAGE_SIZE, |
| 935 | PCI_DMA_BIDIRECTIONAL); |
| 936 | if (pci_dma_mapping_error(dev->pdev, dma_addr)) |
| 937 | return -EINVAL; |
| 938 | #else |
| 939 | dma_addr = page_to_phys(page); |
| 940 | #endif |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 941 | dev_priv->gtt.base.scratch.page = page; |
| 942 | dev_priv->gtt.base.scratch.addr = dma_addr; |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 943 | |
| 944 | return 0; |
| 945 | } |
| 946 | |
| 947 | static void teardown_scratch_page(struct drm_device *dev) |
| 948 | { |
| 949 | struct drm_i915_private *dev_priv = dev->dev_private; |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 950 | struct page *page = dev_priv->gtt.base.scratch.page; |
| 951 | |
| 952 | set_pages_wb(page, 1); |
| 953 | pci_unmap_page(dev->pdev, dev_priv->gtt.base.scratch.addr, |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 954 | PAGE_SIZE, PCI_DMA_BIDIRECTIONAL); |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 955 | put_page(page); |
| 956 | __free_page(page); |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 957 | } |
| 958 | |
| 959 | static inline unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl) |
| 960 | { |
| 961 | snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT; |
| 962 | snb_gmch_ctl &= SNB_GMCH_GGMS_MASK; |
| 963 | return snb_gmch_ctl << 20; |
| 964 | } |
| 965 | |
Ben Widawsky | 9459d25 | 2013-11-03 16:53:55 -0800 | [diff] [blame] | 966 | static inline unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl) |
| 967 | { |
| 968 | bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT; |
| 969 | bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK; |
| 970 | if (bdw_gmch_ctl) |
| 971 | bdw_gmch_ctl = 1 << bdw_gmch_ctl; |
| 972 | return bdw_gmch_ctl << 20; |
| 973 | } |
| 974 | |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 975 | static inline size_t gen6_get_stolen_size(u16 snb_gmch_ctl) |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 976 | { |
| 977 | snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT; |
| 978 | snb_gmch_ctl &= SNB_GMCH_GMS_MASK; |
| 979 | return snb_gmch_ctl << 25; /* 32 MB units */ |
| 980 | } |
| 981 | |
Ben Widawsky | 9459d25 | 2013-11-03 16:53:55 -0800 | [diff] [blame] | 982 | static inline size_t gen8_get_stolen_size(u16 bdw_gmch_ctl) |
| 983 | { |
| 984 | bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT; |
| 985 | bdw_gmch_ctl &= BDW_GMCH_GMS_MASK; |
| 986 | return bdw_gmch_ctl << 25; /* 32 MB units */ |
| 987 | } |
| 988 | |
Ben Widawsky | 6334013 | 2013-11-04 19:32:22 -0800 | [diff] [blame] | 989 | static int ggtt_probe_common(struct drm_device *dev, |
| 990 | size_t gtt_size) |
| 991 | { |
| 992 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 993 | phys_addr_t gtt_bus_addr; |
| 994 | int ret; |
| 995 | |
| 996 | /* For Modern GENs the PTEs and register space are split in the BAR */ |
| 997 | gtt_bus_addr = pci_resource_start(dev->pdev, 0) + |
| 998 | (pci_resource_len(dev->pdev, 0) / 2); |
| 999 | |
| 1000 | dev_priv->gtt.gsm = ioremap_wc(gtt_bus_addr, gtt_size); |
| 1001 | if (!dev_priv->gtt.gsm) { |
| 1002 | DRM_ERROR("Failed to map the gtt page table\n"); |
| 1003 | return -ENOMEM; |
| 1004 | } |
| 1005 | |
| 1006 | ret = setup_scratch_page(dev); |
| 1007 | if (ret) { |
| 1008 | DRM_ERROR("Scratch setup failed\n"); |
| 1009 | /* iounmap will also get called at remove, but meh */ |
| 1010 | iounmap(dev_priv->gtt.gsm); |
| 1011 | } |
| 1012 | |
| 1013 | return ret; |
| 1014 | } |
| 1015 | |
Ben Widawsky | fbe5d36 | 2013-11-04 19:56:49 -0800 | [diff] [blame^] | 1016 | /* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability |
| 1017 | * bits. When using advanced contexts each context stores its own PAT, but |
| 1018 | * writing this data shouldn't be harmful even in those cases. */ |
| 1019 | static void gen8_setup_private_ppat(struct drm_i915_private *dev_priv) |
| 1020 | { |
| 1021 | #define GEN8_PPAT_UC (0<<0) |
| 1022 | #define GEN8_PPAT_WC (1<<0) |
| 1023 | #define GEN8_PPAT_WT (2<<0) |
| 1024 | #define GEN8_PPAT_WB (3<<0) |
| 1025 | #define GEN8_PPAT_ELLC_OVERRIDE (0<<2) |
| 1026 | /* FIXME(BDW): Bspec is completely confused about cache control bits. */ |
| 1027 | #define GEN8_PPAT_LLC (1<<2) |
| 1028 | #define GEN8_PPAT_LLCELLC (2<<2) |
| 1029 | #define GEN8_PPAT_LLCeLLC (3<<2) |
| 1030 | #define GEN8_PPAT_AGE(x) (x<<4) |
| 1031 | #define GEN8_PPAT(i, x) ((uint64_t) (x) << ((i) * 8)) |
| 1032 | uint64_t pat; |
| 1033 | |
| 1034 | pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */ |
| 1035 | GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */ |
| 1036 | GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */ |
| 1037 | GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */ |
| 1038 | GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) | |
| 1039 | GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) | |
| 1040 | GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) | |
| 1041 | GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3)); |
| 1042 | |
| 1043 | /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b |
| 1044 | * write would work. */ |
| 1045 | I915_WRITE(GEN8_PRIVATE_PAT, pat); |
| 1046 | I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32); |
| 1047 | } |
| 1048 | |
Ben Widawsky | 6334013 | 2013-11-04 19:32:22 -0800 | [diff] [blame] | 1049 | static int gen8_gmch_probe(struct drm_device *dev, |
| 1050 | size_t *gtt_total, |
| 1051 | size_t *stolen, |
| 1052 | phys_addr_t *mappable_base, |
| 1053 | unsigned long *mappable_end) |
| 1054 | { |
| 1055 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 1056 | unsigned int gtt_size; |
| 1057 | u16 snb_gmch_ctl; |
| 1058 | int ret; |
| 1059 | |
| 1060 | /* TODO: We're not aware of mappable constraints on gen8 yet */ |
| 1061 | *mappable_base = pci_resource_start(dev->pdev, 2); |
| 1062 | *mappable_end = pci_resource_len(dev->pdev, 2); |
| 1063 | |
| 1064 | if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(39))) |
| 1065 | pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(39)); |
| 1066 | |
| 1067 | pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl); |
| 1068 | |
| 1069 | *stolen = gen8_get_stolen_size(snb_gmch_ctl); |
| 1070 | |
| 1071 | gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl); |
Ben Widawsky | d31eb10 | 2013-11-02 21:07:17 -0700 | [diff] [blame] | 1072 | *gtt_total = (gtt_size / sizeof(gen8_gtt_pte_t)) << PAGE_SHIFT; |
Ben Widawsky | 6334013 | 2013-11-04 19:32:22 -0800 | [diff] [blame] | 1073 | |
Ben Widawsky | fbe5d36 | 2013-11-04 19:56:49 -0800 | [diff] [blame^] | 1074 | gen8_setup_private_ppat(dev_priv); |
| 1075 | |
Ben Widawsky | 6334013 | 2013-11-04 19:32:22 -0800 | [diff] [blame] | 1076 | ret = ggtt_probe_common(dev, gtt_size); |
| 1077 | |
Ben Widawsky | 94ec8f6 | 2013-11-02 21:07:18 -0700 | [diff] [blame] | 1078 | dev_priv->gtt.base.clear_range = gen8_ggtt_clear_range; |
| 1079 | dev_priv->gtt.base.insert_entries = gen8_ggtt_insert_entries; |
Ben Widawsky | 6334013 | 2013-11-04 19:32:22 -0800 | [diff] [blame] | 1080 | |
| 1081 | return ret; |
| 1082 | } |
| 1083 | |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 1084 | static int gen6_gmch_probe(struct drm_device *dev, |
| 1085 | size_t *gtt_total, |
Ben Widawsky | 41907dd | 2013-02-08 11:32:47 -0800 | [diff] [blame] | 1086 | size_t *stolen, |
| 1087 | phys_addr_t *mappable_base, |
| 1088 | unsigned long *mappable_end) |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 1089 | { |
| 1090 | struct drm_i915_private *dev_priv = dev->dev_private; |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 1091 | unsigned int gtt_size; |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 1092 | u16 snb_gmch_ctl; |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 1093 | int ret; |
| 1094 | |
Ben Widawsky | 41907dd | 2013-02-08 11:32:47 -0800 | [diff] [blame] | 1095 | *mappable_base = pci_resource_start(dev->pdev, 2); |
| 1096 | *mappable_end = pci_resource_len(dev->pdev, 2); |
| 1097 | |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 1098 | /* 64/512MB is the current min/max we actually know of, but this is just |
| 1099 | * a coarse sanity check. |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 1100 | */ |
Ben Widawsky | 41907dd | 2013-02-08 11:32:47 -0800 | [diff] [blame] | 1101 | if ((*mappable_end < (64<<20) || (*mappable_end > (512<<20)))) { |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 1102 | DRM_ERROR("Unknown GMADR size (%lx)\n", |
| 1103 | dev_priv->gtt.mappable_end); |
| 1104 | return -ENXIO; |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 1105 | } |
| 1106 | |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 1107 | if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(40))) |
| 1108 | pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(40)); |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 1109 | pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl); |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 1110 | |
Ben Widawsky | 6334013 | 2013-11-04 19:32:22 -0800 | [diff] [blame] | 1111 | *stolen = gen6_get_stolen_size(snb_gmch_ctl); |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 1112 | |
Ben Widawsky | 6334013 | 2013-11-04 19:32:22 -0800 | [diff] [blame] | 1113 | gtt_size = gen6_get_total_gtt_size(snb_gmch_ctl); |
| 1114 | *gtt_total = (gtt_size / sizeof(gen6_gtt_pte_t)) << PAGE_SHIFT; |
Ben Widawsky | a93e416 | 2013-04-08 18:43:47 -0700 | [diff] [blame] | 1115 | |
Ben Widawsky | 6334013 | 2013-11-04 19:32:22 -0800 | [diff] [blame] | 1116 | ret = ggtt_probe_common(dev, gtt_size); |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 1117 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 1118 | dev_priv->gtt.base.clear_range = gen6_ggtt_clear_range; |
| 1119 | dev_priv->gtt.base.insert_entries = gen6_ggtt_insert_entries; |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 1120 | |
| 1121 | return ret; |
| 1122 | } |
| 1123 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 1124 | static void gen6_gmch_remove(struct i915_address_space *vm) |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 1125 | { |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 1126 | |
| 1127 | struct i915_gtt *gtt = container_of(vm, struct i915_gtt, base); |
| 1128 | iounmap(gtt->gsm); |
| 1129 | teardown_scratch_page(vm->dev); |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 1130 | } |
| 1131 | |
| 1132 | static int i915_gmch_probe(struct drm_device *dev, |
| 1133 | size_t *gtt_total, |
Ben Widawsky | 41907dd | 2013-02-08 11:32:47 -0800 | [diff] [blame] | 1134 | size_t *stolen, |
| 1135 | phys_addr_t *mappable_base, |
| 1136 | unsigned long *mappable_end) |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 1137 | { |
| 1138 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 1139 | int ret; |
| 1140 | |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 1141 | ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->dev->pdev, NULL); |
| 1142 | if (!ret) { |
| 1143 | DRM_ERROR("failed to set up gmch\n"); |
| 1144 | return -EIO; |
| 1145 | } |
| 1146 | |
Ben Widawsky | 41907dd | 2013-02-08 11:32:47 -0800 | [diff] [blame] | 1147 | intel_gtt_get(gtt_total, stolen, mappable_base, mappable_end); |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 1148 | |
| 1149 | dev_priv->gtt.do_idle_maps = needs_idle_maps(dev_priv->dev); |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 1150 | dev_priv->gtt.base.clear_range = i915_ggtt_clear_range; |
| 1151 | dev_priv->gtt.base.insert_entries = i915_ggtt_insert_entries; |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 1152 | |
| 1153 | return 0; |
| 1154 | } |
| 1155 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 1156 | static void i915_gmch_remove(struct i915_address_space *vm) |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 1157 | { |
| 1158 | intel_gmch_remove(); |
| 1159 | } |
| 1160 | |
| 1161 | int i915_gem_gtt_init(struct drm_device *dev) |
| 1162 | { |
| 1163 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 1164 | struct i915_gtt *gtt = &dev_priv->gtt; |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 1165 | int ret; |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 1166 | |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 1167 | if (INTEL_INFO(dev)->gen <= 5) { |
Ben Widawsky | b2f21b4 | 2013-06-27 16:30:20 -0700 | [diff] [blame] | 1168 | gtt->gtt_probe = i915_gmch_probe; |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 1169 | gtt->base.cleanup = i915_gmch_remove; |
Ben Widawsky | 6334013 | 2013-11-04 19:32:22 -0800 | [diff] [blame] | 1170 | } else if (INTEL_INFO(dev)->gen < 8) { |
Ben Widawsky | b2f21b4 | 2013-06-27 16:30:20 -0700 | [diff] [blame] | 1171 | gtt->gtt_probe = gen6_gmch_probe; |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 1172 | gtt->base.cleanup = gen6_gmch_remove; |
Ben Widawsky | 4d15c14 | 2013-07-04 11:02:06 -0700 | [diff] [blame] | 1173 | if (IS_HASWELL(dev) && dev_priv->ellc_size) |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 1174 | gtt->base.pte_encode = iris_pte_encode; |
Ben Widawsky | 4d15c14 | 2013-07-04 11:02:06 -0700 | [diff] [blame] | 1175 | else if (IS_HASWELL(dev)) |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 1176 | gtt->base.pte_encode = hsw_pte_encode; |
Ben Widawsky | b2f21b4 | 2013-06-27 16:30:20 -0700 | [diff] [blame] | 1177 | else if (IS_VALLEYVIEW(dev)) |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 1178 | gtt->base.pte_encode = byt_pte_encode; |
Chris Wilson | 350ec88 | 2013-08-06 13:17:02 +0100 | [diff] [blame] | 1179 | else if (INTEL_INFO(dev)->gen >= 7) |
| 1180 | gtt->base.pte_encode = ivb_pte_encode; |
Ben Widawsky | b2f21b4 | 2013-06-27 16:30:20 -0700 | [diff] [blame] | 1181 | else |
Chris Wilson | 350ec88 | 2013-08-06 13:17:02 +0100 | [diff] [blame] | 1182 | gtt->base.pte_encode = snb_pte_encode; |
Ben Widawsky | 6334013 | 2013-11-04 19:32:22 -0800 | [diff] [blame] | 1183 | } else { |
| 1184 | dev_priv->gtt.gtt_probe = gen8_gmch_probe; |
| 1185 | dev_priv->gtt.base.cleanup = gen6_gmch_remove; |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 1186 | } |
| 1187 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 1188 | ret = gtt->gtt_probe(dev, >t->base.total, >t->stolen_size, |
Ben Widawsky | b2f21b4 | 2013-06-27 16:30:20 -0700 | [diff] [blame] | 1189 | >t->mappable_base, >t->mappable_end); |
Ben Widawsky | a54c0c2 | 2013-01-24 14:45:00 -0800 | [diff] [blame] | 1190 | if (ret) |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 1191 | return ret; |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 1192 | |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 1193 | gtt->base.dev = dev; |
| 1194 | |
Ben Widawsky | baa09f5 | 2013-01-24 13:49:57 -0800 | [diff] [blame] | 1195 | /* GMADR is the PCI mmio aperture into the global GTT. */ |
Ben Widawsky | 853ba5d | 2013-07-16 16:50:05 -0700 | [diff] [blame] | 1196 | DRM_INFO("Memory usable by graphics device = %zdM\n", |
| 1197 | gtt->base.total >> 20); |
Ben Widawsky | b2f21b4 | 2013-06-27 16:30:20 -0700 | [diff] [blame] | 1198 | DRM_DEBUG_DRIVER("GMADR size = %ldM\n", gtt->mappable_end >> 20); |
| 1199 | DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", gtt->stolen_size >> 20); |
Daniel Vetter | 7faf1ab | 2013-01-24 14:44:55 -0800 | [diff] [blame] | 1200 | |
Ben Widawsky | e76e9ae | 2012-11-04 09:21:27 -0800 | [diff] [blame] | 1201 | return 0; |
Daniel Vetter | 644ec02 | 2012-03-26 09:45:40 +0200 | [diff] [blame] | 1202 | } |