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