Home | History | Annotate | Line # | Download | only in pci
agp_amd64.c revision 1.9
      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.9       chs __KERNEL_RCSID(0, "$NetBSD: agp_amd64.c,v 1.9 2019/11/10 21:16:36 chs 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.9       chs 	asc = malloc(sizeof(struct agp_amd64_softc), M_AGP, M_WAITOK | M_ZERO);
    212  1.1  kiyohara 
    213  1.1  kiyohara 	if (agp_map_aperture(pa, sc, AGP_APBASE) != 0) {
    214  1.1  kiyohara 		aprint_error(": can't map aperture\n");
    215  1.8  riastrad 		error = ENXIO;
    216  1.8  riastrad 		goto fail1;
    217  1.1  kiyohara 	}
    218  1.1  kiyohara 
    219  1.1  kiyohara 	maxdevs = pci_bus_maxdevs(pa->pa_pc, 0);
    220  1.1  kiyohara 	for (i = 0, n = 0; i < maxdevs && n < AMD64_MAX_MCTRL; i++) {
    221  1.1  kiyohara 		tag = pci_make_tag(pa->pa_pc, 0, i, 3);
    222  1.1  kiyohara 		id = pci_conf_read(pa->pa_pc, tag, PCI_ID_REG);
    223  1.1  kiyohara 		if (PCI_VENDOR(id) == PCI_VENDOR_AMD &&
    224  1.7   tsutsui 		    (PCI_PRODUCT(id) == PCI_PRODUCT_AMD_AMD64_MISC ||
    225  1.7   tsutsui 		     PCI_PRODUCT(id) == PCI_PRODUCT_AMD_AMD64_F10_MISC)) {
    226  1.1  kiyohara 			asc->mctrl_tag[n] = tag;
    227  1.1  kiyohara 			n++;
    228  1.1  kiyohara 		}
    229  1.1  kiyohara 	}
    230  1.7   tsutsui 	if (n == 0) {
    231  1.7   tsutsui 		aprint_error(": No Miscellaneous Control unit found.\n");
    232  1.8  riastrad 		error = ENXIO;
    233  1.8  riastrad 		goto fail1;
    234  1.7   tsutsui 	}
    235  1.1  kiyohara 	asc->n_mctrl = n;
    236  1.1  kiyohara 
    237  1.5  kiyohara 	aprint_normal(": %d Miscellaneous Control unit(s) found.\n",
    238  1.1  kiyohara 	    asc->n_mctrl);
    239  1.5  kiyohara 	aprint_normal("%s", device_xname(self));
    240  1.1  kiyohara 
    241  1.1  kiyohara 	sc->as_chipc = asc;
    242  1.1  kiyohara 	sc->as_methods = &agp_amd64_methods;
    243  1.1  kiyohara 	pci_get_capability(pa->pa_pc, pa->pa_tag, PCI_CAP_AGP, &sc->as_capoff,
    244  1.1  kiyohara 	    NULL);
    245  1.1  kiyohara 	asc->initial_aperture = AGP_GET_APERTURE(sc);
    246  1.1  kiyohara 
    247  1.1  kiyohara 	for (;;) {
    248  1.1  kiyohara 		gatt = agp_alloc_gatt(sc);
    249  1.1  kiyohara 		if (gatt)
    250  1.1  kiyohara 			break;
    251  1.1  kiyohara 
    252  1.1  kiyohara 		/*
    253  1.1  kiyohara 		 * Probably contigmalloc failure. Try reducing the
    254  1.1  kiyohara 		 * aperture so that the gatt size reduces.
    255  1.1  kiyohara 		 */
    256  1.1  kiyohara 		if (AGP_SET_APERTURE(sc, AGP_GET_APERTURE(sc) / 2)) {
    257  1.8  riastrad 			error = ENOMEM;
    258  1.8  riastrad 			goto fail1;
    259  1.1  kiyohara 		}
    260  1.1  kiyohara 	}
    261  1.1  kiyohara 	asc->gatt = gatt;
    262  1.1  kiyohara 
    263  1.1  kiyohara 	switch (PCI_VENDOR(sc->as_id)) {
    264  1.1  kiyohara 	case PCI_VENDOR_ALI:
    265  1.1  kiyohara 		agp_amd64_uli_init(sc);
    266  1.8  riastrad 		if (agp_amd64_uli_set_aperture(sc, asc->initial_aperture)) {
    267  1.8  riastrad 			/* XXX Back out agp_amd64_uli_init?  */
    268  1.8  riastrad 			error = ENXIO;
    269  1.8  riastrad 			goto fail2;
    270  1.8  riastrad 		}
    271  1.1  kiyohara 		break;
    272  1.1  kiyohara 
    273  1.1  kiyohara 	case PCI_VENDOR_NVIDIA:
    274  1.1  kiyohara 		asc->ctrl_tag = AGP_AMD64_NVIDIA_PCITAG(pa->pa_pc);
    275  1.1  kiyohara 		agp_amd64_nvidia_init(sc);
    276  1.8  riastrad 		if (agp_amd64_nvidia_set_aperture(sc, asc->initial_aperture)) {
    277  1.8  riastrad 			/* XXX Back out agp_amd64_nvidia_init?  */
    278  1.8  riastrad 			error = ENXIO;
    279  1.8  riastrad 			goto fail2;
    280  1.8  riastrad 		}
    281  1.1  kiyohara 		break;
    282  1.1  kiyohara 
    283  1.1  kiyohara 	case PCI_VENDOR_VIATECH:
    284  1.1  kiyohara 		asc->via_agp = agp_amd64_via_match(pa);
    285  1.1  kiyohara 		if (asc->via_agp) {
    286  1.1  kiyohara 			asc->ctrl_tag = AGP_AMD64_VIA_PCITAG(pa->pa_pc);
    287  1.1  kiyohara 			agp_amd64_via_init(sc);
    288  1.1  kiyohara 			if (agp_amd64_via_set_aperture(sc,
    289  1.8  riastrad 			    asc->initial_aperture)) {
    290  1.8  riastrad 				/* XXX Back out agp_amd64_via_init?  */
    291  1.8  riastrad 				error = ENXIO;
    292  1.8  riastrad 				goto fail2;
    293  1.8  riastrad 			}
    294  1.1  kiyohara 		}
    295  1.1  kiyohara 		break;
    296  1.1  kiyohara 	}
    297  1.1  kiyohara 
    298  1.1  kiyohara 	/* Install the gatt and enable aperture. */
    299  1.1  kiyohara 	attbase = (uint32_t)(gatt->ag_physical >> 8) & AGP_AMD64_ATTBASE_MASK;
    300  1.1  kiyohara 	for (i = 0; i < asc->n_mctrl; i++) {
    301  1.1  kiyohara 		pci_conf_write(pa->pa_pc, asc->mctrl_tag[i], AGP_AMD64_ATTBASE,
    302  1.1  kiyohara 		    attbase);
    303  1.1  kiyohara 		apctrl = pci_conf_read(pa->pa_pc, asc->mctrl_tag[i],
    304  1.1  kiyohara 		    AGP_AMD64_APCTRL);
    305  1.1  kiyohara 		apctrl |= AGP_AMD64_APCTRL_GARTEN;
    306  1.1  kiyohara 		apctrl &=
    307  1.1  kiyohara 		    ~(AGP_AMD64_APCTRL_DISGARTCPU | AGP_AMD64_APCTRL_DISGARTIO);
    308  1.1  kiyohara 		pci_conf_write(pa->pa_pc, asc->mctrl_tag[i], AGP_AMD64_APCTRL,
    309  1.1  kiyohara 		    apctrl);
    310  1.1  kiyohara 	}
    311  1.1  kiyohara 
    312  1.1  kiyohara 	agp_flush_cache();
    313  1.1  kiyohara 
    314  1.8  riastrad 	/* Success!  */
    315  1.1  kiyohara 	return 0;
    316  1.8  riastrad 
    317  1.8  riastrad fail2:	agp_free_gatt(sc, gatt);
    318  1.8  riastrad fail1:	free(asc, M_AGP);
    319  1.9       chs 	agp_generic_detach(sc);
    320  1.8  riastrad 	KASSERT(error);
    321  1.8  riastrad 	return error;
    322  1.1  kiyohara }
    323  1.1  kiyohara 
    324  1.1  kiyohara 
    325  1.1  kiyohara static uint32_t agp_amd64_table[] = {
    326  1.1  kiyohara 	0x02000000,	/*   32 MB */
    327  1.1  kiyohara 	0x04000000,	/*   64 MB */
    328  1.1  kiyohara 	0x08000000,	/*  128 MB */
    329  1.1  kiyohara 	0x10000000,	/*  256 MB */
    330  1.1  kiyohara 	0x20000000,	/*  512 MB */
    331  1.1  kiyohara 	0x40000000,	/* 1024 MB */
    332  1.1  kiyohara 	0x80000000,	/* 2048 MB */
    333  1.1  kiyohara };
    334  1.1  kiyohara 
    335  1.1  kiyohara #define AGP_AMD64_TABLE_SIZE \
    336  1.1  kiyohara 	(sizeof(agp_amd64_table) / sizeof(agp_amd64_table[0]))
    337  1.1  kiyohara 
    338  1.1  kiyohara static uint32_t
    339  1.1  kiyohara agp_amd64_get_aperture(struct agp_softc *sc)
    340  1.1  kiyohara {
    341  1.1  kiyohara 	struct agp_amd64_softc *asc = sc->as_chipc;
    342  1.1  kiyohara 	uint32_t i;
    343  1.1  kiyohara 
    344  1.1  kiyohara 	i = (pci_conf_read(sc->as_pc, asc->mctrl_tag[0], AGP_AMD64_APCTRL) &
    345  1.1  kiyohara 		AGP_AMD64_APCTRL_SIZE_MASK) >> 1;
    346  1.1  kiyohara 
    347  1.1  kiyohara 	if (i >= AGP_AMD64_TABLE_SIZE)
    348  1.1  kiyohara 		return 0;
    349  1.1  kiyohara 
    350  1.1  kiyohara 	return agp_amd64_table[i];
    351  1.1  kiyohara }
    352  1.1  kiyohara 
    353  1.1  kiyohara static int
    354  1.1  kiyohara agp_amd64_set_aperture(struct agp_softc *sc, uint32_t aperture)
    355  1.1  kiyohara {
    356  1.1  kiyohara 	struct agp_amd64_softc *asc = sc->as_chipc;
    357  1.1  kiyohara 	uint32_t i;
    358  1.1  kiyohara 	pcireg_t apctrl;
    359  1.1  kiyohara 	int j;
    360  1.1  kiyohara 
    361  1.1  kiyohara 	for (i = 0; i < AGP_AMD64_TABLE_SIZE; i++)
    362  1.1  kiyohara 		if (agp_amd64_table[i] == aperture)
    363  1.1  kiyohara 			break;
    364  1.1  kiyohara 	if (i >= AGP_AMD64_TABLE_SIZE)
    365  1.1  kiyohara 		return EINVAL;
    366  1.1  kiyohara 
    367  1.1  kiyohara 	for (j = 0; j < asc->n_mctrl; j++) {
    368  1.1  kiyohara 		apctrl = pci_conf_read(sc->as_pc, asc->mctrl_tag[0],
    369  1.1  kiyohara 		    AGP_AMD64_APCTRL);
    370  1.1  kiyohara 		pci_conf_write(sc->as_pc, asc->mctrl_tag[0], AGP_AMD64_APCTRL,
    371  1.1  kiyohara 		    (apctrl & ~(AGP_AMD64_APCTRL_SIZE_MASK)) | (i << 1));
    372  1.1  kiyohara 	}
    373  1.1  kiyohara 
    374  1.1  kiyohara 	switch (PCI_VENDOR(sc->as_id)) {
    375  1.1  kiyohara 	case PCI_VENDOR_ALI:
    376  1.1  kiyohara 		return agp_amd64_uli_set_aperture(sc, aperture);
    377  1.1  kiyohara 		break;
    378  1.1  kiyohara 
    379  1.1  kiyohara 	case PCI_VENDOR_NVIDIA:
    380  1.1  kiyohara 		return agp_amd64_nvidia_set_aperture(sc, aperture);
    381  1.1  kiyohara 		break;
    382  1.1  kiyohara 
    383  1.1  kiyohara 	case PCI_VENDOR_VIATECH:
    384  1.1  kiyohara 		if (asc->via_agp)
    385  1.1  kiyohara 			return agp_amd64_via_set_aperture(sc, aperture);
    386  1.1  kiyohara 		break;
    387  1.1  kiyohara 	}
    388  1.1  kiyohara 
    389  1.1  kiyohara 	return 0;
    390  1.1  kiyohara }
    391  1.1  kiyohara 
    392  1.1  kiyohara static int
    393  1.1  kiyohara agp_amd64_bind_page(struct agp_softc *sc, off_t offset, bus_addr_t physical)
    394  1.1  kiyohara {
    395  1.1  kiyohara 	struct agp_amd64_softc *asc = sc->as_chipc;
    396  1.1  kiyohara 
    397  1.1  kiyohara 	if (offset < 0 || offset >= (asc->gatt->ag_entries << AGP_PAGE_SHIFT))
    398  1.1  kiyohara 		return EINVAL;
    399  1.1  kiyohara 
    400  1.1  kiyohara 	asc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] =
    401  1.1  kiyohara 	    (physical & 0xfffff000) | ((physical >> 28) & 0x00000ff0) | 3;
    402  1.1  kiyohara 
    403  1.1  kiyohara 	return 0;
    404  1.1  kiyohara }
    405  1.1  kiyohara 
    406  1.1  kiyohara static int
    407  1.1  kiyohara agp_amd64_unbind_page(struct agp_softc *sc, off_t offset)
    408  1.1  kiyohara {
    409  1.1  kiyohara 	struct agp_amd64_softc *asc = sc->as_chipc;
    410  1.1  kiyohara 
    411  1.1  kiyohara 	if (offset < 0 || offset >= (asc->gatt->ag_entries << AGP_PAGE_SHIFT))
    412  1.1  kiyohara 		return EINVAL;
    413  1.1  kiyohara 
    414  1.1  kiyohara 	asc->gatt->ag_virtual[offset >> AGP_PAGE_SHIFT] = 0;
    415  1.1  kiyohara 
    416  1.1  kiyohara 	return 0;
    417  1.1  kiyohara }
    418  1.1  kiyohara 
    419  1.1  kiyohara static void
    420  1.1  kiyohara agp_amd64_flush_tlb(struct agp_softc *sc)
    421  1.1  kiyohara {
    422  1.1  kiyohara 	struct agp_amd64_softc *asc = sc->as_chipc;
    423  1.1  kiyohara 	pcireg_t cachectrl;
    424  1.1  kiyohara 	int i;
    425  1.1  kiyohara 
    426  1.1  kiyohara 	for (i = 0; i < asc->n_mctrl; i++) {
    427  1.1  kiyohara 		cachectrl = pci_conf_read(sc->as_pc, asc->mctrl_tag[i],
    428  1.1  kiyohara 		    AGP_AMD64_CACHECTRL);
    429  1.1  kiyohara 		pci_conf_write(sc->as_pc, asc->mctrl_tag[i],
    430  1.1  kiyohara 		    AGP_AMD64_CACHECTRL,
    431  1.1  kiyohara 		    cachectrl | AGP_AMD64_CACHECTRL_INVGART);
    432  1.1  kiyohara 	}
    433  1.1  kiyohara }
    434  1.1  kiyohara 
    435  1.1  kiyohara static void
    436  1.1  kiyohara agp_amd64_apbase_fixup(struct agp_softc *sc)
    437  1.1  kiyohara {
    438  1.1  kiyohara 	struct agp_amd64_softc *asc = sc->as_chipc;
    439  1.1  kiyohara 	uint32_t apbase;
    440  1.1  kiyohara 	int i;
    441  1.1  kiyohara 
    442  1.1  kiyohara 	apbase = pci_conf_read(sc->as_pc, sc->as_tag, AGP_APBASE);
    443  1.1  kiyohara 	asc->apbase = PCI_MAPREG_MEM_ADDR(apbase);
    444  1.1  kiyohara 	apbase = (asc->apbase >> 25) & AGP_AMD64_APBASE_MASK;
    445  1.1  kiyohara 	for (i = 0; i < asc->n_mctrl; i++)
    446  1.1  kiyohara 		pci_conf_write(sc->as_pc, asc->mctrl_tag[i], AGP_AMD64_APBASE,
    447  1.1  kiyohara 		    apbase);
    448  1.1  kiyohara }
    449  1.1  kiyohara 
    450  1.1  kiyohara static void
    451  1.1  kiyohara agp_amd64_uli_init(struct agp_softc *sc)
    452  1.1  kiyohara {
    453  1.1  kiyohara 	struct agp_amd64_softc *asc = sc->as_chipc;
    454  1.1  kiyohara 	pcireg_t apbase;
    455  1.1  kiyohara 
    456  1.1  kiyohara 	agp_amd64_apbase_fixup(sc);
    457  1.1  kiyohara 	apbase = pci_conf_read(sc->as_pc, sc->as_tag, AGP_AMD64_ULI_APBASE);
    458  1.1  kiyohara 	pci_conf_write(sc->as_pc, sc->as_tag, AGP_AMD64_ULI_APBASE,
    459  1.1  kiyohara 	    (apbase & 0x0000000f) | asc->apbase);
    460  1.1  kiyohara 	pci_conf_write(sc->as_pc, sc->as_tag, AGP_AMD64_ULI_HTT_FEATURE,
    461  1.1  kiyohara 	    asc->apbase);
    462  1.1  kiyohara }
    463  1.1  kiyohara 
    464  1.1  kiyohara static int
    465  1.1  kiyohara agp_amd64_uli_set_aperture(struct agp_softc *sc, uint32_t aperture)
    466  1.1  kiyohara {
    467  1.1  kiyohara 	struct agp_amd64_softc *asc = sc->as_chipc;
    468  1.1  kiyohara 
    469  1.1  kiyohara 	switch (aperture) {
    470  1.1  kiyohara 	case 0x02000000:	/*  32 MB */
    471  1.1  kiyohara 	case 0x04000000:	/*  64 MB */
    472  1.1  kiyohara 	case 0x08000000:	/* 128 MB */
    473  1.1  kiyohara 	case 0x10000000:	/* 256 MB */
    474  1.1  kiyohara 		break;
    475  1.1  kiyohara 	default:
    476  1.1  kiyohara 		return EINVAL;
    477  1.1  kiyohara 	}
    478  1.1  kiyohara 
    479  1.1  kiyohara 	pci_conf_write(sc->as_pc, sc->as_tag, AGP_AMD64_ULI_ENU_SCR,
    480  1.1  kiyohara 	    asc->apbase + aperture - 1);
    481  1.1  kiyohara 
    482  1.1  kiyohara 	return 0;
    483  1.1  kiyohara }
    484  1.1  kiyohara 
    485  1.1  kiyohara static void
    486  1.1  kiyohara agp_amd64_nvidia_init(struct agp_softc *sc)
    487  1.1  kiyohara {
    488  1.1  kiyohara 	struct agp_amd64_softc *asc = sc->as_chipc;
    489  1.1  kiyohara 	pcireg_t apbase;
    490  1.1  kiyohara 
    491  1.1  kiyohara 	agp_amd64_apbase_fixup(sc);
    492  1.1  kiyohara 	apbase =
    493  1.1  kiyohara 	    pci_conf_read(sc->as_pc, sc->as_tag, AGP_AMD64_NVIDIA_0_APBASE);
    494  1.1  kiyohara 	pci_conf_write(sc->as_pc, sc->as_tag, AGP_AMD64_NVIDIA_0_APBASE,
    495  1.1  kiyohara 	    (apbase & 0x0000000f) | asc->apbase);
    496  1.1  kiyohara 	pci_conf_write(sc->as_pc, asc->ctrl_tag, AGP_AMD64_NVIDIA_1_APBASE1,
    497  1.1  kiyohara 	    asc->apbase);
    498  1.1  kiyohara 	pci_conf_write(sc->as_pc, asc->ctrl_tag, AGP_AMD64_NVIDIA_1_APBASE2,
    499  1.1  kiyohara 	    asc->apbase);
    500  1.1  kiyohara }
    501  1.1  kiyohara 
    502  1.1  kiyohara static int
    503  1.1  kiyohara agp_amd64_nvidia_set_aperture(struct agp_softc *sc, uint32_t aperture)
    504  1.1  kiyohara {
    505  1.1  kiyohara 	struct agp_amd64_softc *asc = sc->as_chipc;
    506  1.1  kiyohara 	uint32_t apsize;
    507  1.1  kiyohara 
    508  1.1  kiyohara 	switch (aperture) {
    509  1.1  kiyohara 	case 0x02000000:	apsize = 0x0f;	break;	/*  32 MB */
    510  1.1  kiyohara 	case 0x04000000:	apsize = 0x0e;	break;	/*  64 MB */
    511  1.1  kiyohara 	case 0x08000000:	apsize = 0x0c;	break;	/* 128 MB */
    512  1.1  kiyohara 	case 0x10000000:	apsize = 0x08;	break;	/* 256 MB */
    513  1.1  kiyohara 	case 0x20000000:	apsize = 0x00;	break;	/* 512 MB */
    514  1.1  kiyohara 	default:
    515  1.1  kiyohara 		return EINVAL;
    516  1.1  kiyohara 	}
    517  1.1  kiyohara 
    518  1.1  kiyohara 	pci_conf_write(sc->as_pc, asc->ctrl_tag, AGP_AMD64_NVIDIA_1_APSIZE,
    519  1.1  kiyohara 	    (pci_conf_read(sc->as_pc, asc->ctrl_tag,
    520  1.1  kiyohara 	    AGP_AMD64_NVIDIA_1_APSIZE) & 0xfffffff0) | apsize);
    521  1.1  kiyohara 	pci_conf_write(sc->as_pc, asc->ctrl_tag, AGP_AMD64_NVIDIA_1_APLIMIT1,
    522  1.1  kiyohara 	    asc->apbase + aperture - 1);
    523  1.1  kiyohara 	pci_conf_write(sc->as_pc, asc->ctrl_tag, AGP_AMD64_NVIDIA_1_APLIMIT2,
    524  1.1  kiyohara 	    asc->apbase + aperture - 1);
    525  1.1  kiyohara 
    526  1.1  kiyohara 	return 0;
    527  1.1  kiyohara }
    528  1.1  kiyohara 
    529  1.1  kiyohara static void
    530  1.1  kiyohara agp_amd64_via_init(struct agp_softc *sc)
    531  1.1  kiyohara {
    532  1.1  kiyohara 	struct agp_amd64_softc *asc = sc->as_chipc;
    533  1.1  kiyohara 
    534  1.1  kiyohara 	agp_amd64_apbase_fixup(sc);
    535  1.1  kiyohara 	pci_conf_write(sc->as_pc, asc->ctrl_tag, AGP3_VIA_ATTBASE,
    536  1.1  kiyohara 	    asc->gatt->ag_physical);
    537  1.1  kiyohara 	pci_conf_write(sc->as_pc, asc->ctrl_tag, AGP3_VIA_GARTCTRL,
    538  1.1  kiyohara 	    pci_conf_read(sc->as_pc, asc->ctrl_tag, AGP3_VIA_ATTBASE) | 0x180);
    539  1.1  kiyohara }
    540  1.1  kiyohara 
    541  1.1  kiyohara static int
    542  1.1  kiyohara agp_amd64_via_set_aperture(struct agp_softc *sc, uint32_t aperture)
    543  1.1  kiyohara {
    544  1.1  kiyohara 	struct agp_amd64_softc *asc = sc->as_chipc;
    545  1.1  kiyohara 	uint32_t apsize;
    546  1.1  kiyohara 
    547  1.1  kiyohara 	apsize = ((aperture - 1) >> 20) ^ 0xff;
    548  1.1  kiyohara 	if ((((apsize ^ 0xff) << 20) | ((1 << 20) - 1)) + 1 != aperture)
    549  1.1  kiyohara 		return EINVAL;
    550  1.1  kiyohara 	pci_conf_write(sc->as_pc, asc->ctrl_tag, AGP3_VIA_APSIZE,
    551  1.1  kiyohara 	    (pci_conf_read(sc->as_pc, asc->ctrl_tag, AGP3_VIA_APSIZE) & ~0xff) |
    552  1.1  kiyohara 	    apsize);
    553  1.1  kiyohara 
    554  1.1  kiyohara 	return 0;
    555  1.1  kiyohara }
    556