Home | History | Annotate | Line # | Download | only in pci
agp_amd.c revision 1.20.20.1
      1  1.20.20.1     rmind /*	$NetBSD: agp_amd.c,v 1.20.20.1 2011/03/05 20:53:34 rmind Exp $	*/
      2        1.1      fvdl 
      3        1.1      fvdl /*-
      4        1.1      fvdl  * Copyright (c) 2000 Doug Rabson
      5        1.1      fvdl  * All rights reserved.
      6        1.1      fvdl  *
      7        1.1      fvdl  * Redistribution and use in source and binary forms, with or without
      8        1.1      fvdl  * modification, are permitted provided that the following conditions
      9        1.1      fvdl  * are met:
     10        1.1      fvdl  * 1. Redistributions of source code must retain the above copyright
     11        1.1      fvdl  *    notice, this list of conditions and the following disclaimer.
     12        1.1      fvdl  * 2. Redistributions in binary form must reproduce the above copyright
     13        1.1      fvdl  *    notice, this list of conditions and the following disclaimer in the
     14        1.1      fvdl  *    documentation and/or other materials provided with the distribution.
     15        1.1      fvdl  *
     16        1.1      fvdl  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17        1.1      fvdl  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18        1.1      fvdl  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19        1.1      fvdl  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20        1.1      fvdl  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21        1.1      fvdl  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22        1.1      fvdl  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23        1.1      fvdl  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24        1.1      fvdl  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25        1.1      fvdl  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26        1.1      fvdl  * SUCH DAMAGE.
     27        1.1      fvdl  *
     28        1.1      fvdl  *	$FreeBSD: src/sys/pci/agp_amd.c,v 1.6 2001/07/05 21:28:46 jhb Exp $
     29        1.1      fvdl  */
     30        1.7     lukem 
     31        1.7     lukem #include <sys/cdefs.h>
     32  1.20.20.1     rmind __KERNEL_RCSID(0, "$NetBSD: agp_amd.c,v 1.20.20.1 2011/03/05 20:53:34 rmind Exp $");
     33        1.1      fvdl 
     34        1.1      fvdl #include <sys/param.h>
     35        1.1      fvdl #include <sys/systm.h>
     36        1.1      fvdl #include <sys/malloc.h>
     37        1.1      fvdl #include <sys/kernel.h>
     38        1.1      fvdl #include <sys/proc.h>
     39        1.1      fvdl #include <sys/conf.h>
     40        1.1      fvdl #include <sys/device.h>
     41        1.1      fvdl #include <sys/agpio.h>
     42        1.1      fvdl 
     43        1.1      fvdl #include <dev/pci/pcivar.h>
     44        1.1      fvdl #include <dev/pci/pcireg.h>
     45        1.1      fvdl #include <dev/pci/agpvar.h>
     46        1.1      fvdl #include <dev/pci/agpreg.h>
     47        1.1      fvdl 
     48        1.1      fvdl #include <dev/pci/pcidevs.h>
     49        1.1      fvdl 
     50        1.1      fvdl #define READ2(off)	bus_space_read_2(asc->iot, asc->ioh, off)
     51        1.1      fvdl #define READ4(off)	bus_space_read_4(asc->iot, asc->ioh, off)
     52        1.1      fvdl #define WRITE2(off,v)	bus_space_write_2(asc->iot, asc->ioh, off, v)
     53        1.1      fvdl #define WRITE4(off,v)	bus_space_write_4(asc->iot, asc->ioh, off, v)
     54        1.1      fvdl 
     55        1.1      fvdl struct agp_amd_gatt {
     56        1.1      fvdl 	bus_dmamap_t	ag_dmamap;
     57        1.1      fvdl 	bus_dma_segment_t ag_dmaseg;
     58        1.1      fvdl 	int		ag_nseg;
     59        1.1      fvdl 	u_int32_t	ag_entries;
     60        1.1      fvdl 	u_int32_t      *ag_vdir;	/* virtual address of page dir */
     61        1.1      fvdl 	bus_addr_t	ag_pdir;	/* bus address of page dir */
     62        1.1      fvdl 	u_int32_t      *ag_virtual;	/* virtual address of gatt */
     63        1.1      fvdl 	bus_addr_t	ag_physical;	/* bus address of gatt */
     64        1.1      fvdl 	size_t		ag_size;
     65        1.1      fvdl };
     66        1.1      fvdl 
     67        1.1      fvdl struct agp_amd_softc {
     68        1.1      fvdl 	u_int32_t	initial_aperture; /* aperture size at startup */
     69        1.1      fvdl 	struct agp_amd_gatt *gatt;
     70        1.1      fvdl 	bus_space_handle_t ioh;
     71        1.1      fvdl 	bus_space_tag_t iot;
     72        1.1      fvdl };
     73        1.1      fvdl 
     74        1.1      fvdl static u_int32_t agp_amd_get_aperture(struct agp_softc *);
     75        1.1      fvdl static int agp_amd_set_aperture(struct agp_softc *, u_int32_t);
     76        1.1      fvdl static int agp_amd_bind_page(struct agp_softc *, off_t, bus_addr_t);
     77        1.1      fvdl static int agp_amd_unbind_page(struct agp_softc *, off_t);
     78        1.1      fvdl static void agp_amd_flush_tlb(struct agp_softc *);
     79        1.1      fvdl 
     80        1.1      fvdl 
     81       1.12   thorpej static struct agp_methods agp_amd_methods = {
     82        1.1      fvdl 	agp_amd_get_aperture,
     83        1.1      fvdl 	agp_amd_set_aperture,
     84        1.1      fvdl 	agp_amd_bind_page,
     85        1.1      fvdl 	agp_amd_unbind_page,
     86        1.1      fvdl 	agp_amd_flush_tlb,
     87        1.1      fvdl 	agp_generic_enable,
     88        1.1      fvdl 	agp_generic_alloc_memory,
     89        1.1      fvdl 	agp_generic_free_memory,
     90        1.1      fvdl 	agp_generic_bind_memory,
     91        1.1      fvdl 	agp_generic_unbind_memory,
     92        1.1      fvdl };
     93        1.1      fvdl 
     94        1.1      fvdl 
     95        1.1      fvdl static struct agp_amd_gatt *
     96        1.1      fvdl agp_amd_alloc_gatt(struct agp_softc *sc)
     97        1.1      fvdl {
     98        1.1      fvdl 	u_int32_t apsize = AGP_GET_APERTURE(sc);
     99        1.1      fvdl 	u_int32_t entries = apsize >> AGP_PAGE_SHIFT;
    100        1.1      fvdl 	struct agp_amd_gatt *gatt;
    101        1.1      fvdl 	int i, npages;
    102       1.18  christos 	void *vdir;
    103        1.1      fvdl 
    104        1.1      fvdl 	gatt = malloc(sizeof(struct agp_amd_gatt), M_AGP, M_NOWAIT);
    105        1.1      fvdl 	if (!gatt)
    106        1.1      fvdl 		return 0;
    107        1.1      fvdl 
    108        1.1      fvdl 	if (agp_alloc_dmamem(sc->as_dmat,
    109        1.1      fvdl 	    AGP_PAGE_SIZE + entries * sizeof(u_int32_t), 0,
    110        1.6   thorpej 	    &gatt->ag_dmamap, &vdir, &gatt->ag_pdir,
    111        1.1      fvdl 	    &gatt->ag_dmaseg, 1, &gatt->ag_nseg) != 0) {
    112        1.1      fvdl 		printf("failed to allocate GATT\n");
    113        1.6   thorpej 		free(gatt, M_AGP);
    114        1.1      fvdl 		return NULL;
    115        1.1      fvdl 	}
    116        1.1      fvdl 
    117        1.4      fvdl 	gatt->ag_vdir = (u_int32_t *)vdir;
    118        1.1      fvdl 	gatt->ag_entries = entries;
    119       1.18  christos 	gatt->ag_virtual = (u_int32_t *)((char *)vdir + AGP_PAGE_SIZE);
    120        1.1      fvdl 	gatt->ag_physical = gatt->ag_pdir + AGP_PAGE_SIZE;
    121        1.1      fvdl 	gatt->ag_size = AGP_PAGE_SIZE + entries * sizeof(u_int32_t);
    122        1.1      fvdl 
    123        1.1      fvdl 	memset(gatt->ag_vdir, 0, AGP_PAGE_SIZE);
    124        1.1      fvdl 	memset(gatt->ag_virtual, 0, entries * sizeof(u_int32_t));
    125        1.1      fvdl 
    126        1.1      fvdl 	/*
    127        1.1      fvdl 	 * Map the pages of the GATT into the page directory.
    128        1.1      fvdl 	 */
    129        1.1      fvdl 	npages = ((entries * sizeof(u_int32_t) + AGP_PAGE_SIZE - 1)
    130        1.1      fvdl 		  >> AGP_PAGE_SHIFT);
    131        1.1      fvdl 
    132        1.1      fvdl 	for (i = 0; i < npages; i++)
    133        1.1      fvdl 		gatt->ag_vdir[i] = (gatt->ag_physical + i * AGP_PAGE_SIZE) | 1;
    134        1.1      fvdl 
    135        1.1      fvdl 	/*
    136        1.1      fvdl 	 * Make sure the chipset can see everything.
    137        1.1      fvdl 	 */
    138        1.1      fvdl 	agp_flush_cache();
    139        1.1      fvdl 
    140        1.1      fvdl 	return gatt;
    141        1.1      fvdl }
    142        1.1      fvdl 
    143        1.1      fvdl #if 0
    144        1.1      fvdl static void
    145        1.1      fvdl agp_amd_free_gatt(struct agp_softc *sc, struct agp_amd_gatt *gatt)
    146        1.1      fvdl {
    147        1.1      fvdl 	agp_free_dmamem(sc->as_dmat, gatt->ag_size,
    148       1.18  christos 	    gatt->ag_dmamap, (void *)gatt->ag_virtual, &gatt->ag_dmaseg,
    149        1.1      fvdl 	    gatt->ag_nseg);
    150        1.1      fvdl 	free(gatt, M_AGP);
    151        1.1      fvdl }
    152        1.1      fvdl #endif
    153        1.1      fvdl 
    154        1.1      fvdl int
    155        1.2   thorpej agp_amd_match(const struct pci_attach_args *pa)
    156        1.1      fvdl {
    157        1.1      fvdl 
    158        1.1      fvdl 	switch (PCI_PRODUCT(pa->pa_id)) {
    159        1.1      fvdl 	case PCI_PRODUCT_AMD_SC751_SC:
    160       1.10   xtraeme 	case PCI_PRODUCT_AMD_SC761_SC:
    161        1.5      fvdl 	case PCI_PRODUCT_AMD_SC762_NB:
    162        1.1      fvdl 		return 1;
    163        1.1      fvdl 	}
    164        1.1      fvdl 
    165        1.1      fvdl 	return 0;
    166        1.1      fvdl }
    167        1.1      fvdl 
    168        1.1      fvdl int
    169       1.20     freza agp_amd_attach(device_t parent, device_t self, void *aux)
    170        1.1      fvdl {
    171       1.20     freza 	struct agp_softc *sc = device_private(self);
    172        1.1      fvdl 	struct agp_amd_softc *asc;
    173        1.1      fvdl 	struct pci_attach_args *pa = aux;
    174        1.1      fvdl 	struct agp_amd_gatt *gatt;
    175        1.1      fvdl 	pcireg_t reg;
    176        1.1      fvdl 	int error;
    177        1.1      fvdl 
    178        1.8   tsutsui 	asc = malloc(sizeof *asc, M_AGP, M_NOWAIT|M_ZERO);
    179        1.1      fvdl 	if (asc == NULL) {
    180        1.9   thorpej 		aprint_error(": can't allocate softc\n");
    181        1.1      fvdl 		/* agp_generic_detach(sc) */
    182        1.1      fvdl 		return ENOMEM;
    183        1.1      fvdl 	}
    184        1.1      fvdl 
    185        1.3   thorpej 	error = pci_mapreg_map(pa, AGP_AMD751_REGISTERS, PCI_MAPREG_TYPE_MEM, 0,
    186        1.1      fvdl 	    &asc->iot, &asc->ioh, NULL, NULL);
    187        1.1      fvdl 	if (error != 0) {
    188        1.9   thorpej 		aprint_error(": can't map AGP registers\n");
    189        1.1      fvdl 		agp_generic_detach(sc);
    190       1.15    rpaulo 		free(asc, M_AGP);
    191        1.1      fvdl 		return error;
    192        1.1      fvdl 	}
    193        1.1      fvdl 
    194       1.14  christos 	if (agp_map_aperture(pa, sc, AGP_APBASE) != 0) {
    195        1.9   thorpej 		aprint_error(": can't map aperture\n");
    196        1.1      fvdl 		agp_generic_detach(sc);
    197        1.1      fvdl 		free(asc, M_AGP);
    198        1.1      fvdl 		return ENXIO;
    199        1.1      fvdl 	}
    200        1.1      fvdl 	pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_AGP, &sc->as_capoff,
    201        1.1      fvdl 	    NULL);
    202        1.1      fvdl 	sc->as_methods = &agp_amd_methods;
    203        1.1      fvdl 	sc->as_chipc = asc;
    204        1.1      fvdl 	asc->initial_aperture = AGP_GET_APERTURE(sc);
    205        1.1      fvdl 
    206        1.1      fvdl 	for (;;) {
    207        1.1      fvdl 		gatt = agp_amd_alloc_gatt(sc);
    208        1.1      fvdl 		if (gatt)
    209        1.1      fvdl 			break;
    210        1.1      fvdl 
    211        1.1      fvdl 		/*
    212        1.1      fvdl 		 * Probably contigmalloc failure. Try reducing the
    213        1.1      fvdl 		 * aperture so that the gatt size reduces.
    214        1.1      fvdl 		 */
    215        1.1      fvdl 		if (AGP_SET_APERTURE(sc, AGP_GET_APERTURE(sc) / 2)) {
    216        1.9   thorpej 			aprint_error(": can't set aperture\n");
    217        1.1      fvdl 			return ENOMEM;
    218        1.1      fvdl 		}
    219        1.1      fvdl 	}
    220        1.1      fvdl 	asc->gatt = gatt;
    221        1.1      fvdl 
    222        1.1      fvdl 	/* Install the gatt. */
    223        1.1      fvdl 	WRITE4(AGP_AMD751_ATTBASE, gatt->ag_physical);
    224       1.11     perry 
    225        1.1      fvdl 	/* Enable synchronisation between host and agp. */
    226        1.1      fvdl 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, AGP_AMD751_MODECTRL);
    227        1.1      fvdl 	reg &= ~0x00ff00ff;
    228        1.1      fvdl 	reg |= (AGP_AMD751_MODECTRL_SYNEN) | (AGP_AMD751_MODECTRL2_GPDCE << 16);
    229        1.1      fvdl 	pci_conf_write(pa->pa_pc, pa->pa_tag, AGP_AMD751_MODECTRL, reg);
    230        1.1      fvdl 	/* Enable the TLB and flush */
    231        1.1      fvdl 	WRITE2(AGP_AMD751_STATUS,
    232        1.1      fvdl 	       READ2(AGP_AMD751_STATUS) | AGP_AMD751_STATUS_GCE);
    233        1.1      fvdl 	AGP_FLUSH_TLB(sc);
    234        1.1      fvdl 
    235        1.1      fvdl 	return 0;
    236        1.1      fvdl }
    237        1.1      fvdl 
    238        1.1      fvdl #if 0
    239        1.1      fvdl static int
    240        1.1      fvdl agp_amd_detach(struct agp_softc *sc)
    241        1.1      fvdl {
    242        1.1      fvdl 	pcireg_t reg;
    243        1.1      fvdl 	struct agp_amd_softc *asc = sc->as_chipc;
    244        1.1      fvdl 
    245        1.1      fvdl 	/* Disable the TLB.. */
    246        1.1      fvdl 	WRITE2(AGP_AMD751_STATUS,
    247        1.1      fvdl 	       READ2(AGP_AMD751_STATUS) & ~AGP_AMD751_STATUS_GCE);
    248       1.11     perry 
    249        1.1      fvdl 	/* Disable host-agp sync */
    250        1.1      fvdl 	reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_AMD751_MODECTRL);
    251        1.1      fvdl 	reg &= 0xffffff00;
    252        1.1      fvdl 	pci_conf_write(sc->as_pc, sc->as_tag, AGP_AMD751_MODECTRL, reg);
    253       1.11     perry 
    254        1.1      fvdl 	/* Clear the GATT base */
    255        1.1      fvdl 	WRITE4(AGP_AMD751_ATTBASE, 0);
    256        1.1      fvdl 
    257        1.1      fvdl 	/* Put the aperture back the way it started. */
    258        1.1      fvdl 	AGP_SET_APERTURE(sc, asc->initial_aperture);
    259        1.1      fvdl 
    260        1.1      fvdl 	agp_amd_free_gatt(sc, asc->gatt);
    261        1.1      fvdl 
    262        1.1      fvdl 	/* XXXfvdl no pci_mapreg_unmap */
    263        1.1      fvdl 
    264        1.1      fvdl 	return 0;
    265        1.1      fvdl }
    266        1.1      fvdl #endif
    267        1.1      fvdl 
    268        1.1      fvdl static u_int32_t
    269        1.1      fvdl agp_amd_get_aperture(struct agp_softc *sc)
    270        1.1      fvdl {
    271        1.1      fvdl 	int vas;
    272        1.1      fvdl 
    273        1.1      fvdl 	vas = (pci_conf_read(sc->as_pc, sc->as_tag, AGP_AMD751_APCTRL) & 0x06);
    274        1.1      fvdl 	vas >>= 1;
    275        1.1      fvdl 	/*
    276        1.1      fvdl 	 * The aperture size is equal to 32M<<vas.
    277        1.1      fvdl 	 */
    278        1.1      fvdl 	return (32*1024*1024) << vas;
    279        1.1      fvdl }
    280        1.1      fvdl 
    281        1.1      fvdl static int
    282        1.1      fvdl agp_amd_set_aperture(struct agp_softc *sc, u_int32_t aperture)
    283        1.1      fvdl {
    284        1.1      fvdl 	int vas;
    285        1.1      fvdl 	pcireg_t reg;
    286        1.1      fvdl 
    287        1.1      fvdl 	/*
    288        1.1      fvdl 	 * Check for a power of two and make sure its within the
    289        1.1      fvdl 	 * programmable range.
    290        1.1      fvdl 	 */
    291        1.1      fvdl 	if (aperture & (aperture - 1)
    292        1.1      fvdl 	    || aperture < 32*1024*1024
    293        1.1      fvdl 	    || aperture > 2U*1024*1024*1024)
    294        1.1      fvdl 		return EINVAL;
    295        1.1      fvdl 
    296        1.1      fvdl 	vas = ffs(aperture / 32*1024*1024) - 1;
    297        1.1      fvdl 
    298        1.1      fvdl 	reg = pci_conf_read(sc->as_pc, sc->as_tag, AGP_AMD751_APCTRL);
    299        1.1      fvdl 	reg = (reg & ~0x06) | (vas << 1);
    300        1.1      fvdl 	pci_conf_write(sc->as_pc, sc->as_tag, AGP_AMD751_APCTRL, reg);
    301       1.11     perry 
    302        1.1      fvdl 	return 0;
    303        1.1      fvdl }
    304        1.1      fvdl 
    305        1.1      fvdl static int
    306        1.1      fvdl agp_amd_bind_page(struct agp_softc *sc, off_t offset, bus_addr_t physical)
    307        1.1      fvdl {
    308        1.1      fvdl 	struct agp_amd_softc *asc = sc->as_chipc;
    309        1.1      fvdl 
    310        1.1      fvdl 	if (offset < 0 || offset >= (asc->gatt->ag_entries << AGP_PAGE_SHIFT))
    311        1.1      fvdl 		return EINVAL;
    312        1.1      fvdl 
    313        1.1      fvdl 	asc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = physical | 1;
    314        1.1      fvdl 	return 0;
    315        1.1      fvdl }
    316        1.1      fvdl 
    317        1.1      fvdl static int
    318        1.1      fvdl agp_amd_unbind_page(struct agp_softc *sc, off_t offset)
    319        1.1      fvdl {
    320        1.1      fvdl 	struct agp_amd_softc *asc = sc->as_chipc;
    321        1.1      fvdl 
    322        1.1      fvdl 	if (offset < 0 || offset >= (asc->gatt->ag_entries << AGP_PAGE_SHIFT))
    323        1.1      fvdl 		return EINVAL;
    324        1.1      fvdl 
    325        1.1      fvdl 	asc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0;
    326        1.1      fvdl 	return 0;
    327        1.1      fvdl }
    328        1.1      fvdl 
    329        1.1      fvdl static void
    330        1.1      fvdl agp_amd_flush_tlb(struct agp_softc *sc)
    331        1.1      fvdl {
    332        1.1      fvdl 	struct agp_amd_softc *asc = sc->as_chipc;
    333        1.1      fvdl 
    334        1.1      fvdl 	/* Set the cache invalidate bit and wait for the chipset to clear */
    335        1.1      fvdl 	WRITE4(AGP_AMD751_TLBCTRL, 1);
    336        1.1      fvdl 	do {
    337        1.1      fvdl 		DELAY(1);
    338        1.1      fvdl 	} while (READ4(AGP_AMD751_TLBCTRL));
    339        1.1      fvdl }
    340