Home | History | Annotate | Line # | Download | only in pci
agp_i810.c revision 1.4
      1 /*	$NetBSD: agp_i810.c,v 1.4 2001/09/13 16:18:53 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 static int
     91 agp_i810_vgamatch(struct pci_attach_args *pa)
     92 {
     93 	if (PCI_CLASS(pa->pa_class) != PCI_CLASS_DISPLAY ||
     94 	    PCI_SUBCLASS(pa->pa_class) != PCI_SUBCLASS_DISPLAY_VGA)
     95 		return 0;
     96 	switch (PCI_PRODUCT(pa->pa_id)) {
     97 	case PCI_PRODUCT_INTEL_82810_GC:
     98 	case PCI_PRODUCT_INTEL_82810_DC100_GC:
     99 	case PCI_PRODUCT_INTEL_82810E_GC:
    100 	case PCI_PRODUCT_INTEL_82815_FULL_GRAPH:
    101 		return 1;
    102 	};
    103 
    104 	return 0;
    105 }
    106 
    107 /*
    108  * Find bridge device.
    109  */
    110 int
    111 agp_i810_bridgematch(struct pci_attach_args *pa)
    112 {
    113 	switch (PCI_PRODUCT(pa->pa_id)) {
    114 	case PCI_PRODUCT_INTEL_82810_MCH:
    115 	case PCI_PRODUCT_INTEL_82810_DC100_MCH:
    116 	case PCI_PRODUCT_INTEL_82810E_MCH:
    117 	case PCI_PRODUCT_INTEL_82815_FULL_HUB:
    118 		return 1;
    119 	}
    120 
    121 	return 0;
    122 }
    123 
    124 int
    125 agp_i810_match(struct device *parent, struct cfdata *match, void *aux)
    126 {
    127 	struct pci_attach_args vga_pa, *pa = aux;
    128 	pcireg_t ramreg;
    129 
    130 	if (agp_i810_bridgematch(pa) == 0)
    131 		return 0;
    132 	/*
    133 	 * XXXXfvdl
    134 	 * this relies on the 'memory hub' and the VGA controller
    135 	 * being on the same bus, which is bad. Fortunately, we
    136 	 * know this to be the case with the i810.
    137 	 *
    138 	 * Could just have the attach fail later, leave as_chipc NULL
    139 	 * and fail any open() call.
    140 	 */
    141 	if (pci_find_device(&vga_pa, agp_i810_vgamatch) == 0)
    142 		return 0;
    143 
    144 	ramreg = pci_conf_read(pa->pa_pc, pa->pa_tag, AGP_I810_SMRAM);
    145 	if ((ramreg & 0xff) == 0)
    146 		return 0;
    147 
    148 	return 1;
    149 }
    150 
    151 int
    152 agp_i810_attach(struct device *parent, struct device *self, void *aux)
    153 {
    154 	struct agp_softc *sc = (void *)self;
    155 	struct agp_i810_softc *isc;
    156 	struct agp_gatt *gatt;
    157 	int error;
    158 
    159 	isc = malloc(sizeof *isc, M_AGP, M_NOWAIT);
    160 	if (isc == NULL) {
    161 		printf(": can't allocate chipset-specific softc\n");
    162 		return ENOMEM;
    163 	}
    164 	memset(isc, 0, sizeof *isc);
    165 	sc->as_chipc = isc;
    166 	sc->as_methods = &agp_i810_methods;
    167 
    168 	if (pci_find_device(&isc->vga_pa, agp_i810_vgamatch) == 0) {
    169 		free(isc, M_AGP);
    170 		return ENOENT;
    171 	}
    172 
    173 	/* XXXfvdl */
    174 	sc->as_dmat = isc->vga_pa.pa_dmat;
    175 
    176 	error = agp_map_aperture(&isc->vga_pa, sc);
    177 	if (error != 0) {
    178 		free(isc, M_AGP);
    179 		return error;
    180 	}
    181 
    182 	error = pci_mapreg_map(&isc->vga_pa, AGP_I810_MMADR,
    183 	    PCI_MAPREG_TYPE_MEM, 0, &isc->bst, &isc->bsh, NULL, NULL);
    184 	if (error != 0) {
    185 		printf(": can't map mmadr registers\n");
    186 		return error;
    187 	}
    188 
    189 	isc->initial_aperture = AGP_GET_APERTURE(sc);
    190 
    191 	if (READ1(AGP_I810_DRT) & AGP_I810_DRT_POPULATED)
    192 		isc->dcache_size = 4 * 1024 * 1024;
    193 	else
    194 		isc->dcache_size = 0;
    195 
    196 	for (;;) {
    197 		gatt = agp_alloc_gatt(sc);
    198 		if (gatt)
    199 			break;
    200 
    201 		/*
    202 		 * Probably contigmalloc failure. Try reducing the
    203 		 * aperture so that the gatt size reduces.
    204 		 */
    205 		if (AGP_SET_APERTURE(sc, AGP_GET_APERTURE(sc) / 2)) {
    206 			agp_generic_detach(sc);
    207 			return ENOMEM;
    208 		}
    209 	}
    210 	isc->gatt = gatt;
    211 
    212 	/* Install the GATT. */
    213 	WRITE4(AGP_I810_PGTBL_CTL, gatt->ag_physical | 1);
    214 
    215 	/*
    216 	 * Make sure the chipset can see everything.
    217 	 */
    218 	agp_flush_cache();
    219 
    220 	return 0;
    221 }
    222 
    223 #if 0
    224 static int
    225 agp_i810_detach(struct agp_softc *sc)
    226 {
    227 	int error;
    228 	struct agp_i810_softc *isc = sc->as_chipc;
    229 
    230 	error = agp_generic_detach(sc);
    231 	if (error)
    232 		return error;
    233 
    234 	/* Clear the GATT base. */
    235 	WRITE4(AGP_I810_PGTBL_CTL, 0);
    236 
    237 	/* Put the aperture back the way it started. */
    238 	AGP_SET_APERTURE(sc, isc->initial_aperture);
    239 
    240 	agp_free_gatt(sc, isc->gatt);
    241 
    242 	return 0;
    243 }
    244 #endif
    245 
    246 static u_int32_t
    247 agp_i810_get_aperture(struct agp_softc *sc)
    248 {
    249 	u_int16_t miscc;
    250 
    251 	miscc = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I810_SMRAM) >> 16;
    252 	if ((miscc & AGP_I810_MISCC_WINSIZE) == AGP_I810_MISCC_WINSIZE_32)
    253 		return 32 * 1024 * 1024;
    254 	else
    255 		return 64 * 1024 * 1024;
    256 }
    257 
    258 static int
    259 agp_i810_set_aperture(struct agp_softc *sc, u_int32_t aperture)
    260 {
    261 	pcireg_t reg, miscc;
    262 
    263 	/*
    264 	 * Double check for sanity.
    265 	 */
    266 	if (aperture != 32 * 1024 * 1024 && aperture != 64 * 1024 * 1024) {
    267 		printf("%s: bad aperture size %d\n", sc->as_dev.dv_xname,
    268 		       aperture);
    269 		return EINVAL;
    270 	}
    271 
    272 	reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_I810_SMRAM);
    273 	miscc = reg >> 16;
    274 	miscc &= ~AGP_I810_MISCC_WINSIZE;
    275 	if (aperture == 32 * 1024 * 1024)
    276 		miscc |= AGP_I810_MISCC_WINSIZE_32;
    277 	else
    278 		miscc |= AGP_I810_MISCC_WINSIZE_64;
    279 
    280 	reg &= 0x0000ffff;
    281 	reg |= (miscc << 16);
    282 	pci_conf_write(sc->as_pc, sc->as_tag, AGP_I810_SMRAM, miscc);
    283 
    284 	return 0;
    285 }
    286 
    287 static int
    288 agp_i810_bind_page(struct agp_softc *sc, off_t offset, bus_addr_t physical)
    289 {
    290 	struct agp_i810_softc *isc = sc->as_chipc;
    291 
    292 	if (offset < 0 || offset >= (isc->gatt->ag_entries << AGP_PAGE_SHIFT))
    293 		return EINVAL;
    294 
    295 	WRITE4(AGP_I810_GTT + (u_int32_t)(offset >> AGP_PAGE_SHIFT) * 4,
    296 	    physical | 1);
    297 	return 0;
    298 }
    299 
    300 static int
    301 agp_i810_unbind_page(struct agp_softc *sc, off_t offset)
    302 {
    303 	struct agp_i810_softc *isc = sc->as_chipc;
    304 
    305 	if (offset < 0 || offset >= (isc->gatt->ag_entries << AGP_PAGE_SHIFT))
    306 		return EINVAL;
    307 
    308 	WRITE4(AGP_I810_GTT + (u_int32_t)(offset >> AGP_PAGE_SHIFT) * 4, 0);
    309 	return 0;
    310 }
    311 
    312 /*
    313  * Writing via memory mapped registers already flushes all TLBs.
    314  */
    315 static void
    316 agp_i810_flush_tlb(struct agp_softc *sc)
    317 {
    318 }
    319 
    320 static int
    321 agp_i810_enable(struct agp_softc *sc, u_int32_t mode)
    322 {
    323 
    324 	return 0;
    325 }
    326 
    327 static struct agp_memory *
    328 agp_i810_alloc_memory(struct agp_softc *sc, int type, vsize_t size)
    329 {
    330 	struct agp_i810_softc *isc = sc->as_chipc;
    331 	struct agp_memory *mem;
    332 
    333 	if ((size & (AGP_PAGE_SIZE - 1)) != 0)
    334 		return 0;
    335 
    336 	if (sc->as_allocated + size > sc->as_maxmem)
    337 		return 0;
    338 
    339 	if (type == 1) {
    340 		/*
    341 		 * Mapping local DRAM into GATT.
    342 		 */
    343 		if (size != isc->dcache_size)
    344 			return 0;
    345 	} else if (type == 2) {
    346 		/*
    347 		 * Bogus mapping of a single page for the hardware cursor.
    348 		 */
    349 		if (size != AGP_PAGE_SIZE)
    350 			return 0;
    351 	}
    352 
    353 	mem = malloc(sizeof *mem, M_AGP, M_WAITOK);
    354 	if (mem == NULL)
    355 		return NULL;
    356 	memset(mem, 0, sizeof *mem);
    357 	mem->am_id = sc->as_nextid++;
    358 	mem->am_size = size;
    359 	mem->am_type = type;
    360 
    361 	if (type == 2) {
    362 		/*
    363 		 * Allocate and wire down the page now so that we can
    364 		 * get its physical address.
    365 		 */
    366 		mem->am_dmaseg = malloc(sizeof *mem->am_dmaseg, M_AGP,
    367 		    M_WAITOK);
    368 		if (mem->am_dmaseg == NULL) {
    369 			free(mem, M_AGP);
    370 			return NULL;
    371 		}
    372 		if (agp_alloc_dmamem(sc->as_dmat, size, 0,
    373 		    &mem->am_dmamap, &mem->am_virtual, &mem->am_physical,
    374 		    mem->am_dmaseg, 1, &mem->am_nseg) != 0) {
    375 			free(mem->am_dmaseg, M_AGP);
    376 			free(mem, M_AGP);
    377 			return NULL;
    378 		}
    379 	} else if (type != 1) {
    380 		if (bus_dmamap_create(sc->as_dmat, size, size / PAGE_SIZE + 1,
    381 				      size, 0, BUS_DMA_NOWAIT,
    382 				      &mem->am_dmamap) != 0) {
    383 			free(mem, M_AGP);
    384 			return NULL;
    385 		}
    386 	}
    387 
    388 	TAILQ_INSERT_TAIL(&sc->as_memory, mem, am_link);
    389 	sc->as_allocated += size;
    390 
    391 	return mem;
    392 }
    393 
    394 static int
    395 agp_i810_free_memory(struct agp_softc *sc, struct agp_memory *mem)
    396 {
    397 	if (mem->am_is_bound)
    398 		return EBUSY;
    399 
    400 	if (mem->am_type == 2) {
    401 		agp_free_dmamem(sc->as_dmat, mem->am_size, mem->am_dmamap,
    402 		    mem->am_virtual, mem->am_dmaseg, mem->am_nseg);
    403 		free(mem->am_dmaseg, M_AGP);
    404 	}
    405 
    406 	sc->as_allocated -= mem->am_size;
    407 	TAILQ_REMOVE(&sc->as_memory, mem, am_link);
    408 	free(mem, M_AGP);
    409 	return 0;
    410 }
    411 
    412 static int
    413 agp_i810_bind_memory(struct agp_softc *sc, struct agp_memory *mem,
    414 		     off_t offset)
    415 {
    416 	struct agp_i810_softc *isc = sc->as_chipc;
    417 	u_int32_t regval, i;
    418 
    419 	/*
    420 	 * XXX evil hack: the PGTBL_CTL appearently gets overwritten by the
    421 	 * X server for mysterious reasons which leads to crashes if we write
    422 	 * to the GTT through the MMIO window.
    423 	 * Until the issue is solved, simply restore it.
    424 	 */
    425 	regval = bus_space_read_4(isc->bst, isc->bsh, AGP_I810_PGTBL_CTL);
    426 	if (regval != (isc->gatt->ag_physical | 1)) {
    427 		printf("agp_i810_bind_memory: PGTBL_CTL is 0x%x - fixing\n",
    428 		       regval);
    429 		bus_space_write_4(isc->bst, isc->bsh, AGP_I810_PGTBL_CTL,
    430 				  isc->gatt->ag_physical | 1);
    431 	}
    432 
    433 	if (mem->am_type == 2)
    434 		return 0;
    435 	if (mem->am_type != 1)
    436 		return agp_generic_bind_memory(sc, mem, offset);
    437 
    438 	for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) {
    439 		WRITE4(AGP_I810_GTT + (u_int32_t)(offset >> AGP_PAGE_SHIFT) * 4,
    440 		       i | 3);
    441 	}
    442 
    443 	return 0;
    444 }
    445 
    446 static int
    447 agp_i810_unbind_memory(struct agp_softc *sc, struct agp_memory *mem)
    448 {
    449 	struct agp_i810_softc *isc = sc->as_chipc;
    450 	u_int32_t i;
    451 
    452 	if (mem->am_type == 2)
    453 		return 0;
    454 
    455 	if (mem->am_type != 1)
    456 		return agp_generic_unbind_memory(sc, mem);
    457 
    458 	for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE)
    459 		WRITE4(AGP_I810_GTT + (i >> AGP_PAGE_SHIFT) * 4, 0);
    460 
    461 	return 0;
    462 }
    463