Home | History | Annotate | Line # | Download | only in i915drm
intel_gtt_subr.c revision 1.3
      1  1.3  riastrad /*	$NetBSD: intel_gtt_subr.c,v 1.3 2021/12/19 12:37:36 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*-
      4  1.1  riastrad  * Copyright (c) 2014 The NetBSD Foundation, Inc.
      5  1.1  riastrad  * All rights reserved.
      6  1.1  riastrad  *
      7  1.1  riastrad  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  riastrad  * by Taylor R. Campbell.
      9  1.1  riastrad  *
     10  1.1  riastrad  * Redistribution and use in source and binary forms, with or without
     11  1.1  riastrad  * modification, are permitted provided that the following conditions
     12  1.1  riastrad  * are met:
     13  1.1  riastrad  * 1. Redistributions of source code must retain the above copyright
     14  1.1  riastrad  *    notice, this list of conditions and the following disclaimer.
     15  1.1  riastrad  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  riastrad  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  riastrad  *    documentation and/or other materials provided with the distribution.
     18  1.1  riastrad  *
     19  1.1  riastrad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  riastrad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  riastrad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  riastrad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  riastrad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  riastrad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  riastrad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  riastrad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  riastrad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  riastrad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  riastrad  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  riastrad  */
     31  1.1  riastrad 
     32  1.1  riastrad /* Intel GTT stubs */
     33  1.1  riastrad 
     34  1.1  riastrad #include <sys/cdefs.h>
     35  1.3  riastrad __KERNEL_RCSID(0, "$NetBSD: intel_gtt_subr.c,v 1.3 2021/12/19 12:37:36 riastradh Exp $");
     36  1.1  riastrad 
     37  1.1  riastrad #include <sys/types.h>
     38  1.1  riastrad #include <sys/bus.h>
     39  1.1  riastrad #include <sys/errno.h>
     40  1.1  riastrad #include <sys/systm.h>
     41  1.1  riastrad 
     42  1.1  riastrad #include <machine/vmparam.h>
     43  1.1  riastrad 
     44  1.1  riastrad #include <dev/pci/pcivar.h>		/* XXX agpvar.h needs...  */
     45  1.1  riastrad #include <dev/pci/agpvar.h>
     46  1.1  riastrad #include <dev/pci/agp_i810var.h>
     47  1.1  riastrad 
     48  1.2  riastrad #include <linux/pci.h>
     49  1.1  riastrad #include <linux/scatterlist.h>
     50  1.1  riastrad 
     51  1.2  riastrad #include "drm/i915_drm.h"
     52  1.1  riastrad #include "drm/intel-gtt.h"
     53  1.1  riastrad 
     54  1.2  riastrad static uint8_t
     55  1.2  riastrad pci_conf_read8(pci_chipset_tag_t pc, pcitag_t tag, bus_size_t reg)
     56  1.2  riastrad {
     57  1.2  riastrad 	uint32_t v;
     58  1.2  riastrad 
     59  1.2  riastrad 	v = pci_conf_read(pc, tag, reg & ~3);
     60  1.2  riastrad 
     61  1.2  riastrad 	return 0xff & (v >> (8 * (reg & 3)));
     62  1.2  riastrad }
     63  1.2  riastrad 
     64  1.2  riastrad static uint8_t
     65  1.2  riastrad pci_read8(pci_chipset_tag_t pc, int bus, int dev, int func, bus_size_t reg)
     66  1.2  riastrad {
     67  1.2  riastrad 	pcitag_t tag = pci_make_tag(pc, bus, dev, func);
     68  1.2  riastrad 
     69  1.2  riastrad 	return pci_conf_read8(pc, tag, reg);
     70  1.2  riastrad }
     71  1.2  riastrad 
     72  1.2  riastrad static uint16_t
     73  1.2  riastrad pci_conf_read16(pci_chipset_tag_t pc, pcitag_t tag, bus_size_t reg)
     74  1.2  riastrad {
     75  1.2  riastrad 	uint32_t v;
     76  1.2  riastrad 
     77  1.2  riastrad 	KASSERT((reg & 1) == 0);
     78  1.2  riastrad 
     79  1.2  riastrad 	v = pci_conf_read(pc, tag, reg & ~2);
     80  1.2  riastrad 
     81  1.2  riastrad 	return 0xffff & (v >> (8 * (reg & 2)));
     82  1.2  riastrad }
     83  1.2  riastrad 
     84  1.2  riastrad static uint16_t
     85  1.2  riastrad pci_read16(pci_chipset_tag_t pc, int bus, int dev, int func, bus_size_t reg)
     86  1.2  riastrad {
     87  1.2  riastrad 	pcitag_t tag = pci_make_tag(pc, bus, dev, func);
     88  1.2  riastrad 
     89  1.2  riastrad 	return pci_conf_read16(pc, tag, reg);
     90  1.2  riastrad }
     91  1.2  riastrad 
     92  1.1  riastrad /* Access to this should be single-threaded.  */
     93  1.1  riastrad static struct {
     94  1.1  riastrad 	bus_dma_segment_t	scratch_seg;
     95  1.1  riastrad 	bus_dmamap_t		scratch_map;
     96  1.1  riastrad } intel_gtt;
     97  1.1  riastrad 
     98  1.2  riastrad /* XXX This logic should be merged with agp_i810.c.  */
     99  1.1  riastrad struct resource intel_graphics_stolen_res;
    100  1.1  riastrad 
    101  1.2  riastrad static bus_size_t
    102  1.2  riastrad i830_tseg_size(pci_chipset_tag_t pc)
    103  1.2  riastrad {
    104  1.2  riastrad 	uint8_t esmramc = pci_read8(pc, 0, 0, 0, I830_ESMRAMC);
    105  1.2  riastrad 
    106  1.2  riastrad 	if ((esmramc & TSEG_ENABLE) == 0)
    107  1.2  riastrad 		return 0;
    108  1.2  riastrad 
    109  1.2  riastrad 	return (esmramc & I830_TSEG_SIZE_1M) ? 1024*1024 : 512*1024;
    110  1.2  riastrad }
    111  1.2  riastrad 
    112  1.2  riastrad static bus_size_t
    113  1.2  riastrad i845_tseg_size(pci_chipset_tag_t pc)
    114  1.2  riastrad {
    115  1.2  riastrad 	uint8_t esmramc = pci_read8(pc, 0, 0, 0, I845_ESMRAMC);
    116  1.2  riastrad 
    117  1.2  riastrad 	if ((esmramc & TSEG_ENABLE) == 0)
    118  1.2  riastrad 		return 0;
    119  1.2  riastrad 
    120  1.2  riastrad 	switch (esmramc & I845_TSEG_SIZE_MASK) {
    121  1.2  riastrad 	case I845_TSEG_SIZE_512K:
    122  1.2  riastrad 		return 512*1024;
    123  1.2  riastrad 	case I845_TSEG_SIZE_1M:
    124  1.2  riastrad 		return 1024*1024;
    125  1.2  riastrad 	default:
    126  1.2  riastrad 		return 0;
    127  1.2  riastrad 	}
    128  1.2  riastrad }
    129  1.2  riastrad 
    130  1.2  riastrad static bus_size_t
    131  1.2  riastrad i85x_tseg_size(pci_chipset_tag_t pc)
    132  1.2  riastrad {
    133  1.2  riastrad 	uint8_t esmramc = pci_read8(pc, 0, 0, 0, I85X_ESMRAMC);
    134  1.2  riastrad 
    135  1.2  riastrad 	if ((esmramc & TSEG_ENABLE) == 0)
    136  1.2  riastrad 		return 0;
    137  1.2  riastrad 
    138  1.2  riastrad 	return 1024*1024;
    139  1.2  riastrad }
    140  1.2  riastrad 
    141  1.2  riastrad static bus_size_t
    142  1.2  riastrad i830_tom(pci_chipset_tag_t pc)
    143  1.2  riastrad {
    144  1.2  riastrad 	uint8_t drb3 = pci_read8(pc, 0, 0, 0, I830_DRB3);
    145  1.2  riastrad 
    146  1.2  riastrad 	return (bus_size_t)32*1024*1024 * drb3;
    147  1.2  riastrad }
    148  1.2  riastrad 
    149  1.2  riastrad static bus_size_t
    150  1.2  riastrad i85x_tom(pci_chipset_tag_t pc)
    151  1.2  riastrad {
    152  1.2  riastrad 	uint8_t drb3 = pci_read8(pc, 0, 0, 1, I85X_DRB3);
    153  1.2  riastrad 
    154  1.2  riastrad 	return (bus_size_t)32*1024*1024 * drb3;
    155  1.2  riastrad }
    156  1.2  riastrad 
    157  1.2  riastrad static bus_size_t
    158  1.2  riastrad i830_stolen_size(pci_chipset_tag_t pc, pcitag_t tag)
    159  1.2  riastrad {
    160  1.2  riastrad 	uint16_t gmch_ctrl = pci_read16(pc, 0, 0, 0, I830_GMCH_CTRL);
    161  1.2  riastrad 
    162  1.2  riastrad 	switch (gmch_ctrl & I830_GMCH_GMS_MASK) {
    163  1.2  riastrad 	case I830_GMCH_GMS_STOLEN_512:
    164  1.2  riastrad 		return 512*1024;
    165  1.2  riastrad 	case I830_GMCH_GMS_STOLEN_1024:
    166  1.2  riastrad 		return 1024*1024;
    167  1.2  riastrad 	case I830_GMCH_GMS_STOLEN_8192:
    168  1.2  riastrad 		return 8*1024*1024;
    169  1.2  riastrad 	case I830_GMCH_GMS_LOCAL:
    170  1.2  riastrad 	default:
    171  1.2  riastrad 		aprint_error("%s: invalid gmch_ctrl 0x%04x\n", __func__,
    172  1.2  riastrad 		    gmch_ctrl);
    173  1.2  riastrad 		return 0;
    174  1.2  riastrad 	}
    175  1.2  riastrad }
    176  1.2  riastrad 
    177  1.2  riastrad static bus_size_t
    178  1.2  riastrad gen3_stolen_size(pci_chipset_tag_t pc, pcitag_t tag)
    179  1.2  riastrad {
    180  1.2  riastrad 	uint16_t gmch_ctrl = pci_read16(pc, 0, 0, 0, I830_GMCH_CTRL);
    181  1.2  riastrad 
    182  1.2  riastrad 	switch (gmch_ctrl & I855_GMCH_GMS_MASK) {
    183  1.2  riastrad 	case I855_GMCH_GMS_STOLEN_1M:
    184  1.2  riastrad 		return 1024*1024;
    185  1.2  riastrad 	case I855_GMCH_GMS_STOLEN_4M:
    186  1.2  riastrad 		return 4*1024*1024;
    187  1.2  riastrad 	case I855_GMCH_GMS_STOLEN_8M:
    188  1.2  riastrad 		return 8*1024*1024;
    189  1.2  riastrad 	case I855_GMCH_GMS_STOLEN_16M:
    190  1.2  riastrad 		return 16*1024*1024;
    191  1.2  riastrad 	case I855_GMCH_GMS_STOLEN_32M:
    192  1.2  riastrad 		return 32*1024*1024;
    193  1.2  riastrad 	case I915_GMCH_GMS_STOLEN_48M:
    194  1.2  riastrad 		return 48*1024*1024;
    195  1.2  riastrad 	case I915_GMCH_GMS_STOLEN_64M:
    196  1.2  riastrad 		return 64*1024*1024;
    197  1.2  riastrad 	case G33_GMCH_GMS_STOLEN_128M:
    198  1.2  riastrad 		return 128*1024*1024;
    199  1.2  riastrad 	case G33_GMCH_GMS_STOLEN_256M:
    200  1.2  riastrad 		return 256*1024*1024;
    201  1.2  riastrad 	case INTEL_GMCH_GMS_STOLEN_96M:
    202  1.2  riastrad 		return 96*1024*1024;
    203  1.2  riastrad 	case INTEL_GMCH_GMS_STOLEN_160M:
    204  1.2  riastrad 		return 160*1024*1024;
    205  1.2  riastrad 	case INTEL_GMCH_GMS_STOLEN_224M:
    206  1.2  riastrad 		return 224*1024*1024;
    207  1.2  riastrad 	case INTEL_GMCH_GMS_STOLEN_352M:
    208  1.2  riastrad 		return 352*1024*1024;
    209  1.2  riastrad 	default:
    210  1.2  riastrad 		aprint_error("%s: invalid gmch_ctrl 0x%04x\n", __func__,
    211  1.2  riastrad 		    gmch_ctrl);
    212  1.2  riastrad 		return 0;
    213  1.2  riastrad 	}
    214  1.2  riastrad }
    215  1.2  riastrad 
    216  1.2  riastrad static bus_size_t
    217  1.2  riastrad gen6_stolen_size(pci_chipset_tag_t pc, pcitag_t tag)
    218  1.2  riastrad {
    219  1.2  riastrad 	uint16_t gmch_ctrl = pci_conf_read16(pc, tag, SNB_GMCH_CTRL);
    220  1.2  riastrad 	uint16_t gms = (gmch_ctrl >> SNB_GMCH_GMS_SHIFT) & SNB_GMCH_GMS_MASK;
    221  1.2  riastrad 
    222  1.2  riastrad 	return (bus_size_t)32*1024*1024 * gms;
    223  1.2  riastrad }
    224  1.2  riastrad 
    225  1.2  riastrad static bus_size_t
    226  1.2  riastrad gen8_stolen_size(pci_chipset_tag_t pc, pcitag_t tag)
    227  1.2  riastrad {
    228  1.2  riastrad 	uint16_t gmch_ctrl = pci_conf_read16(pc, tag, SNB_GMCH_CTRL);
    229  1.2  riastrad 	uint16_t gms = (gmch_ctrl >> BDW_GMCH_GMS_SHIFT) & BDW_GMCH_GMS_MASK;
    230  1.2  riastrad 
    231  1.2  riastrad 	return (bus_size_t)32*1024*1024 * gms;
    232  1.2  riastrad }
    233  1.2  riastrad 
    234  1.2  riastrad static bus_size_t
    235  1.2  riastrad chv_stolen_size(pci_chipset_tag_t pc, pcitag_t tag)
    236  1.2  riastrad {
    237  1.2  riastrad 	uint16_t gmch_ctrl = pci_conf_read16(pc, tag, SNB_GMCH_CTRL);
    238  1.2  riastrad 	uint16_t gms = (gmch_ctrl >> SNB_GMCH_GMS_SHIFT) & SNB_GMCH_GMS_MASK;
    239  1.2  riastrad 
    240  1.2  riastrad 	if (gms <= 0x10)
    241  1.2  riastrad 		return (bus_size_t)32*1024*1024 * gms;
    242  1.2  riastrad 	else if (gms <= 0x16)
    243  1.2  riastrad 		return (bus_size_t)(8 + 4*(gms - 0x11))*1024*1024;
    244  1.2  riastrad 	else
    245  1.2  riastrad 		return (bus_size_t)(36 + 4*(gms - 0x17))*1024*1024;
    246  1.2  riastrad }
    247  1.2  riastrad 
    248  1.2  riastrad static bus_size_t
    249  1.2  riastrad gen9_stolen_size(pci_chipset_tag_t pc, pcitag_t tag)
    250  1.2  riastrad {
    251  1.2  riastrad 	uint16_t gmch_ctrl = pci_conf_read16(pc, tag, SNB_GMCH_CTRL);
    252  1.2  riastrad 	uint16_t gms = (gmch_ctrl >> BDW_GMCH_GMS_SHIFT) & BDW_GMCH_GMS_MASK;
    253  1.2  riastrad 
    254  1.2  riastrad 	if (gms <= 0xef)
    255  1.2  riastrad 		return (bus_size_t)32*1024*1024 * gms;
    256  1.2  riastrad 	else
    257  1.2  riastrad 		return (bus_size_t)(4 + 4*(gms - 0xf0))*1024*1024;
    258  1.2  riastrad }
    259  1.2  riastrad 
    260  1.2  riastrad static bus_addr_t
    261  1.2  riastrad i830_stolen_base(pci_chipset_tag_t pc, pcitag_t tag, bus_size_t stolen_size)
    262  1.2  riastrad {
    263  1.2  riastrad 
    264  1.2  riastrad 	return i830_tom(pc) - i830_tseg_size(pc) - stolen_size;
    265  1.2  riastrad }
    266  1.2  riastrad 
    267  1.2  riastrad static bus_addr_t
    268  1.2  riastrad i845_stolen_base(pci_chipset_tag_t pc, pcitag_t tag, bus_size_t stolen_size)
    269  1.2  riastrad {
    270  1.2  riastrad 
    271  1.2  riastrad 	return i830_tom(pc) - i845_tseg_size(pc) - stolen_size;
    272  1.2  riastrad }
    273  1.2  riastrad 
    274  1.2  riastrad static bus_addr_t
    275  1.2  riastrad i85x_stolen_base(pci_chipset_tag_t pc, pcitag_t tag, bus_size_t stolen_size)
    276  1.2  riastrad {
    277  1.2  riastrad 
    278  1.2  riastrad 	return i85x_tom(pc) - i85x_tseg_size(pc) - stolen_size;
    279  1.2  riastrad }
    280  1.2  riastrad 
    281  1.2  riastrad static bus_addr_t
    282  1.2  riastrad i865_stolen_base(pci_chipset_tag_t pc, pcitag_t tag, bus_size_t stolen_size)
    283  1.2  riastrad {
    284  1.2  riastrad 	uint16_t toud = pci_read16(pc, 0, 0, 0, I865_TOUD);
    285  1.2  riastrad 
    286  1.2  riastrad 	return i845_tseg_size(pc) + (64*1024 * toud);
    287  1.2  riastrad }
    288  1.2  riastrad 
    289  1.2  riastrad static bus_addr_t
    290  1.2  riastrad gen3_stolen_base(pci_chipset_tag_t pc, pcitag_t tag, bus_size_t stolen_size)
    291  1.2  riastrad {
    292  1.2  riastrad 	uint32_t bsm = pci_conf_read(pc, tag, INTEL_BSM);
    293  1.2  riastrad 
    294  1.2  riastrad 	return bsm & INTEL_BSM_MASK;
    295  1.2  riastrad }
    296  1.2  riastrad 
    297  1.2  riastrad static bus_addr_t
    298  1.2  riastrad gen11_stolen_base(pci_chipset_tag_t pc, pcitag_t tag, bus_size_t stolen_size)
    299  1.2  riastrad {
    300  1.2  riastrad 	uint32_t bsm;
    301  1.2  riastrad 
    302  1.2  riastrad 	bsm = pci_conf_read(pc, tag, INTEL_GEN11_BSM_DW0) & INTEL_BSM_MASK;
    303  1.2  riastrad 	bsm |= (uint64_t)pci_conf_read(pc, tag, INTEL_GEN11_BSM_DW1) << 32;
    304  1.2  riastrad 
    305  1.2  riastrad 	return bsm;
    306  1.2  riastrad }
    307  1.2  riastrad 
    308  1.2  riastrad struct intel_stolen_ops {
    309  1.2  riastrad 	bus_size_t	(*size)(pci_chipset_tag_t, pcitag_t);
    310  1.2  riastrad 	bus_addr_t	(*base)(pci_chipset_tag_t, pcitag_t, bus_size_t);
    311  1.2  riastrad };
    312  1.2  riastrad 
    313  1.2  riastrad static const struct intel_stolen_ops i830_stolen_ops = {
    314  1.2  riastrad 	.size = i830_stolen_size,
    315  1.2  riastrad 	.base = i830_stolen_base,
    316  1.2  riastrad };
    317  1.2  riastrad 
    318  1.2  riastrad static const struct intel_stolen_ops i845_stolen_ops = {
    319  1.2  riastrad 	.size = i830_stolen_size,
    320  1.2  riastrad 	.base = i845_stolen_base,
    321  1.2  riastrad };
    322  1.2  riastrad 
    323  1.2  riastrad static const struct intel_stolen_ops i85x_stolen_ops = {
    324  1.2  riastrad 	.size = gen3_stolen_size,
    325  1.2  riastrad 	.base = i85x_stolen_base,
    326  1.2  riastrad };
    327  1.2  riastrad 
    328  1.2  riastrad static const struct intel_stolen_ops i865_stolen_ops = {
    329  1.2  riastrad 	.size = gen3_stolen_size,
    330  1.2  riastrad 	.base = i865_stolen_base,
    331  1.2  riastrad };
    332  1.2  riastrad 
    333  1.2  riastrad static const struct intel_stolen_ops gen3_stolen_ops = {
    334  1.2  riastrad 	.size = gen3_stolen_size,
    335  1.2  riastrad 	.base = gen3_stolen_base,
    336  1.2  riastrad };
    337  1.2  riastrad 
    338  1.2  riastrad static const struct intel_stolen_ops gen6_stolen_ops = {
    339  1.2  riastrad 	.size = gen6_stolen_size,
    340  1.2  riastrad 	.base = gen3_stolen_base,
    341  1.2  riastrad };
    342  1.2  riastrad 
    343  1.2  riastrad static const struct intel_stolen_ops gen8_stolen_ops = {
    344  1.2  riastrad 	.size = gen8_stolen_size,
    345  1.2  riastrad 	.base = gen3_stolen_base,
    346  1.2  riastrad };
    347  1.2  riastrad 
    348  1.2  riastrad static const struct intel_stolen_ops gen9_stolen_ops = {
    349  1.2  riastrad 	.size = gen9_stolen_size,
    350  1.2  riastrad 	.base = gen3_stolen_base,
    351  1.2  riastrad };
    352  1.2  riastrad 
    353  1.2  riastrad static const struct intel_stolen_ops chv_stolen_ops = {
    354  1.2  riastrad 	.size = chv_stolen_size,
    355  1.2  riastrad 	.base = gen3_stolen_base,
    356  1.2  riastrad };
    357  1.2  riastrad 
    358  1.2  riastrad static const struct intel_stolen_ops gen11_stolen_ops = {
    359  1.2  riastrad 	.size = gen9_stolen_size,
    360  1.2  riastrad 	.base = gen11_stolen_base,
    361  1.2  riastrad };
    362  1.2  riastrad 
    363  1.2  riastrad static const struct pci_device_id intel_stolen_ids[] = {
    364  1.2  riastrad 	INTEL_I830_IDS(&i830_stolen_ops),
    365  1.2  riastrad 	INTEL_I845G_IDS(&i845_stolen_ops),
    366  1.2  riastrad 	INTEL_I85X_IDS(&i85x_stolen_ops),
    367  1.2  riastrad 	INTEL_I865G_IDS(&i865_stolen_ops),
    368  1.2  riastrad 	INTEL_I915G_IDS(&gen3_stolen_ops),
    369  1.2  riastrad 	INTEL_I915GM_IDS(&gen3_stolen_ops),
    370  1.2  riastrad 	INTEL_I945G_IDS(&gen3_stolen_ops),
    371  1.2  riastrad 	INTEL_I945GM_IDS(&gen3_stolen_ops),
    372  1.2  riastrad 	INTEL_VLV_IDS(&gen6_stolen_ops),
    373  1.2  riastrad 	INTEL_PINEVIEW_G_IDS(&gen3_stolen_ops),
    374  1.2  riastrad 	INTEL_PINEVIEW_M_IDS(&gen3_stolen_ops),
    375  1.2  riastrad 	INTEL_I965G_IDS(&gen3_stolen_ops),
    376  1.2  riastrad 	INTEL_G33_IDS(&gen3_stolen_ops),
    377  1.2  riastrad 	INTEL_I965GM_IDS(&gen3_stolen_ops),
    378  1.2  riastrad 	INTEL_GM45_IDS(&gen3_stolen_ops),
    379  1.2  riastrad 	INTEL_G45_IDS(&gen3_stolen_ops),
    380  1.2  riastrad 	INTEL_IRONLAKE_D_IDS(&gen3_stolen_ops),
    381  1.2  riastrad 	INTEL_IRONLAKE_M_IDS(&gen3_stolen_ops),
    382  1.2  riastrad 	INTEL_SNB_D_IDS(&gen6_stolen_ops),
    383  1.2  riastrad 	INTEL_SNB_M_IDS(&gen6_stolen_ops),
    384  1.2  riastrad 	INTEL_IVB_M_IDS(&gen6_stolen_ops),
    385  1.2  riastrad 	INTEL_IVB_D_IDS(&gen6_stolen_ops),
    386  1.2  riastrad 	INTEL_HSW_IDS(&gen6_stolen_ops),
    387  1.2  riastrad 	INTEL_BDW_IDS(&gen8_stolen_ops),
    388  1.2  riastrad 	INTEL_CHV_IDS(&chv_stolen_ops),
    389  1.2  riastrad 	INTEL_SKL_IDS(&gen9_stolen_ops),
    390  1.2  riastrad 	INTEL_BXT_IDS(&gen9_stolen_ops),
    391  1.2  riastrad 	INTEL_KBL_IDS(&gen9_stolen_ops),
    392  1.2  riastrad 	INTEL_CFL_IDS(&gen9_stolen_ops),
    393  1.2  riastrad 	INTEL_GLK_IDS(&gen9_stolen_ops),
    394  1.2  riastrad 	INTEL_CNL_IDS(&gen9_stolen_ops),
    395  1.2  riastrad 	INTEL_ICL_11_IDS(&gen11_stolen_ops),
    396  1.2  riastrad 	INTEL_EHL_IDS(&gen11_stolen_ops),
    397  1.2  riastrad 	INTEL_TGL_12_IDS(&gen11_stolen_ops),
    398  1.2  riastrad };
    399  1.2  riastrad 
    400  1.1  riastrad void
    401  1.3  riastrad intel_gtt_get(uint64_t *va_size, bus_addr_t *aper_base,
    402  1.3  riastrad     resource_size_t *aper_size)
    403  1.1  riastrad {
    404  1.2  riastrad 	struct agp_softc *sc;
    405  1.2  riastrad 	pci_chipset_tag_t pc;
    406  1.2  riastrad 	pcitag_t tag;
    407  1.2  riastrad 	struct agp_i810_softc *isc;
    408  1.2  riastrad 	const struct intel_stolen_ops *ops;
    409  1.2  riastrad 	bus_addr_t stolen_base;
    410  1.2  riastrad 	bus_size_t stolen_size;
    411  1.2  riastrad 	unsigned i;
    412  1.1  riastrad 
    413  1.2  riastrad 	if ((sc = agp_i810_sc) == NULL) {
    414  1.1  riastrad 		*va_size = 0;
    415  1.1  riastrad 		*aper_base = 0;
    416  1.1  riastrad 		*aper_size = 0;
    417  1.1  riastrad 		return;
    418  1.1  riastrad 	}
    419  1.1  riastrad 
    420  1.2  riastrad 	pc = sc->as_pc;
    421  1.2  riastrad 	tag = sc->as_tag;
    422  1.2  riastrad 
    423  1.2  riastrad 	isc = sc->as_chipc;
    424  1.1  riastrad 	*va_size = ((size_t)(isc->gtt_size/sizeof(uint32_t)) << PAGE_SHIFT);
    425  1.1  riastrad 	*aper_base = sc->as_apaddr;
    426  1.1  riastrad 	*aper_size = sc->as_apsize;
    427  1.1  riastrad 
    428  1.2  riastrad 	for (i = 0; i < __arraycount(intel_stolen_ids); i++) {
    429  1.2  riastrad 		if (intel_stolen_ids[i].device == PCI_PRODUCT(sc->as_id)) {
    430  1.2  riastrad 			ops = (const struct intel_stolen_ops *)
    431  1.2  riastrad 			    intel_stolen_ids[i].driver_data;
    432  1.2  riastrad 			stolen_size = (*ops->size)(pc, tag);
    433  1.2  riastrad 			stolen_base = (*ops->base)(pc, tag, stolen_size);
    434  1.2  riastrad 			intel_graphics_stolen_res.start = stolen_base;
    435  1.2  riastrad 			intel_graphics_stolen_res.end =
    436  1.2  riastrad 			    stolen_base + stolen_size - 1;
    437  1.2  riastrad 			break;
    438  1.2  riastrad 		}
    439  1.2  riastrad 	}
    440  1.1  riastrad }
    441  1.1  riastrad 
    442  1.1  riastrad int
    443  1.1  riastrad intel_gmch_probe(struct pci_dev *bridge_pci __unused,
    444  1.1  riastrad     struct pci_dev *gpu __unused, struct agp_bridge_data *bridge_agp __unused)
    445  1.1  riastrad {
    446  1.1  riastrad 	struct agp_softc *const sc = agp_i810_sc;
    447  1.1  riastrad 	int nsegs;
    448  1.1  riastrad 	int error;
    449  1.1  riastrad 
    450  1.1  riastrad 	if (sc == NULL)
    451  1.1  riastrad 		return 0;
    452  1.1  riastrad 
    453  1.1  riastrad 	error = bus_dmamem_alloc(sc->as_dmat, PAGE_SIZE, PAGE_SIZE, 0,
    454  1.1  riastrad 	    &intel_gtt.scratch_seg, 1, &nsegs, BUS_DMA_WAITOK);
    455  1.1  riastrad 	if (error)
    456  1.1  riastrad 		goto fail0;
    457  1.1  riastrad 	KASSERT(nsegs == 1);
    458  1.1  riastrad 
    459  1.1  riastrad 	error = bus_dmamap_create(sc->as_dmat, PAGE_SIZE, 1, PAGE_SIZE, 0,
    460  1.1  riastrad 	    BUS_DMA_WAITOK, &intel_gtt.scratch_map);
    461  1.1  riastrad 	if (error)
    462  1.1  riastrad 		goto fail1;
    463  1.1  riastrad 
    464  1.1  riastrad 	error = bus_dmamap_load_raw(sc->as_dmat, intel_gtt.scratch_map,
    465  1.1  riastrad 	    &intel_gtt.scratch_seg, 1, PAGE_SIZE, BUS_DMA_WAITOK);
    466  1.1  riastrad 	if (error)
    467  1.1  riastrad 		goto fail2;
    468  1.1  riastrad 
    469  1.1  riastrad 	/* Success!  */
    470  1.1  riastrad 	return 1;
    471  1.1  riastrad 
    472  1.1  riastrad fail3: __unused
    473  1.1  riastrad 	bus_dmamap_unload(sc->as_dmat, intel_gtt.scratch_map);
    474  1.1  riastrad fail2:	bus_dmamap_destroy(sc->as_dmat, intel_gtt.scratch_map);
    475  1.1  riastrad fail1:	bus_dmamem_free(sc->as_dmat, &intel_gtt.scratch_seg, 1);
    476  1.1  riastrad fail0:	KASSERT(error);
    477  1.1  riastrad 	return 0;
    478  1.1  riastrad }
    479  1.1  riastrad 
    480  1.1  riastrad void
    481  1.1  riastrad intel_gmch_remove(void)
    482  1.1  riastrad {
    483  1.1  riastrad 	struct agp_softc *const sc = agp_i810_sc;
    484  1.1  riastrad 
    485  1.1  riastrad 	bus_dmamap_unload(sc->as_dmat, intel_gtt.scratch_map);
    486  1.1  riastrad 	bus_dmamap_destroy(sc->as_dmat, intel_gtt.scratch_map);
    487  1.1  riastrad 	bus_dmamem_free(sc->as_dmat, &intel_gtt.scratch_seg, 1);
    488  1.1  riastrad }
    489  1.1  riastrad 
    490  1.1  riastrad bool
    491  1.1  riastrad intel_enable_gtt(void)
    492  1.1  riastrad {
    493  1.1  riastrad 	struct agp_softc *sc = agp_i810_sc;
    494  1.1  riastrad 	struct agp_i810_softc *isc;
    495  1.1  riastrad 
    496  1.1  riastrad 	if (sc == NULL)
    497  1.1  riastrad 		return false;
    498  1.1  riastrad 	isc = sc->as_chipc;
    499  1.1  riastrad 	agp_i810_reset(isc);
    500  1.1  riastrad 	return true;
    501  1.1  riastrad }
    502  1.1  riastrad 
    503  1.1  riastrad void
    504  1.1  riastrad intel_gtt_chipset_flush(void)
    505  1.1  riastrad {
    506  1.1  riastrad 
    507  1.1  riastrad 	KASSERT(agp_i810_sc != NULL);
    508  1.1  riastrad 	agp_i810_chipset_flush(agp_i810_sc->as_chipc);
    509  1.1  riastrad }
    510  1.1  riastrad 
    511  1.1  riastrad static int
    512  1.1  riastrad intel_gtt_flags(unsigned flags)
    513  1.1  riastrad {
    514  1.1  riastrad 	int gtt_flags = AGP_I810_GTT_VALID;
    515  1.1  riastrad 
    516  1.1  riastrad 	switch (flags) {
    517  1.1  riastrad 	case AGP_USER_MEMORY:
    518  1.1  riastrad 		break;
    519  1.1  riastrad 	case AGP_USER_CACHED_MEMORY:
    520  1.1  riastrad 		gtt_flags |= AGP_I810_GTT_CACHED;
    521  1.1  riastrad 		break;
    522  1.1  riastrad 	default:
    523  1.1  riastrad 		panic("invalid intel gtt flags: %x", flags);
    524  1.1  riastrad 	}
    525  1.1  riastrad 
    526  1.1  riastrad 	return gtt_flags;
    527  1.1  riastrad }
    528  1.1  riastrad 
    529  1.1  riastrad void
    530  1.1  riastrad intel_gtt_insert_page(bus_addr_t addr, unsigned va_page, unsigned flags)
    531  1.1  riastrad {
    532  1.1  riastrad 	struct agp_i810_softc *const isc = agp_i810_sc->as_chipc;
    533  1.1  riastrad 	off_t va = (off_t)va_page << PAGE_SHIFT;
    534  1.1  riastrad 	int gtt_flags = intel_gtt_flags(flags);
    535  1.1  riastrad 	int error;
    536  1.1  riastrad 
    537  1.1  riastrad 	error = agp_i810_write_gtt_entry(isc, va, addr, gtt_flags);
    538  1.1  riastrad 	if (error)
    539  1.1  riastrad 		device_printf(agp_i810_sc->as_dev,
    540  1.1  riastrad 		    "write gtt entry"
    541  1.1  riastrad 		    " %"PRIxMAX" -> %"PRIxMAX" (flags=%x) failed: %d\n",
    542  1.1  riastrad 		    (uintmax_t)va, (uintmax_t)addr, flags,
    543  1.1  riastrad 		    error);
    544  1.1  riastrad 	agp_i810_post_gtt_entry(isc, va);
    545  1.1  riastrad 	intel_gtt_chipset_flush();
    546  1.1  riastrad }
    547  1.1  riastrad 
    548  1.1  riastrad void
    549  1.1  riastrad intel_gtt_insert_sg_entries(struct sg_table *sg, unsigned va_page,
    550  1.1  riastrad     unsigned flags)
    551  1.1  riastrad {
    552  1.1  riastrad 	bus_dmamap_t dmamap = sg->sgl[0].sg_dmamap;
    553  1.1  riastrad 	struct agp_i810_softc *const isc = agp_i810_sc->as_chipc;
    554  1.1  riastrad 	off_t va = (off_t)va_page << PAGE_SHIFT;
    555  1.1  riastrad 	unsigned seg;
    556  1.1  riastrad 	int gtt_flags = intel_gtt_flags(flags);
    557  1.1  riastrad 	int error;
    558  1.1  riastrad 
    559  1.1  riastrad 	KASSERT(0 <= va);
    560  1.1  riastrad 	KASSERT((va >> PAGE_SHIFT) == va_page);
    561  1.1  riastrad 	KASSERT(0 < dmamap->dm_nsegs);
    562  1.1  riastrad 
    563  1.1  riastrad 	for (seg = 0; seg < dmamap->dm_nsegs; seg++) {
    564  1.1  riastrad 		const bus_addr_t addr = dmamap->dm_segs[seg].ds_addr;
    565  1.1  riastrad 		bus_size_t len;
    566  1.1  riastrad 
    567  1.1  riastrad 		for (len = dmamap->dm_segs[seg].ds_len;
    568  1.1  riastrad 		     len >= PAGE_SIZE;
    569  1.1  riastrad 		     len -= PAGE_SIZE, va += PAGE_SIZE) {
    570  1.1  riastrad 			error = agp_i810_write_gtt_entry(isc, va, addr,
    571  1.1  riastrad 			    gtt_flags);
    572  1.1  riastrad 			if (error)
    573  1.1  riastrad 				device_printf(agp_i810_sc->as_dev,
    574  1.1  riastrad 				    "write gtt entry"
    575  1.1  riastrad 				    " %"PRIxMAX" -> %"PRIxMAX" (flags=%x)"
    576  1.1  riastrad 				    " failed: %d\n",
    577  1.1  riastrad 				    (uintmax_t)va, (uintmax_t)addr, flags,
    578  1.1  riastrad 				    error);
    579  1.1  riastrad 		}
    580  1.1  riastrad 		KASSERTMSG(len == 0,
    581  1.1  riastrad 		    "segment length not divisible by PAGE_SIZE: %jx",
    582  1.1  riastrad 		    (uintmax_t)dmamap->dm_segs[seg].ds_len);
    583  1.1  riastrad 	}
    584  1.1  riastrad 	agp_i810_post_gtt_entry(isc, (va - PAGE_SIZE));
    585  1.1  riastrad 	intel_gtt_chipset_flush();
    586  1.1  riastrad }
    587  1.1  riastrad 
    588  1.1  riastrad void
    589  1.1  riastrad intel_gtt_clear_range(unsigned va_page, unsigned npages)
    590  1.1  riastrad {
    591  1.1  riastrad 	struct agp_i810_softc *const isc = agp_i810_sc->as_chipc;
    592  1.1  riastrad 	const bus_addr_t addr = intel_gtt.scratch_map->dm_segs[0].ds_addr;
    593  1.1  riastrad 	const int gtt_flags = AGP_I810_GTT_VALID;
    594  1.1  riastrad 	off_t va = (va_page << PAGE_SHIFT);
    595  1.1  riastrad 
    596  1.1  riastrad 	KASSERT(0 <= va);
    597  1.1  riastrad 	KASSERT((va >> PAGE_SHIFT) == va_page);
    598  1.1  riastrad 	KASSERT(0 < npages);
    599  1.1  riastrad 
    600  1.1  riastrad 	while (npages--) {
    601  1.1  riastrad 		agp_i810_write_gtt_entry(isc, va, addr, gtt_flags);
    602  1.1  riastrad 		va += PAGE_SIZE;
    603  1.1  riastrad 	}
    604  1.1  riastrad 	agp_i810_post_gtt_entry(isc, va - PAGE_SIZE);
    605  1.1  riastrad }
    606