Home | History | Annotate | Line # | Download | only in gt
      1  1.19  riastrad /*	$NetBSD: intel_ggtt.c,v 1.19 2025/01/26 18:23:56 riastradh Exp $	*/
      2   1.1  riastrad 
      3   1.1  riastrad // SPDX-License-Identifier: MIT
      4   1.1  riastrad /*
      5   1.1  riastrad  * Copyright  2020 Intel Corporation
      6   1.1  riastrad  */
      7   1.1  riastrad 
      8   1.1  riastrad #include <sys/cdefs.h>
      9  1.19  riastrad __KERNEL_RCSID(0, "$NetBSD: intel_ggtt.c,v 1.19 2025/01/26 18:23:56 riastradh Exp $");
     10   1.1  riastrad 
     11   1.1  riastrad #include <linux/stop_machine.h>
     12   1.1  riastrad 
     13   1.1  riastrad #include <asm/set_memory.h>
     14   1.1  riastrad #include <asm/smp.h>
     15   1.1  riastrad 
     16   1.6  riastrad #ifdef __NetBSD__
     17   1.6  riastrad #include <drm/io-mapping.h>
     18   1.6  riastrad #endif
     19   1.6  riastrad 
     20   1.1  riastrad #include "intel_gt.h"
     21   1.1  riastrad #include "i915_drv.h"
     22   1.1  riastrad #include "i915_scatterlist.h"
     23   1.1  riastrad #include "i915_vgpu.h"
     24   1.1  riastrad 
     25   1.1  riastrad #include "intel_gtt.h"
     26   1.1  riastrad 
     27   1.7  riastrad #include <linux/nbsd-namespace.h>
     28   1.7  riastrad 
     29   1.1  riastrad static int
     30   1.1  riastrad i915_get_ggtt_vma_pages(struct i915_vma *vma);
     31   1.1  riastrad 
     32   1.1  riastrad static void i915_ggtt_color_adjust(const struct drm_mm_node *node,
     33   1.1  riastrad 				   unsigned long color,
     34   1.1  riastrad 				   u64 *start,
     35   1.1  riastrad 				   u64 *end)
     36   1.1  riastrad {
     37   1.1  riastrad 	if (i915_node_color_differs(node, color))
     38   1.1  riastrad 		*start += I915_GTT_PAGE_SIZE;
     39   1.1  riastrad 
     40   1.1  riastrad 	/*
     41   1.1  riastrad 	 * Also leave a space between the unallocated reserved node after the
     42   1.1  riastrad 	 * GTT and any objects within the GTT, i.e. we use the color adjustment
     43   1.1  riastrad 	 * to insert a guard page to prevent prefetches crossing over the
     44   1.1  riastrad 	 * GTT boundary.
     45   1.1  riastrad 	 */
     46   1.1  riastrad 	node = list_next_entry(node, node_list);
     47   1.1  riastrad 	if (node->color != color)
     48   1.1  riastrad 		*end -= I915_GTT_PAGE_SIZE;
     49   1.1  riastrad }
     50   1.1  riastrad 
     51   1.1  riastrad static int ggtt_init_hw(struct i915_ggtt *ggtt)
     52   1.1  riastrad {
     53   1.1  riastrad 	struct drm_i915_private *i915 = ggtt->vm.i915;
     54   1.1  riastrad 
     55   1.1  riastrad 	i915_address_space_init(&ggtt->vm, VM_CLASS_GGTT);
     56   1.1  riastrad 
     57   1.1  riastrad 	ggtt->vm.is_ggtt = true;
     58   1.1  riastrad 
     59   1.1  riastrad 	/* Only VLV supports read-only GGTT mappings */
     60   1.1  riastrad 	ggtt->vm.has_read_only = IS_VALLEYVIEW(i915);
     61   1.1  riastrad 
     62   1.1  riastrad 	if (!HAS_LLC(i915) && !HAS_PPGTT(i915))
     63   1.1  riastrad 		ggtt->vm.mm.color_adjust = i915_ggtt_color_adjust;
     64   1.1  riastrad 
     65   1.1  riastrad 	if (ggtt->mappable_end) {
     66   1.5  riastrad #ifdef __NetBSD__
     67   1.5  riastrad 		if (!drm_io_mapping_init_wc(&i915->drm, &ggtt->iomap,
     68   1.5  riastrad 			ggtt->gmadr.start, ggtt->mappable_end)) {
     69   1.5  riastrad 			ggtt->vm.cleanup(&ggtt->vm);
     70   1.5  riastrad 			return -EIO;
     71   1.5  riastrad 		}
     72   1.5  riastrad 		/*
     73   1.5  riastrad 		 * Note: mappable_end is the size, not end paddr, of
     74   1.5  riastrad 		 * the aperture.
     75   1.5  riastrad 		 */
     76   1.5  riastrad 		pmap_pv_track(ggtt->gmadr.start, ggtt->mappable_end);
     77   1.5  riastrad #else
     78   1.1  riastrad 		if (!io_mapping_init_wc(&ggtt->iomap,
     79   1.1  riastrad 					ggtt->gmadr.start,
     80   1.1  riastrad 					ggtt->mappable_end)) {
     81   1.1  riastrad 			ggtt->vm.cleanup(&ggtt->vm);
     82   1.1  riastrad 			return -EIO;
     83   1.1  riastrad 		}
     84   1.5  riastrad #endif
     85   1.1  riastrad 
     86   1.1  riastrad 		ggtt->mtrr = arch_phys_wc_add(ggtt->gmadr.start,
     87   1.1  riastrad 					      ggtt->mappable_end);
     88   1.1  riastrad 	}
     89   1.1  riastrad 
     90   1.1  riastrad 	i915_ggtt_init_fences(ggtt);
     91   1.1  riastrad 
     92   1.1  riastrad 	return 0;
     93   1.1  riastrad }
     94   1.1  riastrad 
     95   1.1  riastrad /**
     96   1.1  riastrad  * i915_ggtt_init_hw - Initialize GGTT hardware
     97   1.1  riastrad  * @i915: i915 device
     98   1.1  riastrad  */
     99   1.1  riastrad int i915_ggtt_init_hw(struct drm_i915_private *i915)
    100   1.1  riastrad {
    101   1.1  riastrad 	int ret;
    102   1.1  riastrad 
    103   1.8  riastrad #ifndef __NetBSD__
    104   1.1  riastrad 	stash_init(&i915->mm.wc_stash);
    105   1.8  riastrad #endif
    106   1.1  riastrad 
    107   1.1  riastrad 	/*
    108   1.1  riastrad 	 * Note that we use page colouring to enforce a guard page at the
    109   1.1  riastrad 	 * end of the address space. This is required as the CS may prefetch
    110   1.1  riastrad 	 * beyond the end of the batch buffer, across the page boundary,
    111   1.1  riastrad 	 * and beyond the end of the GTT if we do not provide a guard.
    112   1.1  riastrad 	 */
    113   1.1  riastrad 	ret = ggtt_init_hw(&i915->ggtt);
    114   1.1  riastrad 	if (ret)
    115   1.1  riastrad 		return ret;
    116   1.1  riastrad 
    117   1.1  riastrad 	return 0;
    118   1.1  riastrad }
    119   1.1  riastrad 
    120   1.1  riastrad /*
    121   1.1  riastrad  * Certain Gen5 chipsets require require idling the GPU before
    122   1.1  riastrad  * unmapping anything from the GTT when VT-d is enabled.
    123   1.1  riastrad  */
    124   1.1  riastrad static bool needs_idle_maps(struct drm_i915_private *i915)
    125   1.1  riastrad {
    126   1.1  riastrad 	/*
    127   1.1  riastrad 	 * Query intel_iommu to see if we need the workaround. Presumably that
    128   1.1  riastrad 	 * was loaded first.
    129   1.1  riastrad 	 */
    130   1.1  riastrad 	return IS_GEN(i915, 5) && IS_MOBILE(i915) && intel_vtd_active();
    131   1.1  riastrad }
    132   1.1  riastrad 
    133   1.1  riastrad static void ggtt_suspend_mappings(struct i915_ggtt *ggtt)
    134   1.1  riastrad {
    135   1.1  riastrad 	struct drm_i915_private *i915 = ggtt->vm.i915;
    136   1.1  riastrad 
    137   1.1  riastrad 	/*
    138   1.1  riastrad 	 * Don't bother messing with faults pre GEN6 as we have little
    139   1.1  riastrad 	 * documentation supporting that it's a good idea.
    140   1.1  riastrad 	 */
    141   1.1  riastrad 	if (INTEL_GEN(i915) < 6)
    142   1.1  riastrad 		return;
    143   1.1  riastrad 
    144   1.1  riastrad 	intel_gt_check_and_clear_faults(ggtt->vm.gt);
    145   1.1  riastrad 
    146   1.1  riastrad 	ggtt->vm.clear_range(&ggtt->vm, 0, ggtt->vm.total);
    147   1.1  riastrad 
    148   1.1  riastrad 	ggtt->invalidate(ggtt);
    149   1.1  riastrad }
    150   1.1  riastrad 
    151   1.1  riastrad void i915_gem_suspend_gtt_mappings(struct drm_i915_private *i915)
    152   1.1  riastrad {
    153   1.1  riastrad 	ggtt_suspend_mappings(&i915->ggtt);
    154   1.1  riastrad }
    155   1.1  riastrad 
    156   1.1  riastrad void gen6_ggtt_invalidate(struct i915_ggtt *ggtt)
    157   1.1  riastrad {
    158   1.1  riastrad 	struct intel_uncore *uncore = ggtt->vm.gt->uncore;
    159   1.1  riastrad 
    160   1.1  riastrad 	spin_lock_irq(&uncore->lock);
    161   1.1  riastrad 	intel_uncore_write_fw(uncore, GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
    162   1.1  riastrad 	intel_uncore_read_fw(uncore, GFX_FLSH_CNTL_GEN6);
    163   1.1  riastrad 	spin_unlock_irq(&uncore->lock);
    164   1.1  riastrad }
    165   1.1  riastrad 
    166   1.1  riastrad static void gen8_ggtt_invalidate(struct i915_ggtt *ggtt)
    167   1.1  riastrad {
    168   1.1  riastrad 	struct intel_uncore *uncore = ggtt->vm.gt->uncore;
    169   1.1  riastrad 
    170   1.1  riastrad 	/*
    171   1.1  riastrad 	 * Note that as an uncached mmio write, this will flush the
    172   1.1  riastrad 	 * WCB of the writes into the GGTT before it triggers the invalidate.
    173   1.1  riastrad 	 */
    174   1.1  riastrad 	intel_uncore_write_fw(uncore, GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
    175   1.1  riastrad }
    176   1.1  riastrad 
    177   1.1  riastrad static void guc_ggtt_invalidate(struct i915_ggtt *ggtt)
    178   1.1  riastrad {
    179   1.1  riastrad 	struct intel_uncore *uncore = ggtt->vm.gt->uncore;
    180   1.1  riastrad 	struct drm_i915_private *i915 = ggtt->vm.i915;
    181   1.1  riastrad 
    182   1.1  riastrad 	gen8_ggtt_invalidate(ggtt);
    183   1.1  riastrad 
    184   1.1  riastrad 	if (INTEL_GEN(i915) >= 12)
    185   1.1  riastrad 		intel_uncore_write_fw(uncore, GEN12_GUC_TLB_INV_CR,
    186   1.1  riastrad 				      GEN12_GUC_TLB_INV_CR_INVALIDATE);
    187   1.1  riastrad 	else
    188   1.1  riastrad 		intel_uncore_write_fw(uncore, GEN8_GTCR, GEN8_GTCR_INVALIDATE);
    189   1.1  riastrad }
    190   1.1  riastrad 
    191   1.1  riastrad static void gmch_ggtt_invalidate(struct i915_ggtt *ggtt)
    192   1.1  riastrad {
    193   1.1  riastrad 	intel_gtt_chipset_flush();
    194   1.1  riastrad }
    195   1.1  riastrad 
    196   1.2  riastrad #ifdef __NetBSD__
    197   1.2  riastrad static inline void
    198   1.2  riastrad gen8_set_pte(bus_space_tag_t bst, bus_space_handle_t bsh, unsigned i,
    199   1.2  riastrad     gen8_pte_t pte)
    200   1.2  riastrad {
    201   1.2  riastrad         CTASSERT(_BYTE_ORDER == _LITTLE_ENDIAN); /* x86 */
    202   1.2  riastrad         CTASSERT(sizeof(gen8_pte_t) == 8);
    203   1.2  riastrad #ifdef _LP64                    /* XXX How to detect bus_space_write_8?  */
    204   1.2  riastrad         bus_space_write_8(bst, bsh, 8*i, pte);
    205   1.2  riastrad #else
    206   1.2  riastrad         bus_space_write_4(bst, bsh, 8*i, (uint32_t)pte);
    207   1.2  riastrad         bus_space_write_4(bst, bsh, 8*i + 4, (uint32_t)(pte >> 32));
    208   1.2  riastrad #endif
    209   1.2  riastrad }
    210   1.2  riastrad #else
    211   1.1  riastrad static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
    212   1.1  riastrad {
    213   1.1  riastrad 	writeq(pte, addr);
    214   1.1  riastrad }
    215   1.2  riastrad #endif
    216   1.1  riastrad 
    217   1.1  riastrad static void gen8_ggtt_insert_page(struct i915_address_space *vm,
    218   1.1  riastrad 				  dma_addr_t addr,
    219   1.1  riastrad 				  u64 offset,
    220   1.1  riastrad 				  enum i915_cache_level level,
    221   1.1  riastrad 				  u32 unused)
    222   1.1  riastrad {
    223   1.1  riastrad 	struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
    224   1.7  riastrad #ifndef __NetBSD__
    225   1.1  riastrad 	gen8_pte_t __iomem *pte =
    226   1.1  riastrad 		(gen8_pte_t __iomem *)ggtt->gsm + offset / I915_GTT_PAGE_SIZE;
    227   1.7  riastrad #endif
    228   1.1  riastrad 
    229   1.7  riastrad #ifdef __NetBSD__
    230   1.7  riastrad 	gen8_set_pte(ggtt->gsmt, ggtt->gsmh, offset / I915_GTT_PAGE_SIZE,
    231   1.7  riastrad 	    gen8_pte_encode(addr, level, 0));
    232   1.7  riastrad #else
    233   1.1  riastrad 	gen8_set_pte(pte, gen8_pte_encode(addr, level, 0));
    234   1.7  riastrad #endif
    235   1.1  riastrad 
    236   1.1  riastrad 	ggtt->invalidate(ggtt);
    237   1.1  riastrad }
    238   1.1  riastrad 
    239   1.1  riastrad static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
    240   1.1  riastrad 				     struct i915_vma *vma,
    241   1.1  riastrad 				     enum i915_cache_level level,
    242   1.1  riastrad 				     u32 flags)
    243   1.1  riastrad {
    244   1.1  riastrad 	struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
    245   1.3  riastrad #ifdef __NetBSD__
    246   1.7  riastrad 	bus_dmamap_t map = vma->pages->sgl[0].sg_dmamap;
    247   1.3  riastrad 	unsigned seg;
    248   1.3  riastrad 	unsigned pgno;
    249   1.3  riastrad #else
    250   1.1  riastrad 	struct sgt_iter sgt_iter;
    251   1.1  riastrad 	gen8_pte_t __iomem *gtt_entries;
    252   1.3  riastrad #endif
    253   1.1  riastrad 	const gen8_pte_t pte_encode = gen8_pte_encode(0, level, 0);
    254   1.1  riastrad 	dma_addr_t addr;
    255   1.1  riastrad 
    256   1.1  riastrad 	/*
    257   1.1  riastrad 	 * Note that we ignore PTE_READ_ONLY here. The caller must be careful
    258   1.1  riastrad 	 * not to allow the user to override access to a read only page.
    259   1.1  riastrad 	 */
    260   1.1  riastrad 
    261   1.7  riastrad #ifdef __NetBSD__
    262   1.7  riastrad 	pgno = vma->node.start / I915_GTT_PAGE_SIZE;
    263   1.7  riastrad 	for (seg = 0; seg < map->dm_nsegs; seg++) {
    264   1.7  riastrad 		addr = map->dm_segs[seg].ds_addr;
    265   1.7  riastrad 		bus_size_t len = map->dm_segs[seg].ds_len;
    266   1.7  riastrad 		KASSERT((addr % I915_GTT_PAGE_SIZE) == 0);
    267   1.7  riastrad 		KASSERT((len % I915_GTT_PAGE_SIZE) == 0);
    268   1.7  riastrad 		for (;
    269   1.7  riastrad 		     len >= I915_GTT_PAGE_SIZE;
    270   1.7  riastrad 		     addr += I915_GTT_PAGE_SIZE, len -= I915_GTT_PAGE_SIZE) {
    271   1.7  riastrad 			gen8_set_pte(ggtt->gsmt, ggtt->gsmh, pgno++,
    272   1.7  riastrad 			    pte_encode | addr);
    273   1.7  riastrad 		}
    274   1.7  riastrad 		KASSERT(len == 0);
    275   1.7  riastrad 	}
    276   1.7  riastrad #else
    277   1.1  riastrad 	gtt_entries = (gen8_pte_t __iomem *)ggtt->gsm;
    278   1.1  riastrad 	gtt_entries += vma->node.start / I915_GTT_PAGE_SIZE;
    279   1.1  riastrad 	for_each_sgt_daddr(addr, sgt_iter, vma->pages)
    280   1.1  riastrad 		gen8_set_pte(gtt_entries++, pte_encode | addr);
    281   1.7  riastrad #endif
    282   1.1  riastrad 
    283   1.1  riastrad 	/*
    284   1.1  riastrad 	 * We want to flush the TLBs only after we're certain all the PTE
    285   1.1  riastrad 	 * updates have finished.
    286   1.1  riastrad 	 */
    287   1.1  riastrad 	ggtt->invalidate(ggtt);
    288   1.1  riastrad }
    289   1.1  riastrad 
    290   1.1  riastrad static void gen6_ggtt_insert_page(struct i915_address_space *vm,
    291   1.1  riastrad 				  dma_addr_t addr,
    292   1.1  riastrad 				  u64 offset,
    293   1.1  riastrad 				  enum i915_cache_level level,
    294   1.1  riastrad 				  u32 flags)
    295   1.1  riastrad {
    296   1.1  riastrad 	struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
    297   1.3  riastrad #ifndef __NetBSD__
    298   1.1  riastrad 	gen6_pte_t __iomem *pte =
    299   1.1  riastrad 		(gen6_pte_t __iomem *)ggtt->gsm + offset / I915_GTT_PAGE_SIZE;
    300   1.3  riastrad #endif
    301   1.1  riastrad 
    302   1.3  riastrad #ifdef __NetBSD__
    303  1.17  riastrad 	CTASSERT(sizeof(gen6_pte_t) == 4);
    304  1.17  riastrad 	bus_space_write_4(ggtt->gsmt, ggtt->gsmh,
    305  1.17  riastrad 	    sizeof(gen6_pte_t) * (offset / I915_GTT_PAGE_SIZE),
    306   1.3  riastrad 	    vm->pte_encode(addr, level, flags));
    307   1.3  riastrad #else
    308   1.1  riastrad 	iowrite32(vm->pte_encode(addr, level, flags), pte);
    309   1.3  riastrad #endif
    310   1.1  riastrad 
    311   1.1  riastrad 	ggtt->invalidate(ggtt);
    312   1.1  riastrad }
    313   1.1  riastrad 
    314   1.1  riastrad /*
    315   1.1  riastrad  * Binds an object into the global gtt with the specified cache level.
    316   1.1  riastrad  * The object will be accessible to the GPU via commands whose operands
    317   1.1  riastrad  * reference offsets within the global GTT as well as accessible by the GPU
    318   1.1  riastrad  * through the GMADR mapped BAR (i915->mm.gtt->gtt).
    319   1.1  riastrad  */
    320   1.2  riastrad 
    321   1.1  riastrad static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
    322   1.1  riastrad 				     struct i915_vma *vma,
    323   1.1  riastrad 				     enum i915_cache_level level,
    324   1.1  riastrad 				     u32 flags)
    325   1.1  riastrad {
    326   1.1  riastrad 	struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
    327   1.3  riastrad #ifdef __NetBSD__
    328   1.7  riastrad 	bus_dmamap_t map = vma->pages->sgl[0].sg_dmamap;
    329   1.3  riastrad 	unsigned seg;
    330   1.3  riastrad 	unsigned pgno;
    331   1.3  riastrad #else
    332   1.1  riastrad 	gen6_pte_t __iomem *entries = (gen6_pte_t __iomem *)ggtt->gsm;
    333   1.1  riastrad 	unsigned int i = vma->node.start / I915_GTT_PAGE_SIZE;
    334   1.1  riastrad 	struct sgt_iter iter;
    335   1.7  riastrad #endif
    336   1.1  riastrad 	dma_addr_t addr;
    337   1.1  riastrad 
    338   1.3  riastrad #ifdef __NetBSD__
    339   1.3  riastrad 	pgno = vma->node.start >> PAGE_SHIFT;
    340   1.3  riastrad 	for (seg = 0; seg < map->dm_nsegs; seg++) {
    341   1.4  riastrad 		addr = map->dm_segs[seg].ds_addr;
    342   1.3  riastrad 		bus_size_t len = map->dm_segs[seg].ds_len;
    343   1.7  riastrad 		KASSERT((addr % I915_GTT_PAGE_SIZE) == 0);
    344   1.7  riastrad 		KASSERT((len % I915_GTT_PAGE_SIZE) == 0);
    345   1.7  riastrad 		for (;
    346   1.7  riastrad 		     len >= I915_GTT_PAGE_SIZE;
    347   1.7  riastrad 		     addr += I915_GTT_PAGE_SIZE, len -= I915_GTT_PAGE_SIZE) {
    348   1.3  riastrad 			/* XXX KASSERT(pgno < ...)?  */
    349   1.3  riastrad 			CTASSERT(sizeof(gen6_pte_t) == 4);
    350   1.3  riastrad 			bus_space_write_4(ggtt->gsmt, ggtt->gsmh,
    351   1.3  riastrad 			    sizeof(gen6_pte_t) * pgno++,
    352   1.3  riastrad 			    vm->pte_encode(addr, level, flags));
    353   1.3  riastrad 		}
    354   1.3  riastrad 		KASSERT(len == 0);
    355   1.3  riastrad 		/* XXX KASSERT(pgno <= ...)?  */
    356   1.3  riastrad 	}
    357   1.3  riastrad #else
    358   1.1  riastrad 	for_each_sgt_daddr(addr, iter, vma->pages)
    359   1.1  riastrad 		iowrite32(vm->pte_encode(addr, level, flags), &entries[i++]);
    360   1.3  riastrad #endif
    361   1.1  riastrad 
    362   1.1  riastrad 	/*
    363   1.1  riastrad 	 * We want to flush the TLBs only after we're certain all the PTE
    364   1.1  riastrad 	 * updates have finished.
    365   1.1  riastrad 	 */
    366   1.1  riastrad 	ggtt->invalidate(ggtt);
    367   1.1  riastrad }
    368   1.1  riastrad 
    369   1.1  riastrad static void nop_clear_range(struct i915_address_space *vm,
    370   1.1  riastrad 			    u64 start, u64 length)
    371   1.1  riastrad {
    372   1.1  riastrad }
    373   1.1  riastrad 
    374   1.1  riastrad static void gen8_ggtt_clear_range(struct i915_address_space *vm,
    375   1.1  riastrad 				  u64 start, u64 length)
    376   1.1  riastrad {
    377   1.1  riastrad 	struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
    378   1.1  riastrad 	unsigned int first_entry = start / I915_GTT_PAGE_SIZE;
    379   1.1  riastrad 	unsigned int num_entries = length / I915_GTT_PAGE_SIZE;
    380   1.1  riastrad 	const gen8_pte_t scratch_pte = vm->scratch[0].encode;
    381   1.3  riastrad #ifndef __NetBSD__
    382   1.1  riastrad 	gen8_pte_t __iomem *gtt_base =
    383   1.1  riastrad 		(gen8_pte_t __iomem *)ggtt->gsm + first_entry;
    384   1.2  riastrad #endif
    385   1.1  riastrad 	const int max_entries = ggtt_total_entries(ggtt) - first_entry;
    386   1.1  riastrad 	int i;
    387   1.1  riastrad 
    388   1.1  riastrad 	if (WARN(num_entries > max_entries,
    389   1.1  riastrad 		 "First entry = %d; Num entries = %d (max=%d)\n",
    390   1.1  riastrad 		 first_entry, num_entries, max_entries))
    391   1.1  riastrad 		num_entries = max_entries;
    392   1.1  riastrad 
    393   1.2  riastrad #ifdef __NetBSD__
    394   1.2  riastrad 	for (i = 0; i < num_entries; i++)
    395  1.19  riastrad 		gen8_set_pte(ggtt->gsmt, ggtt->gsmh, first_entry + i,
    396   1.3  riastrad 		    scratch_pte);
    397   1.2  riastrad #else
    398   1.1  riastrad 	for (i = 0; i < num_entries; i++)
    399   1.1  riastrad 		gen8_set_pte(&gtt_base[i], scratch_pte);
    400   1.2  riastrad #endif
    401   1.1  riastrad }
    402   1.1  riastrad 
    403   1.1  riastrad static void bxt_vtd_ggtt_wa(struct i915_address_space *vm)
    404   1.1  riastrad {
    405   1.1  riastrad 	/*
    406   1.1  riastrad 	 * Make sure the internal GAM fifo has been cleared of all GTT
    407   1.1  riastrad 	 * writes before exiting stop_machine(). This guarantees that
    408   1.1  riastrad 	 * any aperture accesses waiting to start in another process
    409   1.1  riastrad 	 * cannot back up behind the GTT writes causing a hang.
    410   1.1  riastrad 	 * The register can be any arbitrary GAM register.
    411   1.1  riastrad 	 */
    412   1.1  riastrad 	intel_uncore_posting_read_fw(vm->gt->uncore, GFX_FLSH_CNTL_GEN6);
    413   1.1  riastrad }
    414   1.1  riastrad 
    415   1.1  riastrad struct insert_page {
    416   1.1  riastrad 	struct i915_address_space *vm;
    417   1.1  riastrad 	dma_addr_t addr;
    418   1.1  riastrad 	u64 offset;
    419   1.1  riastrad 	enum i915_cache_level level;
    420   1.1  riastrad };
    421   1.1  riastrad 
    422   1.1  riastrad static int bxt_vtd_ggtt_insert_page__cb(void *_arg)
    423   1.1  riastrad {
    424   1.1  riastrad 	struct insert_page *arg = _arg;
    425   1.1  riastrad 
    426   1.1  riastrad 	gen8_ggtt_insert_page(arg->vm, arg->addr, arg->offset, arg->level, 0);
    427   1.1  riastrad 	bxt_vtd_ggtt_wa(arg->vm);
    428   1.1  riastrad 
    429   1.1  riastrad 	return 0;
    430   1.1  riastrad }
    431   1.1  riastrad 
    432   1.1  riastrad static void bxt_vtd_ggtt_insert_page__BKL(struct i915_address_space *vm,
    433   1.1  riastrad 					  dma_addr_t addr,
    434   1.1  riastrad 					  u64 offset,
    435   1.1  riastrad 					  enum i915_cache_level level,
    436   1.1  riastrad 					  u32 unused)
    437   1.1  riastrad {
    438   1.1  riastrad 	struct insert_page arg = { vm, addr, offset, level };
    439   1.1  riastrad 
    440   1.1  riastrad 	stop_machine(bxt_vtd_ggtt_insert_page__cb, &arg, NULL);
    441   1.1  riastrad }
    442   1.1  riastrad 
    443   1.1  riastrad struct insert_entries {
    444   1.1  riastrad 	struct i915_address_space *vm;
    445   1.1  riastrad 	struct i915_vma *vma;
    446   1.1  riastrad 	enum i915_cache_level level;
    447   1.1  riastrad 	u32 flags;
    448   1.1  riastrad };
    449   1.1  riastrad 
    450   1.1  riastrad static int bxt_vtd_ggtt_insert_entries__cb(void *_arg)
    451   1.1  riastrad {
    452   1.1  riastrad 	struct insert_entries *arg = _arg;
    453   1.1  riastrad 
    454   1.1  riastrad 	gen8_ggtt_insert_entries(arg->vm, arg->vma, arg->level, arg->flags);
    455   1.1  riastrad 	bxt_vtd_ggtt_wa(arg->vm);
    456   1.1  riastrad 
    457   1.1  riastrad 	return 0;
    458   1.1  riastrad }
    459   1.1  riastrad 
    460   1.1  riastrad static void bxt_vtd_ggtt_insert_entries__BKL(struct i915_address_space *vm,
    461   1.1  riastrad 					     struct i915_vma *vma,
    462   1.1  riastrad 					     enum i915_cache_level level,
    463   1.1  riastrad 					     u32 flags)
    464   1.1  riastrad {
    465   1.1  riastrad 	struct insert_entries arg = { vm, vma, level, flags };
    466   1.1  riastrad 
    467   1.1  riastrad 	stop_machine(bxt_vtd_ggtt_insert_entries__cb, &arg, NULL);
    468   1.1  riastrad }
    469   1.1  riastrad 
    470   1.1  riastrad struct clear_range {
    471   1.1  riastrad 	struct i915_address_space *vm;
    472   1.1  riastrad 	u64 start;
    473   1.1  riastrad 	u64 length;
    474   1.1  riastrad };
    475   1.1  riastrad 
    476   1.1  riastrad static int bxt_vtd_ggtt_clear_range__cb(void *_arg)
    477   1.1  riastrad {
    478   1.1  riastrad 	struct clear_range *arg = _arg;
    479   1.1  riastrad 
    480   1.1  riastrad 	gen8_ggtt_clear_range(arg->vm, arg->start, arg->length);
    481   1.1  riastrad 	bxt_vtd_ggtt_wa(arg->vm);
    482   1.1  riastrad 
    483   1.1  riastrad 	return 0;
    484   1.1  riastrad }
    485   1.1  riastrad 
    486   1.1  riastrad static void bxt_vtd_ggtt_clear_range__BKL(struct i915_address_space *vm,
    487   1.1  riastrad 					  u64 start,
    488   1.1  riastrad 					  u64 length)
    489   1.1  riastrad {
    490   1.1  riastrad 	struct clear_range arg = { vm, start, length };
    491   1.1  riastrad 
    492   1.1  riastrad 	stop_machine(bxt_vtd_ggtt_clear_range__cb, &arg, NULL);
    493   1.1  riastrad }
    494   1.1  riastrad 
    495   1.1  riastrad static void gen6_ggtt_clear_range(struct i915_address_space *vm,
    496   1.1  riastrad 				  u64 start, u64 length)
    497   1.1  riastrad {
    498   1.1  riastrad 	struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
    499   1.1  riastrad 	unsigned int first_entry = start / I915_GTT_PAGE_SIZE;
    500   1.1  riastrad 	unsigned int num_entries = length / I915_GTT_PAGE_SIZE;
    501   1.2  riastrad #ifdef __NetBSD__
    502   1.2  riastrad 	gen6_pte_t scratch_pte;
    503   1.2  riastrad #else
    504   1.1  riastrad 	gen6_pte_t scratch_pte, __iomem *gtt_base =
    505   1.1  riastrad 		(gen6_pte_t __iomem *)ggtt->gsm + first_entry;
    506   1.2  riastrad #endif
    507   1.1  riastrad 	const int max_entries = ggtt_total_entries(ggtt) - first_entry;
    508   1.1  riastrad 	int i;
    509   1.1  riastrad 
    510   1.1  riastrad 	if (WARN(num_entries > max_entries,
    511   1.1  riastrad 		 "First entry = %d; Num entries = %d (max=%d)\n",
    512   1.1  riastrad 		 first_entry, num_entries, max_entries))
    513   1.1  riastrad 		num_entries = max_entries;
    514   1.1  riastrad 
    515   1.1  riastrad 	scratch_pte = vm->scratch[0].encode;
    516   1.2  riastrad #ifdef __NetBSD__
    517   1.2  riastrad         CTASSERT(sizeof(gen6_pte_t) == 4);
    518   1.2  riastrad         for (i = 0; i < num_entries; i++)
    519   1.3  riastrad 		bus_space_write_4(ggtt->gsmt, ggtt->gsmh,
    520   1.3  riastrad 		    sizeof(gen6_pte_t) * (first_entry + i),
    521   1.3  riastrad 		    scratch_pte);
    522   1.2  riastrad #else
    523   1.1  riastrad 	for (i = 0; i < num_entries; i++)
    524   1.1  riastrad 		iowrite32(scratch_pte, &gtt_base[i]);
    525   1.2  riastrad #endif
    526   1.1  riastrad }
    527   1.1  riastrad 
    528   1.1  riastrad static void i915_ggtt_insert_page(struct i915_address_space *vm,
    529   1.1  riastrad 				  dma_addr_t addr,
    530   1.1  riastrad 				  u64 offset,
    531   1.1  riastrad 				  enum i915_cache_level cache_level,
    532   1.1  riastrad 				  u32 unused)
    533   1.1  riastrad {
    534   1.1  riastrad 	unsigned int flags = (cache_level == I915_CACHE_NONE) ?
    535   1.1  riastrad 		AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
    536   1.1  riastrad 
    537   1.1  riastrad 	intel_gtt_insert_page(addr, offset >> PAGE_SHIFT, flags);
    538   1.1  riastrad }
    539   1.1  riastrad 
    540   1.1  riastrad static void i915_ggtt_insert_entries(struct i915_address_space *vm,
    541   1.1  riastrad 				     struct i915_vma *vma,
    542   1.1  riastrad 				     enum i915_cache_level cache_level,
    543   1.1  riastrad 				     u32 unused)
    544   1.1  riastrad {
    545   1.1  riastrad 	unsigned int flags = (cache_level == I915_CACHE_NONE) ?
    546   1.1  riastrad 		AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
    547   1.1  riastrad 
    548   1.1  riastrad 	intel_gtt_insert_sg_entries(vma->pages, vma->node.start >> PAGE_SHIFT,
    549   1.1  riastrad 				    flags);
    550   1.1  riastrad }
    551   1.1  riastrad 
    552   1.1  riastrad static void i915_ggtt_clear_range(struct i915_address_space *vm,
    553   1.1  riastrad 				  u64 start, u64 length)
    554   1.1  riastrad {
    555   1.1  riastrad 	intel_gtt_clear_range(start >> PAGE_SHIFT, length >> PAGE_SHIFT);
    556   1.1  riastrad }
    557   1.1  riastrad 
    558   1.1  riastrad static int ggtt_bind_vma(struct i915_vma *vma,
    559   1.1  riastrad 			 enum i915_cache_level cache_level,
    560   1.1  riastrad 			 u32 flags)
    561   1.1  riastrad {
    562   1.1  riastrad 	struct drm_i915_gem_object *obj = vma->obj;
    563   1.1  riastrad 	u32 pte_flags;
    564   1.1  riastrad 
    565   1.1  riastrad 	/* Applicable to VLV (gen8+ do not support RO in the GGTT) */
    566   1.1  riastrad 	pte_flags = 0;
    567   1.1  riastrad 	if (i915_gem_object_is_readonly(obj))
    568   1.1  riastrad 		pte_flags |= PTE_READ_ONLY;
    569   1.1  riastrad 
    570   1.1  riastrad 	vma->vm->insert_entries(vma->vm, vma, cache_level, pte_flags);
    571   1.1  riastrad 
    572   1.1  riastrad 	vma->page_sizes.gtt = I915_GTT_PAGE_SIZE;
    573   1.1  riastrad 
    574   1.1  riastrad 	/*
    575   1.1  riastrad 	 * Without aliasing PPGTT there's no difference between
    576   1.1  riastrad 	 * GLOBAL/LOCAL_BIND, it's all the same ptes. Hence unconditionally
    577   1.1  riastrad 	 * upgrade to both bound if we bind either to avoid double-binding.
    578   1.1  riastrad 	 */
    579   1.1  riastrad 	atomic_or(I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND, &vma->flags);
    580   1.1  riastrad 
    581   1.1  riastrad 	return 0;
    582   1.1  riastrad }
    583   1.1  riastrad 
    584   1.1  riastrad static void ggtt_unbind_vma(struct i915_vma *vma)
    585   1.1  riastrad {
    586   1.1  riastrad 	vma->vm->clear_range(vma->vm, vma->node.start, vma->size);
    587   1.1  riastrad }
    588   1.1  riastrad 
    589   1.1  riastrad static int ggtt_reserve_guc_top(struct i915_ggtt *ggtt)
    590   1.1  riastrad {
    591   1.1  riastrad 	u64 size;
    592   1.1  riastrad 	int ret;
    593   1.1  riastrad 
    594   1.1  riastrad 	if (!USES_GUC(ggtt->vm.i915))
    595   1.1  riastrad 		return 0;
    596   1.1  riastrad 
    597   1.1  riastrad 	GEM_BUG_ON(ggtt->vm.total <= GUC_GGTT_TOP);
    598   1.1  riastrad 	size = ggtt->vm.total - GUC_GGTT_TOP;
    599   1.1  riastrad 
    600   1.1  riastrad 	ret = i915_gem_gtt_reserve(&ggtt->vm, &ggtt->uc_fw, size,
    601   1.1  riastrad 				   GUC_GGTT_TOP, I915_COLOR_UNEVICTABLE,
    602   1.1  riastrad 				   PIN_NOEVICT);
    603   1.1  riastrad 	if (ret)
    604   1.1  riastrad 		DRM_DEBUG_DRIVER("Failed to reserve top of GGTT for GuC\n");
    605   1.1  riastrad 
    606   1.1  riastrad 	return ret;
    607   1.1  riastrad }
    608   1.1  riastrad 
    609   1.1  riastrad static void ggtt_release_guc_top(struct i915_ggtt *ggtt)
    610   1.1  riastrad {
    611   1.1  riastrad 	if (drm_mm_node_allocated(&ggtt->uc_fw))
    612   1.1  riastrad 		drm_mm_remove_node(&ggtt->uc_fw);
    613   1.1  riastrad }
    614   1.1  riastrad 
    615   1.1  riastrad static void cleanup_init_ggtt(struct i915_ggtt *ggtt)
    616   1.1  riastrad {
    617   1.1  riastrad 	ggtt_release_guc_top(ggtt);
    618   1.1  riastrad 	if (drm_mm_node_allocated(&ggtt->error_capture))
    619   1.1  riastrad 		drm_mm_remove_node(&ggtt->error_capture);
    620   1.1  riastrad 	mutex_destroy(&ggtt->error_mutex);
    621   1.1  riastrad }
    622   1.1  riastrad 
    623   1.1  riastrad static int init_ggtt(struct i915_ggtt *ggtt)
    624   1.1  riastrad {
    625   1.1  riastrad 	/*
    626   1.1  riastrad 	 * Let GEM Manage all of the aperture.
    627   1.1  riastrad 	 *
    628   1.1  riastrad 	 * However, leave one page at the end still bound to the scratch page.
    629   1.1  riastrad 	 * There are a number of places where the hardware apparently prefetches
    630   1.1  riastrad 	 * past the end of the object, and we've seen multiple hangs with the
    631   1.1  riastrad 	 * GPU head pointer stuck in a batchbuffer bound at the last page of the
    632   1.1  riastrad 	 * aperture.  One page should be enough to keep any prefetching inside
    633   1.1  riastrad 	 * of the aperture.
    634   1.1  riastrad 	 */
    635   1.1  riastrad 	unsigned long hole_start, hole_end;
    636   1.1  riastrad 	struct drm_mm_node *entry;
    637   1.1  riastrad 	int ret;
    638   1.1  riastrad 
    639   1.1  riastrad 	/*
    640   1.1  riastrad 	 * GuC requires all resources that we're sharing with it to be placed in
    641   1.1  riastrad 	 * non-WOPCM memory. If GuC is not present or not in use we still need a
    642   1.1  riastrad 	 * small bias as ring wraparound at offset 0 sometimes hangs. No idea
    643   1.1  riastrad 	 * why.
    644   1.1  riastrad 	 */
    645   1.1  riastrad 	ggtt->pin_bias = max_t(u32, I915_GTT_PAGE_SIZE,
    646   1.1  riastrad 			       intel_wopcm_guc_size(&ggtt->vm.i915->wopcm));
    647   1.1  riastrad 
    648   1.1  riastrad 	ret = intel_vgt_balloon(ggtt);
    649   1.1  riastrad 	if (ret)
    650   1.1  riastrad 		return ret;
    651   1.1  riastrad 
    652   1.1  riastrad 	mutex_init(&ggtt->error_mutex);
    653   1.1  riastrad 	if (ggtt->mappable_end) {
    654   1.1  riastrad 		/* Reserve a mappable slot for our lockless error capture */
    655   1.1  riastrad 		ret = drm_mm_insert_node_in_range(&ggtt->vm.mm,
    656   1.1  riastrad 						  &ggtt->error_capture,
    657   1.1  riastrad 						  PAGE_SIZE, 0,
    658   1.1  riastrad 						  I915_COLOR_UNEVICTABLE,
    659   1.1  riastrad 						  0, ggtt->mappable_end,
    660   1.1  riastrad 						  DRM_MM_INSERT_LOW);
    661   1.1  riastrad 		if (ret)
    662   1.1  riastrad 			return ret;
    663   1.1  riastrad 	}
    664   1.1  riastrad 
    665   1.1  riastrad 	/*
    666   1.1  riastrad 	 * The upper portion of the GuC address space has a sizeable hole
    667   1.1  riastrad 	 * (several MB) that is inaccessible by GuC. Reserve this range within
    668   1.1  riastrad 	 * GGTT as it can comfortably hold GuC/HuC firmware images.
    669   1.1  riastrad 	 */
    670   1.1  riastrad 	ret = ggtt_reserve_guc_top(ggtt);
    671   1.1  riastrad 	if (ret)
    672   1.1  riastrad 		goto err;
    673   1.1  riastrad 
    674   1.1  riastrad 	/* Clear any non-preallocated blocks */
    675   1.1  riastrad 	drm_mm_for_each_hole(entry, &ggtt->vm.mm, hole_start, hole_end) {
    676   1.1  riastrad 		DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
    677   1.1  riastrad 			      hole_start, hole_end);
    678   1.1  riastrad 		ggtt->vm.clear_range(&ggtt->vm, hole_start,
    679   1.1  riastrad 				     hole_end - hole_start);
    680   1.1  riastrad 	}
    681   1.1  riastrad 
    682   1.1  riastrad 	/* And finally clear the reserved guard page */
    683   1.1  riastrad 	ggtt->vm.clear_range(&ggtt->vm, ggtt->vm.total - PAGE_SIZE, PAGE_SIZE);
    684   1.1  riastrad 
    685   1.1  riastrad 	return 0;
    686   1.1  riastrad 
    687   1.1  riastrad err:
    688   1.1  riastrad 	cleanup_init_ggtt(ggtt);
    689   1.1  riastrad 	return ret;
    690   1.1  riastrad }
    691   1.1  riastrad 
    692   1.1  riastrad static int aliasing_gtt_bind_vma(struct i915_vma *vma,
    693   1.1  riastrad 				 enum i915_cache_level cache_level,
    694   1.1  riastrad 				 u32 flags)
    695   1.1  riastrad {
    696   1.1  riastrad 	u32 pte_flags;
    697   1.1  riastrad 	int ret;
    698   1.1  riastrad 
    699   1.1  riastrad 	/* Currently applicable only to VLV */
    700   1.1  riastrad 	pte_flags = 0;
    701   1.1  riastrad 	if (i915_gem_object_is_readonly(vma->obj))
    702   1.1  riastrad 		pte_flags |= PTE_READ_ONLY;
    703   1.1  riastrad 
    704   1.1  riastrad 	if (flags & I915_VMA_LOCAL_BIND) {
    705   1.1  riastrad 		struct i915_ppgtt *alias = i915_vm_to_ggtt(vma->vm)->alias;
    706   1.1  riastrad 
    707   1.1  riastrad 		if (flags & I915_VMA_ALLOC) {
    708   1.1  riastrad 			ret = alias->vm.allocate_va_range(&alias->vm,
    709   1.1  riastrad 							  vma->node.start,
    710   1.1  riastrad 							  vma->size);
    711   1.1  riastrad 			if (ret)
    712   1.1  riastrad 				return ret;
    713   1.1  riastrad 
    714   1.1  riastrad 			set_bit(I915_VMA_ALLOC_BIT, __i915_vma_flags(vma));
    715   1.1  riastrad 		}
    716   1.1  riastrad 
    717   1.1  riastrad 		GEM_BUG_ON(!test_bit(I915_VMA_ALLOC_BIT,
    718   1.1  riastrad 				     __i915_vma_flags(vma)));
    719   1.1  riastrad 		alias->vm.insert_entries(&alias->vm, vma,
    720   1.1  riastrad 					 cache_level, pte_flags);
    721   1.1  riastrad 	}
    722   1.1  riastrad 
    723   1.1  riastrad 	if (flags & I915_VMA_GLOBAL_BIND)
    724   1.1  riastrad 		vma->vm->insert_entries(vma->vm, vma, cache_level, pte_flags);
    725   1.1  riastrad 
    726   1.1  riastrad 	return 0;
    727   1.1  riastrad }
    728   1.1  riastrad 
    729   1.1  riastrad static void aliasing_gtt_unbind_vma(struct i915_vma *vma)
    730   1.1  riastrad {
    731   1.1  riastrad 	if (i915_vma_is_bound(vma, I915_VMA_GLOBAL_BIND)) {
    732   1.1  riastrad 		struct i915_address_space *vm = vma->vm;
    733   1.1  riastrad 
    734   1.1  riastrad 		vm->clear_range(vm, vma->node.start, vma->size);
    735   1.1  riastrad 	}
    736   1.1  riastrad 
    737   1.1  riastrad 	if (test_and_clear_bit(I915_VMA_ALLOC_BIT, __i915_vma_flags(vma))) {
    738   1.1  riastrad 		struct i915_address_space *vm =
    739   1.1  riastrad 			&i915_vm_to_ggtt(vma->vm)->alias->vm;
    740   1.1  riastrad 
    741   1.1  riastrad 		vm->clear_range(vm, vma->node.start, vma->size);
    742   1.1  riastrad 	}
    743   1.1  riastrad }
    744   1.1  riastrad 
    745   1.1  riastrad static int init_aliasing_ppgtt(struct i915_ggtt *ggtt)
    746   1.1  riastrad {
    747   1.1  riastrad 	struct i915_ppgtt *ppgtt;
    748   1.1  riastrad 	int err;
    749   1.1  riastrad 
    750   1.1  riastrad 	ppgtt = i915_ppgtt_create(ggtt->vm.gt);
    751   1.1  riastrad 	if (IS_ERR(ppgtt))
    752   1.1  riastrad 		return PTR_ERR(ppgtt);
    753   1.1  riastrad 
    754   1.1  riastrad 	if (GEM_WARN_ON(ppgtt->vm.total < ggtt->vm.total)) {
    755   1.1  riastrad 		err = -ENODEV;
    756   1.1  riastrad 		goto err_ppgtt;
    757   1.1  riastrad 	}
    758   1.1  riastrad 
    759   1.1  riastrad 	/*
    760   1.1  riastrad 	 * Note we only pre-allocate as far as the end of the global
    761   1.1  riastrad 	 * GTT. On 48b / 4-level page-tables, the difference is very,
    762   1.1  riastrad 	 * very significant! We have to preallocate as GVT/vgpu does
    763   1.1  riastrad 	 * not like the page directory disappearing.
    764   1.1  riastrad 	 */
    765   1.1  riastrad 	err = ppgtt->vm.allocate_va_range(&ppgtt->vm, 0, ggtt->vm.total);
    766   1.1  riastrad 	if (err)
    767   1.1  riastrad 		goto err_ppgtt;
    768   1.1  riastrad 
    769   1.1  riastrad 	ggtt->alias = ppgtt;
    770   1.1  riastrad 	ggtt->vm.bind_async_flags |= ppgtt->vm.bind_async_flags;
    771   1.1  riastrad 
    772   1.1  riastrad 	GEM_BUG_ON(ggtt->vm.vma_ops.bind_vma != ggtt_bind_vma);
    773   1.1  riastrad 	ggtt->vm.vma_ops.bind_vma = aliasing_gtt_bind_vma;
    774   1.1  riastrad 
    775   1.1  riastrad 	GEM_BUG_ON(ggtt->vm.vma_ops.unbind_vma != ggtt_unbind_vma);
    776   1.1  riastrad 	ggtt->vm.vma_ops.unbind_vma = aliasing_gtt_unbind_vma;
    777   1.1  riastrad 
    778   1.1  riastrad 	return 0;
    779   1.1  riastrad 
    780   1.1  riastrad err_ppgtt:
    781   1.1  riastrad 	i915_vm_put(&ppgtt->vm);
    782   1.1  riastrad 	return err;
    783   1.1  riastrad }
    784   1.1  riastrad 
    785   1.1  riastrad static void fini_aliasing_ppgtt(struct i915_ggtt *ggtt)
    786   1.1  riastrad {
    787   1.1  riastrad 	struct i915_ppgtt *ppgtt;
    788   1.1  riastrad 
    789   1.1  riastrad 	ppgtt = fetch_and_zero(&ggtt->alias);
    790   1.1  riastrad 	if (!ppgtt)
    791   1.1  riastrad 		return;
    792   1.1  riastrad 
    793   1.1  riastrad 	i915_vm_put(&ppgtt->vm);
    794   1.1  riastrad 
    795   1.1  riastrad 	ggtt->vm.vma_ops.bind_vma   = ggtt_bind_vma;
    796   1.1  riastrad 	ggtt->vm.vma_ops.unbind_vma = ggtt_unbind_vma;
    797   1.1  riastrad }
    798   1.1  riastrad 
    799   1.1  riastrad int i915_init_ggtt(struct drm_i915_private *i915)
    800   1.1  riastrad {
    801   1.1  riastrad 	int ret;
    802   1.1  riastrad 
    803   1.1  riastrad 	ret = init_ggtt(&i915->ggtt);
    804   1.1  riastrad 	if (ret)
    805   1.1  riastrad 		return ret;
    806   1.1  riastrad 
    807   1.1  riastrad 	if (INTEL_PPGTT(i915) == INTEL_PPGTT_ALIASING) {
    808   1.1  riastrad 		ret = init_aliasing_ppgtt(&i915->ggtt);
    809   1.1  riastrad 		if (ret)
    810   1.1  riastrad 			cleanup_init_ggtt(&i915->ggtt);
    811   1.1  riastrad 	}
    812   1.1  riastrad 
    813   1.1  riastrad 	return 0;
    814   1.1  riastrad }
    815   1.1  riastrad 
    816   1.1  riastrad static void ggtt_cleanup_hw(struct i915_ggtt *ggtt)
    817   1.1  riastrad {
    818   1.1  riastrad 	struct i915_vma *vma, *vn;
    819   1.1  riastrad 
    820   1.1  riastrad 	atomic_set(&ggtt->vm.open, 0);
    821   1.1  riastrad 
    822   1.1  riastrad 	rcu_barrier(); /* flush the RCU'ed__i915_vm_release */
    823   1.1  riastrad 	flush_workqueue(ggtt->vm.i915->wq);
    824   1.1  riastrad 
    825   1.1  riastrad 	mutex_lock(&ggtt->vm.mutex);
    826   1.1  riastrad 
    827   1.1  riastrad 	list_for_each_entry_safe(vma, vn, &ggtt->vm.bound_list, vm_link)
    828   1.1  riastrad 		WARN_ON(__i915_vma_unbind(vma));
    829   1.1  riastrad 
    830   1.1  riastrad 	if (drm_mm_node_allocated(&ggtt->error_capture))
    831   1.1  riastrad 		drm_mm_remove_node(&ggtt->error_capture);
    832   1.1  riastrad 	mutex_destroy(&ggtt->error_mutex);
    833   1.1  riastrad 
    834   1.1  riastrad 	ggtt_release_guc_top(ggtt);
    835   1.1  riastrad 	intel_vgt_deballoon(ggtt);
    836   1.1  riastrad 
    837   1.1  riastrad 	ggtt->vm.cleanup(&ggtt->vm);
    838   1.1  riastrad 
    839   1.1  riastrad 	mutex_unlock(&ggtt->vm.mutex);
    840   1.1  riastrad 	i915_address_space_fini(&ggtt->vm);
    841   1.1  riastrad 
    842   1.5  riastrad #ifdef __NetBSD__
    843   1.5  riastrad 	if (ggtt->mappable_end)
    844   1.5  riastrad 		pmap_pv_untrack(ggtt->gmadr.start, ggtt->mappable_end);
    845   1.5  riastrad #endif
    846   1.5  riastrad 
    847   1.1  riastrad 	arch_phys_wc_del(ggtt->mtrr);
    848   1.1  riastrad 
    849   1.1  riastrad 	if (ggtt->iomap.size)
    850   1.1  riastrad 		io_mapping_fini(&ggtt->iomap);
    851   1.1  riastrad }
    852   1.1  riastrad 
    853   1.1  riastrad /**
    854   1.1  riastrad  * i915_ggtt_driver_release - Clean up GGTT hardware initialization
    855   1.1  riastrad  * @i915: i915 device
    856   1.1  riastrad  */
    857   1.1  riastrad void i915_ggtt_driver_release(struct drm_i915_private *i915)
    858   1.1  riastrad {
    859   1.3  riastrad #ifndef __NetBSD__
    860   1.1  riastrad 	struct pagevec *pvec;
    861   1.3  riastrad #endif
    862   1.1  riastrad 
    863   1.1  riastrad 	fini_aliasing_ppgtt(&i915->ggtt);
    864   1.1  riastrad 
    865   1.1  riastrad 	ggtt_cleanup_hw(&i915->ggtt);
    866   1.1  riastrad 
    867   1.3  riastrad #ifndef __NetBSD__
    868   1.1  riastrad 	pvec = &i915->mm.wc_stash.pvec;
    869   1.1  riastrad 	if (pvec->nr) {
    870   1.1  riastrad 		set_pages_array_wb(pvec->pages, pvec->nr);
    871   1.1  riastrad 		__pagevec_release(pvec);
    872   1.1  riastrad 	}
    873   1.3  riastrad #endif
    874   1.1  riastrad }
    875   1.1  riastrad 
    876   1.1  riastrad static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
    877   1.1  riastrad {
    878   1.1  riastrad 	snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
    879   1.1  riastrad 	snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
    880   1.1  riastrad 	return snb_gmch_ctl << 20;
    881   1.1  riastrad }
    882   1.1  riastrad 
    883   1.1  riastrad static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
    884   1.1  riastrad {
    885   1.1  riastrad 	bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
    886   1.1  riastrad 	bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
    887   1.1  riastrad 	if (bdw_gmch_ctl)
    888   1.1  riastrad 		bdw_gmch_ctl = 1 << bdw_gmch_ctl;
    889   1.1  riastrad 
    890   1.1  riastrad #ifdef CONFIG_X86_32
    891   1.1  riastrad 	/* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * I915_GTT_PAGE_SIZE */
    892   1.1  riastrad 	if (bdw_gmch_ctl > 4)
    893   1.1  riastrad 		bdw_gmch_ctl = 4;
    894   1.1  riastrad #endif
    895   1.1  riastrad 
    896   1.1  riastrad 	return bdw_gmch_ctl << 20;
    897   1.1  riastrad }
    898   1.1  riastrad 
    899   1.1  riastrad static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
    900   1.1  riastrad {
    901   1.1  riastrad 	gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
    902   1.1  riastrad 	gmch_ctrl &= SNB_GMCH_GGMS_MASK;
    903   1.1  riastrad 
    904   1.1  riastrad 	if (gmch_ctrl)
    905   1.1  riastrad 		return 1 << (20 + gmch_ctrl);
    906   1.1  riastrad 
    907   1.1  riastrad 	return 0;
    908   1.1  riastrad }
    909   1.1  riastrad 
    910   1.1  riastrad static int ggtt_probe_common(struct i915_ggtt *ggtt, u64 size)
    911   1.1  riastrad {
    912   1.1  riastrad 	struct drm_i915_private *i915 = ggtt->vm.i915;
    913   1.1  riastrad 	struct pci_dev *pdev = i915->drm.pdev;
    914   1.1  riastrad 	phys_addr_t phys_addr;
    915   1.1  riastrad 	int ret;
    916   1.1  riastrad 
    917   1.1  riastrad 	/* For Modern GENs the PTEs and register space are split in the BAR */
    918   1.1  riastrad 	phys_addr = pci_resource_start(pdev, 0) + pci_resource_len(pdev, 0) / 2;
    919   1.1  riastrad 
    920   1.1  riastrad 	/*
    921   1.1  riastrad 	 * On BXT+/CNL+ writes larger than 64 bit to the GTT pagetable range
    922   1.1  riastrad 	 * will be dropped. For WC mappings in general we have 64 byte burst
    923   1.1  riastrad 	 * writes when the WC buffer is flushed, so we can't use it, but have to
    924   1.1  riastrad 	 * resort to an uncached mapping. The WC issue is easily caught by the
    925   1.1  riastrad 	 * readback check when writing GTT PTE entries.
    926   1.1  riastrad 	 */
    927   1.3  riastrad #ifdef __NetBSD__
    928   1.3  riastrad     {
    929   1.3  riastrad 	int flags;
    930   1.3  riastrad 	if (IS_GEN9_LP(i915) || INTEL_GEN(i915) >= 10)
    931   1.3  riastrad 		flags = 0;
    932   1.3  riastrad 	else
    933   1.3  riastrad 		flags = BUS_SPACE_MAP_PREFETCHABLE;
    934   1.3  riastrad 	ggtt->gsmt = i915->drm.pdev->pd_pa.pa_memt;
    935   1.3  riastrad 	/* XXX errno NetBSD->Linux */
    936   1.3  riastrad 	ret = -bus_space_map(ggtt->gsmt, phys_addr, size, flags, &ggtt->gsmh);
    937   1.3  riastrad 	if (ret) {
    938   1.3  riastrad 		DRM_ERROR("Failed to map the ggtt page table: %d\n", ret);
    939   1.3  riastrad 		return ret;
    940   1.3  riastrad 	}
    941   1.3  riastrad 	ggtt->gsmsz = size;
    942   1.3  riastrad     }
    943   1.3  riastrad #else
    944   1.1  riastrad 	if (IS_GEN9_LP(i915) || INTEL_GEN(i915) >= 10)
    945   1.1  riastrad 		ggtt->gsm = ioremap(phys_addr, size);
    946   1.1  riastrad 	else
    947   1.1  riastrad 		ggtt->gsm = ioremap_wc(phys_addr, size);
    948   1.1  riastrad 	if (!ggtt->gsm) {
    949   1.1  riastrad 		DRM_ERROR("Failed to map the ggtt page table\n");
    950   1.1  riastrad 		return -ENOMEM;
    951   1.1  riastrad 	}
    952   1.3  riastrad #endif
    953   1.1  riastrad 
    954   1.1  riastrad 	ret = setup_scratch_page(&ggtt->vm, GFP_DMA32);
    955   1.1  riastrad 	if (ret) {
    956   1.1  riastrad 		DRM_ERROR("Scratch setup failed\n");
    957   1.1  riastrad 		/* iounmap will also get called at remove, but meh */
    958   1.3  riastrad #ifdef __NetBSD__
    959   1.3  riastrad 		KASSERT(ggtt->gsmsz == size);
    960   1.3  riastrad 		bus_space_unmap(ggtt->gsmt, ggtt->gsmh, ggtt->gsmsz);
    961   1.3  riastrad 		ggtt->gsmsz = 0;
    962   1.3  riastrad #else
    963   1.1  riastrad 		iounmap(ggtt->gsm);
    964   1.3  riastrad #endif
    965   1.1  riastrad 		return ret;
    966   1.1  riastrad 	}
    967   1.1  riastrad 
    968   1.1  riastrad 	ggtt->vm.scratch[0].encode =
    969   1.1  riastrad 		ggtt->vm.pte_encode(px_dma(&ggtt->vm.scratch[0]),
    970   1.1  riastrad 				    I915_CACHE_NONE, 0);
    971   1.1  riastrad 
    972   1.1  riastrad 	return 0;
    973   1.1  riastrad }
    974   1.1  riastrad 
    975   1.1  riastrad int ggtt_set_pages(struct i915_vma *vma)
    976   1.1  riastrad {
    977   1.1  riastrad 	int ret;
    978   1.1  riastrad 
    979   1.1  riastrad 	GEM_BUG_ON(vma->pages);
    980   1.1  riastrad 
    981   1.1  riastrad 	ret = i915_get_ggtt_vma_pages(vma);
    982   1.1  riastrad 	if (ret)
    983   1.1  riastrad 		return ret;
    984   1.1  riastrad 
    985   1.1  riastrad 	vma->page_sizes = vma->obj->mm.page_sizes;
    986   1.1  riastrad 
    987   1.1  riastrad 	return 0;
    988   1.1  riastrad }
    989   1.1  riastrad 
    990   1.1  riastrad static void gen6_gmch_remove(struct i915_address_space *vm)
    991   1.1  riastrad {
    992   1.1  riastrad 	struct i915_ggtt *ggtt = i915_vm_to_ggtt(vm);
    993   1.1  riastrad 
    994   1.3  riastrad #ifdef __NetBSD__
    995   1.3  riastrad 	if (ggtt->gsmsz) {
    996   1.3  riastrad 		bus_space_unmap(ggtt->gsmt, ggtt->gsmh, ggtt->gsmsz);
    997   1.3  riastrad 		ggtt->gsmsz = 0;
    998   1.3  riastrad 	}
    999   1.3  riastrad #else
   1000   1.1  riastrad 	iounmap(ggtt->gsm);
   1001   1.3  riastrad #endif
   1002   1.1  riastrad 	cleanup_scratch_page(vm);
   1003   1.1  riastrad }
   1004   1.1  riastrad 
   1005   1.1  riastrad static struct resource pci_resource(struct pci_dev *pdev, int bar)
   1006   1.1  riastrad {
   1007   1.1  riastrad 	return (struct resource)DEFINE_RES_MEM(pci_resource_start(pdev, bar),
   1008   1.1  riastrad 					       pci_resource_len(pdev, bar));
   1009   1.1  riastrad }
   1010   1.1  riastrad 
   1011   1.1  riastrad static int gen8_gmch_probe(struct i915_ggtt *ggtt)
   1012   1.1  riastrad {
   1013   1.1  riastrad 	struct drm_i915_private *i915 = ggtt->vm.i915;
   1014   1.1  riastrad 	struct pci_dev *pdev = i915->drm.pdev;
   1015   1.1  riastrad 	unsigned int size;
   1016   1.1  riastrad 	u16 snb_gmch_ctl;
   1017   1.1  riastrad 	int err;
   1018   1.1  riastrad 
   1019   1.1  riastrad 	/* TODO: We're not aware of mappable constraints on gen8 yet */
   1020   1.1  riastrad 	if (!IS_DGFX(i915)) {
   1021   1.1  riastrad 		ggtt->gmadr = pci_resource(pdev, 2);
   1022   1.1  riastrad 		ggtt->mappable_end = resource_size(&ggtt->gmadr);
   1023   1.1  riastrad 	}
   1024   1.1  riastrad 
   1025   1.3  riastrad #ifdef __NetBSD__
   1026   1.7  riastrad 	__USE(err);
   1027   1.3  riastrad 	ggtt->max_paddr = DMA_BIT_MASK(39);
   1028   1.3  riastrad #else
   1029   1.1  riastrad 	err = pci_set_dma_mask(pdev, DMA_BIT_MASK(39));
   1030   1.1  riastrad 	if (!err)
   1031   1.1  riastrad 		err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(39));
   1032   1.1  riastrad 	if (err)
   1033   1.1  riastrad 		DRM_ERROR("Can't set DMA mask/consistent mask (%d)\n", err);
   1034   1.3  riastrad #endif
   1035   1.1  riastrad 
   1036   1.1  riastrad 	pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
   1037   1.1  riastrad 	if (IS_CHERRYVIEW(i915))
   1038   1.1  riastrad 		size = chv_get_total_gtt_size(snb_gmch_ctl);
   1039   1.1  riastrad 	else
   1040   1.1  riastrad 		size = gen8_get_total_gtt_size(snb_gmch_ctl);
   1041   1.1  riastrad 
   1042   1.1  riastrad 	ggtt->vm.total = (size / sizeof(gen8_pte_t)) * I915_GTT_PAGE_SIZE;
   1043   1.1  riastrad 	ggtt->vm.cleanup = gen6_gmch_remove;
   1044   1.1  riastrad 	ggtt->vm.insert_page = gen8_ggtt_insert_page;
   1045   1.1  riastrad 	ggtt->vm.clear_range = nop_clear_range;
   1046   1.1  riastrad 	if (intel_scanout_needs_vtd_wa(i915))
   1047   1.1  riastrad 		ggtt->vm.clear_range = gen8_ggtt_clear_range;
   1048   1.1  riastrad 
   1049   1.1  riastrad 	ggtt->vm.insert_entries = gen8_ggtt_insert_entries;
   1050   1.1  riastrad 
   1051   1.1  riastrad 	/* Serialize GTT updates with aperture access on BXT if VT-d is on. */
   1052   1.1  riastrad 	if (intel_ggtt_update_needs_vtd_wa(i915) ||
   1053   1.1  riastrad 	    IS_CHERRYVIEW(i915) /* fails with concurrent use/update */) {
   1054   1.1  riastrad 		ggtt->vm.insert_entries = bxt_vtd_ggtt_insert_entries__BKL;
   1055   1.1  riastrad 		ggtt->vm.insert_page    = bxt_vtd_ggtt_insert_page__BKL;
   1056   1.1  riastrad 		if (ggtt->vm.clear_range != nop_clear_range)
   1057   1.1  riastrad 			ggtt->vm.clear_range = bxt_vtd_ggtt_clear_range__BKL;
   1058   1.1  riastrad 	}
   1059   1.1  riastrad 
   1060   1.1  riastrad 	ggtt->invalidate = gen8_ggtt_invalidate;
   1061   1.1  riastrad 
   1062   1.1  riastrad 	ggtt->vm.vma_ops.bind_vma    = ggtt_bind_vma;
   1063   1.1  riastrad 	ggtt->vm.vma_ops.unbind_vma  = ggtt_unbind_vma;
   1064   1.1  riastrad 	ggtt->vm.vma_ops.set_pages   = ggtt_set_pages;
   1065   1.1  riastrad 	ggtt->vm.vma_ops.clear_pages = clear_pages;
   1066   1.1  riastrad 
   1067   1.1  riastrad 	ggtt->vm.pte_encode = gen8_pte_encode;
   1068   1.1  riastrad 
   1069   1.1  riastrad 	setup_private_pat(ggtt->vm.gt->uncore);
   1070   1.1  riastrad 
   1071   1.1  riastrad 	return ggtt_probe_common(ggtt, size);
   1072   1.1  riastrad }
   1073   1.1  riastrad 
   1074   1.1  riastrad static u64 snb_pte_encode(dma_addr_t addr,
   1075   1.1  riastrad 			  enum i915_cache_level level,
   1076   1.1  riastrad 			  u32 flags)
   1077   1.1  riastrad {
   1078   1.1  riastrad 	gen6_pte_t pte = GEN6_PTE_ADDR_ENCODE(addr) | GEN6_PTE_VALID;
   1079   1.1  riastrad 
   1080   1.1  riastrad 	switch (level) {
   1081   1.1  riastrad 	case I915_CACHE_L3_LLC:
   1082   1.1  riastrad 	case I915_CACHE_LLC:
   1083   1.1  riastrad 		pte |= GEN6_PTE_CACHE_LLC;
   1084   1.1  riastrad 		break;
   1085   1.1  riastrad 	case I915_CACHE_NONE:
   1086   1.1  riastrad 		pte |= GEN6_PTE_UNCACHED;
   1087   1.1  riastrad 		break;
   1088   1.1  riastrad 	default:
   1089   1.1  riastrad 		MISSING_CASE(level);
   1090   1.1  riastrad 	}
   1091   1.1  riastrad 
   1092   1.1  riastrad 	return pte;
   1093   1.1  riastrad }
   1094   1.1  riastrad 
   1095   1.1  riastrad static u64 ivb_pte_encode(dma_addr_t addr,
   1096   1.1  riastrad 			  enum i915_cache_level level,
   1097   1.1  riastrad 			  u32 flags)
   1098   1.1  riastrad {
   1099   1.1  riastrad 	gen6_pte_t pte = GEN6_PTE_ADDR_ENCODE(addr) | GEN6_PTE_VALID;
   1100   1.1  riastrad 
   1101   1.1  riastrad 	switch (level) {
   1102   1.1  riastrad 	case I915_CACHE_L3_LLC:
   1103   1.1  riastrad 		pte |= GEN7_PTE_CACHE_L3_LLC;
   1104   1.1  riastrad 		break;
   1105   1.1  riastrad 	case I915_CACHE_LLC:
   1106   1.1  riastrad 		pte |= GEN6_PTE_CACHE_LLC;
   1107   1.1  riastrad 		break;
   1108   1.1  riastrad 	case I915_CACHE_NONE:
   1109   1.1  riastrad 		pte |= GEN6_PTE_UNCACHED;
   1110   1.1  riastrad 		break;
   1111   1.1  riastrad 	default:
   1112   1.1  riastrad 		MISSING_CASE(level);
   1113   1.1  riastrad 	}
   1114   1.1  riastrad 
   1115   1.1  riastrad 	return pte;
   1116   1.1  riastrad }
   1117   1.1  riastrad 
   1118   1.1  riastrad static u64 byt_pte_encode(dma_addr_t addr,
   1119   1.1  riastrad 			  enum i915_cache_level level,
   1120   1.1  riastrad 			  u32 flags)
   1121   1.1  riastrad {
   1122   1.1  riastrad 	gen6_pte_t pte = GEN6_PTE_ADDR_ENCODE(addr) | GEN6_PTE_VALID;
   1123   1.1  riastrad 
   1124   1.1  riastrad 	if (!(flags & PTE_READ_ONLY))
   1125   1.1  riastrad 		pte |= BYT_PTE_WRITEABLE;
   1126   1.1  riastrad 
   1127   1.1  riastrad 	if (level != I915_CACHE_NONE)
   1128   1.1  riastrad 		pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
   1129   1.1  riastrad 
   1130   1.1  riastrad 	return pte;
   1131   1.1  riastrad }
   1132   1.1  riastrad 
   1133   1.1  riastrad static u64 hsw_pte_encode(dma_addr_t addr,
   1134   1.1  riastrad 			  enum i915_cache_level level,
   1135   1.1  riastrad 			  u32 flags)
   1136   1.1  riastrad {
   1137   1.1  riastrad 	gen6_pte_t pte = HSW_PTE_ADDR_ENCODE(addr) | GEN6_PTE_VALID;
   1138   1.1  riastrad 
   1139   1.1  riastrad 	if (level != I915_CACHE_NONE)
   1140   1.1  riastrad 		pte |= HSW_WB_LLC_AGE3;
   1141   1.1  riastrad 
   1142   1.1  riastrad 	return pte;
   1143   1.1  riastrad }
   1144   1.1  riastrad 
   1145   1.1  riastrad static u64 iris_pte_encode(dma_addr_t addr,
   1146   1.1  riastrad 			   enum i915_cache_level level,
   1147   1.1  riastrad 			   u32 flags)
   1148   1.1  riastrad {
   1149   1.1  riastrad 	gen6_pte_t pte = HSW_PTE_ADDR_ENCODE(addr) | GEN6_PTE_VALID;
   1150   1.1  riastrad 
   1151   1.1  riastrad 	switch (level) {
   1152   1.1  riastrad 	case I915_CACHE_NONE:
   1153   1.1  riastrad 		break;
   1154   1.1  riastrad 	case I915_CACHE_WT:
   1155   1.1  riastrad 		pte |= HSW_WT_ELLC_LLC_AGE3;
   1156   1.1  riastrad 		break;
   1157   1.1  riastrad 	default:
   1158   1.1  riastrad 		pte |= HSW_WB_ELLC_LLC_AGE3;
   1159   1.1  riastrad 		break;
   1160   1.1  riastrad 	}
   1161   1.1  riastrad 
   1162   1.1  riastrad 	return pte;
   1163   1.1  riastrad }
   1164   1.1  riastrad 
   1165   1.1  riastrad static int gen6_gmch_probe(struct i915_ggtt *ggtt)
   1166   1.1  riastrad {
   1167   1.1  riastrad 	struct drm_i915_private *i915 = ggtt->vm.i915;
   1168   1.1  riastrad 	struct pci_dev *pdev = i915->drm.pdev;
   1169   1.1  riastrad 	unsigned int size;
   1170   1.1  riastrad 	u16 snb_gmch_ctl;
   1171   1.1  riastrad 	int err;
   1172   1.1  riastrad 
   1173   1.1  riastrad 	ggtt->gmadr = pci_resource(pdev, 2);
   1174   1.1  riastrad 	ggtt->mappable_end = resource_size(&ggtt->gmadr);
   1175   1.1  riastrad 
   1176   1.1  riastrad 	/*
   1177   1.1  riastrad 	 * 64/512MB is the current min/max we actually know of, but this is
   1178   1.1  riastrad 	 * just a coarse sanity check.
   1179   1.1  riastrad 	 */
   1180   1.1  riastrad 	if (ggtt->mappable_end < (64<<20) || ggtt->mappable_end > (512<<20)) {
   1181   1.1  riastrad 		DRM_ERROR("Unknown GMADR size (%pa)\n", &ggtt->mappable_end);
   1182   1.1  riastrad 		return -ENXIO;
   1183   1.1  riastrad 	}
   1184   1.1  riastrad 
   1185   1.3  riastrad #ifdef __NetBSD__
   1186   1.7  riastrad 	__USE(err);
   1187   1.3  riastrad 	ggtt->max_paddr = DMA_BIT_MASK(40);
   1188   1.3  riastrad #else
   1189   1.1  riastrad 	err = pci_set_dma_mask(pdev, DMA_BIT_MASK(40));
   1190   1.1  riastrad 	if (!err)
   1191   1.1  riastrad 		err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
   1192   1.1  riastrad 	if (err)
   1193   1.1  riastrad 		DRM_ERROR("Can't set DMA mask/consistent mask (%d)\n", err);
   1194   1.3  riastrad #endif
   1195   1.1  riastrad 	pci_read_config_word(pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
   1196   1.1  riastrad 
   1197   1.1  riastrad 	size = gen6_get_total_gtt_size(snb_gmch_ctl);
   1198   1.1  riastrad 	ggtt->vm.total = (size / sizeof(gen6_pte_t)) * I915_GTT_PAGE_SIZE;
   1199   1.1  riastrad 
   1200   1.1  riastrad 	ggtt->vm.clear_range = nop_clear_range;
   1201   1.1  riastrad 	if (!HAS_FULL_PPGTT(i915) || intel_scanout_needs_vtd_wa(i915))
   1202   1.1  riastrad 		ggtt->vm.clear_range = gen6_ggtt_clear_range;
   1203   1.1  riastrad 	ggtt->vm.insert_page = gen6_ggtt_insert_page;
   1204   1.1  riastrad 	ggtt->vm.insert_entries = gen6_ggtt_insert_entries;
   1205   1.1  riastrad 	ggtt->vm.cleanup = gen6_gmch_remove;
   1206   1.1  riastrad 
   1207   1.1  riastrad 	ggtt->invalidate = gen6_ggtt_invalidate;
   1208   1.1  riastrad 
   1209   1.1  riastrad 	if (HAS_EDRAM(i915))
   1210   1.1  riastrad 		ggtt->vm.pte_encode = iris_pte_encode;
   1211   1.1  riastrad 	else if (IS_HASWELL(i915))
   1212   1.1  riastrad 		ggtt->vm.pte_encode = hsw_pte_encode;
   1213   1.1  riastrad 	else if (IS_VALLEYVIEW(i915))
   1214   1.1  riastrad 		ggtt->vm.pte_encode = byt_pte_encode;
   1215   1.1  riastrad 	else if (INTEL_GEN(i915) >= 7)
   1216   1.1  riastrad 		ggtt->vm.pte_encode = ivb_pte_encode;
   1217   1.1  riastrad 	else
   1218   1.1  riastrad 		ggtt->vm.pte_encode = snb_pte_encode;
   1219   1.1  riastrad 
   1220   1.1  riastrad 	ggtt->vm.vma_ops.bind_vma    = ggtt_bind_vma;
   1221   1.1  riastrad 	ggtt->vm.vma_ops.unbind_vma  = ggtt_unbind_vma;
   1222   1.1  riastrad 	ggtt->vm.vma_ops.set_pages   = ggtt_set_pages;
   1223   1.1  riastrad 	ggtt->vm.vma_ops.clear_pages = clear_pages;
   1224   1.1  riastrad 
   1225   1.1  riastrad 	return ggtt_probe_common(ggtt, size);
   1226   1.1  riastrad }
   1227   1.1  riastrad 
   1228   1.1  riastrad static void i915_gmch_remove(struct i915_address_space *vm)
   1229   1.1  riastrad {
   1230   1.1  riastrad 	intel_gmch_remove();
   1231   1.1  riastrad }
   1232   1.1  riastrad 
   1233   1.1  riastrad static int i915_gmch_probe(struct i915_ggtt *ggtt)
   1234   1.1  riastrad {
   1235   1.1  riastrad 	struct drm_i915_private *i915 = ggtt->vm.i915;
   1236   1.1  riastrad 	phys_addr_t gmadr_base;
   1237   1.1  riastrad 	int ret;
   1238   1.1  riastrad 
   1239   1.1  riastrad 	ret = intel_gmch_probe(i915->bridge_dev, i915->drm.pdev, NULL);
   1240   1.1  riastrad 	if (!ret) {
   1241   1.1  riastrad 		DRM_ERROR("failed to set up gmch\n");
   1242   1.1  riastrad 		return -EIO;
   1243   1.1  riastrad 	}
   1244   1.1  riastrad 
   1245   1.1  riastrad 	intel_gtt_get(&ggtt->vm.total, &gmadr_base, &ggtt->mappable_end);
   1246   1.1  riastrad 
   1247   1.7  riastrad 	ggtt->gmadr =
   1248   1.7  riastrad 		(struct resource)DEFINE_RES_MEM(gmadr_base, ggtt->mappable_end);
   1249   1.7  riastrad 
   1250   1.3  riastrad #ifdef __NetBSD__
   1251   1.5  riastrad 	/* Based on i915_drv.c, i915_driver_hw_probe.  */
   1252   1.7  riastrad 	if (IS_GEN(i915, 2))
   1253   1.5  riastrad 		ggtt->max_paddr = DMA_BIT_MASK(30);
   1254   1.7  riastrad 	else if (IS_I965G(i915) || IS_I965GM(i915))
   1255   1.5  riastrad 		ggtt->max_paddr = DMA_BIT_MASK(32);
   1256   1.3  riastrad 	else
   1257   1.5  riastrad 		ggtt->max_paddr = DMA_BIT_MASK(40);
   1258   1.3  riastrad #endif
   1259   1.1  riastrad 
   1260   1.1  riastrad 	ggtt->do_idle_maps = needs_idle_maps(i915);
   1261   1.1  riastrad 	ggtt->vm.insert_page = i915_ggtt_insert_page;
   1262   1.1  riastrad 	ggtt->vm.insert_entries = i915_ggtt_insert_entries;
   1263   1.1  riastrad 	ggtt->vm.clear_range = i915_ggtt_clear_range;
   1264   1.1  riastrad 	ggtt->vm.cleanup = i915_gmch_remove;
   1265   1.1  riastrad 
   1266   1.1  riastrad 	ggtt->invalidate = gmch_ggtt_invalidate;
   1267   1.1  riastrad 
   1268   1.1  riastrad 	ggtt->vm.vma_ops.bind_vma    = ggtt_bind_vma;
   1269   1.1  riastrad 	ggtt->vm.vma_ops.unbind_vma  = ggtt_unbind_vma;
   1270   1.1  riastrad 	ggtt->vm.vma_ops.set_pages   = ggtt_set_pages;
   1271   1.1  riastrad 	ggtt->vm.vma_ops.clear_pages = clear_pages;
   1272   1.1  riastrad 
   1273   1.1  riastrad 	if (unlikely(ggtt->do_idle_maps))
   1274   1.1  riastrad 		dev_notice(i915->drm.dev,
   1275   1.1  riastrad 			   "Applying Ironlake quirks for intel_iommu\n");
   1276   1.1  riastrad 
   1277   1.1  riastrad 	return 0;
   1278   1.1  riastrad }
   1279   1.1  riastrad 
   1280   1.1  riastrad static int ggtt_probe_hw(struct i915_ggtt *ggtt, struct intel_gt *gt)
   1281   1.1  riastrad {
   1282   1.1  riastrad 	struct drm_i915_private *i915 = gt->i915;
   1283   1.1  riastrad 	int ret;
   1284   1.1  riastrad 
   1285   1.1  riastrad 	ggtt->vm.gt = gt;
   1286   1.1  riastrad 	ggtt->vm.i915 = i915;
   1287   1.4  riastrad #ifdef __NetBSD__
   1288   1.4  riastrad 	ggtt->vm.dmat = i915->drm.dmat;
   1289   1.4  riastrad #else
   1290   1.1  riastrad 	ggtt->vm.dma = &i915->drm.pdev->dev;
   1291   1.4  riastrad #endif
   1292   1.1  riastrad 
   1293   1.1  riastrad 	if (INTEL_GEN(i915) <= 5)
   1294   1.1  riastrad 		ret = i915_gmch_probe(ggtt);
   1295   1.1  riastrad 	else if (INTEL_GEN(i915) < 8)
   1296   1.1  riastrad 		ret = gen6_gmch_probe(ggtt);
   1297   1.1  riastrad 	else
   1298   1.1  riastrad 		ret = gen8_gmch_probe(ggtt);
   1299   1.1  riastrad 	if (ret)
   1300   1.1  riastrad 		return ret;
   1301   1.1  riastrad 
   1302   1.5  riastrad #ifdef __NetBSD__
   1303   1.5  riastrad 	ggtt->pgfl = x86_select_freelist(ggtt->max_paddr);
   1304   1.5  riastrad 	ret = drm_limit_dma_space(&i915->drm, 0, ggtt->max_paddr);
   1305   1.5  riastrad 	if (ret) {
   1306   1.5  riastrad 		DRM_ERROR("Unable to limit DMA paddr allocations: %d\n", ret);
   1307   1.5  riastrad 		i915_ggtt_driver_release(i915);
   1308   1.5  riastrad 		return ret;
   1309   1.5  riastrad 	}
   1310   1.5  riastrad #endif
   1311   1.5  riastrad 
   1312   1.1  riastrad 	if ((ggtt->vm.total - 1) >> 32) {
   1313   1.1  riastrad 		DRM_ERROR("We never expected a Global GTT with more than 32bits"
   1314   1.7  riastrad 			  " of address space! Found %"PRId64"M!\n",
   1315   1.1  riastrad 			  ggtt->vm.total >> 20);
   1316   1.1  riastrad 		ggtt->vm.total = 1ULL << 32;
   1317   1.1  riastrad 		ggtt->mappable_end =
   1318   1.1  riastrad 			min_t(u64, ggtt->mappable_end, ggtt->vm.total);
   1319   1.1  riastrad 	}
   1320   1.1  riastrad 
   1321   1.1  riastrad 	if (ggtt->mappable_end > ggtt->vm.total) {
   1322   1.1  riastrad 		DRM_ERROR("mappable aperture extends past end of GGTT,"
   1323   1.7  riastrad 			  " aperture=%pa, total=%"PRIx64"\n",
   1324   1.1  riastrad 			  &ggtt->mappable_end, ggtt->vm.total);
   1325   1.1  riastrad 		ggtt->mappable_end = ggtt->vm.total;
   1326   1.1  riastrad 	}
   1327   1.1  riastrad 
   1328   1.1  riastrad 	/* GMADR is the PCI mmio aperture into the global GTT. */
   1329   1.9  riastrad 	DRM_DEBUG_DRIVER("GGTT size = %"PRIu64"M\n", ggtt->vm.total >> 20);
   1330   1.9  riastrad 	DRM_DEBUG_DRIVER("GMADR size = %"PRIu64"M\n", (u64)ggtt->mappable_end >> 20);
   1331   1.9  riastrad 	DRM_DEBUG_DRIVER("DSM size = %"PRIu64"M\n",
   1332   1.1  riastrad 			 (u64)resource_size(&intel_graphics_stolen_res) >> 20);
   1333   1.1  riastrad 
   1334   1.1  riastrad 	return 0;
   1335   1.1  riastrad }
   1336   1.1  riastrad 
   1337   1.1  riastrad /**
   1338   1.1  riastrad  * i915_ggtt_probe_hw - Probe GGTT hardware location
   1339   1.1  riastrad  * @i915: i915 device
   1340   1.1  riastrad  */
   1341   1.1  riastrad int i915_ggtt_probe_hw(struct drm_i915_private *i915)
   1342   1.1  riastrad {
   1343   1.1  riastrad 	int ret;
   1344   1.1  riastrad 
   1345   1.1  riastrad 	ret = ggtt_probe_hw(&i915->ggtt, &i915->gt);
   1346   1.1  riastrad 	if (ret)
   1347   1.1  riastrad 		return ret;
   1348   1.1  riastrad 
   1349   1.1  riastrad 	if (intel_vtd_active())
   1350   1.1  riastrad 		dev_info(i915->drm.dev, "VT-d active for gfx access\n");
   1351   1.1  riastrad 
   1352   1.1  riastrad 	return 0;
   1353   1.1  riastrad }
   1354   1.1  riastrad 
   1355   1.1  riastrad int i915_ggtt_enable_hw(struct drm_i915_private *i915)
   1356   1.1  riastrad {
   1357   1.1  riastrad 	if (INTEL_GEN(i915) < 6 && !intel_enable_gtt())
   1358   1.1  riastrad 		return -EIO;
   1359   1.1  riastrad 
   1360   1.1  riastrad 	return 0;
   1361   1.1  riastrad }
   1362   1.1  riastrad 
   1363   1.1  riastrad void i915_ggtt_enable_guc(struct i915_ggtt *ggtt)
   1364   1.1  riastrad {
   1365   1.1  riastrad 	GEM_BUG_ON(ggtt->invalidate != gen8_ggtt_invalidate);
   1366   1.1  riastrad 
   1367   1.1  riastrad 	ggtt->invalidate = guc_ggtt_invalidate;
   1368   1.1  riastrad 
   1369   1.1  riastrad 	ggtt->invalidate(ggtt);
   1370   1.1  riastrad }
   1371   1.1  riastrad 
   1372   1.1  riastrad void i915_ggtt_disable_guc(struct i915_ggtt *ggtt)
   1373   1.1  riastrad {
   1374   1.1  riastrad 	/* XXX Temporary pardon for error unload */
   1375   1.1  riastrad 	if (ggtt->invalidate == gen8_ggtt_invalidate)
   1376   1.1  riastrad 		return;
   1377   1.1  riastrad 
   1378   1.1  riastrad 	/* We should only be called after i915_ggtt_enable_guc() */
   1379   1.1  riastrad 	GEM_BUG_ON(ggtt->invalidate != guc_ggtt_invalidate);
   1380   1.1  riastrad 
   1381   1.1  riastrad 	ggtt->invalidate = gen8_ggtt_invalidate;
   1382   1.1  riastrad 
   1383   1.1  riastrad 	ggtt->invalidate(ggtt);
   1384   1.1  riastrad }
   1385   1.1  riastrad 
   1386   1.1  riastrad static void ggtt_restore_mappings(struct i915_ggtt *ggtt)
   1387   1.1  riastrad {
   1388   1.1  riastrad 	struct i915_vma *vma;
   1389   1.1  riastrad 	bool flush = false;
   1390   1.1  riastrad 	int open;
   1391   1.1  riastrad 
   1392   1.1  riastrad 	intel_gt_check_and_clear_faults(ggtt->vm.gt);
   1393   1.1  riastrad 
   1394   1.1  riastrad 	mutex_lock(&ggtt->vm.mutex);
   1395   1.1  riastrad 
   1396   1.1  riastrad 	/* First fill our portion of the GTT with scratch pages */
   1397   1.1  riastrad 	ggtt->vm.clear_range(&ggtt->vm, 0, ggtt->vm.total);
   1398   1.1  riastrad 
   1399   1.1  riastrad 	/* Skip rewriting PTE on VMA unbind. */
   1400   1.1  riastrad 	open = atomic_xchg(&ggtt->vm.open, 0);
   1401   1.1  riastrad 
   1402   1.1  riastrad 	/* clflush objects bound into the GGTT and rebind them. */
   1403   1.1  riastrad 	list_for_each_entry(vma, &ggtt->vm.bound_list, vm_link) {
   1404   1.1  riastrad 		struct drm_i915_gem_object *obj = vma->obj;
   1405   1.1  riastrad 
   1406   1.1  riastrad 		if (!i915_vma_is_bound(vma, I915_VMA_GLOBAL_BIND))
   1407   1.1  riastrad 			continue;
   1408   1.1  riastrad 
   1409   1.1  riastrad 		clear_bit(I915_VMA_GLOBAL_BIND_BIT, __i915_vma_flags(vma));
   1410   1.1  riastrad 		WARN_ON(i915_vma_bind(vma,
   1411   1.1  riastrad 				      obj ? obj->cache_level : 0,
   1412   1.1  riastrad 				      PIN_GLOBAL, NULL));
   1413   1.1  riastrad 		if (obj) { /* only used during resume => exclusive access */
   1414   1.1  riastrad 			flush |= fetch_and_zero(&obj->write_domain);
   1415   1.1  riastrad 			obj->read_domains |= I915_GEM_DOMAIN_GTT;
   1416   1.1  riastrad 		}
   1417   1.1  riastrad 	}
   1418   1.1  riastrad 
   1419   1.1  riastrad 	atomic_set(&ggtt->vm.open, open);
   1420   1.1  riastrad 	ggtt->invalidate(ggtt);
   1421   1.1  riastrad 
   1422   1.1  riastrad 	mutex_unlock(&ggtt->vm.mutex);
   1423   1.1  riastrad 
   1424   1.1  riastrad 	if (flush)
   1425   1.1  riastrad 		wbinvd_on_all_cpus();
   1426   1.1  riastrad }
   1427   1.1  riastrad 
   1428   1.1  riastrad void i915_gem_restore_gtt_mappings(struct drm_i915_private *i915)
   1429   1.1  riastrad {
   1430   1.1  riastrad 	struct i915_ggtt *ggtt = &i915->ggtt;
   1431   1.1  riastrad 
   1432   1.1  riastrad 	ggtt_restore_mappings(ggtt);
   1433   1.1  riastrad 
   1434   1.1  riastrad 	if (INTEL_GEN(i915) >= 8)
   1435   1.1  riastrad 		setup_private_pat(ggtt->vm.gt->uncore);
   1436   1.1  riastrad }
   1437   1.1  riastrad 
   1438   1.7  riastrad #ifndef __NetBSD__
   1439   1.7  riastrad 
   1440   1.1  riastrad static struct scatterlist *
   1441   1.1  riastrad rotate_pages(struct drm_i915_gem_object *obj, unsigned int offset,
   1442   1.1  riastrad 	     unsigned int width, unsigned int height,
   1443   1.1  riastrad 	     unsigned int stride,
   1444   1.1  riastrad 	     struct sg_table *st, struct scatterlist *sg)
   1445   1.1  riastrad {
   1446   1.1  riastrad 	unsigned int column, row;
   1447   1.1  riastrad 	unsigned int src_idx;
   1448   1.1  riastrad 
   1449   1.1  riastrad 	for (column = 0; column < width; column++) {
   1450   1.1  riastrad 		src_idx = stride * (height - 1) + column + offset;
   1451   1.1  riastrad 		for (row = 0; row < height; row++) {
   1452   1.1  riastrad 			st->nents++;
   1453   1.1  riastrad 			/*
   1454   1.1  riastrad 			 * We don't need the pages, but need to initialize
   1455   1.1  riastrad 			 * the entries so the sg list can be happily traversed.
   1456   1.1  riastrad 			 * The only thing we need are DMA addresses.
   1457   1.1  riastrad 			 */
   1458   1.1  riastrad 			sg_set_page(sg, NULL, I915_GTT_PAGE_SIZE, 0);
   1459   1.1  riastrad 			sg_dma_address(sg) =
   1460   1.1  riastrad 				i915_gem_object_get_dma_address(obj, src_idx);
   1461   1.1  riastrad 			sg_dma_len(sg) = I915_GTT_PAGE_SIZE;
   1462   1.1  riastrad 			sg = sg_next(sg);
   1463   1.1  riastrad 			src_idx -= stride;
   1464   1.1  riastrad 		}
   1465   1.1  riastrad 	}
   1466   1.1  riastrad 
   1467   1.1  riastrad 	return sg;
   1468   1.1  riastrad }
   1469   1.1  riastrad 
   1470   1.1  riastrad static noinline struct sg_table *
   1471   1.1  riastrad intel_rotate_pages(struct intel_rotation_info *rot_info,
   1472   1.1  riastrad 		   struct drm_i915_gem_object *obj)
   1473   1.1  riastrad {
   1474   1.1  riastrad 	unsigned int size = intel_rotation_info_size(rot_info);
   1475   1.1  riastrad 	struct sg_table *st;
   1476   1.1  riastrad 	struct scatterlist *sg;
   1477   1.1  riastrad 	int ret = -ENOMEM;
   1478   1.1  riastrad 	int i;
   1479   1.1  riastrad 
   1480   1.1  riastrad 	/* Allocate target SG list. */
   1481   1.1  riastrad 	st = kmalloc(sizeof(*st), GFP_KERNEL);
   1482   1.1  riastrad 	if (!st)
   1483   1.1  riastrad 		goto err_st_alloc;
   1484   1.1  riastrad 
   1485   1.1  riastrad 	ret = sg_alloc_table(st, size, GFP_KERNEL);
   1486   1.1  riastrad 	if (ret)
   1487   1.1  riastrad 		goto err_sg_alloc;
   1488   1.1  riastrad 
   1489   1.1  riastrad 	st->nents = 0;
   1490   1.1  riastrad 	sg = st->sgl;
   1491   1.1  riastrad 
   1492   1.1  riastrad 	for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++) {
   1493   1.1  riastrad 		sg = rotate_pages(obj, rot_info->plane[i].offset,
   1494   1.1  riastrad 				  rot_info->plane[i].width, rot_info->plane[i].height,
   1495   1.1  riastrad 				  rot_info->plane[i].stride, st, sg);
   1496   1.1  riastrad 	}
   1497   1.1  riastrad 
   1498   1.1  riastrad 	return st;
   1499   1.1  riastrad 
   1500   1.1  riastrad err_sg_alloc:
   1501   1.1  riastrad 	kfree(st);
   1502   1.1  riastrad err_st_alloc:
   1503   1.1  riastrad 
   1504   1.1  riastrad 	DRM_DEBUG_DRIVER("Failed to create rotated mapping for object size %zu! (%ux%u tiles, %u pages)\n",
   1505   1.1  riastrad 			 obj->base.size, rot_info->plane[0].width, rot_info->plane[0].height, size);
   1506   1.1  riastrad 
   1507   1.1  riastrad 	return ERR_PTR(ret);
   1508   1.1  riastrad }
   1509   1.1  riastrad 
   1510   1.1  riastrad static struct scatterlist *
   1511   1.1  riastrad remap_pages(struct drm_i915_gem_object *obj, unsigned int offset,
   1512   1.1  riastrad 	    unsigned int width, unsigned int height,
   1513   1.1  riastrad 	    unsigned int stride,
   1514   1.1  riastrad 	    struct sg_table *st, struct scatterlist *sg)
   1515   1.1  riastrad {
   1516   1.1  riastrad 	unsigned int row;
   1517   1.1  riastrad 
   1518   1.1  riastrad 	for (row = 0; row < height; row++) {
   1519   1.1  riastrad 		unsigned int left = width * I915_GTT_PAGE_SIZE;
   1520   1.1  riastrad 
   1521   1.1  riastrad 		while (left) {
   1522   1.1  riastrad 			dma_addr_t addr;
   1523   1.1  riastrad 			unsigned int length;
   1524   1.1  riastrad 
   1525   1.1  riastrad 			/*
   1526   1.1  riastrad 			 * We don't need the pages, but need to initialize
   1527   1.1  riastrad 			 * the entries so the sg list can be happily traversed.
   1528   1.1  riastrad 			 * The only thing we need are DMA addresses.
   1529   1.1  riastrad 			 */
   1530   1.1  riastrad 
   1531   1.1  riastrad 			addr = i915_gem_object_get_dma_address_len(obj, offset, &length);
   1532   1.1  riastrad 
   1533   1.1  riastrad 			length = min(left, length);
   1534   1.1  riastrad 
   1535   1.1  riastrad 			st->nents++;
   1536   1.1  riastrad 
   1537   1.1  riastrad 			sg_set_page(sg, NULL, length, 0);
   1538   1.1  riastrad 			sg_dma_address(sg) = addr;
   1539   1.1  riastrad 			sg_dma_len(sg) = length;
   1540   1.1  riastrad 			sg = sg_next(sg);
   1541   1.1  riastrad 
   1542   1.1  riastrad 			offset += length / I915_GTT_PAGE_SIZE;
   1543   1.1  riastrad 			left -= length;
   1544   1.1  riastrad 		}
   1545   1.1  riastrad 
   1546   1.1  riastrad 		offset += stride - width;
   1547   1.1  riastrad 	}
   1548   1.1  riastrad 
   1549   1.1  riastrad 	return sg;
   1550   1.1  riastrad }
   1551   1.1  riastrad 
   1552   1.1  riastrad static noinline struct sg_table *
   1553   1.1  riastrad intel_remap_pages(struct intel_remapped_info *rem_info,
   1554   1.1  riastrad 		  struct drm_i915_gem_object *obj)
   1555   1.1  riastrad {
   1556   1.1  riastrad 	unsigned int size = intel_remapped_info_size(rem_info);
   1557   1.1  riastrad 	struct sg_table *st;
   1558   1.1  riastrad 	struct scatterlist *sg;
   1559   1.1  riastrad 	int ret = -ENOMEM;
   1560   1.1  riastrad 	int i;
   1561   1.1  riastrad 
   1562   1.1  riastrad 	/* Allocate target SG list. */
   1563   1.1  riastrad 	st = kmalloc(sizeof(*st), GFP_KERNEL);
   1564   1.1  riastrad 	if (!st)
   1565   1.1  riastrad 		goto err_st_alloc;
   1566   1.1  riastrad 
   1567   1.1  riastrad 	ret = sg_alloc_table(st, size, GFP_KERNEL);
   1568   1.1  riastrad 	if (ret)
   1569   1.1  riastrad 		goto err_sg_alloc;
   1570   1.1  riastrad 
   1571   1.1  riastrad 	st->nents = 0;
   1572   1.1  riastrad 	sg = st->sgl;
   1573   1.1  riastrad 
   1574   1.1  riastrad 	for (i = 0 ; i < ARRAY_SIZE(rem_info->plane); i++) {
   1575   1.1  riastrad 		sg = remap_pages(obj, rem_info->plane[i].offset,
   1576   1.1  riastrad 				 rem_info->plane[i].width, rem_info->plane[i].height,
   1577   1.1  riastrad 				 rem_info->plane[i].stride, st, sg);
   1578   1.1  riastrad 	}
   1579   1.1  riastrad 
   1580   1.1  riastrad 	i915_sg_trim(st);
   1581   1.1  riastrad 
   1582   1.1  riastrad 	return st;
   1583   1.1  riastrad 
   1584   1.1  riastrad err_sg_alloc:
   1585   1.1  riastrad 	kfree(st);
   1586   1.1  riastrad err_st_alloc:
   1587   1.1  riastrad 
   1588   1.1  riastrad 	DRM_DEBUG_DRIVER("Failed to create remapped mapping for object size %zu! (%ux%u tiles, %u pages)\n",
   1589   1.1  riastrad 			 obj->base.size, rem_info->plane[0].width, rem_info->plane[0].height, size);
   1590   1.1  riastrad 
   1591   1.1  riastrad 	return ERR_PTR(ret);
   1592   1.1  riastrad }
   1593   1.1  riastrad 
   1594  1.10  riastrad #endif	/* __NetBSD__ */
   1595  1.10  riastrad 
   1596   1.1  riastrad static noinline struct sg_table *
   1597   1.1  riastrad intel_partial_pages(const struct i915_ggtt_view *view,
   1598   1.1  riastrad 		    struct drm_i915_gem_object *obj)
   1599   1.1  riastrad {
   1600  1.10  riastrad #ifdef __NetBSD__
   1601  1.10  riastrad 	struct sg_table *st = NULL;
   1602  1.10  riastrad 	int ret = -ENOMEM;
   1603  1.10  riastrad 
   1604  1.10  riastrad 	KASSERTMSG(view->partial.offset <= obj->base.size >> PAGE_SHIFT,
   1605  1.10  riastrad 	    "obj=%p size=0x%zx; view offset=0x%zx size=0x%zx",
   1606  1.10  riastrad 	    obj,
   1607  1.10  riastrad 	    (size_t)obj->base.size >> PAGE_SHIFT,
   1608  1.10  riastrad 	    (size_t)view->partial.offset,
   1609  1.10  riastrad 	    (size_t)view->partial.size);
   1610  1.10  riastrad 	KASSERTMSG((view->partial.size <=
   1611  1.10  riastrad 		(obj->base.size >> PAGE_SHIFT) - view->partial.offset),
   1612  1.10  riastrad 	    "obj=%p size=0x%zx; view offset=0x%zx size=0x%zx",
   1613  1.10  riastrad 	    obj,
   1614  1.10  riastrad 	    (size_t)obj->base.size >> PAGE_SHIFT,
   1615  1.10  riastrad 	    (size_t)view->partial.offset,
   1616  1.10  riastrad 	    (size_t)view->partial.size);
   1617  1.10  riastrad 	KASSERTMSG(view->partial.size <= INT_MAX, "view size=0x%zx",
   1618  1.10  riastrad 	    (size_t)view->partial.size);
   1619  1.10  riastrad 
   1620  1.10  riastrad 	st = kmalloc(sizeof(*st), GFP_KERNEL);
   1621  1.10  riastrad 	if (st == NULL)
   1622  1.10  riastrad 		goto fail;
   1623  1.10  riastrad 	ret = sg_alloc_table(st, view->partial.size, GFP_KERNEL);
   1624  1.10  riastrad 	if (ret) {
   1625  1.10  riastrad 		kfree(st);
   1626  1.10  riastrad 		st = NULL;
   1627  1.10  riastrad 		goto fail;
   1628  1.10  riastrad 	}
   1629  1.10  riastrad 
   1630  1.10  riastrad 	/* XXX errno NetBSD->Linux */
   1631  1.10  riastrad 	if (obj->mm.pages->sgl->sg_dmamap) { /* XXX KASSERT?  */
   1632  1.10  riastrad 		ret = -bus_dmamap_create(obj->base.dev->dmat,
   1633  1.10  riastrad 		    (bus_size_t)view->partial.size << PAGE_SHIFT,
   1634  1.10  riastrad 		    view->partial.size, PAGE_SIZE, 0, BUS_DMA_NOWAIT,
   1635  1.10  riastrad 		    &st->sgl->sg_dmamap);
   1636  1.11  riastrad 		if (ret) {
   1637  1.11  riastrad 			st->sgl->sg_dmamap = NULL;
   1638  1.10  riastrad 			goto fail;
   1639  1.11  riastrad 		}
   1640  1.11  riastrad 		st->sgl->sg_dmat = obj->base.dev->dmat;
   1641  1.10  riastrad 	}
   1642  1.10  riastrad 
   1643  1.10  riastrad 	/*
   1644  1.10  riastrad 	 * Copy over the pages.  The view's offset and size are in
   1645  1.10  riastrad 	 * units of pages already.
   1646  1.10  riastrad 	 */
   1647  1.10  riastrad 	KASSERT(st->sgl->sg_npgs == view->partial.size);
   1648  1.10  riastrad 	memcpy(st->sgl->sg_pgs,
   1649  1.10  riastrad 	    obj->mm.pages->sgl->sg_pgs + view->partial.offset,
   1650  1.10  riastrad 	    sizeof(st->sgl->sg_pgs[0]) * view->partial.size);
   1651  1.10  riastrad 
   1652  1.10  riastrad 	/*
   1653  1.10  riastrad 	 * Copy over the DMA addresses.  For simplicity, we don't do
   1654  1.10  riastrad 	 * anything to compress contiguous pages into larger segments.
   1655  1.10  riastrad 	 */
   1656  1.10  riastrad 	if (obj->mm.pages->sgl->sg_dmamap) {
   1657  1.12  riastrad 		bus_size_t offset = (bus_size_t)view->partial.offset
   1658  1.12  riastrad 		    << PAGE_SHIFT;
   1659  1.10  riastrad 		unsigned i, j, k;
   1660  1.10  riastrad 
   1661  1.10  riastrad 		st->sgl->sg_dmamap->dm_nsegs = view->partial.size;
   1662  1.10  riastrad 		for (i = j = 0; i < view->partial.size; j++) {
   1663  1.13  riastrad 			KASSERT(j < obj->mm.pages->sgl->sg_dmamap->dm_nsegs);
   1664  1.10  riastrad 			const bus_dma_segment_t *iseg =
   1665  1.10  riastrad 			    &obj->mm.pages->sgl->sg_dmamap->dm_segs[j];
   1666  1.12  riastrad 
   1667  1.10  riastrad 			KASSERT(iseg->ds_len % PAGE_SIZE == 0);
   1668  1.12  riastrad 
   1669  1.12  riastrad 			/* Skip segments prior to the start offset.  */
   1670  1.12  riastrad 			if (offset >= iseg->ds_len) {
   1671  1.12  riastrad 				offset -= iseg->ds_len;
   1672  1.12  riastrad 				continue;
   1673  1.12  riastrad 			}
   1674  1.15  riastrad 			for (k = 0;
   1675  1.15  riastrad 			     (i < view->partial.size &&
   1676  1.15  riastrad 				 k < iseg->ds_len >> PAGE_SHIFT);
   1677  1.15  riastrad 			     k++) {
   1678  1.14  riastrad 				KASSERT(i < view->partial.size);
   1679  1.10  riastrad 				bus_dma_segment_t *oseg =
   1680  1.10  riastrad 				    &st->sgl->sg_dmamap->dm_segs[i++];
   1681  1.12  riastrad 				oseg->ds_addr = iseg->ds_addr + offset +
   1682  1.12  riastrad 				    k*PAGE_SIZE;
   1683  1.10  riastrad 				oseg->ds_len = PAGE_SIZE;
   1684  1.10  riastrad 			}
   1685  1.12  riastrad 
   1686  1.12  riastrad 			/*
   1687  1.12  riastrad 			 * After the first segment which we possibly
   1688  1.12  riastrad 			 * use only a suffix of, the remainder we will
   1689  1.12  riastrad 			 * take from the beginning.
   1690  1.12  riastrad 			 */
   1691  1.12  riastrad 			offset = 0;
   1692  1.10  riastrad 		}
   1693  1.10  riastrad 	}
   1694  1.10  riastrad 
   1695  1.10  riastrad 	/* Success!  */
   1696  1.10  riastrad 	return st;
   1697  1.10  riastrad 
   1698  1.11  riastrad fail:	if (st) {
   1699  1.11  riastrad 		sg_free_table(st);
   1700  1.10  riastrad 		kfree(st);
   1701  1.11  riastrad 	}
   1702  1.10  riastrad 	return ERR_PTR(ret);
   1703  1.10  riastrad #else
   1704   1.1  riastrad 	struct sg_table *st;
   1705   1.1  riastrad 	struct scatterlist *sg, *iter;
   1706   1.1  riastrad 	unsigned int count = view->partial.size;
   1707   1.1  riastrad 	unsigned int offset;
   1708  1.10  riastrad 	int ret;
   1709   1.1  riastrad 
   1710   1.1  riastrad 	st = kmalloc(sizeof(*st), GFP_KERNEL);
   1711  1.10  riastrad 	if (!st) {
   1712  1.10  riastrad 		ret = -ENOMEM;
   1713   1.1  riastrad 		goto err_st_alloc;
   1714  1.10  riastrad 	}
   1715   1.1  riastrad 
   1716   1.1  riastrad 	ret = sg_alloc_table(st, count, GFP_KERNEL);
   1717   1.1  riastrad 	if (ret)
   1718   1.1  riastrad 		goto err_sg_alloc;
   1719   1.1  riastrad 
   1720   1.1  riastrad 	iter = i915_gem_object_get_sg(obj, view->partial.offset, &offset);
   1721   1.1  riastrad 	GEM_BUG_ON(!iter);
   1722   1.1  riastrad 
   1723   1.1  riastrad 	sg = st->sgl;
   1724   1.1  riastrad 	st->nents = 0;
   1725   1.1  riastrad 	do {
   1726   1.1  riastrad 		unsigned int len;
   1727   1.1  riastrad 
   1728   1.1  riastrad 		len = min(iter->length - (offset << PAGE_SHIFT),
   1729   1.1  riastrad 			  count << PAGE_SHIFT);
   1730   1.1  riastrad 		sg_set_page(sg, NULL, len, 0);
   1731   1.1  riastrad 		sg_dma_address(sg) =
   1732   1.1  riastrad 			sg_dma_address(iter) + (offset << PAGE_SHIFT);
   1733   1.1  riastrad 		sg_dma_len(sg) = len;
   1734   1.1  riastrad 
   1735   1.1  riastrad 		st->nents++;
   1736   1.1  riastrad 		count -= len >> PAGE_SHIFT;
   1737   1.1  riastrad 		if (count == 0) {
   1738   1.1  riastrad 			sg_mark_end(sg);
   1739   1.1  riastrad 			i915_sg_trim(st); /* Drop any unused tail entries. */
   1740   1.1  riastrad 
   1741   1.1  riastrad 			return st;
   1742   1.1  riastrad 		}
   1743   1.1  riastrad 
   1744   1.1  riastrad 		sg = __sg_next(sg);
   1745   1.1  riastrad 		iter = __sg_next(iter);
   1746   1.1  riastrad 		offset = 0;
   1747   1.1  riastrad 	} while (1);
   1748   1.1  riastrad 
   1749   1.1  riastrad err_sg_alloc:
   1750   1.1  riastrad 	kfree(st);
   1751   1.1  riastrad err_st_alloc:
   1752   1.1  riastrad 	return ERR_PTR(ret);
   1753  1.10  riastrad #endif	/* __NetBSD__ */
   1754   1.1  riastrad }
   1755   1.1  riastrad 
   1756   1.1  riastrad static int
   1757   1.1  riastrad i915_get_ggtt_vma_pages(struct i915_vma *vma)
   1758   1.1  riastrad {
   1759   1.1  riastrad 	int ret;
   1760   1.1  riastrad 
   1761   1.1  riastrad 	/*
   1762   1.1  riastrad 	 * The vma->pages are only valid within the lifespan of the borrowed
   1763   1.1  riastrad 	 * obj->mm.pages. When the obj->mm.pages sg_table is regenerated, so
   1764   1.1  riastrad 	 * must be the vma->pages. A simple rule is that vma->pages must only
   1765   1.1  riastrad 	 * be accessed when the obj->mm.pages are pinned.
   1766   1.1  riastrad 	 */
   1767   1.1  riastrad 	GEM_BUG_ON(!i915_gem_object_has_pinned_pages(vma->obj));
   1768   1.1  riastrad 
   1769   1.1  riastrad 	switch (vma->ggtt_view.type) {
   1770   1.1  riastrad 	default:
   1771   1.1  riastrad 		GEM_BUG_ON(vma->ggtt_view.type);
   1772   1.1  riastrad 		/* fall through */
   1773   1.1  riastrad 	case I915_GGTT_VIEW_NORMAL:
   1774   1.1  riastrad 		vma->pages = vma->obj->mm.pages;
   1775   1.1  riastrad 		return 0;
   1776   1.1  riastrad 
   1777   1.1  riastrad 	case I915_GGTT_VIEW_ROTATED:
   1778  1.16  riastrad #ifdef __NetBSD__
   1779  1.16  riastrad 		vma->pages = ERR_PTR(-ENODEV);
   1780  1.16  riastrad #else
   1781   1.1  riastrad 		vma->pages =
   1782   1.1  riastrad 			intel_rotate_pages(&vma->ggtt_view.rotated, vma->obj);
   1783  1.16  riastrad #endif
   1784   1.1  riastrad 		break;
   1785   1.1  riastrad 
   1786   1.1  riastrad 	case I915_GGTT_VIEW_REMAPPED:
   1787  1.16  riastrad #ifdef __NetBSD__
   1788  1.16  riastrad 		vma->pages = ERR_PTR(-ENODEV);
   1789  1.16  riastrad #else
   1790   1.1  riastrad 		vma->pages =
   1791   1.1  riastrad 			intel_remap_pages(&vma->ggtt_view.remapped, vma->obj);
   1792  1.16  riastrad #endif
   1793   1.1  riastrad 		break;
   1794   1.1  riastrad 
   1795   1.1  riastrad 	case I915_GGTT_VIEW_PARTIAL:
   1796   1.1  riastrad 		vma->pages = intel_partial_pages(&vma->ggtt_view, vma->obj);
   1797   1.1  riastrad 		break;
   1798   1.1  riastrad 	}
   1799   1.1  riastrad 
   1800   1.1  riastrad 	ret = 0;
   1801   1.1  riastrad 	if (IS_ERR(vma->pages)) {
   1802   1.1  riastrad 		ret = PTR_ERR(vma->pages);
   1803   1.1  riastrad 		vma->pages = NULL;
   1804   1.1  riastrad 		DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n",
   1805   1.1  riastrad 			  vma->ggtt_view.type, ret);
   1806   1.1  riastrad 	}
   1807   1.1  riastrad 	return ret;
   1808   1.1  riastrad }
   1809