1e3adcf8fSFrançois Tigeot /* 2e3adcf8fSFrançois Tigeot * Copyright © 2010 Daniel Vetter 3ba55f2f5SFrançois Tigeot * Copyright © 2011-2014 Intel Corporation 4e3adcf8fSFrançois Tigeot * 5e3adcf8fSFrançois Tigeot * Permission is hereby granted, free of charge, to any person obtaining a 6e3adcf8fSFrançois Tigeot * copy of this software and associated documentation files (the "Software"), 7e3adcf8fSFrançois Tigeot * to deal in the Software without restriction, including without limitation 8e3adcf8fSFrançois Tigeot * the rights to use, copy, modify, merge, publish, distribute, sublicense, 9e3adcf8fSFrançois Tigeot * and/or sell copies of the Software, and to permit persons to whom the 10e3adcf8fSFrançois Tigeot * Software is furnished to do so, subject to the following conditions: 11e3adcf8fSFrançois Tigeot * 12e3adcf8fSFrançois Tigeot * The above copyright notice and this permission notice (including the next 13e3adcf8fSFrançois Tigeot * paragraph) shall be included in all copies or substantial portions of the 14e3adcf8fSFrançois Tigeot * Software. 15e3adcf8fSFrançois Tigeot * 16e3adcf8fSFrançois Tigeot * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17e3adcf8fSFrançois Tigeot * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18e3adcf8fSFrançois Tigeot * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19e3adcf8fSFrançois Tigeot * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20e3adcf8fSFrançois Tigeot * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21e3adcf8fSFrançois Tigeot * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 22e3adcf8fSFrançois Tigeot * IN THE SOFTWARE. 23e3adcf8fSFrançois Tigeot * 24e3adcf8fSFrançois Tigeot */ 25e3adcf8fSFrançois Tigeot 26ba55f2f5SFrançois Tigeot #include <linux/seq_file.h> 2718e26a6dSFrançois Tigeot #include <drm/drmP.h> 285c6c6f23SFrançois Tigeot #include <drm/i915_drm.h> 29e3adcf8fSFrançois Tigeot #include "i915_drv.h" 30477eb7f9SFrançois Tigeot #include "i915_vgpu.h" 31477eb7f9SFrançois Tigeot #include "i915_trace.h" 32e3adcf8fSFrançois Tigeot #include "intel_drv.h" 33e3adcf8fSFrançois Tigeot 34477eb7f9SFrançois Tigeot #include <linux/bitmap.h> 3582046b5cSFrançois Tigeot #include <linux/highmem.h> 3682046b5cSFrançois Tigeot 372c9916cdSFrançois Tigeot /** 382c9916cdSFrançois Tigeot * DOC: Global GTT views 392c9916cdSFrançois Tigeot * 402c9916cdSFrançois Tigeot * Background and previous state 412c9916cdSFrançois Tigeot * 422c9916cdSFrançois Tigeot * Historically objects could exists (be bound) in global GTT space only as 432c9916cdSFrançois Tigeot * singular instances with a view representing all of the object's backing pages 442c9916cdSFrançois Tigeot * in a linear fashion. This view will be called a normal view. 452c9916cdSFrançois Tigeot * 462c9916cdSFrançois Tigeot * To support multiple views of the same object, where the number of mapped 472c9916cdSFrançois Tigeot * pages is not equal to the backing store, or where the layout of the pages 482c9916cdSFrançois Tigeot * is not linear, concept of a GGTT view was added. 492c9916cdSFrançois Tigeot * 502c9916cdSFrançois Tigeot * One example of an alternative view is a stereo display driven by a single 512c9916cdSFrançois Tigeot * image. In this case we would have a framebuffer looking like this 522c9916cdSFrançois Tigeot * (2x2 pages): 532c9916cdSFrançois Tigeot * 542c9916cdSFrançois Tigeot * 12 552c9916cdSFrançois Tigeot * 34 562c9916cdSFrançois Tigeot * 572c9916cdSFrançois Tigeot * Above would represent a normal GGTT view as normally mapped for GPU or CPU 582c9916cdSFrançois Tigeot * rendering. In contrast, fed to the display engine would be an alternative 592c9916cdSFrançois Tigeot * view which could look something like this: 602c9916cdSFrançois Tigeot * 612c9916cdSFrançois Tigeot * 1212 622c9916cdSFrançois Tigeot * 3434 632c9916cdSFrançois Tigeot * 642c9916cdSFrançois Tigeot * In this example both the size and layout of pages in the alternative view is 652c9916cdSFrançois Tigeot * different from the normal view. 662c9916cdSFrançois Tigeot * 672c9916cdSFrançois Tigeot * Implementation and usage 682c9916cdSFrançois Tigeot * 692c9916cdSFrançois Tigeot * GGTT views are implemented using VMAs and are distinguished via enum 702c9916cdSFrançois Tigeot * i915_ggtt_view_type and struct i915_ggtt_view. 712c9916cdSFrançois Tigeot * 722c9916cdSFrançois Tigeot * A new flavour of core GEM functions which work with GGTT bound objects were 73477eb7f9SFrançois Tigeot * added with the _ggtt_ infix, and sometimes with _view postfix to avoid 74477eb7f9SFrançois Tigeot * renaming in large amounts of code. They take the struct i915_ggtt_view 75477eb7f9SFrançois Tigeot * parameter encapsulating all metadata required to implement a view. 762c9916cdSFrançois Tigeot * 772c9916cdSFrançois Tigeot * As a helper for callers which are only interested in the normal view, 782c9916cdSFrançois Tigeot * globally const i915_ggtt_view_normal singleton instance exists. All old core 792c9916cdSFrançois Tigeot * GEM API functions, the ones not taking the view parameter, are operating on, 802c9916cdSFrançois Tigeot * or with the normal GGTT view. 812c9916cdSFrançois Tigeot * 822c9916cdSFrançois Tigeot * Code wanting to add or use a new GGTT view needs to: 832c9916cdSFrançois Tigeot * 842c9916cdSFrançois Tigeot * 1. Add a new enum with a suitable name. 852c9916cdSFrançois Tigeot * 2. Extend the metadata in the i915_ggtt_view structure if required. 862c9916cdSFrançois Tigeot * 3. Add support to i915_get_vma_pages(). 872c9916cdSFrançois Tigeot * 882c9916cdSFrançois Tigeot * New views are required to build a scatter-gather table from within the 892c9916cdSFrançois Tigeot * i915_get_vma_pages function. This table is stored in the vma.ggtt_view and 902c9916cdSFrançois Tigeot * exists for the lifetime of an VMA. 912c9916cdSFrançois Tigeot * 922c9916cdSFrançois Tigeot * Core API is designed to have copy semantics which means that passed in 932c9916cdSFrançois Tigeot * struct i915_ggtt_view does not need to be persistent (left around after 942c9916cdSFrançois Tigeot * calling the core API functions). 952c9916cdSFrançois Tigeot * 962c9916cdSFrançois Tigeot */ 972c9916cdSFrançois Tigeot 9819c468b4SFrançois Tigeot static int 9919c468b4SFrançois Tigeot i915_get_ggtt_vma_pages(struct i915_vma *vma); 10019c468b4SFrançois Tigeot 1012c9916cdSFrançois Tigeot const struct i915_ggtt_view i915_ggtt_view_normal; 102477eb7f9SFrançois Tigeot const struct i915_ggtt_view i915_ggtt_view_rotated = { 103477eb7f9SFrançois Tigeot .type = I915_GGTT_VIEW_ROTATED 104477eb7f9SFrançois Tigeot }; 1052c9916cdSFrançois Tigeot 10624edb884SFrançois Tigeot static int sanitize_enable_ppgtt(struct drm_device *dev, int enable_ppgtt) 10724edb884SFrançois Tigeot { 1082c9916cdSFrançois Tigeot bool has_aliasing_ppgtt; 1092c9916cdSFrançois Tigeot bool has_full_ppgtt; 1102c9916cdSFrançois Tigeot 1112c9916cdSFrançois Tigeot has_aliasing_ppgtt = INTEL_INFO(dev)->gen >= 6; 1122c9916cdSFrançois Tigeot has_full_ppgtt = INTEL_INFO(dev)->gen >= 7; 1132c9916cdSFrançois Tigeot 114477eb7f9SFrançois Tigeot if (intel_vgpu_active(dev)) 115477eb7f9SFrançois Tigeot has_full_ppgtt = false; /* emulation is too hard */ 116477eb7f9SFrançois Tigeot 1172c9916cdSFrançois Tigeot /* 1182c9916cdSFrançois Tigeot * We don't allow disabling PPGTT for gen9+ as it's a requirement for 1192c9916cdSFrançois Tigeot * execlists, the sole mechanism available to submit work. 1202c9916cdSFrançois Tigeot */ 1212c9916cdSFrançois Tigeot if (INTEL_INFO(dev)->gen < 9 && 1222c9916cdSFrançois Tigeot (enable_ppgtt == 0 || !has_aliasing_ppgtt)) 12324edb884SFrançois Tigeot return 0; 12424edb884SFrançois Tigeot 12524edb884SFrançois Tigeot if (enable_ppgtt == 1) 12624edb884SFrançois Tigeot return 1; 12724edb884SFrançois Tigeot 1282c9916cdSFrançois Tigeot if (enable_ppgtt == 2 && has_full_ppgtt) 12924edb884SFrançois Tigeot return 2; 13024edb884SFrançois Tigeot 131ba55f2f5SFrançois Tigeot #ifdef CONFIG_INTEL_IOMMU 132ba55f2f5SFrançois Tigeot /* Disable ppgtt on SNB if VT-d is on. */ 133ba55f2f5SFrançois Tigeot if (INTEL_INFO(dev)->gen == 6 && intel_iommu_gfx_mapped) { 134ba55f2f5SFrançois Tigeot DRM_INFO("Disabling PPGTT because VT-d is on\n"); 13524edb884SFrançois Tigeot return 0; 136ba55f2f5SFrançois Tigeot } 137ba55f2f5SFrançois Tigeot #endif 138f4e1c372SFrançois Tigeot 13924edb884SFrançois Tigeot /* Early VLV doesn't have this */ 14024edb884SFrançois Tigeot if (IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) && 14130d4b906SFrançois Tigeot dev->pdev->revision < 0xb) { 14224edb884SFrançois Tigeot DRM_DEBUG_DRIVER("disabling PPGTT on pre-B3 step VLV\n"); 14324edb884SFrançois Tigeot return 0; 14424edb884SFrançois Tigeot } 14524edb884SFrançois Tigeot 1462c9916cdSFrançois Tigeot if (INTEL_INFO(dev)->gen >= 8 && i915.enable_execlists) 1472c9916cdSFrançois Tigeot return 2; 1482c9916cdSFrançois Tigeot else 1492c9916cdSFrançois Tigeot return has_aliasing_ppgtt ? 1 : 0; 150ba55f2f5SFrançois Tigeot } 1519edbd4a0SFrançois Tigeot 15219c468b4SFrançois Tigeot static int ppgtt_bind_vma(struct i915_vma *vma, 153ba55f2f5SFrançois Tigeot enum i915_cache_level cache_level, 15419c468b4SFrançois Tigeot u32 unused) 15519c468b4SFrançois Tigeot { 15619c468b4SFrançois Tigeot u32 pte_flags = 0; 1579edbd4a0SFrançois Tigeot 15819c468b4SFrançois Tigeot /* Currently applicable only to VLV */ 15919c468b4SFrançois Tigeot if (vma->obj->gt_ro) 16019c468b4SFrançois Tigeot pte_flags |= PTE_READ_ONLY; 16119c468b4SFrançois Tigeot 16219c468b4SFrançois Tigeot vma->vm->insert_entries(vma->vm, vma->obj->pages, vma->node.start, 16319c468b4SFrançois Tigeot cache_level, pte_flags); 16419c468b4SFrançois Tigeot 16519c468b4SFrançois Tigeot return 0; 16619c468b4SFrançois Tigeot } 16719c468b4SFrançois Tigeot 16819c468b4SFrançois Tigeot static void ppgtt_unbind_vma(struct i915_vma *vma) 16919c468b4SFrançois Tigeot { 17019c468b4SFrançois Tigeot vma->vm->clear_range(vma->vm, 17119c468b4SFrançois Tigeot vma->node.start, 17219c468b4SFrançois Tigeot vma->obj->base.size, 17319c468b4SFrançois Tigeot true); 17419c468b4SFrançois Tigeot } 17519c468b4SFrançois Tigeot 17619c468b4SFrançois Tigeot static gen8_pte_t gen8_pte_encode(dma_addr_t addr, 1779edbd4a0SFrançois Tigeot enum i915_cache_level level, 1789edbd4a0SFrançois Tigeot bool valid) 1799edbd4a0SFrançois Tigeot { 180477eb7f9SFrançois Tigeot gen8_pte_t pte = valid ? _PAGE_PRESENT | _PAGE_RW : 0; 1819edbd4a0SFrançois Tigeot pte |= addr; 182ba55f2f5SFrançois Tigeot 183ba55f2f5SFrançois Tigeot switch (level) { 184ba55f2f5SFrançois Tigeot case I915_CACHE_NONE: 1859edbd4a0SFrançois Tigeot pte |= PPAT_UNCACHED_INDEX; 186ba55f2f5SFrançois Tigeot break; 187ba55f2f5SFrançois Tigeot case I915_CACHE_WT: 188ba55f2f5SFrançois Tigeot pte |= PPAT_DISPLAY_ELLC_INDEX; 189ba55f2f5SFrançois Tigeot break; 190ba55f2f5SFrançois Tigeot default: 191ba55f2f5SFrançois Tigeot pte |= PPAT_CACHED_INDEX; 192ba55f2f5SFrançois Tigeot break; 193ba55f2f5SFrançois Tigeot } 194ba55f2f5SFrançois Tigeot 1959edbd4a0SFrançois Tigeot return pte; 1969edbd4a0SFrançois Tigeot } 1979edbd4a0SFrançois Tigeot 19819c468b4SFrançois Tigeot static gen8_pde_t gen8_pde_encode(struct drm_device *dev, 199f4e1c372SFrançois Tigeot dma_addr_t addr, 200f4e1c372SFrançois Tigeot enum i915_cache_level level) 201e3adcf8fSFrançois Tigeot { 202477eb7f9SFrançois Tigeot gen8_pde_t pde = _PAGE_PRESENT | _PAGE_RW; 2039edbd4a0SFrançois Tigeot pde |= addr; 2049edbd4a0SFrançois Tigeot if (level != I915_CACHE_NONE) 2059edbd4a0SFrançois Tigeot pde |= PPAT_CACHED_PDE_INDEX; 2069edbd4a0SFrançois Tigeot else 2079edbd4a0SFrançois Tigeot pde |= PPAT_UNCACHED_INDEX; 2089edbd4a0SFrançois Tigeot return pde; 2099edbd4a0SFrançois Tigeot } 2109edbd4a0SFrançois Tigeot 211477eb7f9SFrançois Tigeot static gen6_pte_t snb_pte_encode(dma_addr_t addr, 2129edbd4a0SFrançois Tigeot enum i915_cache_level level, 21324edb884SFrançois Tigeot bool valid, u32 unused) 2149edbd4a0SFrançois Tigeot { 215477eb7f9SFrançois Tigeot gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0; 216f4e1c372SFrançois Tigeot pte |= GEN6_PTE_ADDR_ENCODE(addr); 217f4e1c372SFrançois Tigeot 218f4e1c372SFrançois Tigeot switch (level) { 2199edbd4a0SFrançois Tigeot case I915_CACHE_L3_LLC: 2209edbd4a0SFrançois Tigeot case I915_CACHE_LLC: 2219edbd4a0SFrançois Tigeot pte |= GEN6_PTE_CACHE_LLC; 2229edbd4a0SFrançois Tigeot break; 2239edbd4a0SFrançois Tigeot case I915_CACHE_NONE: 2249edbd4a0SFrançois Tigeot pte |= GEN6_PTE_UNCACHED; 2259edbd4a0SFrançois Tigeot break; 2269edbd4a0SFrançois Tigeot default: 2272c9916cdSFrançois Tigeot MISSING_CASE(level); 2289edbd4a0SFrançois Tigeot } 2299edbd4a0SFrançois Tigeot 2309edbd4a0SFrançois Tigeot return pte; 2319edbd4a0SFrançois Tigeot } 2329edbd4a0SFrançois Tigeot 233477eb7f9SFrançois Tigeot static gen6_pte_t ivb_pte_encode(dma_addr_t addr, 2349edbd4a0SFrançois Tigeot enum i915_cache_level level, 23524edb884SFrançois Tigeot bool valid, u32 unused) 2369edbd4a0SFrançois Tigeot { 237477eb7f9SFrançois Tigeot gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0; 2389edbd4a0SFrançois Tigeot pte |= GEN6_PTE_ADDR_ENCODE(addr); 2399edbd4a0SFrançois Tigeot 2409edbd4a0SFrançois Tigeot switch (level) { 2419edbd4a0SFrançois Tigeot case I915_CACHE_L3_LLC: 2429edbd4a0SFrançois Tigeot pte |= GEN7_PTE_CACHE_L3_LLC; 243f4e1c372SFrançois Tigeot break; 244f4e1c372SFrançois Tigeot case I915_CACHE_LLC: 245f4e1c372SFrançois Tigeot pte |= GEN6_PTE_CACHE_LLC; 246f4e1c372SFrançois Tigeot break; 247f4e1c372SFrançois Tigeot case I915_CACHE_NONE: 248f4e1c372SFrançois Tigeot pte |= GEN6_PTE_UNCACHED; 249f4e1c372SFrançois Tigeot break; 250f4e1c372SFrançois Tigeot default: 2512c9916cdSFrançois Tigeot MISSING_CASE(level); 252f4e1c372SFrançois Tigeot } 253f4e1c372SFrançois Tigeot 254f4e1c372SFrançois Tigeot return pte; 255f4e1c372SFrançois Tigeot } 256f4e1c372SFrançois Tigeot 257477eb7f9SFrançois Tigeot static gen6_pte_t byt_pte_encode(dma_addr_t addr, 2589edbd4a0SFrançois Tigeot enum i915_cache_level level, 25924edb884SFrançois Tigeot bool valid, u32 flags) 2605d0b1887SFrançois Tigeot { 261477eb7f9SFrançois Tigeot gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0; 2625d0b1887SFrançois Tigeot pte |= GEN6_PTE_ADDR_ENCODE(addr); 2635d0b1887SFrançois Tigeot 26424edb884SFrançois Tigeot if (!(flags & PTE_READ_ONLY)) 2655d0b1887SFrançois Tigeot pte |= BYT_PTE_WRITEABLE; 2665d0b1887SFrançois Tigeot 2675d0b1887SFrançois Tigeot if (level != I915_CACHE_NONE) 2685d0b1887SFrançois Tigeot pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES; 2695d0b1887SFrançois Tigeot 2705d0b1887SFrançois Tigeot return pte; 2715d0b1887SFrançois Tigeot } 2725d0b1887SFrançois Tigeot 273477eb7f9SFrançois Tigeot static gen6_pte_t hsw_pte_encode(dma_addr_t addr, 2749edbd4a0SFrançois Tigeot enum i915_cache_level level, 27524edb884SFrançois Tigeot bool valid, u32 unused) 2765d0b1887SFrançois Tigeot { 277477eb7f9SFrançois Tigeot gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0; 2789edbd4a0SFrançois Tigeot pte |= HSW_PTE_ADDR_ENCODE(addr); 2795d0b1887SFrançois Tigeot 2805d0b1887SFrançois Tigeot if (level != I915_CACHE_NONE) 2819edbd4a0SFrançois Tigeot pte |= HSW_WB_LLC_AGE3; 2825d0b1887SFrançois Tigeot 2835d0b1887SFrançois Tigeot return pte; 2845d0b1887SFrançois Tigeot } 2855d0b1887SFrançois Tigeot 286477eb7f9SFrançois Tigeot static gen6_pte_t iris_pte_encode(dma_addr_t addr, 2879edbd4a0SFrançois Tigeot enum i915_cache_level level, 28824edb884SFrançois Tigeot bool valid, u32 unused) 2899edbd4a0SFrançois Tigeot { 290477eb7f9SFrançois Tigeot gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0; 2919edbd4a0SFrançois Tigeot pte |= HSW_PTE_ADDR_ENCODE(addr); 2929edbd4a0SFrançois Tigeot 2939edbd4a0SFrançois Tigeot switch (level) { 2949edbd4a0SFrançois Tigeot case I915_CACHE_NONE: 2959edbd4a0SFrançois Tigeot break; 2969edbd4a0SFrançois Tigeot case I915_CACHE_WT: 2979edbd4a0SFrançois Tigeot pte |= HSW_WT_ELLC_LLC_AGE3; 2989edbd4a0SFrançois Tigeot break; 2999edbd4a0SFrançois Tigeot default: 3009edbd4a0SFrançois Tigeot pte |= HSW_WB_ELLC_LLC_AGE3; 3019edbd4a0SFrançois Tigeot break; 3029edbd4a0SFrançois Tigeot } 3039edbd4a0SFrançois Tigeot 3049edbd4a0SFrançois Tigeot return pte; 3059edbd4a0SFrançois Tigeot } 3069edbd4a0SFrançois Tigeot 307477eb7f9SFrançois Tigeot #define i915_dma_unmap_single(px, dev) \ 308477eb7f9SFrançois Tigeot __i915_dma_unmap_single((px)->daddr, dev) 309477eb7f9SFrançois Tigeot 31019c468b4SFrançois Tigeot static void __i915_dma_unmap_single(dma_addr_t daddr, 311477eb7f9SFrançois Tigeot struct drm_device *dev) 312477eb7f9SFrançois Tigeot { 313477eb7f9SFrançois Tigeot #if 0 314477eb7f9SFrançois Tigeot struct device *device = &dev->pdev->dev; 315477eb7f9SFrançois Tigeot 316477eb7f9SFrançois Tigeot dma_unmap_page(device, daddr, 4096, PCI_DMA_BIDIRECTIONAL); 317477eb7f9SFrançois Tigeot #endif 318477eb7f9SFrançois Tigeot } 319477eb7f9SFrançois Tigeot 320477eb7f9SFrançois Tigeot /** 321477eb7f9SFrançois Tigeot * i915_dma_map_single() - Create a dma mapping for a page table/dir/etc. 322477eb7f9SFrançois Tigeot * @px: Page table/dir/etc to get a DMA map for 323477eb7f9SFrançois Tigeot * @dev: drm device 324477eb7f9SFrançois Tigeot * 325477eb7f9SFrançois Tigeot * Page table allocations are unified across all gens. They always require a 326477eb7f9SFrançois Tigeot * single 4k allocation, as well as a DMA mapping. If we keep the structs 327477eb7f9SFrançois Tigeot * symmetric here, the simple macro covers us for every page table type. 328477eb7f9SFrançois Tigeot * 329477eb7f9SFrançois Tigeot * Return: 0 if success. 330477eb7f9SFrançois Tigeot */ 331477eb7f9SFrançois Tigeot #define i915_dma_map_single(px, dev) \ 332477eb7f9SFrançois Tigeot i915_dma_map_page_single((px)->page, (dev), &(px)->daddr) 333477eb7f9SFrançois Tigeot 33419c468b4SFrançois Tigeot static int i915_dma_map_page_single(struct vm_page *page, 335477eb7f9SFrançois Tigeot struct drm_device *dev, 336477eb7f9SFrançois Tigeot dma_addr_t *daddr) 337477eb7f9SFrançois Tigeot { 338477eb7f9SFrançois Tigeot struct device *device = dev->pdev->dev; 339477eb7f9SFrançois Tigeot 340477eb7f9SFrançois Tigeot *daddr = dma_map_page(device, page, 0, 4096, PCI_DMA_BIDIRECTIONAL); 341477eb7f9SFrançois Tigeot if (dma_mapping_error(device, *daddr)) 342477eb7f9SFrançois Tigeot return -ENOMEM; 343477eb7f9SFrançois Tigeot 344477eb7f9SFrançois Tigeot return 0; 345477eb7f9SFrançois Tigeot } 346477eb7f9SFrançois Tigeot 34719c468b4SFrançois Tigeot static void unmap_and_free_pt(struct i915_page_table *pt, 348477eb7f9SFrançois Tigeot struct drm_device *dev) 349477eb7f9SFrançois Tigeot { 350477eb7f9SFrançois Tigeot if (WARN_ON(!pt->page)) 351477eb7f9SFrançois Tigeot return; 352477eb7f9SFrançois Tigeot 353477eb7f9SFrançois Tigeot i915_dma_unmap_single(pt, dev); 354477eb7f9SFrançois Tigeot __free_page(pt->page); 355477eb7f9SFrançois Tigeot kfree(pt->used_ptes); 356477eb7f9SFrançois Tigeot kfree(pt); 357477eb7f9SFrançois Tigeot } 358477eb7f9SFrançois Tigeot 35919c468b4SFrançois Tigeot static void gen8_initialize_pt(struct i915_address_space *vm, 36019c468b4SFrançois Tigeot struct i915_page_table *pt) 361477eb7f9SFrançois Tigeot { 36219c468b4SFrançois Tigeot gen8_pte_t *pt_vaddr, scratch_pte; 36319c468b4SFrançois Tigeot int i; 36419c468b4SFrançois Tigeot 36519c468b4SFrançois Tigeot pt_vaddr = kmap_atomic(pt->page); 36619c468b4SFrançois Tigeot scratch_pte = gen8_pte_encode(vm->scratch.addr, 36719c468b4SFrançois Tigeot I915_CACHE_LLC, true); 36819c468b4SFrançois Tigeot 36919c468b4SFrançois Tigeot for (i = 0; i < GEN8_PTES; i++) 37019c468b4SFrançois Tigeot pt_vaddr[i] = scratch_pte; 37119c468b4SFrançois Tigeot 37219c468b4SFrançois Tigeot if (!HAS_LLC(vm->dev)) 37319c468b4SFrançois Tigeot drm_clflush_virt_range(pt_vaddr, PAGE_SIZE); 37419c468b4SFrançois Tigeot kunmap_atomic(pt_vaddr); 37519c468b4SFrançois Tigeot } 37619c468b4SFrançois Tigeot 37719c468b4SFrançois Tigeot static struct i915_page_table *alloc_pt_single(struct drm_device *dev) 37819c468b4SFrançois Tigeot { 37919c468b4SFrançois Tigeot struct i915_page_table *pt; 380477eb7f9SFrançois Tigeot const size_t count = INTEL_INFO(dev)->gen >= 8 ? 381477eb7f9SFrançois Tigeot GEN8_PTES : GEN6_PTES; 382477eb7f9SFrançois Tigeot int ret = -ENOMEM; 383477eb7f9SFrançois Tigeot 384477eb7f9SFrançois Tigeot pt = kzalloc(sizeof(*pt), GFP_KERNEL); 385477eb7f9SFrançois Tigeot if (!pt) 386477eb7f9SFrançois Tigeot return ERR_PTR(-ENOMEM); 387477eb7f9SFrançois Tigeot 388477eb7f9SFrançois Tigeot pt->used_ptes = kcalloc(BITS_TO_LONGS(count), sizeof(*pt->used_ptes), 389477eb7f9SFrançois Tigeot GFP_KERNEL); 390477eb7f9SFrançois Tigeot 391477eb7f9SFrançois Tigeot if (!pt->used_ptes) 392477eb7f9SFrançois Tigeot goto fail_bitmap; 393477eb7f9SFrançois Tigeot 394477eb7f9SFrançois Tigeot pt->page = alloc_page(GFP_KERNEL); 395477eb7f9SFrançois Tigeot if (!pt->page) 396477eb7f9SFrançois Tigeot goto fail_page; 397477eb7f9SFrançois Tigeot 398477eb7f9SFrançois Tigeot ret = i915_dma_map_single(pt, dev); 399477eb7f9SFrançois Tigeot if (ret) 400477eb7f9SFrançois Tigeot goto fail_dma; 401477eb7f9SFrançois Tigeot 402477eb7f9SFrançois Tigeot return pt; 403477eb7f9SFrançois Tigeot 404477eb7f9SFrançois Tigeot fail_dma: 405477eb7f9SFrançois Tigeot __free_page(pt->page); 406477eb7f9SFrançois Tigeot fail_page: 407477eb7f9SFrançois Tigeot kfree(pt->used_ptes); 408477eb7f9SFrançois Tigeot fail_bitmap: 409477eb7f9SFrançois Tigeot kfree(pt); 410477eb7f9SFrançois Tigeot 411477eb7f9SFrançois Tigeot return ERR_PTR(ret); 412477eb7f9SFrançois Tigeot } 413477eb7f9SFrançois Tigeot 414477eb7f9SFrançois Tigeot /** 415477eb7f9SFrançois Tigeot * alloc_pt_range() - Allocate a multiple page tables 416477eb7f9SFrançois Tigeot * @pd: The page directory which will have at least @count entries 417477eb7f9SFrançois Tigeot * available to point to the allocated page tables. 418477eb7f9SFrançois Tigeot * @pde: First page directory entry for which we are allocating. 419477eb7f9SFrançois Tigeot * @count: Number of pages to allocate. 420477eb7f9SFrançois Tigeot * @dev: DRM device. 421477eb7f9SFrançois Tigeot * 422477eb7f9SFrançois Tigeot * Allocates multiple page table pages and sets the appropriate entries in the 423477eb7f9SFrançois Tigeot * page table structure within the page directory. Function cleans up after 424477eb7f9SFrançois Tigeot * itself on any failures. 425477eb7f9SFrançois Tigeot * 426477eb7f9SFrançois Tigeot * Return: 0 if allocation succeeded. 427477eb7f9SFrançois Tigeot */ 42819c468b4SFrançois Tigeot static int alloc_pt_range(struct i915_page_directory *pd, uint16_t pde, size_t count, 429477eb7f9SFrançois Tigeot struct drm_device *dev) 430477eb7f9SFrançois Tigeot { 431477eb7f9SFrançois Tigeot int i, ret; 432477eb7f9SFrançois Tigeot 433477eb7f9SFrançois Tigeot /* 512 is the max page tables per page_directory on any platform. */ 434477eb7f9SFrançois Tigeot if (WARN_ON(pde + count > I915_PDES)) 435477eb7f9SFrançois Tigeot return -EINVAL; 436477eb7f9SFrançois Tigeot 437477eb7f9SFrançois Tigeot for (i = pde; i < pde + count; i++) { 43819c468b4SFrançois Tigeot struct i915_page_table *pt = alloc_pt_single(dev); 439477eb7f9SFrançois Tigeot 440477eb7f9SFrançois Tigeot if (IS_ERR(pt)) { 441477eb7f9SFrançois Tigeot ret = PTR_ERR(pt); 442477eb7f9SFrançois Tigeot goto err_out; 443477eb7f9SFrançois Tigeot } 444477eb7f9SFrançois Tigeot WARN(pd->page_table[i], 445477eb7f9SFrançois Tigeot "Leaking page directory entry %d (%p)\n", 446477eb7f9SFrançois Tigeot i, pd->page_table[i]); 447477eb7f9SFrançois Tigeot pd->page_table[i] = pt; 448477eb7f9SFrançois Tigeot } 449477eb7f9SFrançois Tigeot 450477eb7f9SFrançois Tigeot return 0; 451477eb7f9SFrançois Tigeot 452477eb7f9SFrançois Tigeot err_out: 453477eb7f9SFrançois Tigeot while (i-- > pde) 454477eb7f9SFrançois Tigeot unmap_and_free_pt(pd->page_table[i], dev); 455477eb7f9SFrançois Tigeot return ret; 456477eb7f9SFrançois Tigeot } 457477eb7f9SFrançois Tigeot 45819c468b4SFrançois Tigeot static void unmap_and_free_pd(struct i915_page_directory *pd, 45919c468b4SFrançois Tigeot struct drm_device *dev) 460477eb7f9SFrançois Tigeot { 461477eb7f9SFrançois Tigeot if (pd->page) { 46219c468b4SFrançois Tigeot i915_dma_unmap_single(pd, dev); 463477eb7f9SFrançois Tigeot __free_page(pd->page); 46419c468b4SFrançois Tigeot kfree(pd->used_pdes); 465477eb7f9SFrançois Tigeot kfree(pd); 466477eb7f9SFrançois Tigeot } 467477eb7f9SFrançois Tigeot } 468477eb7f9SFrançois Tigeot 46919c468b4SFrançois Tigeot static struct i915_page_directory *alloc_pd_single(struct drm_device *dev) 470477eb7f9SFrançois Tigeot { 47119c468b4SFrançois Tigeot struct i915_page_directory *pd; 47219c468b4SFrançois Tigeot int ret = -ENOMEM; 473477eb7f9SFrançois Tigeot 474477eb7f9SFrançois Tigeot pd = kzalloc(sizeof(*pd), GFP_KERNEL); 475477eb7f9SFrançois Tigeot if (!pd) 476477eb7f9SFrançois Tigeot return ERR_PTR(-ENOMEM); 477477eb7f9SFrançois Tigeot 47819c468b4SFrançois Tigeot pd->used_pdes = kcalloc(BITS_TO_LONGS(I915_PDES), 47919c468b4SFrançois Tigeot sizeof(*pd->used_pdes), GFP_KERNEL); 48019c468b4SFrançois Tigeot if (!pd->used_pdes) 48119c468b4SFrançois Tigeot goto free_pd; 48219c468b4SFrançois Tigeot 48319c468b4SFrançois Tigeot pd->page = alloc_page(GFP_KERNEL); 48419c468b4SFrançois Tigeot if (!pd->page) 48519c468b4SFrançois Tigeot goto free_bitmap; 48619c468b4SFrançois Tigeot 48719c468b4SFrançois Tigeot ret = i915_dma_map_single(pd, dev); 48819c468b4SFrançois Tigeot if (ret) 48919c468b4SFrançois Tigeot goto free_page; 490477eb7f9SFrançois Tigeot 491477eb7f9SFrançois Tigeot return pd; 49219c468b4SFrançois Tigeot 49319c468b4SFrançois Tigeot free_page: 49419c468b4SFrançois Tigeot __free_page(pd->page); 49519c468b4SFrançois Tigeot free_bitmap: 49619c468b4SFrançois Tigeot kfree(pd->used_pdes); 49719c468b4SFrançois Tigeot free_pd: 49819c468b4SFrançois Tigeot kfree(pd); 49919c468b4SFrançois Tigeot 50019c468b4SFrançois Tigeot return ERR_PTR(ret); 501477eb7f9SFrançois Tigeot } 502477eb7f9SFrançois Tigeot 5039edbd4a0SFrançois Tigeot /* Broadwell Page Directory Pointer Descriptors */ 50419c468b4SFrançois Tigeot static int gen8_write_pdp(struct intel_engine_cs *ring, 50519c468b4SFrançois Tigeot unsigned entry, 50619c468b4SFrançois Tigeot dma_addr_t addr) 5079edbd4a0SFrançois Tigeot { 5089edbd4a0SFrançois Tigeot int ret; 5099edbd4a0SFrançois Tigeot 5109edbd4a0SFrançois Tigeot BUG_ON(entry >= 4); 5119edbd4a0SFrançois Tigeot 5129edbd4a0SFrançois Tigeot ret = intel_ring_begin(ring, 6); 5139edbd4a0SFrançois Tigeot if (ret) 5149edbd4a0SFrançois Tigeot return ret; 5159edbd4a0SFrançois Tigeot 5169edbd4a0SFrançois Tigeot intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1)); 5179edbd4a0SFrançois Tigeot intel_ring_emit(ring, GEN8_RING_PDP_UDW(ring, entry)); 51819c468b4SFrançois Tigeot intel_ring_emit(ring, upper_32_bits(addr)); 5199edbd4a0SFrançois Tigeot intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1)); 5209edbd4a0SFrançois Tigeot intel_ring_emit(ring, GEN8_RING_PDP_LDW(ring, entry)); 52119c468b4SFrançois Tigeot intel_ring_emit(ring, lower_32_bits(addr)); 5229edbd4a0SFrançois Tigeot intel_ring_advance(ring); 5239edbd4a0SFrançois Tigeot 5249edbd4a0SFrançois Tigeot return 0; 5259edbd4a0SFrançois Tigeot } 5269edbd4a0SFrançois Tigeot 527ba55f2f5SFrançois Tigeot static int gen8_mm_switch(struct i915_hw_ppgtt *ppgtt, 5281b13d190SFrançois Tigeot struct intel_engine_cs *ring) 5299edbd4a0SFrançois Tigeot { 530ba55f2f5SFrançois Tigeot int i, ret; 5319edbd4a0SFrançois Tigeot 53219c468b4SFrançois Tigeot for (i = GEN8_LEGACY_PDPES - 1; i >= 0; i--) { 53319c468b4SFrançois Tigeot struct i915_page_directory *pd = ppgtt->pdp.page_directory[i]; 53419c468b4SFrançois Tigeot dma_addr_t pd_daddr = pd ? pd->daddr : ppgtt->scratch_pd->daddr; 53519c468b4SFrançois Tigeot /* The page directory might be NULL, but we need to clear out 53619c468b4SFrançois Tigeot * whatever the previous context might have used. */ 53719c468b4SFrançois Tigeot ret = gen8_write_pdp(ring, i, pd_daddr); 5389edbd4a0SFrançois Tigeot if (ret) 5399edbd4a0SFrançois Tigeot return ret; 5409edbd4a0SFrançois Tigeot } 5419edbd4a0SFrançois Tigeot 542ba55f2f5SFrançois Tigeot return 0; 543ba55f2f5SFrançois Tigeot } 544ba55f2f5SFrançois Tigeot 5459edbd4a0SFrançois Tigeot static void gen8_ppgtt_clear_range(struct i915_address_space *vm, 546ba55f2f5SFrançois Tigeot uint64_t start, 547ba55f2f5SFrançois Tigeot uint64_t length, 5489edbd4a0SFrançois Tigeot bool use_scratch) 5499edbd4a0SFrançois Tigeot { 5509edbd4a0SFrançois Tigeot struct i915_hw_ppgtt *ppgtt = 5519edbd4a0SFrançois Tigeot container_of(vm, struct i915_hw_ppgtt, base); 552477eb7f9SFrançois Tigeot gen8_pte_t *pt_vaddr, scratch_pte; 553ba55f2f5SFrançois Tigeot unsigned pdpe = start >> GEN8_PDPE_SHIFT & GEN8_PDPE_MASK; 554ba55f2f5SFrançois Tigeot unsigned pde = start >> GEN8_PDE_SHIFT & GEN8_PDE_MASK; 555ba55f2f5SFrançois Tigeot unsigned pte = start >> GEN8_PTE_SHIFT & GEN8_PTE_MASK; 556ba55f2f5SFrançois Tigeot unsigned num_entries = length >> PAGE_SHIFT; 5579edbd4a0SFrançois Tigeot unsigned last_pte, i; 5589edbd4a0SFrançois Tigeot 5599edbd4a0SFrançois Tigeot scratch_pte = gen8_pte_encode(ppgtt->base.scratch.addr, 5609edbd4a0SFrançois Tigeot I915_CACHE_LLC, use_scratch); 5619edbd4a0SFrançois Tigeot 5629edbd4a0SFrançois Tigeot while (num_entries) { 56319c468b4SFrançois Tigeot struct i915_page_directory *pd; 56419c468b4SFrançois Tigeot struct i915_page_table *pt; 565477eb7f9SFrançois Tigeot struct vm_page *page_table; 566477eb7f9SFrançois Tigeot 567477eb7f9SFrançois Tigeot if (WARN_ON(!ppgtt->pdp.page_directory[pdpe])) 56819c468b4SFrançois Tigeot break; 569477eb7f9SFrançois Tigeot 570477eb7f9SFrançois Tigeot pd = ppgtt->pdp.page_directory[pdpe]; 571477eb7f9SFrançois Tigeot 572477eb7f9SFrançois Tigeot if (WARN_ON(!pd->page_table[pde])) 57319c468b4SFrançois Tigeot break; 574477eb7f9SFrançois Tigeot 575477eb7f9SFrançois Tigeot pt = pd->page_table[pde]; 576477eb7f9SFrançois Tigeot 577477eb7f9SFrançois Tigeot if (WARN_ON(!pt->page)) 57819c468b4SFrançois Tigeot break; 579477eb7f9SFrançois Tigeot 580477eb7f9SFrançois Tigeot page_table = pt->page; 5819edbd4a0SFrançois Tigeot 582ba55f2f5SFrançois Tigeot last_pte = pte + num_entries; 583477eb7f9SFrançois Tigeot if (last_pte > GEN8_PTES) 584477eb7f9SFrançois Tigeot last_pte = GEN8_PTES; 5859edbd4a0SFrançois Tigeot 5869edbd4a0SFrançois Tigeot pt_vaddr = kmap_atomic(page_table); 5879edbd4a0SFrançois Tigeot 588ba55f2f5SFrançois Tigeot for (i = pte; i < last_pte; i++) { 5899edbd4a0SFrançois Tigeot pt_vaddr[i] = scratch_pte; 590ba55f2f5SFrançois Tigeot num_entries--; 591ba55f2f5SFrançois Tigeot } 5929edbd4a0SFrançois Tigeot 593ba55f2f5SFrançois Tigeot if (!HAS_LLC(ppgtt->base.dev)) 594ba55f2f5SFrançois Tigeot drm_clflush_virt_range(pt_vaddr, PAGE_SIZE); 5959edbd4a0SFrançois Tigeot kunmap_atomic(pt_vaddr); 5969edbd4a0SFrançois Tigeot 597ba55f2f5SFrançois Tigeot pte = 0; 598477eb7f9SFrançois Tigeot if (++pde == I915_PDES) { 599ba55f2f5SFrançois Tigeot pdpe++; 600ba55f2f5SFrançois Tigeot pde = 0; 601ba55f2f5SFrançois Tigeot } 6029edbd4a0SFrançois Tigeot } 6039edbd4a0SFrançois Tigeot } 6049edbd4a0SFrançois Tigeot 6059edbd4a0SFrançois Tigeot static void gen8_ppgtt_insert_entries(struct i915_address_space *vm, 606*7ec9f8e5SFrançois Tigeot struct sg_table *pages, 607ba55f2f5SFrançois Tigeot uint64_t start, 60824edb884SFrançois Tigeot enum i915_cache_level cache_level, u32 unused) 6099edbd4a0SFrançois Tigeot { 6109edbd4a0SFrançois Tigeot struct i915_hw_ppgtt *ppgtt = 6119edbd4a0SFrançois Tigeot container_of(vm, struct i915_hw_ppgtt, base); 612477eb7f9SFrançois Tigeot gen8_pte_t *pt_vaddr; 613ba55f2f5SFrançois Tigeot unsigned pdpe = start >> GEN8_PDPE_SHIFT & GEN8_PDPE_MASK; 614ba55f2f5SFrançois Tigeot unsigned pde = start >> GEN8_PDE_SHIFT & GEN8_PDE_MASK; 615ba55f2f5SFrançois Tigeot unsigned pte = start >> GEN8_PTE_SHIFT & GEN8_PTE_MASK; 616*7ec9f8e5SFrançois Tigeot struct sg_page_iter sg_iter; 6179edbd4a0SFrançois Tigeot 6189edbd4a0SFrançois Tigeot pt_vaddr = NULL; 6199edbd4a0SFrançois Tigeot 620*7ec9f8e5SFrançois Tigeot for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) { 621477eb7f9SFrançois Tigeot if (WARN_ON(pdpe >= GEN8_LEGACY_PDPES)) 622ba55f2f5SFrançois Tigeot break; 623ba55f2f5SFrançois Tigeot 624477eb7f9SFrançois Tigeot if (pt_vaddr == NULL) { 62519c468b4SFrançois Tigeot struct i915_page_directory *pd = ppgtt->pdp.page_directory[pdpe]; 62619c468b4SFrançois Tigeot struct i915_page_table *pt = pd->page_table[pde]; 627477eb7f9SFrançois Tigeot struct vm_page *page_table = pt->page; 628477eb7f9SFrançois Tigeot 629477eb7f9SFrançois Tigeot pt_vaddr = kmap_atomic(page_table); 630477eb7f9SFrançois Tigeot } 631ba55f2f5SFrançois Tigeot 632ba55f2f5SFrançois Tigeot pt_vaddr[pte] = 633*7ec9f8e5SFrançois Tigeot gen8_pte_encode(sg_page_iter_dma_address(&sg_iter), 6349edbd4a0SFrançois Tigeot cache_level, true); 635477eb7f9SFrançois Tigeot if (++pte == GEN8_PTES) { 636ba55f2f5SFrançois Tigeot if (!HAS_LLC(ppgtt->base.dev)) 637ba55f2f5SFrançois Tigeot drm_clflush_virt_range(pt_vaddr, PAGE_SIZE); 6389edbd4a0SFrançois Tigeot kunmap_atomic(pt_vaddr); 6399edbd4a0SFrançois Tigeot pt_vaddr = NULL; 640477eb7f9SFrançois Tigeot if (++pde == I915_PDES) { 641ba55f2f5SFrançois Tigeot pdpe++; 642ba55f2f5SFrançois Tigeot pde = 0; 643ba55f2f5SFrançois Tigeot } 644ba55f2f5SFrançois Tigeot pte = 0; 6459edbd4a0SFrançois Tigeot } 6469edbd4a0SFrançois Tigeot } 647ba55f2f5SFrançois Tigeot if (pt_vaddr) { 648ba55f2f5SFrançois Tigeot if (!HAS_LLC(ppgtt->base.dev)) 649ba55f2f5SFrançois Tigeot drm_clflush_virt_range(pt_vaddr, PAGE_SIZE); 6509edbd4a0SFrançois Tigeot kunmap_atomic(pt_vaddr); 6519edbd4a0SFrançois Tigeot } 652ba55f2f5SFrançois Tigeot } 653ba55f2f5SFrançois Tigeot 65419c468b4SFrançois Tigeot static void __gen8_do_map_pt(gen8_pde_t * const pde, 65519c468b4SFrançois Tigeot struct i915_page_table *pt, 65619c468b4SFrançois Tigeot struct drm_device *dev) 65719c468b4SFrançois Tigeot { 65819c468b4SFrançois Tigeot gen8_pde_t entry = 65919c468b4SFrançois Tigeot gen8_pde_encode(dev, pt->daddr, I915_CACHE_LLC); 66019c468b4SFrançois Tigeot *pde = entry; 66119c468b4SFrançois Tigeot } 66219c468b4SFrançois Tigeot 66319c468b4SFrançois Tigeot static void gen8_initialize_pd(struct i915_address_space *vm, 66419c468b4SFrançois Tigeot struct i915_page_directory *pd) 66519c468b4SFrançois Tigeot { 66619c468b4SFrançois Tigeot struct i915_hw_ppgtt *ppgtt = 66719c468b4SFrançois Tigeot container_of(vm, struct i915_hw_ppgtt, base); 66819c468b4SFrançois Tigeot gen8_pde_t *page_directory; 66919c468b4SFrançois Tigeot struct i915_page_table *pt; 67019c468b4SFrançois Tigeot int i; 67119c468b4SFrançois Tigeot 67219c468b4SFrançois Tigeot page_directory = kmap_atomic(pd->page); 67319c468b4SFrançois Tigeot pt = ppgtt->scratch_pt; 67419c468b4SFrançois Tigeot for (i = 0; i < I915_PDES; i++) 67519c468b4SFrançois Tigeot /* Map the PDE to the page table */ 67619c468b4SFrançois Tigeot __gen8_do_map_pt(page_directory + i, pt, vm->dev); 67719c468b4SFrançois Tigeot 67819c468b4SFrançois Tigeot if (!HAS_LLC(vm->dev)) 67919c468b4SFrançois Tigeot drm_clflush_virt_range(page_directory, PAGE_SIZE); 68019c468b4SFrançois Tigeot kunmap_atomic(page_directory); 68119c468b4SFrançois Tigeot } 68219c468b4SFrançois Tigeot 68319c468b4SFrançois Tigeot static void gen8_free_page_tables(struct i915_page_directory *pd, struct drm_device *dev) 684ba55f2f5SFrançois Tigeot { 685ba55f2f5SFrançois Tigeot int i; 686ba55f2f5SFrançois Tigeot 687477eb7f9SFrançois Tigeot if (!pd->page) 688ba55f2f5SFrançois Tigeot return; 689ba55f2f5SFrançois Tigeot 69019c468b4SFrançois Tigeot for_each_set_bit(i, pd->used_pdes, I915_PDES) { 691477eb7f9SFrançois Tigeot if (WARN_ON(!pd->page_table[i])) 692477eb7f9SFrançois Tigeot continue; 693477eb7f9SFrançois Tigeot 694477eb7f9SFrançois Tigeot unmap_and_free_pt(pd->page_table[i], dev); 695477eb7f9SFrançois Tigeot pd->page_table[i] = NULL; 696477eb7f9SFrançois Tigeot } 697ba55f2f5SFrançois Tigeot } 698ba55f2f5SFrançois Tigeot 6999edbd4a0SFrançois Tigeot static void gen8_ppgtt_cleanup(struct i915_address_space *vm) 7009edbd4a0SFrançois Tigeot { 7019edbd4a0SFrançois Tigeot struct i915_hw_ppgtt *ppgtt = 7029edbd4a0SFrançois Tigeot container_of(vm, struct i915_hw_ppgtt, base); 70319c468b4SFrançois Tigeot int i; 7049edbd4a0SFrançois Tigeot 70519c468b4SFrançois Tigeot for_each_set_bit(i, ppgtt->pdp.used_pdpes, GEN8_LEGACY_PDPES) { 70619c468b4SFrançois Tigeot if (WARN_ON(!ppgtt->pdp.page_directory[i])) 70719c468b4SFrançois Tigeot continue; 70819c468b4SFrançois Tigeot 70919c468b4SFrançois Tigeot gen8_free_page_tables(ppgtt->pdp.page_directory[i], ppgtt->base.dev); 71019c468b4SFrançois Tigeot unmap_and_free_pd(ppgtt->pdp.page_directory[i], ppgtt->base.dev); 7119edbd4a0SFrançois Tigeot } 7129edbd4a0SFrançois Tigeot 71319c468b4SFrançois Tigeot unmap_and_free_pd(ppgtt->scratch_pd, ppgtt->base.dev); 71419c468b4SFrançois Tigeot unmap_and_free_pt(ppgtt->scratch_pt, ppgtt->base.dev); 71519c468b4SFrançois Tigeot } 716ba55f2f5SFrançois Tigeot 71719c468b4SFrançois Tigeot /** 71819c468b4SFrançois Tigeot * gen8_ppgtt_alloc_pagetabs() - Allocate page tables for VA range. 71919c468b4SFrançois Tigeot * @ppgtt: Master ppgtt structure. 72019c468b4SFrançois Tigeot * @pd: Page directory for this address range. 72119c468b4SFrançois Tigeot * @start: Starting virtual address to begin allocations. 72219c468b4SFrançois Tigeot * @length Size of the allocations. 72319c468b4SFrançois Tigeot * @new_pts: Bitmap set by function with new allocations. Likely used by the 72419c468b4SFrançois Tigeot * caller to free on error. 72519c468b4SFrançois Tigeot * 72619c468b4SFrançois Tigeot * Allocate the required number of page tables. Extremely similar to 72719c468b4SFrançois Tigeot * gen8_ppgtt_alloc_page_directories(). The main difference is here we are limited by 72819c468b4SFrançois Tigeot * the page directory boundary (instead of the page directory pointer). That 72919c468b4SFrançois Tigeot * boundary is 1GB virtual. Therefore, unlike gen8_ppgtt_alloc_page_directories(), it is 73019c468b4SFrançois Tigeot * possible, and likely that the caller will need to use multiple calls of this 73119c468b4SFrançois Tigeot * function to achieve the appropriate allocation. 73219c468b4SFrançois Tigeot * 73319c468b4SFrançois Tigeot * Return: 0 if success; negative error code otherwise. 73419c468b4SFrançois Tigeot */ 73519c468b4SFrançois Tigeot static int gen8_ppgtt_alloc_pagetabs(struct i915_hw_ppgtt *ppgtt, 73619c468b4SFrançois Tigeot struct i915_page_directory *pd, 73719c468b4SFrançois Tigeot uint64_t start, 73819c468b4SFrançois Tigeot uint64_t length, 73919c468b4SFrançois Tigeot unsigned long *new_pts) 74019c468b4SFrançois Tigeot { 74119c468b4SFrançois Tigeot struct drm_device *dev = ppgtt->base.dev; 74219c468b4SFrançois Tigeot struct i915_page_table *pt; 74319c468b4SFrançois Tigeot uint64_t temp; 74419c468b4SFrançois Tigeot uint32_t pde; 74519c468b4SFrançois Tigeot 74619c468b4SFrançois Tigeot gen8_for_each_pde(pt, pd, start, length, temp, pde) { 74719c468b4SFrançois Tigeot /* Don't reallocate page tables */ 74819c468b4SFrançois Tigeot if (pt) { 74919c468b4SFrançois Tigeot /* Scratch is never allocated this way */ 75019c468b4SFrançois Tigeot WARN_ON(pt == ppgtt->scratch_pt); 75119c468b4SFrançois Tigeot continue; 75219c468b4SFrançois Tigeot } 75319c468b4SFrançois Tigeot 75419c468b4SFrançois Tigeot pt = alloc_pt_single(dev); 75519c468b4SFrançois Tigeot if (IS_ERR(pt)) 756ba55f2f5SFrançois Tigeot goto unwind_out; 75719c468b4SFrançois Tigeot 75819c468b4SFrançois Tigeot gen8_initialize_pt(&ppgtt->base, pt); 75919c468b4SFrançois Tigeot pd->page_table[pde] = pt; 76019c468b4SFrançois Tigeot set_bit(pde, new_pts); 761ba55f2f5SFrançois Tigeot } 762ba55f2f5SFrançois Tigeot 763ba55f2f5SFrançois Tigeot return 0; 764ba55f2f5SFrançois Tigeot 765ba55f2f5SFrançois Tigeot unwind_out: 76619c468b4SFrançois Tigeot for_each_set_bit(pde, new_pts, I915_PDES) 76719c468b4SFrançois Tigeot unmap_and_free_pt(pd->page_table[pde], dev); 768ba55f2f5SFrançois Tigeot 769ba55f2f5SFrançois Tigeot return -ENOMEM; 770ba55f2f5SFrançois Tigeot } 771ba55f2f5SFrançois Tigeot 77219c468b4SFrançois Tigeot /** 77319c468b4SFrançois Tigeot * gen8_ppgtt_alloc_page_directories() - Allocate page directories for VA range. 77419c468b4SFrançois Tigeot * @ppgtt: Master ppgtt structure. 77519c468b4SFrançois Tigeot * @pdp: Page directory pointer for this address range. 77619c468b4SFrançois Tigeot * @start: Starting virtual address to begin allocations. 77719c468b4SFrançois Tigeot * @length Size of the allocations. 77819c468b4SFrançois Tigeot * @new_pds Bitmap set by function with new allocations. Likely used by the 77919c468b4SFrançois Tigeot * caller to free on error. 78019c468b4SFrançois Tigeot * 78119c468b4SFrançois Tigeot * Allocate the required number of page directories starting at the pde index of 78219c468b4SFrançois Tigeot * @start, and ending at the pde index @start + @length. This function will skip 78319c468b4SFrançois Tigeot * over already allocated page directories within the range, and only allocate 78419c468b4SFrançois Tigeot * new ones, setting the appropriate pointer within the pdp as well as the 78519c468b4SFrançois Tigeot * correct position in the bitmap @new_pds. 78619c468b4SFrançois Tigeot * 78719c468b4SFrançois Tigeot * The function will only allocate the pages within the range for a give page 78819c468b4SFrançois Tigeot * directory pointer. In other words, if @start + @length straddles a virtually 78919c468b4SFrançois Tigeot * addressed PDP boundary (512GB for 4k pages), there will be more allocations 79019c468b4SFrançois Tigeot * required by the caller, This is not currently possible, and the BUG in the 79119c468b4SFrançois Tigeot * code will prevent it. 79219c468b4SFrançois Tigeot * 79319c468b4SFrançois Tigeot * Return: 0 if success; negative error code otherwise. 79419c468b4SFrançois Tigeot */ 79519c468b4SFrançois Tigeot static int gen8_ppgtt_alloc_page_directories(struct i915_hw_ppgtt *ppgtt, 79619c468b4SFrançois Tigeot struct i915_page_directory_pointer *pdp, 79719c468b4SFrançois Tigeot uint64_t start, 79819c468b4SFrançois Tigeot uint64_t length, 79919c468b4SFrançois Tigeot unsigned long *new_pds) 80019c468b4SFrançois Tigeot { 80119c468b4SFrançois Tigeot struct drm_device *dev = ppgtt->base.dev; 80219c468b4SFrançois Tigeot struct i915_page_directory *pd; 80319c468b4SFrançois Tigeot uint64_t temp; 80419c468b4SFrançois Tigeot uint32_t pdpe; 80519c468b4SFrançois Tigeot 80619c468b4SFrançois Tigeot WARN_ON(!bitmap_empty(new_pds, GEN8_LEGACY_PDPES)); 80719c468b4SFrançois Tigeot 80819c468b4SFrançois Tigeot /* FIXME: upper bound must not overflow 32 bits */ 80919c468b4SFrançois Tigeot WARN_ON((start + length) > (1ULL << 32)); 81019c468b4SFrançois Tigeot 81119c468b4SFrançois Tigeot gen8_for_each_pdpe(pd, pdp, start, length, temp, pdpe) { 81219c468b4SFrançois Tigeot if (pd) 81319c468b4SFrançois Tigeot continue; 81419c468b4SFrançois Tigeot 81519c468b4SFrançois Tigeot pd = alloc_pd_single(dev); 81619c468b4SFrançois Tigeot if (IS_ERR(pd)) 81719c468b4SFrançois Tigeot goto unwind_out; 81819c468b4SFrançois Tigeot 81919c468b4SFrançois Tigeot gen8_initialize_pd(&ppgtt->base, pd); 82019c468b4SFrançois Tigeot pdp->page_directory[pdpe] = pd; 82119c468b4SFrançois Tigeot set_bit(pdpe, new_pds); 82219c468b4SFrançois Tigeot } 82319c468b4SFrançois Tigeot 82419c468b4SFrançois Tigeot return 0; 82519c468b4SFrançois Tigeot 82619c468b4SFrançois Tigeot unwind_out: 82719c468b4SFrançois Tigeot for_each_set_bit(pdpe, new_pds, GEN8_LEGACY_PDPES) 82819c468b4SFrançois Tigeot unmap_and_free_pd(pdp->page_directory[pdpe], dev); 82919c468b4SFrançois Tigeot 83019c468b4SFrançois Tigeot return -ENOMEM; 83119c468b4SFrançois Tigeot } 83219c468b4SFrançois Tigeot 83319c468b4SFrançois Tigeot static void 83419c468b4SFrançois Tigeot free_gen8_temp_bitmaps(unsigned long *new_pds, unsigned long **new_pts) 835ba55f2f5SFrançois Tigeot { 836477eb7f9SFrançois Tigeot int i; 8379edbd4a0SFrançois Tigeot 83819c468b4SFrançois Tigeot for (i = 0; i < GEN8_LEGACY_PDPES; i++) 83919c468b4SFrançois Tigeot kfree(new_pts[i]); 84019c468b4SFrançois Tigeot kfree(new_pts); 84119c468b4SFrançois Tigeot kfree(new_pds); 842477eb7f9SFrançois Tigeot } 843477eb7f9SFrançois Tigeot 84419c468b4SFrançois Tigeot /* Fills in the page directory bitmap, and the array of page tables bitmap. Both 84519c468b4SFrançois Tigeot * of these are based on the number of PDPEs in the system. 84619c468b4SFrançois Tigeot */ 84719c468b4SFrançois Tigeot static 84819c468b4SFrançois Tigeot int __must_check alloc_gen8_temp_bitmaps(unsigned long **new_pds, 84919c468b4SFrançois Tigeot unsigned long ***new_pts) 85019c468b4SFrançois Tigeot { 85119c468b4SFrançois Tigeot int i; 85219c468b4SFrançois Tigeot unsigned long *pds; 85319c468b4SFrançois Tigeot unsigned long **pts; 8549edbd4a0SFrançois Tigeot 85519c468b4SFrançois Tigeot pds = kcalloc(BITS_TO_LONGS(GEN8_LEGACY_PDPES), sizeof(unsigned long), GFP_KERNEL); 85619c468b4SFrançois Tigeot if (!pds) 85719c468b4SFrançois Tigeot return -ENOMEM; 858477eb7f9SFrançois Tigeot 85919c468b4SFrançois Tigeot pts = kcalloc(GEN8_LEGACY_PDPES, sizeof(unsigned long *), GFP_KERNEL); 86019c468b4SFrançois Tigeot if (!pts) { 86119c468b4SFrançois Tigeot kfree(pds); 862477eb7f9SFrançois Tigeot return -ENOMEM; 863ba55f2f5SFrançois Tigeot } 864ba55f2f5SFrançois Tigeot 86519c468b4SFrançois Tigeot for (i = 0; i < GEN8_LEGACY_PDPES; i++) { 86619c468b4SFrançois Tigeot pts[i] = kcalloc(BITS_TO_LONGS(I915_PDES), 86719c468b4SFrançois Tigeot sizeof(unsigned long), GFP_KERNEL); 86819c468b4SFrançois Tigeot if (!pts[i]) 869477eb7f9SFrançois Tigeot goto err_out; 87019c468b4SFrançois Tigeot } 871ba55f2f5SFrançois Tigeot 87219c468b4SFrançois Tigeot *new_pds = pds; 87319c468b4SFrançois Tigeot *new_pts = pts; 874477eb7f9SFrançois Tigeot 875477eb7f9SFrançois Tigeot return 0; 876477eb7f9SFrançois Tigeot 877477eb7f9SFrançois Tigeot err_out: 87819c468b4SFrançois Tigeot free_gen8_temp_bitmaps(pds, pts); 87919c468b4SFrançois Tigeot return -ENOMEM; 880ba55f2f5SFrançois Tigeot } 881ba55f2f5SFrançois Tigeot 88219c468b4SFrançois Tigeot static int gen8_alloc_va_range(struct i915_address_space *vm, 88319c468b4SFrançois Tigeot uint64_t start, 88419c468b4SFrançois Tigeot uint64_t length) 885ba55f2f5SFrançois Tigeot { 88619c468b4SFrançois Tigeot struct i915_hw_ppgtt *ppgtt = 88719c468b4SFrançois Tigeot container_of(vm, struct i915_hw_ppgtt, base); 88819c468b4SFrançois Tigeot unsigned long *new_page_dirs, **new_page_tables; 88919c468b4SFrançois Tigeot struct i915_page_directory *pd; 89019c468b4SFrançois Tigeot const uint64_t orig_start = start; 89119c468b4SFrançois Tigeot const uint64_t orig_length = length; 89219c468b4SFrançois Tigeot uint64_t temp; 89319c468b4SFrançois Tigeot uint32_t pdpe; 894ba55f2f5SFrançois Tigeot int ret; 895ba55f2f5SFrançois Tigeot 89619c468b4SFrançois Tigeot /* Wrap is never okay since we can only represent 48b, and we don't 89719c468b4SFrançois Tigeot * actually use the other side of the canonical address space. 89819c468b4SFrançois Tigeot */ 89919c468b4SFrançois Tigeot if (WARN_ON(start + length < start)) 90019c468b4SFrançois Tigeot return -ERANGE; 901ba55f2f5SFrançois Tigeot 90219c468b4SFrançois Tigeot ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables); 903ba55f2f5SFrançois Tigeot if (ret) 904ba55f2f5SFrançois Tigeot return ret; 905ba55f2f5SFrançois Tigeot 90619c468b4SFrançois Tigeot /* Do the allocations first so we can easily bail out */ 90719c468b4SFrançois Tigeot ret = gen8_ppgtt_alloc_page_directories(ppgtt, &ppgtt->pdp, start, length, 90819c468b4SFrançois Tigeot new_page_dirs); 90919c468b4SFrançois Tigeot if (ret) { 91019c468b4SFrançois Tigeot free_gen8_temp_bitmaps(new_page_dirs, new_page_tables); 91119c468b4SFrançois Tigeot return ret; 91219c468b4SFrançois Tigeot } 91319c468b4SFrançois Tigeot 91419c468b4SFrançois Tigeot /* For every page directory referenced, allocate page tables */ 91519c468b4SFrançois Tigeot gen8_for_each_pdpe(pd, &ppgtt->pdp, start, length, temp, pdpe) { 91619c468b4SFrançois Tigeot ret = gen8_ppgtt_alloc_pagetabs(ppgtt, pd, start, length, 91719c468b4SFrançois Tigeot new_page_tables[pdpe]); 91819c468b4SFrançois Tigeot if (ret) 91919c468b4SFrançois Tigeot goto err_out; 92019c468b4SFrançois Tigeot } 92119c468b4SFrançois Tigeot 92219c468b4SFrançois Tigeot start = orig_start; 92319c468b4SFrançois Tigeot length = orig_length; 92419c468b4SFrançois Tigeot 92519c468b4SFrançois Tigeot /* Allocations have completed successfully, so set the bitmaps, and do 92619c468b4SFrançois Tigeot * the mappings. */ 92719c468b4SFrançois Tigeot gen8_for_each_pdpe(pd, &ppgtt->pdp, start, length, temp, pdpe) { 92819c468b4SFrançois Tigeot gen8_pde_t *const page_directory = kmap_atomic(pd->page); 92919c468b4SFrançois Tigeot struct i915_page_table *pt; 93019c468b4SFrançois Tigeot uint64_t pd_len = gen8_clamp_pd(start, length); 93119c468b4SFrançois Tigeot uint64_t pd_start = start; 93219c468b4SFrançois Tigeot uint32_t pde; 93319c468b4SFrançois Tigeot 93419c468b4SFrançois Tigeot /* Every pd should be allocated, we just did that above. */ 93519c468b4SFrançois Tigeot WARN_ON(!pd); 93619c468b4SFrançois Tigeot 93719c468b4SFrançois Tigeot gen8_for_each_pde(pt, pd, pd_start, pd_len, temp, pde) { 93819c468b4SFrançois Tigeot /* Same reasoning as pd */ 93919c468b4SFrançois Tigeot WARN_ON(!pt); 94019c468b4SFrançois Tigeot WARN_ON(!pd_len); 94119c468b4SFrançois Tigeot WARN_ON(!gen8_pte_count(pd_start, pd_len)); 94219c468b4SFrançois Tigeot 94319c468b4SFrançois Tigeot /* Set our used ptes within the page table */ 94419c468b4SFrançois Tigeot bitmap_set(pt->used_ptes, 94519c468b4SFrançois Tigeot gen8_pte_index(pd_start), 94619c468b4SFrançois Tigeot gen8_pte_count(pd_start, pd_len)); 94719c468b4SFrançois Tigeot 94819c468b4SFrançois Tigeot /* Our pde is now pointing to the pagetable, pt */ 94919c468b4SFrançois Tigeot set_bit(pde, pd->used_pdes); 95019c468b4SFrançois Tigeot 95119c468b4SFrançois Tigeot /* Map the PDE to the page table */ 95219c468b4SFrançois Tigeot __gen8_do_map_pt(page_directory + pde, pt, vm->dev); 95319c468b4SFrançois Tigeot 95419c468b4SFrançois Tigeot /* NB: We haven't yet mapped ptes to pages. At this 95519c468b4SFrançois Tigeot * point we're still relying on insert_entries() */ 95619c468b4SFrançois Tigeot } 95719c468b4SFrançois Tigeot 95819c468b4SFrançois Tigeot if (!HAS_LLC(vm->dev)) 95919c468b4SFrançois Tigeot drm_clflush_virt_range(page_directory, PAGE_SIZE); 96019c468b4SFrançois Tigeot 96119c468b4SFrançois Tigeot kunmap_atomic(page_directory); 96219c468b4SFrançois Tigeot 96319c468b4SFrançois Tigeot set_bit(pdpe, ppgtt->pdp.used_pdpes); 96419c468b4SFrançois Tigeot } 96519c468b4SFrançois Tigeot 96619c468b4SFrançois Tigeot free_gen8_temp_bitmaps(new_page_dirs, new_page_tables); 96719c468b4SFrançois Tigeot return 0; 96819c468b4SFrançois Tigeot 96919c468b4SFrançois Tigeot err_out: 97019c468b4SFrançois Tigeot while (pdpe--) { 97119c468b4SFrançois Tigeot for_each_set_bit(temp, new_page_tables[pdpe], I915_PDES) 97219c468b4SFrançois Tigeot unmap_and_free_pt(ppgtt->pdp.page_directory[pdpe]->page_table[temp], vm->dev); 97319c468b4SFrançois Tigeot } 97419c468b4SFrançois Tigeot 97519c468b4SFrançois Tigeot for_each_set_bit(pdpe, new_page_dirs, GEN8_LEGACY_PDPES) 97619c468b4SFrançois Tigeot unmap_and_free_pd(ppgtt->pdp.page_directory[pdpe], vm->dev); 97719c468b4SFrançois Tigeot 97819c468b4SFrançois Tigeot free_gen8_temp_bitmaps(new_page_dirs, new_page_tables); 97919c468b4SFrançois Tigeot return ret; 98019c468b4SFrançois Tigeot } 98119c468b4SFrançois Tigeot 98219c468b4SFrançois Tigeot /* 98319c468b4SFrançois Tigeot * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers 98419c468b4SFrançois Tigeot * with a net effect resembling a 2-level page table in normal x86 terms. Each 98519c468b4SFrançois Tigeot * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address 98619c468b4SFrançois Tigeot * space. 98719c468b4SFrançois Tigeot * 98819c468b4SFrançois Tigeot */ 98919c468b4SFrançois Tigeot static int gen8_ppgtt_init_common(struct i915_hw_ppgtt *ppgtt, uint64_t size) 99019c468b4SFrançois Tigeot { 99119c468b4SFrançois Tigeot ppgtt->scratch_pt = alloc_pt_single(ppgtt->base.dev); 99219c468b4SFrançois Tigeot if (IS_ERR(ppgtt->scratch_pt)) 99319c468b4SFrançois Tigeot return PTR_ERR(ppgtt->scratch_pt); 99419c468b4SFrançois Tigeot 99519c468b4SFrançois Tigeot ppgtt->scratch_pd = alloc_pd_single(ppgtt->base.dev); 99619c468b4SFrançois Tigeot if (IS_ERR(ppgtt->scratch_pd)) 99719c468b4SFrançois Tigeot return PTR_ERR(ppgtt->scratch_pd); 99819c468b4SFrançois Tigeot 99919c468b4SFrançois Tigeot gen8_initialize_pt(&ppgtt->base, ppgtt->scratch_pt); 100019c468b4SFrançois Tigeot gen8_initialize_pd(&ppgtt->base, ppgtt->scratch_pd); 100119c468b4SFrançois Tigeot 100219c468b4SFrançois Tigeot ppgtt->base.start = 0; 100319c468b4SFrançois Tigeot ppgtt->base.total = size; 100419c468b4SFrançois Tigeot ppgtt->base.cleanup = gen8_ppgtt_cleanup; 100519c468b4SFrançois Tigeot ppgtt->base.insert_entries = gen8_ppgtt_insert_entries; 100619c468b4SFrançois Tigeot ppgtt->base.clear_range = gen8_ppgtt_clear_range; 100719c468b4SFrançois Tigeot ppgtt->base.unbind_vma = ppgtt_unbind_vma; 100819c468b4SFrançois Tigeot ppgtt->base.bind_vma = ppgtt_bind_vma; 100919c468b4SFrançois Tigeot 101019c468b4SFrançois Tigeot ppgtt->switch_mm = gen8_mm_switch; 1011ba55f2f5SFrançois Tigeot 1012ba55f2f5SFrançois Tigeot return 0; 1013ba55f2f5SFrançois Tigeot } 1014ba55f2f5SFrançois Tigeot 101519c468b4SFrançois Tigeot static int gen8_aliasing_ppgtt_init(struct i915_hw_ppgtt *ppgtt) 1016ba55f2f5SFrançois Tigeot { 101719c468b4SFrançois Tigeot struct drm_device *dev = ppgtt->base.dev; 101819c468b4SFrançois Tigeot struct drm_i915_private *dev_priv = dev->dev_private; 101919c468b4SFrançois Tigeot uint64_t start = 0, size = dev_priv->gtt.base.total; 1020ba55f2f5SFrançois Tigeot int ret; 1021ba55f2f5SFrançois Tigeot 102219c468b4SFrançois Tigeot ret = gen8_ppgtt_init_common(ppgtt, dev_priv->gtt.base.total); 1023ba55f2f5SFrançois Tigeot if (ret) 1024ba55f2f5SFrançois Tigeot return ret; 1025ba55f2f5SFrançois Tigeot 102619c468b4SFrançois Tigeot /* Aliasing PPGTT has to always work and be mapped because of the way we 102719c468b4SFrançois Tigeot * use RESTORE_INHIBIT in the context switch. This will be fixed 102819c468b4SFrançois Tigeot * eventually. */ 102919c468b4SFrançois Tigeot ret = gen8_alloc_va_range(&ppgtt->base, start, size); 103019c468b4SFrançois Tigeot if (ret) { 103119c468b4SFrançois Tigeot unmap_and_free_pd(ppgtt->scratch_pd, ppgtt->base.dev); 103219c468b4SFrançois Tigeot unmap_and_free_pt(ppgtt->scratch_pt, ppgtt->base.dev); 103319c468b4SFrançois Tigeot return ret; 103419c468b4SFrançois Tigeot } 103519c468b4SFrançois Tigeot 103619c468b4SFrançois Tigeot ppgtt->base.allocate_va_range = NULL; 103719c468b4SFrançois Tigeot ppgtt->base.clear_range(&ppgtt->base, 0, ppgtt->base.total, true); 1038ba55f2f5SFrançois Tigeot 1039ba55f2f5SFrançois Tigeot return 0; 1040ba55f2f5SFrançois Tigeot } 1041ba55f2f5SFrançois Tigeot 1042477eb7f9SFrançois Tigeot /* 1043ba55f2f5SFrançois Tigeot * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers 1044ba55f2f5SFrançois Tigeot * with a net effect resembling a 2-level page table in normal x86 terms. Each 1045ba55f2f5SFrançois Tigeot * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address 1046ba55f2f5SFrançois Tigeot * space. 1047ba55f2f5SFrançois Tigeot * 1048ba55f2f5SFrançois Tigeot */ 104919c468b4SFrançois Tigeot static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt) 1050ba55f2f5SFrançois Tigeot { 105119c468b4SFrançois Tigeot ppgtt->scratch_pt = alloc_pt_single(ppgtt->base.dev); 105219c468b4SFrançois Tigeot if (IS_ERR(ppgtt->scratch_pt)) 105319c468b4SFrançois Tigeot return PTR_ERR(ppgtt->scratch_pt); 1054ba55f2f5SFrançois Tigeot 105519c468b4SFrançois Tigeot ppgtt->scratch_pd = alloc_pd_single(ppgtt->base.dev); 105619c468b4SFrançois Tigeot if (IS_ERR(ppgtt->scratch_pd)) 105719c468b4SFrançois Tigeot return PTR_ERR(ppgtt->scratch_pd); 1058ba55f2f5SFrançois Tigeot 105919c468b4SFrançois Tigeot gen8_initialize_pt(&ppgtt->base, ppgtt->scratch_pt); 106019c468b4SFrançois Tigeot gen8_initialize_pd(&ppgtt->base, ppgtt->scratch_pd); 106119c468b4SFrançois Tigeot 106219c468b4SFrançois Tigeot ppgtt->base.start = 0; 106319c468b4SFrançois Tigeot ppgtt->base.total = 1ULL << 32; 106419c468b4SFrançois Tigeot #define CONFIG_X86_32 0 106519c468b4SFrançois Tigeot if (IS_ENABLED(CONFIG_X86_32)) 106619c468b4SFrançois Tigeot /* While we have a proliferation of size_t variables 106719c468b4SFrançois Tigeot * we cannot represent the full ppgtt size on 32bit, 106819c468b4SFrançois Tigeot * so limit it to the same size as the GGTT (currently 106919c468b4SFrançois Tigeot * 2GiB). 1070477eb7f9SFrançois Tigeot */ 107119c468b4SFrançois Tigeot ppgtt->base.total = to_i915(ppgtt->base.dev)->gtt.base.total; 107219c468b4SFrançois Tigeot ppgtt->base.cleanup = gen8_ppgtt_cleanup; 107319c468b4SFrançois Tigeot ppgtt->base.allocate_va_range = gen8_alloc_va_range; 107419c468b4SFrançois Tigeot ppgtt->base.insert_entries = gen8_ppgtt_insert_entries; 107519c468b4SFrançois Tigeot ppgtt->base.clear_range = gen8_ppgtt_clear_range; 107619c468b4SFrançois Tigeot ppgtt->base.unbind_vma = ppgtt_unbind_vma; 107719c468b4SFrançois Tigeot ppgtt->base.bind_vma = ppgtt_bind_vma; 10789edbd4a0SFrançois Tigeot 1079ba55f2f5SFrançois Tigeot ppgtt->switch_mm = gen8_mm_switch; 1080ba55f2f5SFrançois Tigeot 10819edbd4a0SFrançois Tigeot return 0; 10829edbd4a0SFrançois Tigeot } 10839edbd4a0SFrançois Tigeot 1084ba55f2f5SFrançois Tigeot static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m) 1085ba55f2f5SFrançois Tigeot { 1086ba55f2f5SFrançois Tigeot struct i915_address_space *vm = &ppgtt->base; 108719c468b4SFrançois Tigeot struct i915_page_table *unused; 1088477eb7f9SFrançois Tigeot gen6_pte_t scratch_pte; 1089ba55f2f5SFrançois Tigeot uint32_t pd_entry; 109019c468b4SFrançois Tigeot uint32_t pte, pde, temp; 109119c468b4SFrançois Tigeot uint32_t start = ppgtt->base.start, length = ppgtt->base.total; 1092ba55f2f5SFrançois Tigeot 109324edb884SFrançois Tigeot scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true, 0); 1094ba55f2f5SFrançois Tigeot 109519c468b4SFrançois Tigeot gen6_for_each_pde(unused, &ppgtt->pd, start, length, temp, pde) { 1096ba55f2f5SFrançois Tigeot u32 expected; 1097477eb7f9SFrançois Tigeot gen6_pte_t *pt_vaddr; 1098477eb7f9SFrançois Tigeot dma_addr_t pt_addr = ppgtt->pd.page_table[pde]->daddr; 109919c468b4SFrançois Tigeot pd_entry = readl(ppgtt->pd_addr + pde); 1100ba55f2f5SFrançois Tigeot expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID); 1101ba55f2f5SFrançois Tigeot 1102ba55f2f5SFrançois Tigeot if (pd_entry != expected) 1103ba55f2f5SFrançois Tigeot seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n", 1104ba55f2f5SFrançois Tigeot pde, 1105ba55f2f5SFrançois Tigeot pd_entry, 1106ba55f2f5SFrançois Tigeot expected); 1107ba55f2f5SFrançois Tigeot seq_printf(m, "\tPDE: %x\n", pd_entry); 1108ba55f2f5SFrançois Tigeot 1109477eb7f9SFrançois Tigeot pt_vaddr = kmap_atomic(ppgtt->pd.page_table[pde]->page); 1110477eb7f9SFrançois Tigeot for (pte = 0; pte < GEN6_PTES; pte+=4) { 1111ba55f2f5SFrançois Tigeot unsigned long va = 1112477eb7f9SFrançois Tigeot (pde * PAGE_SIZE * GEN6_PTES) + 1113ba55f2f5SFrançois Tigeot (pte * PAGE_SIZE); 1114ba55f2f5SFrançois Tigeot int i; 1115ba55f2f5SFrançois Tigeot bool found = false; 1116ba55f2f5SFrançois Tigeot for (i = 0; i < 4; i++) 1117ba55f2f5SFrançois Tigeot if (pt_vaddr[pte + i] != scratch_pte) 1118ba55f2f5SFrançois Tigeot found = true; 1119ba55f2f5SFrançois Tigeot if (!found) 1120ba55f2f5SFrançois Tigeot continue; 1121ba55f2f5SFrançois Tigeot 1122ba55f2f5SFrançois Tigeot seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte); 1123ba55f2f5SFrançois Tigeot for (i = 0; i < 4; i++) { 1124ba55f2f5SFrançois Tigeot if (pt_vaddr[pte + i] != scratch_pte) 1125ba55f2f5SFrançois Tigeot seq_printf(m, " %08x", pt_vaddr[pte + i]); 1126ba55f2f5SFrançois Tigeot else 1127477eb7f9SFrançois Tigeot seq_puts(m, " SCRATCH "); 1128ba55f2f5SFrançois Tigeot } 1129477eb7f9SFrançois Tigeot seq_puts(m, "\n"); 1130ba55f2f5SFrançois Tigeot } 1131ba55f2f5SFrançois Tigeot kunmap_atomic(pt_vaddr); 1132ba55f2f5SFrançois Tigeot } 1133ba55f2f5SFrançois Tigeot } 1134ba55f2f5SFrançois Tigeot 1135477eb7f9SFrançois Tigeot /* Write pde (index) from the page directory @pd to the page table @pt */ 113619c468b4SFrançois Tigeot static void gen6_write_pde(struct i915_page_directory *pd, 113719c468b4SFrançois Tigeot const int pde, struct i915_page_table *pt) 11389edbd4a0SFrançois Tigeot { 1139477eb7f9SFrançois Tigeot /* Caller needs to make sure the write completes if necessary */ 1140477eb7f9SFrançois Tigeot struct i915_hw_ppgtt *ppgtt = 1141477eb7f9SFrançois Tigeot container_of(pd, struct i915_hw_ppgtt, pd); 1142477eb7f9SFrançois Tigeot u32 pd_entry; 11439edbd4a0SFrançois Tigeot 1144477eb7f9SFrançois Tigeot pd_entry = GEN6_PDE_ADDR_ENCODE(pt->daddr); 11459edbd4a0SFrançois Tigeot pd_entry |= GEN6_PDE_VALID; 11469edbd4a0SFrançois Tigeot 1147477eb7f9SFrançois Tigeot writel(pd_entry, ppgtt->pd_addr + pde); 11489edbd4a0SFrançois Tigeot } 1149477eb7f9SFrançois Tigeot 1150477eb7f9SFrançois Tigeot /* Write all the page tables found in the ppgtt structure to incrementing page 1151477eb7f9SFrançois Tigeot * directories. */ 1152477eb7f9SFrançois Tigeot static void gen6_write_page_range(struct drm_i915_private *dev_priv, 115319c468b4SFrançois Tigeot struct i915_page_directory *pd, 1154477eb7f9SFrançois Tigeot uint32_t start, uint32_t length) 1155477eb7f9SFrançois Tigeot { 115619c468b4SFrançois Tigeot struct i915_page_table *pt; 1157477eb7f9SFrançois Tigeot uint32_t pde, temp; 1158477eb7f9SFrançois Tigeot 1159477eb7f9SFrançois Tigeot gen6_for_each_pde(pt, pd, start, length, temp, pde) 1160477eb7f9SFrançois Tigeot gen6_write_pde(pd, pde, pt); 1161477eb7f9SFrançois Tigeot 1162477eb7f9SFrançois Tigeot /* Make sure write is complete before other code can use this page 1163477eb7f9SFrançois Tigeot * table. Also require for WC mapped PTEs */ 1164477eb7f9SFrançois Tigeot readl(dev_priv->gtt.gsm); 11659edbd4a0SFrançois Tigeot } 11669edbd4a0SFrançois Tigeot 1167ba55f2f5SFrançois Tigeot static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt) 11688e26cdf6SFrançois Tigeot { 1169477eb7f9SFrançois Tigeot BUG_ON(ppgtt->pd.pd_offset & 0x3f); 11705d0b1887SFrançois Tigeot 1171477eb7f9SFrançois Tigeot return (ppgtt->pd.pd_offset / 64) << 16; 1172ba55f2f5SFrançois Tigeot } 11738e26cdf6SFrançois Tigeot 1174ba55f2f5SFrançois Tigeot static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt, 11751b13d190SFrançois Tigeot struct intel_engine_cs *ring) 1176ba55f2f5SFrançois Tigeot { 1177ba55f2f5SFrançois Tigeot int ret; 11788e26cdf6SFrançois Tigeot 1179ba55f2f5SFrançois Tigeot /* NB: TLBs must be flushed and invalidated before a switch */ 1180ba55f2f5SFrançois Tigeot ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS); 1181ba55f2f5SFrançois Tigeot if (ret) 1182ba55f2f5SFrançois Tigeot return ret; 11838e26cdf6SFrançois Tigeot 1184ba55f2f5SFrançois Tigeot ret = intel_ring_begin(ring, 6); 1185ba55f2f5SFrançois Tigeot if (ret) 1186ba55f2f5SFrançois Tigeot return ret; 11878e26cdf6SFrançois Tigeot 1188ba55f2f5SFrançois Tigeot intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2)); 1189ba55f2f5SFrançois Tigeot intel_ring_emit(ring, RING_PP_DIR_DCLV(ring)); 1190ba55f2f5SFrançois Tigeot intel_ring_emit(ring, PP_DIR_DCLV_2G); 1191ba55f2f5SFrançois Tigeot intel_ring_emit(ring, RING_PP_DIR_BASE(ring)); 1192ba55f2f5SFrançois Tigeot intel_ring_emit(ring, get_pd_offset(ppgtt)); 1193ba55f2f5SFrançois Tigeot intel_ring_emit(ring, MI_NOOP); 1194ba55f2f5SFrançois Tigeot intel_ring_advance(ring); 1195ba55f2f5SFrançois Tigeot 1196ba55f2f5SFrançois Tigeot return 0; 1197ba55f2f5SFrançois Tigeot } 1198ba55f2f5SFrançois Tigeot 1199477eb7f9SFrançois Tigeot static int vgpu_mm_switch(struct i915_hw_ppgtt *ppgtt, 1200477eb7f9SFrançois Tigeot struct intel_engine_cs *ring) 1201477eb7f9SFrançois Tigeot { 1202477eb7f9SFrançois Tigeot struct drm_i915_private *dev_priv = to_i915(ppgtt->base.dev); 1203477eb7f9SFrançois Tigeot 1204477eb7f9SFrançois Tigeot I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G); 1205477eb7f9SFrançois Tigeot I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt)); 1206477eb7f9SFrançois Tigeot return 0; 1207477eb7f9SFrançois Tigeot } 1208477eb7f9SFrançois Tigeot 1209ba55f2f5SFrançois Tigeot static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt, 12101b13d190SFrançois Tigeot struct intel_engine_cs *ring) 1211ba55f2f5SFrançois Tigeot { 1212ba55f2f5SFrançois Tigeot int ret; 1213ba55f2f5SFrançois Tigeot 1214ba55f2f5SFrançois Tigeot /* NB: TLBs must be flushed and invalidated before a switch */ 1215ba55f2f5SFrançois Tigeot ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS); 1216ba55f2f5SFrançois Tigeot if (ret) 1217ba55f2f5SFrançois Tigeot return ret; 1218ba55f2f5SFrançois Tigeot 1219ba55f2f5SFrançois Tigeot ret = intel_ring_begin(ring, 6); 1220ba55f2f5SFrançois Tigeot if (ret) 1221ba55f2f5SFrançois Tigeot return ret; 1222ba55f2f5SFrançois Tigeot 1223ba55f2f5SFrançois Tigeot intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2)); 1224ba55f2f5SFrançois Tigeot intel_ring_emit(ring, RING_PP_DIR_DCLV(ring)); 1225ba55f2f5SFrançois Tigeot intel_ring_emit(ring, PP_DIR_DCLV_2G); 1226ba55f2f5SFrançois Tigeot intel_ring_emit(ring, RING_PP_DIR_BASE(ring)); 1227ba55f2f5SFrançois Tigeot intel_ring_emit(ring, get_pd_offset(ppgtt)); 1228ba55f2f5SFrançois Tigeot intel_ring_emit(ring, MI_NOOP); 1229ba55f2f5SFrançois Tigeot intel_ring_advance(ring); 1230ba55f2f5SFrançois Tigeot 1231ba55f2f5SFrançois Tigeot /* XXX: RCS is the only one to auto invalidate the TLBs? */ 1232ba55f2f5SFrançois Tigeot if (ring->id != RCS) { 1233ba55f2f5SFrançois Tigeot ret = ring->flush(ring, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS); 1234ba55f2f5SFrançois Tigeot if (ret) 1235ba55f2f5SFrançois Tigeot return ret; 1236ba55f2f5SFrançois Tigeot } 1237ba55f2f5SFrançois Tigeot 1238ba55f2f5SFrançois Tigeot return 0; 1239ba55f2f5SFrançois Tigeot } 1240ba55f2f5SFrançois Tigeot 1241ba55f2f5SFrançois Tigeot static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt, 12421b13d190SFrançois Tigeot struct intel_engine_cs *ring) 1243ba55f2f5SFrançois Tigeot { 1244ba55f2f5SFrançois Tigeot struct drm_device *dev = ppgtt->base.dev; 1245ba55f2f5SFrançois Tigeot struct drm_i915_private *dev_priv = dev->dev_private; 1246ba55f2f5SFrançois Tigeot 1247ba55f2f5SFrançois Tigeot 1248ba55f2f5SFrançois Tigeot I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G); 1249ba55f2f5SFrançois Tigeot I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt)); 1250ba55f2f5SFrançois Tigeot 1251ba55f2f5SFrançois Tigeot POSTING_READ(RING_PP_DIR_DCLV(ring)); 1252ba55f2f5SFrançois Tigeot 1253ba55f2f5SFrançois Tigeot return 0; 1254ba55f2f5SFrançois Tigeot } 1255ba55f2f5SFrançois Tigeot 12561b13d190SFrançois Tigeot static void gen8_ppgtt_enable(struct drm_device *dev) 1257ba55f2f5SFrançois Tigeot { 1258ba55f2f5SFrançois Tigeot struct drm_i915_private *dev_priv = dev->dev_private; 1259ba55f2f5SFrançois Tigeot struct intel_engine_cs *ring; 12601b13d190SFrançois Tigeot int j; 1261ba55f2f5SFrançois Tigeot 1262ba55f2f5SFrançois Tigeot for_each_ring(ring, dev_priv, j) { 1263ba55f2f5SFrançois Tigeot I915_WRITE(RING_MODE_GEN7(ring), 1264ba55f2f5SFrançois Tigeot _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE)); 12651b13d190SFrançois Tigeot } 1266ba55f2f5SFrançois Tigeot } 1267ba55f2f5SFrançois Tigeot 12681b13d190SFrançois Tigeot static void gen7_ppgtt_enable(struct drm_device *dev) 1269ba55f2f5SFrançois Tigeot { 1270ba55f2f5SFrançois Tigeot struct drm_i915_private *dev_priv = dev->dev_private; 1271ba55f2f5SFrançois Tigeot struct intel_engine_cs *ring; 12728e26cdf6SFrançois Tigeot uint32_t ecochk, ecobits; 1273ba55f2f5SFrançois Tigeot int i; 12748e26cdf6SFrançois Tigeot 12758e26cdf6SFrançois Tigeot ecobits = I915_READ(GAC_ECO_BITS); 12768e26cdf6SFrançois Tigeot I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B); 12778e26cdf6SFrançois Tigeot 12788e26cdf6SFrançois Tigeot ecochk = I915_READ(GAM_ECOCHK); 12798e26cdf6SFrançois Tigeot if (IS_HASWELL(dev)) { 12808e26cdf6SFrançois Tigeot ecochk |= ECOCHK_PPGTT_WB_HSW; 12818e26cdf6SFrançois Tigeot } else { 12828e26cdf6SFrançois Tigeot ecochk |= ECOCHK_PPGTT_LLC_IVB; 12838e26cdf6SFrançois Tigeot ecochk &= ~ECOCHK_PPGTT_GFDT_IVB; 12848e26cdf6SFrançois Tigeot } 12858e26cdf6SFrançois Tigeot I915_WRITE(GAM_ECOCHK, ecochk); 12868e26cdf6SFrançois Tigeot 12878e26cdf6SFrançois Tigeot for_each_ring(ring, dev_priv, i) { 1288ba55f2f5SFrançois Tigeot /* GFX_MODE is per-ring on gen7+ */ 12898e26cdf6SFrançois Tigeot I915_WRITE(RING_MODE_GEN7(ring), 12908e26cdf6SFrançois Tigeot _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE)); 12911b13d190SFrançois Tigeot } 12928e26cdf6SFrançois Tigeot } 1293ba55f2f5SFrançois Tigeot 12941b13d190SFrançois Tigeot static void gen6_ppgtt_enable(struct drm_device *dev) 1295ba55f2f5SFrançois Tigeot { 1296ba55f2f5SFrançois Tigeot struct drm_i915_private *dev_priv = dev->dev_private; 1297ba55f2f5SFrançois Tigeot uint32_t ecochk, gab_ctl, ecobits; 1298ba55f2f5SFrançois Tigeot 1299ba55f2f5SFrançois Tigeot ecobits = I915_READ(GAC_ECO_BITS); 1300ba55f2f5SFrançois Tigeot I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT | 1301ba55f2f5SFrançois Tigeot ECOBITS_PPGTT_CACHE64B); 1302ba55f2f5SFrançois Tigeot 1303ba55f2f5SFrançois Tigeot gab_ctl = I915_READ(GAB_CTL); 1304ba55f2f5SFrançois Tigeot I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT); 1305ba55f2f5SFrançois Tigeot 1306ba55f2f5SFrançois Tigeot ecochk = I915_READ(GAM_ECOCHK); 1307ba55f2f5SFrançois Tigeot I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B); 1308ba55f2f5SFrançois Tigeot 1309ba55f2f5SFrançois Tigeot I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE)); 13108e26cdf6SFrançois Tigeot } 13118e26cdf6SFrançois Tigeot 1312f4e1c372SFrançois Tigeot /* PPGTT support for Sandybdrige/Gen6 and later */ 13139edbd4a0SFrançois Tigeot static void gen6_ppgtt_clear_range(struct i915_address_space *vm, 1314ba55f2f5SFrançois Tigeot uint64_t start, 1315ba55f2f5SFrançois Tigeot uint64_t length, 13169edbd4a0SFrançois Tigeot bool use_scratch) 1317f4e1c372SFrançois Tigeot { 13189edbd4a0SFrançois Tigeot struct i915_hw_ppgtt *ppgtt = 13199edbd4a0SFrançois Tigeot container_of(vm, struct i915_hw_ppgtt, base); 1320477eb7f9SFrançois Tigeot gen6_pte_t *pt_vaddr, scratch_pte; 1321ba55f2f5SFrançois Tigeot unsigned first_entry = start >> PAGE_SHIFT; 1322ba55f2f5SFrançois Tigeot unsigned num_entries = length >> PAGE_SHIFT; 1323477eb7f9SFrançois Tigeot unsigned act_pt = first_entry / GEN6_PTES; 1324477eb7f9SFrançois Tigeot unsigned first_pte = first_entry % GEN6_PTES; 1325f4e1c372SFrançois Tigeot unsigned last_pte, i; 1326e3adcf8fSFrançois Tigeot 132724edb884SFrançois Tigeot scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, true, 0); 1328e3adcf8fSFrançois Tigeot 1329e3adcf8fSFrançois Tigeot while (num_entries) { 1330e3adcf8fSFrançois Tigeot last_pte = first_pte + num_entries; 1331477eb7f9SFrançois Tigeot if (last_pte > GEN6_PTES) 1332477eb7f9SFrançois Tigeot last_pte = GEN6_PTES; 1333e3adcf8fSFrançois Tigeot 1334477eb7f9SFrançois Tigeot pt_vaddr = kmap_atomic(ppgtt->pd.page_table[act_pt]->page); 1335e3adcf8fSFrançois Tigeot 1336e3adcf8fSFrançois Tigeot for (i = first_pte; i < last_pte; i++) 1337e3adcf8fSFrançois Tigeot pt_vaddr[i] = scratch_pte; 1338e3adcf8fSFrançois Tigeot 133982046b5cSFrançois Tigeot kunmap_atomic(pt_vaddr); 1340e3adcf8fSFrançois Tigeot 1341e3adcf8fSFrançois Tigeot num_entries -= last_pte - first_pte; 1342e3adcf8fSFrançois Tigeot first_pte = 0; 13438e26cdf6SFrançois Tigeot act_pt++; 1344e3adcf8fSFrançois Tigeot } 1345e3adcf8fSFrançois Tigeot } 1346e3adcf8fSFrançois Tigeot 13479edbd4a0SFrançois Tigeot static void gen6_ppgtt_insert_entries(struct i915_address_space *vm, 1348*7ec9f8e5SFrançois Tigeot struct sg_table *pages, 1349ba55f2f5SFrançois Tigeot uint64_t start, 135024edb884SFrançois Tigeot enum i915_cache_level cache_level, u32 flags) 1351e3adcf8fSFrançois Tigeot { 13529edbd4a0SFrançois Tigeot struct i915_hw_ppgtt *ppgtt = 13539edbd4a0SFrançois Tigeot container_of(vm, struct i915_hw_ppgtt, base); 1354477eb7f9SFrançois Tigeot gen6_pte_t *pt_vaddr; 1355ba55f2f5SFrançois Tigeot unsigned first_entry = start >> PAGE_SHIFT; 1356477eb7f9SFrançois Tigeot unsigned act_pt = first_entry / GEN6_PTES; 1357477eb7f9SFrançois Tigeot unsigned act_pte = first_entry % GEN6_PTES; 1358*7ec9f8e5SFrançois Tigeot struct sg_page_iter sg_iter; 1359a2fdbec6SFrançois Tigeot 13609edbd4a0SFrançois Tigeot pt_vaddr = NULL; 1361*7ec9f8e5SFrançois Tigeot for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) { 13629edbd4a0SFrançois Tigeot if (pt_vaddr == NULL) 1363477eb7f9SFrançois Tigeot pt_vaddr = kmap_atomic(ppgtt->pd.page_table[act_pt]->page); 1364a2fdbec6SFrançois Tigeot 13659edbd4a0SFrançois Tigeot pt_vaddr[act_pte] = 1366*7ec9f8e5SFrançois Tigeot vm->pte_encode(sg_page_iter_dma_address(&sg_iter), 136724edb884SFrançois Tigeot cache_level, true, flags); 1368477eb7f9SFrançois Tigeot 1369477eb7f9SFrançois Tigeot if (++act_pte == GEN6_PTES) { 1370a2fdbec6SFrançois Tigeot kunmap_atomic(pt_vaddr); 13719edbd4a0SFrançois Tigeot pt_vaddr = NULL; 13728e26cdf6SFrançois Tigeot act_pt++; 13739edbd4a0SFrançois Tigeot act_pte = 0; 1374a2fdbec6SFrançois Tigeot } 1375a2fdbec6SFrançois Tigeot } 13769edbd4a0SFrançois Tigeot if (pt_vaddr) 13779edbd4a0SFrançois Tigeot kunmap_atomic(pt_vaddr); 13789edbd4a0SFrançois Tigeot } 1379a2fdbec6SFrançois Tigeot 1380477eb7f9SFrançois Tigeot /* PDE TLBs are a pain invalidate pre GEN8. It requires a context reload. If we 1381477eb7f9SFrançois Tigeot * are switching between contexts with the same LRCA, we also must do a force 1382477eb7f9SFrançois Tigeot * restore. 1383477eb7f9SFrançois Tigeot */ 138419c468b4SFrançois Tigeot static void mark_tlbs_dirty(struct i915_hw_ppgtt *ppgtt) 1385a2fdbec6SFrançois Tigeot { 1386477eb7f9SFrançois Tigeot /* If current vm != vm, */ 1387477eb7f9SFrançois Tigeot ppgtt->pd_dirty_rings = INTEL_INFO(ppgtt->base.dev)->ring_mask; 1388477eb7f9SFrançois Tigeot } 1389477eb7f9SFrançois Tigeot 1390477eb7f9SFrançois Tigeot static void gen6_initialize_pt(struct i915_address_space *vm, 139119c468b4SFrançois Tigeot struct i915_page_table *pt) 1392477eb7f9SFrançois Tigeot { 1393477eb7f9SFrançois Tigeot gen6_pte_t *pt_vaddr, scratch_pte; 1394a2fdbec6SFrançois Tigeot int i; 1395a2fdbec6SFrançois Tigeot 1396477eb7f9SFrançois Tigeot WARN_ON(vm->scratch.addr == 0); 1397477eb7f9SFrançois Tigeot 1398477eb7f9SFrançois Tigeot scratch_pte = vm->pte_encode(vm->scratch.addr, 1399477eb7f9SFrançois Tigeot I915_CACHE_LLC, true, 0); 1400477eb7f9SFrançois Tigeot 1401477eb7f9SFrançois Tigeot pt_vaddr = kmap_atomic(pt->page); 1402477eb7f9SFrançois Tigeot 1403477eb7f9SFrançois Tigeot for (i = 0; i < GEN6_PTES; i++) 1404477eb7f9SFrançois Tigeot pt_vaddr[i] = scratch_pte; 1405477eb7f9SFrançois Tigeot 1406477eb7f9SFrançois Tigeot kunmap_atomic(pt_vaddr); 1407a2fdbec6SFrançois Tigeot } 1408477eb7f9SFrançois Tigeot 1409477eb7f9SFrançois Tigeot static int gen6_alloc_va_range(struct i915_address_space *vm, 1410477eb7f9SFrançois Tigeot uint64_t start, uint64_t length) 1411477eb7f9SFrançois Tigeot { 1412477eb7f9SFrançois Tigeot DECLARE_BITMAP(new_page_tables, I915_PDES); 1413477eb7f9SFrançois Tigeot struct drm_device *dev = vm->dev; 1414477eb7f9SFrançois Tigeot struct drm_i915_private *dev_priv = dev->dev_private; 1415477eb7f9SFrançois Tigeot struct i915_hw_ppgtt *ppgtt = 1416477eb7f9SFrançois Tigeot container_of(vm, struct i915_hw_ppgtt, base); 141719c468b4SFrançois Tigeot struct i915_page_table *pt; 1418477eb7f9SFrançois Tigeot const uint32_t start_save = start, length_save = length; 1419477eb7f9SFrançois Tigeot uint32_t pde, temp; 1420477eb7f9SFrançois Tigeot int ret; 1421477eb7f9SFrançois Tigeot 1422477eb7f9SFrançois Tigeot WARN_ON(upper_32_bits(start)); 1423477eb7f9SFrançois Tigeot 1424477eb7f9SFrançois Tigeot bitmap_zero(new_page_tables, I915_PDES); 1425477eb7f9SFrançois Tigeot 1426477eb7f9SFrançois Tigeot /* The allocation is done in two stages so that we can bail out with 1427477eb7f9SFrançois Tigeot * minimal amount of pain. The first stage finds new page tables that 1428477eb7f9SFrançois Tigeot * need allocation. The second stage marks use ptes within the page 1429477eb7f9SFrançois Tigeot * tables. 1430477eb7f9SFrançois Tigeot */ 1431477eb7f9SFrançois Tigeot gen6_for_each_pde(pt, &ppgtt->pd, start, length, temp, pde) { 1432477eb7f9SFrançois Tigeot if (pt != ppgtt->scratch_pt) { 1433477eb7f9SFrançois Tigeot WARN_ON(bitmap_empty(pt->used_ptes, GEN6_PTES)); 1434477eb7f9SFrançois Tigeot continue; 1435477eb7f9SFrançois Tigeot } 1436477eb7f9SFrançois Tigeot 1437477eb7f9SFrançois Tigeot /* We've already allocated a page table */ 1438477eb7f9SFrançois Tigeot WARN_ON(!bitmap_empty(pt->used_ptes, GEN6_PTES)); 1439477eb7f9SFrançois Tigeot 1440477eb7f9SFrançois Tigeot pt = alloc_pt_single(dev); 1441477eb7f9SFrançois Tigeot if (IS_ERR(pt)) { 1442477eb7f9SFrançois Tigeot ret = PTR_ERR(pt); 1443477eb7f9SFrançois Tigeot goto unwind_out; 1444477eb7f9SFrançois Tigeot } 1445477eb7f9SFrançois Tigeot 1446477eb7f9SFrançois Tigeot gen6_initialize_pt(vm, pt); 1447477eb7f9SFrançois Tigeot 1448477eb7f9SFrançois Tigeot ppgtt->pd.page_table[pde] = pt; 1449477eb7f9SFrançois Tigeot set_bit(pde, new_page_tables); 1450477eb7f9SFrançois Tigeot trace_i915_page_table_entry_alloc(vm, pde, start, GEN6_PDE_SHIFT); 1451477eb7f9SFrançois Tigeot } 1452477eb7f9SFrançois Tigeot 1453477eb7f9SFrançois Tigeot start = start_save; 1454477eb7f9SFrançois Tigeot length = length_save; 1455477eb7f9SFrançois Tigeot 1456477eb7f9SFrançois Tigeot gen6_for_each_pde(pt, &ppgtt->pd, start, length, temp, pde) { 1457477eb7f9SFrançois Tigeot DECLARE_BITMAP(tmp_bitmap, GEN6_PTES); 1458477eb7f9SFrançois Tigeot 1459477eb7f9SFrançois Tigeot bitmap_zero(tmp_bitmap, GEN6_PTES); 1460477eb7f9SFrançois Tigeot bitmap_set(tmp_bitmap, gen6_pte_index(start), 1461477eb7f9SFrançois Tigeot gen6_pte_count(start, length)); 1462477eb7f9SFrançois Tigeot 1463477eb7f9SFrançois Tigeot if (test_and_clear_bit(pde, new_page_tables)) 1464477eb7f9SFrançois Tigeot gen6_write_pde(&ppgtt->pd, pde, pt); 1465477eb7f9SFrançois Tigeot 1466477eb7f9SFrançois Tigeot trace_i915_page_table_entry_map(vm, pde, pt, 1467477eb7f9SFrançois Tigeot gen6_pte_index(start), 1468477eb7f9SFrançois Tigeot gen6_pte_count(start, length), 1469477eb7f9SFrançois Tigeot GEN6_PTES); 1470477eb7f9SFrançois Tigeot bitmap_or(pt->used_ptes, tmp_bitmap, pt->used_ptes, 1471477eb7f9SFrançois Tigeot GEN6_PTES); 1472477eb7f9SFrançois Tigeot } 1473477eb7f9SFrançois Tigeot 1474477eb7f9SFrançois Tigeot WARN_ON(!bitmap_empty(new_page_tables, I915_PDES)); 1475477eb7f9SFrançois Tigeot 1476477eb7f9SFrançois Tigeot /* Make sure write is complete before other code can use this page 1477477eb7f9SFrançois Tigeot * table. Also require for WC mapped PTEs */ 1478477eb7f9SFrançois Tigeot readl(dev_priv->gtt.gsm); 1479477eb7f9SFrançois Tigeot 1480477eb7f9SFrançois Tigeot mark_tlbs_dirty(ppgtt); 1481477eb7f9SFrançois Tigeot return 0; 1482477eb7f9SFrançois Tigeot 1483477eb7f9SFrançois Tigeot unwind_out: 1484477eb7f9SFrançois Tigeot for_each_set_bit(pde, new_page_tables, I915_PDES) { 148519c468b4SFrançois Tigeot struct i915_page_table *pt = ppgtt->pd.page_table[pde]; 1486477eb7f9SFrançois Tigeot 1487477eb7f9SFrançois Tigeot ppgtt->pd.page_table[pde] = ppgtt->scratch_pt; 1488477eb7f9SFrançois Tigeot unmap_and_free_pt(pt, vm->dev); 1489477eb7f9SFrançois Tigeot } 1490477eb7f9SFrançois Tigeot 1491477eb7f9SFrançois Tigeot mark_tlbs_dirty(ppgtt); 1492477eb7f9SFrançois Tigeot return ret; 1493ba55f2f5SFrançois Tigeot } 1494ba55f2f5SFrançois Tigeot 1495ba55f2f5SFrançois Tigeot static void gen6_ppgtt_cleanup(struct i915_address_space *vm) 1496ba55f2f5SFrançois Tigeot { 1497ba55f2f5SFrançois Tigeot struct i915_hw_ppgtt *ppgtt = 1498ba55f2f5SFrançois Tigeot container_of(vm, struct i915_hw_ppgtt, base); 149919c468b4SFrançois Tigeot struct i915_page_table *pt; 150019c468b4SFrançois Tigeot uint32_t pde; 150119c468b4SFrançois Tigeot 1502ba55f2f5SFrançois Tigeot 1503ba55f2f5SFrançois Tigeot drm_mm_remove_node(&ppgtt->node); 1504ba55f2f5SFrançois Tigeot 150519c468b4SFrançois Tigeot gen6_for_all_pdes(pt, ppgtt, pde) { 150619c468b4SFrançois Tigeot if (pt != ppgtt->scratch_pt) 150719c468b4SFrançois Tigeot unmap_and_free_pt(pt, ppgtt->base.dev); 150819c468b4SFrançois Tigeot } 150919c468b4SFrançois Tigeot 151019c468b4SFrançois Tigeot unmap_and_free_pt(ppgtt->scratch_pt, ppgtt->base.dev); 151119c468b4SFrançois Tigeot unmap_and_free_pd(&ppgtt->pd, ppgtt->base.dev); 1512ba55f2f5SFrançois Tigeot } 1513ba55f2f5SFrançois Tigeot 1514ba55f2f5SFrançois Tigeot static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt) 1515a2fdbec6SFrançois Tigeot { 15169edbd4a0SFrançois Tigeot struct drm_device *dev = ppgtt->base.dev; 15177cbd1a46SFrançois Tigeot struct drm_i915_private *dev_priv = dev->dev_private; 1518ba55f2f5SFrançois Tigeot bool retried = false; 1519ba55f2f5SFrançois Tigeot int ret; 1520e3adcf8fSFrançois Tigeot 1521ba55f2f5SFrançois Tigeot /* PPGTT PDEs reside in the GGTT and consists of 512 entries. The 1522ba55f2f5SFrançois Tigeot * allocator works in address space sizes, so it's multiplied by page 1523ba55f2f5SFrançois Tigeot * size. We allocate at the top of the GTT to avoid fragmentation. 1524ba55f2f5SFrançois Tigeot */ 1525ba55f2f5SFrançois Tigeot BUG_ON(!drm_mm_initialized(&dev_priv->gtt.base.mm)); 1526477eb7f9SFrançois Tigeot ppgtt->scratch_pt = alloc_pt_single(ppgtt->base.dev); 1527477eb7f9SFrançois Tigeot if (IS_ERR(ppgtt->scratch_pt)) 1528477eb7f9SFrançois Tigeot return PTR_ERR(ppgtt->scratch_pt); 1529477eb7f9SFrançois Tigeot 1530477eb7f9SFrançois Tigeot gen6_initialize_pt(&ppgtt->base, ppgtt->scratch_pt); 1531477eb7f9SFrançois Tigeot 1532ba55f2f5SFrançois Tigeot alloc: 1533ba55f2f5SFrançois Tigeot ret = drm_mm_insert_node_in_range_generic(&dev_priv->gtt.base.mm, 1534ba55f2f5SFrançois Tigeot &ppgtt->node, GEN6_PD_SIZE, 1535ba55f2f5SFrançois Tigeot GEN6_PD_ALIGN, 0, 1536ba55f2f5SFrançois Tigeot 0, dev_priv->gtt.base.total, 1537ba55f2f5SFrançois Tigeot DRM_MM_TOPDOWN); 1538ba55f2f5SFrançois Tigeot if (ret == -ENOSPC && !retried) { 1539ba55f2f5SFrançois Tigeot ret = i915_gem_evict_something(dev, &dev_priv->gtt.base, 1540ba55f2f5SFrançois Tigeot GEN6_PD_SIZE, GEN6_PD_ALIGN, 1541ba55f2f5SFrançois Tigeot I915_CACHE_NONE, 1542ba55f2f5SFrançois Tigeot 0, dev_priv->gtt.base.total, 1543ba55f2f5SFrançois Tigeot 0); 1544ba55f2f5SFrançois Tigeot if (ret) 1545477eb7f9SFrançois Tigeot goto err_out; 1546e3adcf8fSFrançois Tigeot 1547ba55f2f5SFrançois Tigeot retried = true; 1548ba55f2f5SFrançois Tigeot goto alloc; 1549ba55f2f5SFrançois Tigeot } 1550ba55f2f5SFrançois Tigeot 1551477eb7f9SFrançois Tigeot if (ret) 1552477eb7f9SFrançois Tigeot goto err_out; 1553477eb7f9SFrançois Tigeot 1554477eb7f9SFrançois Tigeot 1555ba55f2f5SFrançois Tigeot if (ppgtt->node.start < dev_priv->gtt.mappable_end) 1556ba55f2f5SFrançois Tigeot DRM_DEBUG("Forced to use aperture for PDEs\n"); 1557ba55f2f5SFrançois Tigeot 1558ba55f2f5SFrançois Tigeot return 0; 1559477eb7f9SFrançois Tigeot 1560477eb7f9SFrançois Tigeot err_out: 1561477eb7f9SFrançois Tigeot unmap_and_free_pt(ppgtt->scratch_pt, ppgtt->base.dev); 1562477eb7f9SFrançois Tigeot return ret; 1563ba55f2f5SFrançois Tigeot } 1564ba55f2f5SFrançois Tigeot 1565ba55f2f5SFrançois Tigeot static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt) 1566ba55f2f5SFrançois Tigeot { 1567477eb7f9SFrançois Tigeot return gen6_ppgtt_allocate_page_directories(ppgtt); 1568e3adcf8fSFrançois Tigeot } 1569e3adcf8fSFrançois Tigeot 1570477eb7f9SFrançois Tigeot static void gen6_scratch_va_range(struct i915_hw_ppgtt *ppgtt, 1571477eb7f9SFrançois Tigeot uint64_t start, uint64_t length) 1572ba55f2f5SFrançois Tigeot { 157319c468b4SFrançois Tigeot struct i915_page_table *unused; 1574477eb7f9SFrançois Tigeot uint32_t pde, temp; 15759edbd4a0SFrançois Tigeot 1576477eb7f9SFrançois Tigeot gen6_for_each_pde(unused, &ppgtt->pd, start, length, temp, pde) 1577477eb7f9SFrançois Tigeot ppgtt->pd.page_table[pde] = ppgtt->scratch_pt; 15789edbd4a0SFrançois Tigeot } 1579ba55f2f5SFrançois Tigeot 1580477eb7f9SFrançois Tigeot static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt, bool aliasing) 1581ba55f2f5SFrançois Tigeot { 1582ba55f2f5SFrançois Tigeot struct drm_device *dev = ppgtt->base.dev; 1583ba55f2f5SFrançois Tigeot struct drm_i915_private *dev_priv = dev->dev_private; 1584ba55f2f5SFrançois Tigeot int ret; 1585ba55f2f5SFrançois Tigeot 1586ba55f2f5SFrançois Tigeot ppgtt->base.pte_encode = dev_priv->gtt.base.pte_encode; 1587ba55f2f5SFrançois Tigeot if (IS_GEN6(dev)) { 1588ba55f2f5SFrançois Tigeot ppgtt->switch_mm = gen6_mm_switch; 1589ba55f2f5SFrançois Tigeot } else if (IS_HASWELL(dev)) { 1590ba55f2f5SFrançois Tigeot ppgtt->switch_mm = hsw_mm_switch; 1591ba55f2f5SFrançois Tigeot } else if (IS_GEN7(dev)) { 1592ba55f2f5SFrançois Tigeot ppgtt->switch_mm = gen7_mm_switch; 1593ba55f2f5SFrançois Tigeot } else 1594ba55f2f5SFrançois Tigeot BUG(); 1595ba55f2f5SFrançois Tigeot 1596477eb7f9SFrançois Tigeot if (intel_vgpu_active(dev)) 1597477eb7f9SFrançois Tigeot ppgtt->switch_mm = vgpu_mm_switch; 1598477eb7f9SFrançois Tigeot 1599ba55f2f5SFrançois Tigeot ret = gen6_ppgtt_alloc(ppgtt); 1600ba55f2f5SFrançois Tigeot if (ret) 1601ba55f2f5SFrançois Tigeot return ret; 1602ba55f2f5SFrançois Tigeot 1603477eb7f9SFrançois Tigeot if (aliasing) { 1604477eb7f9SFrançois Tigeot /* preallocate all pts */ 160519c468b4SFrançois Tigeot ret = alloc_pt_range(&ppgtt->pd, 0, I915_PDES, 1606477eb7f9SFrançois Tigeot ppgtt->base.dev); 1607477eb7f9SFrançois Tigeot 1608ba55f2f5SFrançois Tigeot if (ret) { 1609477eb7f9SFrançois Tigeot gen6_ppgtt_cleanup(&ppgtt->base); 16100b869d8aSFrançois Tigeot return ret; 1611e3adcf8fSFrançois Tigeot } 1612477eb7f9SFrançois Tigeot } 1613e3adcf8fSFrançois Tigeot 161419c468b4SFrançois Tigeot ppgtt->base.allocate_va_range = aliasing ? NULL : gen6_alloc_va_range; 1615*7ec9f8e5SFrançois Tigeot ppgtt->base.allocate_va_range = gen6_alloc_va_range; 1616ba55f2f5SFrançois Tigeot ppgtt->base.clear_range = gen6_ppgtt_clear_range; 1617ba55f2f5SFrançois Tigeot ppgtt->base.insert_entries = gen6_ppgtt_insert_entries; 161819c468b4SFrançois Tigeot ppgtt->base.unbind_vma = ppgtt_unbind_vma; 161919c468b4SFrançois Tigeot ppgtt->base.bind_vma = ppgtt_bind_vma; 1620ba55f2f5SFrançois Tigeot ppgtt->base.cleanup = gen6_ppgtt_cleanup; 1621ba55f2f5SFrançois Tigeot ppgtt->base.start = 0; 162219c468b4SFrançois Tigeot ppgtt->base.total = I915_PDES * GEN6_PTES * PAGE_SIZE; 1623ba55f2f5SFrançois Tigeot ppgtt->debug_dump = gen6_dump_ppgtt; 1624ba55f2f5SFrançois Tigeot 1625477eb7f9SFrançois Tigeot ppgtt->pd.pd_offset = 1626477eb7f9SFrançois Tigeot ppgtt->node.start / PAGE_SIZE * sizeof(gen6_pte_t); 1627ba55f2f5SFrançois Tigeot 1628477eb7f9SFrançois Tigeot ppgtt->pd_addr = (gen6_pte_t __iomem *)dev_priv->gtt.gsm + 1629477eb7f9SFrançois Tigeot ppgtt->pd.pd_offset / sizeof(gen6_pte_t); 1630477eb7f9SFrançois Tigeot 1631477eb7f9SFrançois Tigeot if (aliasing) 1632ba55f2f5SFrançois Tigeot ppgtt->base.clear_range(&ppgtt->base, 0, ppgtt->base.total, true); 1633477eb7f9SFrançois Tigeot else 1634477eb7f9SFrançois Tigeot gen6_scratch_va_range(ppgtt, 0, ppgtt->base.total); 1635477eb7f9SFrançois Tigeot 1636477eb7f9SFrançois Tigeot gen6_write_page_range(dev_priv, &ppgtt->pd, 0, ppgtt->base.total); 1637ba55f2f5SFrançois Tigeot 1638ba55f2f5SFrançois Tigeot DRM_DEBUG_DRIVER("Allocated pde space (%ldM) at GTT entry: %lx\n", 1639ba55f2f5SFrançois Tigeot ppgtt->node.size >> 20, 1640ba55f2f5SFrançois Tigeot ppgtt->node.start / PAGE_SIZE); 1641ba55f2f5SFrançois Tigeot 16421b13d190SFrançois Tigeot DRM_DEBUG("Adding PPGTT at offset %x\n", 1643477eb7f9SFrançois Tigeot ppgtt->pd.pd_offset << 10); 16441b13d190SFrançois Tigeot 1645ba55f2f5SFrançois Tigeot return 0; 1646ba55f2f5SFrançois Tigeot } 1647ba55f2f5SFrançois Tigeot 1648477eb7f9SFrançois Tigeot static int __hw_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt, 1649477eb7f9SFrançois Tigeot bool aliasing) 1650a2fdbec6SFrançois Tigeot { 1651a2fdbec6SFrançois Tigeot struct drm_i915_private *dev_priv = dev->dev_private; 1652a2fdbec6SFrançois Tigeot 16539edbd4a0SFrançois Tigeot ppgtt->base.dev = dev; 1654ba55f2f5SFrançois Tigeot ppgtt->base.scratch = dev_priv->gtt.base.scratch; 1655a2fdbec6SFrançois Tigeot 16568e26cdf6SFrançois Tigeot if (INTEL_INFO(dev)->gen < 8) 1657477eb7f9SFrançois Tigeot return gen6_ppgtt_init(ppgtt, aliasing); 165819c468b4SFrançois Tigeot else if (aliasing) 165919c468b4SFrançois Tigeot return gen8_aliasing_ppgtt_init(ppgtt); 16608e26cdf6SFrançois Tigeot else 166119c468b4SFrançois Tigeot return gen8_ppgtt_init(ppgtt); 16621b13d190SFrançois Tigeot } 16631b13d190SFrançois Tigeot int i915_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt) 16641b13d190SFrançois Tigeot { 1665ba55f2f5SFrançois Tigeot struct drm_i915_private *dev_priv = dev->dev_private; 16661b13d190SFrançois Tigeot int ret = 0; 16671b13d190SFrançois Tigeot 1668477eb7f9SFrançois Tigeot ret = __hw_ppgtt_init(dev, ppgtt, false); 16691b13d190SFrançois Tigeot if (ret == 0) { 1670ba55f2f5SFrançois Tigeot kref_init(&ppgtt->ref); 16719edbd4a0SFrançois Tigeot drm_mm_init(&ppgtt->base.mm, ppgtt->base.start, 16729edbd4a0SFrançois Tigeot ppgtt->base.total); 1673ba55f2f5SFrançois Tigeot i915_init_vm(dev_priv, &ppgtt->base); 16741b13d190SFrançois Tigeot } 16751b13d190SFrançois Tigeot 16761b13d190SFrançois Tigeot return ret; 16771b13d190SFrançois Tigeot } 16781b13d190SFrançois Tigeot 16791b13d190SFrançois Tigeot int i915_ppgtt_init_hw(struct drm_device *dev) 16801b13d190SFrançois Tigeot { 16811b13d190SFrançois Tigeot struct drm_i915_private *dev_priv = dev->dev_private; 16821b13d190SFrançois Tigeot struct intel_engine_cs *ring; 16831b13d190SFrançois Tigeot struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt; 16841b13d190SFrançois Tigeot int i, ret = 0; 16851b13d190SFrançois Tigeot 16861b13d190SFrançois Tigeot /* In the case of execlists, PPGTT is enabled by the context descriptor 16871b13d190SFrançois Tigeot * and the PDPs are contained within the context itself. We don't 16881b13d190SFrançois Tigeot * need to do anything here. */ 16891b13d190SFrançois Tigeot if (i915.enable_execlists) 16901b13d190SFrançois Tigeot return 0; 16911b13d190SFrançois Tigeot 16921b13d190SFrançois Tigeot if (!USES_PPGTT(dev)) 16931b13d190SFrançois Tigeot return 0; 16941b13d190SFrançois Tigeot 16951b13d190SFrançois Tigeot if (IS_GEN6(dev)) 16961b13d190SFrançois Tigeot gen6_ppgtt_enable(dev); 16971b13d190SFrançois Tigeot else if (IS_GEN7(dev)) 16981b13d190SFrançois Tigeot gen7_ppgtt_enable(dev); 16991b13d190SFrançois Tigeot else if (INTEL_INFO(dev)->gen >= 8) 17001b13d190SFrançois Tigeot gen8_ppgtt_enable(dev); 17011b13d190SFrançois Tigeot else 17022c9916cdSFrançois Tigeot MISSING_CASE(INTEL_INFO(dev)->gen); 17031b13d190SFrançois Tigeot 17041b13d190SFrançois Tigeot if (ppgtt) { 17051b13d190SFrançois Tigeot for_each_ring(ring, dev_priv, i) { 17061b13d190SFrançois Tigeot ret = ppgtt->switch_mm(ppgtt, ring); 17071b13d190SFrançois Tigeot if (ret != 0) 17081b13d190SFrançois Tigeot return ret; 1709ba55f2f5SFrançois Tigeot } 17109edbd4a0SFrançois Tigeot } 1711a2fdbec6SFrançois Tigeot 1712a2fdbec6SFrançois Tigeot return ret; 1713a2fdbec6SFrançois Tigeot } 17141b13d190SFrançois Tigeot struct i915_hw_ppgtt * 17151b13d190SFrançois Tigeot i915_ppgtt_create(struct drm_device *dev, struct drm_i915_file_private *fpriv) 17161b13d190SFrançois Tigeot { 17171b13d190SFrançois Tigeot struct i915_hw_ppgtt *ppgtt; 17181b13d190SFrançois Tigeot int ret; 17191b13d190SFrançois Tigeot 17201b13d190SFrançois Tigeot ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL); 17211b13d190SFrançois Tigeot if (!ppgtt) 17221b13d190SFrançois Tigeot return ERR_PTR(-ENOMEM); 17231b13d190SFrançois Tigeot 17241b13d190SFrançois Tigeot ret = i915_ppgtt_init(dev, ppgtt); 17251b13d190SFrançois Tigeot if (ret) { 17261b13d190SFrançois Tigeot kfree(ppgtt); 17271b13d190SFrançois Tigeot return ERR_PTR(ret); 17281b13d190SFrançois Tigeot } 17291b13d190SFrançois Tigeot 17301b13d190SFrançois Tigeot ppgtt->file_priv = fpriv; 17311b13d190SFrançois Tigeot 17322c9916cdSFrançois Tigeot trace_i915_ppgtt_create(&ppgtt->base); 17332c9916cdSFrançois Tigeot 17341b13d190SFrançois Tigeot return ppgtt; 17351b13d190SFrançois Tigeot } 17361b13d190SFrançois Tigeot 17371b13d190SFrançois Tigeot void i915_ppgtt_release(struct kref *kref) 17381b13d190SFrançois Tigeot { 17391b13d190SFrançois Tigeot struct i915_hw_ppgtt *ppgtt = 17401b13d190SFrançois Tigeot container_of(kref, struct i915_hw_ppgtt, ref); 17411b13d190SFrançois Tigeot 17422c9916cdSFrançois Tigeot trace_i915_ppgtt_release(&ppgtt->base); 17432c9916cdSFrançois Tigeot 17441b13d190SFrançois Tigeot /* vmas should already be unbound */ 17451b13d190SFrançois Tigeot WARN_ON(!list_empty(&ppgtt->base.active_list)); 17461b13d190SFrançois Tigeot WARN_ON(!list_empty(&ppgtt->base.inactive_list)); 17471b13d190SFrançois Tigeot 17481b13d190SFrançois Tigeot list_del(&ppgtt->base.global_link); 17491b13d190SFrançois Tigeot drm_mm_takedown(&ppgtt->base.mm); 17501b13d190SFrançois Tigeot 17511b13d190SFrançois Tigeot ppgtt->base.cleanup(&ppgtt->base); 17521b13d190SFrançois Tigeot kfree(ppgtt); 17531b13d190SFrançois Tigeot } 1754a2fdbec6SFrançois Tigeot 1755a2fdbec6SFrançois Tigeot extern int intel_iommu_gfx_mapped; 1756a2fdbec6SFrançois Tigeot /* Certain Gen5 chipsets require require idling the GPU before 1757a2fdbec6SFrançois Tigeot * unmapping anything from the GTT when VT-d is enabled. 1758a2fdbec6SFrançois Tigeot */ 175919c468b4SFrançois Tigeot static bool needs_idle_maps(struct drm_device *dev) 1760a2fdbec6SFrançois Tigeot { 1761a2fdbec6SFrançois Tigeot #ifdef CONFIG_INTEL_IOMMU 1762a2fdbec6SFrançois Tigeot /* Query intel_iommu to see if we need the workaround. Presumably that 1763a2fdbec6SFrançois Tigeot * was loaded first. 1764a2fdbec6SFrançois Tigeot */ 1765a2fdbec6SFrançois Tigeot if (IS_GEN5(dev) && IS_MOBILE(dev) && intel_iommu_gfx_mapped) 1766a2fdbec6SFrançois Tigeot return true; 1767a2fdbec6SFrançois Tigeot #endif 1768a2fdbec6SFrançois Tigeot return false; 1769a2fdbec6SFrançois Tigeot } 1770a2fdbec6SFrançois Tigeot 17717cbd1a46SFrançois Tigeot static bool do_idling(struct drm_i915_private *dev_priv) 1772e3adcf8fSFrançois Tigeot { 1773e3adcf8fSFrançois Tigeot bool ret = dev_priv->mm.interruptible; 1774e3adcf8fSFrançois Tigeot 1775a2fdbec6SFrançois Tigeot if (unlikely(dev_priv->gtt.do_idle_maps)) { 1776e3adcf8fSFrançois Tigeot dev_priv->mm.interruptible = false; 1777b030f26bSFrançois Tigeot if (i915_gpu_idle(dev_priv->dev)) { 1778e3adcf8fSFrançois Tigeot DRM_ERROR("Couldn't idle GPU\n"); 1779e3adcf8fSFrançois Tigeot /* Wait a bit, in hopes it avoids the hang */ 17800b869d8aSFrançois Tigeot udelay(10); 1781e3adcf8fSFrançois Tigeot } 1782e3adcf8fSFrançois Tigeot } 1783e3adcf8fSFrançois Tigeot 1784e3adcf8fSFrançois Tigeot return ret; 1785e3adcf8fSFrançois Tigeot } 1786e3adcf8fSFrançois Tigeot 17877cbd1a46SFrançois Tigeot static void undo_idling(struct drm_i915_private *dev_priv, bool interruptible) 1788e3adcf8fSFrançois Tigeot { 1789a2fdbec6SFrançois Tigeot if (unlikely(dev_priv->gtt.do_idle_maps)) 1790e3adcf8fSFrançois Tigeot dev_priv->mm.interruptible = interruptible; 1791e3adcf8fSFrançois Tigeot } 1792e3adcf8fSFrançois Tigeot 17939edbd4a0SFrançois Tigeot void i915_check_and_clear_faults(struct drm_device *dev) 17949edbd4a0SFrançois Tigeot { 17959edbd4a0SFrançois Tigeot struct drm_i915_private *dev_priv = dev->dev_private; 1796ba55f2f5SFrançois Tigeot struct intel_engine_cs *ring; 17979edbd4a0SFrançois Tigeot int i; 17989edbd4a0SFrançois Tigeot 17999edbd4a0SFrançois Tigeot if (INTEL_INFO(dev)->gen < 6) 18009edbd4a0SFrançois Tigeot return; 18019edbd4a0SFrançois Tigeot 18029edbd4a0SFrançois Tigeot for_each_ring(ring, dev_priv, i) { 18039edbd4a0SFrançois Tigeot u32 fault_reg; 18049edbd4a0SFrançois Tigeot fault_reg = I915_READ(RING_FAULT_REG(ring)); 18059edbd4a0SFrançois Tigeot if (fault_reg & RING_FAULT_VALID) { 18069edbd4a0SFrançois Tigeot #if 0 18079edbd4a0SFrançois Tigeot DRM_DEBUG_DRIVER("Unexpected fault\n" 18082c9916cdSFrançois Tigeot "\tAddr: 0x%08lx\n" 18099edbd4a0SFrançois Tigeot "\tAddress space: %s\n" 18109edbd4a0SFrançois Tigeot "\tSource ID: %d\n" 18119edbd4a0SFrançois Tigeot "\tType: %d\n", 18129edbd4a0SFrançois Tigeot fault_reg & PAGE_MASK, 18139edbd4a0SFrançois Tigeot fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT", 18149edbd4a0SFrançois Tigeot RING_FAULT_SRCID(fault_reg), 18159edbd4a0SFrançois Tigeot RING_FAULT_FAULT_TYPE(fault_reg)); 18169edbd4a0SFrançois Tigeot #endif 18179edbd4a0SFrançois Tigeot I915_WRITE(RING_FAULT_REG(ring), 18189edbd4a0SFrançois Tigeot fault_reg & ~RING_FAULT_VALID); 18199edbd4a0SFrançois Tigeot } 18209edbd4a0SFrançois Tigeot } 18219edbd4a0SFrançois Tigeot POSTING_READ(RING_FAULT_REG(&dev_priv->ring[RCS])); 18229edbd4a0SFrançois Tigeot } 18239edbd4a0SFrançois Tigeot 182424edb884SFrançois Tigeot static void i915_ggtt_flush(struct drm_i915_private *dev_priv) 182524edb884SFrançois Tigeot { 182624edb884SFrançois Tigeot if (INTEL_INFO(dev_priv->dev)->gen < 6) { 182724edb884SFrançois Tigeot intel_gtt_chipset_flush(); 182824edb884SFrançois Tigeot } else { 182924edb884SFrançois Tigeot I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN); 183024edb884SFrançois Tigeot POSTING_READ(GFX_FLSH_CNTL_GEN6); 183124edb884SFrançois Tigeot } 183224edb884SFrançois Tigeot } 183324edb884SFrançois Tigeot 18349edbd4a0SFrançois Tigeot void i915_gem_suspend_gtt_mappings(struct drm_device *dev) 18359edbd4a0SFrançois Tigeot { 18369edbd4a0SFrançois Tigeot struct drm_i915_private *dev_priv = dev->dev_private; 18379edbd4a0SFrançois Tigeot 18389edbd4a0SFrançois Tigeot /* Don't bother messing with faults pre GEN6 as we have little 18399edbd4a0SFrançois Tigeot * documentation supporting that it's a good idea. 18409edbd4a0SFrançois Tigeot */ 18419edbd4a0SFrançois Tigeot if (INTEL_INFO(dev)->gen < 6) 18429edbd4a0SFrançois Tigeot return; 18439edbd4a0SFrançois Tigeot 18449edbd4a0SFrançois Tigeot i915_check_and_clear_faults(dev); 18459edbd4a0SFrançois Tigeot 18469edbd4a0SFrançois Tigeot dev_priv->gtt.base.clear_range(&dev_priv->gtt.base, 1847ba55f2f5SFrançois Tigeot dev_priv->gtt.base.start, 1848ba55f2f5SFrançois Tigeot dev_priv->gtt.base.total, 18499edbd4a0SFrançois Tigeot true); 185024edb884SFrançois Tigeot 185124edb884SFrançois Tigeot i915_ggtt_flush(dev_priv); 18529edbd4a0SFrançois Tigeot } 18539edbd4a0SFrançois Tigeot 18540b869d8aSFrançois Tigeot int i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj) 18550b869d8aSFrançois Tigeot { 1856*7ec9f8e5SFrançois Tigeot if (!dma_map_sg(obj->base.dev->pdev->dev, 18570b869d8aSFrançois Tigeot obj->pages->sgl, obj->pages->nents, 18580b869d8aSFrançois Tigeot PCI_DMA_BIDIRECTIONAL)) 18590b869d8aSFrançois Tigeot return -ENOSPC; 18600b869d8aSFrançois Tigeot 18610b869d8aSFrançois Tigeot return 0; 18620b869d8aSFrançois Tigeot } 18630b869d8aSFrançois Tigeot 186419c468b4SFrançois Tigeot static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte) 1865e3adcf8fSFrançois Tigeot { 18669edbd4a0SFrançois Tigeot #if 0 18679edbd4a0SFrançois Tigeot writeq(pte, addr); 18689edbd4a0SFrançois Tigeot #else 18699edbd4a0SFrançois Tigeot iowrite32((u32)pte, addr); 18709edbd4a0SFrançois Tigeot iowrite32(pte >> 32, addr + 4); 18719edbd4a0SFrançois Tigeot #endif 18727cbd1a46SFrançois Tigeot } 1873e3adcf8fSFrançois Tigeot 18749edbd4a0SFrançois Tigeot static void gen8_ggtt_insert_entries(struct i915_address_space *vm, 1875*7ec9f8e5SFrançois Tigeot struct sg_table *st, 1876ba55f2f5SFrançois Tigeot uint64_t start, 187724edb884SFrançois Tigeot enum i915_cache_level level, u32 unused) 18789edbd4a0SFrançois Tigeot { 18799edbd4a0SFrançois Tigeot struct drm_i915_private *dev_priv = vm->dev->dev_private; 1880ba55f2f5SFrançois Tigeot unsigned first_entry = start >> PAGE_SHIFT; 1881477eb7f9SFrançois Tigeot gen8_pte_t __iomem *gtt_entries = 1882477eb7f9SFrançois Tigeot (gen8_pte_t __iomem *)dev_priv->gtt.gsm + first_entry; 18839edbd4a0SFrançois Tigeot int i = 0; 1884*7ec9f8e5SFrançois Tigeot struct sg_page_iter sg_iter; 1885*7ec9f8e5SFrançois Tigeot dma_addr_t addr = 0; /* shut up gcc */ 18869edbd4a0SFrançois Tigeot 1887*7ec9f8e5SFrançois Tigeot for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) { 1888*7ec9f8e5SFrançois Tigeot addr = sg_dma_address(sg_iter.sg) + 1889*7ec9f8e5SFrançois Tigeot (sg_iter.sg_pgoffset << PAGE_SHIFT); 18909edbd4a0SFrançois Tigeot gen8_set_pte(>t_entries[i], 18919edbd4a0SFrançois Tigeot gen8_pte_encode(addr, level, true)); 1892*7ec9f8e5SFrançois Tigeot i++; 18939edbd4a0SFrançois Tigeot } 18949edbd4a0SFrançois Tigeot 18959edbd4a0SFrançois Tigeot /* 18969edbd4a0SFrançois Tigeot * XXX: This serves as a posting read to make sure that the PTE has 18977cbd1a46SFrançois Tigeot * actually been updated. There is some concern that even though 18987cbd1a46SFrançois Tigeot * registers and PTEs are within the same BAR that they are potentially 18997cbd1a46SFrançois Tigeot * of NUMA access patterns. Therefore, even with the way we assume 19007cbd1a46SFrançois Tigeot * hardware should work, we must keep this posting read for paranoia. 19017cbd1a46SFrançois Tigeot */ 19027cbd1a46SFrançois Tigeot if (i != 0) 19039edbd4a0SFrançois Tigeot WARN_ON(readq(>t_entries[i-1]) 19049edbd4a0SFrançois Tigeot != gen8_pte_encode(addr, level, true)); 19057cbd1a46SFrançois Tigeot 19067cbd1a46SFrançois Tigeot /* This next bit makes the above posting read even more important. We 19077cbd1a46SFrançois Tigeot * want to flush the TLBs only after we're certain all the PTE updates 19087cbd1a46SFrançois Tigeot * have finished. 19097cbd1a46SFrançois Tigeot */ 19107cbd1a46SFrançois Tigeot I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN); 19117cbd1a46SFrançois Tigeot POSTING_READ(GFX_FLSH_CNTL_GEN6); 1912a2fdbec6SFrançois Tigeot } 1913a2fdbec6SFrançois Tigeot 19149edbd4a0SFrançois Tigeot /* 19159edbd4a0SFrançois Tigeot * Binds an object into the global gtt with the specified cache level. The object 19169edbd4a0SFrançois Tigeot * will be accessible to the GPU via commands whose operands reference offsets 19179edbd4a0SFrançois Tigeot * within the global GTT as well as accessible by the GPU through the GMADR 19189edbd4a0SFrançois Tigeot * mapped BAR (dev_priv->mm.gtt->gtt). 19199edbd4a0SFrançois Tigeot */ 19209edbd4a0SFrançois Tigeot static void gen6_ggtt_insert_entries(struct i915_address_space *vm, 1921*7ec9f8e5SFrançois Tigeot struct sg_table *st, 1922ba55f2f5SFrançois Tigeot uint64_t start, 192324edb884SFrançois Tigeot enum i915_cache_level level, u32 flags) 1924a2fdbec6SFrançois Tigeot { 19259edbd4a0SFrançois Tigeot struct drm_i915_private *dev_priv = vm->dev->dev_private; 1926ba55f2f5SFrançois Tigeot unsigned first_entry = start >> PAGE_SHIFT; 1927477eb7f9SFrançois Tigeot gen6_pte_t __iomem *gtt_entries = 1928477eb7f9SFrançois Tigeot (gen6_pte_t __iomem *)dev_priv->gtt.gsm + first_entry; 19299edbd4a0SFrançois Tigeot int i = 0; 1930*7ec9f8e5SFrançois Tigeot struct sg_page_iter sg_iter; 1931*7ec9f8e5SFrançois Tigeot dma_addr_t addr = 0; 19329edbd4a0SFrançois Tigeot 1933*7ec9f8e5SFrançois Tigeot for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) { 1934*7ec9f8e5SFrançois Tigeot addr = sg_page_iter_dma_address(&sg_iter); 193524edb884SFrançois Tigeot iowrite32(vm->pte_encode(addr, level, true, flags), >t_entries[i]); 1936*7ec9f8e5SFrançois Tigeot i++; 19379edbd4a0SFrançois Tigeot } 19389edbd4a0SFrançois Tigeot 19399edbd4a0SFrançois Tigeot /* XXX: This serves as a posting read to make sure that the PTE has 19409edbd4a0SFrançois Tigeot * actually been updated. There is some concern that even though 19419edbd4a0SFrançois Tigeot * registers and PTEs are within the same BAR that they are potentially 19429edbd4a0SFrançois Tigeot * of NUMA access patterns. Therefore, even with the way we assume 19439edbd4a0SFrançois Tigeot * hardware should work, we must keep this posting read for paranoia. 19449edbd4a0SFrançois Tigeot */ 194524edb884SFrançois Tigeot if (i != 0) { 194624edb884SFrançois Tigeot unsigned long gtt = readl(>t_entries[i-1]); 194724edb884SFrançois Tigeot WARN_ON(gtt != vm->pte_encode(addr, level, true, flags)); 194824edb884SFrançois Tigeot } 19499edbd4a0SFrançois Tigeot 19509edbd4a0SFrançois Tigeot /* This next bit makes the above posting read even more important. We 19519edbd4a0SFrançois Tigeot * want to flush the TLBs only after we're certain all the PTE updates 19529edbd4a0SFrançois Tigeot * have finished. 19539edbd4a0SFrançois Tigeot */ 19549edbd4a0SFrançois Tigeot I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN); 19559edbd4a0SFrançois Tigeot POSTING_READ(GFX_FLSH_CNTL_GEN6); 19569edbd4a0SFrançois Tigeot } 19579edbd4a0SFrançois Tigeot 19589edbd4a0SFrançois Tigeot static void gen8_ggtt_clear_range(struct i915_address_space *vm, 1959ba55f2f5SFrançois Tigeot uint64_t start, 1960ba55f2f5SFrançois Tigeot uint64_t length, 19619edbd4a0SFrançois Tigeot bool use_scratch) 19629edbd4a0SFrançois Tigeot { 19639edbd4a0SFrançois Tigeot struct drm_i915_private *dev_priv = vm->dev->dev_private; 1964ba55f2f5SFrançois Tigeot unsigned first_entry = start >> PAGE_SHIFT; 1965ba55f2f5SFrançois Tigeot unsigned num_entries = length >> PAGE_SHIFT; 1966477eb7f9SFrançois Tigeot gen8_pte_t scratch_pte, __iomem *gtt_base = 1967477eb7f9SFrançois Tigeot (gen8_pte_t __iomem *) dev_priv->gtt.gsm + first_entry; 19689edbd4a0SFrançois Tigeot const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry; 19699edbd4a0SFrançois Tigeot int i; 19709edbd4a0SFrançois Tigeot 19719edbd4a0SFrançois Tigeot if (WARN(num_entries > max_entries, 19729edbd4a0SFrançois Tigeot "First entry = %d; Num entries = %d (max=%d)\n", 19739edbd4a0SFrançois Tigeot first_entry, num_entries, max_entries)) 19749edbd4a0SFrançois Tigeot num_entries = max_entries; 19759edbd4a0SFrançois Tigeot 19769edbd4a0SFrançois Tigeot scratch_pte = gen8_pte_encode(vm->scratch.addr, 19779edbd4a0SFrançois Tigeot I915_CACHE_LLC, 19789edbd4a0SFrançois Tigeot use_scratch); 19799edbd4a0SFrançois Tigeot for (i = 0; i < num_entries; i++) 19809edbd4a0SFrançois Tigeot gen8_set_pte(>t_base[i], scratch_pte); 19819edbd4a0SFrançois Tigeot readl(gtt_base); 19829edbd4a0SFrançois Tigeot } 19839edbd4a0SFrançois Tigeot 19849edbd4a0SFrançois Tigeot static void gen6_ggtt_clear_range(struct i915_address_space *vm, 1985ba55f2f5SFrançois Tigeot uint64_t start, 1986ba55f2f5SFrançois Tigeot uint64_t length, 19879edbd4a0SFrançois Tigeot bool use_scratch) 19889edbd4a0SFrançois Tigeot { 19899edbd4a0SFrançois Tigeot struct drm_i915_private *dev_priv = vm->dev->dev_private; 1990ba55f2f5SFrançois Tigeot unsigned first_entry = start >> PAGE_SHIFT; 1991ba55f2f5SFrançois Tigeot unsigned num_entries = length >> PAGE_SHIFT; 1992477eb7f9SFrançois Tigeot gen6_pte_t scratch_pte, __iomem *gtt_base = 1993477eb7f9SFrançois Tigeot (gen6_pte_t __iomem *) dev_priv->gtt.gsm + first_entry; 19948e26cdf6SFrançois Tigeot const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry; 1995a2fdbec6SFrançois Tigeot int i; 1996a2fdbec6SFrançois Tigeot 1997a2fdbec6SFrançois Tigeot if (WARN(num_entries > max_entries, 1998a2fdbec6SFrançois Tigeot "First entry = %d; Num entries = %d (max=%d)\n", 1999a2fdbec6SFrançois Tigeot first_entry, num_entries, max_entries)) 2000a2fdbec6SFrançois Tigeot num_entries = max_entries; 2001a2fdbec6SFrançois Tigeot 200224edb884SFrançois Tigeot scratch_pte = vm->pte_encode(vm->scratch.addr, I915_CACHE_LLC, use_scratch, 0); 20039edbd4a0SFrançois Tigeot 2004a2fdbec6SFrançois Tigeot for (i = 0; i < num_entries; i++) 2005a2fdbec6SFrançois Tigeot iowrite32(scratch_pte, >t_base[i]); 2006a2fdbec6SFrançois Tigeot readl(gtt_base); 2007a2fdbec6SFrançois Tigeot } 2008a2fdbec6SFrançois Tigeot 2009*7ec9f8e5SFrançois Tigeot static void i915_ggtt_insert_entries(struct i915_address_space *vm, 2010*7ec9f8e5SFrançois Tigeot struct sg_table *pages, 2011*7ec9f8e5SFrançois Tigeot uint64_t start, 2012*7ec9f8e5SFrançois Tigeot enum i915_cache_level cache_level, u32 unused) 2013a2fdbec6SFrançois Tigeot { 2014a2fdbec6SFrançois Tigeot unsigned int flags = (cache_level == I915_CACHE_NONE) ? 2015a2fdbec6SFrançois Tigeot AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY; 2016a2fdbec6SFrançois Tigeot 2017*7ec9f8e5SFrançois Tigeot intel_gtt_insert_sg_entries(pages, start >> PAGE_SHIFT, flags); 2018a2fdbec6SFrançois Tigeot } 2019a2fdbec6SFrançois Tigeot 20209edbd4a0SFrançois Tigeot static void i915_ggtt_clear_range(struct i915_address_space *vm, 2021ba55f2f5SFrançois Tigeot uint64_t start, 2022ba55f2f5SFrançois Tigeot uint64_t length, 20239edbd4a0SFrançois Tigeot bool unused) 2024a2fdbec6SFrançois Tigeot { 2025ba55f2f5SFrançois Tigeot unsigned first_entry = start >> PAGE_SHIFT; 2026ba55f2f5SFrançois Tigeot unsigned num_entries = length >> PAGE_SHIFT; 2027a2fdbec6SFrançois Tigeot intel_gtt_clear_range(first_entry, num_entries); 2028a2fdbec6SFrançois Tigeot } 20297cbd1a46SFrançois Tigeot 203019c468b4SFrançois Tigeot static int ggtt_bind_vma(struct i915_vma *vma, 2031ba55f2f5SFrançois Tigeot enum i915_cache_level cache_level, 2032ba55f2f5SFrançois Tigeot u32 flags) 2033e3adcf8fSFrançois Tigeot { 2034ba55f2f5SFrançois Tigeot struct drm_device *dev = vma->vm->dev; 2035e3adcf8fSFrançois Tigeot struct drm_i915_private *dev_priv = dev->dev_private; 2036ba55f2f5SFrançois Tigeot struct drm_i915_gem_object *obj = vma->obj; 2037*7ec9f8e5SFrançois Tigeot struct sg_table *pages = obj->pages; 203819c468b4SFrançois Tigeot u32 pte_flags = 0; 2039*7ec9f8e5SFrançois Tigeot int ret; 2040*7ec9f8e5SFrançois Tigeot 2041*7ec9f8e5SFrançois Tigeot ret = i915_get_ggtt_vma_pages(vma); 2042*7ec9f8e5SFrançois Tigeot if (ret) 2043*7ec9f8e5SFrançois Tigeot return ret; 2044*7ec9f8e5SFrançois Tigeot pages = vma->ggtt_view.pages; 2045e3adcf8fSFrançois Tigeot 204624edb884SFrançois Tigeot /* Currently applicable only to VLV */ 204724edb884SFrançois Tigeot if (obj->gt_ro) 204819c468b4SFrançois Tigeot pte_flags |= PTE_READ_ONLY; 204924edb884SFrançois Tigeot 2050477eb7f9SFrançois Tigeot 2051ba55f2f5SFrançois Tigeot if (!dev_priv->mm.aliasing_ppgtt || flags & GLOBAL_BIND) { 2052477eb7f9SFrançois Tigeot vma->vm->insert_entries(vma->vm, pages, 2053ba55f2f5SFrançois Tigeot vma->node.start, 205419c468b4SFrançois Tigeot cache_level, pte_flags); 205519c468b4SFrançois Tigeot 205619c468b4SFrançois Tigeot /* Note the inconsistency here is due to absence of the 205719c468b4SFrançois Tigeot * aliasing ppgtt on gen4 and earlier. Though we always 205819c468b4SFrançois Tigeot * request PIN_USER for execbuffer (translated to LOCAL_BIND), 205919c468b4SFrançois Tigeot * without the appgtt, we cannot honour that request and so 206019c468b4SFrançois Tigeot * must substitute it with a global binding. Since we do this 206119c468b4SFrançois Tigeot * behind the upper layers back, we need to explicitly set 206219c468b4SFrançois Tigeot * the bound flag ourselves. 206319c468b4SFrançois Tigeot */ 20642c9916cdSFrançois Tigeot vma->bound |= GLOBAL_BIND; 206519c468b4SFrançois Tigeot 2066ba55f2f5SFrançois Tigeot } 2067e3adcf8fSFrançois Tigeot 206819c468b4SFrançois Tigeot if (dev_priv->mm.aliasing_ppgtt && flags & LOCAL_BIND) { 2069ba55f2f5SFrançois Tigeot struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt; 2070477eb7f9SFrançois Tigeot appgtt->base.insert_entries(&appgtt->base, pages, 2071ba55f2f5SFrançois Tigeot vma->node.start, 207219c468b4SFrançois Tigeot cache_level, pte_flags); 2073ba55f2f5SFrançois Tigeot } 207419c468b4SFrançois Tigeot 207519c468b4SFrançois Tigeot return 0; 2076ba55f2f5SFrançois Tigeot } 2077ba55f2f5SFrançois Tigeot 2078ba55f2f5SFrançois Tigeot static void ggtt_unbind_vma(struct i915_vma *vma) 2079ba55f2f5SFrançois Tigeot { 2080ba55f2f5SFrançois Tigeot struct drm_device *dev = vma->vm->dev; 2081ba55f2f5SFrançois Tigeot struct drm_i915_private *dev_priv = dev->dev_private; 2082ba55f2f5SFrançois Tigeot struct drm_i915_gem_object *obj = vma->obj; 208319c468b4SFrançois Tigeot const uint64_t size = min_t(uint64_t, 208419c468b4SFrançois Tigeot obj->base.size, 208519c468b4SFrançois Tigeot vma->node.size); 2086ba55f2f5SFrançois Tigeot 20872c9916cdSFrançois Tigeot if (vma->bound & GLOBAL_BIND) { 2088ba55f2f5SFrançois Tigeot vma->vm->clear_range(vma->vm, 2089ba55f2f5SFrançois Tigeot vma->node.start, 209019c468b4SFrançois Tigeot size, 2091ba55f2f5SFrançois Tigeot true); 2092f4e1c372SFrançois Tigeot } 2093f192107fSFrançois Tigeot 209419c468b4SFrançois Tigeot if (dev_priv->mm.aliasing_ppgtt && vma->bound & LOCAL_BIND) { 2095ba55f2f5SFrançois Tigeot struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt; 209619c468b4SFrançois Tigeot 2097ba55f2f5SFrançois Tigeot appgtt->base.clear_range(&appgtt->base, 2098ba55f2f5SFrançois Tigeot vma->node.start, 209919c468b4SFrançois Tigeot size, 2100ba55f2f5SFrançois Tigeot true); 2101ba55f2f5SFrançois Tigeot } 2102ba55f2f5SFrançois Tigeot } 2103ba55f2f5SFrançois Tigeot 2104f192107fSFrançois Tigeot void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj) 2105f192107fSFrançois Tigeot { 2106f192107fSFrançois Tigeot struct drm_device *dev = obj->base.dev; 2107f192107fSFrançois Tigeot struct drm_i915_private *dev_priv = dev->dev_private; 2108f192107fSFrançois Tigeot bool interruptible; 2109f192107fSFrançois Tigeot 2110f192107fSFrançois Tigeot interruptible = do_idling(dev_priv); 2111f192107fSFrançois Tigeot 2112*7ec9f8e5SFrançois Tigeot dma_unmap_sg(dev->pdev->dev, obj->pages->sgl, obj->pages->nents, 2113f192107fSFrançois Tigeot PCI_DMA_BIDIRECTIONAL); 2114f192107fSFrançois Tigeot 2115f192107fSFrançois Tigeot undo_idling(dev_priv, interruptible); 2116f192107fSFrançois Tigeot } 2117d1c259eeSFrançois Tigeot 2118d1c259eeSFrançois Tigeot static void i915_gtt_color_adjust(struct drm_mm_node *node, 2119d1c259eeSFrançois Tigeot unsigned long color, 21202c9916cdSFrançois Tigeot u64 *start, 21212c9916cdSFrançois Tigeot u64 *end) 2122d1c259eeSFrançois Tigeot { 2123d1c259eeSFrançois Tigeot if (node->color != color) 2124d1c259eeSFrançois Tigeot *start += 4096; 2125d1c259eeSFrançois Tigeot 2126d1c259eeSFrançois Tigeot if (!list_empty(&node->node_list)) { 2127d1c259eeSFrançois Tigeot node = list_entry(node->node_list.next, 2128d1c259eeSFrançois Tigeot struct drm_mm_node, 2129d1c259eeSFrançois Tigeot node_list); 2130d1c259eeSFrançois Tigeot if (node->allocated && node->color != color) 2131d1c259eeSFrançois Tigeot *end -= 4096; 2132d1c259eeSFrançois Tigeot } 2133d1c259eeSFrançois Tigeot } 21349edbd4a0SFrançois Tigeot 21352c9916cdSFrançois Tigeot static int i915_gem_setup_global_gtt(struct drm_device *dev, 2136d1c259eeSFrançois Tigeot unsigned long start, 2137d1c259eeSFrançois Tigeot unsigned long mappable_end, 2138d1c259eeSFrançois Tigeot unsigned long end) 2139d1c259eeSFrançois Tigeot { 2140a2fdbec6SFrançois Tigeot /* Let GEM Manage all of the aperture. 2141a2fdbec6SFrançois Tigeot * 2142a2fdbec6SFrançois Tigeot * However, leave one page at the end still bound to the scratch page. 2143a2fdbec6SFrançois Tigeot * There are a number of places where the hardware apparently prefetches 2144a2fdbec6SFrançois Tigeot * past the end of the object, and we've seen multiple hangs with the 2145a2fdbec6SFrançois Tigeot * GPU head pointer stuck in a batchbuffer bound at the last page of the 2146a2fdbec6SFrançois Tigeot * aperture. One page should be enough to keep any prefetching inside 2147a2fdbec6SFrançois Tigeot * of the aperture. 2148a2fdbec6SFrançois Tigeot */ 21499edbd4a0SFrançois Tigeot struct drm_i915_private *dev_priv = dev->dev_private; 21509edbd4a0SFrançois Tigeot struct i915_address_space *ggtt_vm = &dev_priv->gtt.base; 2151e084c92dSFrançois Tigeot unsigned long mappable; 2152e084c92dSFrançois Tigeot int error; 21539edbd4a0SFrançois Tigeot struct drm_mm_node *entry; 21549edbd4a0SFrançois Tigeot struct drm_i915_gem_object *obj; 21559edbd4a0SFrançois Tigeot unsigned long hole_start, hole_end; 21561b13d190SFrançois Tigeot int ret; 2157e084c92dSFrançois Tigeot 2158e084c92dSFrançois Tigeot mappable = min(end, mappable_end) - start; 2159*7ec9f8e5SFrançois Tigeot BUG_ON(mappable_end > end); 2160e084c92dSFrançois Tigeot 21619edbd4a0SFrançois Tigeot /* Subtract the guard page ... */ 21629edbd4a0SFrançois Tigeot drm_mm_init(&ggtt_vm->mm, start, end - start - PAGE_SIZE); 2163477eb7f9SFrançois Tigeot 2164477eb7f9SFrançois Tigeot dev_priv->gtt.base.start = start; 2165477eb7f9SFrançois Tigeot dev_priv->gtt.base.total = end - start; 2166477eb7f9SFrançois Tigeot 2167477eb7f9SFrançois Tigeot if (intel_vgpu_active(dev)) { 2168477eb7f9SFrançois Tigeot ret = intel_vgt_balloon(dev); 2169477eb7f9SFrançois Tigeot if (ret) 2170477eb7f9SFrançois Tigeot return ret; 2171477eb7f9SFrançois Tigeot } 2172477eb7f9SFrançois Tigeot 2173d1c259eeSFrançois Tigeot if (!HAS_LLC(dev)) 21749edbd4a0SFrançois Tigeot dev_priv->gtt.base.mm.color_adjust = i915_gtt_color_adjust; 2175d1c259eeSFrançois Tigeot 21769edbd4a0SFrançois Tigeot /* Mark any preallocated objects as occupied */ 21779edbd4a0SFrançois Tigeot list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) { 21789edbd4a0SFrançois Tigeot struct i915_vma *vma = i915_gem_obj_to_vma(obj, ggtt_vm); 21791b13d190SFrançois Tigeot 21809edbd4a0SFrançois Tigeot DRM_DEBUG_KMS("reserving preallocated space: %lx + %zx\n", 21819edbd4a0SFrançois Tigeot i915_gem_obj_ggtt_offset(obj), obj->base.size); 2182d1c259eeSFrançois Tigeot 21839edbd4a0SFrançois Tigeot WARN_ON(i915_gem_obj_ggtt_bound(obj)); 21849edbd4a0SFrançois Tigeot ret = drm_mm_reserve_node(&ggtt_vm->mm, &vma->node); 21851b13d190SFrançois Tigeot if (ret) { 21861b13d190SFrançois Tigeot DRM_DEBUG_KMS("Reservation failed: %i\n", ret); 21871b13d190SFrançois Tigeot return ret; 21881b13d190SFrançois Tigeot } 21892c9916cdSFrançois Tigeot vma->bound |= GLOBAL_BIND; 21909edbd4a0SFrançois Tigeot } 21919edbd4a0SFrançois Tigeot 21929edbd4a0SFrançois Tigeot /* Clear any non-preallocated blocks */ 21939edbd4a0SFrançois Tigeot drm_mm_for_each_hole(entry, &ggtt_vm->mm, hole_start, hole_end) { 21949edbd4a0SFrançois Tigeot DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n", 21959edbd4a0SFrançois Tigeot hole_start, hole_end); 2196ba55f2f5SFrançois Tigeot ggtt_vm->clear_range(ggtt_vm, hole_start, 2197ba55f2f5SFrançois Tigeot hole_end - hole_start, true); 21989edbd4a0SFrançois Tigeot } 2199cb170299SFrançois Tigeot 2200310880c6SSascha Wildner #ifdef __DragonFly__ 2201e084c92dSFrançois Tigeot device_printf(dev->dev, 2202e084c92dSFrançois Tigeot "taking over the fictitious range 0x%lx-0x%lx\n", 2203cb170299SFrançois Tigeot dev_priv->gtt.mappable_base + start, dev_priv->gtt.mappable_base + start + mappable); 2204cb170299SFrançois Tigeot error = -vm_phys_fictitious_reg_range(dev_priv->gtt.mappable_base + start, 2205cb170299SFrançois Tigeot dev_priv->gtt.mappable_base + start + mappable, VM_MEMATTR_WRITE_COMBINING); 2206310880c6SSascha Wildner #endif 22079edbd4a0SFrançois Tigeot 22089edbd4a0SFrançois Tigeot /* And finally clear the reserved guard page */ 2209ba55f2f5SFrançois Tigeot ggtt_vm->clear_range(ggtt_vm, end - PAGE_SIZE, PAGE_SIZE, true); 22101b13d190SFrançois Tigeot 22111b13d190SFrançois Tigeot if (USES_PPGTT(dev) && !USES_FULL_PPGTT(dev)) { 22121b13d190SFrançois Tigeot struct i915_hw_ppgtt *ppgtt; 22131b13d190SFrançois Tigeot 22141b13d190SFrançois Tigeot ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL); 22151b13d190SFrançois Tigeot if (!ppgtt) 22161b13d190SFrançois Tigeot return -ENOMEM; 22171b13d190SFrançois Tigeot 2218477eb7f9SFrançois Tigeot ret = __hw_ppgtt_init(dev, ppgtt, true); 2219477eb7f9SFrançois Tigeot if (ret) { 222019c468b4SFrançois Tigeot ppgtt->base.cleanup(&ppgtt->base); 2221477eb7f9SFrançois Tigeot kfree(ppgtt); 22221b13d190SFrançois Tigeot return ret; 2223477eb7f9SFrançois Tigeot } 22241b13d190SFrançois Tigeot 2225*7ec9f8e5SFrançois Tigeot if (ppgtt->base.allocate_va_range) 2226*7ec9f8e5SFrançois Tigeot ret = ppgtt->base.allocate_va_range(&ppgtt->base, 0, 2227*7ec9f8e5SFrançois Tigeot ppgtt->base.total); 2228*7ec9f8e5SFrançois Tigeot if (ret) { 2229*7ec9f8e5SFrançois Tigeot ppgtt->base.cleanup(&ppgtt->base); 2230*7ec9f8e5SFrançois Tigeot kfree(ppgtt); 2231*7ec9f8e5SFrançois Tigeot return ret; 2232*7ec9f8e5SFrançois Tigeot } 2233*7ec9f8e5SFrançois Tigeot 2234*7ec9f8e5SFrançois Tigeot ppgtt->base.clear_range(&ppgtt->base, 2235*7ec9f8e5SFrançois Tigeot ppgtt->base.start, 2236*7ec9f8e5SFrançois Tigeot ppgtt->base.total, 2237*7ec9f8e5SFrançois Tigeot true); 2238*7ec9f8e5SFrançois Tigeot 22391b13d190SFrançois Tigeot dev_priv->mm.aliasing_ppgtt = ppgtt; 22401b13d190SFrançois Tigeot } 22411b13d190SFrançois Tigeot 22421b13d190SFrançois Tigeot return 0; 2243a2fdbec6SFrançois Tigeot } 2244a2fdbec6SFrançois Tigeot 2245a2fdbec6SFrançois Tigeot void i915_gem_init_global_gtt(struct drm_device *dev) 2246a2fdbec6SFrançois Tigeot { 2247a2fdbec6SFrançois Tigeot struct drm_i915_private *dev_priv = dev->dev_private; 2248a2fdbec6SFrançois Tigeot unsigned long gtt_size, mappable_size; 2249a2fdbec6SFrançois Tigeot 22509edbd4a0SFrançois Tigeot gtt_size = dev_priv->gtt.base.total; 22519edbd4a0SFrançois Tigeot mappable_size = dev_priv->gtt.mappable_end; 2252a2fdbec6SFrançois Tigeot 2253a2fdbec6SFrançois Tigeot i915_gem_setup_global_gtt(dev, 0, mappable_size, gtt_size); 2254a2fdbec6SFrançois Tigeot } 2255a2fdbec6SFrançois Tigeot 22561b13d190SFrançois Tigeot void i915_global_gtt_cleanup(struct drm_device *dev) 22571b13d190SFrançois Tigeot { 22581b13d190SFrançois Tigeot struct drm_i915_private *dev_priv = dev->dev_private; 22591b13d190SFrançois Tigeot struct i915_address_space *vm = &dev_priv->gtt.base; 22601b13d190SFrançois Tigeot 22611b13d190SFrançois Tigeot if (dev_priv->mm.aliasing_ppgtt) { 22621b13d190SFrançois Tigeot struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt; 22631b13d190SFrançois Tigeot 22641b13d190SFrançois Tigeot ppgtt->base.cleanup(&ppgtt->base); 22651b13d190SFrançois Tigeot } 22661b13d190SFrançois Tigeot 22671b13d190SFrançois Tigeot if (drm_mm_initialized(&vm->mm)) { 2268477eb7f9SFrançois Tigeot if (intel_vgpu_active(dev)) 2269477eb7f9SFrançois Tigeot intel_vgt_deballoon(); 2270477eb7f9SFrançois Tigeot 22711b13d190SFrançois Tigeot drm_mm_takedown(&vm->mm); 22721b13d190SFrançois Tigeot list_del(&vm->global_link); 22731b13d190SFrançois Tigeot } 22741b13d190SFrançois Tigeot 22751b13d190SFrançois Tigeot vm->cleanup(vm); 22761b13d190SFrançois Tigeot } 22771b13d190SFrançois Tigeot 22789edbd4a0SFrançois Tigeot static int setup_scratch_page(struct drm_device *dev) 22799edbd4a0SFrançois Tigeot { 22809edbd4a0SFrançois Tigeot struct drm_i915_private *dev_priv = dev->dev_private; 22819edbd4a0SFrançois Tigeot struct vm_page *page; 22829edbd4a0SFrançois Tigeot dma_addr_t dma_addr; 22839edbd4a0SFrançois Tigeot 22849edbd4a0SFrançois Tigeot page = alloc_page(GFP_KERNEL | GFP_DMA32 | __GFP_ZERO); 22859edbd4a0SFrançois Tigeot if (page == NULL) 22869edbd4a0SFrançois Tigeot return -ENOMEM; 22879edbd4a0SFrançois Tigeot set_pages_uc(page, 1); 22889edbd4a0SFrançois Tigeot 22899edbd4a0SFrançois Tigeot #ifdef CONFIG_INTEL_IOMMU 22909edbd4a0SFrançois Tigeot dma_addr = pci_map_page(dev->pdev, page, 0, PAGE_SIZE, 22919edbd4a0SFrançois Tigeot PCI_DMA_BIDIRECTIONAL); 22929edbd4a0SFrançois Tigeot if (pci_dma_mapping_error(dev->pdev, dma_addr)) 22939edbd4a0SFrançois Tigeot return -EINVAL; 22949edbd4a0SFrançois Tigeot #else 22959edbd4a0SFrançois Tigeot dma_addr = page_to_phys(page); 22969edbd4a0SFrançois Tigeot #endif 22979edbd4a0SFrançois Tigeot dev_priv->gtt.base.scratch.page = page; 22989edbd4a0SFrançois Tigeot dev_priv->gtt.base.scratch.addr = dma_addr; 22999edbd4a0SFrançois Tigeot 23009edbd4a0SFrançois Tigeot return 0; 23019edbd4a0SFrançois Tigeot } 23029edbd4a0SFrançois Tigeot 23039edbd4a0SFrançois Tigeot static void teardown_scratch_page(struct drm_device *dev) 23049edbd4a0SFrançois Tigeot { 23059edbd4a0SFrançois Tigeot struct drm_i915_private *dev_priv = dev->dev_private; 2306ba55f2f5SFrançois Tigeot struct vm_page *page = dev_priv->gtt.base.scratch.page; 23079edbd4a0SFrançois Tigeot 23089edbd4a0SFrançois Tigeot set_pages_wb(page, 1); 23099edbd4a0SFrançois Tigeot pci_unmap_page(dev->pdev, dev_priv->gtt.base.scratch.addr, 23109edbd4a0SFrançois Tigeot PAGE_SIZE, PCI_DMA_BIDIRECTIONAL); 23119edbd4a0SFrançois Tigeot __free_page(page); 23129edbd4a0SFrançois Tigeot } 23139edbd4a0SFrançois Tigeot 231419c468b4SFrançois Tigeot static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl) 23159edbd4a0SFrançois Tigeot { 23169edbd4a0SFrançois Tigeot snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT; 23179edbd4a0SFrançois Tigeot snb_gmch_ctl &= SNB_GMCH_GGMS_MASK; 23189edbd4a0SFrançois Tigeot return snb_gmch_ctl << 20; 23199edbd4a0SFrançois Tigeot } 23209edbd4a0SFrançois Tigeot 232119c468b4SFrançois Tigeot static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl) 23229edbd4a0SFrançois Tigeot { 23239edbd4a0SFrançois Tigeot bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT; 23249edbd4a0SFrançois Tigeot bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK; 23259edbd4a0SFrançois Tigeot if (bdw_gmch_ctl) 23269edbd4a0SFrançois Tigeot bdw_gmch_ctl = 1 << bdw_gmch_ctl; 2327ba55f2f5SFrançois Tigeot 2328ba55f2f5SFrançois Tigeot #ifdef CONFIG_X86_32 2329ba55f2f5SFrançois Tigeot /* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */ 2330ba55f2f5SFrançois Tigeot if (bdw_gmch_ctl > 4) 2331ba55f2f5SFrançois Tigeot bdw_gmch_ctl = 4; 2332ba55f2f5SFrançois Tigeot #endif 23339edbd4a0SFrançois Tigeot 23349edbd4a0SFrançois Tigeot return bdw_gmch_ctl << 20; 23359edbd4a0SFrançois Tigeot } 23369edbd4a0SFrançois Tigeot 233719c468b4SFrançois Tigeot static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl) 2338ba55f2f5SFrançois Tigeot { 2339ba55f2f5SFrançois Tigeot gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT; 2340ba55f2f5SFrançois Tigeot gmch_ctrl &= SNB_GMCH_GGMS_MASK; 2341ba55f2f5SFrançois Tigeot 2342ba55f2f5SFrançois Tigeot if (gmch_ctrl) 2343ba55f2f5SFrançois Tigeot return 1 << (20 + gmch_ctrl); 2344ba55f2f5SFrançois Tigeot 2345ba55f2f5SFrançois Tigeot return 0; 2346ba55f2f5SFrançois Tigeot } 2347ba55f2f5SFrançois Tigeot 234819c468b4SFrançois Tigeot static size_t gen6_get_stolen_size(u16 snb_gmch_ctl) 23499edbd4a0SFrançois Tigeot { 23509edbd4a0SFrançois Tigeot snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT; 23519edbd4a0SFrançois Tigeot snb_gmch_ctl &= SNB_GMCH_GMS_MASK; 23529edbd4a0SFrançois Tigeot return snb_gmch_ctl << 25; /* 32 MB units */ 23539edbd4a0SFrançois Tigeot } 23549edbd4a0SFrançois Tigeot 235519c468b4SFrançois Tigeot static size_t gen8_get_stolen_size(u16 bdw_gmch_ctl) 23569edbd4a0SFrançois Tigeot { 23579edbd4a0SFrançois Tigeot bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT; 23589edbd4a0SFrançois Tigeot bdw_gmch_ctl &= BDW_GMCH_GMS_MASK; 23599edbd4a0SFrançois Tigeot return bdw_gmch_ctl << 25; /* 32 MB units */ 23609edbd4a0SFrançois Tigeot } 23619edbd4a0SFrançois Tigeot 2362ba55f2f5SFrançois Tigeot static size_t chv_get_stolen_size(u16 gmch_ctrl) 2363ba55f2f5SFrançois Tigeot { 2364ba55f2f5SFrançois Tigeot gmch_ctrl >>= SNB_GMCH_GMS_SHIFT; 2365ba55f2f5SFrançois Tigeot gmch_ctrl &= SNB_GMCH_GMS_MASK; 2366ba55f2f5SFrançois Tigeot 2367ba55f2f5SFrançois Tigeot /* 2368ba55f2f5SFrançois Tigeot * 0x0 to 0x10: 32MB increments starting at 0MB 2369ba55f2f5SFrançois Tigeot * 0x11 to 0x16: 4MB increments starting at 8MB 2370ba55f2f5SFrançois Tigeot * 0x17 to 0x1d: 4MB increments start at 36MB 2371ba55f2f5SFrançois Tigeot */ 2372ba55f2f5SFrançois Tigeot if (gmch_ctrl < 0x11) 2373ba55f2f5SFrançois Tigeot return gmch_ctrl << 25; 2374ba55f2f5SFrançois Tigeot else if (gmch_ctrl < 0x17) 2375ba55f2f5SFrançois Tigeot return (gmch_ctrl - 0x11 + 2) << 22; 2376ba55f2f5SFrançois Tigeot else 2377ba55f2f5SFrançois Tigeot return (gmch_ctrl - 0x17 + 9) << 22; 2378ba55f2f5SFrançois Tigeot } 2379ba55f2f5SFrançois Tigeot 23802c9916cdSFrançois Tigeot static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl) 23812c9916cdSFrançois Tigeot { 23822c9916cdSFrançois Tigeot gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT; 23832c9916cdSFrançois Tigeot gen9_gmch_ctl &= BDW_GMCH_GMS_MASK; 23842c9916cdSFrançois Tigeot 23852c9916cdSFrançois Tigeot if (gen9_gmch_ctl < 0xf0) 23862c9916cdSFrançois Tigeot return gen9_gmch_ctl << 25; /* 32 MB units */ 23872c9916cdSFrançois Tigeot else 23882c9916cdSFrançois Tigeot /* 4MB increments starting at 0xf0 for 4MB */ 23892c9916cdSFrançois Tigeot return (gen9_gmch_ctl - 0xf0 + 1) << 22; 23902c9916cdSFrançois Tigeot } 23912c9916cdSFrançois Tigeot 23929edbd4a0SFrançois Tigeot static int ggtt_probe_common(struct drm_device *dev, 23939edbd4a0SFrançois Tigeot size_t gtt_size) 23949edbd4a0SFrançois Tigeot { 23959edbd4a0SFrançois Tigeot struct drm_i915_private *dev_priv = dev->dev_private; 23969edbd4a0SFrançois Tigeot phys_addr_t gtt_phys_addr; 23979edbd4a0SFrançois Tigeot int ret; 23989edbd4a0SFrançois Tigeot 23999edbd4a0SFrançois Tigeot /* For Modern GENs the PTEs and register space are split in the BAR */ 24009edbd4a0SFrançois Tigeot gtt_phys_addr = pci_resource_start(dev->pdev, 0) + 24019edbd4a0SFrançois Tigeot (pci_resource_len(dev->pdev, 0) / 2); 24029edbd4a0SFrançois Tigeot 240319c468b4SFrançois Tigeot /* 240419c468b4SFrançois Tigeot * On BXT writes larger than 64 bit to the GTT pagetable range will be 240519c468b4SFrançois Tigeot * dropped. For WC mappings in general we have 64 byte burst writes 240619c468b4SFrançois Tigeot * when the WC buffer is flushed, so we can't use it, but have to 240719c468b4SFrançois Tigeot * resort to an uncached mapping. The WC issue is easily caught by the 240819c468b4SFrançois Tigeot * readback check when writing GTT PTE entries. 240919c468b4SFrançois Tigeot */ 241019c468b4SFrançois Tigeot if (IS_BROXTON(dev)) 241119c468b4SFrançois Tigeot dev_priv->gtt.gsm = ioremap_nocache(gtt_phys_addr, gtt_size); 241219c468b4SFrançois Tigeot else 24139edbd4a0SFrançois Tigeot dev_priv->gtt.gsm = ioremap_wc(gtt_phys_addr, gtt_size); 24149edbd4a0SFrançois Tigeot if (!dev_priv->gtt.gsm) { 24159edbd4a0SFrançois Tigeot DRM_ERROR("Failed to map the gtt page table\n"); 24169edbd4a0SFrançois Tigeot return -ENOMEM; 24179edbd4a0SFrançois Tigeot } 24189edbd4a0SFrançois Tigeot 24199edbd4a0SFrançois Tigeot ret = setup_scratch_page(dev); 24209edbd4a0SFrançois Tigeot if (ret) { 24219edbd4a0SFrançois Tigeot DRM_ERROR("Scratch setup failed\n"); 24229edbd4a0SFrançois Tigeot /* iounmap will also get called at remove, but meh */ 24239edbd4a0SFrançois Tigeot iounmap(dev_priv->gtt.gsm); 24249edbd4a0SFrançois Tigeot } 24259edbd4a0SFrançois Tigeot 24269edbd4a0SFrançois Tigeot return ret; 24279edbd4a0SFrançois Tigeot } 24289edbd4a0SFrançois Tigeot 24299edbd4a0SFrançois Tigeot /* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability 24309edbd4a0SFrançois Tigeot * bits. When using advanced contexts each context stores its own PAT, but 24319edbd4a0SFrançois Tigeot * writing this data shouldn't be harmful even in those cases. */ 2432ba55f2f5SFrançois Tigeot static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv) 24339edbd4a0SFrançois Tigeot { 24349edbd4a0SFrançois Tigeot uint64_t pat; 24359edbd4a0SFrançois Tigeot 24369edbd4a0SFrançois Tigeot pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC) | /* for normal objects, no eLLC */ 24379edbd4a0SFrançois Tigeot GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */ 24389edbd4a0SFrançois Tigeot GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */ 24399edbd4a0SFrançois Tigeot GEN8_PPAT(3, GEN8_PPAT_UC) | /* Uncached objects, mostly for scanout */ 24409edbd4a0SFrançois Tigeot GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) | 24419edbd4a0SFrançois Tigeot GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) | 24429edbd4a0SFrançois Tigeot GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) | 24439edbd4a0SFrançois Tigeot GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3)); 24449edbd4a0SFrançois Tigeot 2445a6e033d9SFrançois Tigeot if (!USES_PPGTT(dev_priv->dev)) 2446a6e033d9SFrançois Tigeot /* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry, 2447a6e033d9SFrançois Tigeot * so RTL will always use the value corresponding to 2448a6e033d9SFrançois Tigeot * pat_sel = 000". 2449a6e033d9SFrançois Tigeot * So let's disable cache for GGTT to avoid screen corruptions. 2450a6e033d9SFrançois Tigeot * MOCS still can be used though. 2451a6e033d9SFrançois Tigeot * - System agent ggtt writes (i.e. cpu gtt mmaps) already work 2452a6e033d9SFrançois Tigeot * before this patch, i.e. the same uncached + snooping access 2453a6e033d9SFrançois Tigeot * like on gen6/7 seems to be in effect. 2454a6e033d9SFrançois Tigeot * - So this just fixes blitter/render access. Again it looks 2455a6e033d9SFrançois Tigeot * like it's not just uncached access, but uncached + snooping. 2456a6e033d9SFrançois Tigeot * So we can still hold onto all our assumptions wrt cpu 2457a6e033d9SFrançois Tigeot * clflushing on LLC machines. 2458a6e033d9SFrançois Tigeot */ 2459a6e033d9SFrançois Tigeot pat = GEN8_PPAT(0, GEN8_PPAT_UC); 2460a6e033d9SFrançois Tigeot 24619edbd4a0SFrançois Tigeot /* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b 24629edbd4a0SFrançois Tigeot * write would work. */ 24639edbd4a0SFrançois Tigeot I915_WRITE(GEN8_PRIVATE_PAT, pat); 24649edbd4a0SFrançois Tigeot I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32); 24659edbd4a0SFrançois Tigeot } 24669edbd4a0SFrançois Tigeot 2467ba55f2f5SFrançois Tigeot static void chv_setup_private_ppat(struct drm_i915_private *dev_priv) 2468ba55f2f5SFrançois Tigeot { 2469ba55f2f5SFrançois Tigeot uint64_t pat; 2470ba55f2f5SFrançois Tigeot 2471ba55f2f5SFrançois Tigeot /* 2472ba55f2f5SFrançois Tigeot * Map WB on BDW to snooped on CHV. 2473ba55f2f5SFrançois Tigeot * 2474ba55f2f5SFrançois Tigeot * Only the snoop bit has meaning for CHV, the rest is 2475ba55f2f5SFrançois Tigeot * ignored. 2476ba55f2f5SFrançois Tigeot * 24772c9916cdSFrançois Tigeot * The hardware will never snoop for certain types of accesses: 24782c9916cdSFrançois Tigeot * - CPU GTT (GMADR->GGTT->no snoop->memory) 24792c9916cdSFrançois Tigeot * - PPGTT page tables 24802c9916cdSFrançois Tigeot * - some other special cycles 24812c9916cdSFrançois Tigeot * 24822c9916cdSFrançois Tigeot * As with BDW, we also need to consider the following for GT accesses: 24832c9916cdSFrançois Tigeot * "For GGTT, there is NO pat_sel[2:0] from the entry, 24842c9916cdSFrançois Tigeot * so RTL will always use the value corresponding to 24852c9916cdSFrançois Tigeot * pat_sel = 000". 24862c9916cdSFrançois Tigeot * Which means we must set the snoop bit in PAT entry 0 24872c9916cdSFrançois Tigeot * in order to keep the global status page working. 2488ba55f2f5SFrançois Tigeot */ 2489ba55f2f5SFrançois Tigeot pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) | 2490ba55f2f5SFrançois Tigeot GEN8_PPAT(1, 0) | 2491ba55f2f5SFrançois Tigeot GEN8_PPAT(2, 0) | 2492ba55f2f5SFrançois Tigeot GEN8_PPAT(3, 0) | 2493ba55f2f5SFrançois Tigeot GEN8_PPAT(4, CHV_PPAT_SNOOP) | 2494ba55f2f5SFrançois Tigeot GEN8_PPAT(5, CHV_PPAT_SNOOP) | 2495ba55f2f5SFrançois Tigeot GEN8_PPAT(6, CHV_PPAT_SNOOP) | 2496ba55f2f5SFrançois Tigeot GEN8_PPAT(7, CHV_PPAT_SNOOP); 2497ba55f2f5SFrançois Tigeot 2498ba55f2f5SFrançois Tigeot I915_WRITE(GEN8_PRIVATE_PAT, pat); 2499ba55f2f5SFrançois Tigeot I915_WRITE(GEN8_PRIVATE_PAT + 4, pat >> 32); 2500ba55f2f5SFrançois Tigeot } 2501ba55f2f5SFrançois Tigeot 25029edbd4a0SFrançois Tigeot static int gen8_gmch_probe(struct drm_device *dev, 25039edbd4a0SFrançois Tigeot size_t *gtt_total, 25049edbd4a0SFrançois Tigeot size_t *stolen, 25059edbd4a0SFrançois Tigeot phys_addr_t *mappable_base, 25069edbd4a0SFrançois Tigeot unsigned long *mappable_end) 25079edbd4a0SFrançois Tigeot { 25089edbd4a0SFrançois Tigeot struct drm_i915_private *dev_priv = dev->dev_private; 25099edbd4a0SFrançois Tigeot unsigned int gtt_size; 25109edbd4a0SFrançois Tigeot u16 snb_gmch_ctl; 25119edbd4a0SFrançois Tigeot int ret; 25129edbd4a0SFrançois Tigeot 25139edbd4a0SFrançois Tigeot /* TODO: We're not aware of mappable constraints on gen8 yet */ 25149edbd4a0SFrançois Tigeot *mappable_base = pci_resource_start(dev->pdev, 2); 25159edbd4a0SFrançois Tigeot *mappable_end = pci_resource_len(dev->pdev, 2); 25169edbd4a0SFrançois Tigeot 25179edbd4a0SFrançois Tigeot #if 0 25189edbd4a0SFrançois Tigeot if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(39))) 25199edbd4a0SFrançois Tigeot pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(39)); 25209edbd4a0SFrançois Tigeot #endif 25219edbd4a0SFrançois Tigeot 25229edbd4a0SFrançois Tigeot pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl); 25239edbd4a0SFrançois Tigeot 25242c9916cdSFrançois Tigeot if (INTEL_INFO(dev)->gen >= 9) { 25252c9916cdSFrançois Tigeot *stolen = gen9_get_stolen_size(snb_gmch_ctl); 25262c9916cdSFrançois Tigeot gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl); 25272c9916cdSFrançois Tigeot } else if (IS_CHERRYVIEW(dev)) { 2528ba55f2f5SFrançois Tigeot *stolen = chv_get_stolen_size(snb_gmch_ctl); 2529ba55f2f5SFrançois Tigeot gtt_size = chv_get_total_gtt_size(snb_gmch_ctl); 2530ba55f2f5SFrançois Tigeot } else { 25319edbd4a0SFrançois Tigeot *stolen = gen8_get_stolen_size(snb_gmch_ctl); 25329edbd4a0SFrançois Tigeot gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl); 2533ba55f2f5SFrançois Tigeot } 2534ba55f2f5SFrançois Tigeot 2535477eb7f9SFrançois Tigeot *gtt_total = (gtt_size / sizeof(gen8_pte_t)) << PAGE_SHIFT; 25369edbd4a0SFrançois Tigeot 253719c468b4SFrançois Tigeot if (IS_CHERRYVIEW(dev) || IS_BROXTON(dev)) 2538ba55f2f5SFrançois Tigeot chv_setup_private_ppat(dev_priv); 2539ba55f2f5SFrançois Tigeot else 2540ba55f2f5SFrançois Tigeot bdw_setup_private_ppat(dev_priv); 25419edbd4a0SFrançois Tigeot 25429edbd4a0SFrançois Tigeot ret = ggtt_probe_common(dev, gtt_size); 25439edbd4a0SFrançois Tigeot 25449edbd4a0SFrançois Tigeot dev_priv->gtt.base.clear_range = gen8_ggtt_clear_range; 25459edbd4a0SFrançois Tigeot dev_priv->gtt.base.insert_entries = gen8_ggtt_insert_entries; 254619c468b4SFrançois Tigeot dev_priv->gtt.base.bind_vma = ggtt_bind_vma; 254719c468b4SFrançois Tigeot dev_priv->gtt.base.unbind_vma = ggtt_unbind_vma; 25489edbd4a0SFrançois Tigeot 25499edbd4a0SFrançois Tigeot return ret; 25509edbd4a0SFrançois Tigeot } 25519edbd4a0SFrançois Tigeot 25525d0b1887SFrançois Tigeot static int gen6_gmch_probe(struct drm_device *dev, 25535d0b1887SFrançois Tigeot size_t *gtt_total, 25545d0b1887SFrançois Tigeot size_t *stolen, 25555d0b1887SFrançois Tigeot phys_addr_t *mappable_base, 25565d0b1887SFrançois Tigeot unsigned long *mappable_end) 25575d0b1887SFrançois Tigeot { 25585d0b1887SFrançois Tigeot struct drm_i915_private *dev_priv = dev->dev_private; 25595d0b1887SFrançois Tigeot unsigned int gtt_size; 25605d0b1887SFrançois Tigeot u16 snb_gmch_ctl; 25615d0b1887SFrançois Tigeot int ret; 25625d0b1887SFrançois Tigeot 25635d0b1887SFrançois Tigeot *mappable_base = pci_resource_start(dev->pdev, 2); 25645d0b1887SFrançois Tigeot *mappable_end = pci_resource_len(dev->pdev, 2); 25655d0b1887SFrançois Tigeot 25665d0b1887SFrançois Tigeot /* 64/512MB is the current min/max we actually know of, but this is just 25675d0b1887SFrançois Tigeot * a coarse sanity check. 25685d0b1887SFrançois Tigeot */ 25695d0b1887SFrançois Tigeot if ((*mappable_end < (64<<20) || (*mappable_end > (512<<20)))) { 25705d0b1887SFrançois Tigeot DRM_ERROR("Unknown GMADR size (%lx)\n", 25715d0b1887SFrançois Tigeot dev_priv->gtt.mappable_end); 25725d0b1887SFrançois Tigeot return -ENXIO; 25735d0b1887SFrançois Tigeot } 25745d0b1887SFrançois Tigeot 25759edbd4a0SFrançois Tigeot #if 0 25765d0b1887SFrançois Tigeot if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(40))) 25775d0b1887SFrançois Tigeot pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(40)); 25789edbd4a0SFrançois Tigeot #endif 25795d0b1887SFrançois Tigeot pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl); 25805d0b1887SFrançois Tigeot 25815d0b1887SFrançois Tigeot *stolen = gen6_get_stolen_size(snb_gmch_ctl); 25825d0b1887SFrançois Tigeot 25839edbd4a0SFrançois Tigeot gtt_size = gen6_get_total_gtt_size(snb_gmch_ctl); 2584477eb7f9SFrançois Tigeot *gtt_total = (gtt_size / sizeof(gen6_pte_t)) << PAGE_SHIFT; 25855d0b1887SFrançois Tigeot 25869edbd4a0SFrançois Tigeot ret = ggtt_probe_common(dev, gtt_size); 25875d0b1887SFrançois Tigeot 25889edbd4a0SFrançois Tigeot dev_priv->gtt.base.clear_range = gen6_ggtt_clear_range; 25899edbd4a0SFrançois Tigeot dev_priv->gtt.base.insert_entries = gen6_ggtt_insert_entries; 259019c468b4SFrançois Tigeot dev_priv->gtt.base.bind_vma = ggtt_bind_vma; 259119c468b4SFrançois Tigeot dev_priv->gtt.base.unbind_vma = ggtt_unbind_vma; 25929edbd4a0SFrançois Tigeot 25939edbd4a0SFrançois Tigeot return ret; 25945d0b1887SFrançois Tigeot } 25955d0b1887SFrançois Tigeot 25969edbd4a0SFrançois Tigeot static void gen6_gmch_remove(struct i915_address_space *vm) 25975d0b1887SFrançois Tigeot { 25989edbd4a0SFrançois Tigeot struct i915_gtt *gtt = container_of(vm, struct i915_gtt, base); 25999edbd4a0SFrançois Tigeot 26009edbd4a0SFrançois Tigeot iounmap(gtt->gsm); 26019edbd4a0SFrançois Tigeot teardown_scratch_page(vm->dev); 26029edbd4a0SFrançois Tigeot } 26035d0b1887SFrançois Tigeot 26045d0b1887SFrançois Tigeot static int i915_gmch_probe(struct drm_device *dev, 26055d0b1887SFrançois Tigeot size_t *gtt_total, 26065d0b1887SFrançois Tigeot size_t *stolen, 26075d0b1887SFrançois Tigeot phys_addr_t *mappable_base, 26085d0b1887SFrançois Tigeot unsigned long *mappable_end) 26095d0b1887SFrançois Tigeot { 26109edbd4a0SFrançois Tigeot struct drm_i915_private *dev_priv = dev->dev_private; 26119edbd4a0SFrançois Tigeot #if 0 26129edbd4a0SFrançois Tigeot int ret; 26139edbd4a0SFrançois Tigeot 26149edbd4a0SFrançois Tigeot ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->dev->pdev, NULL); 26159edbd4a0SFrançois Tigeot if (!ret) { 26169edbd4a0SFrançois Tigeot DRM_ERROR("failed to set up gmch\n"); 26179edbd4a0SFrançois Tigeot return -EIO; 26189edbd4a0SFrançois Tigeot } 26199edbd4a0SFrançois Tigeot #endif 26209edbd4a0SFrançois Tigeot 26219edbd4a0SFrançois Tigeot intel_gtt_get(gtt_total, stolen, mappable_base, mappable_end); 26229edbd4a0SFrançois Tigeot 26239edbd4a0SFrançois Tigeot dev_priv->gtt.do_idle_maps = needs_idle_maps(dev_priv->dev); 2624*7ec9f8e5SFrançois Tigeot dev_priv->gtt.base.insert_entries = i915_ggtt_insert_entries; 26259edbd4a0SFrançois Tigeot dev_priv->gtt.base.clear_range = i915_ggtt_clear_range; 2626*7ec9f8e5SFrançois Tigeot dev_priv->gtt.base.bind_vma = ggtt_bind_vma; 2627*7ec9f8e5SFrançois Tigeot dev_priv->gtt.base.unbind_vma = ggtt_unbind_vma; 26289edbd4a0SFrançois Tigeot 26299edbd4a0SFrançois Tigeot if (unlikely(dev_priv->gtt.do_idle_maps)) 26309edbd4a0SFrançois Tigeot DRM_INFO("applying Ironlake quirks for intel_iommu\n"); 26319edbd4a0SFrançois Tigeot 26325d0b1887SFrançois Tigeot return 0; 26335d0b1887SFrançois Tigeot } 26345d0b1887SFrançois Tigeot 26359edbd4a0SFrançois Tigeot static void i915_gmch_remove(struct i915_address_space *vm) 26365d0b1887SFrançois Tigeot { 263724edb884SFrançois Tigeot intel_gmch_remove(); 26385d0b1887SFrançois Tigeot } 26395d0b1887SFrançois Tigeot 26400b869d8aSFrançois Tigeot int i915_gem_gtt_init(struct drm_device *dev) 26410b869d8aSFrançois Tigeot { 26420b869d8aSFrançois Tigeot struct drm_i915_private *dev_priv = dev->dev_private; 26439edbd4a0SFrançois Tigeot struct i915_gtt *gtt = &dev_priv->gtt; 26449edbd4a0SFrançois Tigeot int ret; 26450b869d8aSFrançois Tigeot 26469edbd4a0SFrançois Tigeot if (INTEL_INFO(dev)->gen <= 5) { 26479edbd4a0SFrançois Tigeot gtt->gtt_probe = i915_gmch_probe; 26489edbd4a0SFrançois Tigeot gtt->base.cleanup = i915_gmch_remove; 26499edbd4a0SFrançois Tigeot } else if (INTEL_INFO(dev)->gen < 8) { 26509edbd4a0SFrançois Tigeot gtt->gtt_probe = gen6_gmch_probe; 26519edbd4a0SFrançois Tigeot gtt->base.cleanup = gen6_gmch_remove; 26529edbd4a0SFrançois Tigeot if (IS_HASWELL(dev) && dev_priv->ellc_size) 26539edbd4a0SFrançois Tigeot gtt->base.pte_encode = iris_pte_encode; 26549edbd4a0SFrançois Tigeot else if (IS_HASWELL(dev)) 26559edbd4a0SFrançois Tigeot gtt->base.pte_encode = hsw_pte_encode; 26569edbd4a0SFrançois Tigeot else if (IS_VALLEYVIEW(dev)) 26579edbd4a0SFrançois Tigeot gtt->base.pte_encode = byt_pte_encode; 26589edbd4a0SFrançois Tigeot else if (INTEL_INFO(dev)->gen >= 7) 26599edbd4a0SFrançois Tigeot gtt->base.pte_encode = ivb_pte_encode; 26609edbd4a0SFrançois Tigeot else 26619edbd4a0SFrançois Tigeot gtt->base.pte_encode = snb_pte_encode; 26625d0b1887SFrançois Tigeot } else { 26639edbd4a0SFrançois Tigeot dev_priv->gtt.gtt_probe = gen8_gmch_probe; 26649edbd4a0SFrançois Tigeot dev_priv->gtt.base.cleanup = gen6_gmch_remove; 26655d0b1887SFrançois Tigeot } 2666a2fdbec6SFrançois Tigeot 26679edbd4a0SFrançois Tigeot ret = gtt->gtt_probe(dev, >t->base.total, >t->stolen_size, 26689edbd4a0SFrançois Tigeot >t->mappable_base, >t->mappable_end); 26699edbd4a0SFrançois Tigeot if (ret) 26709edbd4a0SFrançois Tigeot return ret; 26710b869d8aSFrançois Tigeot 26729edbd4a0SFrançois Tigeot gtt->base.dev = dev; 26730b869d8aSFrançois Tigeot 26745d0b1887SFrançois Tigeot /* GMADR is the PCI mmio aperture into the global GTT. */ 26755d0b1887SFrançois Tigeot DRM_INFO("Memory usable by graphics device = %zdM\n", 26769edbd4a0SFrançois Tigeot gtt->base.total >> 20); 26779edbd4a0SFrançois Tigeot DRM_DEBUG_DRIVER("GMADR size = %ldM\n", gtt->mappable_end >> 20); 26789edbd4a0SFrançois Tigeot DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", gtt->stolen_size >> 20); 2679ba55f2f5SFrançois Tigeot #ifdef CONFIG_INTEL_IOMMU 2680ba55f2f5SFrançois Tigeot if (intel_iommu_gfx_mapped) 2681ba55f2f5SFrançois Tigeot DRM_INFO("VT-d active for gfx access\n"); 2682ba55f2f5SFrançois Tigeot #endif 268324edb884SFrançois Tigeot /* 268424edb884SFrançois Tigeot * i915.enable_ppgtt is read-only, so do an early pass to validate the 268524edb884SFrançois Tigeot * user's requested state against the hardware/driver capabilities. We 268624edb884SFrançois Tigeot * do this now so that we can print out any log messages once rather 268724edb884SFrançois Tigeot * than every time we check intel_enable_ppgtt(). 268824edb884SFrançois Tigeot */ 268924edb884SFrançois Tigeot i915.enable_ppgtt = sanitize_enable_ppgtt(dev, i915.enable_ppgtt); 269024edb884SFrançois Tigeot DRM_DEBUG_DRIVER("ppgtt mode: %i\n", i915.enable_ppgtt); 2691a2fdbec6SFrançois Tigeot 26920b869d8aSFrançois Tigeot return 0; 26930b869d8aSFrançois Tigeot } 2694ba55f2f5SFrançois Tigeot 269519c468b4SFrançois Tigeot void i915_gem_restore_gtt_mappings(struct drm_device *dev) 269619c468b4SFrançois Tigeot { 269719c468b4SFrançois Tigeot struct drm_i915_private *dev_priv = dev->dev_private; 269819c468b4SFrançois Tigeot struct drm_i915_gem_object *obj; 269919c468b4SFrançois Tigeot struct i915_address_space *vm; 270019c468b4SFrançois Tigeot struct i915_vma *vma; 270119c468b4SFrançois Tigeot bool flush; 270219c468b4SFrançois Tigeot 270319c468b4SFrançois Tigeot i915_check_and_clear_faults(dev); 270419c468b4SFrançois Tigeot 270519c468b4SFrançois Tigeot /* First fill our portion of the GTT with scratch pages */ 270619c468b4SFrançois Tigeot dev_priv->gtt.base.clear_range(&dev_priv->gtt.base, 270719c468b4SFrançois Tigeot dev_priv->gtt.base.start, 270819c468b4SFrançois Tigeot dev_priv->gtt.base.total, 270919c468b4SFrançois Tigeot true); 271019c468b4SFrançois Tigeot 271119c468b4SFrançois Tigeot /* Cache flush objects bound into GGTT and rebind them. */ 271219c468b4SFrançois Tigeot vm = &dev_priv->gtt.base; 271319c468b4SFrançois Tigeot list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) { 271419c468b4SFrançois Tigeot flush = false; 271519c468b4SFrançois Tigeot list_for_each_entry(vma, &obj->vma_list, vma_link) { 271619c468b4SFrançois Tigeot if (vma->vm != vm) 271719c468b4SFrançois Tigeot continue; 271819c468b4SFrançois Tigeot 271919c468b4SFrançois Tigeot WARN_ON(i915_vma_bind(vma, obj->cache_level, 272019c468b4SFrançois Tigeot PIN_UPDATE)); 272119c468b4SFrançois Tigeot 272219c468b4SFrançois Tigeot flush = true; 272319c468b4SFrançois Tigeot } 272419c468b4SFrançois Tigeot 272519c468b4SFrançois Tigeot if (flush) 272619c468b4SFrançois Tigeot i915_gem_clflush_object(obj, obj->pin_display); 272719c468b4SFrançois Tigeot } 272819c468b4SFrançois Tigeot 272919c468b4SFrançois Tigeot if (INTEL_INFO(dev)->gen >= 8) { 273019c468b4SFrançois Tigeot if (IS_CHERRYVIEW(dev) || IS_BROXTON(dev)) 273119c468b4SFrançois Tigeot chv_setup_private_ppat(dev_priv); 273219c468b4SFrançois Tigeot else 273319c468b4SFrançois Tigeot bdw_setup_private_ppat(dev_priv); 273419c468b4SFrançois Tigeot 273519c468b4SFrançois Tigeot return; 273619c468b4SFrançois Tigeot } 273719c468b4SFrançois Tigeot 273819c468b4SFrançois Tigeot if (USES_PPGTT(dev)) { 273919c468b4SFrançois Tigeot list_for_each_entry(vm, &dev_priv->vm_list, global_link) { 274019c468b4SFrançois Tigeot /* TODO: Perhaps it shouldn't be gen6 specific */ 274119c468b4SFrançois Tigeot 274219c468b4SFrançois Tigeot struct i915_hw_ppgtt *ppgtt = 274319c468b4SFrançois Tigeot container_of(vm, struct i915_hw_ppgtt, 274419c468b4SFrançois Tigeot base); 274519c468b4SFrançois Tigeot 274619c468b4SFrançois Tigeot if (i915_is_ggtt(vm)) 274719c468b4SFrançois Tigeot ppgtt = dev_priv->mm.aliasing_ppgtt; 274819c468b4SFrançois Tigeot 274919c468b4SFrançois Tigeot gen6_write_page_range(dev_priv, &ppgtt->pd, 275019c468b4SFrançois Tigeot 0, ppgtt->base.total); 275119c468b4SFrançois Tigeot } 275219c468b4SFrançois Tigeot } 275319c468b4SFrançois Tigeot 275419c468b4SFrançois Tigeot i915_ggtt_flush(dev_priv); 275519c468b4SFrançois Tigeot } 275619c468b4SFrançois Tigeot 2757477eb7f9SFrançois Tigeot static struct i915_vma * 2758477eb7f9SFrançois Tigeot __i915_gem_vma_create(struct drm_i915_gem_object *obj, 27592c9916cdSFrançois Tigeot struct i915_address_space *vm, 2760477eb7f9SFrançois Tigeot const struct i915_ggtt_view *ggtt_view) 2761ba55f2f5SFrançois Tigeot { 2762477eb7f9SFrançois Tigeot struct i915_vma *vma; 2763477eb7f9SFrançois Tigeot 2764477eb7f9SFrançois Tigeot if (WARN_ON(i915_is_ggtt(vm) != !!ggtt_view)) 2765477eb7f9SFrançois Tigeot return ERR_PTR(-EINVAL); 2766*7ec9f8e5SFrançois Tigeot 2767477eb7f9SFrançois Tigeot vma = kzalloc(sizeof(*vma), GFP_KERNEL); 2768ba55f2f5SFrançois Tigeot if (vma == NULL) 2769ba55f2f5SFrançois Tigeot return ERR_PTR(-ENOMEM); 2770ba55f2f5SFrançois Tigeot 2771ba55f2f5SFrançois Tigeot INIT_LIST_HEAD(&vma->vma_link); 2772ba55f2f5SFrançois Tigeot INIT_LIST_HEAD(&vma->mm_list); 2773ba55f2f5SFrançois Tigeot INIT_LIST_HEAD(&vma->exec_list); 2774ba55f2f5SFrançois Tigeot vma->vm = vm; 2775ba55f2f5SFrançois Tigeot vma->obj = obj; 2776ba55f2f5SFrançois Tigeot 277719c468b4SFrançois Tigeot if (i915_is_ggtt(vm)) 2778477eb7f9SFrançois Tigeot vma->ggtt_view = *ggtt_view; 2779477eb7f9SFrançois Tigeot 2780ba55f2f5SFrançois Tigeot list_add_tail(&vma->vma_link, &obj->vma_list); 27812c9916cdSFrançois Tigeot if (!i915_is_ggtt(vm)) 27821b13d190SFrançois Tigeot i915_ppgtt_get(i915_vm_to_ppgtt(vm)); 2783ba55f2f5SFrançois Tigeot 2784ba55f2f5SFrançois Tigeot return vma; 2785ba55f2f5SFrançois Tigeot } 2786ba55f2f5SFrançois Tigeot 2787ba55f2f5SFrançois Tigeot struct i915_vma * 2788477eb7f9SFrançois Tigeot i915_gem_obj_lookup_or_create_vma(struct drm_i915_gem_object *obj, 2789477eb7f9SFrançois Tigeot struct i915_address_space *vm) 2790ba55f2f5SFrançois Tigeot { 2791ba55f2f5SFrançois Tigeot struct i915_vma *vma; 2792ba55f2f5SFrançois Tigeot 2793477eb7f9SFrançois Tigeot vma = i915_gem_obj_to_vma(obj, vm); 2794ba55f2f5SFrançois Tigeot if (!vma) 2795477eb7f9SFrançois Tigeot vma = __i915_gem_vma_create(obj, vm, 2796477eb7f9SFrançois Tigeot i915_is_ggtt(vm) ? &i915_ggtt_view_normal : NULL); 2797ba55f2f5SFrançois Tigeot 2798ba55f2f5SFrançois Tigeot return vma; 2799ba55f2f5SFrançois Tigeot } 28002c9916cdSFrançois Tigeot 2801477eb7f9SFrançois Tigeot struct i915_vma * 2802477eb7f9SFrançois Tigeot i915_gem_obj_lookup_or_create_ggtt_vma(struct drm_i915_gem_object *obj, 2803477eb7f9SFrançois Tigeot const struct i915_ggtt_view *view) 28042c9916cdSFrançois Tigeot { 2805477eb7f9SFrançois Tigeot struct i915_address_space *ggtt = i915_obj_to_ggtt(obj); 2806477eb7f9SFrançois Tigeot struct i915_vma *vma; 2807477eb7f9SFrançois Tigeot 2808477eb7f9SFrançois Tigeot if (WARN_ON(!view)) 2809477eb7f9SFrançois Tigeot return ERR_PTR(-EINVAL); 2810477eb7f9SFrançois Tigeot 2811477eb7f9SFrançois Tigeot vma = i915_gem_obj_to_ggtt_view(obj, view); 2812477eb7f9SFrançois Tigeot 2813477eb7f9SFrançois Tigeot if (IS_ERR(vma)) 2814477eb7f9SFrançois Tigeot return vma; 2815477eb7f9SFrançois Tigeot 2816477eb7f9SFrançois Tigeot if (!vma) 2817477eb7f9SFrançois Tigeot vma = __i915_gem_vma_create(obj, ggtt, view); 2818477eb7f9SFrançois Tigeot 2819477eb7f9SFrançois Tigeot return vma; 2820477eb7f9SFrançois Tigeot 2821477eb7f9SFrançois Tigeot } 2822477eb7f9SFrançois Tigeot 2823477eb7f9SFrançois Tigeot static void 2824477eb7f9SFrançois Tigeot rotate_pages(dma_addr_t *in, unsigned int width, unsigned int height, 2825477eb7f9SFrançois Tigeot struct sg_table *st) 2826477eb7f9SFrançois Tigeot { 2827477eb7f9SFrançois Tigeot unsigned int column, row; 2828477eb7f9SFrançois Tigeot unsigned int src_idx; 2829477eb7f9SFrançois Tigeot struct scatterlist *sg = st->sgl; 2830477eb7f9SFrançois Tigeot 2831477eb7f9SFrançois Tigeot st->nents = 0; 2832477eb7f9SFrançois Tigeot 2833477eb7f9SFrançois Tigeot for (column = 0; column < width; column++) { 2834477eb7f9SFrançois Tigeot src_idx = width * (height - 1) + column; 2835477eb7f9SFrançois Tigeot for (row = 0; row < height; row++) { 2836477eb7f9SFrançois Tigeot st->nents++; 2837477eb7f9SFrançois Tigeot /* We don't need the pages, but need to initialize 2838477eb7f9SFrançois Tigeot * the entries so the sg list can be happily traversed. 2839477eb7f9SFrançois Tigeot * The only thing we need are DMA addresses. 2840477eb7f9SFrançois Tigeot */ 2841477eb7f9SFrançois Tigeot sg_set_page(sg, NULL, PAGE_SIZE, 0); 2842477eb7f9SFrançois Tigeot sg_dma_address(sg) = in[src_idx]; 2843477eb7f9SFrançois Tigeot sg_dma_len(sg) = PAGE_SIZE; 2844477eb7f9SFrançois Tigeot sg = sg_next(sg); 2845477eb7f9SFrançois Tigeot src_idx -= width; 2846477eb7f9SFrançois Tigeot } 2847477eb7f9SFrançois Tigeot } 2848477eb7f9SFrançois Tigeot } 2849477eb7f9SFrançois Tigeot 2850477eb7f9SFrançois Tigeot static struct sg_table * 2851477eb7f9SFrançois Tigeot intel_rotate_fb_obj_pages(struct i915_ggtt_view *ggtt_view, 2852477eb7f9SFrançois Tigeot struct drm_i915_gem_object *obj) 2853477eb7f9SFrançois Tigeot { 2854477eb7f9SFrançois Tigeot struct drm_device *dev = obj->base.dev; 2855477eb7f9SFrançois Tigeot struct intel_rotation_info *rot_info = &ggtt_view->rotation_info; 2856477eb7f9SFrançois Tigeot unsigned long size, pages, rot_pages; 2857477eb7f9SFrançois Tigeot struct sg_page_iter sg_iter; 2858477eb7f9SFrançois Tigeot unsigned long i; 2859477eb7f9SFrançois Tigeot dma_addr_t *page_addr_list; 2860477eb7f9SFrançois Tigeot struct sg_table *st; 2861477eb7f9SFrançois Tigeot unsigned int tile_pitch, tile_height; 2862477eb7f9SFrançois Tigeot unsigned int width_pages, height_pages; 2863477eb7f9SFrançois Tigeot int ret = -ENOMEM; 2864477eb7f9SFrançois Tigeot 2865477eb7f9SFrançois Tigeot pages = obj->base.size / PAGE_SIZE; 2866477eb7f9SFrançois Tigeot 2867477eb7f9SFrançois Tigeot /* Calculate tiling geometry. */ 2868477eb7f9SFrançois Tigeot tile_height = intel_tile_height(dev, rot_info->pixel_format, 2869477eb7f9SFrançois Tigeot rot_info->fb_modifier); 2870477eb7f9SFrançois Tigeot tile_pitch = PAGE_SIZE / tile_height; 2871477eb7f9SFrançois Tigeot width_pages = DIV_ROUND_UP(rot_info->pitch, tile_pitch); 2872477eb7f9SFrançois Tigeot height_pages = DIV_ROUND_UP(rot_info->height, tile_height); 2873477eb7f9SFrançois Tigeot rot_pages = width_pages * height_pages; 2874477eb7f9SFrançois Tigeot size = rot_pages * PAGE_SIZE; 2875477eb7f9SFrançois Tigeot 2876477eb7f9SFrançois Tigeot /* Allocate a temporary list of source pages for random access. */ 2877477eb7f9SFrançois Tigeot page_addr_list = drm_malloc_ab(pages, sizeof(dma_addr_t)); 2878477eb7f9SFrançois Tigeot if (!page_addr_list) 2879477eb7f9SFrançois Tigeot return ERR_PTR(ret); 2880477eb7f9SFrançois Tigeot 2881477eb7f9SFrançois Tigeot /* Allocate target SG list. */ 2882*7ec9f8e5SFrançois Tigeot st = kmalloc(sizeof(*st), M_DRM, M_WAITOK); 2883477eb7f9SFrançois Tigeot if (!st) 2884477eb7f9SFrançois Tigeot goto err_st_alloc; 2885477eb7f9SFrançois Tigeot 2886477eb7f9SFrançois Tigeot ret = sg_alloc_table(st, rot_pages, GFP_KERNEL); 2887477eb7f9SFrançois Tigeot if (ret) 2888477eb7f9SFrançois Tigeot goto err_sg_alloc; 2889477eb7f9SFrançois Tigeot 2890477eb7f9SFrançois Tigeot /* Populate source page list from the object. */ 2891477eb7f9SFrançois Tigeot i = 0; 2892477eb7f9SFrançois Tigeot for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) { 2893477eb7f9SFrançois Tigeot page_addr_list[i] = sg_page_iter_dma_address(&sg_iter); 2894477eb7f9SFrançois Tigeot i++; 2895477eb7f9SFrançois Tigeot } 2896477eb7f9SFrançois Tigeot 2897477eb7f9SFrançois Tigeot /* Rotate the pages. */ 2898477eb7f9SFrançois Tigeot rotate_pages(page_addr_list, width_pages, height_pages, st); 2899477eb7f9SFrançois Tigeot 2900477eb7f9SFrançois Tigeot DRM_DEBUG_KMS( 2901477eb7f9SFrançois Tigeot "Created rotated page mapping for object size %lu (pitch=%u, height=%u, pixel_format=0x%x, %ux%u tiles, %lu pages).\n", 2902477eb7f9SFrançois Tigeot size, rot_info->pitch, rot_info->height, 2903477eb7f9SFrançois Tigeot rot_info->pixel_format, width_pages, height_pages, 2904477eb7f9SFrançois Tigeot rot_pages); 2905477eb7f9SFrançois Tigeot 2906477eb7f9SFrançois Tigeot drm_free_large(page_addr_list); 2907477eb7f9SFrançois Tigeot 2908477eb7f9SFrançois Tigeot return st; 2909477eb7f9SFrançois Tigeot 2910477eb7f9SFrançois Tigeot err_sg_alloc: 2911477eb7f9SFrançois Tigeot kfree(st); 2912477eb7f9SFrançois Tigeot err_st_alloc: 2913477eb7f9SFrançois Tigeot drm_free_large(page_addr_list); 2914477eb7f9SFrançois Tigeot 2915477eb7f9SFrançois Tigeot DRM_DEBUG_KMS( 2916477eb7f9SFrançois Tigeot "Failed to create rotated mapping for object size %lu! (%d) (pitch=%u, height=%u, pixel_format=0x%x, %ux%u tiles, %lu pages)\n", 2917477eb7f9SFrançois Tigeot size, ret, rot_info->pitch, rot_info->height, 2918477eb7f9SFrançois Tigeot rot_info->pixel_format, width_pages, height_pages, 2919477eb7f9SFrançois Tigeot rot_pages); 2920477eb7f9SFrançois Tigeot return ERR_PTR(ret); 2921477eb7f9SFrançois Tigeot } 292219c468b4SFrançois Tigeot 292319c468b4SFrançois Tigeot static struct sg_table * 292419c468b4SFrançois Tigeot intel_partial_pages(const struct i915_ggtt_view *view, 292519c468b4SFrançois Tigeot struct drm_i915_gem_object *obj) 292619c468b4SFrançois Tigeot { 292719c468b4SFrançois Tigeot struct sg_table *st; 292819c468b4SFrançois Tigeot struct scatterlist *sg; 292919c468b4SFrançois Tigeot struct sg_page_iter obj_sg_iter; 293019c468b4SFrançois Tigeot int ret = -ENOMEM; 293119c468b4SFrançois Tigeot 2932*7ec9f8e5SFrançois Tigeot st = kmalloc(sizeof(*st), M_DRM, M_WAITOK); 293319c468b4SFrançois Tigeot if (!st) 293419c468b4SFrançois Tigeot goto err_st_alloc; 293519c468b4SFrançois Tigeot 293619c468b4SFrançois Tigeot ret = sg_alloc_table(st, view->params.partial.size, GFP_KERNEL); 293719c468b4SFrançois Tigeot if (ret) 293819c468b4SFrançois Tigeot goto err_sg_alloc; 293919c468b4SFrançois Tigeot 294019c468b4SFrançois Tigeot sg = st->sgl; 294119c468b4SFrançois Tigeot st->nents = 0; 294219c468b4SFrançois Tigeot for_each_sg_page(obj->pages->sgl, &obj_sg_iter, obj->pages->nents, 294319c468b4SFrançois Tigeot view->params.partial.offset) 294419c468b4SFrançois Tigeot { 294519c468b4SFrançois Tigeot if (st->nents >= view->params.partial.size) 294619c468b4SFrançois Tigeot break; 294719c468b4SFrançois Tigeot 294819c468b4SFrançois Tigeot sg_set_page(sg, NULL, PAGE_SIZE, 0); 294919c468b4SFrançois Tigeot sg_dma_address(sg) = sg_page_iter_dma_address(&obj_sg_iter); 295019c468b4SFrançois Tigeot sg_dma_len(sg) = PAGE_SIZE; 295119c468b4SFrançois Tigeot 295219c468b4SFrançois Tigeot sg = sg_next(sg); 295319c468b4SFrançois Tigeot st->nents++; 295419c468b4SFrançois Tigeot } 295519c468b4SFrançois Tigeot 295619c468b4SFrançois Tigeot return st; 295719c468b4SFrançois Tigeot 295819c468b4SFrançois Tigeot err_sg_alloc: 295919c468b4SFrançois Tigeot kfree(st); 296019c468b4SFrançois Tigeot err_st_alloc: 296119c468b4SFrançois Tigeot return ERR_PTR(ret); 296219c468b4SFrançois Tigeot } 2963477eb7f9SFrançois Tigeot 296419c468b4SFrançois Tigeot static int 2965477eb7f9SFrançois Tigeot i915_get_ggtt_vma_pages(struct i915_vma *vma) 2966477eb7f9SFrançois Tigeot { 2967477eb7f9SFrançois Tigeot int ret = 0; 2968477eb7f9SFrançois Tigeot 29692c9916cdSFrançois Tigeot if (vma->ggtt_view.pages) 29702c9916cdSFrançois Tigeot return 0; 29712c9916cdSFrançois Tigeot 29722c9916cdSFrançois Tigeot if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL) 29732c9916cdSFrançois Tigeot vma->ggtt_view.pages = vma->obj->pages; 2974477eb7f9SFrançois Tigeot else if (vma->ggtt_view.type == I915_GGTT_VIEW_ROTATED) 2975477eb7f9SFrançois Tigeot vma->ggtt_view.pages = 2976477eb7f9SFrançois Tigeot intel_rotate_fb_obj_pages(&vma->ggtt_view, vma->obj); 297719c468b4SFrançois Tigeot else if (vma->ggtt_view.type == I915_GGTT_VIEW_PARTIAL) 297819c468b4SFrançois Tigeot vma->ggtt_view.pages = 297919c468b4SFrançois Tigeot intel_partial_pages(&vma->ggtt_view, vma->obj); 29802c9916cdSFrançois Tigeot else 29812c9916cdSFrançois Tigeot WARN_ONCE(1, "GGTT view %u not implemented!\n", 29822c9916cdSFrançois Tigeot vma->ggtt_view.type); 29832c9916cdSFrançois Tigeot 29842c9916cdSFrançois Tigeot if (!vma->ggtt_view.pages) { 2985477eb7f9SFrançois Tigeot DRM_ERROR("Failed to get pages for GGTT view type %u!\n", 29862c9916cdSFrançois Tigeot vma->ggtt_view.type); 2987477eb7f9SFrançois Tigeot ret = -EINVAL; 2988477eb7f9SFrançois Tigeot } else if (IS_ERR(vma->ggtt_view.pages)) { 2989477eb7f9SFrançois Tigeot ret = PTR_ERR(vma->ggtt_view.pages); 2990477eb7f9SFrançois Tigeot vma->ggtt_view.pages = NULL; 2991477eb7f9SFrançois Tigeot DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n", 2992477eb7f9SFrançois Tigeot vma->ggtt_view.type, ret); 29932c9916cdSFrançois Tigeot } 29942c9916cdSFrançois Tigeot 2995477eb7f9SFrançois Tigeot return ret; 29962c9916cdSFrançois Tigeot } 29972c9916cdSFrançois Tigeot 29982c9916cdSFrançois Tigeot /** 29992c9916cdSFrançois Tigeot * i915_vma_bind - Sets up PTEs for an VMA in it's corresponding address space. 30002c9916cdSFrançois Tigeot * @vma: VMA to map 30012c9916cdSFrançois Tigeot * @cache_level: mapping cache level 30022c9916cdSFrançois Tigeot * @flags: flags like global or local mapping 30032c9916cdSFrançois Tigeot * 30042c9916cdSFrançois Tigeot * DMA addresses are taken from the scatter-gather table of this object (or of 30052c9916cdSFrançois Tigeot * this VMA in case of non-default GGTT views) and PTE entries set up. 30062c9916cdSFrançois Tigeot * Note that DMA addresses are also the only part of the SG table we care about. 30072c9916cdSFrançois Tigeot */ 30082c9916cdSFrançois Tigeot int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level, 30092c9916cdSFrançois Tigeot u32 flags) 30102c9916cdSFrançois Tigeot { 301119c468b4SFrançois Tigeot int ret; 301219c468b4SFrançois Tigeot u32 bind_flags; 30132c9916cdSFrançois Tigeot 301419c468b4SFrançois Tigeot if (WARN_ON(flags == 0)) 301519c468b4SFrançois Tigeot return -EINVAL; 301619c468b4SFrançois Tigeot 301719c468b4SFrançois Tigeot bind_flags = 0; 301819c468b4SFrançois Tigeot if (flags & PIN_GLOBAL) 301919c468b4SFrançois Tigeot bind_flags |= GLOBAL_BIND; 302019c468b4SFrançois Tigeot if (flags & PIN_USER) 302119c468b4SFrançois Tigeot bind_flags |= LOCAL_BIND; 302219c468b4SFrançois Tigeot 302319c468b4SFrançois Tigeot if (flags & PIN_UPDATE) 302419c468b4SFrançois Tigeot bind_flags |= vma->bound; 302519c468b4SFrançois Tigeot else 302619c468b4SFrançois Tigeot bind_flags &= ~vma->bound; 302719c468b4SFrançois Tigeot 302819c468b4SFrançois Tigeot if (bind_flags == 0) 302919c468b4SFrançois Tigeot return 0; 303019c468b4SFrançois Tigeot 303119c468b4SFrançois Tigeot if (vma->bound == 0 && vma->vm->allocate_va_range) { 303219c468b4SFrançois Tigeot trace_i915_va_alloc(vma->vm, 303319c468b4SFrançois Tigeot vma->node.start, 303419c468b4SFrançois Tigeot vma->node.size, 303519c468b4SFrançois Tigeot VM_TO_TRACE_NAME(vma->vm)); 303619c468b4SFrançois Tigeot 303719c468b4SFrançois Tigeot ret = vma->vm->allocate_va_range(vma->vm, 303819c468b4SFrançois Tigeot vma->node.start, 303919c468b4SFrançois Tigeot vma->node.size); 304019c468b4SFrançois Tigeot if (ret) 304119c468b4SFrançois Tigeot return ret; 304219c468b4SFrançois Tigeot } 304319c468b4SFrançois Tigeot 304419c468b4SFrançois Tigeot ret = vma->vm->bind_vma(vma, cache_level, bind_flags); 304519c468b4SFrançois Tigeot if (ret) 304619c468b4SFrançois Tigeot return ret; 304719c468b4SFrançois Tigeot 304819c468b4SFrançois Tigeot vma->bound |= bind_flags; 30492c9916cdSFrançois Tigeot 30502c9916cdSFrançois Tigeot return 0; 30512c9916cdSFrançois Tigeot } 305219c468b4SFrançois Tigeot 305319c468b4SFrançois Tigeot /** 305419c468b4SFrançois Tigeot * i915_ggtt_view_size - Get the size of a GGTT view. 305519c468b4SFrançois Tigeot * @obj: Object the view is of. 305619c468b4SFrançois Tigeot * @view: The view in question. 305719c468b4SFrançois Tigeot * 305819c468b4SFrançois Tigeot * @return The size of the GGTT view in bytes. 305919c468b4SFrançois Tigeot */ 306019c468b4SFrançois Tigeot size_t 306119c468b4SFrançois Tigeot i915_ggtt_view_size(struct drm_i915_gem_object *obj, 306219c468b4SFrançois Tigeot const struct i915_ggtt_view *view) 306319c468b4SFrançois Tigeot { 306419c468b4SFrançois Tigeot if (view->type == I915_GGTT_VIEW_NORMAL || 306519c468b4SFrançois Tigeot view->type == I915_GGTT_VIEW_ROTATED) { 306619c468b4SFrançois Tigeot return obj->base.size; 306719c468b4SFrançois Tigeot } else if (view->type == I915_GGTT_VIEW_PARTIAL) { 306819c468b4SFrançois Tigeot return view->params.partial.size << PAGE_SHIFT; 306919c468b4SFrançois Tigeot } else { 307019c468b4SFrançois Tigeot WARN_ONCE(1, "GGTT view %u not implemented!\n", view->type); 307119c468b4SFrançois Tigeot return obj->base.size; 307219c468b4SFrançois Tigeot } 307319c468b4SFrançois Tigeot } 3074