Home | History | Annotate | Line # | Download | only in pci
agp_amd64.c revision 1.8
      1  1.1  kiyohara /*-
      2  1.1  kiyohara  * Copyright (c) 2004, 2005 Jung-uk Kim <jkim (at) FreeBSD.org>
      3  1.1  kiyohara  * All rights reserved.
      4  1.1  kiyohara  *
      5  1.1  kiyohara  * Redistribution and use in source and binary forms, with or without
      6  1.1  kiyohara  * modification, are permitted provided that the following conditions
      7  1.1  kiyohara  * are met:
      8  1.1  kiyohara  * 1. Redistributions of source code must retain the above copyright
      9  1.1  kiyohara  *    notice, this list of conditions and the following disclaimer.
     10  1.1  kiyohara  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1  kiyohara  *    notice, this list of conditions and the following disclaimer in the
     12  1.1  kiyohara  *    documentation and/or other materials provided with the distribution.
     13  1.1  kiyohara  *
     14  1.1  kiyohara  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     15  1.1  kiyohara  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     16  1.1  kiyohara  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     17  1.1  kiyohara  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     18  1.1  kiyohara  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     19  1.1  kiyohara  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     20  1.1  kiyohara  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     21  1.1  kiyohara  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     22  1.1  kiyohara  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     23  1.1  kiyohara  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     24  1.1  kiyohara  * SUCH DAMAGE.
     25  1.1  kiyohara  */
     26  1.1  kiyohara 
     27  1.1  kiyohara #include <sys/cdefs.h>
     28  1.8  riastrad __KERNEL_RCSID(0, "$NetBSD: agp_amd64.c,v 1.8 2015/04/04 15:08:40 riastradh Exp $");
     29  1.1  kiyohara 
     30  1.1  kiyohara #include <sys/param.h>
     31  1.1  kiyohara #include <sys/systm.h>
     32  1.1  kiyohara #include <sys/malloc.h>
     33  1.1  kiyohara #include <sys/kernel.h>
     34  1.1  kiyohara #include <sys/proc.h>
     35  1.1  kiyohara #include <sys/conf.h>
     36  1.1  kiyohara #include <sys/device.h>
     37  1.1  kiyohara #include <sys/agpio.h>
     38  1.1  kiyohara 
     39  1.1  kiyohara #include <dev/pci/pcivar.h>
     40  1.1  kiyohara #include <dev/pci/pcireg.h>
     41  1.1  kiyohara #include <dev/pci/agpvar.h>
     42  1.1  kiyohara #include <dev/pci/agpreg.h>
     43  1.1  kiyohara 
     44  1.1  kiyohara #include <dev/pci/pcidevs.h>
     45  1.1  kiyohara 
     46  1.2        ad #include <sys/bus.h>
     47  1.1  kiyohara 
     48  1.1  kiyohara 
     49  1.1  kiyohara #define	AMD64_MAX_MCTRL		8
     50  1.1  kiyohara 
     51  1.1  kiyohara /* XXX nForce3 requires secondary AGP bridge at 0:11:0. */
     52  1.1  kiyohara #define AGP_AMD64_NVIDIA_PCITAG(pc)	pci_make_tag(pc, 0, 11, 0)
     53  1.1  kiyohara /* XXX Some VIA bridge requires secondary AGP bridge at 0:1:0. */
     54  1.1  kiyohara #define AGP_AMD64_VIA_PCITAG(pc)	pci_make_tag(pc, 0, 1, 0)
     55  1.1  kiyohara 
     56  1.1  kiyohara 
     57  1.1  kiyohara static uint32_t agp_amd64_get_aperture(struct agp_softc *);
     58  1.1  kiyohara static int agp_amd64_set_aperture(struct agp_softc *, uint32_t);
     59  1.1  kiyohara static int agp_amd64_bind_page(struct agp_softc *, off_t, bus_addr_t);
     60  1.1  kiyohara static int agp_amd64_unbind_page(struct agp_softc *, off_t);
     61  1.1  kiyohara static void agp_amd64_flush_tlb(struct agp_softc *);
     62  1.1  kiyohara 
     63  1.1  kiyohara static void agp_amd64_apbase_fixup(struct agp_softc *);
     64  1.1  kiyohara 
     65  1.1  kiyohara static void agp_amd64_uli_init(struct agp_softc *);
     66  1.1  kiyohara static int agp_amd64_uli_set_aperture(struct agp_softc *, uint32_t);
     67  1.1  kiyohara 
     68  1.1  kiyohara static int agp_amd64_nvidia_match(const struct pci_attach_args *, uint16_t);
     69  1.1  kiyohara static void agp_amd64_nvidia_init(struct agp_softc *);
     70  1.1  kiyohara static int agp_amd64_nvidia_set_aperture(struct agp_softc *, uint32_t);
     71  1.1  kiyohara 
     72  1.1  kiyohara static int agp_amd64_via_match(const struct pci_attach_args *);
     73  1.1  kiyohara static void agp_amd64_via_init(struct agp_softc *);
     74  1.1  kiyohara static int agp_amd64_via_set_aperture(struct agp_softc *, uint32_t);
     75  1.1  kiyohara 
     76  1.1  kiyohara 
     77  1.1  kiyohara struct agp_amd64_softc {
     78  1.1  kiyohara 	uint32_t		initial_aperture;
     79  1.1  kiyohara 	struct agp_gatt		*gatt;
     80  1.1  kiyohara 	uint32_t		apbase;
     81  1.1  kiyohara 	pcitag_t		ctrl_tag;	/* use NVIDIA and VIA */
     82  1.1  kiyohara 	pcitag_t		mctrl_tag[AMD64_MAX_MCTRL];
     83  1.1  kiyohara 	int			n_mctrl;
     84  1.1  kiyohara 	int			via_agp;
     85  1.1  kiyohara };
     86  1.1  kiyohara 
     87  1.1  kiyohara static struct agp_methods agp_amd64_methods = {
     88  1.1  kiyohara 	agp_amd64_get_aperture,
     89  1.1  kiyohara 	agp_amd64_set_aperture,
     90  1.1  kiyohara 	agp_amd64_bind_page,
     91  1.1  kiyohara 	agp_amd64_unbind_page,
     92  1.1  kiyohara 	agp_amd64_flush_tlb,
     93  1.1  kiyohara 	agp_generic_enable,
     94  1.1  kiyohara 	agp_generic_alloc_memory,
     95  1.1  kiyohara 	agp_generic_free_memory,
     96  1.1  kiyohara 	agp_generic_bind_memory,
     97  1.1  kiyohara 	agp_generic_unbind_memory,
     98  1.1  kiyohara };
     99  1.1  kiyohara 
    100  1.1  kiyohara 
    101  1.1  kiyohara int
    102  1.1  kiyohara agp_amd64_match(const struct pci_attach_args *pa)
    103  1.1  kiyohara {
    104  1.1  kiyohara 
    105  1.1  kiyohara 	switch (PCI_VENDOR(pa->pa_id)) {
    106  1.1  kiyohara 	case PCI_VENDOR_AMD:
    107  1.1  kiyohara 		switch (PCI_PRODUCT(pa->pa_id)) {
    108  1.1  kiyohara 		case PCI_PRODUCT_AMD_AGP8151_DEV:
    109  1.1  kiyohara 			return 1;
    110  1.1  kiyohara 		}
    111  1.1  kiyohara 		break;
    112  1.1  kiyohara 
    113  1.1  kiyohara 	case PCI_VENDOR_SIS:
    114  1.1  kiyohara 		switch (PCI_PRODUCT(pa->pa_id)) {
    115  1.1  kiyohara 		case PCI_PRODUCT_SIS_755:
    116  1.1  kiyohara 		case PCI_PRODUCT_SIS_760:
    117  1.1  kiyohara 			return 1;
    118  1.1  kiyohara 		}
    119  1.1  kiyohara 		break;
    120  1.1  kiyohara 
    121  1.1  kiyohara 	case PCI_VENDOR_ALI:
    122  1.1  kiyohara 		switch (PCI_PRODUCT(pa->pa_id)) {
    123  1.1  kiyohara 		case PCI_PRODUCT_ALI_M1689:
    124  1.1  kiyohara 			return 1;
    125  1.1  kiyohara 		}
    126  1.1  kiyohara 		break;
    127  1.1  kiyohara 
    128  1.1  kiyohara 	case PCI_VENDOR_NVIDIA:
    129  1.1  kiyohara 		switch (PCI_PRODUCT(pa->pa_id)) {
    130  1.1  kiyohara 		case PCI_PRODUCT_NVIDIA_NFORCE3_PCHB:
    131  1.1  kiyohara 			return agp_amd64_nvidia_match(pa,
    132  1.1  kiyohara 			    PCI_PRODUCT_NVIDIA_NFORCE3_PPB2);
    133  1.1  kiyohara 
    134  1.1  kiyohara 			/* NOTREACHED */
    135  1.1  kiyohara 
    136  1.1  kiyohara 		case PCI_PRODUCT_NVIDIA_NFORCE3_250_PCHB:
    137  1.1  kiyohara 			return agp_amd64_nvidia_match(pa,
    138  1.1  kiyohara 			    PCI_PRODUCT_NVIDIA_NFORCE3_250_AGP);
    139  1.1  kiyohara 
    140  1.1  kiyohara 			/* NOTREACHED */
    141  1.1  kiyohara 		}
    142  1.1  kiyohara 		break;
    143  1.1  kiyohara 
    144  1.1  kiyohara 	case PCI_VENDOR_VIATECH:
    145  1.1  kiyohara 		switch (PCI_PRODUCT(pa->pa_id)) {
    146  1.1  kiyohara 		case PCI_PRODUCT_VIATECH_K8M800_0:
    147  1.1  kiyohara 		case PCI_PRODUCT_VIATECH_K8T890_0:
    148  1.1  kiyohara 		case PCI_PRODUCT_VIATECH_K8HTB_0:
    149  1.1  kiyohara 		case PCI_PRODUCT_VIATECH_K8HTB:
    150  1.1  kiyohara 			return 1;
    151  1.1  kiyohara 		}
    152  1.1  kiyohara 		break;
    153  1.1  kiyohara 	}
    154  1.1  kiyohara 
    155  1.1  kiyohara 	return 0;
    156  1.1  kiyohara }
    157  1.1  kiyohara 
    158  1.1  kiyohara static int
    159  1.1  kiyohara agp_amd64_nvidia_match(const struct pci_attach_args *pa, uint16_t devid)
    160  1.1  kiyohara {
    161  1.1  kiyohara 	pcitag_t tag;
    162  1.1  kiyohara 	pcireg_t reg;
    163  1.1  kiyohara 
    164  1.1  kiyohara 	tag = AGP_AMD64_NVIDIA_PCITAG(pa->pa_pc);
    165  1.1  kiyohara 
    166  1.1  kiyohara 	reg = pci_conf_read(pa->pa_pc, tag, PCI_CLASS_REG);
    167  1.1  kiyohara 	if (PCI_CLASS(reg) != PCI_CLASS_BRIDGE ||
    168  1.1  kiyohara 	    PCI_SUBCLASS(reg) != PCI_SUBCLASS_BRIDGE_PCI)
    169  1.1  kiyohara 		return 0;
    170  1.1  kiyohara 
    171  1.1  kiyohara 	reg = pci_conf_read(pa->pa_pc, tag, PCI_ID_REG);
    172  1.1  kiyohara 	if (PCI_VENDOR(reg) != PCI_VENDOR_NVIDIA || PCI_PRODUCT(reg) != devid)
    173  1.1  kiyohara 		return 0;
    174  1.1  kiyohara 
    175  1.1  kiyohara 	return 1;
    176  1.1  kiyohara }
    177  1.1  kiyohara 
    178  1.1  kiyohara static int
    179  1.1  kiyohara agp_amd64_via_match(const struct pci_attach_args *pa)
    180  1.1  kiyohara {
    181  1.1  kiyohara 	pcitag_t tag;
    182  1.1  kiyohara 	pcireg_t reg;
    183  1.1  kiyohara 
    184  1.1  kiyohara 	tag = AGP_AMD64_VIA_PCITAG(pa->pa_pc);
    185  1.1  kiyohara 
    186  1.1  kiyohara 	reg = pci_conf_read(pa->pa_pc, tag, PCI_CLASS_REG);
    187  1.1  kiyohara 	if (PCI_CLASS(reg) != PCI_CLASS_BRIDGE ||
    188  1.1  kiyohara 	    PCI_SUBCLASS(reg) != PCI_SUBCLASS_BRIDGE_PCI)
    189  1.1  kiyohara 		return 0;
    190  1.1  kiyohara 
    191  1.1  kiyohara 	reg = pci_conf_read(pa->pa_pc, tag, PCI_ID_REG);
    192  1.1  kiyohara 	if (PCI_VENDOR(reg) != PCI_VENDOR_VIATECH ||
    193  1.1  kiyohara 	    PCI_PRODUCT(reg) != PCI_PRODUCT_VIATECH_K8HTB_AGP)
    194  1.1  kiyohara 		return 0;
    195  1.1  kiyohara 
    196  1.1  kiyohara 	return 1;
    197  1.1  kiyohara }
    198  1.1  kiyohara 
    199  1.1  kiyohara int
    200  1.4  kiyohara agp_amd64_attach(device_t parent, device_t self, void *aux)
    201  1.1  kiyohara {
    202  1.4  kiyohara 	struct agp_softc *sc = device_private(self);
    203  1.1  kiyohara 	struct agp_amd64_softc *asc;
    204  1.1  kiyohara 	struct pci_attach_args *pa = aux;
    205  1.1  kiyohara 	struct agp_gatt *gatt;
    206  1.1  kiyohara 	pcitag_t tag;
    207  1.1  kiyohara 	pcireg_t id, attbase, apctrl;
    208  1.1  kiyohara 	int maxdevs, i, n;
    209  1.8  riastrad 	int error;
    210  1.1  kiyohara 
    211  1.1  kiyohara 	asc = malloc(sizeof(struct agp_amd64_softc), M_AGP, M_NOWAIT | M_ZERO);
    212  1.1  kiyohara 	if (asc == NULL) {
    213  1.1  kiyohara 		aprint_error(": can't allocate softc\n");
    214  1.8  riastrad 		error = ENOMEM;
    215  1.8  riastrad 		goto fail0;
    216  1.1  kiyohara 	}
    217  1.1  kiyohara 
    218  1.1  kiyohara 	if (agp_map_aperture(pa, sc, AGP_APBASE) != 0) {
    219  1.1  kiyohara 		aprint_error(": can't map aperture\n");
    220  1.8  riastrad 		error = ENXIO;
    221  1.8  riastrad 		goto fail1;
    222  1.1  kiyohara 	}
    223  1.1  kiyohara 
    224  1.1  kiyohara 	maxdevs = pci_bus_maxdevs(pa->pa_pc, 0);
    225  1.1  kiyohara 	for (i = 0, n = 0; i < maxdevs && n < AMD64_MAX_MCTRL; i++) {
    226  1.1  kiyohara 		tag = pci_make_tag(pa->pa_pc, 0, i, 3);
    227  1.1  kiyohara 		id = pci_conf_read(pa->pa_pc, tag, PCI_ID_REG);
    228  1.1  kiyohara 		if (PCI_VENDOR(id) == PCI_VENDOR_AMD &&
    229  1.7   tsutsui 		    (PCI_PRODUCT(id) == PCI_PRODUCT_AMD_AMD64_MISC ||
    230  1.7   tsutsui 		     PCI_PRODUCT(id) == PCI_PRODUCT_AMD_AMD64_F10_MISC)) {
    231  1.1  kiyohara 			asc->mctrl_tag[n] = tag;
    232  1.1  kiyohara 			n++;
    233  1.1  kiyohara 		}
    234  1.1  kiyohara 	}
    235  1.7   tsutsui 	if (n == 0) {
    236  1.7   tsutsui 		aprint_error(": No Miscellaneous Control unit found.\n");
    237  1.8  riastrad 		error = ENXIO;
    238  1.8  riastrad 		goto fail1;
    239  1.7   tsutsui 	}
    240  1.1  kiyohara 	asc->n_mctrl = n;
    241  1.1  kiyohara 
    242  1.5  kiyohara 	aprint_normal(": %d Miscellaneous Control unit(s) found.\n",
    243  1.1  kiyohara 	    asc->n_mctrl);
    244  1.5  kiyohara 	aprint_normal("%s", device_xname(self));
    245  1.1  kiyohara 
    246  1.1  kiyohara 	sc->as_chipc = asc;
    247  1.1  kiyohara 	sc->as_methods = &agp_amd64_methods;
    248  1.1  kiyohara 	pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_AGP, &sc->as_capoff,
    249  1.1  kiyohara 	    NULL);
    250  1.1  kiyohara 	asc->initial_aperture = AGP_GET_APERTURE(sc);
    251  1.1  kiyohara 
    252  1.1  kiyohara 	for (;;) {
    253  1.1  kiyohara 		gatt = agp_alloc_gatt(sc);
    254  1.1  kiyohara 		if (gatt)
    255  1.1  kiyohara 			break;
    256  1.1  kiyohara 
    257  1.1  kiyohara 		/*
    258  1.1  kiyohara 		 * Probably contigmalloc failure. Try reducing the
    259  1.1  kiyohara 		 * aperture so that the gatt size reduces.
    260  1.1  kiyohara 		 */
    261  1.1  kiyohara 		if (AGP_SET_APERTURE(sc, AGP_GET_APERTURE(sc) / 2)) {
    262  1.8  riastrad 			error = ENOMEM;
    263  1.8  riastrad 			goto fail1;
    264  1.1  kiyohara 		}
    265  1.1  kiyohara 	}
    266  1.1  kiyohara 	asc->gatt = gatt;
    267  1.1  kiyohara 
    268  1.1  kiyohara 	switch (PCI_VENDOR(sc->as_id)) {
    269  1.1  kiyohara 	case PCI_VENDOR_ALI:
    270  1.1  kiyohara 		agp_amd64_uli_init(sc);
    271  1.8  riastrad 		if (agp_amd64_uli_set_aperture(sc, asc->initial_aperture)) {
    272  1.8  riastrad 			/* XXX Back out agp_amd64_uli_init?  */
    273  1.8  riastrad 			error = ENXIO;
    274  1.8  riastrad 			goto fail2;
    275  1.8  riastrad 		}
    276  1.1  kiyohara 		break;
    277  1.1  kiyohara 
    278  1.1  kiyohara 	case PCI_VENDOR_NVIDIA:
    279  1.1  kiyohara 		asc->ctrl_tag = AGP_AMD64_NVIDIA_PCITAG(pa->pa_pc);
    280  1.1  kiyohara 		agp_amd64_nvidia_init(sc);
    281  1.8  riastrad 		if (agp_amd64_nvidia_set_aperture(sc, asc->initial_aperture)) {
    282  1.8  riastrad 			/* XXX Back out agp_amd64_nvidia_init?  */
    283  1.8  riastrad 			error = ENXIO;
    284  1.8  riastrad 			goto fail2;
    285  1.8  riastrad 		}
    286  1.1  kiyohara 		break;
    287  1.1  kiyohara 
    288  1.1  kiyohara 	case PCI_VENDOR_VIATECH:
    289  1.1  kiyohara 		asc->via_agp = agp_amd64_via_match(pa);
    290  1.1  kiyohara 		if (asc->via_agp) {
    291  1.1  kiyohara 			asc->ctrl_tag = AGP_AMD64_VIA_PCITAG(pa->pa_pc);
    292  1.1  kiyohara 			agp_amd64_via_init(sc);
    293  1.1  kiyohara 			if (agp_amd64_via_set_aperture(sc,
    294  1.8  riastrad 			    asc->initial_aperture)) {
    295  1.8  riastrad 				/* XXX Back out agp_amd64_via_init?  */
    296  1.8  riastrad 				error = ENXIO;
    297  1.8  riastrad 				goto fail2;
    298  1.8  riastrad 			}
    299  1.1  kiyohara 		}
    300  1.1  kiyohara 		break;
    301  1.1  kiyohara 	}
    302  1.1  kiyohara 
    303  1.1  kiyohara 	/* Install the gatt and enable aperture. */
    304  1.1  kiyohara 	attbase = (uint32_t)(gatt->ag_physical >> 8) & AGP_AMD64_ATTBASE_MASK;
    305  1.1  kiyohara 	for (i = 0; i < asc->n_mctrl; i++) {
    306  1.1  kiyohara 		pci_conf_write(pa->pa_pc, asc->mctrl_tag[i], AGP_AMD64_ATTBASE,
    307  1.1  kiyohara 		    attbase);
    308  1.1  kiyohara 		apctrl = pci_conf_read(pa->pa_pc, asc->mctrl_tag[i],
    309  1.1  kiyohara 		    AGP_AMD64_APCTRL);
    310  1.1  kiyohara 		apctrl |= AGP_AMD64_APCTRL_GARTEN;
    311  1.1  kiyohara 		apctrl &=
    312  1.1  kiyohara 		    ~(AGP_AMD64_APCTRL_DISGARTCPU | AGP_AMD64_APCTRL_DISGARTIO);
    313  1.1  kiyohara 		pci_conf_write(pa->pa_pc, asc->mctrl_tag[i], AGP_AMD64_APCTRL,
    314  1.1  kiyohara 		    apctrl);
    315  1.1  kiyohara 	}
    316  1.1  kiyohara 
    317  1.1  kiyohara 	agp_flush_cache();
    318  1.1  kiyohara 
    319  1.8  riastrad 	/* Success!  */
    320  1.1  kiyohara 	return 0;
    321  1.8  riastrad 
    322  1.8  riastrad fail2:	agp_free_gatt(sc, gatt);
    323  1.8  riastrad fail1:	free(asc, M_AGP);
    324  1.8  riastrad fail0:	agp_generic_detach(sc);
    325  1.8  riastrad 	KASSERT(error);
    326  1.8  riastrad 	return error;
    327  1.1  kiyohara }
    328  1.1  kiyohara 
    329  1.1  kiyohara 
    330  1.1  kiyohara static uint32_t agp_amd64_table[] = {
    331  1.1  kiyohara 	0x02000000,	/*   32 MB */
    332  1.1  kiyohara 	0x04000000,	/*   64 MB */
    333  1.1  kiyohara 	0x08000000,	/*  128 MB */
    334  1.1  kiyohara 	0x10000000,	/*  256 MB */
    335  1.1  kiyohara 	0x20000000,	/*  512 MB */
    336  1.1  kiyohara 	0x40000000,	/* 1024 MB */
    337  1.1  kiyohara 	0x80000000,	/* 2048 MB */
    338  1.1  kiyohara };
    339  1.1  kiyohara 
    340  1.1  kiyohara #define AGP_AMD64_TABLE_SIZE \
    341  1.1  kiyohara 	(sizeof(agp_amd64_table) / sizeof(agp_amd64_table[0]))
    342  1.1  kiyohara 
    343  1.1  kiyohara static uint32_t
    344  1.1  kiyohara agp_amd64_get_aperture(struct agp_softc *sc)
    345  1.1  kiyohara {
    346  1.1  kiyohara 	struct agp_amd64_softc *asc = sc->as_chipc;
    347  1.1  kiyohara 	uint32_t i;
    348  1.1  kiyohara 
    349  1.1  kiyohara 	i = (pci_conf_read(sc->as_pc, asc->mctrl_tag[0], AGP_AMD64_APCTRL) &
    350  1.1  kiyohara 		AGP_AMD64_APCTRL_SIZE_MASK) >> 1;
    351  1.1  kiyohara 
    352  1.1  kiyohara 	if (i >= AGP_AMD64_TABLE_SIZE)
    353  1.1  kiyohara 		return 0;
    354  1.1  kiyohara 
    355  1.1  kiyohara 	return agp_amd64_table[i];
    356  1.1  kiyohara }
    357  1.1  kiyohara 
    358  1.1  kiyohara static int
    359  1.1  kiyohara agp_amd64_set_aperture(struct agp_softc *sc, uint32_t aperture)
    360  1.1  kiyohara {
    361  1.1  kiyohara 	struct agp_amd64_softc *asc = sc->as_chipc;
    362  1.1  kiyohara 	uint32_t i;
    363  1.1  kiyohara 	pcireg_t apctrl;
    364  1.1  kiyohara 	int j;
    365  1.1  kiyohara 
    366  1.1  kiyohara 	for (i = 0; i < AGP_AMD64_TABLE_SIZE; i++)
    367  1.1  kiyohara 		if (agp_amd64_table[i] == aperture)
    368  1.1  kiyohara 			break;
    369  1.1  kiyohara 	if (i >= AGP_AMD64_TABLE_SIZE)
    370  1.1  kiyohara 		return EINVAL;
    371  1.1  kiyohara 
    372  1.1  kiyohara 	for (j = 0; j < asc->n_mctrl; j++) {
    373  1.1  kiyohara 		apctrl = pci_conf_read(sc->as_pc, asc->mctrl_tag[0],
    374  1.1  kiyohara 		    AGP_AMD64_APCTRL);
    375  1.1  kiyohara 		pci_conf_write(sc->as_pc, asc->mctrl_tag[0], AGP_AMD64_APCTRL,
    376  1.1  kiyohara 		    (apctrl & ~(AGP_AMD64_APCTRL_SIZE_MASK)) | (i << 1));
    377  1.1  kiyohara 	}
    378  1.1  kiyohara 
    379  1.1  kiyohara 	switch (PCI_VENDOR(sc->as_id)) {
    380  1.1  kiyohara 	case PCI_VENDOR_ALI:
    381  1.1  kiyohara 		return agp_amd64_uli_set_aperture(sc, aperture);
    382  1.1  kiyohara 		break;
    383  1.1  kiyohara 
    384  1.1  kiyohara 	case PCI_VENDOR_NVIDIA:
    385  1.1  kiyohara 		return agp_amd64_nvidia_set_aperture(sc, aperture);
    386  1.1  kiyohara 		break;
    387  1.1  kiyohara 
    388  1.1  kiyohara 	case PCI_VENDOR_VIATECH:
    389  1.1  kiyohara 		if (asc->via_agp)
    390  1.1  kiyohara 			return agp_amd64_via_set_aperture(sc, aperture);
    391  1.1  kiyohara 		break;
    392  1.1  kiyohara 	}
    393  1.1  kiyohara 
    394  1.1  kiyohara 	return 0;
    395  1.1  kiyohara }
    396  1.1  kiyohara 
    397  1.1  kiyohara static int
    398  1.1  kiyohara agp_amd64_bind_page(struct agp_softc *sc, off_t offset, bus_addr_t physical)
    399  1.1  kiyohara {
    400  1.1  kiyohara 	struct agp_amd64_softc *asc = sc->as_chipc;
    401  1.1  kiyohara 
    402  1.1  kiyohara 	if (offset < 0 || offset >= (asc->gatt->ag_entries << AGP_PAGE_SHIFT))
    403  1.1  kiyohara 		return EINVAL;
    404  1.1  kiyohara 
    405  1.1  kiyohara 	asc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] =
    406  1.1  kiyohara 	    (physical & 0xfffff000) | ((physical >> 28) & 0x00000ff0) | 3;
    407  1.1  kiyohara 
    408  1.1  kiyohara 	return 0;
    409  1.1  kiyohara }
    410  1.1  kiyohara 
    411  1.1  kiyohara static int
    412  1.1  kiyohara agp_amd64_unbind_page(struct agp_softc *sc, off_t offset)
    413  1.1  kiyohara {
    414  1.1  kiyohara 	struct agp_amd64_softc *asc = sc->as_chipc;
    415  1.1  kiyohara 
    416  1.1  kiyohara 	if (offset < 0 || offset >= (asc->gatt->ag_entries << AGP_PAGE_SHIFT))
    417  1.1  kiyohara 		return EINVAL;
    418  1.1  kiyohara 
    419  1.1  kiyohara 	asc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0;
    420  1.1  kiyohara 
    421  1.1  kiyohara 	return 0;
    422  1.1  kiyohara }
    423  1.1  kiyohara 
    424  1.1  kiyohara static void
    425  1.1  kiyohara agp_amd64_flush_tlb(struct agp_softc *sc)
    426  1.1  kiyohara {
    427  1.1  kiyohara 	struct agp_amd64_softc *asc = sc->as_chipc;
    428  1.1  kiyohara 	pcireg_t cachectrl;
    429  1.1  kiyohara 	int i;
    430  1.1  kiyohara 
    431  1.1  kiyohara 	for (i = 0; i < asc->n_mctrl; i++) {
    432  1.1  kiyohara 		cachectrl = pci_conf_read(sc->as_pc, asc->mctrl_tag[i],
    433  1.1  kiyohara 		    AGP_AMD64_CACHECTRL);
    434  1.1  kiyohara 		pci_conf_write(sc->as_pc, asc->mctrl_tag[i],
    435  1.1  kiyohara 		    AGP_AMD64_CACHECTRL,
    436  1.1  kiyohara 		    cachectrl | AGP_AMD64_CACHECTRL_INVGART);
    437  1.1  kiyohara 	}
    438  1.1  kiyohara }
    439  1.1  kiyohara 
    440  1.1  kiyohara static void
    441  1.1  kiyohara agp_amd64_apbase_fixup(struct agp_softc *sc)
    442  1.1  kiyohara {
    443  1.1  kiyohara 	struct agp_amd64_softc *asc = sc->as_chipc;
    444  1.1  kiyohara 	uint32_t apbase;
    445  1.1  kiyohara 	int i;
    446  1.1  kiyohara 
    447  1.1  kiyohara 	apbase = pci_conf_read(sc->as_pc, sc->as_tag, AGP_APBASE);
    448  1.1  kiyohara 	asc->apbase = PCI_MAPREG_MEM_ADDR(apbase);
    449  1.1  kiyohara 	apbase = (asc->apbase >> 25) & AGP_AMD64_APBASE_MASK;
    450  1.1  kiyohara 	for (i = 0; i < asc->n_mctrl; i++)
    451  1.1  kiyohara 		pci_conf_write(sc->as_pc, asc->mctrl_tag[i], AGP_AMD64_APBASE,
    452  1.1  kiyohara 		    apbase);
    453  1.1  kiyohara }
    454  1.1  kiyohara 
    455  1.1  kiyohara static void
    456  1.1  kiyohara agp_amd64_uli_init(struct agp_softc *sc)
    457  1.1  kiyohara {
    458  1.1  kiyohara 	struct agp_amd64_softc *asc = sc->as_chipc;
    459  1.1  kiyohara 	pcireg_t apbase;
    460  1.1  kiyohara 
    461  1.1  kiyohara 	agp_amd64_apbase_fixup(sc);
    462  1.1  kiyohara 	apbase = pci_conf_read(sc->as_pc, sc->as_tag, AGP_AMD64_ULI_APBASE);
    463  1.1  kiyohara 	pci_conf_write(sc->as_pc, sc->as_tag, AGP_AMD64_ULI_APBASE,
    464  1.1  kiyohara 	    (apbase & 0x0000000f) | asc->apbase);
    465  1.1  kiyohara 	pci_conf_write(sc->as_pc, sc->as_tag, AGP_AMD64_ULI_HTT_FEATURE,
    466  1.1  kiyohara 	    asc->apbase);
    467  1.1  kiyohara }
    468  1.1  kiyohara 
    469  1.1  kiyohara static int
    470  1.1  kiyohara agp_amd64_uli_set_aperture(struct agp_softc *sc, uint32_t aperture)
    471  1.1  kiyohara {
    472  1.1  kiyohara 	struct agp_amd64_softc *asc = sc->as_chipc;
    473  1.1  kiyohara 
    474  1.1  kiyohara 	switch (aperture) {
    475  1.1  kiyohara 	case 0x02000000:	/*  32 MB */
    476  1.1  kiyohara 	case 0x04000000:	/*  64 MB */
    477  1.1  kiyohara 	case 0x08000000:	/* 128 MB */
    478  1.1  kiyohara 	case 0x10000000:	/* 256 MB */
    479  1.1  kiyohara 		break;
    480  1.1  kiyohara 	default:
    481  1.1  kiyohara 		return EINVAL;
    482  1.1  kiyohara 	}
    483  1.1  kiyohara 
    484  1.1  kiyohara 	pci_conf_write(sc->as_pc, sc->as_tag, AGP_AMD64_ULI_ENU_SCR,
    485  1.1  kiyohara 	    asc->apbase + aperture - 1);
    486  1.1  kiyohara 
    487  1.1  kiyohara 	return 0;
    488  1.1  kiyohara }
    489  1.1  kiyohara 
    490  1.1  kiyohara static void
    491  1.1  kiyohara agp_amd64_nvidia_init(struct agp_softc *sc)
    492  1.1  kiyohara {
    493  1.1  kiyohara 	struct agp_amd64_softc *asc = sc->as_chipc;
    494  1.1  kiyohara 	pcireg_t apbase;
    495  1.1  kiyohara 
    496  1.1  kiyohara 	agp_amd64_apbase_fixup(sc);
    497  1.1  kiyohara 	apbase =
    498  1.1  kiyohara 	    pci_conf_read(sc->as_pc, sc->as_tag, AGP_AMD64_NVIDIA_0_APBASE);
    499  1.1  kiyohara 	pci_conf_write(sc->as_pc, sc->as_tag, AGP_AMD64_NVIDIA_0_APBASE,
    500  1.1  kiyohara 	    (apbase & 0x0000000f) | asc->apbase);
    501  1.1  kiyohara 	pci_conf_write(sc->as_pc, asc->ctrl_tag, AGP_AMD64_NVIDIA_1_APBASE1,
    502  1.1  kiyohara 	    asc->apbase);
    503  1.1  kiyohara 	pci_conf_write(sc->as_pc, asc->ctrl_tag, AGP_AMD64_NVIDIA_1_APBASE2,
    504  1.1  kiyohara 	    asc->apbase);
    505  1.1  kiyohara }
    506  1.1  kiyohara 
    507  1.1  kiyohara static int
    508  1.1  kiyohara agp_amd64_nvidia_set_aperture(struct agp_softc *sc, uint32_t aperture)
    509  1.1  kiyohara {
    510  1.1  kiyohara 	struct agp_amd64_softc *asc = sc->as_chipc;
    511  1.1  kiyohara 	uint32_t apsize;
    512  1.1  kiyohara 
    513  1.1  kiyohara 	switch (aperture) {
    514  1.1  kiyohara 	case 0x02000000:	apsize = 0x0f;	break;	/*  32 MB */
    515  1.1  kiyohara 	case 0x04000000:	apsize = 0x0e;	break;	/*  64 MB */
    516  1.1  kiyohara 	case 0x08000000:	apsize = 0x0c;	break;	/* 128 MB */
    517  1.1  kiyohara 	case 0x10000000:	apsize = 0x08;	break;	/* 256 MB */
    518  1.1  kiyohara 	case 0x20000000:	apsize = 0x00;	break;	/* 512 MB */
    519  1.1  kiyohara 	default:
    520  1.1  kiyohara 		return EINVAL;
    521  1.1  kiyohara 	}
    522  1.1  kiyohara 
    523  1.1  kiyohara 	pci_conf_write(sc->as_pc, asc->ctrl_tag, AGP_AMD64_NVIDIA_1_APSIZE,
    524  1.1  kiyohara 	    (pci_conf_read(sc->as_pc, asc->ctrl_tag,
    525  1.1  kiyohara 	    AGP_AMD64_NVIDIA_1_APSIZE) & 0xfffffff0) | apsize);
    526  1.1  kiyohara 	pci_conf_write(sc->as_pc, asc->ctrl_tag, AGP_AMD64_NVIDIA_1_APLIMIT1,
    527  1.1  kiyohara 	    asc->apbase + aperture - 1);
    528  1.1  kiyohara 	pci_conf_write(sc->as_pc, asc->ctrl_tag, AGP_AMD64_NVIDIA_1_APLIMIT2,
    529  1.1  kiyohara 	    asc->apbase + aperture - 1);
    530  1.1  kiyohara 
    531  1.1  kiyohara 	return 0;
    532  1.1  kiyohara }
    533  1.1  kiyohara 
    534  1.1  kiyohara static void
    535  1.1  kiyohara agp_amd64_via_init(struct agp_softc *sc)
    536  1.1  kiyohara {
    537  1.1  kiyohara 	struct agp_amd64_softc *asc = sc->as_chipc;
    538  1.1  kiyohara 
    539  1.1  kiyohara 	agp_amd64_apbase_fixup(sc);
    540  1.1  kiyohara 	pci_conf_write(sc->as_pc, asc->ctrl_tag, AGP3_VIA_ATTBASE,
    541  1.1  kiyohara 	    asc->gatt->ag_physical);
    542  1.1  kiyohara 	pci_conf_write(sc->as_pc, asc->ctrl_tag, AGP3_VIA_GARTCTRL,
    543  1.1  kiyohara 	    pci_conf_read(sc->as_pc, asc->ctrl_tag, AGP3_VIA_ATTBASE) | 0x180);
    544  1.1  kiyohara }
    545  1.1  kiyohara 
    546  1.1  kiyohara static int
    547  1.1  kiyohara agp_amd64_via_set_aperture(struct agp_softc *sc, uint32_t aperture)
    548  1.1  kiyohara {
    549  1.1  kiyohara 	struct agp_amd64_softc *asc = sc->as_chipc;
    550  1.1  kiyohara 	uint32_t apsize;
    551  1.1  kiyohara 
    552  1.1  kiyohara 	apsize = ((aperture - 1) >> 20) ^ 0xff;
    553  1.1  kiyohara 	if ((((apsize ^ 0xff) << 20) | ((1 << 20) - 1)) + 1 != aperture)
    554  1.1  kiyohara 		return EINVAL;
    555  1.1  kiyohara 	pci_conf_write(sc->as_pc, asc->ctrl_tag, AGP3_VIA_APSIZE,
    556  1.1  kiyohara 	    (pci_conf_read(sc->as_pc, asc->ctrl_tag, AGP3_VIA_APSIZE) & ~0xff) |
    557  1.1  kiyohara 	    apsize);
    558  1.1  kiyohara 
    559  1.1  kiyohara 	return 0;
    560  1.1  kiyohara }
    561