Home | History | Annotate | Line # | Download | only in pci
agp_i810.c revision 1.10
      1 /*	$NetBSD: agp_i810.c,v 1.10 2002/01/12 16:17:05 tsutsui Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000 Doug Rabson
      5  * Copyright (c) 2000 Ruslan Ermilov
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27  * SUCH DAMAGE.
     28  *
     29  *	$FreeBSD: src/sys/pci/agp_i810.c,v 1.4 2001/07/05 21:28:47 jhb Exp $
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 __KERNEL_RCSID(0, "$NetBSD: agp_i810.c,v 1.10 2002/01/12 16:17:05 tsutsui Exp $");
     34 
     35 #include <sys/param.h>
     36 #include <sys/systm.h>
     37 #include <sys/malloc.h>
     38 #include <sys/kernel.h>
     39 #include <sys/lock.h>
     40 #include <sys/proc.h>
     41 #include <sys/device.h>
     42 #include <sys/conf.h>
     43 
     44 #include <uvm/uvm_extern.h>
     45 
     46 #include <dev/pci/pcivar.h>
     47 #include <dev/pci/pcireg.h>
     48 #include <dev/pci/pcidevs.h>
     49 #include <dev/pci/agpvar.h>
     50 #include <dev/pci/agpreg.h>
     51 
     52 #include <sys/agpio.h>
     53 
     54 #include <machine/bus.h>
     55 
     56 #define READ1(off)	bus_space_read_1(isc->bst, isc->bsh, off)
     57 #define WRITE4(off,v)	bus_space_write_4(isc->bst, isc->bsh, off, v)
     58 
     59 struct agp_i810_softc {
     60 	u_int32_t initial_aperture;	/* aperture size at startup */
     61 	struct agp_gatt *gatt;
     62 	u_int32_t dcache_size;
     63 	bus_space_tag_t bst;		/* bus_space tag */
     64 	bus_space_handle_t bsh;		/* bus_space handle */
     65 	struct pci_attach_args vga_pa;
     66 };
     67 
     68 static u_int32_t agp_i810_get_aperture(struct agp_softc *);
     69 static int agp_i810_set_aperture(struct agp_softc *, u_int32_t);
     70 static int agp_i810_bind_page(struct agp_softc *, off_t, bus_addr_t);
     71 static int agp_i810_unbind_page(struct agp_softc *, off_t);
     72 static void agp_i810_flush_tlb(struct agp_softc *);
     73 static int agp_i810_enable(struct agp_softc *, u_int32_t mode);
     74 static struct agp_memory *agp_i810_alloc_memory(struct agp_softc *, int,
     75 						vsize_t);
     76 static int agp_i810_free_memory(struct agp_softc *, struct agp_memory *);
     77 static int agp_i810_bind_memory(struct agp_softc *, struct agp_memory *, off_t);
     78 static int agp_i810_unbind_memory(struct agp_softc *, struct agp_memory *);
     79 
     80 struct agp_methods agp_i810_methods = {
     81 	agp_i810_get_aperture,
     82 	agp_i810_set_aperture,
     83 	agp_i810_bind_page,
     84 	agp_i810_unbind_page,
     85 	agp_i810_flush_tlb,
     86 	agp_i810_enable,
     87 	agp_i810_alloc_memory,
     88 	agp_i810_free_memory,
     89 	agp_i810_bind_memory,
     90 	agp_i810_unbind_memory,
     91 };
     92 
     93 /* XXXthorpej -- duplicated code (see arch/i386/pci/pchb.c) */
     94 static int
     95 agp_i810_vgamatch(struct pci_attach_args *pa)
     96 {
     97 
     98 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY ||
     99 	    PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_DISPLAY_VGA)
    100 		return (0);
    101 
    102 	switch (PCI_PRODUCT(pa->pa_id)) {
    103 	case PCI_PRODUCT_INTEL_82810_GC:
    104 	case PCI_PRODUCT_INTEL_82810_DC100_GC:
    105 	case PCI_PRODUCT_INTEL_82810E_GC:
    106 	case PCI_PRODUCT_INTEL_82815_FULL_GRAPH:
    107 		return (1);
    108 	}
    109 
    110 	return (0);
    111 }
    112 
    113 int
    114 agp_i810_attach(struct device *parent, struct device *self, void *aux)
    115 {
    116 	struct agp_softc *sc = (void *)self;
    117 	struct agp_i810_softc *isc;
    118 	struct agp_gatt *gatt;
    119 	int error;
    120 
    121 	isc = malloc(sizeof *isc, M_AGP, M_NOWAIT|M_ZERO);
    122 	if (isc == NULL) {
    123 		printf(": can't allocate chipset-specific softc\n");
    124 		return ENOMEM;
    125 	}
    126 	sc->as_chipc = isc;
    127 	sc->as_methods = &agp_i810_methods;
    128 
    129 	if (pci_find_device(&isc->vga_pa, agp_i810_vgamatch) == 0) {
    130 		printf(": can't find internal VGA device config space\n");
    131 		free(isc, M_AGP);
    132 		return ENOENT;
    133 	}
    134 
    135 	/* XXXfvdl */
    136 	sc->as_dmat = isc->vga_pa.pa_dmat;
    137 
    138 	error = agp_map_aperture(&isc->vga_pa, sc);
    139 	if (error != 0) {
    140 		printf(": can't map aperture\n");
    141 		free(isc, M_AGP);
    142 		return error;
    143 	}
    144 
    145 	error = pci_mapreg_map(&isc->vga_pa, AGP_I810_MMADR,
    146 	    PCI_MAPREG_TYPE_MEM, 0, &isc->bst, &isc->bsh, NULL, NULL);
    147 	if (error != 0) {
    148 		printf(": can't map mmadr registers\n");
    149 		return error;
    150 	}
    151 
    152 	isc->initial_aperture = AGP_GET_APERTURE(sc);
    153 
    154 	if (READ1(AGP_I810_DRT) & AGP_I810_DRT_POPULATED)
    155 		isc->dcache_size = 4 * 1024 * 1024;
    156 	else
    157 		isc->dcache_size = 0;
    158 
    159 	for (;;) {
    160 		gatt = agp_alloc_gatt(sc);
    161 		if (gatt)
    162 			break;
    163 
    164 		/*
    165 		 * Probably contigmalloc failure. Try reducing the
    166 		 * aperture so that the gatt size reduces.
    167 		 */
    168 		if (AGP_SET_APERTURE(sc, AGP_GET_APERTURE(sc) / 2)) {
    169 			agp_generic_detach(sc);
    170 			return ENOMEM;
    171 		}
    172 	}
    173 	isc->gatt = gatt;
    174 
    175 	/* Install the GATT. */
    176 	WRITE4(AGP_I810_PGTBL_CTL, gatt->ag_physical | 1);
    177 
    178 	/*
    179 	 * Make sure the chipset can see everything.
    180 	 */
    181 	agp_flush_cache();
    182 
    183 	return 0;
    184 }
    185 
    186 #if 0
    187 static int
    188 agp_i810_detach(struct agp_softc *sc)
    189 {
    190 	int error;
    191 	struct agp_i810_softc *isc = sc->as_chipc;
    192 
    193 	error = agp_generic_detach(sc);
    194 	if (error)
    195 		return error;
    196 
    197 	/* Clear the GATT base. */
    198 	WRITE4(AGP_I810_PGTBL_CTL, 0);
    199 
    200 	/* Put the aperture back the way it started. */
    201 	AGP_SET_APERTURE(sc, isc->initial_aperture);
    202 
    203 	agp_free_gatt(sc, isc->gatt);
    204 
    205 	return 0;
    206 }
    207 #endif
    208 
    209 static u_int32_t
    210 agp_i810_get_aperture(struct agp_softc *sc)
    211 {
    212 	u_int16_t miscc;
    213 
    214 	miscc = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I810_SMRAM) >> 16;
    215 	if ((miscc & AGP_I810_MISCC_WINSIZE) == AGP_I810_MISCC_WINSIZE_32)
    216 		return 32 * 1024 * 1024;
    217 	else
    218 		return 64 * 1024 * 1024;
    219 }
    220 
    221 static int
    222 agp_i810_set_aperture(struct agp_softc *sc, u_int32_t aperture)
    223 {
    224 	pcireg_t reg, miscc;
    225 
    226 	/*
    227 	 * Double check for sanity.
    228 	 */
    229 	if (aperture != 32 * 1024 * 1024 && aperture != 64 * 1024 * 1024) {
    230 		printf("%s: bad aperture size %d\n", sc->as_dev.dv_xname,
    231 		       aperture);
    232 		return EINVAL;
    233 	}
    234 
    235 	reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I810_SMRAM);
    236 	miscc = reg >> 16;
    237 	miscc &= ~AGP_I810_MISCC_WINSIZE;
    238 	if (aperture == 32 * 1024 * 1024)
    239 		miscc |= AGP_I810_MISCC_WINSIZE_32;
    240 	else
    241 		miscc |= AGP_I810_MISCC_WINSIZE_64;
    242 
    243 	reg &= 0x0000ffff;
    244 	reg |= (miscc << 16);
    245 	pci_conf_write(sc->as_pc, sc->as_tag, AGP_I810_SMRAM, miscc);
    246 
    247 	return 0;
    248 }
    249 
    250 static int
    251 agp_i810_bind_page(struct agp_softc *sc, off_t offset, bus_addr_t physical)
    252 {
    253 	struct agp_i810_softc *isc = sc->as_chipc;
    254 
    255 	if (offset < 0 || offset >= (isc->gatt->ag_entries << AGP_PAGE_SHIFT))
    256 		return EINVAL;
    257 
    258 	WRITE4(AGP_I810_GTT + (u_int32_t)(offset >> AGP_PAGE_SHIFT) * 4,
    259 	    physical | 1);
    260 	return 0;
    261 }
    262 
    263 static int
    264 agp_i810_unbind_page(struct agp_softc *sc, off_t offset)
    265 {
    266 	struct agp_i810_softc *isc = sc->as_chipc;
    267 
    268 	if (offset < 0 || offset >= (isc->gatt->ag_entries << AGP_PAGE_SHIFT))
    269 		return EINVAL;
    270 
    271 	WRITE4(AGP_I810_GTT + (u_int32_t)(offset >> AGP_PAGE_SHIFT) * 4, 0);
    272 	return 0;
    273 }
    274 
    275 /*
    276  * Writing via memory mapped registers already flushes all TLBs.
    277  */
    278 static void
    279 agp_i810_flush_tlb(struct agp_softc *sc)
    280 {
    281 }
    282 
    283 static int
    284 agp_i810_enable(struct agp_softc *sc, u_int32_t mode)
    285 {
    286 
    287 	return 0;
    288 }
    289 
    290 static struct agp_memory *
    291 agp_i810_alloc_memory(struct agp_softc *sc, int type, vsize_t size)
    292 {
    293 	struct agp_i810_softc *isc = sc->as_chipc;
    294 	struct agp_memory *mem;
    295 
    296 	if ((size & (AGP_PAGE_SIZE - 1)) != 0)
    297 		return 0;
    298 
    299 	if (sc->as_allocated + size > sc->as_maxmem)
    300 		return 0;
    301 
    302 	if (type == 1) {
    303 		/*
    304 		 * Mapping local DRAM into GATT.
    305 		 */
    306 		if (size != isc->dcache_size)
    307 			return 0;
    308 	} else if (type == 2) {
    309 		/*
    310 		 * Bogus mapping of a single page for the hardware cursor.
    311 		 */
    312 		if (size != AGP_PAGE_SIZE)
    313 			return 0;
    314 	}
    315 
    316 	mem = malloc(sizeof *mem, M_AGP, M_WAITOK|M_ZERO);
    317 	if (mem == NULL)
    318 		return NULL;
    319 	mem->am_id = sc->as_nextid++;
    320 	mem->am_size = size;
    321 	mem->am_type = type;
    322 
    323 	if (type == 2) {
    324 		/*
    325 		 * Allocate and wire down the page now so that we can
    326 		 * get its physical address.
    327 		 */
    328 		mem->am_dmaseg = malloc(sizeof *mem->am_dmaseg, M_AGP,
    329 		    M_WAITOK);
    330 		if (mem->am_dmaseg == NULL) {
    331 			free(mem, M_AGP);
    332 			return NULL;
    333 		}
    334 		if (agp_alloc_dmamem(sc->as_dmat, size, 0,
    335 		    &mem->am_dmamap, &mem->am_virtual, &mem->am_physical,
    336 		    mem->am_dmaseg, 1, &mem->am_nseg) != 0) {
    337 			free(mem->am_dmaseg, M_AGP);
    338 			free(mem, M_AGP);
    339 			return NULL;
    340 		}
    341 	} else if (type != 1) {
    342 		if (bus_dmamap_create(sc->as_dmat, size, size / PAGE_SIZE + 1,
    343 				      size, 0, BUS_DMA_NOWAIT,
    344 				      &mem->am_dmamap) != 0) {
    345 			free(mem, M_AGP);
    346 			return NULL;
    347 		}
    348 	}
    349 
    350 	TAILQ_INSERT_TAIL(&sc->as_memory, mem, am_link);
    351 	sc->as_allocated += size;
    352 
    353 	return mem;
    354 }
    355 
    356 static int
    357 agp_i810_free_memory(struct agp_softc *sc, struct agp_memory *mem)
    358 {
    359 	if (mem->am_is_bound)
    360 		return EBUSY;
    361 
    362 	if (mem->am_type == 2) {
    363 		agp_free_dmamem(sc->as_dmat, mem->am_size, mem->am_dmamap,
    364 		    mem->am_virtual, mem->am_dmaseg, mem->am_nseg);
    365 		free(mem->am_dmaseg, M_AGP);
    366 	}
    367 
    368 	sc->as_allocated -= mem->am_size;
    369 	TAILQ_REMOVE(&sc->as_memory, mem, am_link);
    370 	free(mem, M_AGP);
    371 	return 0;
    372 }
    373 
    374 static int
    375 agp_i810_bind_memory(struct agp_softc *sc, struct agp_memory *mem,
    376 		     off_t offset)
    377 {
    378 	struct agp_i810_softc *isc = sc->as_chipc;
    379 	u_int32_t regval, i;
    380 
    381 	/*
    382 	 * XXX evil hack: the PGTBL_CTL appearently gets overwritten by the
    383 	 * X server for mysterious reasons which leads to crashes if we write
    384 	 * to the GTT through the MMIO window.
    385 	 * Until the issue is solved, simply restore it.
    386 	 */
    387 	regval = bus_space_read_4(isc->bst, isc->bsh, AGP_I810_PGTBL_CTL);
    388 	if (regval != (isc->gatt->ag_physical | 1)) {
    389 		printf("agp_i810_bind_memory: PGTBL_CTL is 0x%x - fixing\n",
    390 		       regval);
    391 		bus_space_write_4(isc->bst, isc->bsh, AGP_I810_PGTBL_CTL,
    392 				  isc->gatt->ag_physical | 1);
    393 	}
    394 
    395 	if (mem->am_type == 2) {
    396 		WRITE4(AGP_I810_GTT + (u_int32_t)(offset >> AGP_PAGE_SHIFT) * 4,
    397 		       mem->am_physical | 1);
    398 		mem->am_offset = offset;
    399 		mem->am_is_bound = 1;
    400 		return 0;
    401 	}
    402 
    403 	if (mem->am_type != 1)
    404 		return agp_generic_bind_memory(sc, mem, offset);
    405 
    406 	for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) {
    407 		WRITE4(AGP_I810_GTT + (u_int32_t)(offset >> AGP_PAGE_SHIFT) * 4,
    408 		       i | 3);
    409 	}
    410 
    411 	return 0;
    412 }
    413 
    414 static int
    415 agp_i810_unbind_memory(struct agp_softc *sc, struct agp_memory *mem)
    416 {
    417 	struct agp_i810_softc *isc = sc->as_chipc;
    418 	u_int32_t i;
    419 
    420 	if (mem->am_type == 2) {
    421 		WRITE4(AGP_I810_GTT +
    422 		       (u_int32_t)(mem->am_offset >> AGP_PAGE_SHIFT) * 4,
    423 		       0);
    424 		mem->am_offset = 0;
    425 		mem->am_is_bound = 0;
    426 		return 0;
    427 	}
    428 
    429 	if (mem->am_type != 1)
    430 		return agp_generic_unbind_memory(sc, mem);
    431 
    432 	for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE)
    433 		WRITE4(AGP_I810_GTT + (i >> AGP_PAGE_SHIFT) * 4, 0);
    434 
    435 	return 0;
    436 }
    437