xref: /freebsd/sys/dev/agp/agp_nvidia.c (revision d9c9c81c083a76a65c6cacf8fcbc0511e2ffa489)
134345c08SMatthew N. Dodd /*-
234345c08SMatthew N. Dodd  * Copyright (c) 2003 Matthew N. Dodd <winter@jurai.net>
334345c08SMatthew N. Dodd  * All rights reserved.
434345c08SMatthew N. Dodd  *
534345c08SMatthew N. Dodd  * Redistribution and use in source and binary forms, with or without
634345c08SMatthew N. Dodd  * modification, are permitted provided that the following conditions
734345c08SMatthew N. Dodd  * are met:
834345c08SMatthew N. Dodd  * 1. Redistributions of source code must retain the above copyright
934345c08SMatthew N. Dodd  *    notice, this list of conditions and the following disclaimer.
1034345c08SMatthew N. Dodd  * 2. Redistributions in binary form must reproduce the above copyright
1134345c08SMatthew N. Dodd  *    notice, this list of conditions and the following disclaimer in the
1234345c08SMatthew N. Dodd  *    documentation and/or other materials provided with the distribution.
1334345c08SMatthew N. Dodd  *
1434345c08SMatthew N. Dodd  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1534345c08SMatthew N. Dodd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1634345c08SMatthew N. Dodd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1734345c08SMatthew N. Dodd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1834345c08SMatthew N. Dodd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1934345c08SMatthew N. Dodd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2034345c08SMatthew N. Dodd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2134345c08SMatthew N. Dodd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2234345c08SMatthew N. Dodd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2334345c08SMatthew N. Dodd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2434345c08SMatthew N. Dodd  * SUCH DAMAGE.
2534345c08SMatthew N. Dodd  */
2634345c08SMatthew N. Dodd 
27e0026a65SMaxime Henrion #include <sys/cdefs.h>
28e0026a65SMaxime Henrion __FBSDID("$FreeBSD$");
29e0026a65SMaxime Henrion 
3034345c08SMatthew N. Dodd /*
3134345c08SMatthew N. Dodd  * Written using information gleaned from the
3234345c08SMatthew N. Dodd  * NVIDIA nForce/nForce2 AGPGART Linux Kernel Patch.
3334345c08SMatthew N. Dodd  */
3434345c08SMatthew N. Dodd 
3534345c08SMatthew N. Dodd #include <sys/param.h>
3634345c08SMatthew N. Dodd #include <sys/systm.h>
3734345c08SMatthew N. Dodd #include <sys/malloc.h>
3834345c08SMatthew N. Dodd #include <sys/kernel.h>
39f11d01c3SPoul-Henning Kamp #include <sys/module.h>
4034345c08SMatthew N. Dodd #include <sys/bus.h>
4134345c08SMatthew N. Dodd #include <sys/lock.h>
4234345c08SMatthew N. Dodd 
4334345c08SMatthew N. Dodd #if __FreeBSD_version < 500000
4434345c08SMatthew N. Dodd #include "opt_pci.h"
4534345c08SMatthew N. Dodd #endif
4634345c08SMatthew N. Dodd 
4734345c08SMatthew N. Dodd #if __FreeBSD_version > 500000
4834345c08SMatthew N. Dodd #include <sys/mutex.h>
4934345c08SMatthew N. Dodd #include <sys/proc.h>
5034345c08SMatthew N. Dodd #endif
5134345c08SMatthew N. Dodd 
52dbac8ff4SJohn Baldwin #include <dev/agp/agppriv.h>
53dbac8ff4SJohn Baldwin #include <dev/agp/agpreg.h>
546b312d76SMatthew N. Dodd #include <dev/pci/pcivar.h>
556b312d76SMatthew N. Dodd #include <dev/pci/pcireg.h>
5634345c08SMatthew N. Dodd 
5734345c08SMatthew N. Dodd #include <vm/vm.h>
5834345c08SMatthew N. Dodd #include <vm/vm_object.h>
5934345c08SMatthew N. Dodd #include <vm/pmap.h>
6034345c08SMatthew N. Dodd 
6134345c08SMatthew N. Dodd #include <machine/bus.h>
6234345c08SMatthew N. Dodd #include <machine/resource.h>
6334345c08SMatthew N. Dodd #include <sys/rman.h>
6434345c08SMatthew N. Dodd 
6534345c08SMatthew N. Dodd #define	NVIDIA_VENDORID		0x10de
6634345c08SMatthew N. Dodd #define	NVIDIA_DEVICEID_NFORCE	0x01a4
6734345c08SMatthew N. Dodd #define	NVIDIA_DEVICEID_NFORCE2	0x01e0
6834345c08SMatthew N. Dodd 
6934345c08SMatthew N. Dodd struct agp_nvidia_softc {
7034345c08SMatthew N. Dodd 	struct agp_softc	agp;
7134345c08SMatthew N. Dodd 	u_int32_t		initial_aperture; /* aperture size at startup */
7234345c08SMatthew N. Dodd 	struct agp_gatt *	gatt;
7334345c08SMatthew N. Dodd 
7434345c08SMatthew N. Dodd 	device_t		dev;		/* AGP Controller */
7534345c08SMatthew N. Dodd 	device_t		mc1_dev;	/* Memory Controller */
7634345c08SMatthew N. Dodd 	device_t		mc2_dev;	/* Memory Controller */
7734345c08SMatthew N. Dodd 	device_t		bdev;		/* Bridge */
7834345c08SMatthew N. Dodd 
7934345c08SMatthew N. Dodd 	u_int32_t		wbc_mask;
8034345c08SMatthew N. Dodd 	int			num_dirs;
8134345c08SMatthew N. Dodd 	int			num_active_entries;
8234345c08SMatthew N. Dodd 	off_t			pg_offset;
8334345c08SMatthew N. Dodd };
8434345c08SMatthew N. Dodd 
8534345c08SMatthew N. Dodd static const char *agp_nvidia_match(device_t dev);
8634345c08SMatthew N. Dodd static int agp_nvidia_probe(device_t);
8734345c08SMatthew N. Dodd static int agp_nvidia_attach(device_t);
8834345c08SMatthew N. Dodd static int agp_nvidia_detach(device_t);
8934345c08SMatthew N. Dodd static u_int32_t agp_nvidia_get_aperture(device_t);
9034345c08SMatthew N. Dodd static int agp_nvidia_set_aperture(device_t, u_int32_t);
91446188d1SAndriy Gapon static int agp_nvidia_bind_page(device_t, vm_offset_t, vm_offset_t);
92446188d1SAndriy Gapon static int agp_nvidia_unbind_page(device_t, vm_offset_t);
9334345c08SMatthew N. Dodd 
9434345c08SMatthew N. Dodd static int nvidia_init_iorr(u_int32_t, u_int32_t);
9534345c08SMatthew N. Dodd 
9634345c08SMatthew N. Dodd static const char *
9734345c08SMatthew N. Dodd agp_nvidia_match (device_t dev)
9834345c08SMatthew N. Dodd {
9934345c08SMatthew N. Dodd 	if (pci_get_class(dev) != PCIC_BRIDGE ||
10034345c08SMatthew N. Dodd 	    pci_get_subclass(dev) != PCIS_BRIDGE_HOST ||
10134345c08SMatthew N. Dodd 	    pci_get_vendor(dev) != NVIDIA_VENDORID)
10234345c08SMatthew N. Dodd 		return (NULL);
10334345c08SMatthew N. Dodd 
10434345c08SMatthew N. Dodd 	switch (pci_get_device(dev)) {
10534345c08SMatthew N. Dodd 	case NVIDIA_DEVICEID_NFORCE:
10634345c08SMatthew N. Dodd 		return ("NVIDIA nForce AGP Controller");
10734345c08SMatthew N. Dodd 	case NVIDIA_DEVICEID_NFORCE2:
10834345c08SMatthew N. Dodd 		return ("NVIDIA nForce2 AGP Controller");
10934345c08SMatthew N. Dodd 	}
110824a5e96SDavid E. O'Brien 	return (NULL);
11134345c08SMatthew N. Dodd }
11234345c08SMatthew N. Dodd 
11334345c08SMatthew N. Dodd static int
11434345c08SMatthew N. Dodd agp_nvidia_probe (device_t dev)
11534345c08SMatthew N. Dodd {
11634345c08SMatthew N. Dodd 	const char *desc;
11734345c08SMatthew N. Dodd 
118a8de37b0SEitan Adler 	if (resource_disabled("agp", device_get_unit(dev)))
119a8de37b0SEitan Adler 		return (ENXIO);
12034345c08SMatthew N. Dodd 	desc = agp_nvidia_match(dev);
12134345c08SMatthew N. Dodd 	if (desc) {
12234345c08SMatthew N. Dodd 		device_set_desc(dev, desc);
123d701c913SWarner Losh 		return (BUS_PROBE_DEFAULT);
12434345c08SMatthew N. Dodd 	}
12534345c08SMatthew N. Dodd 	return (ENXIO);
12634345c08SMatthew N. Dodd }
12734345c08SMatthew N. Dodd 
12834345c08SMatthew N. Dodd static int
12934345c08SMatthew N. Dodd agp_nvidia_attach (device_t dev)
13034345c08SMatthew N. Dodd {
13134345c08SMatthew N. Dodd 	struct agp_nvidia_softc *sc = device_get_softc(dev);
13234345c08SMatthew N. Dodd 	struct agp_gatt *gatt;
13334345c08SMatthew N. Dodd 	u_int32_t apbase;
13434345c08SMatthew N. Dodd 	u_int32_t aplimit;
13534345c08SMatthew N. Dodd 	u_int32_t temp;
13634345c08SMatthew N. Dodd 	int size;
13734345c08SMatthew N. Dodd 	int i;
13834345c08SMatthew N. Dodd 	int error;
13934345c08SMatthew N. Dodd 
14034345c08SMatthew N. Dodd 	switch (pci_get_device(dev)) {
14134345c08SMatthew N. Dodd 	case NVIDIA_DEVICEID_NFORCE:
14234345c08SMatthew N. Dodd 		sc->wbc_mask = 0x00010000;
14334345c08SMatthew N. Dodd 		break;
14434345c08SMatthew N. Dodd 	case NVIDIA_DEVICEID_NFORCE2:
14534345c08SMatthew N. Dodd 		sc->wbc_mask = 0x80000000;
14634345c08SMatthew N. Dodd 		break;
14734345c08SMatthew N. Dodd 	default:
148695b15caSEric Anholt 		device_printf(dev, "Bad chip id\n");
149695b15caSEric Anholt 		return (ENODEV);
15034345c08SMatthew N. Dodd 	}
15134345c08SMatthew N. Dodd 
15234345c08SMatthew N. Dodd 	/* AGP Controller */
15334345c08SMatthew N. Dodd 	sc->dev = dev;
15434345c08SMatthew N. Dodd 
15534345c08SMatthew N. Dodd 	/* Memory Controller 1 */
15634345c08SMatthew N. Dodd 	sc->mc1_dev = pci_find_bsf(pci_get_bus(dev), 0, 1);
15734345c08SMatthew N. Dodd 	if (sc->mc1_dev == NULL) {
15834345c08SMatthew N. Dodd 		device_printf(dev,
15934345c08SMatthew N. Dodd 			"Unable to find NVIDIA Memory Controller 1.\n");
16034345c08SMatthew N. Dodd 		return (ENODEV);
16134345c08SMatthew N. Dodd 	}
16234345c08SMatthew N. Dodd 
16334345c08SMatthew N. Dodd 	/* Memory Controller 2 */
16434345c08SMatthew N. Dodd 	sc->mc2_dev = pci_find_bsf(pci_get_bus(dev), 0, 2);
16534345c08SMatthew N. Dodd 	if (sc->mc2_dev == NULL) {
16634345c08SMatthew N. Dodd 		device_printf(dev,
16734345c08SMatthew N. Dodd 			"Unable to find NVIDIA Memory Controller 2.\n");
16834345c08SMatthew N. Dodd 		return (ENODEV);
16934345c08SMatthew N. Dodd 	}
17034345c08SMatthew N. Dodd 
17134345c08SMatthew N. Dodd 	/* AGP Host to PCI Bridge */
17234345c08SMatthew N. Dodd 	sc->bdev = pci_find_bsf(pci_get_bus(dev), 30, 0);
17334345c08SMatthew N. Dodd 	if (sc->bdev == NULL) {
17434345c08SMatthew N. Dodd 		device_printf(dev,
17534345c08SMatthew N. Dodd 			"Unable to find NVIDIA AGP Host to PCI Bridge.\n");
17634345c08SMatthew N. Dodd 		return (ENODEV);
17734345c08SMatthew N. Dodd 	}
17834345c08SMatthew N. Dodd 
17934345c08SMatthew N. Dodd 	error = agp_generic_attach(dev);
18034345c08SMatthew N. Dodd 	if (error)
18134345c08SMatthew N. Dodd 		return (error);
18234345c08SMatthew N. Dodd 
18334345c08SMatthew N. Dodd 	sc->initial_aperture = AGP_GET_APERTURE(dev);
18434345c08SMatthew N. Dodd 
18534345c08SMatthew N. Dodd 	for (;;) {
18634345c08SMatthew N. Dodd 		gatt = agp_alloc_gatt(dev);
18734345c08SMatthew N. Dodd 		if (gatt)
18834345c08SMatthew N. Dodd 			break;
18934345c08SMatthew N. Dodd 		/*
19034345c08SMatthew N. Dodd 		 * Probably contigmalloc failure. Try reducing the
19134345c08SMatthew N. Dodd 		 * aperture so that the gatt size reduces.
19234345c08SMatthew N. Dodd 		 */
19334345c08SMatthew N. Dodd 		if (AGP_SET_APERTURE(dev, AGP_GET_APERTURE(dev) / 2))
19434345c08SMatthew N. Dodd 			goto fail;
19534345c08SMatthew N. Dodd 	}
19634345c08SMatthew N. Dodd 	sc->gatt = gatt;
19734345c08SMatthew N. Dodd 
19834345c08SMatthew N. Dodd 	apbase = rman_get_start(sc->agp.as_aperture);
19934345c08SMatthew N. Dodd 	aplimit = apbase + AGP_GET_APERTURE(dev) - 1;
20034345c08SMatthew N. Dodd 	pci_write_config(sc->mc2_dev, AGP_NVIDIA_2_APBASE, apbase, 4);
20134345c08SMatthew N. Dodd 	pci_write_config(sc->mc2_dev, AGP_NVIDIA_2_APLIMIT, aplimit, 4);
20234345c08SMatthew N. Dodd 	pci_write_config(sc->bdev, AGP_NVIDIA_3_APBASE, apbase, 4);
20334345c08SMatthew N. Dodd 	pci_write_config(sc->bdev, AGP_NVIDIA_3_APLIMIT, aplimit, 4);
20434345c08SMatthew N. Dodd 
20534345c08SMatthew N. Dodd 	error = nvidia_init_iorr(apbase, AGP_GET_APERTURE(dev));
20634345c08SMatthew N. Dodd 	if (error) {
20734345c08SMatthew N. Dodd 		device_printf(dev, "Failed to setup IORRs\n");
20834345c08SMatthew N. Dodd 		goto fail;
20934345c08SMatthew N. Dodd 	}
21034345c08SMatthew N. Dodd 
21134345c08SMatthew N. Dodd 	/* directory size is 64k */
21234345c08SMatthew N. Dodd 	size = AGP_GET_APERTURE(dev) / 1024 / 1024;
21334345c08SMatthew N. Dodd 	sc->num_dirs = size / 64;
21434345c08SMatthew N. Dodd 	sc->num_active_entries = (size == 32) ? 16384 : ((size * 1024) / 4);
21534345c08SMatthew N. Dodd 	sc->pg_offset = 0;
21634345c08SMatthew N. Dodd 	if (sc->num_dirs == 0) {
21734345c08SMatthew N. Dodd 		sc->num_dirs = 1;
21834345c08SMatthew N. Dodd 		sc->num_active_entries /= (64 / size);
219*d9c9c81cSPedro F. Giffuni 		sc->pg_offset = rounddown2(apbase & (64 * 1024 * 1024 - 1),
220*d9c9c81cSPedro F. Giffuni 		    AGP_GET_APERTURE(dev)) / PAGE_SIZE;
22134345c08SMatthew N. Dodd 	}
22234345c08SMatthew N. Dodd 
22334345c08SMatthew N. Dodd 	/* (G)ATT Base Address */
22434345c08SMatthew N. Dodd 	for (i = 0; i < 8; i++) {
22534345c08SMatthew N. Dodd 		pci_write_config(sc->mc2_dev, AGP_NVIDIA_2_ATTBASE(i),
22634345c08SMatthew N. Dodd 				 (sc->gatt->ag_physical +
227695b15caSEric Anholt 				   (i % sc->num_dirs) * 64 * 1024) | 1, 4);
22834345c08SMatthew N. Dodd 	}
22934345c08SMatthew N. Dodd 
23034345c08SMatthew N. Dodd 	/* GTLB Control */
23134345c08SMatthew N. Dodd 	temp = pci_read_config(sc->mc2_dev, AGP_NVIDIA_2_GARTCTRL, 4);
23234345c08SMatthew N. Dodd 	pci_write_config(sc->mc2_dev, AGP_NVIDIA_2_GARTCTRL, temp | 0x11, 4);
23334345c08SMatthew N. Dodd 
23434345c08SMatthew N. Dodd 	/* GART Control */
23534345c08SMatthew N. Dodd 	temp = pci_read_config(sc->dev, AGP_NVIDIA_0_APSIZE, 4);
23634345c08SMatthew N. Dodd 	pci_write_config(sc->dev, AGP_NVIDIA_0_APSIZE, temp | 0x100, 4);
23734345c08SMatthew N. Dodd 
23834345c08SMatthew N. Dodd 	return (0);
23934345c08SMatthew N. Dodd fail:
24034345c08SMatthew N. Dodd 	agp_generic_detach(dev);
24134345c08SMatthew N. Dodd 	return (ENOMEM);
24234345c08SMatthew N. Dodd }
24334345c08SMatthew N. Dodd 
24434345c08SMatthew N. Dodd static int
24534345c08SMatthew N. Dodd agp_nvidia_detach (device_t dev)
24634345c08SMatthew N. Dodd {
24734345c08SMatthew N. Dodd 	struct agp_nvidia_softc *sc = device_get_softc(dev);
24834345c08SMatthew N. Dodd 	u_int32_t temp;
24934345c08SMatthew N. Dodd 
250f82a1d49SJohn Baldwin 	agp_free_cdev(dev);
25134345c08SMatthew N. Dodd 
25234345c08SMatthew N. Dodd 	/* GART Control */
25334345c08SMatthew N. Dodd 	temp = pci_read_config(sc->dev, AGP_NVIDIA_0_APSIZE, 4);
25434345c08SMatthew N. Dodd 	pci_write_config(sc->dev, AGP_NVIDIA_0_APSIZE, temp & ~(0x100), 4);
25534345c08SMatthew N. Dodd 
25634345c08SMatthew N. Dodd 	/* GTLB Control */
25734345c08SMatthew N. Dodd 	temp = pci_read_config(sc->mc2_dev, AGP_NVIDIA_2_GARTCTRL, 4);
25834345c08SMatthew N. Dodd 	pci_write_config(sc->mc2_dev, AGP_NVIDIA_2_GARTCTRL, temp & ~(0x11), 4);
25934345c08SMatthew N. Dodd 
26034345c08SMatthew N. Dodd 	/* Put the aperture back the way it started. */
26134345c08SMatthew N. Dodd 	AGP_SET_APERTURE(dev, sc->initial_aperture);
26234345c08SMatthew N. Dodd 
26334345c08SMatthew N. Dodd 	/* restore iorr for previous aperture size */
26434345c08SMatthew N. Dodd 	nvidia_init_iorr(rman_get_start(sc->agp.as_aperture),
26534345c08SMatthew N. Dodd 			 sc->initial_aperture);
26634345c08SMatthew N. Dodd 
26734345c08SMatthew N. Dodd 	agp_free_gatt(sc->gatt);
268f82a1d49SJohn Baldwin 	agp_free_res(dev);
26934345c08SMatthew N. Dodd 
27034345c08SMatthew N. Dodd 	return (0);
27134345c08SMatthew N. Dodd }
27234345c08SMatthew N. Dodd 
27334345c08SMatthew N. Dodd static u_int32_t
27434345c08SMatthew N. Dodd agp_nvidia_get_aperture(device_t dev)
27534345c08SMatthew N. Dodd {
276695b15caSEric Anholt 	switch (pci_read_config(dev, AGP_NVIDIA_0_APSIZE, 1) & 0x0f) {
277128236c0SKevin Lo 	case 0: return (512 * 1024 * 1024);
278128236c0SKevin Lo 	case 8: return (256 * 1024 * 1024);
279128236c0SKevin Lo 	case 12: return (128 * 1024 * 1024);
280128236c0SKevin Lo 	case 14: return (64 * 1024 * 1024);
281128236c0SKevin Lo 	case 15: return (32 * 1024 * 1024);
282695b15caSEric Anholt 	default:
2835d6d2228SBrian Somers 		device_printf(dev, "Invalid aperture setting 0x%x\n",
284695b15caSEric Anholt 		    pci_read_config(dev, AGP_NVIDIA_0_APSIZE, 1));
285695b15caSEric Anholt 		return 0;
286695b15caSEric Anholt 	}
28734345c08SMatthew N. Dodd }
28834345c08SMatthew N. Dodd 
28934345c08SMatthew N. Dodd static int
29034345c08SMatthew N. Dodd agp_nvidia_set_aperture(device_t dev, u_int32_t aperture)
29134345c08SMatthew N. Dodd {
29234345c08SMatthew N. Dodd 	u_int8_t val;
29334345c08SMatthew N. Dodd 	u_int8_t key;
29434345c08SMatthew N. Dodd 
29534345c08SMatthew N. Dodd 	switch (aperture) {
29634345c08SMatthew N. Dodd 	case (512 * 1024 * 1024): key = 0; break;
29734345c08SMatthew N. Dodd 	case (256 * 1024 * 1024): key = 8; break;
29834345c08SMatthew N. Dodd 	case (128 * 1024 * 1024): key = 12; break;
29934345c08SMatthew N. Dodd 	case (64 * 1024 * 1024): key = 14; break;
30034345c08SMatthew N. Dodd 	case (32 * 1024 * 1024): key = 15; break;
30134345c08SMatthew N. Dodd 	default:
30234345c08SMatthew N. Dodd 		device_printf(dev, "Invalid aperture size (%dMb)\n",
30334345c08SMatthew N. Dodd 				aperture / 1024 / 1024);
30434345c08SMatthew N. Dodd 		return (EINVAL);
30534345c08SMatthew N. Dodd 	}
30634345c08SMatthew N. Dodd 	val = pci_read_config(dev, AGP_NVIDIA_0_APSIZE, 1);
30734345c08SMatthew N. Dodd 	pci_write_config(dev, AGP_NVIDIA_0_APSIZE, ((val & ~0x0f) | key), 1);
30834345c08SMatthew N. Dodd 
30934345c08SMatthew N. Dodd 	return (0);
31034345c08SMatthew N. Dodd }
31134345c08SMatthew N. Dodd 
31234345c08SMatthew N. Dodd static int
313446188d1SAndriy Gapon agp_nvidia_bind_page(device_t dev, vm_offset_t offset, vm_offset_t physical)
31434345c08SMatthew N. Dodd {
31534345c08SMatthew N. Dodd 	struct agp_nvidia_softc *sc = device_get_softc(dev);
31634345c08SMatthew N. Dodd 	u_int32_t index;
31734345c08SMatthew N. Dodd 
318446188d1SAndriy Gapon 	if (offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
31934345c08SMatthew N. Dodd 		return (EINVAL);
32034345c08SMatthew N. Dodd 
32134345c08SMatthew N. Dodd 	index = (sc->pg_offset + offset) >> AGP_PAGE_SHIFT;
322695b15caSEric Anholt 	sc->gatt->ag_virtual[index] = physical | 1;
32334345c08SMatthew N. Dodd 
32434345c08SMatthew N. Dodd 	return (0);
32534345c08SMatthew N. Dodd }
32634345c08SMatthew N. Dodd 
32734345c08SMatthew N. Dodd static int
328446188d1SAndriy Gapon agp_nvidia_unbind_page(device_t dev, vm_offset_t offset)
32934345c08SMatthew N. Dodd {
33034345c08SMatthew N. Dodd 	struct agp_nvidia_softc *sc = device_get_softc(dev);
33134345c08SMatthew N. Dodd 	u_int32_t index;
33234345c08SMatthew N. Dodd 
333446188d1SAndriy Gapon 	if (offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
33434345c08SMatthew N. Dodd 		return (EINVAL);
33534345c08SMatthew N. Dodd 
33634345c08SMatthew N. Dodd 	index = (sc->pg_offset + offset) >> AGP_PAGE_SHIFT;
33734345c08SMatthew N. Dodd 	sc->gatt->ag_virtual[index] = 0;
33834345c08SMatthew N. Dodd 
33934345c08SMatthew N. Dodd 	return (0);
34034345c08SMatthew N. Dodd }
34134345c08SMatthew N. Dodd 
342446188d1SAndriy Gapon static void
343446188d1SAndriy Gapon agp_nvidia_flush_tlb (device_t dev)
34434345c08SMatthew N. Dodd {
34534345c08SMatthew N. Dodd 	struct agp_nvidia_softc *sc;
34634345c08SMatthew N. Dodd 	u_int32_t wbc_reg, temp;
347695b15caSEric Anholt 	volatile u_int32_t *ag_virtual;
348ce6d6902SJohn Baldwin 	int i, pages;
34934345c08SMatthew N. Dodd 
35034345c08SMatthew N. Dodd 	sc = (struct agp_nvidia_softc *)device_get_softc(dev);
35134345c08SMatthew N. Dodd 
35234345c08SMatthew N. Dodd 	if (sc->wbc_mask) {
35334345c08SMatthew N. Dodd 		wbc_reg = pci_read_config(sc->mc1_dev, AGP_NVIDIA_1_WBC, 4);
35434345c08SMatthew N. Dodd 		wbc_reg |= sc->wbc_mask;
35534345c08SMatthew N. Dodd 		pci_write_config(sc->mc1_dev, AGP_NVIDIA_1_WBC, wbc_reg, 4);
35634345c08SMatthew N. Dodd 
35734345c08SMatthew N. Dodd 		/* Wait no more than 3 seconds. */
35834345c08SMatthew N. Dodd 		for (i = 0; i < 3000; i++) {
35934345c08SMatthew N. Dodd 			wbc_reg = pci_read_config(sc->mc1_dev,
36034345c08SMatthew N. Dodd 						  AGP_NVIDIA_1_WBC, 4);
36134345c08SMatthew N. Dodd 			if ((sc->wbc_mask & wbc_reg) == 0)
36234345c08SMatthew N. Dodd 				break;
36334345c08SMatthew N. Dodd 			else
36434345c08SMatthew N. Dodd 				DELAY(1000);
36534345c08SMatthew N. Dodd 		}
36634345c08SMatthew N. Dodd 		if (i == 3000)
36734345c08SMatthew N. Dodd 			device_printf(dev,
36834345c08SMatthew N. Dodd 				"TLB flush took more than 3 seconds.\n");
36934345c08SMatthew N. Dodd 	}
37034345c08SMatthew N. Dodd 
371695b15caSEric Anholt 	ag_virtual = (volatile u_int32_t *)sc->gatt->ag_virtual;
372695b15caSEric Anholt 
37334345c08SMatthew N. Dodd 	/* Flush TLB entries. */
374ce6d6902SJohn Baldwin 	pages = sc->gatt->ag_entries * sizeof(u_int32_t) / PAGE_SIZE;
375ce6d6902SJohn Baldwin 	for(i = 0; i < pages; i++)
376695b15caSEric Anholt 		temp = ag_virtual[i * PAGE_SIZE / sizeof(u_int32_t)];
377ce6d6902SJohn Baldwin 	for(i = 0; i < pages; i++)
378695b15caSEric Anholt 		temp = ag_virtual[i * PAGE_SIZE / sizeof(u_int32_t)];
37934345c08SMatthew N. Dodd }
38034345c08SMatthew N. Dodd 
38134345c08SMatthew N. Dodd #define	SYSCFG		0xC0010010
38234345c08SMatthew N. Dodd #define	IORR_BASE0	0xC0010016
38334345c08SMatthew N. Dodd #define	IORR_MASK0	0xC0010017
38434345c08SMatthew N. Dodd #define	AMD_K7_NUM_IORR	2
38534345c08SMatthew N. Dodd 
38634345c08SMatthew N. Dodd static int
38734345c08SMatthew N. Dodd nvidia_init_iorr(u_int32_t addr, u_int32_t size)
38834345c08SMatthew N. Dodd {
38934345c08SMatthew N. Dodd 	quad_t base, mask, sys;
39034345c08SMatthew N. Dodd 	u_int32_t iorr_addr, free_iorr_addr;
39134345c08SMatthew N. Dodd 
39234345c08SMatthew N. Dodd 	/* Find the iorr that is already used for the addr */
39334345c08SMatthew N. Dodd 	/* If not found, determine the uppermost available iorr */
39434345c08SMatthew N. Dodd 	free_iorr_addr = AMD_K7_NUM_IORR;
39534345c08SMatthew N. Dodd 	for(iorr_addr = 0; iorr_addr < AMD_K7_NUM_IORR; iorr_addr++) {
39634345c08SMatthew N. Dodd 		base = rdmsr(IORR_BASE0 + 2 * iorr_addr);
39734345c08SMatthew N. Dodd 		mask = rdmsr(IORR_MASK0 + 2 * iorr_addr);
39834345c08SMatthew N. Dodd 
39934345c08SMatthew N. Dodd 		if ((base & 0xfffff000ULL) == (addr & 0xfffff000))
40034345c08SMatthew N. Dodd 			break;
40134345c08SMatthew N. Dodd 
40234345c08SMatthew N. Dodd 		if ((mask & 0x00000800ULL) == 0)
40334345c08SMatthew N. Dodd 			free_iorr_addr = iorr_addr;
40434345c08SMatthew N. Dodd 	}
40534345c08SMatthew N. Dodd 
40634345c08SMatthew N. Dodd 	if (iorr_addr >= AMD_K7_NUM_IORR) {
40734345c08SMatthew N. Dodd 		iorr_addr = free_iorr_addr;
40834345c08SMatthew N. Dodd 		if (iorr_addr >= AMD_K7_NUM_IORR)
40934345c08SMatthew N. Dodd 			return (EINVAL);
41034345c08SMatthew N. Dodd 	}
41134345c08SMatthew N. Dodd 
41234345c08SMatthew N. Dodd 	base = (addr & ~0xfff) | 0x18;
413*d9c9c81cSPedro F. Giffuni 	mask = (0xfULL << 32) | rounddown2(0xfffff000, size) | 0x800;
41434345c08SMatthew N. Dodd 	wrmsr(IORR_BASE0 + 2 * iorr_addr, base);
41534345c08SMatthew N. Dodd 	wrmsr(IORR_MASK0 + 2 * iorr_addr, mask);
41634345c08SMatthew N. Dodd 
41734345c08SMatthew N. Dodd 	sys = rdmsr(SYSCFG);
41834345c08SMatthew N. Dodd 	sys |= 0x00100000ULL;
41934345c08SMatthew N. Dodd 	wrmsr(SYSCFG, sys);
42034345c08SMatthew N. Dodd 
42134345c08SMatthew N. Dodd 	return (0);
42234345c08SMatthew N. Dodd }
42334345c08SMatthew N. Dodd 
42434345c08SMatthew N. Dodd static device_method_t agp_nvidia_methods[] = {
42534345c08SMatthew N. Dodd 	/* Device interface */
42634345c08SMatthew N. Dodd 	DEVMETHOD(device_probe,		agp_nvidia_probe),
42734345c08SMatthew N. Dodd 	DEVMETHOD(device_attach,	agp_nvidia_attach),
42834345c08SMatthew N. Dodd 	DEVMETHOD(device_detach,	agp_nvidia_detach),
42934345c08SMatthew N. Dodd 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
43034345c08SMatthew N. Dodd 	DEVMETHOD(device_suspend,	bus_generic_suspend),
43134345c08SMatthew N. Dodd 	DEVMETHOD(device_resume,	bus_generic_resume),
43234345c08SMatthew N. Dodd 
43334345c08SMatthew N. Dodd 	/* AGP interface */
43434345c08SMatthew N. Dodd 	DEVMETHOD(agp_get_aperture,	agp_nvidia_get_aperture),
43534345c08SMatthew N. Dodd 	DEVMETHOD(agp_set_aperture,	agp_nvidia_set_aperture),
43634345c08SMatthew N. Dodd 	DEVMETHOD(agp_bind_page,	agp_nvidia_bind_page),
43734345c08SMatthew N. Dodd 	DEVMETHOD(agp_unbind_page,	agp_nvidia_unbind_page),
43834345c08SMatthew N. Dodd 	DEVMETHOD(agp_flush_tlb,	agp_nvidia_flush_tlb),
43934345c08SMatthew N. Dodd 
44034345c08SMatthew N. Dodd 	DEVMETHOD(agp_enable,		agp_generic_enable),
44134345c08SMatthew N. Dodd 	DEVMETHOD(agp_alloc_memory,	agp_generic_alloc_memory),
44234345c08SMatthew N. Dodd 	DEVMETHOD(agp_free_memory,	agp_generic_free_memory),
44334345c08SMatthew N. Dodd 	DEVMETHOD(agp_bind_memory,	agp_generic_bind_memory),
44434345c08SMatthew N. Dodd 	DEVMETHOD(agp_unbind_memory,	agp_generic_unbind_memory),
44534345c08SMatthew N. Dodd 
44634345c08SMatthew N. Dodd 	{ 0, 0 }
44734345c08SMatthew N. Dodd };
44834345c08SMatthew N. Dodd 
44934345c08SMatthew N. Dodd static driver_t agp_nvidia_driver = {
45034345c08SMatthew N. Dodd 	"agp",
45134345c08SMatthew N. Dodd 	agp_nvidia_methods,
45234345c08SMatthew N. Dodd 	sizeof(struct agp_nvidia_softc),
45334345c08SMatthew N. Dodd };
45434345c08SMatthew N. Dodd 
45534345c08SMatthew N. Dodd static devclass_t agp_devclass;
45634345c08SMatthew N. Dodd 
457c626f1feSJohn Baldwin DRIVER_MODULE(agp_nvidia, hostb, agp_nvidia_driver, agp_devclass, 0, 0);
45834345c08SMatthew N. Dodd MODULE_DEPEND(agp_nvidia, agp, 1, 1, 1);
45934345c08SMatthew N. Dodd MODULE_DEPEND(agp_nvidia, pci, 1, 1, 1);
460