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