Home | History | Annotate | Line # | Download | only in i915
i915_gem_gtt.c revision 1.8
      1 /*	$NetBSD: i915_gem_gtt.c,v 1.8 2018/08/27 06:33:34 riastradh Exp $	*/
      2 
      3 /*
      4  * Copyright  2010 Daniel Vetter
      5  * Copyright  2011-2014 Intel Corporation
      6  *
      7  * Permission is hereby granted, free of charge, to any person obtaining a
      8  * copy of this software and associated documentation files (the "Software"),
      9  * to deal in the Software without restriction, including without limitation
     10  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     11  * and/or sell copies of the Software, and to permit persons to whom the
     12  * Software is furnished to do so, subject to the following conditions:
     13  *
     14  * The above copyright notice and this permission notice (including the next
     15  * paragraph) shall be included in all copies or substantial portions of the
     16  * Software.
     17  *
     18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
     24  * IN THE SOFTWARE.
     25  *
     26  */
     27 
     28 #include <sys/cdefs.h>
     29 __KERNEL_RCSID(0, "$NetBSD: i915_gem_gtt.c,v 1.8 2018/08/27 06:33:34 riastradh Exp $");
     30 
     31 #include <linux/err.h>
     32 #include <linux/seq_file.h>
     33 #include <drm/drmP.h>
     34 #include <drm/i915_drm.h>
     35 #include "i915_drv.h"
     36 #include "i915_vgpu.h"
     37 #include "i915_trace.h"
     38 #include "intel_drv.h"
     39 
     40 #ifdef __NetBSD__
     41 #include <x86/machdep.h>
     42 #include <x86/pte.h>
     43 #define	_PAGE_PRESENT	PG_V	/* 0x01 PTE is present / valid */
     44 #define	_PAGE_RW	PG_RW	/* 0x02 read/write */
     45 #define	_PAGE_PWT	PG_WT	/* 0x08 write-through */
     46 #define	_PAGE_PCD	PG_N	/* 0x10 page cache disabled / non-cacheable */
     47 #define	_PAGE_PAT	PG_PAT	/* 0x80 page attribute table on PTE */
     48 #endif
     49 
     50 /**
     51  * DOC: Global GTT views
     52  *
     53  * Background and previous state
     54  *
     55  * Historically objects could exists (be bound) in global GTT space only as
     56  * singular instances with a view representing all of the object's backing pages
     57  * in a linear fashion. This view will be called a normal view.
     58  *
     59  * To support multiple views of the same object, where the number of mapped
     60  * pages is not equal to the backing store, or where the layout of the pages
     61  * is not linear, concept of a GGTT view was added.
     62  *
     63  * One example of an alternative view is a stereo display driven by a single
     64  * image. In this case we would have a framebuffer looking like this
     65  * (2x2 pages):
     66  *
     67  *    12
     68  *    34
     69  *
     70  * Above would represent a normal GGTT view as normally mapped for GPU or CPU
     71  * rendering. In contrast, fed to the display engine would be an alternative
     72  * view which could look something like this:
     73  *
     74  *   1212
     75  *   3434
     76  *
     77  * In this example both the size and layout of pages in the alternative view is
     78  * different from the normal view.
     79  *
     80  * Implementation and usage
     81  *
     82  * GGTT views are implemented using VMAs and are distinguished via enum
     83  * i915_ggtt_view_type and struct i915_ggtt_view.
     84  *
     85  * A new flavour of core GEM functions which work with GGTT bound objects were
     86  * added with the _ggtt_ infix, and sometimes with _view postfix to avoid
     87  * renaming  in large amounts of code. They take the struct i915_ggtt_view
     88  * parameter encapsulating all metadata required to implement a view.
     89  *
     90  * As a helper for callers which are only interested in the normal view,
     91  * globally const i915_ggtt_view_normal singleton instance exists. All old core
     92  * GEM API functions, the ones not taking the view parameter, are operating on,
     93  * or with the normal GGTT view.
     94  *
     95  * Code wanting to add or use a new GGTT view needs to:
     96  *
     97  * 1. Add a new enum with a suitable name.
     98  * 2. Extend the metadata in the i915_ggtt_view structure if required.
     99  * 3. Add support to i915_get_vma_pages().
    100  *
    101  * New views are required to build a scatter-gather table from within the
    102  * i915_get_vma_pages function. This table is stored in the vma.ggtt_view and
    103  * exists for the lifetime of an VMA.
    104  *
    105  * Core API is designed to have copy semantics which means that passed in
    106  * struct i915_ggtt_view does not need to be persistent (left around after
    107  * calling the core API functions).
    108  *
    109  */
    110 
    111 static int
    112 i915_get_ggtt_vma_pages(struct i915_vma *vma);
    113 
    114 const struct i915_ggtt_view i915_ggtt_view_normal;
    115 const struct i915_ggtt_view i915_ggtt_view_rotated = {
    116         .type = I915_GGTT_VIEW_ROTATED
    117 };
    118 
    119 static int sanitize_enable_ppgtt(struct drm_device *dev, int enable_ppgtt)
    120 {
    121 	bool has_aliasing_ppgtt;
    122 	bool has_full_ppgtt;
    123 
    124 	has_aliasing_ppgtt = INTEL_INFO(dev)->gen >= 6;
    125 	has_full_ppgtt = INTEL_INFO(dev)->gen >= 7;
    126 
    127 	if (intel_vgpu_active(dev))
    128 		has_full_ppgtt = false; /* emulation is too hard */
    129 
    130 	/*
    131 	 * We don't allow disabling PPGTT for gen9+ as it's a requirement for
    132 	 * execlists, the sole mechanism available to submit work.
    133 	 */
    134 	if (INTEL_INFO(dev)->gen < 9 &&
    135 	    (enable_ppgtt == 0 || !has_aliasing_ppgtt))
    136 		return 0;
    137 
    138 	if (enable_ppgtt == 1)
    139 		return 1;
    140 
    141 	if (enable_ppgtt == 2 && has_full_ppgtt)
    142 		return 2;
    143 
    144 #ifdef CONFIG_INTEL_IOMMU
    145 	/* Disable ppgtt on SNB if VT-d is on. */
    146 	if (INTEL_INFO(dev)->gen == 6 && intel_iommu_gfx_mapped) {
    147 		DRM_INFO("Disabling PPGTT because VT-d is on\n");
    148 		return 0;
    149 	}
    150 #endif
    151 
    152 	/* Early VLV doesn't have this */
    153 	if (IS_VALLEYVIEW(dev) && !IS_CHERRYVIEW(dev) &&
    154 	    dev->pdev->revision < 0xb) {
    155 		DRM_DEBUG_DRIVER("disabling PPGTT on pre-B3 step VLV\n");
    156 		return 0;
    157 	}
    158 
    159 	if (INTEL_INFO(dev)->gen >= 8 && i915.enable_execlists)
    160 		return 2;
    161 	else
    162 		return has_aliasing_ppgtt ? 1 : 0;
    163 }
    164 
    165 static int ppgtt_bind_vma(struct i915_vma *vma,
    166 			  enum i915_cache_level cache_level,
    167 			  u32 unused)
    168 {
    169 	u32 pte_flags = 0;
    170 
    171 	/* Currently applicable only to VLV */
    172 	if (vma->obj->gt_ro)
    173 		pte_flags |= PTE_READ_ONLY;
    174 
    175 	vma->vm->insert_entries(vma->vm, vma->obj->pages, vma->node.start,
    176 				cache_level, pte_flags);
    177 
    178 	return 0;
    179 }
    180 
    181 static void ppgtt_unbind_vma(struct i915_vma *vma)
    182 {
    183 	vma->vm->clear_range(vma->vm,
    184 			     vma->node.start,
    185 			     vma->obj->base.size,
    186 			     true);
    187 }
    188 
    189 static gen8_pte_t gen8_pte_encode(dma_addr_t addr,
    190 				  enum i915_cache_level level,
    191 				  bool valid)
    192 {
    193 	gen8_pte_t pte = valid ? _PAGE_PRESENT | _PAGE_RW : 0;
    194 	pte |= addr;
    195 
    196 	switch (level) {
    197 	case I915_CACHE_NONE:
    198 		pte |= PPAT_UNCACHED_INDEX;
    199 		break;
    200 	case I915_CACHE_WT:
    201 		pte |= PPAT_DISPLAY_ELLC_INDEX;
    202 		break;
    203 	default:
    204 		pte |= PPAT_CACHED_INDEX;
    205 		break;
    206 	}
    207 
    208 	return pte;
    209 }
    210 
    211 static gen8_pde_t gen8_pde_encode(const dma_addr_t addr,
    212 				  const enum i915_cache_level level)
    213 {
    214 	gen8_pde_t pde = _PAGE_PRESENT | _PAGE_RW;
    215 	pde |= addr;
    216 	if (level != I915_CACHE_NONE)
    217 		pde |= PPAT_CACHED_PDE_INDEX;
    218 	else
    219 		pde |= PPAT_UNCACHED_INDEX;
    220 	return pde;
    221 }
    222 
    223 #define gen8_pdpe_encode gen8_pde_encode
    224 #define gen8_pml4e_encode gen8_pde_encode
    225 
    226 static gen6_pte_t snb_pte_encode(dma_addr_t addr,
    227 				 enum i915_cache_level level,
    228 				 bool valid, u32 unused)
    229 {
    230 	gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
    231 	pte |= GEN6_PTE_ADDR_ENCODE(addr);
    232 
    233 	switch (level) {
    234 	case I915_CACHE_L3_LLC:
    235 	case I915_CACHE_LLC:
    236 		pte |= GEN6_PTE_CACHE_LLC;
    237 		break;
    238 	case I915_CACHE_NONE:
    239 		pte |= GEN6_PTE_UNCACHED;
    240 		break;
    241 	default:
    242 		MISSING_CASE(level);
    243 	}
    244 
    245 	return pte;
    246 }
    247 
    248 static gen6_pte_t ivb_pte_encode(dma_addr_t addr,
    249 				 enum i915_cache_level level,
    250 				 bool valid, u32 unused)
    251 {
    252 	gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
    253 	pte |= GEN6_PTE_ADDR_ENCODE(addr);
    254 
    255 	switch (level) {
    256 	case I915_CACHE_L3_LLC:
    257 		pte |= GEN7_PTE_CACHE_L3_LLC;
    258 		break;
    259 	case I915_CACHE_LLC:
    260 		pte |= GEN6_PTE_CACHE_LLC;
    261 		break;
    262 	case I915_CACHE_NONE:
    263 		pte |= GEN6_PTE_UNCACHED;
    264 		break;
    265 	default:
    266 		MISSING_CASE(level);
    267 	}
    268 
    269 	return pte;
    270 }
    271 
    272 static gen6_pte_t byt_pte_encode(dma_addr_t addr,
    273 				 enum i915_cache_level level,
    274 				 bool valid, u32 flags)
    275 {
    276 	gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
    277 	pte |= GEN6_PTE_ADDR_ENCODE(addr);
    278 
    279 	if (!(flags & PTE_READ_ONLY))
    280 		pte |= BYT_PTE_WRITEABLE;
    281 
    282 	if (level != I915_CACHE_NONE)
    283 		pte |= BYT_PTE_SNOOPED_BY_CPU_CACHES;
    284 
    285 	return pte;
    286 }
    287 
    288 static gen6_pte_t hsw_pte_encode(dma_addr_t addr,
    289 				 enum i915_cache_level level,
    290 				 bool valid, u32 unused)
    291 {
    292 	gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
    293 	pte |= HSW_PTE_ADDR_ENCODE(addr);
    294 
    295 	if (level != I915_CACHE_NONE)
    296 		pte |= HSW_WB_LLC_AGE3;
    297 
    298 	return pte;
    299 }
    300 
    301 static gen6_pte_t iris_pte_encode(dma_addr_t addr,
    302 				  enum i915_cache_level level,
    303 				  bool valid, u32 unused)
    304 {
    305 	gen6_pte_t pte = valid ? GEN6_PTE_VALID : 0;
    306 	pte |= HSW_PTE_ADDR_ENCODE(addr);
    307 
    308 	switch (level) {
    309 	case I915_CACHE_NONE:
    310 		break;
    311 	case I915_CACHE_WT:
    312 		pte |= HSW_WT_ELLC_LLC_AGE3;
    313 		break;
    314 	default:
    315 		pte |= HSW_WB_ELLC_LLC_AGE3;
    316 		break;
    317 	}
    318 
    319 	return pte;
    320 }
    321 
    322 static int __setup_page_dma(struct drm_device *dev,
    323 			    struct i915_page_dma *p, gfp_t flags)
    324 {
    325 #ifdef __NetBSD__
    326 	int error;
    327 	int nseg = 1;
    328 
    329 	error = bus_dmamem_alloc(dev->dmat, PAGE_SIZE, PAGE_SIZE, PAGE_SIZE,
    330 	    &p->seg, nseg, &nseg, BUS_DMA_WAITOK);
    331 	if (error)
    332 fail0:		return -error;	/* XXX errno NetBSD->Linux */
    333 	KASSERT(nseg == 1);
    334 	error = bus_dmamap_create(dev->dmat, PAGE_SIZE, 1, PAGE_SIZE,
    335 	    PAGE_SIZE, BUS_DMA_WAITOK, &p->map);
    336 	if (error) {
    337 fail1:		bus_dmamem_free(dev->dmat, &p->seg, 1);
    338 		goto fail0;
    339 	}
    340 	error = bus_dmamap_load_raw(dev->dmat, p->map, &p->seg, 1, PAGE_SIZE,
    341 	    BUS_DMA_WAITOK);
    342 	if (error) {
    343 fail2: __unused
    344 		bus_dmamap_destroy(dev->dmat, p->map);
    345 		goto fail1;
    346 	}
    347 #else
    348 	struct device *device = &dev->pdev->dev;
    349 
    350 	p->page = alloc_page(flags);
    351 	if (!p->page)
    352 		return -ENOMEM;
    353 
    354 	p->daddr = dma_map_page(device,
    355 				p->page, 0, 4096, PCI_DMA_BIDIRECTIONAL);
    356 
    357 	if (dma_mapping_error(device, p->daddr)) {
    358 		__free_page(p->page);
    359 		return -EINVAL;
    360 	}
    361 #endif
    362 
    363 	return 0;
    364 }
    365 
    366 static int setup_page_dma(struct drm_device *dev, struct i915_page_dma *p)
    367 {
    368 	return __setup_page_dma(dev, p, GFP_KERNEL);
    369 }
    370 
    371 static void cleanup_page_dma(struct drm_device *dev, struct i915_page_dma *p)
    372 {
    373 #ifdef __NetBSD__
    374 	if (WARN_ON(!p->map))
    375 		return;
    376 
    377 	bus_dmamap_unload(dev->dmat, p->map);
    378 	bus_dmamap_destroy(dev->dmat, p->dmap);
    379 	bus_dmamem_free(dev->dmat, &p->seg, 1);
    380 #else
    381 	if (WARN_ON(!p->page))
    382 		return;
    383 
    384 	dma_unmap_page(&dev->pdev->dev, p->daddr, 4096, PCI_DMA_BIDIRECTIONAL);
    385 	__free_page(p->page);
    386 	memset(p, 0, sizeof(*p));
    387 #endif
    388 }
    389 
    390 static void *kmap_page_dma(struct i915_page_dma *p)
    391 {
    392 #ifdef __NetBSD__
    393 	return kmap_atomic(PHYS_TO_VM_PAGE(p->seg.ds_addr));
    394 #else
    395 	return kmap_atomic(p->page);
    396 #endif
    397 }
    398 
    399 /* We use the flushing unmap only with ppgtt structures:
    400  * page directories, page tables and scratch pages.
    401  */
    402 static void kunmap_page_dma(struct drm_device *dev, void *vaddr)
    403 {
    404 	/* There are only few exceptions for gen >=6. chv and bxt.
    405 	 * And we are not sure about the latter so play safe for now.
    406 	 */
    407 	if (IS_CHERRYVIEW(dev) || IS_BROXTON(dev))
    408 		drm_clflush_virt_range(vaddr, PAGE_SIZE);
    409 
    410 	kunmap_atomic(vaddr);
    411 }
    412 
    413 #define kmap_px(px) kmap_page_dma(px_base(px))
    414 #define kunmap_px(ppgtt, vaddr) kunmap_page_dma((ppgtt)->base.dev, (vaddr))
    415 
    416 #define setup_px(dev, px) setup_page_dma((dev), px_base(px))
    417 #define cleanup_px(dev, px) cleanup_page_dma((dev), px_base(px))
    418 #define fill_px(dev, px, v) fill_page_dma((dev), px_base(px), (v))
    419 #define fill32_px(dev, px, v) fill_page_dma_32((dev), px_base(px), (v))
    420 
    421 static void fill_page_dma(struct drm_device *dev, struct i915_page_dma *p,
    422 			  const uint64_t val)
    423 {
    424 	int i;
    425 	uint64_t * const vaddr = kmap_page_dma(p);
    426 
    427 	for (i = 0; i < 512; i++)
    428 		vaddr[i] = val;
    429 
    430 	kunmap_page_dma(dev, vaddr);
    431 }
    432 
    433 static void fill_page_dma_32(struct drm_device *dev, struct i915_page_dma *p,
    434 			     const uint32_t val32)
    435 {
    436 	uint64_t v = val32;
    437 
    438 	v = v << 32 | val32;
    439 
    440 	fill_page_dma(dev, p, v);
    441 }
    442 
    443 static struct i915_page_scratch *alloc_scratch_page(struct drm_device *dev)
    444 {
    445 	struct i915_page_scratch *sp;
    446 	int ret;
    447 
    448 	sp = kzalloc(sizeof(*sp), GFP_KERNEL);
    449 	if (sp == NULL)
    450 		return ERR_PTR(-ENOMEM);
    451 
    452 	ret = __setup_page_dma(dev, px_base(sp), GFP_DMA32 | __GFP_ZERO);
    453 	if (ret) {
    454 		kfree(sp);
    455 		return ERR_PTR(ret);
    456 	}
    457 
    458 	set_pages_uc(px_page(sp), 1);
    459 
    460 	return sp;
    461 }
    462 
    463 static void free_scratch_page(struct drm_device *dev,
    464 			      struct i915_page_scratch *sp)
    465 {
    466 	set_pages_wb(px_page(sp), 1);
    467 
    468 	cleanup_px(dev, sp);
    469 	kfree(sp);
    470 }
    471 
    472 static struct i915_page_table *alloc_pt(struct drm_device *dev)
    473 {
    474 	struct i915_page_table *pt;
    475 	const size_t count = INTEL_INFO(dev)->gen >= 8 ?
    476 		GEN8_PTES : GEN6_PTES;
    477 	int ret = -ENOMEM;
    478 
    479 	pt = kzalloc(sizeof(*pt), GFP_KERNEL);
    480 	if (!pt)
    481 		return ERR_PTR(-ENOMEM);
    482 
    483 	pt->used_ptes = kcalloc(BITS_TO_LONGS(count), sizeof(*pt->used_ptes),
    484 				GFP_KERNEL);
    485 
    486 	if (!pt->used_ptes)
    487 		goto fail_bitmap;
    488 
    489 	ret = setup_px(dev, pt);
    490 	if (ret)
    491 		goto fail_page_m;
    492 
    493 	return pt;
    494 
    495 fail_page_m:
    496 	kfree(pt->used_ptes);
    497 fail_bitmap:
    498 	kfree(pt);
    499 
    500 	return ERR_PTR(ret);
    501 }
    502 
    503 static void free_pt(struct drm_device *dev, struct i915_page_table *pt)
    504 {
    505 	cleanup_px(dev, pt);
    506 	kfree(pt->used_ptes);
    507 	kfree(pt);
    508 }
    509 
    510 static void gen8_initialize_pt(struct i915_address_space *vm,
    511 			       struct i915_page_table *pt)
    512 {
    513 	gen8_pte_t scratch_pte;
    514 
    515 	scratch_pte = gen8_pte_encode(px_dma(vm->scratch_page),
    516 				      I915_CACHE_LLC, true);
    517 
    518 	fill_px(vm->dev, pt, scratch_pte);
    519 }
    520 
    521 static void gen6_initialize_pt(struct i915_address_space *vm,
    522 			       struct i915_page_table *pt)
    523 {
    524 	gen6_pte_t scratch_pte;
    525 
    526 	WARN_ON(px_dma(vm->scratch_page) == 0);
    527 
    528 	scratch_pte = vm->pte_encode(px_dma(vm->scratch_page),
    529 				     I915_CACHE_LLC, true, 0);
    530 
    531 	fill32_px(vm->dev, pt, scratch_pte);
    532 }
    533 
    534 static struct i915_page_directory *alloc_pd(struct drm_device *dev)
    535 {
    536 	struct i915_page_directory *pd;
    537 	int ret = -ENOMEM;
    538 
    539 	pd = kzalloc(sizeof(*pd), GFP_KERNEL);
    540 	if (!pd)
    541 		return ERR_PTR(-ENOMEM);
    542 
    543 	pd->used_pdes = kcalloc(BITS_TO_LONGS(I915_PDES),
    544 				sizeof(*pd->used_pdes), GFP_KERNEL);
    545 	if (!pd->used_pdes)
    546 		goto fail_bitmap;
    547 
    548 	ret = setup_px(dev, pd);
    549 	if (ret)
    550 		goto fail_page_m;
    551 
    552 	return pd;
    553 
    554 fail_page_m:
    555 	kfree(pd->used_pdes);
    556 fail_bitmap:
    557 	kfree(pd);
    558 
    559 	return ERR_PTR(ret);
    560 }
    561 
    562 static void free_pd(struct drm_device *dev, struct i915_page_directory *pd)
    563 {
    564 	if (px_page(pd)) {
    565 		cleanup_px(dev, pd);
    566 		kfree(pd->used_pdes);
    567 		kfree(pd);
    568 	}
    569 }
    570 
    571 static void gen8_initialize_pd(struct i915_address_space *vm,
    572 			       struct i915_page_directory *pd)
    573 {
    574 	gen8_pde_t scratch_pde;
    575 
    576 	scratch_pde = gen8_pde_encode(px_dma(vm->scratch_pt), I915_CACHE_LLC);
    577 
    578 	fill_px(vm->dev, pd, scratch_pde);
    579 }
    580 
    581 static int __pdp_init(struct drm_device *dev,
    582 		      struct i915_page_directory_pointer *pdp)
    583 {
    584 	size_t pdpes = I915_PDPES_PER_PDP(dev);
    585 
    586 	pdp->used_pdpes = kcalloc(BITS_TO_LONGS(pdpes),
    587 				  sizeof(unsigned long),
    588 				  GFP_KERNEL);
    589 	if (!pdp->used_pdpes)
    590 		return -ENOMEM;
    591 
    592 	pdp->page_directory = kcalloc(pdpes, sizeof(*pdp->page_directory),
    593 				      GFP_KERNEL);
    594 	if (!pdp->page_directory) {
    595 		kfree(pdp->used_pdpes);
    596 		/* the PDP might be the statically allocated top level. Keep it
    597 		 * as clean as possible */
    598 		pdp->used_pdpes = NULL;
    599 		return -ENOMEM;
    600 	}
    601 
    602 	return 0;
    603 }
    604 
    605 static void __pdp_fini(struct i915_page_directory_pointer *pdp)
    606 {
    607 	kfree(pdp->used_pdpes);
    608 	kfree(pdp->page_directory);
    609 	pdp->page_directory = NULL;
    610 }
    611 
    612 static struct
    613 i915_page_directory_pointer *alloc_pdp(struct drm_device *dev)
    614 {
    615 	struct i915_page_directory_pointer *pdp;
    616 	int ret = -ENOMEM;
    617 
    618 	WARN_ON(!USES_FULL_48BIT_PPGTT(dev));
    619 
    620 	pdp = kzalloc(sizeof(*pdp), GFP_KERNEL);
    621 	if (!pdp)
    622 		return ERR_PTR(-ENOMEM);
    623 
    624 	ret = __pdp_init(dev, pdp);
    625 	if (ret)
    626 		goto fail_bitmap;
    627 
    628 	ret = setup_px(dev, pdp);
    629 	if (ret)
    630 		goto fail_page_m;
    631 
    632 	return pdp;
    633 
    634 fail_page_m:
    635 	__pdp_fini(pdp);
    636 fail_bitmap:
    637 	kfree(pdp);
    638 
    639 	return ERR_PTR(ret);
    640 }
    641 
    642 static void free_pdp(struct drm_device *dev,
    643 		     struct i915_page_directory_pointer *pdp)
    644 {
    645 	__pdp_fini(pdp);
    646 	if (USES_FULL_48BIT_PPGTT(dev)) {
    647 		cleanup_px(dev, pdp);
    648 		kfree(pdp);
    649 	}
    650 }
    651 
    652 static void gen8_initialize_pdp(struct i915_address_space *vm,
    653 				struct i915_page_directory_pointer *pdp)
    654 {
    655 	gen8_ppgtt_pdpe_t scratch_pdpe;
    656 
    657 	scratch_pdpe = gen8_pdpe_encode(px_dma(vm->scratch_pd), I915_CACHE_LLC);
    658 
    659 	fill_px(vm->dev, pdp, scratch_pdpe);
    660 }
    661 
    662 static void gen8_initialize_pml4(struct i915_address_space *vm,
    663 				 struct i915_pml4 *pml4)
    664 {
    665 	gen8_ppgtt_pml4e_t scratch_pml4e;
    666 
    667 	scratch_pml4e = gen8_pml4e_encode(px_dma(vm->scratch_pdp),
    668 					  I915_CACHE_LLC);
    669 
    670 	fill_px(vm->dev, pml4, scratch_pml4e);
    671 }
    672 
    673 static void
    674 gen8_setup_page_directory(struct i915_hw_ppgtt *ppgtt,
    675 			  struct i915_page_directory_pointer *pdp,
    676 			  struct i915_page_directory *pd,
    677 			  int index)
    678 {
    679 	gen8_ppgtt_pdpe_t *page_directorypo;
    680 
    681 	if (!USES_FULL_48BIT_PPGTT(ppgtt->base.dev))
    682 		return;
    683 
    684 	page_directorypo = kmap_px(pdp);
    685 	page_directorypo[index] = gen8_pdpe_encode(px_dma(pd), I915_CACHE_LLC);
    686 	kunmap_px(ppgtt, page_directorypo);
    687 }
    688 
    689 static void
    690 gen8_setup_page_directory_pointer(struct i915_hw_ppgtt *ppgtt,
    691 				  struct i915_pml4 *pml4,
    692 				  struct i915_page_directory_pointer *pdp,
    693 				  int index)
    694 {
    695 	gen8_ppgtt_pml4e_t *pagemap = kmap_px(pml4);
    696 
    697 	WARN_ON(!USES_FULL_48BIT_PPGTT(ppgtt->base.dev));
    698 	pagemap[index] = gen8_pml4e_encode(px_dma(pdp), I915_CACHE_LLC);
    699 	kunmap_px(ppgtt, pagemap);
    700 }
    701 
    702 /* Broadwell Page Directory Pointer Descriptors */
    703 static int gen8_write_pdp(struct drm_i915_gem_request *req,
    704 			  unsigned entry,
    705 			  dma_addr_t addr)
    706 {
    707 	struct intel_engine_cs *ring = req->ring;
    708 	int ret;
    709 
    710 	BUG_ON(entry >= 4);
    711 
    712 	ret = intel_ring_begin(req, 6);
    713 	if (ret)
    714 		return ret;
    715 
    716 	intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
    717 	intel_ring_emit(ring, GEN8_RING_PDP_UDW(ring, entry));
    718 	intel_ring_emit(ring, upper_32_bits(addr));
    719 	intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
    720 	intel_ring_emit(ring, GEN8_RING_PDP_LDW(ring, entry));
    721 	intel_ring_emit(ring, lower_32_bits(addr));
    722 	intel_ring_advance(ring);
    723 
    724 	return 0;
    725 }
    726 
    727 static int gen8_legacy_mm_switch(struct i915_hw_ppgtt *ppgtt,
    728 				 struct drm_i915_gem_request *req)
    729 {
    730 	int i, ret;
    731 
    732 	for (i = GEN8_LEGACY_PDPES - 1; i >= 0; i--) {
    733 		const dma_addr_t pd_daddr = i915_page_dir_dma_addr(ppgtt, i);
    734 
    735 		ret = gen8_write_pdp(req, i, pd_daddr);
    736 		if (ret)
    737 			return ret;
    738 	}
    739 
    740 	return 0;
    741 }
    742 
    743 static int gen8_48b_mm_switch(struct i915_hw_ppgtt *ppgtt,
    744 			      struct drm_i915_gem_request *req)
    745 {
    746 	return gen8_write_pdp(req, 0, px_dma(&ppgtt->pml4));
    747 }
    748 
    749 static void gen8_ppgtt_clear_pte_range(struct i915_address_space *vm,
    750 				       struct i915_page_directory_pointer *pdp,
    751 				       uint64_t start,
    752 				       uint64_t length,
    753 				       gen8_pte_t scratch_pte)
    754 {
    755 	struct i915_hw_ppgtt *ppgtt =
    756 		container_of(vm, struct i915_hw_ppgtt, base);
    757 	gen8_pte_t *pt_vaddr;
    758 	unsigned pdpe = gen8_pdpe_index(start);
    759 	unsigned pde = gen8_pde_index(start);
    760 	unsigned pte = gen8_pte_index(start);
    761 	unsigned num_entries = length >> PAGE_SHIFT;
    762 	unsigned last_pte, i;
    763 
    764 	if (WARN_ON(!pdp))
    765 		return;
    766 
    767 	while (num_entries) {
    768 		struct i915_page_directory *pd;
    769 		struct i915_page_table *pt;
    770 
    771 		if (WARN_ON(!pdp->page_directory[pdpe]))
    772 			break;
    773 
    774 		pd = pdp->page_directory[pdpe];
    775 
    776 		if (WARN_ON(!pd->page_table[pde]))
    777 			break;
    778 
    779 		pt = pd->page_table[pde];
    780 
    781 		if (WARN_ON(!px_page(pt)))
    782 			break;
    783 
    784 		last_pte = pte + num_entries;
    785 		if (last_pte > GEN8_PTES)
    786 			last_pte = GEN8_PTES;
    787 
    788 		pt_vaddr = kmap_px(pt);
    789 
    790 		for (i = pte; i < last_pte; i++) {
    791 			pt_vaddr[i] = scratch_pte;
    792 			num_entries--;
    793 		}
    794 
    795 		kunmap_px(ppgtt, pt);
    796 
    797 		pte = 0;
    798 		if (++pde == I915_PDES) {
    799 			if (++pdpe == I915_PDPES_PER_PDP(vm->dev))
    800 				break;
    801 			pde = 0;
    802 		}
    803 	}
    804 }
    805 
    806 static void gen8_ppgtt_clear_range(struct i915_address_space *vm,
    807 				   uint64_t start,
    808 				   uint64_t length,
    809 				   bool use_scratch)
    810 {
    811 	struct i915_hw_ppgtt *ppgtt =
    812 		container_of(vm, struct i915_hw_ppgtt, base);
    813 	gen8_pte_t scratch_pte = gen8_pte_encode(px_dma(vm->scratch_page),
    814 						 I915_CACHE_LLC, use_scratch);
    815 
    816 	if (!USES_FULL_48BIT_PPGTT(vm->dev)) {
    817 		gen8_ppgtt_clear_pte_range(vm, &ppgtt->pdp, start, length,
    818 					   scratch_pte);
    819 	} else {
    820 		uint64_t templ4, pml4e;
    821 		struct i915_page_directory_pointer *pdp;
    822 
    823 		gen8_for_each_pml4e(pdp, &ppgtt->pml4, start, length, templ4, pml4e) {
    824 			gen8_ppgtt_clear_pte_range(vm, pdp, start, length,
    825 						   scratch_pte);
    826 		}
    827 	}
    828 }
    829 
    830 static void
    831 gen8_ppgtt_insert_pte_entries(struct i915_address_space *vm,
    832 			      struct i915_page_directory_pointer *pdp,
    833 			      struct sg_page_iter *sg_iter,
    834 			      uint64_t start,
    835 			      enum i915_cache_level cache_level)
    836 {
    837 	struct i915_hw_ppgtt *ppgtt =
    838 		container_of(vm, struct i915_hw_ppgtt, base);
    839 	gen8_pte_t *pt_vaddr;
    840 	unsigned pdpe = gen8_pdpe_index(start);
    841 	unsigned pde = gen8_pde_index(start);
    842 	unsigned pte = gen8_pte_index(start);
    843 
    844 	pt_vaddr = NULL;
    845 
    846 	while (__sg_page_iter_next(sg_iter)) {
    847 		if (pt_vaddr == NULL) {
    848 			struct i915_page_directory *pd = pdp->page_directory[pdpe];
    849 			struct i915_page_table *pt = pd->page_table[pde];
    850 			pt_vaddr = kmap_px(pt);
    851 		}
    852 
    853 		pt_vaddr[pte] =
    854 			gen8_pte_encode(sg_page_iter_dma_address(sg_iter),
    855 					cache_level, true);
    856 		if (++pte == GEN8_PTES) {
    857 			kunmap_px(ppgtt, pt_vaddr);
    858 			pt_vaddr = NULL;
    859 			if (++pde == I915_PDES) {
    860 				if (++pdpe == I915_PDPES_PER_PDP(vm->dev))
    861 					break;
    862 				pde = 0;
    863 			}
    864 			pte = 0;
    865 		}
    866 	}
    867 
    868 	if (pt_vaddr)
    869 		kunmap_px(ppgtt, pt_vaddr);
    870 }
    871 
    872 static void gen8_ppgtt_insert_entries(struct i915_address_space *vm,
    873 				      struct sg_table *pages,
    874 				      uint64_t start,
    875 				      enum i915_cache_level cache_level,
    876 				      u32 unused)
    877 {
    878 	struct i915_hw_ppgtt *ppgtt =
    879 		container_of(vm, struct i915_hw_ppgtt, base);
    880 	struct sg_page_iter sg_iter;
    881 
    882 	__sg_page_iter_start(&sg_iter, pages->sgl, sg_nents(pages->sgl), 0);
    883 
    884 	if (!USES_FULL_48BIT_PPGTT(vm->dev)) {
    885 		gen8_ppgtt_insert_pte_entries(vm, &ppgtt->pdp, &sg_iter, start,
    886 					      cache_level);
    887 	} else {
    888 		struct i915_page_directory_pointer *pdp;
    889 		uint64_t templ4, pml4e;
    890 		uint64_t length = (uint64_t)pages->orig_nents << PAGE_SHIFT;
    891 
    892 		gen8_for_each_pml4e(pdp, &ppgtt->pml4, start, length, templ4, pml4e) {
    893 			gen8_ppgtt_insert_pte_entries(vm, pdp, &sg_iter,
    894 						      start, cache_level);
    895 		}
    896 	}
    897 }
    898 
    899 static void gen8_free_page_tables(struct drm_device *dev,
    900 				  struct i915_page_directory *pd)
    901 {
    902 	int i;
    903 
    904 	if (!px_page(pd))
    905 		return;
    906 
    907 	for_each_set_bit(i, pd->used_pdes, I915_PDES) {
    908 		if (WARN_ON(!pd->page_table[i]))
    909 			continue;
    910 
    911 		free_pt(dev, pd->page_table[i]);
    912 		pd->page_table[i] = NULL;
    913 	}
    914 }
    915 
    916 static int gen8_init_scratch(struct i915_address_space *vm)
    917 {
    918 	struct drm_device *dev = vm->dev;
    919 
    920 	vm->scratch_page = alloc_scratch_page(dev);
    921 	if (IS_ERR(vm->scratch_page))
    922 		return PTR_ERR(vm->scratch_page);
    923 
    924 	vm->scratch_pt = alloc_pt(dev);
    925 	if (IS_ERR(vm->scratch_pt)) {
    926 		free_scratch_page(dev, vm->scratch_page);
    927 		return PTR_ERR(vm->scratch_pt);
    928 	}
    929 
    930 	vm->scratch_pd = alloc_pd(dev);
    931 	if (IS_ERR(vm->scratch_pd)) {
    932 		free_pt(dev, vm->scratch_pt);
    933 		free_scratch_page(dev, vm->scratch_page);
    934 		return PTR_ERR(vm->scratch_pd);
    935 	}
    936 
    937 	if (USES_FULL_48BIT_PPGTT(dev)) {
    938 		vm->scratch_pdp = alloc_pdp(dev);
    939 		if (IS_ERR(vm->scratch_pdp)) {
    940 			free_pd(dev, vm->scratch_pd);
    941 			free_pt(dev, vm->scratch_pt);
    942 			free_scratch_page(dev, vm->scratch_page);
    943 			return PTR_ERR(vm->scratch_pdp);
    944 		}
    945 	}
    946 
    947 	gen8_initialize_pt(vm, vm->scratch_pt);
    948 	gen8_initialize_pd(vm, vm->scratch_pd);
    949 	if (USES_FULL_48BIT_PPGTT(dev))
    950 		gen8_initialize_pdp(vm, vm->scratch_pdp);
    951 
    952 	return 0;
    953 }
    954 
    955 static int gen8_ppgtt_notify_vgt(struct i915_hw_ppgtt *ppgtt, bool create)
    956 {
    957 	enum vgt_g2v_type msg;
    958 	struct drm_device *dev = ppgtt->base.dev;
    959 	struct drm_i915_private *dev_priv = dev->dev_private;
    960 	unsigned int offset = vgtif_reg(pdp0_lo);
    961 	int i;
    962 
    963 	if (USES_FULL_48BIT_PPGTT(dev)) {
    964 		u64 daddr = px_dma(&ppgtt->pml4);
    965 
    966 		I915_WRITE(offset, lower_32_bits(daddr));
    967 		I915_WRITE(offset + 4, upper_32_bits(daddr));
    968 
    969 		msg = (create ? VGT_G2V_PPGTT_L4_PAGE_TABLE_CREATE :
    970 				VGT_G2V_PPGTT_L4_PAGE_TABLE_DESTROY);
    971 	} else {
    972 		for (i = 0; i < GEN8_LEGACY_PDPES; i++) {
    973 			u64 daddr = i915_page_dir_dma_addr(ppgtt, i);
    974 
    975 			I915_WRITE(offset, lower_32_bits(daddr));
    976 			I915_WRITE(offset + 4, upper_32_bits(daddr));
    977 
    978 			offset += 8;
    979 		}
    980 
    981 		msg = (create ? VGT_G2V_PPGTT_L3_PAGE_TABLE_CREATE :
    982 				VGT_G2V_PPGTT_L3_PAGE_TABLE_DESTROY);
    983 	}
    984 
    985 	I915_WRITE(vgtif_reg(g2v_notify), msg);
    986 
    987 	return 0;
    988 }
    989 
    990 static void gen8_free_scratch(struct i915_address_space *vm)
    991 {
    992 	struct drm_device *dev = vm->dev;
    993 
    994 	if (USES_FULL_48BIT_PPGTT(dev))
    995 		free_pdp(dev, vm->scratch_pdp);
    996 	free_pd(dev, vm->scratch_pd);
    997 	free_pt(dev, vm->scratch_pt);
    998 	free_scratch_page(dev, vm->scratch_page);
    999 }
   1000 
   1001 static void gen8_ppgtt_cleanup_3lvl(struct drm_device *dev,
   1002 				    struct i915_page_directory_pointer *pdp)
   1003 {
   1004 	int i;
   1005 
   1006 	for_each_set_bit(i, pdp->used_pdpes, I915_PDPES_PER_PDP(dev)) {
   1007 		if (WARN_ON(!pdp->page_directory[i]))
   1008 			continue;
   1009 
   1010 		gen8_free_page_tables(dev, pdp->page_directory[i]);
   1011 		free_pd(dev, pdp->page_directory[i]);
   1012 	}
   1013 
   1014 	free_pdp(dev, pdp);
   1015 }
   1016 
   1017 static void gen8_ppgtt_cleanup_4lvl(struct i915_hw_ppgtt *ppgtt)
   1018 {
   1019 	int i;
   1020 
   1021 	for_each_set_bit(i, ppgtt->pml4.used_pml4es, GEN8_PML4ES_PER_PML4) {
   1022 		if (WARN_ON(!ppgtt->pml4.pdps[i]))
   1023 			continue;
   1024 
   1025 		gen8_ppgtt_cleanup_3lvl(ppgtt->base.dev, ppgtt->pml4.pdps[i]);
   1026 	}
   1027 
   1028 	cleanup_px(ppgtt->base.dev, &ppgtt->pml4);
   1029 }
   1030 
   1031 static void gen8_ppgtt_cleanup(struct i915_address_space *vm)
   1032 {
   1033 	struct i915_hw_ppgtt *ppgtt =
   1034 		container_of(vm, struct i915_hw_ppgtt, base);
   1035 
   1036 	if (intel_vgpu_active(vm->dev))
   1037 		gen8_ppgtt_notify_vgt(ppgtt, false);
   1038 
   1039 	if (!USES_FULL_48BIT_PPGTT(ppgtt->base.dev))
   1040 		gen8_ppgtt_cleanup_3lvl(ppgtt->base.dev, &ppgtt->pdp);
   1041 	else
   1042 		gen8_ppgtt_cleanup_4lvl(ppgtt);
   1043 
   1044 	gen8_free_scratch(vm);
   1045 }
   1046 
   1047 /**
   1048  * gen8_ppgtt_alloc_pagetabs() - Allocate page tables for VA range.
   1049  * @vm:	Master vm structure.
   1050  * @pd:	Page directory for this address range.
   1051  * @start:	Starting virtual address to begin allocations.
   1052  * @length:	Size of the allocations.
   1053  * @new_pts:	Bitmap set by function with new allocations. Likely used by the
   1054  *		caller to free on error.
   1055  *
   1056  * Allocate the required number of page tables. Extremely similar to
   1057  * gen8_ppgtt_alloc_page_directories(). The main difference is here we are limited by
   1058  * the page directory boundary (instead of the page directory pointer). That
   1059  * boundary is 1GB virtual. Therefore, unlike gen8_ppgtt_alloc_page_directories(), it is
   1060  * possible, and likely that the caller will need to use multiple calls of this
   1061  * function to achieve the appropriate allocation.
   1062  *
   1063  * Return: 0 if success; negative error code otherwise.
   1064  */
   1065 static int gen8_ppgtt_alloc_pagetabs(struct i915_address_space *vm,
   1066 				     struct i915_page_directory *pd,
   1067 				     uint64_t start,
   1068 				     uint64_t length,
   1069 				     unsigned long *new_pts)
   1070 {
   1071 	struct drm_device *dev = vm->dev;
   1072 	struct i915_page_table *pt;
   1073 	uint64_t temp;
   1074 	uint32_t pde;
   1075 
   1076 	gen8_for_each_pde(pt, pd, start, length, temp, pde) {
   1077 		/* Don't reallocate page tables */
   1078 		if (test_bit(pde, pd->used_pdes)) {
   1079 			/* Scratch is never allocated this way */
   1080 			WARN_ON(pt == vm->scratch_pt);
   1081 			continue;
   1082 		}
   1083 
   1084 		pt = alloc_pt(dev);
   1085 		if (IS_ERR(pt))
   1086 			goto unwind_out;
   1087 
   1088 		gen8_initialize_pt(vm, pt);
   1089 		pd->page_table[pde] = pt;
   1090 		__set_bit(pde, new_pts);
   1091 		trace_i915_page_table_entry_alloc(vm, pde, start, GEN8_PDE_SHIFT);
   1092 	}
   1093 
   1094 	return 0;
   1095 
   1096 unwind_out:
   1097 	for_each_set_bit(pde, new_pts, I915_PDES)
   1098 		free_pt(dev, pd->page_table[pde]);
   1099 
   1100 	return -ENOMEM;
   1101 }
   1102 
   1103 /**
   1104  * gen8_ppgtt_alloc_page_directories() - Allocate page directories for VA range.
   1105  * @vm:	Master vm structure.
   1106  * @pdp:	Page directory pointer for this address range.
   1107  * @start:	Starting virtual address to begin allocations.
   1108  * @length:	Size of the allocations.
   1109  * @new_pds:	Bitmap set by function with new allocations. Likely used by the
   1110  *		caller to free on error.
   1111  *
   1112  * Allocate the required number of page directories starting at the pde index of
   1113  * @start, and ending at the pde index @start + @length. This function will skip
   1114  * over already allocated page directories within the range, and only allocate
   1115  * new ones, setting the appropriate pointer within the pdp as well as the
   1116  * correct position in the bitmap @new_pds.
   1117  *
   1118  * The function will only allocate the pages within the range for a give page
   1119  * directory pointer. In other words, if @start + @length straddles a virtually
   1120  * addressed PDP boundary (512GB for 4k pages), there will be more allocations
   1121  * required by the caller, This is not currently possible, and the BUG in the
   1122  * code will prevent it.
   1123  *
   1124  * Return: 0 if success; negative error code otherwise.
   1125  */
   1126 static int
   1127 gen8_ppgtt_alloc_page_directories(struct i915_address_space *vm,
   1128 				  struct i915_page_directory_pointer *pdp,
   1129 				  uint64_t start,
   1130 				  uint64_t length,
   1131 				  unsigned long *new_pds)
   1132 {
   1133 	struct drm_device *dev = vm->dev;
   1134 	struct i915_page_directory *pd;
   1135 	uint64_t temp;
   1136 	uint32_t pdpe;
   1137 	uint32_t pdpes = I915_PDPES_PER_PDP(dev);
   1138 
   1139 	WARN_ON(!bitmap_empty(new_pds, pdpes));
   1140 
   1141 	gen8_for_each_pdpe(pd, pdp, start, length, temp, pdpe) {
   1142 		if (test_bit(pdpe, pdp->used_pdpes))
   1143 			continue;
   1144 
   1145 		pd = alloc_pd(dev);
   1146 		if (IS_ERR(pd))
   1147 			goto unwind_out;
   1148 
   1149 		gen8_initialize_pd(vm, pd);
   1150 		pdp->page_directory[pdpe] = pd;
   1151 		__set_bit(pdpe, new_pds);
   1152 		trace_i915_page_directory_entry_alloc(vm, pdpe, start, GEN8_PDPE_SHIFT);
   1153 	}
   1154 
   1155 	return 0;
   1156 
   1157 unwind_out:
   1158 	for_each_set_bit(pdpe, new_pds, pdpes)
   1159 		free_pd(dev, pdp->page_directory[pdpe]);
   1160 
   1161 	return -ENOMEM;
   1162 }
   1163 
   1164 /**
   1165  * gen8_ppgtt_alloc_page_dirpointers() - Allocate pdps for VA range.
   1166  * @vm:	Master vm structure.
   1167  * @pml4:	Page map level 4 for this address range.
   1168  * @start:	Starting virtual address to begin allocations.
   1169  * @length:	Size of the allocations.
   1170  * @new_pdps:	Bitmap set by function with new allocations. Likely used by the
   1171  *		caller to free on error.
   1172  *
   1173  * Allocate the required number of page directory pointers. Extremely similar to
   1174  * gen8_ppgtt_alloc_page_directories() and gen8_ppgtt_alloc_pagetabs().
   1175  * The main difference is here we are limited by the pml4 boundary (instead of
   1176  * the page directory pointer).
   1177  *
   1178  * Return: 0 if success; negative error code otherwise.
   1179  */
   1180 static int
   1181 gen8_ppgtt_alloc_page_dirpointers(struct i915_address_space *vm,
   1182 				  struct i915_pml4 *pml4,
   1183 				  uint64_t start,
   1184 				  uint64_t length,
   1185 				  unsigned long *new_pdps)
   1186 {
   1187 	struct drm_device *dev = vm->dev;
   1188 	struct i915_page_directory_pointer *pdp;
   1189 	uint64_t temp;
   1190 	uint32_t pml4e;
   1191 
   1192 	WARN_ON(!bitmap_empty(new_pdps, GEN8_PML4ES_PER_PML4));
   1193 
   1194 	gen8_for_each_pml4e(pdp, pml4, start, length, temp, pml4e) {
   1195 		if (!test_bit(pml4e, pml4->used_pml4es)) {
   1196 			pdp = alloc_pdp(dev);
   1197 			if (IS_ERR(pdp))
   1198 				goto unwind_out;
   1199 
   1200 			gen8_initialize_pdp(vm, pdp);
   1201 			pml4->pdps[pml4e] = pdp;
   1202 			__set_bit(pml4e, new_pdps);
   1203 			trace_i915_page_directory_pointer_entry_alloc(vm,
   1204 								      pml4e,
   1205 								      start,
   1206 								      GEN8_PML4E_SHIFT);
   1207 		}
   1208 	}
   1209 
   1210 	return 0;
   1211 
   1212 unwind_out:
   1213 	for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
   1214 		free_pdp(dev, pml4->pdps[pml4e]);
   1215 
   1216 	return -ENOMEM;
   1217 }
   1218 
   1219 static void
   1220 free_gen8_temp_bitmaps(unsigned long *new_pds, unsigned long *new_pts)
   1221 {
   1222 	kfree(new_pts);
   1223 	kfree(new_pds);
   1224 }
   1225 
   1226 /* Fills in the page directory bitmap, and the array of page tables bitmap. Both
   1227  * of these are based on the number of PDPEs in the system.
   1228  */
   1229 static
   1230 int __must_check alloc_gen8_temp_bitmaps(unsigned long **new_pds,
   1231 					 unsigned long **new_pts,
   1232 					 uint32_t pdpes)
   1233 {
   1234 	unsigned long *pds;
   1235 	unsigned long *pts;
   1236 
   1237 	pds = kcalloc(BITS_TO_LONGS(pdpes), sizeof(unsigned long), GFP_TEMPORARY);
   1238 	if (!pds)
   1239 		return -ENOMEM;
   1240 
   1241 	pts = kcalloc(pdpes, BITS_TO_LONGS(I915_PDES) * sizeof(unsigned long),
   1242 		      GFP_TEMPORARY);
   1243 	if (!pts)
   1244 		goto err_out;
   1245 
   1246 	*new_pds = pds;
   1247 	*new_pts = pts;
   1248 
   1249 	return 0;
   1250 
   1251 err_out:
   1252 	free_gen8_temp_bitmaps(pds, pts);
   1253 	return -ENOMEM;
   1254 }
   1255 
   1256 /* PDE TLBs are a pain to invalidate on GEN8+. When we modify
   1257  * the page table structures, we mark them dirty so that
   1258  * context switching/execlist queuing code takes extra steps
   1259  * to ensure that tlbs are flushed.
   1260  */
   1261 static void mark_tlbs_dirty(struct i915_hw_ppgtt *ppgtt)
   1262 {
   1263 	ppgtt->pd_dirty_rings = INTEL_INFO(ppgtt->base.dev)->ring_mask;
   1264 }
   1265 
   1266 static int gen8_alloc_va_range_3lvl(struct i915_address_space *vm,
   1267 				    struct i915_page_directory_pointer *pdp,
   1268 				    uint64_t start,
   1269 				    uint64_t length)
   1270 {
   1271 	struct i915_hw_ppgtt *ppgtt =
   1272 		container_of(vm, struct i915_hw_ppgtt, base);
   1273 	unsigned long *new_page_dirs, *new_page_tables;
   1274 	struct drm_device *dev = vm->dev;
   1275 	struct i915_page_directory *pd;
   1276 	const uint64_t orig_start = start;
   1277 	const uint64_t orig_length = length;
   1278 	uint64_t temp;
   1279 	uint32_t pdpe;
   1280 	uint32_t pdpes = I915_PDPES_PER_PDP(dev);
   1281 	int ret;
   1282 
   1283 	/* Wrap is never okay since we can only represent 48b, and we don't
   1284 	 * actually use the other side of the canonical address space.
   1285 	 */
   1286 	if (WARN_ON(start + length < start))
   1287 		return -ENODEV;
   1288 
   1289 	if (WARN_ON(start + length > vm->total))
   1290 		return -ENODEV;
   1291 
   1292 	ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
   1293 	if (ret)
   1294 		return ret;
   1295 
   1296 	/* Do the allocations first so we can easily bail out */
   1297 	ret = gen8_ppgtt_alloc_page_directories(vm, pdp, start, length,
   1298 						new_page_dirs);
   1299 	if (ret) {
   1300 		free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
   1301 		return ret;
   1302 	}
   1303 
   1304 	/* For every page directory referenced, allocate page tables */
   1305 	gen8_for_each_pdpe(pd, pdp, start, length, temp, pdpe) {
   1306 		ret = gen8_ppgtt_alloc_pagetabs(vm, pd, start, length,
   1307 						new_page_tables + pdpe * BITS_TO_LONGS(I915_PDES));
   1308 		if (ret)
   1309 			goto err_out;
   1310 	}
   1311 
   1312 	start = orig_start;
   1313 	length = orig_length;
   1314 
   1315 	/* Allocations have completed successfully, so set the bitmaps, and do
   1316 	 * the mappings. */
   1317 	gen8_for_each_pdpe(pd, pdp, start, length, temp, pdpe) {
   1318 		gen8_pde_t *const page_directory = kmap_px(pd);
   1319 		struct i915_page_table *pt;
   1320 		uint64_t pd_len = length;
   1321 		uint64_t pd_start = start;
   1322 		uint32_t pde;
   1323 
   1324 		/* Every pd should be allocated, we just did that above. */
   1325 		WARN_ON(!pd);
   1326 
   1327 		gen8_for_each_pde(pt, pd, pd_start, pd_len, temp, pde) {
   1328 			/* Same reasoning as pd */
   1329 			WARN_ON(!pt);
   1330 			WARN_ON(!pd_len);
   1331 			WARN_ON(!gen8_pte_count(pd_start, pd_len));
   1332 
   1333 			/* Set our used ptes within the page table */
   1334 			bitmap_set(pt->used_ptes,
   1335 				   gen8_pte_index(pd_start),
   1336 				   gen8_pte_count(pd_start, pd_len));
   1337 
   1338 			/* Our pde is now pointing to the pagetable, pt */
   1339 			__set_bit(pde, pd->used_pdes);
   1340 
   1341 			/* Map the PDE to the page table */
   1342 			page_directory[pde] = gen8_pde_encode(px_dma(pt),
   1343 							      I915_CACHE_LLC);
   1344 			trace_i915_page_table_entry_map(&ppgtt->base, pde, pt,
   1345 							gen8_pte_index(start),
   1346 							gen8_pte_count(start, length),
   1347 							GEN8_PTES);
   1348 
   1349 			/* NB: We haven't yet mapped ptes to pages. At this
   1350 			 * point we're still relying on insert_entries() */
   1351 		}
   1352 
   1353 		kunmap_px(ppgtt, page_directory);
   1354 		__set_bit(pdpe, pdp->used_pdpes);
   1355 		gen8_setup_page_directory(ppgtt, pdp, pd, pdpe);
   1356 	}
   1357 
   1358 	free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
   1359 	mark_tlbs_dirty(ppgtt);
   1360 	return 0;
   1361 
   1362 err_out:
   1363 	while (pdpe--) {
   1364 		for_each_set_bit(temp, new_page_tables + pdpe *
   1365 				BITS_TO_LONGS(I915_PDES), I915_PDES)
   1366 			free_pt(dev, pdp->page_directory[pdpe]->page_table[temp]);
   1367 	}
   1368 
   1369 	for_each_set_bit(pdpe, new_page_dirs, pdpes)
   1370 		free_pd(dev, pdp->page_directory[pdpe]);
   1371 
   1372 	free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
   1373 	mark_tlbs_dirty(ppgtt);
   1374 	return ret;
   1375 }
   1376 
   1377 static int gen8_alloc_va_range_4lvl(struct i915_address_space *vm,
   1378 				    struct i915_pml4 *pml4,
   1379 				    uint64_t start,
   1380 				    uint64_t length)
   1381 {
   1382 	DECLARE_BITMAP(new_pdps, GEN8_PML4ES_PER_PML4);
   1383 	struct i915_hw_ppgtt *ppgtt =
   1384 			container_of(vm, struct i915_hw_ppgtt, base);
   1385 	struct i915_page_directory_pointer *pdp;
   1386 	uint64_t temp, pml4e;
   1387 	int ret = 0;
   1388 
   1389 	/* Do the pml4 allocations first, so we don't need to track the newly
   1390 	 * allocated tables below the pdp */
   1391 	bitmap_zero(new_pdps, GEN8_PML4ES_PER_PML4);
   1392 
   1393 	/* The pagedirectory and pagetable allocations are done in the shared 3
   1394 	 * and 4 level code. Just allocate the pdps.
   1395 	 */
   1396 	ret = gen8_ppgtt_alloc_page_dirpointers(vm, pml4, start, length,
   1397 						new_pdps);
   1398 	if (ret)
   1399 		return ret;
   1400 
   1401 	WARN(bitmap_weight(new_pdps, GEN8_PML4ES_PER_PML4) > 2,
   1402 	     "The allocation has spanned more than 512GB. "
   1403 	     "It is highly likely this is incorrect.");
   1404 
   1405 	gen8_for_each_pml4e(pdp, pml4, start, length, temp, pml4e) {
   1406 		WARN_ON(!pdp);
   1407 
   1408 		ret = gen8_alloc_va_range_3lvl(vm, pdp, start, length);
   1409 		if (ret)
   1410 			goto err_out;
   1411 
   1412 		gen8_setup_page_directory_pointer(ppgtt, pml4, pdp, pml4e);
   1413 	}
   1414 
   1415 	bitmap_or(pml4->used_pml4es, new_pdps, pml4->used_pml4es,
   1416 		  GEN8_PML4ES_PER_PML4);
   1417 
   1418 	return 0;
   1419 
   1420 err_out:
   1421 	for_each_set_bit(pml4e, new_pdps, GEN8_PML4ES_PER_PML4)
   1422 		gen8_ppgtt_cleanup_3lvl(vm->dev, pml4->pdps[pml4e]);
   1423 
   1424 	return ret;
   1425 }
   1426 
   1427 static int gen8_alloc_va_range(struct i915_address_space *vm,
   1428 			       uint64_t start, uint64_t length)
   1429 {
   1430 	struct i915_hw_ppgtt *ppgtt =
   1431 		container_of(vm, struct i915_hw_ppgtt, base);
   1432 
   1433 	if (USES_FULL_48BIT_PPGTT(vm->dev))
   1434 		return gen8_alloc_va_range_4lvl(vm, &ppgtt->pml4, start, length);
   1435 	else
   1436 		return gen8_alloc_va_range_3lvl(vm, &ppgtt->pdp, start, length);
   1437 }
   1438 
   1439 static void gen8_dump_pdp(struct i915_page_directory_pointer *pdp,
   1440 			  uint64_t start, uint64_t length,
   1441 			  gen8_pte_t scratch_pte,
   1442 			  struct seq_file *m)
   1443 {
   1444 	struct i915_page_directory *pd;
   1445 	uint64_t temp;
   1446 	uint32_t pdpe;
   1447 
   1448 	gen8_for_each_pdpe(pd, pdp, start, length, temp, pdpe) {
   1449 		struct i915_page_table *pt;
   1450 		uint64_t pd_len = length;
   1451 		uint64_t pd_start = start;
   1452 		uint32_t pde;
   1453 
   1454 		if (!test_bit(pdpe, pdp->used_pdpes))
   1455 			continue;
   1456 
   1457 		seq_printf(m, "\tPDPE #%d\n", pdpe);
   1458 		gen8_for_each_pde(pt, pd, pd_start, pd_len, temp, pde) {
   1459 			uint32_t  pte;
   1460 			gen8_pte_t *pt_vaddr;
   1461 
   1462 			if (!test_bit(pde, pd->used_pdes))
   1463 				continue;
   1464 
   1465 			pt_vaddr = kmap_px(pt);
   1466 			for (pte = 0; pte < GEN8_PTES; pte += 4) {
   1467 				uint64_t va =
   1468 					(pdpe << GEN8_PDPE_SHIFT) |
   1469 					(pde << GEN8_PDE_SHIFT) |
   1470 					(pte << GEN8_PTE_SHIFT);
   1471 				int i;
   1472 				bool found = false;
   1473 
   1474 				for (i = 0; i < 4; i++)
   1475 					if (pt_vaddr[pte + i] != scratch_pte)
   1476 						found = true;
   1477 				if (!found)
   1478 					continue;
   1479 
   1480 				seq_printf(m, "\t\t0x%llx [%03d,%03d,%04d]: =", va, pdpe, pde, pte);
   1481 				for (i = 0; i < 4; i++) {
   1482 					if (pt_vaddr[pte + i] != scratch_pte)
   1483 						seq_printf(m, " %llx", pt_vaddr[pte + i]);
   1484 					else
   1485 						seq_puts(m, "  SCRATCH ");
   1486 				}
   1487 				seq_puts(m, "\n");
   1488 			}
   1489 			/* don't use kunmap_px, it could trigger
   1490 			 * an unnecessary flush.
   1491 			 */
   1492 			kunmap_atomic(pt_vaddr);
   1493 		}
   1494 	}
   1495 }
   1496 
   1497 static void gen8_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
   1498 {
   1499 	struct i915_address_space *vm = &ppgtt->base;
   1500 	uint64_t start = ppgtt->base.start;
   1501 	uint64_t length = ppgtt->base.total;
   1502 	gen8_pte_t scratch_pte = gen8_pte_encode(px_dma(vm->scratch_page),
   1503 						 I915_CACHE_LLC, true);
   1504 
   1505 	if (!USES_FULL_48BIT_PPGTT(vm->dev)) {
   1506 		gen8_dump_pdp(&ppgtt->pdp, start, length, scratch_pte, m);
   1507 	} else {
   1508 		uint64_t templ4, pml4e;
   1509 		struct i915_pml4 *pml4 = &ppgtt->pml4;
   1510 		struct i915_page_directory_pointer *pdp;
   1511 
   1512 		gen8_for_each_pml4e(pdp, pml4, start, length, templ4, pml4e) {
   1513 			if (!test_bit(pml4e, pml4->used_pml4es))
   1514 				continue;
   1515 
   1516 			seq_printf(m, "    PML4E #%llu\n", pml4e);
   1517 			gen8_dump_pdp(pdp, start, length, scratch_pte, m);
   1518 		}
   1519 	}
   1520 }
   1521 
   1522 static int gen8_preallocate_top_level_pdps(struct i915_hw_ppgtt *ppgtt)
   1523 {
   1524 	unsigned long *new_page_dirs, *new_page_tables;
   1525 	uint32_t pdpes = I915_PDPES_PER_PDP(dev);
   1526 	int ret;
   1527 
   1528 	/* We allocate temp bitmap for page tables for no gain
   1529 	 * but as this is for init only, lets keep the things simple
   1530 	 */
   1531 	ret = alloc_gen8_temp_bitmaps(&new_page_dirs, &new_page_tables, pdpes);
   1532 	if (ret)
   1533 		return ret;
   1534 
   1535 	/* Allocate for all pdps regardless of how the ppgtt
   1536 	 * was defined.
   1537 	 */
   1538 	ret = gen8_ppgtt_alloc_page_directories(&ppgtt->base, &ppgtt->pdp,
   1539 						0, 1ULL << 32,
   1540 						new_page_dirs);
   1541 	if (!ret)
   1542 		*ppgtt->pdp.used_pdpes = *new_page_dirs;
   1543 
   1544 	free_gen8_temp_bitmaps(new_page_dirs, new_page_tables);
   1545 
   1546 	return ret;
   1547 }
   1548 
   1549 /*
   1550  * GEN8 legacy ppgtt programming is accomplished through a max 4 PDP registers
   1551  * with a net effect resembling a 2-level page table in normal x86 terms. Each
   1552  * PDP represents 1GB of memory 4 * 512 * 512 * 4096 = 4GB legacy 32b address
   1553  * space.
   1554  *
   1555  */
   1556 static int gen8_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
   1557 {
   1558 	int ret;
   1559 
   1560 	ret = gen8_init_scratch(&ppgtt->base);
   1561 	if (ret)
   1562 		return ret;
   1563 
   1564 	ppgtt->base.start = 0;
   1565 	ppgtt->base.cleanup = gen8_ppgtt_cleanup;
   1566 	ppgtt->base.allocate_va_range = gen8_alloc_va_range;
   1567 	ppgtt->base.insert_entries = gen8_ppgtt_insert_entries;
   1568 	ppgtt->base.clear_range = gen8_ppgtt_clear_range;
   1569 	ppgtt->base.unbind_vma = ppgtt_unbind_vma;
   1570 	ppgtt->base.bind_vma = ppgtt_bind_vma;
   1571 	ppgtt->debug_dump = gen8_dump_ppgtt;
   1572 
   1573 	if (USES_FULL_48BIT_PPGTT(ppgtt->base.dev)) {
   1574 		ret = setup_px(ppgtt->base.dev, &ppgtt->pml4);
   1575 		if (ret)
   1576 			goto free_scratch;
   1577 
   1578 		gen8_initialize_pml4(&ppgtt->base, &ppgtt->pml4);
   1579 
   1580 		ppgtt->base.total = 1ULL << 48;
   1581 		ppgtt->switch_mm = gen8_48b_mm_switch;
   1582 	} else {
   1583 		ret = __pdp_init(ppgtt->base.dev, &ppgtt->pdp);
   1584 		if (ret)
   1585 			goto free_scratch;
   1586 
   1587 		ppgtt->base.total = 1ULL << 32;
   1588 		ppgtt->switch_mm = gen8_legacy_mm_switch;
   1589 		trace_i915_page_directory_pointer_entry_alloc(&ppgtt->base,
   1590 							      0, 0,
   1591 							      GEN8_PML4E_SHIFT);
   1592 
   1593 		if (intel_vgpu_active(ppgtt->base.dev)) {
   1594 			ret = gen8_preallocate_top_level_pdps(ppgtt);
   1595 			if (ret)
   1596 				goto free_scratch;
   1597 		}
   1598 	}
   1599 
   1600 	if (intel_vgpu_active(ppgtt->base.dev))
   1601 		gen8_ppgtt_notify_vgt(ppgtt, true);
   1602 
   1603 	return 0;
   1604 
   1605 free_scratch:
   1606 	gen8_free_scratch(&ppgtt->base);
   1607 	return ret;
   1608 }
   1609 
   1610 #ifndef __NetBSD__
   1611 static void gen6_dump_ppgtt(struct i915_hw_ppgtt *ppgtt, struct seq_file *m)
   1612 {
   1613 	struct i915_address_space *vm = &ppgtt->base;
   1614 	struct i915_page_table *unused;
   1615 	gen6_pte_t scratch_pte;
   1616 	uint32_t pd_entry;
   1617 	uint32_t  pte, pde, temp;
   1618 	uint32_t start = ppgtt->base.start, length = ppgtt->base.total;
   1619 
   1620 	scratch_pte = vm->pte_encode(px_dma(vm->scratch_page),
   1621 				     I915_CACHE_LLC, true, 0);
   1622 
   1623 	gen6_for_each_pde(unused, &ppgtt->pd, start, length, temp, pde) {
   1624 		u32 expected;
   1625 		gen6_pte_t *pt_vaddr;
   1626 		const dma_addr_t pt_addr = px_dma(ppgtt->pd.page_table[pde]);
   1627 		pd_entry = readl(ppgtt->pd_addr + pde);
   1628 		expected = (GEN6_PDE_ADDR_ENCODE(pt_addr) | GEN6_PDE_VALID);
   1629 
   1630 		if (pd_entry != expected)
   1631 			seq_printf(m, "\tPDE #%d mismatch: Actual PDE: %x Expected PDE: %x\n",
   1632 				   pde,
   1633 				   pd_entry,
   1634 				   expected);
   1635 		seq_printf(m, "\tPDE: %x\n", pd_entry);
   1636 
   1637 		pt_vaddr = kmap_px(ppgtt->pd.page_table[pde]);
   1638 
   1639 		for (pte = 0; pte < GEN6_PTES; pte+=4) {
   1640 			unsigned long va =
   1641 				(pde * PAGE_SIZE * GEN6_PTES) +
   1642 				(pte * PAGE_SIZE);
   1643 			int i;
   1644 			bool found = false;
   1645 			for (i = 0; i < 4; i++)
   1646 				if (pt_vaddr[pte + i] != scratch_pte)
   1647 					found = true;
   1648 			if (!found)
   1649 				continue;
   1650 
   1651 			seq_printf(m, "\t\t0x%lx [%03d,%04d]: =", va, pde, pte);
   1652 			for (i = 0; i < 4; i++) {
   1653 				if (pt_vaddr[pte + i] != scratch_pte)
   1654 					seq_printf(m, " %08x", pt_vaddr[pte + i]);
   1655 				else
   1656 					seq_puts(m, "  SCRATCH ");
   1657 			}
   1658 			seq_puts(m, "\n");
   1659 		}
   1660 		kunmap_px(ppgtt, pt_vaddr);
   1661 	}
   1662 }
   1663 #endif
   1664 
   1665 /* Write pde (index) from the page directory @pd to the page table @pt */
   1666 static void gen6_write_pde(struct i915_page_directory *pd,
   1667 			    const int pde, struct i915_page_table *pt)
   1668 {
   1669 	/* Caller needs to make sure the write completes if necessary */
   1670 	struct i915_hw_ppgtt *ppgtt =
   1671 		container_of(pd, struct i915_hw_ppgtt, pd);
   1672 #ifdef __NetBSD__
   1673 	struct drm_i915_private *dev_priv = ppgtt->base.dev->dev_private;
   1674 	const bus_space_tag_t bst = dev_priv->gtt.bst;
   1675 	const bus_space_handle_t bsh = dev_priv->gtt.bsh;
   1676 	const bus_addr_t pd_base = ppgtt->pd.base.ggtt_offset;
   1677 #endif
   1678 	u32 pd_entry;
   1679 
   1680 	pd_entry = GEN6_PDE_ADDR_ENCODE(px_dma(pt));
   1681 	pd_entry |= GEN6_PDE_VALID;
   1682 
   1683 #ifdef __NetBSD__
   1684 	bus_space_write_4(bst, bsh, pd_base + pde, pd_entry);
   1685 #else
   1686 	writel(pd_entry, ppgtt->pd_addr + pde);
   1687 #endif
   1688 }
   1689 
   1690 /* Write all the page tables found in the ppgtt structure to incrementing page
   1691  * directories. */
   1692 static void gen6_write_page_range(struct drm_i915_private *dev_priv,
   1693 				  struct i915_page_directory *pd,
   1694 				  uint32_t start, uint32_t length)
   1695 {
   1696 	struct i915_page_table *pt;
   1697 	uint32_t pde, temp;
   1698 
   1699 	gen6_for_each_pde(pt, pd, start, length, temp, pde)
   1700 		gen6_write_pde(pd, pde, pt);
   1701 
   1702 	/* Make sure write is complete before other code can use this page
   1703 	 * table. Also require for WC mapped PTEs */
   1704 #ifdef __NetBSD__
   1705 	bus_space_read_4(dev_priv->gtt.bst, dev_priv->gtt.bsh, 0);
   1706 #else
   1707 	readl(dev_priv->gtt.gsm);
   1708 #endif
   1709 }
   1710 
   1711 static uint32_t get_pd_offset(struct i915_hw_ppgtt *ppgtt)
   1712 {
   1713 	BUG_ON(ppgtt->pd.base.ggtt_offset & 0x3f);
   1714 
   1715 	return (ppgtt->pd.base.ggtt_offset / 64) << 16;
   1716 }
   1717 
   1718 static int hsw_mm_switch(struct i915_hw_ppgtt *ppgtt,
   1719 			 struct drm_i915_gem_request *req)
   1720 {
   1721 	struct intel_engine_cs *ring = req->ring;
   1722 	int ret;
   1723 
   1724 	/* NB: TLBs must be flushed and invalidated before a switch */
   1725 	ret = ring->flush(req, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
   1726 	if (ret)
   1727 		return ret;
   1728 
   1729 	ret = intel_ring_begin(req, 6);
   1730 	if (ret)
   1731 		return ret;
   1732 
   1733 	intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
   1734 	intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
   1735 	intel_ring_emit(ring, PP_DIR_DCLV_2G);
   1736 	intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
   1737 	intel_ring_emit(ring, get_pd_offset(ppgtt));
   1738 	intel_ring_emit(ring, MI_NOOP);
   1739 	intel_ring_advance(ring);
   1740 
   1741 	return 0;
   1742 }
   1743 
   1744 static int vgpu_mm_switch(struct i915_hw_ppgtt *ppgtt,
   1745 			  struct drm_i915_gem_request *req)
   1746 {
   1747 	struct intel_engine_cs *ring = req->ring;
   1748 	struct drm_i915_private *dev_priv = to_i915(ppgtt->base.dev);
   1749 
   1750 	I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
   1751 	I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
   1752 	return 0;
   1753 }
   1754 
   1755 static int gen7_mm_switch(struct i915_hw_ppgtt *ppgtt,
   1756 			  struct drm_i915_gem_request *req)
   1757 {
   1758 	struct intel_engine_cs *ring = req->ring;
   1759 	int ret;
   1760 
   1761 	/* NB: TLBs must be flushed and invalidated before a switch */
   1762 	ret = ring->flush(req, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
   1763 	if (ret)
   1764 		return ret;
   1765 
   1766 	ret = intel_ring_begin(req, 6);
   1767 	if (ret)
   1768 		return ret;
   1769 
   1770 	intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(2));
   1771 	intel_ring_emit(ring, RING_PP_DIR_DCLV(ring));
   1772 	intel_ring_emit(ring, PP_DIR_DCLV_2G);
   1773 	intel_ring_emit(ring, RING_PP_DIR_BASE(ring));
   1774 	intel_ring_emit(ring, get_pd_offset(ppgtt));
   1775 	intel_ring_emit(ring, MI_NOOP);
   1776 	intel_ring_advance(ring);
   1777 
   1778 	/* XXX: RCS is the only one to auto invalidate the TLBs? */
   1779 	if (ring->id != RCS) {
   1780 		ret = ring->flush(req, I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
   1781 		if (ret)
   1782 			return ret;
   1783 	}
   1784 
   1785 	return 0;
   1786 }
   1787 
   1788 static int gen6_mm_switch(struct i915_hw_ppgtt *ppgtt,
   1789 			  struct drm_i915_gem_request *req)
   1790 {
   1791 	struct intel_engine_cs *ring = req->ring;
   1792 	struct drm_device *dev = ppgtt->base.dev;
   1793 	struct drm_i915_private *dev_priv = dev->dev_private;
   1794 
   1795 
   1796 	I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
   1797 	I915_WRITE(RING_PP_DIR_BASE(ring), get_pd_offset(ppgtt));
   1798 
   1799 	POSTING_READ(RING_PP_DIR_DCLV(ring));
   1800 
   1801 	return 0;
   1802 }
   1803 
   1804 static void gen8_ppgtt_enable(struct drm_device *dev)
   1805 {
   1806 	struct drm_i915_private *dev_priv = dev->dev_private;
   1807 	struct intel_engine_cs *ring;
   1808 	int j;
   1809 
   1810 	for_each_ring(ring, dev_priv, j) {
   1811 		u32 four_level = USES_FULL_48BIT_PPGTT(dev) ? GEN8_GFX_PPGTT_48B : 0;
   1812 		I915_WRITE(RING_MODE_GEN7(ring),
   1813 			   _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE | four_level));
   1814 	}
   1815 }
   1816 
   1817 static void gen7_ppgtt_enable(struct drm_device *dev)
   1818 {
   1819 	struct drm_i915_private *dev_priv = dev->dev_private;
   1820 	struct intel_engine_cs *ring;
   1821 	uint32_t ecochk, ecobits;
   1822 	int i;
   1823 
   1824 	ecobits = I915_READ(GAC_ECO_BITS);
   1825 	I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_PPGTT_CACHE64B);
   1826 
   1827 	ecochk = I915_READ(GAM_ECOCHK);
   1828 	if (IS_HASWELL(dev)) {
   1829 		ecochk |= ECOCHK_PPGTT_WB_HSW;
   1830 	} else {
   1831 		ecochk |= ECOCHK_PPGTT_LLC_IVB;
   1832 		ecochk &= ~ECOCHK_PPGTT_GFDT_IVB;
   1833 	}
   1834 	I915_WRITE(GAM_ECOCHK, ecochk);
   1835 
   1836 	for_each_ring(ring, dev_priv, i) {
   1837 		/* GFX_MODE is per-ring on gen7+ */
   1838 		I915_WRITE(RING_MODE_GEN7(ring),
   1839 			   _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
   1840 	}
   1841 }
   1842 
   1843 static void gen6_ppgtt_enable(struct drm_device *dev)
   1844 {
   1845 	struct drm_i915_private *dev_priv = dev->dev_private;
   1846 	uint32_t ecochk, gab_ctl, ecobits;
   1847 
   1848 	ecobits = I915_READ(GAC_ECO_BITS);
   1849 	I915_WRITE(GAC_ECO_BITS, ecobits | ECOBITS_SNB_BIT |
   1850 		   ECOBITS_PPGTT_CACHE64B);
   1851 
   1852 	gab_ctl = I915_READ(GAB_CTL);
   1853 	I915_WRITE(GAB_CTL, gab_ctl | GAB_CTL_CONT_AFTER_PAGEFAULT);
   1854 
   1855 	ecochk = I915_READ(GAM_ECOCHK);
   1856 	I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT | ECOCHK_PPGTT_CACHE64B);
   1857 
   1858 	I915_WRITE(GFX_MODE, _MASKED_BIT_ENABLE(GFX_PPGTT_ENABLE));
   1859 }
   1860 
   1861 /* PPGTT support for Sandybdrige/Gen6 and later */
   1862 static void gen6_ppgtt_clear_range(struct i915_address_space *vm,
   1863 				   uint64_t start,
   1864 				   uint64_t length,
   1865 				   bool use_scratch)
   1866 {
   1867 	struct i915_hw_ppgtt *ppgtt =
   1868 		container_of(vm, struct i915_hw_ppgtt, base);
   1869 	gen6_pte_t *pt_vaddr, scratch_pte;
   1870 	unsigned first_entry = start >> PAGE_SHIFT;
   1871 	unsigned num_entries = length >> PAGE_SHIFT;
   1872 	unsigned act_pt = first_entry / GEN6_PTES;
   1873 	unsigned first_pte = first_entry % GEN6_PTES;
   1874 	unsigned last_pte, i;
   1875 
   1876 	scratch_pte = vm->pte_encode(px_dma(vm->scratch_page),
   1877 				     I915_CACHE_LLC, true, 0);
   1878 
   1879 	while (num_entries) {
   1880 		last_pte = first_pte + num_entries;
   1881 		if (last_pte > GEN6_PTES)
   1882 			last_pte = GEN6_PTES;
   1883 
   1884 		pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
   1885 
   1886 		for (i = first_pte; i < last_pte; i++)
   1887 			pt_vaddr[i] = scratch_pte;
   1888 
   1889 		kunmap_px(ppgtt, pt_vaddr);
   1890 
   1891 		num_entries -= last_pte - first_pte;
   1892 		first_pte = 0;
   1893 		act_pt++;
   1894 	}
   1895 }
   1896 
   1897 #ifdef __NetBSD__
   1898 static void
   1899 gen6_ppgtt_insert_entries(struct i915_address_space *vm, bus_dmamap_t dmamap,
   1900     uint64_t start, enum i915_cache_level cache_level)
   1901 {
   1902 	struct i915_hw_ppgtt *ppgtt =
   1903 		container_of(vm, struct i915_hw_ppgtt, base);
   1904 	gen6_gtt_pte_t *pt_vaddr;
   1905 	unsigned first_entry = start >> PAGE_SHIFT;
   1906 	unsigned act_pt = first_entry / GEN6_PTES;
   1907 	unsigned act_pte = first_entry % GEN6_PTES;
   1908 	unsigned seg;
   1909 	int ret;
   1910 
   1911 	pt_vaddr = NULL;
   1912 	KASSERT(0 < dmamap->dm_nsegs);
   1913 	for (seg = 0; seg < dmamap->dm_nsegs; seg++) {
   1914 		KASSERT(dmamap->dm_segs[seg].ds_len == PAGE_SIZE);
   1915 		if (pt_vaddr == NULL)
   1916 			pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
   1917 
   1918 		pt_vaddr[act_pte] =
   1919 			vm->pte_encode(dmamap->dm_segs[seg].ds_addr,
   1920 				       cache_level, true, flags);
   1921 
   1922 		if (++act_pte == GEN6_PTES) {
   1923 			kunmap_px(ppgtt, pt_vaddr);
   1924 			pt_vaddr = NULL;
   1925 			act_pt++;
   1926 			act_pte = 0;
   1927 		}
   1928 	}
   1929 	if (pt_vaddr)
   1930 		kunmap_px(ppgtt, pt_vaddr);
   1931 }
   1932 #else
   1933 static void gen6_ppgtt_insert_entries(struct i915_address_space *vm,
   1934 				      struct sg_table *pages,
   1935 				      uint64_t start,
   1936 				      enum i915_cache_level cache_level, u32 flags)
   1937 {
   1938 	struct i915_hw_ppgtt *ppgtt =
   1939 		container_of(vm, struct i915_hw_ppgtt, base);
   1940 	gen6_pte_t *pt_vaddr;
   1941 	unsigned first_entry = start >> PAGE_SHIFT;
   1942 	unsigned act_pt = first_entry / GEN6_PTES;
   1943 	unsigned act_pte = first_entry % GEN6_PTES;
   1944 	struct sg_page_iter sg_iter;
   1945 
   1946 	pt_vaddr = NULL;
   1947 	for_each_sg_page(pages->sgl, &sg_iter, pages->nents, 0) {
   1948 		if (pt_vaddr == NULL)
   1949 			pt_vaddr = kmap_px(ppgtt->pd.page_table[act_pt]);
   1950 
   1951 		pt_vaddr[act_pte] =
   1952 			vm->pte_encode(sg_page_iter_dma_address(&sg_iter),
   1953 				       cache_level, true, flags);
   1954 
   1955 		if (++act_pte == GEN6_PTES) {
   1956 			kunmap_px(ppgtt, pt_vaddr);
   1957 			pt_vaddr = NULL;
   1958 			act_pt++;
   1959 			act_pte = 0;
   1960 		}
   1961 	}
   1962 	if (pt_vaddr)
   1963 		kunmap_px(ppgtt, pt_vaddr);
   1964 }
   1965 #endif
   1966 
   1967 static int gen6_alloc_va_range(struct i915_address_space *vm,
   1968 			       uint64_t start_in, uint64_t length_in)
   1969 {
   1970 	DECLARE_BITMAP(new_page_tables, I915_PDES);
   1971 	struct drm_device *dev = vm->dev;
   1972 	struct drm_i915_private *dev_priv = dev->dev_private;
   1973 	struct i915_hw_ppgtt *ppgtt =
   1974 				container_of(vm, struct i915_hw_ppgtt, base);
   1975 	struct i915_page_table *pt;
   1976 	uint32_t start, length, start_save, length_save;
   1977 	uint32_t pde, temp;
   1978 	int ret;
   1979 
   1980 	if (WARN_ON(start_in + length_in > ppgtt->base.total))
   1981 		return -ENODEV;
   1982 
   1983 	start = start_save = start_in;
   1984 	length = length_save = length_in;
   1985 
   1986 	bitmap_zero(new_page_tables, I915_PDES);
   1987 
   1988 	/* The allocation is done in two stages so that we can bail out with
   1989 	 * minimal amount of pain. The first stage finds new page tables that
   1990 	 * need allocation. The second stage marks use ptes within the page
   1991 	 * tables.
   1992 	 */
   1993 	gen6_for_each_pde(pt, &ppgtt->pd, start, length, temp, pde) {
   1994 		if (pt != vm->scratch_pt) {
   1995 			WARN_ON(bitmap_empty(pt->used_ptes, GEN6_PTES));
   1996 			continue;
   1997 		}
   1998 
   1999 		/* We've already allocated a page table */
   2000 		WARN_ON(!bitmap_empty(pt->used_ptes, GEN6_PTES));
   2001 
   2002 		pt = alloc_pt(dev);
   2003 		if (IS_ERR(pt)) {
   2004 			ret = PTR_ERR(pt);
   2005 			goto unwind_out;
   2006 		}
   2007 
   2008 		gen6_initialize_pt(vm, pt);
   2009 
   2010 		ppgtt->pd.page_table[pde] = pt;
   2011 		__set_bit(pde, new_page_tables);
   2012 		trace_i915_page_table_entry_alloc(vm, pde, start, GEN6_PDE_SHIFT);
   2013 	}
   2014 
   2015 	start = start_save;
   2016 	length = length_save;
   2017 
   2018 	gen6_for_each_pde(pt, &ppgtt->pd, start, length, temp, pde) {
   2019 		DECLARE_BITMAP(tmp_bitmap, GEN6_PTES);
   2020 
   2021 		bitmap_zero(tmp_bitmap, GEN6_PTES);
   2022 		bitmap_set(tmp_bitmap, gen6_pte_index(start),
   2023 			   gen6_pte_count(start, length));
   2024 
   2025 		if (__test_and_clear_bit(pde, new_page_tables))
   2026 			gen6_write_pde(&ppgtt->pd, pde, pt);
   2027 
   2028 		trace_i915_page_table_entry_map(vm, pde, pt,
   2029 					 gen6_pte_index(start),
   2030 					 gen6_pte_count(start, length),
   2031 					 GEN6_PTES);
   2032 		bitmap_or(pt->used_ptes, tmp_bitmap, pt->used_ptes,
   2033 				GEN6_PTES);
   2034 	}
   2035 
   2036 	WARN_ON(!bitmap_empty(new_page_tables, I915_PDES));
   2037 
   2038 	/* Make sure write is complete before other code can use this page
   2039 	 * table. Also require for WC mapped PTEs */
   2040 #ifdef __NetBSD__
   2041 	bus_space_read_4(dev_priv->gtt.bst, dev_priv->gtt.bsh, 0);
   2042 #else
   2043 	readl(dev_priv->gtt.gsm);
   2044 #endif
   2045 
   2046 	mark_tlbs_dirty(ppgtt);
   2047 	return 0;
   2048 
   2049 unwind_out:
   2050 	for_each_set_bit(pde, new_page_tables, I915_PDES) {
   2051 		struct i915_page_table *pt = ppgtt->pd.page_table[pde];
   2052 
   2053 		ppgtt->pd.page_table[pde] = vm->scratch_pt;
   2054 		free_pt(vm->dev, pt);
   2055 	}
   2056 
   2057 	mark_tlbs_dirty(ppgtt);
   2058 	return ret;
   2059 }
   2060 
   2061 static int gen6_init_scratch(struct i915_address_space *vm)
   2062 {
   2063 	struct drm_device *dev = vm->dev;
   2064 
   2065 	vm->scratch_page = alloc_scratch_page(dev);
   2066 	if (IS_ERR(vm->scratch_page))
   2067 		return PTR_ERR(vm->scratch_page);
   2068 
   2069 	vm->scratch_pt = alloc_pt(dev);
   2070 	if (IS_ERR(vm->scratch_pt)) {
   2071 		free_scratch_page(dev, vm->scratch_page);
   2072 		return PTR_ERR(vm->scratch_pt);
   2073 	}
   2074 
   2075 	gen6_initialize_pt(vm, vm->scratch_pt);
   2076 
   2077 	return 0;
   2078 }
   2079 
   2080 static void gen6_free_scratch(struct i915_address_space *vm)
   2081 {
   2082 	struct drm_device *dev = vm->dev;
   2083 
   2084 	free_pt(dev, vm->scratch_pt);
   2085 	free_scratch_page(dev, vm->scratch_page);
   2086 }
   2087 
   2088 static void gen6_ppgtt_cleanup(struct i915_address_space *vm)
   2089 {
   2090 	struct i915_hw_ppgtt *ppgtt =
   2091 		container_of(vm, struct i915_hw_ppgtt, base);
   2092 	struct i915_page_table *pt;
   2093 	uint32_t pde;
   2094 
   2095 	drm_mm_remove_node(&ppgtt->node);
   2096 
   2097 	gen6_for_all_pdes(pt, ppgtt, pde) {
   2098 		if (pt != vm->scratch_pt)
   2099 			free_pt(ppgtt->base.dev, pt);
   2100 	}
   2101 
   2102 	gen6_free_scratch(vm);
   2103 }
   2104 
   2105 static int gen6_ppgtt_allocate_page_directories(struct i915_hw_ppgtt *ppgtt)
   2106 {
   2107 	struct i915_address_space *vm = &ppgtt->base;
   2108 	struct drm_device *dev = ppgtt->base.dev;
   2109 	struct drm_i915_private *dev_priv = dev->dev_private;
   2110 	bool retried = false;
   2111 	int ret;
   2112 
   2113 	/* PPGTT PDEs reside in the GGTT and consists of 512 entries. The
   2114 	 * allocator works in address space sizes, so it's multiplied by page
   2115 	 * size. We allocate at the top of the GTT to avoid fragmentation.
   2116 	 */
   2117 	BUG_ON(!drm_mm_initialized(&dev_priv->gtt.base.mm));
   2118 
   2119 	ret = gen6_init_scratch(vm);
   2120 	if (ret)
   2121 		return ret;
   2122 
   2123 alloc:
   2124 	ret = drm_mm_insert_node_in_range_generic(&dev_priv->gtt.base.mm,
   2125 						  &ppgtt->node, GEN6_PD_SIZE,
   2126 						  GEN6_PD_ALIGN, 0,
   2127 						  0, dev_priv->gtt.base.total,
   2128 						  DRM_MM_TOPDOWN);
   2129 	if (ret == -ENOSPC && !retried) {
   2130 		ret = i915_gem_evict_something(dev, &dev_priv->gtt.base,
   2131 					       GEN6_PD_SIZE, GEN6_PD_ALIGN,
   2132 					       I915_CACHE_NONE,
   2133 					       0, dev_priv->gtt.base.total,
   2134 					       0);
   2135 		if (ret)
   2136 			goto err_out;
   2137 
   2138 		retried = true;
   2139 		goto alloc;
   2140 	}
   2141 
   2142 	if (ret)
   2143 		goto err_out;
   2144 
   2145 
   2146 	if (ppgtt->node.start < dev_priv->gtt.mappable_end)
   2147 		DRM_DEBUG("Forced to use aperture for PDEs\n");
   2148 
   2149 	return 0;
   2150 
   2151 err_out:
   2152 	gen6_free_scratch(vm);
   2153 	return ret;
   2154 }
   2155 
   2156 static int gen6_ppgtt_alloc(struct i915_hw_ppgtt *ppgtt)
   2157 {
   2158 	return gen6_ppgtt_allocate_page_directories(ppgtt);
   2159 }
   2160 
   2161 static void gen6_scratch_va_range(struct i915_hw_ppgtt *ppgtt,
   2162 				  uint64_t start, uint64_t length)
   2163 {
   2164 	struct i915_page_table *unused;
   2165 	uint32_t pde, temp;
   2166 
   2167 	gen6_for_each_pde(unused, &ppgtt->pd, start, length, temp, pde)
   2168 		ppgtt->pd.page_table[pde] = ppgtt->base.scratch_pt;
   2169 }
   2170 
   2171 static int gen6_ppgtt_init(struct i915_hw_ppgtt *ppgtt)
   2172 {
   2173 	struct drm_device *dev = ppgtt->base.dev;
   2174 	struct drm_i915_private *dev_priv = dev->dev_private;
   2175 	int ret;
   2176 
   2177 	ppgtt->base.pte_encode = dev_priv->gtt.base.pte_encode;
   2178 	if (IS_GEN6(dev)) {
   2179 		ppgtt->switch_mm = gen6_mm_switch;
   2180 	} else if (IS_HASWELL(dev)) {
   2181 		ppgtt->switch_mm = hsw_mm_switch;
   2182 	} else if (IS_GEN7(dev)) {
   2183 		ppgtt->switch_mm = gen7_mm_switch;
   2184 	} else
   2185 		BUG();
   2186 
   2187 	if (intel_vgpu_active(dev))
   2188 		ppgtt->switch_mm = vgpu_mm_switch;
   2189 
   2190 	ret = gen6_ppgtt_alloc(ppgtt);
   2191 	if (ret)
   2192 		return ret;
   2193 
   2194 	ppgtt->base.allocate_va_range = gen6_alloc_va_range;
   2195 	ppgtt->base.clear_range = gen6_ppgtt_clear_range;
   2196 	ppgtt->base.insert_entries = gen6_ppgtt_insert_entries;
   2197 	ppgtt->base.unbind_vma = ppgtt_unbind_vma;
   2198 	ppgtt->base.bind_vma = ppgtt_bind_vma;
   2199 	ppgtt->base.cleanup = gen6_ppgtt_cleanup;
   2200 	ppgtt->base.start = 0;
   2201 	ppgtt->base.total = I915_PDES * GEN6_PTES * PAGE_SIZE;
   2202 #ifndef __NetBSD__
   2203 	ppgtt->debug_dump = gen6_dump_ppgtt;
   2204 #endif
   2205 
   2206 	ppgtt->pd.base.ggtt_offset =
   2207 		ppgtt->node.start / PAGE_SIZE * sizeof(gen6_pte_t);
   2208 
   2209 	ppgtt->pd_addr = (gen6_pte_t __iomem *)dev_priv->gtt.gsm +
   2210 		ppgtt->pd.base.ggtt_offset / sizeof(gen6_pte_t);
   2211 
   2212 	gen6_scratch_va_range(ppgtt, 0, ppgtt->base.total);
   2213 
   2214 	gen6_write_page_range(dev_priv, &ppgtt->pd, 0, ppgtt->base.total);
   2215 
   2216 	DRM_DEBUG_DRIVER("Allocated pde space (%lldM) at GTT entry: %llx\n",
   2217 			 ppgtt->node.size >> 20,
   2218 			 ppgtt->node.start / PAGE_SIZE);
   2219 
   2220 	DRM_DEBUG("Adding PPGTT at offset %x\n",
   2221 		  ppgtt->pd.base.ggtt_offset << 10);
   2222 
   2223 	return 0;
   2224 }
   2225 
   2226 static int __hw_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt)
   2227 {
   2228 	ppgtt->base.dev = dev;
   2229 
   2230 	if (INTEL_INFO(dev)->gen < 8)
   2231 		return gen6_ppgtt_init(ppgtt);
   2232 	else
   2233 		return gen8_ppgtt_init(ppgtt);
   2234 }
   2235 
   2236 static void i915_address_space_init(struct i915_address_space *vm,
   2237 				    struct drm_i915_private *dev_priv)
   2238 {
   2239 	drm_mm_init(&vm->mm, vm->start, vm->total);
   2240 	vm->dev = dev_priv->dev;
   2241 	INIT_LIST_HEAD(&vm->active_list);
   2242 	INIT_LIST_HEAD(&vm->inactive_list);
   2243 	list_add_tail(&vm->global_link, &dev_priv->vm_list);
   2244 }
   2245 
   2246 int i915_ppgtt_init(struct drm_device *dev, struct i915_hw_ppgtt *ppgtt)
   2247 {
   2248 	struct drm_i915_private *dev_priv = dev->dev_private;
   2249 	int ret = 0;
   2250 
   2251 	ret = __hw_ppgtt_init(dev, ppgtt);
   2252 	if (ret == 0) {
   2253 		kref_init(&ppgtt->ref);
   2254 		i915_address_space_init(&ppgtt->base, dev_priv);
   2255 	}
   2256 
   2257 	return ret;
   2258 }
   2259 
   2260 int i915_ppgtt_init_hw(struct drm_device *dev)
   2261 {
   2262 	/* In the case of execlists, PPGTT is enabled by the context descriptor
   2263 	 * and the PDPs are contained within the context itself.  We don't
   2264 	 * need to do anything here. */
   2265 	if (i915.enable_execlists)
   2266 		return 0;
   2267 
   2268 	if (!USES_PPGTT(dev))
   2269 		return 0;
   2270 
   2271 	if (IS_GEN6(dev))
   2272 		gen6_ppgtt_enable(dev);
   2273 	else if (IS_GEN7(dev))
   2274 		gen7_ppgtt_enable(dev);
   2275 	else if (INTEL_INFO(dev)->gen >= 8)
   2276 		gen8_ppgtt_enable(dev);
   2277 	else
   2278 		MISSING_CASE(INTEL_INFO(dev)->gen);
   2279 
   2280 	return 0;
   2281 }
   2282 
   2283 int i915_ppgtt_init_ring(struct drm_i915_gem_request *req)
   2284 {
   2285 	struct drm_i915_private *dev_priv = req->ring->dev->dev_private;
   2286 	struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
   2287 
   2288 	if (i915.enable_execlists)
   2289 		return 0;
   2290 
   2291 	if (!ppgtt)
   2292 		return 0;
   2293 
   2294 	return ppgtt->switch_mm(ppgtt, req);
   2295 }
   2296 
   2297 struct i915_hw_ppgtt *
   2298 i915_ppgtt_create(struct drm_device *dev, struct drm_i915_file_private *fpriv)
   2299 {
   2300 	struct i915_hw_ppgtt *ppgtt;
   2301 	int ret;
   2302 
   2303 	ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
   2304 	if (!ppgtt)
   2305 		return ERR_PTR(-ENOMEM);
   2306 
   2307 	ret = i915_ppgtt_init(dev, ppgtt);
   2308 	if (ret) {
   2309 		kfree(ppgtt);
   2310 		return ERR_PTR(ret);
   2311 	}
   2312 
   2313 	ppgtt->file_priv = fpriv;
   2314 
   2315 	trace_i915_ppgtt_create(&ppgtt->base);
   2316 
   2317 	return ppgtt;
   2318 }
   2319 
   2320 void  i915_ppgtt_release(struct kref *kref)
   2321 {
   2322 	struct i915_hw_ppgtt *ppgtt =
   2323 		container_of(kref, struct i915_hw_ppgtt, ref);
   2324 
   2325 	trace_i915_ppgtt_release(&ppgtt->base);
   2326 
   2327 	/* vmas should already be unbound */
   2328 	WARN_ON(!list_empty(&ppgtt->base.active_list));
   2329 	WARN_ON(!list_empty(&ppgtt->base.inactive_list));
   2330 
   2331 	list_del(&ppgtt->base.global_link);
   2332 	drm_mm_takedown(&ppgtt->base.mm);
   2333 
   2334 	ppgtt->base.cleanup(&ppgtt->base);
   2335 	kfree(ppgtt);
   2336 }
   2337 
   2338 extern int intel_iommu_gfx_mapped;
   2339 /* Certain Gen5 chipsets require require idling the GPU before
   2340  * unmapping anything from the GTT when VT-d is enabled.
   2341  */
   2342 static bool needs_idle_maps(struct drm_device *dev)
   2343 {
   2344 #ifdef CONFIG_INTEL_IOMMU
   2345 	/* Query intel_iommu to see if we need the workaround. Presumably that
   2346 	 * was loaded first.
   2347 	 */
   2348 	if (IS_GEN5(dev) && IS_MOBILE(dev) && intel_iommu_gfx_mapped)
   2349 		return true;
   2350 #endif
   2351 	return false;
   2352 }
   2353 
   2354 static bool do_idling(struct drm_i915_private *dev_priv)
   2355 {
   2356 	bool ret = dev_priv->mm.interruptible;
   2357 
   2358 	if (unlikely(dev_priv->gtt.do_idle_maps)) {
   2359 		dev_priv->mm.interruptible = false;
   2360 		if (i915_gpu_idle(dev_priv->dev)) {
   2361 			DRM_ERROR("Couldn't idle GPU\n");
   2362 			/* Wait a bit, in hopes it avoids the hang */
   2363 			udelay(10);
   2364 		}
   2365 	}
   2366 
   2367 	return ret;
   2368 }
   2369 
   2370 static void undo_idling(struct drm_i915_private *dev_priv, bool interruptible)
   2371 {
   2372 	if (unlikely(dev_priv->gtt.do_idle_maps))
   2373 		dev_priv->mm.interruptible = interruptible;
   2374 }
   2375 
   2376 void i915_check_and_clear_faults(struct drm_device *dev)
   2377 {
   2378 	struct drm_i915_private *dev_priv = dev->dev_private;
   2379 	struct intel_engine_cs *ring;
   2380 	int i;
   2381 
   2382 	if (INTEL_INFO(dev)->gen < 6)
   2383 		return;
   2384 
   2385 	for_each_ring(ring, dev_priv, i) {
   2386 		u32 fault_reg;
   2387 		fault_reg = I915_READ(RING_FAULT_REG(ring));
   2388 		if (fault_reg & RING_FAULT_VALID) {
   2389 			DRM_DEBUG_DRIVER("Unexpected fault\n"
   2390 					 "\tAddr: 0x%08"PRIx32"\n"
   2391 					 "\tAddress space: %s\n"
   2392 					 "\tSource ID: %d\n"
   2393 					 "\tType: %d\n",
   2394 					 fault_reg & PAGE_MASK,
   2395 					 fault_reg & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
   2396 					 RING_FAULT_SRCID(fault_reg),
   2397 					 RING_FAULT_FAULT_TYPE(fault_reg));
   2398 			I915_WRITE(RING_FAULT_REG(ring),
   2399 				   fault_reg & ~RING_FAULT_VALID);
   2400 		}
   2401 	}
   2402 	POSTING_READ(RING_FAULT_REG(&dev_priv->ring[RCS]));
   2403 }
   2404 
   2405 static void i915_ggtt_flush(struct drm_i915_private *dev_priv)
   2406 {
   2407 	if (INTEL_INFO(dev_priv->dev)->gen < 6) {
   2408 		intel_gtt_chipset_flush();
   2409 	} else {
   2410 		I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
   2411 		POSTING_READ(GFX_FLSH_CNTL_GEN6);
   2412 	}
   2413 }
   2414 
   2415 void i915_gem_suspend_gtt_mappings(struct drm_device *dev)
   2416 {
   2417 	struct drm_i915_private *dev_priv = dev->dev_private;
   2418 
   2419 	/* Don't bother messing with faults pre GEN6 as we have little
   2420 	 * documentation supporting that it's a good idea.
   2421 	 */
   2422 	if (INTEL_INFO(dev)->gen < 6)
   2423 		return;
   2424 
   2425 	i915_check_and_clear_faults(dev);
   2426 
   2427 	dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
   2428 				       dev_priv->gtt.base.start,
   2429 				       dev_priv->gtt.base.total,
   2430 				       true);
   2431 
   2432 	i915_ggtt_flush(dev_priv);
   2433 }
   2434 
   2435 int i915_gem_gtt_prepare_object(struct drm_i915_gem_object *obj)
   2436 {
   2437 #ifdef __NetBSD__
   2438 	KASSERT(0 < obj->base.size);
   2439 	/* XXX errno NetBSD->Linux */
   2440 	return -bus_dmamap_load_raw(obj->base.dev->dmat, obj->igo_dmamap,
   2441 	    obj->pages, obj->igo_nsegs, obj->base.size, BUS_DMA_NOWAIT);
   2442 #else
   2443 	if (!dma_map_sg(&obj->base.dev->pdev->dev,
   2444 			obj->pages->sgl, obj->pages->nents,
   2445 			PCI_DMA_BIDIRECTIONAL))
   2446 		return -ENOSPC;
   2447 
   2448 	return 0;
   2449 #endif
   2450 }
   2451 
   2452 #ifdef __NetBSD__
   2453 static uint64_t
   2454 gen8_get_pte(bus_space_tag_t bst, bus_space_handle_t bsh, unsigned i)
   2455 {
   2456 	CTASSERT(_BYTE_ORDER == _LITTLE_ENDIAN); /* x86 */
   2457 	CTASSERT(sizeof(gen8_gtt_pte_t) == 8);
   2458 #ifdef _LP64			/* XXX How to detect bus_space_read_8?  */
   2459 	return bus_space_read_8(bst, bsh, 8*i);
   2460 #else
   2461 	/*
   2462 	 * XXX I'm not sure this case can actually happen in practice:
   2463 	 * 32-bit gen8 chipsets?
   2464 	 */
   2465 	return bus_space_read_4(bst, bsh, 8*i) |
   2466 	    ((uint64_t)bus_space_read_4(bst, bsh, 8*i + 4) << 32);
   2467 #endif
   2468 }
   2469 
   2470 static inline void
   2471 gen8_set_pte(bus_space_tag_t bst, bus_space_handle_t bsh, unsigned i,
   2472     gen8_gtt_pte_t pte)
   2473 {
   2474 	CTASSERT(_BYTE_ORDER == _LITTLE_ENDIAN); /* x86 */
   2475 	CTASSERT(sizeof(gen8_gtt_pte_t) == 8);
   2476 #ifdef _LP64			/* XXX How to detect bus_space_write_8?  */
   2477 	bus_space_write_8(bst, bsh, 8*i, pte);
   2478 #else
   2479 	bus_space_write_4(bst, bsh, 8*i, (uint32_t)pte);
   2480 	bus_space_write_4(bst, bsh, 8*i + 4, (uint32_t)(pte >> 32));
   2481 #endif
   2482 }
   2483 #else
   2484 static void gen8_set_pte(void __iomem *addr, gen8_pte_t pte)
   2485 {
   2486 #ifdef writeq
   2487 	writeq(pte, addr);
   2488 #else
   2489 	iowrite32((u32)pte, addr);
   2490 	iowrite32(pte >> 32, addr + 4);
   2491 #endif
   2492 }
   2493 #endif
   2494 
   2495 #ifdef __NetBSD__
   2496 static void
   2497 gen8_ggtt_insert_entries(struct i915_address_space *vm, bus_dmamap_t dmamap,
   2498     uint64_t start, enum i915_cache_level level)
   2499 {
   2500 	struct drm_i915_private *dev_priv = vm->dev->dev_private;
   2501 	unsigned first_entry = start >> PAGE_SHIFT;
   2502 	const bus_space_tag_t bst = dev_priv->gtt.bst;
   2503 	const bus_space_handle_t bsh = dev_priv->gtt.bsh;
   2504 	unsigned i;
   2505 
   2506 	KASSERT(0 < dmamap->dm_nsegs);
   2507 	for (i = 0; i < dmamap->dm_nsegs; i++) {
   2508 		KASSERT(dmamap->dm_segs[i].ds_len == PAGE_SIZE);
   2509 		gen8_set_pte(bst, bsh, first_entry + i,
   2510 		    gen8_pte_encode(dmamap->dm_segs[i].ds_addr, level, true));
   2511 	}
   2512 	if (0 < i) {
   2513 		/* Posting read.  */
   2514 		WARN_ON(gen8_get_pte(bst, bsh, (first_entry + i - 1))
   2515 		    != gen8_pte_encode(dmamap->dm_segs[i - 1].ds_addr, level,
   2516 			true));
   2517 	}
   2518 	I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
   2519 	POSTING_READ(GFX_FLSH_CNTL_GEN6);
   2520 }
   2521 #else
   2522 static void gen8_ggtt_insert_entries(struct i915_address_space *vm,
   2523 				     struct sg_table *st,
   2524 				     uint64_t start,
   2525 				     enum i915_cache_level level, u32 unused)
   2526 {
   2527 	struct drm_i915_private *dev_priv = vm->dev->dev_private;
   2528 	unsigned first_entry = start >> PAGE_SHIFT;
   2529 	gen8_pte_t __iomem *gtt_entries =
   2530 		(gen8_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
   2531 	int i = 0;
   2532 	struct sg_page_iter sg_iter;
   2533 	dma_addr_t addr = 0; /* shut up gcc */
   2534 
   2535 	for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
   2536 		addr = sg_dma_address(sg_iter.sg) +
   2537 			(sg_iter.sg_pgoffset << PAGE_SHIFT);
   2538 		gen8_set_pte(&gtt_entries[i],
   2539 			     gen8_pte_encode(addr, level, true));
   2540 		i++;
   2541 	}
   2542 
   2543 	/*
   2544 	 * XXX: This serves as a posting read to make sure that the PTE has
   2545 	 * actually been updated. There is some concern that even though
   2546 	 * registers and PTEs are within the same BAR that they are potentially
   2547 	 * of NUMA access patterns. Therefore, even with the way we assume
   2548 	 * hardware should work, we must keep this posting read for paranoia.
   2549 	 */
   2550 	if (i != 0)
   2551 		WARN_ON(readq(&gtt_entries[i-1])
   2552 			!= gen8_pte_encode(addr, level, true));
   2553 
   2554 	/* This next bit makes the above posting read even more important. We
   2555 	 * want to flush the TLBs only after we're certain all the PTE updates
   2556 	 * have finished.
   2557 	 */
   2558 	I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
   2559 	POSTING_READ(GFX_FLSH_CNTL_GEN6);
   2560 }
   2561 #endif
   2562 
   2563 /*
   2564  * Binds an object into the global gtt with the specified cache level. The object
   2565  * will be accessible to the GPU via commands whose operands reference offsets
   2566  * within the global GTT as well as accessible by the GPU through the GMADR
   2567  * mapped BAR (dev_priv->mm.gtt->gtt).
   2568  */
   2569 #ifdef __NetBSD__
   2570 static void
   2571 gen6_ggtt_insert_entries(struct i915_address_space *vm, bus_dmamap_t dmamap,
   2572     uint64_t start, enum i915_cache_level level)
   2573 {
   2574 	struct drm_i915_private *dev_priv = vm->dev->dev_private;
   2575 	unsigned first_entry = start >> PAGE_SHIFT;
   2576 	const bus_space_tag_t bst = dev_priv->gtt.bst;
   2577 	const bus_space_handle_t bsh = dev_priv->gtt.bsh;
   2578 	unsigned i;
   2579 
   2580 	KASSERT(0 < dmamap->dm_nsegs);
   2581 	for (i = 0; i < dmamap->dm_nsegs; i++) {
   2582 		KASSERT(dmamap->dm_segs[i].ds_len == PAGE_SIZE);
   2583 		CTASSERT(sizeof(gen6_gtt_pte_t) == 4);
   2584 		bus_space_write_4(bst, bsh, 4*(first_entry + i),
   2585 		    vm->pte_encode(dmamap->dm_segs[i].ds_addr, level, true));
   2586 	}
   2587 	if (0 < i) {
   2588 		/* Posting read.  */
   2589 		WARN_ON(bus_space_read_4(bst, bsh, 4*(first_entry + i - 1))
   2590 		    != vm->pte_encode(dmamap->dm_segs[i - 1].ds_addr, level,
   2591 			true));
   2592 	}
   2593 	I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
   2594 	POSTING_READ(GFX_FLSH_CNTL_GEN6);
   2595 }
   2596 #else
   2597 static void gen6_ggtt_insert_entries(struct i915_address_space *vm,
   2598 				     struct sg_table *st,
   2599 				     uint64_t start,
   2600 				     enum i915_cache_level level, u32 flags)
   2601 {
   2602 	struct drm_i915_private *dev_priv = vm->dev->dev_private;
   2603 	unsigned first_entry = start >> PAGE_SHIFT;
   2604 	gen6_pte_t __iomem *gtt_entries =
   2605 		(gen6_pte_t __iomem *)dev_priv->gtt.gsm + first_entry;
   2606 	int i = 0;
   2607 	struct sg_page_iter sg_iter;
   2608 	dma_addr_t addr = 0;
   2609 
   2610 	for_each_sg_page(st->sgl, &sg_iter, st->nents, 0) {
   2611 		addr = sg_page_iter_dma_address(&sg_iter);
   2612 		iowrite32(vm->pte_encode(addr, level, true, flags), &gtt_entries[i]);
   2613 		i++;
   2614 	}
   2615 
   2616 	/* XXX: This serves as a posting read to make sure that the PTE has
   2617 	 * actually been updated. There is some concern that even though
   2618 	 * registers and PTEs are within the same BAR that they are potentially
   2619 	 * of NUMA access patterns. Therefore, even with the way we assume
   2620 	 * hardware should work, we must keep this posting read for paranoia.
   2621 	 */
   2622 	if (i != 0) {
   2623 		unsigned long gtt = readl(&gtt_entries[i-1]);
   2624 		WARN_ON(gtt != vm->pte_encode(addr, level, true, flags));
   2625 	}
   2626 
   2627 	/* This next bit makes the above posting read even more important. We
   2628 	 * want to flush the TLBs only after we're certain all the PTE updates
   2629 	 * have finished.
   2630 	 */
   2631 	I915_WRITE(GFX_FLSH_CNTL_GEN6, GFX_FLSH_CNTL_EN);
   2632 	POSTING_READ(GFX_FLSH_CNTL_GEN6);
   2633 }
   2634 #endif
   2635 
   2636 static void gen8_ggtt_clear_range(struct i915_address_space *vm,
   2637 				  uint64_t start,
   2638 				  uint64_t length,
   2639 				  bool use_scratch)
   2640 {
   2641 	struct drm_i915_private *dev_priv = vm->dev->dev_private;
   2642 	unsigned first_entry = start >> PAGE_SHIFT;
   2643 	unsigned num_entries = length >> PAGE_SHIFT;
   2644 #ifdef __NetBSD__
   2645 	const bus_space_tag_t bst = dev_priv->gtt.bst;
   2646 	const bus_space_handle_t bsh = dev_priv->gtt.bsh;
   2647 	gen8_pte_t scratch_pte;
   2648 #else
   2649 	gen8_pte_t scratch_pte, __iomem *gtt_base =
   2650 		(gen8_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
   2651 #endif
   2652 	const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
   2653 	int i;
   2654 
   2655 	if (WARN(num_entries > max_entries,
   2656 		 "First entry = %d; Num entries = %d (max=%d)\n",
   2657 		 first_entry, num_entries, max_entries))
   2658 		num_entries = max_entries;
   2659 
   2660 	scratch_pte = gen8_pte_encode(px_dma(vm->scratch_page),
   2661 				      I915_CACHE_LLC,
   2662 				      use_scratch);
   2663 #ifdef __NetBSD__
   2664 	CTASSERT(sizeof(gen8_gtt_pte_t) == 8);
   2665 	for (i = 0; i < num_entries; i++)
   2666 		gen8_set_pte(bst, bsh, first_entry + i, scratch_pte);
   2667 	(void)gen8_get_pte(bst, bsh, first_entry);
   2668 #else
   2669 	for (i = 0; i < num_entries; i++)
   2670 		gen8_set_pte(&gtt_base[i], scratch_pte);
   2671 	readl(gtt_base);
   2672 #endif
   2673 }
   2674 
   2675 static void gen6_ggtt_clear_range(struct i915_address_space *vm,
   2676 				  uint64_t start,
   2677 				  uint64_t length,
   2678 				  bool use_scratch)
   2679 {
   2680 	struct drm_i915_private *dev_priv = vm->dev->dev_private;
   2681 	unsigned first_entry = start >> PAGE_SHIFT;
   2682 	unsigned num_entries = length >> PAGE_SHIFT;
   2683 #ifdef __NetBSD__
   2684 	const bus_space_tag_t bst = dev_priv->gtt.bst;
   2685 	const bus_space_handle_t bsh = dev_priv->gtt.bsh;
   2686 	gen8_pte_t scratch_pte;
   2687 #else
   2688 	gen6_pte_t scratch_pte, __iomem *gtt_base =
   2689 		(gen6_pte_t __iomem *) dev_priv->gtt.gsm + first_entry;
   2690 #endif
   2691 	const int max_entries = gtt_total_entries(dev_priv->gtt) - first_entry;
   2692 	int i;
   2693 
   2694 	if (WARN(num_entries > max_entries,
   2695 		 "First entry = %d; Num entries = %d (max=%d)\n",
   2696 		 first_entry, num_entries, max_entries))
   2697 		num_entries = max_entries;
   2698 
   2699 	scratch_pte = vm->pte_encode(px_dma(vm->scratch_page),
   2700 				     I915_CACHE_LLC, use_scratch, 0);
   2701 
   2702 #ifdef __NetBSD__
   2703 	CTASSERT(sizeof(gen6_gtt_pte_t) == 4);
   2704 	for (i = 0; i < num_entries; i++)
   2705 		bus_space_write_4(bst, bsh, 4*(first_entry + i), scratch_pte);
   2706 	(void)bus_space_read_4(bst, bsh, 4*first_entry);
   2707 #else
   2708 	for (i = 0; i < num_entries; i++)
   2709 		iowrite32(scratch_pte, &gtt_base[i]);
   2710 	readl(gtt_base);
   2711 #endif
   2712 }
   2713 
   2714 static void i915_ggtt_insert_entries(struct i915_address_space *vm,
   2715 #ifdef __NetBSD__
   2716 				     bus_dmamap_t pages,
   2717 #else
   2718 				     struct sg_table *pages,
   2719 #endif
   2720 				     uint64_t start,
   2721 				     enum i915_cache_level cache_level, u32 unused)
   2722 {
   2723 	unsigned int flags = (cache_level == I915_CACHE_NONE) ?
   2724 		AGP_USER_MEMORY : AGP_USER_CACHED_MEMORY;
   2725 
   2726 	intel_gtt_insert_sg_entries(pages, start >> PAGE_SHIFT, flags);
   2727 }
   2728 
   2729 static void i915_ggtt_clear_range(struct i915_address_space *vm,
   2730 				  uint64_t start,
   2731 				  uint64_t length,
   2732 				  bool unused)
   2733 {
   2734 	unsigned first_entry = start >> PAGE_SHIFT;
   2735 	unsigned num_entries = length >> PAGE_SHIFT;
   2736 	intel_gtt_clear_range(first_entry, num_entries);
   2737 }
   2738 
   2739 static int ggtt_bind_vma(struct i915_vma *vma,
   2740 			 enum i915_cache_level cache_level,
   2741 			 u32 flags)
   2742 {
   2743 	struct drm_i915_gem_object *obj = vma->obj;
   2744 	u32 pte_flags = 0;
   2745 	int ret;
   2746 
   2747 	ret = i915_get_ggtt_vma_pages(vma);
   2748 	if (ret)
   2749 		return ret;
   2750 
   2751 	/* Currently applicable only to VLV */
   2752 	if (obj->gt_ro)
   2753 		pte_flags |= PTE_READ_ONLY;
   2754 
   2755 	vma->vm->insert_entries(vma->vm, vma->ggtt_view.pages,
   2756 				vma->node.start,
   2757 				cache_level, pte_flags);
   2758 
   2759 	/*
   2760 	 * Without aliasing PPGTT there's no difference between
   2761 	 * GLOBAL/LOCAL_BIND, it's all the same ptes. Hence unconditionally
   2762 	 * upgrade to both bound if we bind either to avoid double-binding.
   2763 	 */
   2764 	vma->bound |= GLOBAL_BIND | LOCAL_BIND;
   2765 
   2766 	return 0;
   2767 }
   2768 
   2769 static int aliasing_gtt_bind_vma(struct i915_vma *vma,
   2770 				 enum i915_cache_level cache_level,
   2771 				 u32 flags)
   2772 {
   2773 	struct drm_device *dev = vma->vm->dev;
   2774 	struct drm_i915_private *dev_priv = dev->dev_private;
   2775 	struct drm_i915_gem_object *obj = vma->obj;
   2776 #ifdef __NetBSD__
   2777 	bus_dmamap_t pages = obj->pages;
   2778 #else
   2779 	struct sg_table *pages = obj->pages;
   2780 #endif
   2781 	u32 pte_flags = 0;
   2782 	int ret;
   2783 
   2784 	ret = i915_get_ggtt_vma_pages(vma);
   2785 	if (ret)
   2786 		return ret;
   2787 	pages = vma->ggtt_view.pages;
   2788 
   2789 	/* Currently applicable only to VLV */
   2790 	if (obj->gt_ro)
   2791 		pte_flags |= PTE_READ_ONLY;
   2792 
   2793 
   2794 	if (flags & GLOBAL_BIND) {
   2795 		vma->vm->insert_entries(vma->vm, pages,
   2796 					vma->node.start,
   2797 					cache_level, pte_flags);
   2798 	}
   2799 
   2800 	if (flags & LOCAL_BIND) {
   2801 		struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
   2802 		appgtt->base.insert_entries(&appgtt->base, pages,
   2803 					    vma->node.start,
   2804 					    cache_level, pte_flags);
   2805 	}
   2806 
   2807 	return 0;
   2808 }
   2809 
   2810 static void ggtt_unbind_vma(struct i915_vma *vma)
   2811 {
   2812 	struct drm_device *dev = vma->vm->dev;
   2813 	struct drm_i915_private *dev_priv = dev->dev_private;
   2814 	struct drm_i915_gem_object *obj = vma->obj;
   2815 	const uint64_t size = min_t(uint64_t,
   2816 				    obj->base.size,
   2817 				    vma->node.size);
   2818 
   2819 	if (vma->bound & GLOBAL_BIND) {
   2820 		vma->vm->clear_range(vma->vm,
   2821 				     vma->node.start,
   2822 				     size,
   2823 				     true);
   2824 	}
   2825 
   2826 	if (dev_priv->mm.aliasing_ppgtt && vma->bound & LOCAL_BIND) {
   2827 		struct i915_hw_ppgtt *appgtt = dev_priv->mm.aliasing_ppgtt;
   2828 
   2829 		appgtt->base.clear_range(&appgtt->base,
   2830 					 vma->node.start,
   2831 					 size,
   2832 					 true);
   2833 	}
   2834 }
   2835 
   2836 void i915_gem_gtt_finish_object(struct drm_i915_gem_object *obj)
   2837 {
   2838 	struct drm_device *dev = obj->base.dev;
   2839 	struct drm_i915_private *dev_priv = dev->dev_private;
   2840 	bool interruptible;
   2841 
   2842 	interruptible = do_idling(dev_priv);
   2843 
   2844 #ifdef __NetBSD__
   2845 	bus_dmamap_unload(dev->dmat, obj->igo_dmamap);
   2846 #else
   2847 	dma_unmap_sg(&dev->pdev->dev, obj->pages->sgl, obj->pages->nents,
   2848 		     PCI_DMA_BIDIRECTIONAL);
   2849 #endif
   2850 
   2851 	undo_idling(dev_priv, interruptible);
   2852 }
   2853 
   2854 static void i915_gtt_color_adjust(struct drm_mm_node *node,
   2855 				  unsigned long color,
   2856 				  u64 *start,
   2857 				  u64 *end)
   2858 {
   2859 	if (node->color != color)
   2860 		*start += 4096;
   2861 
   2862 	if (!list_empty(&node->node_list)) {
   2863 		node = list_entry(node->node_list.next,
   2864 				  struct drm_mm_node,
   2865 				  node_list);
   2866 		if (node->allocated && node->color != color)
   2867 			*end -= 4096;
   2868 	}
   2869 }
   2870 
   2871 static int i915_gem_setup_global_gtt(struct drm_device *dev,
   2872 				     u64 start,
   2873 				     u64 mappable_end,
   2874 				     u64 end)
   2875 {
   2876 	/* Let GEM Manage all of the aperture.
   2877 	 *
   2878 	 * However, leave one page at the end still bound to the scratch page.
   2879 	 * There are a number of places where the hardware apparently prefetches
   2880 	 * past the end of the object, and we've seen multiple hangs with the
   2881 	 * GPU head pointer stuck in a batchbuffer bound at the last page of the
   2882 	 * aperture.  One page should be enough to keep any prefetching inside
   2883 	 * of the aperture.
   2884 	 */
   2885 	struct drm_i915_private *dev_priv = dev->dev_private;
   2886 	struct i915_address_space *ggtt_vm = &dev_priv->gtt.base;
   2887 	struct drm_mm_node *entry;
   2888 	struct drm_i915_gem_object *obj;
   2889 	unsigned long hole_start, hole_end;
   2890 	int ret;
   2891 
   2892 	BUG_ON(mappable_end > end);
   2893 
   2894 	ggtt_vm->start = start;
   2895 
   2896 	/* Subtract the guard page before address space initialization to
   2897 	 * shrink the range used by drm_mm */
   2898 	ggtt_vm->total = end - start - PAGE_SIZE;
   2899 	i915_address_space_init(ggtt_vm, dev_priv);
   2900 	ggtt_vm->total += PAGE_SIZE;
   2901 
   2902 	if (intel_vgpu_active(dev)) {
   2903 		ret = intel_vgt_balloon(dev);
   2904 		if (ret)
   2905 			return ret;
   2906 	}
   2907 
   2908 	if (!HAS_LLC(dev))
   2909 		ggtt_vm->mm.color_adjust = i915_gtt_color_adjust;
   2910 
   2911 	/* Mark any preallocated objects as occupied */
   2912 	list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
   2913 		struct i915_vma *vma = i915_gem_obj_to_vma(obj, ggtt_vm);
   2914 
   2915 		DRM_DEBUG_KMS("reserving preallocated space: %llx + %zx\n",
   2916 			      i915_gem_obj_ggtt_offset(obj), obj->base.size);
   2917 
   2918 		WARN_ON(i915_gem_obj_ggtt_bound(obj));
   2919 		ret = drm_mm_reserve_node(&ggtt_vm->mm, &vma->node);
   2920 		if (ret) {
   2921 			DRM_DEBUG_KMS("Reservation failed: %i\n", ret);
   2922 			return ret;
   2923 		}
   2924 		vma->bound |= GLOBAL_BIND;
   2925 		__i915_vma_set_map_and_fenceable(vma);
   2926 		list_add_tail(&vma->mm_list, &ggtt_vm->inactive_list);
   2927 	}
   2928 
   2929 	/* Clear any non-preallocated blocks */
   2930 	drm_mm_for_each_hole(entry, &ggtt_vm->mm, hole_start, hole_end) {
   2931 		DRM_DEBUG_KMS("clearing unused GTT space: [%lx, %lx]\n",
   2932 			      hole_start, hole_end);
   2933 		ggtt_vm->clear_range(ggtt_vm, hole_start,
   2934 				     hole_end - hole_start, true);
   2935 	}
   2936 
   2937 	/* And finally clear the reserved guard page */
   2938 	ggtt_vm->clear_range(ggtt_vm, end - PAGE_SIZE, PAGE_SIZE, true);
   2939 
   2940 	if (USES_PPGTT(dev) && !USES_FULL_PPGTT(dev)) {
   2941 		struct i915_hw_ppgtt *ppgtt;
   2942 
   2943 		ppgtt = kzalloc(sizeof(*ppgtt), GFP_KERNEL);
   2944 		if (!ppgtt)
   2945 			return -ENOMEM;
   2946 
   2947 		ret = __hw_ppgtt_init(dev, ppgtt);
   2948 		if (ret) {
   2949 			ppgtt->base.cleanup(&ppgtt->base);
   2950 			kfree(ppgtt);
   2951 			return ret;
   2952 		}
   2953 
   2954 		if (ppgtt->base.allocate_va_range)
   2955 			ret = ppgtt->base.allocate_va_range(&ppgtt->base, 0,
   2956 							    ppgtt->base.total);
   2957 		if (ret) {
   2958 			ppgtt->base.cleanup(&ppgtt->base);
   2959 			kfree(ppgtt);
   2960 			return ret;
   2961 		}
   2962 
   2963 		ppgtt->base.clear_range(&ppgtt->base,
   2964 					ppgtt->base.start,
   2965 					ppgtt->base.total,
   2966 					true);
   2967 
   2968 		dev_priv->mm.aliasing_ppgtt = ppgtt;
   2969 		WARN_ON(dev_priv->gtt.base.bind_vma != ggtt_bind_vma);
   2970 		dev_priv->gtt.base.bind_vma = aliasing_gtt_bind_vma;
   2971 	}
   2972 
   2973 	return 0;
   2974 }
   2975 
   2976 void i915_gem_init_global_gtt(struct drm_device *dev)
   2977 {
   2978 	struct drm_i915_private *dev_priv = dev->dev_private;
   2979 	u64 gtt_size, mappable_size;
   2980 
   2981 	gtt_size = dev_priv->gtt.base.total;
   2982 	mappable_size = dev_priv->gtt.mappable_end;
   2983 
   2984 	i915_gem_setup_global_gtt(dev, 0, mappable_size, gtt_size);
   2985 }
   2986 
   2987 void i915_global_gtt_cleanup(struct drm_device *dev)
   2988 {
   2989 	struct drm_i915_private *dev_priv = dev->dev_private;
   2990 	struct i915_address_space *vm = &dev_priv->gtt.base;
   2991 
   2992 	if (dev_priv->mm.aliasing_ppgtt) {
   2993 		struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
   2994 
   2995 		ppgtt->base.cleanup(&ppgtt->base);
   2996 		kfree(ppgtt);
   2997 	}
   2998 
   2999 	if (drm_mm_initialized(&vm->mm)) {
   3000 		if (intel_vgpu_active(dev))
   3001 			intel_vgt_deballoon();
   3002 
   3003 		drm_mm_takedown(&vm->mm);
   3004 		list_del(&vm->global_link);
   3005 	}
   3006 
   3007 	vm->cleanup(vm);
   3008 }
   3009 
   3010 static unsigned int gen6_get_total_gtt_size(u16 snb_gmch_ctl)
   3011 {
   3012 	snb_gmch_ctl >>= SNB_GMCH_GGMS_SHIFT;
   3013 	snb_gmch_ctl &= SNB_GMCH_GGMS_MASK;
   3014 	return snb_gmch_ctl << 20;
   3015 }
   3016 
   3017 static unsigned int gen8_get_total_gtt_size(u16 bdw_gmch_ctl)
   3018 {
   3019 	bdw_gmch_ctl >>= BDW_GMCH_GGMS_SHIFT;
   3020 	bdw_gmch_ctl &= BDW_GMCH_GGMS_MASK;
   3021 	if (bdw_gmch_ctl)
   3022 		bdw_gmch_ctl = 1 << bdw_gmch_ctl;
   3023 
   3024 #ifdef CONFIG_X86_32
   3025 	/* Limit 32b platforms to a 2GB GGTT: 4 << 20 / pte size * PAGE_SIZE */
   3026 	if (bdw_gmch_ctl > 4)
   3027 		bdw_gmch_ctl = 4;
   3028 #endif
   3029 
   3030 	return bdw_gmch_ctl << 20;
   3031 }
   3032 
   3033 static unsigned int chv_get_total_gtt_size(u16 gmch_ctrl)
   3034 {
   3035 	gmch_ctrl >>= SNB_GMCH_GGMS_SHIFT;
   3036 	gmch_ctrl &= SNB_GMCH_GGMS_MASK;
   3037 
   3038 	if (gmch_ctrl)
   3039 		return 1 << (20 + gmch_ctrl);
   3040 
   3041 	return 0;
   3042 }
   3043 
   3044 static size_t gen6_get_stolen_size(u16 snb_gmch_ctl)
   3045 {
   3046 	snb_gmch_ctl >>= SNB_GMCH_GMS_SHIFT;
   3047 	snb_gmch_ctl &= SNB_GMCH_GMS_MASK;
   3048 	return snb_gmch_ctl << 25; /* 32 MB units */
   3049 }
   3050 
   3051 static size_t gen8_get_stolen_size(u16 bdw_gmch_ctl)
   3052 {
   3053 	bdw_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
   3054 	bdw_gmch_ctl &= BDW_GMCH_GMS_MASK;
   3055 	return bdw_gmch_ctl << 25; /* 32 MB units */
   3056 }
   3057 
   3058 static size_t chv_get_stolen_size(u16 gmch_ctrl)
   3059 {
   3060 	gmch_ctrl >>= SNB_GMCH_GMS_SHIFT;
   3061 	gmch_ctrl &= SNB_GMCH_GMS_MASK;
   3062 
   3063 	/*
   3064 	 * 0x0  to 0x10: 32MB increments starting at 0MB
   3065 	 * 0x11 to 0x16: 4MB increments starting at 8MB
   3066 	 * 0x17 to 0x1d: 4MB increments start at 36MB
   3067 	 */
   3068 	if (gmch_ctrl < 0x11)
   3069 		return gmch_ctrl << 25;
   3070 	else if (gmch_ctrl < 0x17)
   3071 		return (gmch_ctrl - 0x11 + 2) << 22;
   3072 	else
   3073 		return (gmch_ctrl - 0x17 + 9) << 22;
   3074 }
   3075 
   3076 static size_t gen9_get_stolen_size(u16 gen9_gmch_ctl)
   3077 {
   3078 	gen9_gmch_ctl >>= BDW_GMCH_GMS_SHIFT;
   3079 	gen9_gmch_ctl &= BDW_GMCH_GMS_MASK;
   3080 
   3081 	if (gen9_gmch_ctl < 0xf0)
   3082 		return gen9_gmch_ctl << 25; /* 32 MB units */
   3083 	else
   3084 		/* 4MB increments starting at 0xf0 for 4MB */
   3085 		return (gen9_gmch_ctl - 0xf0 + 1) << 22;
   3086 }
   3087 
   3088 static int ggtt_probe_common(struct drm_device *dev,
   3089 			     size_t gtt_size)
   3090 {
   3091 	struct drm_i915_private *dev_priv = dev->dev_private;
   3092 	struct i915_page_scratch *scratch_page;
   3093 	phys_addr_t gtt_phys_addr;
   3094 
   3095 	/* For Modern GENs the PTEs and register space are split in the BAR */
   3096 	gtt_phys_addr = pci_resource_start(dev->pdev, 0) +
   3097 		(pci_resource_len(dev->pdev, 0) / 2);
   3098 
   3099 #ifdef __NetBSD__
   3100 	dev_priv->gtt.bst = dev->pdev->pd_pa.pa_memt;
   3101 	/* XXX errno NetBSD->Linux */
   3102 	ret = -bus_space_map(dev_priv->gtt.bst, gtt_phys_addr, gtt_size,
   3103 	    IS_PROXTON(dev) ? 0 : BUS_SPACE_MAP_PREFETCHABLE,
   3104 	    &dev_priv->gtt.bsh);
   3105 	if (ret) {
   3106 		DRM_ERROR("Failed to map the graphics translation table: %d\n",
   3107 		    ret);
   3108 		return ret;
   3109 	}
   3110 	dev_priv->gtt.size = gtt_size;
   3111 #else
   3112 	/*
   3113 	 * On BXT writes larger than 64 bit to the GTT pagetable range will be
   3114 	 * dropped. For WC mappings in general we have 64 byte burst writes
   3115 	 * when the WC buffer is flushed, so we can't use it, but have to
   3116 	 * resort to an uncached mapping. The WC issue is easily caught by the
   3117 	 * readback check when writing GTT PTE entries.
   3118 	 */
   3119 	if (IS_BROXTON(dev))
   3120 		dev_priv->gtt.gsm = ioremap_nocache(gtt_phys_addr, gtt_size);
   3121 	else
   3122 		dev_priv->gtt.gsm = ioremap_wc(gtt_phys_addr, gtt_size);
   3123 	if (!dev_priv->gtt.gsm) {
   3124 		DRM_ERROR("Failed to map the gtt page table\n");
   3125 		return -ENOMEM;
   3126 	}
   3127 #endif
   3128 
   3129 	scratch_page = alloc_scratch_page(dev);
   3130 	if (IS_ERR(scratch_page)) {
   3131 		DRM_ERROR("Scratch setup failed\n");
   3132 		/* iounmap will also get called at remove, but meh */
   3133 #ifdef __NetBSD__
   3134 		bus_space_unmap(dev_priv->gtt.bst, dev_priv->gtt.bsh,
   3135 		    dev_priv->gtt.size);
   3136 #else
   3137 		iounmap(dev_priv->gtt.gsm);
   3138 #endif
   3139 		return PTR_ERR(scratch_page);
   3140 	}
   3141 
   3142 	dev_priv->gtt.base.scratch_page = scratch_page;
   3143 
   3144 	return 0;
   3145 }
   3146 
   3147 /* The GGTT and PPGTT need a private PPAT setup in order to handle cacheability
   3148  * bits. When using advanced contexts each context stores its own PAT, but
   3149  * writing this data shouldn't be harmful even in those cases. */
   3150 static void bdw_setup_private_ppat(struct drm_i915_private *dev_priv)
   3151 {
   3152 	uint64_t pat;
   3153 
   3154 	pat = GEN8_PPAT(0, GEN8_PPAT_WB | GEN8_PPAT_LLC)     | /* for normal objects, no eLLC */
   3155 	      GEN8_PPAT(1, GEN8_PPAT_WC | GEN8_PPAT_LLCELLC) | /* for something pointing to ptes? */
   3156 	      GEN8_PPAT(2, GEN8_PPAT_WT | GEN8_PPAT_LLCELLC) | /* for scanout with eLLC */
   3157 	      GEN8_PPAT(3, GEN8_PPAT_UC)                     | /* Uncached objects, mostly for scanout */
   3158 	      GEN8_PPAT(4, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(0)) |
   3159 	      GEN8_PPAT(5, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(1)) |
   3160 	      GEN8_PPAT(6, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(2)) |
   3161 	      GEN8_PPAT(7, GEN8_PPAT_WB | GEN8_PPAT_LLCELLC | GEN8_PPAT_AGE(3));
   3162 
   3163 	if (!USES_PPGTT(dev_priv->dev))
   3164 		/* Spec: "For GGTT, there is NO pat_sel[2:0] from the entry,
   3165 		 * so RTL will always use the value corresponding to
   3166 		 * pat_sel = 000".
   3167 		 * So let's disable cache for GGTT to avoid screen corruptions.
   3168 		 * MOCS still can be used though.
   3169 		 * - System agent ggtt writes (i.e. cpu gtt mmaps) already work
   3170 		 * before this patch, i.e. the same uncached + snooping access
   3171 		 * like on gen6/7 seems to be in effect.
   3172 		 * - So this just fixes blitter/render access. Again it looks
   3173 		 * like it's not just uncached access, but uncached + snooping.
   3174 		 * So we can still hold onto all our assumptions wrt cpu
   3175 		 * clflushing on LLC machines.
   3176 		 */
   3177 		pat = GEN8_PPAT(0, GEN8_PPAT_UC);
   3178 
   3179 	/* XXX: spec defines this as 2 distinct registers. It's unclear if a 64b
   3180 	 * write would work. */
   3181 	I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
   3182 	I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
   3183 }
   3184 
   3185 static void chv_setup_private_ppat(struct drm_i915_private *dev_priv)
   3186 {
   3187 	uint64_t pat;
   3188 
   3189 	/*
   3190 	 * Map WB on BDW to snooped on CHV.
   3191 	 *
   3192 	 * Only the snoop bit has meaning for CHV, the rest is
   3193 	 * ignored.
   3194 	 *
   3195 	 * The hardware will never snoop for certain types of accesses:
   3196 	 * - CPU GTT (GMADR->GGTT->no snoop->memory)
   3197 	 * - PPGTT page tables
   3198 	 * - some other special cycles
   3199 	 *
   3200 	 * As with BDW, we also need to consider the following for GT accesses:
   3201 	 * "For GGTT, there is NO pat_sel[2:0] from the entry,
   3202 	 * so RTL will always use the value corresponding to
   3203 	 * pat_sel = 000".
   3204 	 * Which means we must set the snoop bit in PAT entry 0
   3205 	 * in order to keep the global status page working.
   3206 	 */
   3207 	pat = GEN8_PPAT(0, CHV_PPAT_SNOOP) |
   3208 	      GEN8_PPAT(1, 0) |
   3209 	      GEN8_PPAT(2, 0) |
   3210 	      GEN8_PPAT(3, 0) |
   3211 	      GEN8_PPAT(4, CHV_PPAT_SNOOP) |
   3212 	      GEN8_PPAT(5, CHV_PPAT_SNOOP) |
   3213 	      GEN8_PPAT(6, CHV_PPAT_SNOOP) |
   3214 	      GEN8_PPAT(7, CHV_PPAT_SNOOP);
   3215 
   3216 	I915_WRITE(GEN8_PRIVATE_PAT_LO, pat);
   3217 	I915_WRITE(GEN8_PRIVATE_PAT_HI, pat >> 32);
   3218 }
   3219 
   3220 static int gen8_gmch_probe(struct drm_device *dev,
   3221 			   u64 *gtt_total,
   3222 			   size_t *stolen,
   3223 			   phys_addr_t *mappable_base,
   3224 			   u64 *mappable_end)
   3225 {
   3226 	struct drm_i915_private *dev_priv = dev->dev_private;
   3227 	u64 gtt_size;
   3228 	u16 snb_gmch_ctl;
   3229 	int ret;
   3230 
   3231 	/* TODO: We're not aware of mappable constraints on gen8 yet */
   3232 	*mappable_base = pci_resource_start(dev->pdev, 2);
   3233 	*mappable_end = pci_resource_len(dev->pdev, 2);
   3234 
   3235 #ifndef __NetBSD__
   3236 	if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(39)))
   3237 		pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(39));
   3238 #endif
   3239 
   3240 	pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
   3241 
   3242 	if (INTEL_INFO(dev)->gen >= 9) {
   3243 		*stolen = gen9_get_stolen_size(snb_gmch_ctl);
   3244 		gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl);
   3245 	} else if (IS_CHERRYVIEW(dev)) {
   3246 		*stolen = chv_get_stolen_size(snb_gmch_ctl);
   3247 		gtt_size = chv_get_total_gtt_size(snb_gmch_ctl);
   3248 	} else {
   3249 		*stolen = gen8_get_stolen_size(snb_gmch_ctl);
   3250 		gtt_size = gen8_get_total_gtt_size(snb_gmch_ctl);
   3251 	}
   3252 
   3253 	*gtt_total = (gtt_size / sizeof(gen8_pte_t)) << PAGE_SHIFT;
   3254 
   3255 	if (IS_CHERRYVIEW(dev) || IS_BROXTON(dev))
   3256 		chv_setup_private_ppat(dev_priv);
   3257 	else
   3258 		bdw_setup_private_ppat(dev_priv);
   3259 
   3260 	ret = ggtt_probe_common(dev, gtt_size);
   3261 
   3262 	dev_priv->gtt.base.clear_range = gen8_ggtt_clear_range;
   3263 	dev_priv->gtt.base.insert_entries = gen8_ggtt_insert_entries;
   3264 	dev_priv->gtt.base.bind_vma = ggtt_bind_vma;
   3265 	dev_priv->gtt.base.unbind_vma = ggtt_unbind_vma;
   3266 
   3267 	/* XXX 39-bit addresses?  Really?  See pci_set_dma_mask above...  */
   3268 	dev_priv->gtt.max_paddr = __BITS(38, 0);
   3269 
   3270 	return ret;
   3271 }
   3272 
   3273 static int gen6_gmch_probe(struct drm_device *dev,
   3274 			   u64 *gtt_total,
   3275 			   size_t *stolen,
   3276 			   phys_addr_t *mappable_base,
   3277 			   u64 *mappable_end)
   3278 {
   3279 	struct drm_i915_private *dev_priv = dev->dev_private;
   3280 	unsigned int gtt_size;
   3281 	u16 snb_gmch_ctl;
   3282 	int ret;
   3283 
   3284 	*mappable_base = pci_resource_start(dev->pdev, 2);
   3285 	*mappable_end = pci_resource_len(dev->pdev, 2);
   3286 
   3287 	/* 64/512MB is the current min/max we actually know of, but this is just
   3288 	 * a coarse sanity check.
   3289 	 */
   3290 	if ((*mappable_end < (64<<20) || (*mappable_end > (512<<20)))) {
   3291 		DRM_ERROR("Unknown GMADR size (%llx)\n",
   3292 			  dev_priv->gtt.mappable_end);
   3293 		return -ENXIO;
   3294 	}
   3295 
   3296 #ifndef __NetBSD__
   3297 	if (!pci_set_dma_mask(dev->pdev, DMA_BIT_MASK(40)))
   3298 		pci_set_consistent_dma_mask(dev->pdev, DMA_BIT_MASK(40));
   3299 #endif
   3300 	pci_read_config_word(dev->pdev, SNB_GMCH_CTRL, &snb_gmch_ctl);
   3301 
   3302 	*stolen = gen6_get_stolen_size(snb_gmch_ctl);
   3303 
   3304 	gtt_size = gen6_get_total_gtt_size(snb_gmch_ctl);
   3305 	*gtt_total = (gtt_size / sizeof(gen6_pte_t)) << PAGE_SHIFT;
   3306 
   3307 	ret = ggtt_probe_common(dev, gtt_size);
   3308 
   3309 	dev_priv->gtt.base.clear_range = gen6_ggtt_clear_range;
   3310 	dev_priv->gtt.base.insert_entries = gen6_ggtt_insert_entries;
   3311 	dev_priv->gtt.base.bind_vma = ggtt_bind_vma;
   3312 	dev_priv->gtt.base.unbind_vma = ggtt_unbind_vma;
   3313 
   3314 	dev_priv->gtt.max_paddr = __BITS(39, 0);
   3315 
   3316 	return ret;
   3317 }
   3318 
   3319 static void gen6_gmch_remove(struct i915_address_space *vm)
   3320 {
   3321 	struct i915_gtt *gtt = container_of(vm, struct i915_gtt, base);
   3322 
   3323 #ifdef __NetBSD__
   3324 	bus_space_unmap(gtt->bst, gtt->bsh, gtt->size);
   3325 #else
   3326 	iounmap(gtt->gsm);
   3327 #endif
   3328 	free_scratch_page(vm->dev, vm->scratch_page);
   3329 }
   3330 
   3331 static int i915_gmch_probe(struct drm_device *dev,
   3332 			   u64 *gtt_total,
   3333 			   size_t *stolen,
   3334 			   phys_addr_t *mappable_base,
   3335 			   u64 *mappable_end)
   3336 {
   3337 	struct drm_i915_private *dev_priv = dev->dev_private;
   3338 	int ret;
   3339 
   3340 	ret = intel_gmch_probe(dev_priv->bridge_dev, dev_priv->dev->pdev, NULL);
   3341 	if (!ret) {
   3342 		DRM_ERROR("failed to set up gmch\n");
   3343 		return -EIO;
   3344 	}
   3345 
   3346 	intel_gtt_get(gtt_total, stolen, mappable_base, mappable_end);
   3347 
   3348 	dev_priv->gtt.do_idle_maps = needs_idle_maps(dev_priv->dev);
   3349 	dev_priv->gtt.base.insert_entries = i915_ggtt_insert_entries;
   3350 	dev_priv->gtt.base.clear_range = i915_ggtt_clear_range;
   3351 	dev_priv->gtt.base.bind_vma = ggtt_bind_vma;
   3352 	dev_priv->gtt.base.unbind_vma = ggtt_unbind_vma;
   3353 
   3354 	if (unlikely(dev_priv->gtt.do_idle_maps))
   3355 		DRM_INFO("applying Ironlake quirks for intel_iommu\n");
   3356 
   3357 	if (INTEL_INFO(dev)->gen <= 2)
   3358 		dev_priv->gtt.max_paddr = __BITS(29, 0);
   3359 	else if ((INTEL_INFO(dev)->gen <= 3) ||
   3360 	    IS_BROADWATER(dev) || IS_CRESTLINE(dev))
   3361 		dev_priv->gtt.max_paddr = __BITS(31, 0);
   3362 	else if (INTEL_INFO(dev)->gen <= 5)
   3363 		dev_priv->gtt.max_paddr = __BITS(35, 0);
   3364 	else
   3365 		dev_priv->gtt.max_paddr = __BITS(39, 0);
   3366 
   3367 	return 0;
   3368 }
   3369 
   3370 static void i915_gmch_remove(struct i915_address_space *vm)
   3371 {
   3372 	intel_gmch_remove();
   3373 }
   3374 
   3375 int i915_gem_gtt_init(struct drm_device *dev)
   3376 {
   3377 	struct drm_i915_private *dev_priv = dev->dev_private;
   3378 	struct i915_gtt *gtt = &dev_priv->gtt;
   3379 	int ret;
   3380 
   3381 	if (INTEL_INFO(dev)->gen <= 5) {
   3382 		gtt->gtt_probe = i915_gmch_probe;
   3383 		gtt->base.cleanup = i915_gmch_remove;
   3384 	} else if (INTEL_INFO(dev)->gen < 8) {
   3385 		gtt->gtt_probe = gen6_gmch_probe;
   3386 		gtt->base.cleanup = gen6_gmch_remove;
   3387 		if (IS_HASWELL(dev) && dev_priv->ellc_size)
   3388 			gtt->base.pte_encode = iris_pte_encode;
   3389 		else if (IS_HASWELL(dev))
   3390 			gtt->base.pte_encode = hsw_pte_encode;
   3391 		else if (IS_VALLEYVIEW(dev))
   3392 			gtt->base.pte_encode = byt_pte_encode;
   3393 		else if (INTEL_INFO(dev)->gen >= 7)
   3394 			gtt->base.pte_encode = ivb_pte_encode;
   3395 		else
   3396 			gtt->base.pte_encode = snb_pte_encode;
   3397 	} else {
   3398 		dev_priv->gtt.gtt_probe = gen8_gmch_probe;
   3399 		dev_priv->gtt.base.cleanup = gen6_gmch_remove;
   3400 	}
   3401 
   3402 	gtt->base.dev = dev;
   3403 
   3404 	ret = gtt->gtt_probe(dev, &gtt->base.total, &gtt->stolen_size,
   3405 			     &gtt->mappable_base, &gtt->mappable_end);
   3406 	if (ret)
   3407 		return ret;
   3408 
   3409 #ifdef __NetBSD__
   3410 	dev_priv->gtt.pgfl = x86_select_freelist(dev_priv->gtt.max_paddr);
   3411 	ret = drm_limit_dma_space(dev, 0, dev_priv->gtt.max_paddr);
   3412 	if (ret) {
   3413 		DRM_ERROR("Unable to limit DMA paddr allocations: %d!\n", ret);
   3414 		gtt->base.cleanup(&gtt->base);
   3415 		return ret;
   3416 	}
   3417 #endif
   3418 
   3419 	/* GMADR is the PCI mmio aperture into the global GTT. */
   3420 	DRM_INFO("Memory usable by graphics device = %lluM\n",
   3421 		 gtt->base.total >> 20);
   3422 	DRM_DEBUG_DRIVER("GMADR size = %lldM\n", gtt->mappable_end >> 20);
   3423 	DRM_DEBUG_DRIVER("GTT stolen size = %zdM\n", gtt->stolen_size >> 20);
   3424 #ifdef CONFIG_INTEL_IOMMU
   3425 	if (intel_iommu_gfx_mapped)
   3426 		DRM_INFO("VT-d active for gfx access\n");
   3427 #endif
   3428 	/*
   3429 	 * i915.enable_ppgtt is read-only, so do an early pass to validate the
   3430 	 * user's requested state against the hardware/driver capabilities.  We
   3431 	 * do this now so that we can print out any log messages once rather
   3432 	 * than every time we check intel_enable_ppgtt().
   3433 	 */
   3434 	i915.enable_ppgtt = sanitize_enable_ppgtt(dev, i915.enable_ppgtt);
   3435 	DRM_DEBUG_DRIVER("ppgtt mode: %i\n", i915.enable_ppgtt);
   3436 
   3437 	return 0;
   3438 }
   3439 
   3440 void i915_gem_restore_gtt_mappings(struct drm_device *dev)
   3441 {
   3442 	struct drm_i915_private *dev_priv = dev->dev_private;
   3443 	struct drm_i915_gem_object *obj;
   3444 	struct i915_address_space *vm;
   3445 	struct i915_vma *vma;
   3446 	bool flush;
   3447 
   3448 	i915_check_and_clear_faults(dev);
   3449 
   3450 	/* First fill our portion of the GTT with scratch pages */
   3451 	dev_priv->gtt.base.clear_range(&dev_priv->gtt.base,
   3452 				       dev_priv->gtt.base.start,
   3453 				       dev_priv->gtt.base.total,
   3454 				       true);
   3455 
   3456 	/* Cache flush objects bound into GGTT and rebind them. */
   3457 	vm = &dev_priv->gtt.base;
   3458 	list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
   3459 		flush = false;
   3460 		list_for_each_entry(vma, &obj->vma_list, vma_link) {
   3461 			if (vma->vm != vm)
   3462 				continue;
   3463 
   3464 			WARN_ON(i915_vma_bind(vma, obj->cache_level,
   3465 					      PIN_UPDATE));
   3466 
   3467 			flush = true;
   3468 		}
   3469 
   3470 		if (flush)
   3471 			i915_gem_clflush_object(obj, obj->pin_display);
   3472 	}
   3473 
   3474 	if (INTEL_INFO(dev)->gen >= 8) {
   3475 		if (IS_CHERRYVIEW(dev) || IS_BROXTON(dev))
   3476 			chv_setup_private_ppat(dev_priv);
   3477 		else
   3478 			bdw_setup_private_ppat(dev_priv);
   3479 
   3480 		return;
   3481 	}
   3482 
   3483 	if (USES_PPGTT(dev)) {
   3484 		list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
   3485 			/* TODO: Perhaps it shouldn't be gen6 specific */
   3486 
   3487 			struct i915_hw_ppgtt *ppgtt =
   3488 					container_of(vm, struct i915_hw_ppgtt,
   3489 						     base);
   3490 
   3491 			if (i915_is_ggtt(vm))
   3492 				ppgtt = dev_priv->mm.aliasing_ppgtt;
   3493 
   3494 			gen6_write_page_range(dev_priv, &ppgtt->pd,
   3495 					      0, ppgtt->base.total);
   3496 		}
   3497 	}
   3498 
   3499 	i915_ggtt_flush(dev_priv);
   3500 }
   3501 
   3502 static struct i915_vma *
   3503 __i915_gem_vma_create(struct drm_i915_gem_object *obj,
   3504 		      struct i915_address_space *vm,
   3505 		      const struct i915_ggtt_view *ggtt_view)
   3506 {
   3507 	struct i915_vma *vma;
   3508 
   3509 	if (WARN_ON(i915_is_ggtt(vm) != !!ggtt_view))
   3510 		return ERR_PTR(-EINVAL);
   3511 
   3512 	vma = kmem_cache_zalloc(to_i915(obj->base.dev)->vmas, GFP_KERNEL);
   3513 	if (vma == NULL)
   3514 		return ERR_PTR(-ENOMEM);
   3515 
   3516 	INIT_LIST_HEAD(&vma->vma_link);
   3517 	INIT_LIST_HEAD(&vma->mm_list);
   3518 	INIT_LIST_HEAD(&vma->exec_list);
   3519 	vma->vm = vm;
   3520 	vma->obj = obj;
   3521 
   3522 	if (i915_is_ggtt(vm))
   3523 		vma->ggtt_view = *ggtt_view;
   3524 
   3525 	list_add_tail(&vma->vma_link, &obj->vma_list);
   3526 	if (!i915_is_ggtt(vm))
   3527 		i915_ppgtt_get(i915_vm_to_ppgtt(vm));
   3528 
   3529 	return vma;
   3530 }
   3531 
   3532 struct i915_vma *
   3533 i915_gem_obj_lookup_or_create_vma(struct drm_i915_gem_object *obj,
   3534 				  struct i915_address_space *vm)
   3535 {
   3536 	struct i915_vma *vma;
   3537 
   3538 	vma = i915_gem_obj_to_vma(obj, vm);
   3539 	if (!vma)
   3540 		vma = __i915_gem_vma_create(obj, vm,
   3541 					    i915_is_ggtt(vm) ? &i915_ggtt_view_normal : NULL);
   3542 
   3543 	return vma;
   3544 }
   3545 
   3546 struct i915_vma *
   3547 i915_gem_obj_lookup_or_create_ggtt_vma(struct drm_i915_gem_object *obj,
   3548 				       const struct i915_ggtt_view *view)
   3549 {
   3550 	struct i915_address_space *ggtt = i915_obj_to_ggtt(obj);
   3551 	struct i915_vma *vma;
   3552 
   3553 	if (WARN_ON(!view))
   3554 		return ERR_PTR(-EINVAL);
   3555 
   3556 	vma = i915_gem_obj_to_ggtt_view(obj, view);
   3557 
   3558 	if (IS_ERR(vma))
   3559 		return vma;
   3560 
   3561 	if (!vma)
   3562 		vma = __i915_gem_vma_create(obj, ggtt, view);
   3563 
   3564 	return vma;
   3565 
   3566 }
   3567 
   3568 static struct scatterlist *
   3569 rotate_pages(dma_addr_t *in, unsigned int offset,
   3570 	     unsigned int width, unsigned int height,
   3571 	     struct sg_table *st, struct scatterlist *sg)
   3572 {
   3573 #ifdef __NetBSD__
   3574 	panic("XXX");
   3575 #else
   3576 	unsigned int column, row;
   3577 	unsigned int src_idx;
   3578 
   3579 	if (!sg) {
   3580 		st->nents = 0;
   3581 		sg = st->sgl;
   3582 	}
   3583 
   3584 	for (column = 0; column < width; column++) {
   3585 		src_idx = width * (height - 1) + column;
   3586 		for (row = 0; row < height; row++) {
   3587 			st->nents++;
   3588 			/* We don't need the pages, but need to initialize
   3589 			 * the entries so the sg list can be happily traversed.
   3590 			 * The only thing we need are DMA addresses.
   3591 			 */
   3592 			sg_set_page(sg, NULL, PAGE_SIZE, 0);
   3593 			sg_dma_address(sg) = in[offset + src_idx];
   3594 			sg_dma_len(sg) = PAGE_SIZE;
   3595 			sg = sg_next(sg);
   3596 			src_idx -= width;
   3597 		}
   3598 	}
   3599 
   3600 	return sg;
   3601 #endif
   3602 }
   3603 
   3604 static struct sg_table *
   3605 intel_rotate_fb_obj_pages(struct i915_ggtt_view *ggtt_view,
   3606 			  struct drm_i915_gem_object *obj)
   3607 {
   3608 #ifdef __NetBSD__
   3609 	panic("XXX");
   3610 #else
   3611 	struct intel_rotation_info *rot_info = &ggtt_view->rotation_info;
   3612 	unsigned int size_pages = rot_info->size >> PAGE_SHIFT;
   3613 	unsigned int size_pages_uv;
   3614 	struct sg_page_iter sg_iter;
   3615 	unsigned long i;
   3616 	dma_addr_t *page_addr_list;
   3617 	struct sg_table *st;
   3618 	unsigned int uv_start_page;
   3619 	struct scatterlist *sg;
   3620 	int ret = -ENOMEM;
   3621 
   3622 	/* Allocate a temporary list of source pages for random access. */
   3623 	page_addr_list = drm_malloc_ab(obj->base.size / PAGE_SIZE,
   3624 				       sizeof(dma_addr_t));
   3625 	if (!page_addr_list)
   3626 		return ERR_PTR(ret);
   3627 
   3628 	/* Account for UV plane with NV12. */
   3629 	if (rot_info->pixel_format == DRM_FORMAT_NV12)
   3630 		size_pages_uv = rot_info->size_uv >> PAGE_SHIFT;
   3631 	else
   3632 		size_pages_uv = 0;
   3633 
   3634 	/* Allocate target SG list. */
   3635 	st = kmalloc(sizeof(*st), GFP_KERNEL);
   3636 	if (!st)
   3637 		goto err_st_alloc;
   3638 
   3639 	ret = sg_alloc_table(st, size_pages + size_pages_uv, GFP_KERNEL);
   3640 	if (ret)
   3641 		goto err_sg_alloc;
   3642 
   3643 	/* Populate source page list from the object. */
   3644 	i = 0;
   3645 	for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) {
   3646 		page_addr_list[i] = sg_page_iter_dma_address(&sg_iter);
   3647 		i++;
   3648 	}
   3649 
   3650 	/* Rotate the pages. */
   3651 	sg = rotate_pages(page_addr_list, 0,
   3652 		     rot_info->width_pages, rot_info->height_pages,
   3653 		     st, NULL);
   3654 
   3655 	/* Append the UV plane if NV12. */
   3656 	if (rot_info->pixel_format == DRM_FORMAT_NV12) {
   3657 		uv_start_page = size_pages;
   3658 
   3659 		/* Check for tile-row un-alignment. */
   3660 		if (offset_in_page(rot_info->uv_offset))
   3661 			uv_start_page--;
   3662 
   3663 		rot_info->uv_start_page = uv_start_page;
   3664 
   3665 		rotate_pages(page_addr_list, uv_start_page,
   3666 			     rot_info->width_pages_uv,
   3667 			     rot_info->height_pages_uv,
   3668 			     st, sg);
   3669 	}
   3670 
   3671 	DRM_DEBUG_KMS(
   3672 		      "Created rotated page mapping for object size %zu (pitch=%u, height=%u, pixel_format=0x%x, %ux%u tiles, %u pages (%u plane 0)).\n",
   3673 		      obj->base.size, rot_info->pitch, rot_info->height,
   3674 		      rot_info->pixel_format, rot_info->width_pages,
   3675 		      rot_info->height_pages, size_pages + size_pages_uv,
   3676 		      size_pages);
   3677 
   3678 	drm_free_large(page_addr_list);
   3679 
   3680 	return st;
   3681 
   3682 err_sg_alloc:
   3683 	kfree(st);
   3684 err_st_alloc:
   3685 	drm_free_large(page_addr_list);
   3686 
   3687 	DRM_DEBUG_KMS(
   3688 		      "Failed to create rotated mapping for object size %zu! (%d) (pitch=%u, height=%u, pixel_format=0x%x, %ux%u tiles, %u pages (%u plane 0))\n",
   3689 		      obj->base.size, ret, rot_info->pitch, rot_info->height,
   3690 		      rot_info->pixel_format, rot_info->width_pages,
   3691 		      rot_info->height_pages, size_pages + size_pages_uv,
   3692 		      size_pages);
   3693 	return ERR_PTR(ret);
   3694 #endif
   3695 }
   3696 
   3697 static struct sg_table *
   3698 intel_partial_pages(const struct i915_ggtt_view *view,
   3699 		    struct drm_i915_gem_object *obj)
   3700 {
   3701 #ifdef __NetBSD__
   3702 	panic("XXX");
   3703 #else
   3704 	struct sg_table *st;
   3705 	struct scatterlist *sg;
   3706 	struct sg_page_iter obj_sg_iter;
   3707 	int ret = -ENOMEM;
   3708 
   3709 	st = kmalloc(sizeof(*st), GFP_KERNEL);
   3710 	if (!st)
   3711 		goto err_st_alloc;
   3712 
   3713 	ret = sg_alloc_table(st, view->params.partial.size, GFP_KERNEL);
   3714 	if (ret)
   3715 		goto err_sg_alloc;
   3716 
   3717 	sg = st->sgl;
   3718 	st->nents = 0;
   3719 	for_each_sg_page(obj->pages->sgl, &obj_sg_iter, obj->pages->nents,
   3720 		view->params.partial.offset)
   3721 	{
   3722 		if (st->nents >= view->params.partial.size)
   3723 			break;
   3724 
   3725 		sg_set_page(sg, NULL, PAGE_SIZE, 0);
   3726 		sg_dma_address(sg) = sg_page_iter_dma_address(&obj_sg_iter);
   3727 		sg_dma_len(sg) = PAGE_SIZE;
   3728 
   3729 		sg = sg_next(sg);
   3730 		st->nents++;
   3731 	}
   3732 
   3733 	return st;
   3734 
   3735 err_sg_alloc:
   3736 	kfree(st);
   3737 err_st_alloc:
   3738 	return ERR_PTR(ret);
   3739 #endif
   3740 }
   3741 
   3742 static int
   3743 i915_get_ggtt_vma_pages(struct i915_vma *vma)
   3744 {
   3745 	int ret = 0;
   3746 
   3747 	if (vma->ggtt_view.pages)
   3748 		return 0;
   3749 
   3750 	if (vma->ggtt_view.type == I915_GGTT_VIEW_NORMAL)
   3751 		vma->ggtt_view.pages = vma->obj->pages;
   3752 	else if (vma->ggtt_view.type == I915_GGTT_VIEW_ROTATED)
   3753 		vma->ggtt_view.pages =
   3754 			intel_rotate_fb_obj_pages(&vma->ggtt_view, vma->obj);
   3755 	else if (vma->ggtt_view.type == I915_GGTT_VIEW_PARTIAL)
   3756 		vma->ggtt_view.pages =
   3757 			intel_partial_pages(&vma->ggtt_view, vma->obj);
   3758 	else
   3759 		WARN_ONCE(1, "GGTT view %u not implemented!\n",
   3760 			  vma->ggtt_view.type);
   3761 
   3762 	if (!vma->ggtt_view.pages) {
   3763 		DRM_ERROR("Failed to get pages for GGTT view type %u!\n",
   3764 			  vma->ggtt_view.type);
   3765 		ret = -EINVAL;
   3766 	} else if (IS_ERR(vma->ggtt_view.pages)) {
   3767 		ret = PTR_ERR(vma->ggtt_view.pages);
   3768 		vma->ggtt_view.pages = NULL;
   3769 		DRM_ERROR("Failed to get pages for VMA view type %u (%d)!\n",
   3770 			  vma->ggtt_view.type, ret);
   3771 	}
   3772 
   3773 	return ret;
   3774 }
   3775 
   3776 /**
   3777  * i915_vma_bind - Sets up PTEs for an VMA in it's corresponding address space.
   3778  * @vma: VMA to map
   3779  * @cache_level: mapping cache level
   3780  * @flags: flags like global or local mapping
   3781  *
   3782  * DMA addresses are taken from the scatter-gather table of this object (or of
   3783  * this VMA in case of non-default GGTT views) and PTE entries set up.
   3784  * Note that DMA addresses are also the only part of the SG table we care about.
   3785  */
   3786 int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level,
   3787 		  u32 flags)
   3788 {
   3789 	int ret;
   3790 	u32 bind_flags;
   3791 
   3792 	if (WARN_ON(flags == 0))
   3793 		return -EINVAL;
   3794 
   3795 	bind_flags = 0;
   3796 	if (flags & PIN_GLOBAL)
   3797 		bind_flags |= GLOBAL_BIND;
   3798 	if (flags & PIN_USER)
   3799 		bind_flags |= LOCAL_BIND;
   3800 
   3801 	if (flags & PIN_UPDATE)
   3802 		bind_flags |= vma->bound;
   3803 	else
   3804 		bind_flags &= ~vma->bound;
   3805 
   3806 	if (bind_flags == 0)
   3807 		return 0;
   3808 
   3809 	if (vma->bound == 0 && vma->vm->allocate_va_range) {
   3810 		trace_i915_va_alloc(vma->vm,
   3811 				    vma->node.start,
   3812 				    vma->node.size,
   3813 				    VM_TO_TRACE_NAME(vma->vm));
   3814 
   3815 		/* XXX: i915_vma_pin() will fix this +- hack */
   3816 		vma->pin_count++;
   3817 		ret = vma->vm->allocate_va_range(vma->vm,
   3818 						 vma->node.start,
   3819 						 vma->node.size);
   3820 		vma->pin_count--;
   3821 		if (ret)
   3822 			return ret;
   3823 	}
   3824 
   3825 	ret = vma->vm->bind_vma(vma, cache_level, bind_flags);
   3826 	if (ret)
   3827 		return ret;
   3828 
   3829 	vma->bound |= bind_flags;
   3830 
   3831 	return 0;
   3832 }
   3833 
   3834 /**
   3835  * i915_ggtt_view_size - Get the size of a GGTT view.
   3836  * @obj: Object the view is of.
   3837  * @view: The view in question.
   3838  *
   3839  * @return The size of the GGTT view in bytes.
   3840  */
   3841 size_t
   3842 i915_ggtt_view_size(struct drm_i915_gem_object *obj,
   3843 		    const struct i915_ggtt_view *view)
   3844 {
   3845 	if (view->type == I915_GGTT_VIEW_NORMAL) {
   3846 		return obj->base.size;
   3847 	} else if (view->type == I915_GGTT_VIEW_ROTATED) {
   3848 		return view->rotation_info.size;
   3849 	} else if (view->type == I915_GGTT_VIEW_PARTIAL) {
   3850 		return view->params.partial.size << PAGE_SHIFT;
   3851 	} else {
   3852 		WARN_ONCE(1, "GGTT view %u not implemented!\n", view->type);
   3853 		return obj->base.size;
   3854 	}
   3855 }
   3856