agp_i810.c revision 179901
163010Sdfr/*-
263010Sdfr * Copyright (c) 2000 Doug Rabson
363010Sdfr * Copyright (c) 2000 Ruslan Ermilov
463010Sdfr * All rights reserved.
563010Sdfr *
663010Sdfr * Redistribution and use in source and binary forms, with or without
763010Sdfr * modification, are permitted provided that the following conditions
863010Sdfr * are met:
963010Sdfr * 1. Redistributions of source code must retain the above copyright
1063010Sdfr *    notice, this list of conditions and the following disclaimer.
1163010Sdfr * 2. Redistributions in binary form must reproduce the above copyright
1263010Sdfr *    notice, this list of conditions and the following disclaimer in the
1363010Sdfr *    documentation and/or other materials provided with the distribution.
1463010Sdfr *
1563010Sdfr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1663010Sdfr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1763010Sdfr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1863010Sdfr * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1963010Sdfr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2063010Sdfr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2163010Sdfr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2263010Sdfr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2363010Sdfr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2463010Sdfr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2563010Sdfr * SUCH DAMAGE.
2663010Sdfr */
2763010Sdfr
28103243Sanholt/*
29103243Sanholt * Fixes for 830/845G support: David Dawes <dawes@xfree86.org>
30110785Sanholt * 852GM/855GM/865G support added by David Dawes <dawes@xfree86.org>
31103243Sanholt */
32103243Sanholt
33116192Sobrien#include <sys/cdefs.h>
34116192Sobrien__FBSDID("$FreeBSD: head/sys/dev/agp/agp_i810.c 179901 2008-06-20 22:23:41Z gonzo $");
35116192Sobrien
3663010Sdfr#include "opt_bus.h"
3763010Sdfr
3863010Sdfr#include <sys/param.h>
3963010Sdfr#include <sys/systm.h>
4063010Sdfr#include <sys/malloc.h>
4163010Sdfr#include <sys/kernel.h>
42129878Sphk#include <sys/module.h>
4363010Sdfr#include <sys/bus.h>
4463010Sdfr#include <sys/lock.h>
4576827Salfred#include <sys/mutex.h>
4679339Sjhb#include <sys/proc.h>
4763010Sdfr
48173573Sjhb#include <dev/agp/agppriv.h>
49173573Sjhb#include <dev/agp/agpreg.h>
50119288Simp#include <dev/pci/pcivar.h>
51119288Simp#include <dev/pci/pcireg.h>
5263010Sdfr
5363010Sdfr#include <vm/vm.h>
5463010Sdfr#include <vm/vm_object.h>
5563010Sdfr#include <vm/vm_page.h>
5663010Sdfr#include <vm/vm_pageout.h>
5763010Sdfr#include <vm/pmap.h>
5863010Sdfr
5963010Sdfr#include <machine/bus.h>
6063010Sdfr#include <machine/resource.h>
61171433Sanholt#include <machine/md_var.h>
6263010Sdfr#include <sys/rman.h>
6363010Sdfr
6463010SdfrMALLOC_DECLARE(M_AGP);
6563010Sdfr
66171433Sanholtenum {
67171433Sanholt	CHIP_I810,	/* i810/i815 */
68171433Sanholt	CHIP_I830,	/* 830M/845G */
69171433Sanholt	CHIP_I855,	/* 852GM/855GM/865G */
70171433Sanholt	CHIP_I915,	/* 915G/915GM */
71171433Sanholt	CHIP_I965,	/* G965 */
72171433Sanholt	CHIP_G33,	/* G33/Q33/Q35 */
73171433Sanholt};
7463010Sdfr
75171433Sanholt/* The i810 through i855 have the registers at BAR 1, and the GATT gets
76171433Sanholt * allocated by us.  The i915 has registers in BAR 0 and the GATT is at the
77171433Sanholt * start of the stolen memory, and should only be accessed by the OS through
78171433Sanholt * BAR 3.  The G965 has registers and GATT in the same BAR (0) -- first 512KB
79171433Sanholt * is registers, second 512KB is GATT.
80171433Sanholt */
81171433Sanholtstatic struct resource_spec agp_i810_res_spec[] = {
82171433Sanholt	{ SYS_RES_MEMORY, AGP_I810_MMADR, RF_ACTIVE | RF_SHAREABLE },
83171433Sanholt	{ -1, 0 }
84171433Sanholt};
85103243Sanholt
86171433Sanholtstatic struct resource_spec agp_i915_res_spec[] = {
87171433Sanholt	{ SYS_RES_MEMORY, AGP_I915_MMADR, RF_ACTIVE | RF_SHAREABLE },
88171433Sanholt	{ SYS_RES_MEMORY, AGP_I915_GTTADR, RF_ACTIVE | RF_SHAREABLE },
89171433Sanholt	{ -1, 0 }
90171433Sanholt};
91171433Sanholt
92171433Sanholtstatic struct resource_spec agp_i965_res_spec[] = {
93171433Sanholt	{ SYS_RES_MEMORY, AGP_I965_GTTMMADR, RF_ACTIVE | RF_SHAREABLE },
94171433Sanholt	{ -1, 0 }
95171433Sanholt};
96171433Sanholt
9763010Sdfrstruct agp_i810_softc {
9863010Sdfr	struct agp_softc agp;
9963010Sdfr	u_int32_t initial_aperture;	/* aperture size at startup */
10063010Sdfr	struct agp_gatt *gatt;
101103243Sanholt	int chiptype;			/* i810-like or i830 */
102103243Sanholt	u_int32_t dcache_size;		/* i810 only */
103103243Sanholt	u_int32_t stolen;		/* number of i830/845 gtt entries for stolen memory */
10463010Sdfr	device_t bdev;			/* bridge device */
105153031Sanholt
106171433Sanholt	void *argb_cursor;		/* contigmalloc area for ARGB cursor */
107153031Sanholt
108171433Sanholt	struct resource_spec * sc_res_spec;
109171433Sanholt	struct resource *sc_res[2];
11063010Sdfr};
11163010Sdfr
112159926Sanholt/* For adding new devices, devid is the id of the graphics controller
113159926Sanholt * (pci:0:2:0, for example).  The placeholder (usually at pci:0:2:1) for the
114159926Sanholt * second head should never be added.  The bridge_offset is the offset to
115159926Sanholt * subtract from devid to get the id of the hostb that the device is on.
116159926Sanholt */
117159926Sanholtstatic const struct agp_i810_match {
118159926Sanholt	int devid;
119159926Sanholt	int chiptype;
120159926Sanholt	int bridge_offset;
121159926Sanholt	char *name;
122159926Sanholt} agp_i810_matches[] = {
123159926Sanholt	{0x71218086, CHIP_I810, 0x00010000,
124159926Sanholt	    "Intel 82810 (i810 GMCH) SVGA controller"},
125159926Sanholt	{0x71238086, CHIP_I810, 0x00010000,
126159926Sanholt	    "Intel 82810-DC100 (i810-DC100 GMCH) SVGA controller"},
127159926Sanholt	{0x71258086, CHIP_I810, 0x00010000,
128159926Sanholt	    "Intel 82810E (i810E GMCH) SVGA controller"},
129159926Sanholt	{0x11328086, CHIP_I810, 0x00020000,
130159926Sanholt	    "Intel 82815 (i815 GMCH) SVGA controller"},
131159926Sanholt	{0x35778086, CHIP_I830, 0x00020000,
132159926Sanholt	    "Intel 82830M (830M GMCH) SVGA controller"},
133173946Sremko	{0x25628086, CHIP_I830, 0x00020000,
134173946Sremko	    "Intel 82845M (845M GMCH) SVGA controller"},
135159981Sanholt	{0x35828086, CHIP_I855, 0x00020000,
136159926Sanholt	    "Intel 82852/5"},
137159981Sanholt	{0x25728086, CHIP_I855, 0x00020000,
138159926Sanholt	    "Intel 82865G (865G GMCH) SVGA controller"},
139159926Sanholt	{0x25828086, CHIP_I915, 0x00020000,
140159926Sanholt	    "Intel 82915G (915G GMCH) SVGA controller"},
141172187Salc	{0x258A8086, CHIP_I915, 0x00020000,
142172187Salc	    "Intel E7221 SVGA controller"},
143159926Sanholt	{0x25928086, CHIP_I915, 0x00020000,
144159926Sanholt	    "Intel 82915GM (915GM GMCH) SVGA controller"},
145159926Sanholt	{0x27728086, CHIP_I915, 0x00020000,
146159926Sanholt	    "Intel 82945G (945G GMCH) SVGA controller"},
147159926Sanholt	{0x27A28086, CHIP_I915, 0x00020000,
148159926Sanholt	    "Intel 82945GM (945GM GMCH) SVGA controller"},
149179901Sgonzo	{0x27AE8086, CHIP_I915, 0x00020000,
150171433Sanholt	    "Intel 945GME SVGA controller"},
151171433Sanholt	{0x29728086, CHIP_I965, 0x00020000,
152171433Sanholt	    "Intel 946GZ SVGA controller"},
153171433Sanholt	{0x29828086, CHIP_I965, 0x00020000,
154171433Sanholt	    "Intel G965 SVGA controller"},
155171433Sanholt	{0x29928086, CHIP_I965, 0x00020000,
156171433Sanholt	    "Intel Q965 SVGA controller"},
157171433Sanholt	{0x29a28086, CHIP_I965, 0x00020000,
158171433Sanholt	    "Intel G965 SVGA controller"},
159171433Sanholt/*
160171433Sanholt	{0x29b28086, CHIP_G33, 0x00020000,
161171433Sanholt	    "Intel Q35 SVGA controller"},
162171433Sanholt	{0x29c28086, CHIP_G33, 0x00020000,
163171433Sanholt	    "Intel G33 SVGA controller"},
164171433Sanholt	{0x29d28086, CHIP_G33, 0x00020000,
165171433Sanholt	    "Intel Q33 SVGA controller"},
166171433Sanholt*/
167171433Sanholt	{0x2a028086, CHIP_I965, 0x00020000,
168171433Sanholt	    "Intel GM965 SVGA controller"},
169171433Sanholt	{0x2a128086, CHIP_I965, 0x00020000,
170171433Sanholt	    "Intel GME965 SVGA controller"},
171159926Sanholt	{0, 0, 0, NULL}
172159926Sanholt};
173159926Sanholt
174159926Sanholtstatic const struct agp_i810_match*
17563010Sdfragp_i810_match(device_t dev)
17663010Sdfr{
177159926Sanholt	int i, devid;
178159926Sanholt
17963010Sdfr	if (pci_get_class(dev) != PCIC_DISPLAY
18063010Sdfr	    || pci_get_subclass(dev) != PCIS_DISPLAY_VGA)
18163010Sdfr		return NULL;
18263010Sdfr
183159926Sanholt	devid = pci_get_devid(dev);
184159926Sanholt	for (i = 0; agp_i810_matches[i].devid != 0; i++) {
185159926Sanholt		if (agp_i810_matches[i].devid == devid)
186159926Sanholt		    break;
187159926Sanholt	}
188159926Sanholt	if (agp_i810_matches[i].devid == 0)
189159926Sanholt		return NULL;
190159926Sanholt	else
191159926Sanholt		return &agp_i810_matches[i];
19263010Sdfr}
19363010Sdfr
19463010Sdfr/*
19563010Sdfr * Find bridge device.
19663010Sdfr */
19763010Sdfrstatic device_t
19863010Sdfragp_i810_find_bridge(device_t dev)
19963010Sdfr{
20063010Sdfr	device_t *children, child;
20163010Sdfr	int nchildren, i;
20263010Sdfr	u_int32_t devid;
203159926Sanholt	const struct agp_i810_match *match;
204159926Sanholt
205159926Sanholt	match = agp_i810_match(dev);
206159926Sanholt	devid = match->devid - match->bridge_offset;
20763010Sdfr
208153579Sjhb	if (device_get_children(device_get_parent(device_get_parent(dev)),
209153579Sjhb	    &children, &nchildren))
21063010Sdfr		return 0;
21163010Sdfr
21263010Sdfr	for (i = 0; i < nchildren; i++) {
21363010Sdfr		child = children[i];
21463010Sdfr
21563010Sdfr		if (pci_get_devid(child) == devid) {
21663010Sdfr			free(children, M_TEMP);
21763010Sdfr			return child;
21863010Sdfr		}
21963010Sdfr	}
22063010Sdfr	free(children, M_TEMP);
22163010Sdfr	return 0;
22263010Sdfr}
22363010Sdfr
224155186Sjhbstatic void
225155186Sjhbagp_i810_identify(driver_t *driver, device_t parent)
226155186Sjhb{
227155186Sjhb
228155186Sjhb	if (device_find_child(parent, "agp", -1) == NULL &&
229155186Sjhb	    agp_i810_match(parent))
230155186Sjhb		device_add_child(parent, "agp", -1);
231155186Sjhb}
232155186Sjhb
23363010Sdfrstatic int
23463010Sdfragp_i810_probe(device_t dev)
23563010Sdfr{
236159926Sanholt	device_t bdev;
237159926Sanholt	const struct agp_i810_match *match;
238171433Sanholt	u_int8_t smram;
239171433Sanholt	int gcc1, deven;
24063010Sdfr
241127815Snjl	if (resource_disabled("agp", device_get_unit(dev)))
242127815Snjl		return (ENXIO);
243159926Sanholt	match = agp_i810_match(dev);
244159926Sanholt	if (match == NULL)
245159926Sanholt		return ENXIO;
246159926Sanholt
247159926Sanholt	bdev = agp_i810_find_bridge(dev);
248159926Sanholt	if (!bdev) {
249159926Sanholt		if (bootverbose)
250159926Sanholt			printf("I810: can't find bridge device\n");
251159926Sanholt		return ENXIO;
252159926Sanholt	}
253159926Sanholt
254159926Sanholt	/*
255159926Sanholt	 * checking whether internal graphics device has been activated.
256159926Sanholt	 */
257171433Sanholt	switch (match->chiptype) {
258171433Sanholt	case CHIP_I810:
259159926Sanholt		smram = pci_read_config(bdev, AGP_I810_SMRAM, 1);
260171433Sanholt		if ((smram & AGP_I810_SMRAM_GMS) ==
261171433Sanholt		    AGP_I810_SMRAM_GMS_DISABLED) {
262159926Sanholt			if (bootverbose)
263159926Sanholt				printf("I810: disabled, not probing\n");
264159926Sanholt			return ENXIO;
265159926Sanholt		}
266171433Sanholt		break;
267171433Sanholt	case CHIP_I830:
268171433Sanholt	case CHIP_I855:
269159926Sanholt		gcc1 = pci_read_config(bdev, AGP_I830_GCC1, 1);
270159926Sanholt		if ((gcc1 & AGP_I830_GCC1_DEV2) ==
271159926Sanholt		    AGP_I830_GCC1_DEV2_DISABLED) {
27263010Sdfr			if (bootverbose)
273159926Sanholt				printf("I830: disabled, not probing\n");
27463010Sdfr			return ENXIO;
27563010Sdfr		}
276171433Sanholt		break;
277171433Sanholt	case CHIP_I915:
278171433Sanholt	case CHIP_I965:
279171433Sanholt	case CHIP_G33:
280171433Sanholt		deven = pci_read_config(bdev, AGP_I915_DEVEN, 4);
281171433Sanholt		if ((deven & AGP_I915_DEVEN_D2F0) ==
282159926Sanholt		    AGP_I915_DEVEN_D2F0_DISABLED) {
283159926Sanholt			if (bootverbose)
284159926Sanholt				printf("I915: disabled, not probing\n");
285159926Sanholt			return ENXIO;
286159926Sanholt		}
287171433Sanholt		break;
288159926Sanholt	}
289121437Sjhb
290159926Sanholt	if (match->devid == 0x35828086) {
291159926Sanholt		switch (pci_read_config(dev, AGP_I85X_CAPID, 1)) {
292159926Sanholt		case AGP_I855_GME:
293159926Sanholt			device_set_desc(dev,
294159926Sanholt			    "Intel 82855GME (855GME GMCH) SVGA controller");
295121437Sjhb			break;
296159926Sanholt		case AGP_I855_GM:
297159926Sanholt			device_set_desc(dev,
298159926Sanholt			    "Intel 82855GM (855GM GMCH) SVGA controller");
299153031Sanholt			break;
300159926Sanholt		case AGP_I852_GME:
301159926Sanholt			device_set_desc(dev,
302159926Sanholt			    "Intel 82852GME (852GME GMCH) SVGA controller");
303159926Sanholt			break;
304159926Sanholt		case AGP_I852_GM:
305159926Sanholt			device_set_desc(dev,
306159926Sanholt			    "Intel 82852GM (852GM GMCH) SVGA controller");
307159926Sanholt			break;
308121437Sjhb		default:
309159926Sanholt			device_set_desc(dev,
310159926Sanholt			    "Intel 8285xM (85xGM GMCH) SVGA controller");
311159926Sanholt			break;
31263010Sdfr		}
313159926Sanholt	} else {
314159926Sanholt		device_set_desc(dev, match->name);
31563010Sdfr	}
31663010Sdfr
317159926Sanholt	return BUS_PROBE_DEFAULT;
31863010Sdfr}
31963010Sdfr
320171433Sanholtstatic void
321171433Sanholtagp_i810_dump_regs(device_t dev)
322171433Sanholt{
323171433Sanholt	struct agp_i810_softc *sc = device_get_softc(dev);
324171433Sanholt
325171433Sanholt	device_printf(dev, "AGP_I810_PGTBL_CTL: %08x\n",
326171433Sanholt	    bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL));
327171433Sanholt
328171433Sanholt	switch (sc->chiptype) {
329171433Sanholt	case CHIP_I810:
330171433Sanholt		device_printf(dev, "AGP_I810_MISCC: 0x%04x\n",
331171433Sanholt		    pci_read_config(sc->bdev, AGP_I810_MISCC, 2));
332171433Sanholt		break;
333171433Sanholt	case CHIP_I830:
334171433Sanholt		device_printf(dev, "AGP_I830_GCC1: 0x%02x\n",
335171433Sanholt		    pci_read_config(sc->bdev, AGP_I830_GCC1, 1));
336171433Sanholt		break;
337171433Sanholt	case CHIP_I855:
338171433Sanholt		device_printf(dev, "AGP_I855_GCC1: 0x%02x\n",
339171433Sanholt		    pci_read_config(sc->bdev, AGP_I855_GCC1, 1));
340171433Sanholt		break;
341171433Sanholt	case CHIP_I915:
342171433Sanholt	case CHIP_I965:
343171433Sanholt	case CHIP_G33:
344171433Sanholt		device_printf(dev, "AGP_I855_GCC1: 0x%02x\n",
345171433Sanholt		    pci_read_config(sc->bdev, AGP_I855_GCC1, 1));
346171433Sanholt		device_printf(dev, "AGP_I915_MSAC: 0x%02x\n",
347171433Sanholt		    pci_read_config(sc->bdev, AGP_I915_MSAC, 1));
348171433Sanholt		break;
349171433Sanholt	}
350171433Sanholt	device_printf(dev, "Aperture resource size: %d bytes\n",
351171433Sanholt	    AGP_GET_APERTURE(dev));
352171433Sanholt}
353171433Sanholt
35463010Sdfrstatic int
35563010Sdfragp_i810_attach(device_t dev)
35663010Sdfr{
35763010Sdfr	struct agp_i810_softc *sc = device_get_softc(dev);
35863010Sdfr	struct agp_gatt *gatt;
359159926Sanholt	const struct agp_i810_match *match;
360171433Sanholt	int error;
36163010Sdfr
36263010Sdfr	sc->bdev = agp_i810_find_bridge(dev);
36363010Sdfr	if (!sc->bdev)
36463010Sdfr		return ENOENT;
36563010Sdfr
366171433Sanholt	match = agp_i810_match(dev);
367171433Sanholt	sc->chiptype = match->chiptype;
368171433Sanholt
369171433Sanholt	switch (sc->chiptype) {
370171433Sanholt	case CHIP_I810:
371171433Sanholt	case CHIP_I830:
372171433Sanholt	case CHIP_I855:
373171433Sanholt		sc->sc_res_spec = agp_i810_res_spec;
374171433Sanholt		agp_set_aperture_resource(dev, AGP_APBASE);
375171433Sanholt		break;
376171433Sanholt	case CHIP_I915:
377171433Sanholt	case CHIP_G33:
378171433Sanholt		sc->sc_res_spec = agp_i915_res_spec;
379171433Sanholt		agp_set_aperture_resource(dev, AGP_I915_GMADR);
380171433Sanholt		break;
381171433Sanholt	case CHIP_I965:
382171433Sanholt		sc->sc_res_spec = agp_i965_res_spec;
383171433Sanholt		agp_set_aperture_resource(dev, AGP_I915_GMADR);
384171433Sanholt		break;
385171433Sanholt	}
386171433Sanholt
38763010Sdfr	error = agp_generic_attach(dev);
38863010Sdfr	if (error)
38963010Sdfr		return error;
39063010Sdfr
391171433Sanholt	if (sc->chiptype != CHIP_I965 && sc->chiptype != CHIP_G33 &&
392171433Sanholt	    ptoa((vm_paddr_t)Maxmem) > 0xfffffffful)
393171433Sanholt	{
394171433Sanholt		device_printf(dev, "agp_i810.c does not support physical "
395171433Sanholt		    "memory above 4GB.\n");
396171433Sanholt		return ENOENT;
397171433Sanholt	}
398103243Sanholt
399171433Sanholt	if (bus_alloc_resources(dev, sc->sc_res_spec, sc->sc_res)) {
40063010Sdfr		agp_generic_detach(dev);
401153031Sanholt		return ENODEV;
40263010Sdfr	}
40363010Sdfr
40463010Sdfr	sc->initial_aperture = AGP_GET_APERTURE(dev);
40563010Sdfr
406103243Sanholt	gatt = malloc( sizeof(struct agp_gatt), M_AGP, M_NOWAIT);
407103243Sanholt	if (!gatt) {
408171433Sanholt		bus_release_resources(dev, sc->sc_res_spec, sc->sc_res);
409103243Sanholt 		agp_generic_detach(dev);
410103243Sanholt 		return ENOMEM;
411103243Sanholt	}
412103243Sanholt	sc->gatt = gatt;
41363010Sdfr
414103243Sanholt	gatt->ag_entries = AGP_GET_APERTURE(dev) >> AGP_PAGE_SHIFT;
41563010Sdfr
416103243Sanholt	if ( sc->chiptype == CHIP_I810 ) {
417103243Sanholt		/* Some i810s have on-chip memory called dcache */
418171433Sanholt		if (bus_read_1(sc->sc_res[0], AGP_I810_DRT) &
419171433Sanholt		    AGP_I810_DRT_POPULATED)
420103243Sanholt			sc->dcache_size = 4 * 1024 * 1024;
421103243Sanholt		else
422103243Sanholt			sc->dcache_size = 0;
423103243Sanholt
424103243Sanholt		/* According to the specs the gatt on the i810 must be 64k */
425103243Sanholt		gatt->ag_virtual = contigmalloc( 64 * 1024, M_AGP, 0,
426103243Sanholt					0, ~0, PAGE_SIZE, 0);
427103243Sanholt		if (!gatt->ag_virtual) {
428103243Sanholt			if (bootverbose)
429103243Sanholt				device_printf(dev, "contiguous allocation failed\n");
430171433Sanholt			bus_release_resources(dev, sc->sc_res_spec,
431171433Sanholt			    sc->sc_res);
432103243Sanholt			free(gatt, M_AGP);
43363010Sdfr			agp_generic_detach(dev);
43463010Sdfr			return ENOMEM;
43563010Sdfr		}
436103243Sanholt		bzero(gatt->ag_virtual, gatt->ag_entries * sizeof(u_int32_t));
437103243Sanholt
438103243Sanholt		gatt->ag_physical = vtophys((vm_offset_t) gatt->ag_virtual);
439103243Sanholt		agp_flush_cache();
440103243Sanholt		/* Install the GATT. */
441171433Sanholt		bus_write_4(sc->sc_res[0], AGP_I810_PGTBL_CTL,
442171433Sanholt		    gatt->ag_physical | 1);
443110785Sanholt	} else if ( sc->chiptype == CHIP_I830 ) {
444103243Sanholt		/* The i830 automatically initializes the 128k gatt on boot. */
445103243Sanholt		unsigned int gcc1, pgtblctl;
446103243Sanholt
447103243Sanholt		gcc1 = pci_read_config(sc->bdev, AGP_I830_GCC1, 1);
448103243Sanholt		switch (gcc1 & AGP_I830_GCC1_GMS) {
449103243Sanholt			case AGP_I830_GCC1_GMS_STOLEN_512:
450103243Sanholt				sc->stolen = (512 - 132) * 1024 / 4096;
451103243Sanholt				break;
452103243Sanholt			case AGP_I830_GCC1_GMS_STOLEN_1024:
453103243Sanholt				sc->stolen = (1024 - 132) * 1024 / 4096;
454103243Sanholt				break;
455103243Sanholt			case AGP_I830_GCC1_GMS_STOLEN_8192:
456103243Sanholt				sc->stolen = (8192 - 132) * 1024 / 4096;
457103243Sanholt				break;
458103243Sanholt			default:
459103243Sanholt				sc->stolen = 0;
460103243Sanholt				device_printf(dev, "unknown memory configuration, disabling\n");
461171433Sanholt				bus_release_resources(dev, sc->sc_res_spec,
462171433Sanholt				    sc->sc_res);
463171433Sanholt				free(gatt, M_AGP);
464103243Sanholt				agp_generic_detach(dev);
465103243Sanholt				return EINVAL;
466103243Sanholt		}
467171433Sanholt		if (sc->stolen > 0) {
468171433Sanholt			device_printf(dev, "detected %dk stolen memory\n",
469171433Sanholt			    sc->stolen * 4);
470171433Sanholt		}
471171433Sanholt		device_printf(dev, "aperture size is %dM\n",
472171433Sanholt		    sc->initial_aperture / 1024 / 1024);
473103243Sanholt
474103243Sanholt		/* GATT address is already in there, make sure it's enabled */
475171433Sanholt		pgtblctl = bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL);
476103243Sanholt		pgtblctl |= 1;
477171433Sanholt		bus_write_4(sc->sc_res[0], AGP_I810_PGTBL_CTL, pgtblctl);
478103243Sanholt
479103243Sanholt		gatt->ag_physical = pgtblctl & ~1;
480171433Sanholt	} else if (sc->chiptype == CHIP_I855 || sc->chiptype == CHIP_I915 ||
481171433Sanholt	    sc->chiptype == CHIP_I965 || sc->chiptype == CHIP_G33) {
482171433Sanholt		unsigned int gcc1, pgtblctl, stolen, gtt_size;
483153031Sanholt
484153031Sanholt		/* Stolen memory is set up at the beginning of the aperture by
485171433Sanholt		 * the BIOS, consisting of the GATT followed by 4kb for the
486171433Sanholt		 * BIOS display.
487153031Sanholt		 */
488171433Sanholt		switch (sc->chiptype) {
489171433Sanholt		case CHIP_I855:
490171433Sanholt			gtt_size = 128;
491171433Sanholt			break;
492171433Sanholt		case CHIP_I915:
493171433Sanholt			gtt_size = 256;
494171433Sanholt			break;
495171433Sanholt		case CHIP_I965:
496171433Sanholt		case CHIP_G33:
497171433Sanholt			switch (bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL) &
498171433Sanholt			    AGP_I810_PGTBL_SIZE_MASK) {
499171433Sanholt			case AGP_I810_PGTBL_SIZE_128KB:
500171433Sanholt				gtt_size = 128;
501110785Sanholt				break;
502171433Sanholt			case AGP_I810_PGTBL_SIZE_256KB:
503171433Sanholt				gtt_size = 256;
504110785Sanholt				break;
505171433Sanholt			case AGP_I810_PGTBL_SIZE_512KB:
506171433Sanholt				gtt_size = 512;
507110785Sanholt				break;
508110785Sanholt			default:
509171433Sanholt				device_printf(dev, "Bad PGTBL size\n");
510171433Sanholt				bus_release_resources(dev, sc->sc_res_spec,
511171433Sanholt				    sc->sc_res);
512171433Sanholt				free(gatt, M_AGP);
513110785Sanholt				agp_generic_detach(dev);
514110785Sanholt				return EINVAL;
515171433Sanholt			}
516171433Sanholt			break;
517171433Sanholt		default:
518171433Sanholt			device_printf(dev, "Bad chiptype\n");
519171433Sanholt			bus_release_resources(dev, sc->sc_res_spec,
520171433Sanholt			    sc->sc_res);
521171433Sanholt			free(gatt, M_AGP);
522171433Sanholt			agp_generic_detach(dev);
523171433Sanholt			return EINVAL;
524110785Sanholt		}
525171433Sanholt
526171433Sanholt		/* GCC1 is called MGGC on i915+ */
527171433Sanholt		gcc1 = pci_read_config(sc->bdev, AGP_I855_GCC1, 1);
528171433Sanholt		switch (gcc1 & AGP_I855_GCC1_GMS) {
529171433Sanholt		case AGP_I855_GCC1_GMS_STOLEN_1M:
530171433Sanholt			stolen = 1024;
531171433Sanholt			break;
532171433Sanholt		case AGP_I855_GCC1_GMS_STOLEN_4M:
533171433Sanholt			stolen = 4096;
534171433Sanholt			break;
535171433Sanholt		case AGP_I855_GCC1_GMS_STOLEN_8M:
536171433Sanholt			stolen = 8192;
537171433Sanholt			break;
538171433Sanholt		case AGP_I855_GCC1_GMS_STOLEN_16M:
539171433Sanholt			stolen = 16384;
540171433Sanholt			break;
541171433Sanholt		case AGP_I855_GCC1_GMS_STOLEN_32M:
542171433Sanholt			stolen = 32768;
543171433Sanholt			break;
544171433Sanholt		case AGP_I915_GCC1_GMS_STOLEN_48M:
545171433Sanholt			stolen = 49152;
546171433Sanholt			break;
547171433Sanholt		case AGP_I915_GCC1_GMS_STOLEN_64M:
548171433Sanholt			stolen = 65536;
549171433Sanholt			break;
550171433Sanholt		case AGP_G33_GCC1_GMS_STOLEN_128M:
551171433Sanholt			stolen = 128 * 1024;
552171433Sanholt			break;
553171433Sanholt		case AGP_G33_GCC1_GMS_STOLEN_256M:
554171433Sanholt			stolen = 256 * 1024;
555171433Sanholt			break;
556171433Sanholt		default:
557171433Sanholt			device_printf(dev, "unknown memory configuration, "
558171433Sanholt			    "disabling\n");
559171433Sanholt			bus_release_resources(dev, sc->sc_res_spec,
560171433Sanholt			    sc->sc_res);
561171433Sanholt			free(gatt, M_AGP);
562171433Sanholt			agp_generic_detach(dev);
563171433Sanholt			return EINVAL;
564171433Sanholt		}
565171433Sanholt		sc->stolen = (stolen - gtt_size - 4) * 1024 / 4096;
566110785Sanholt		if (sc->stolen > 0)
567110785Sanholt			device_printf(dev, "detected %dk stolen memory\n", sc->stolen * 4);
568110785Sanholt		device_printf(dev, "aperture size is %dM\n", sc->initial_aperture / 1024 / 1024);
569110785Sanholt
570110785Sanholt		/* GATT address is already in there, make sure it's enabled */
571171433Sanholt		pgtblctl = bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL);
572110785Sanholt		pgtblctl |= 1;
573171433Sanholt		bus_write_4(sc->sc_res[0], AGP_I810_PGTBL_CTL, pgtblctl);
574110785Sanholt
575110785Sanholt		gatt->ag_physical = pgtblctl & ~1;
57663010Sdfr	}
57763010Sdfr
578171433Sanholt	if (0)
579171433Sanholt		agp_i810_dump_regs(dev);
580171433Sanholt
581153579Sjhb	return 0;
58263010Sdfr}
58363010Sdfr
58463010Sdfrstatic int
58563010Sdfragp_i810_detach(device_t dev)
58663010Sdfr{
58763010Sdfr	struct agp_i810_softc *sc = device_get_softc(dev);
58863010Sdfr
589173203Sjhb	agp_free_cdev(dev);
59063010Sdfr
59163010Sdfr	/* Clear the GATT base. */
592103243Sanholt	if ( sc->chiptype == CHIP_I810 ) {
593171433Sanholt		bus_write_4(sc->sc_res[0], AGP_I810_PGTBL_CTL, 0);
594103243Sanholt	} else {
595103243Sanholt		unsigned int pgtblctl;
596171433Sanholt		pgtblctl = bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL);
597103243Sanholt		pgtblctl &= ~1;
598171433Sanholt		bus_write_4(sc->sc_res[0], AGP_I810_PGTBL_CTL, pgtblctl);
599103243Sanholt	}
60063010Sdfr
60163010Sdfr	/* Put the aperture back the way it started. */
60263010Sdfr	AGP_SET_APERTURE(dev, sc->initial_aperture);
60363010Sdfr
604103243Sanholt	if ( sc->chiptype == CHIP_I810 ) {
605103243Sanholt		contigfree(sc->gatt->ag_virtual, 64 * 1024, M_AGP);
606103243Sanholt	}
607103243Sanholt	free(sc->gatt, M_AGP);
60863010Sdfr
609171433Sanholt	bus_release_resources(dev, sc->sc_res_spec, sc->sc_res);
610173203Sjhb	agp_free_res(dev);
61163010Sdfr
61263010Sdfr	return 0;
61363010Sdfr}
61463010Sdfr
615177115Sremkostatic int
616177115Sremkoagp_i810_resume(device_t dev)
617177115Sremko{
618177115Sremko	struct agp_i810_softc *sc;
619177115Sremko	sc = device_get_softc(dev);
620177115Sremko
621177115Sremko	AGP_SET_APERTURE(dev, sc->initial_aperture);
622177115Sremko
623177115Sremko	/* Install the GATT. */
624177115Sremko	bus_write_4(sc->sc_res[0], AGP_I810_PGTBL_CTL,
625177115Sremko	sc->gatt->ag_physical | 1);
626177115Sremko
627177115Sremko	return (bus_generic_resume(dev));
628177115Sremko}
629177115Sremko
630171433Sanholt/**
631171433Sanholt * Sets the PCI resource size of the aperture on i830-class and below chipsets,
632171433Sanholt * while returning failure on later chipsets when an actual change is
633171433Sanholt * requested.
634171433Sanholt *
635171433Sanholt * This whole function is likely bogus, as the kernel would probably need to
636171433Sanholt * reconfigure the placement of the AGP aperture if a larger size is requested,
637171433Sanholt * which doesn't happen currently.
638171433Sanholt */
63963010Sdfrstatic int
64063010Sdfragp_i810_set_aperture(device_t dev, u_int32_t aperture)
64163010Sdfr{
64263010Sdfr	struct agp_i810_softc *sc = device_get_softc(dev);
643153031Sanholt	u_int16_t miscc, gcc1;
64463010Sdfr
645153031Sanholt	switch (sc->chiptype) {
646153031Sanholt	case CHIP_I810:
647103243Sanholt		/*
648103243Sanholt		 * Double check for sanity.
649103243Sanholt		 */
650103243Sanholt		if (aperture != 32 * 1024 * 1024 && aperture != 64 * 1024 * 1024) {
651103243Sanholt			device_printf(dev, "bad aperture size %d\n", aperture);
652103243Sanholt			return EINVAL;
653103243Sanholt		}
654153031Sanholt
655103243Sanholt		miscc = pci_read_config(sc->bdev, AGP_I810_MISCC, 2);
656103243Sanholt		miscc &= ~AGP_I810_MISCC_WINSIZE;
657103243Sanholt		if (aperture == 32 * 1024 * 1024)
658103243Sanholt			miscc |= AGP_I810_MISCC_WINSIZE_32;
659103243Sanholt		else
660103243Sanholt			miscc |= AGP_I810_MISCC_WINSIZE_64;
661103243Sanholt
662103243Sanholt		pci_write_config(sc->bdev, AGP_I810_MISCC, miscc, 2);
663153031Sanholt		break;
664153031Sanholt	case CHIP_I830:
665153031Sanholt		if (aperture != 64 * 1024 * 1024 &&
666153031Sanholt		    aperture != 128 * 1024 * 1024) {
667103243Sanholt			device_printf(dev, "bad aperture size %d\n", aperture);
668103243Sanholt			return EINVAL;
669103243Sanholt		}
670103243Sanholt		gcc1 = pci_read_config(sc->bdev, AGP_I830_GCC1, 2);
671103243Sanholt		gcc1 &= ~AGP_I830_GCC1_GMASIZE;
672103243Sanholt		if (aperture == 64 * 1024 * 1024)
673103243Sanholt			gcc1 |= AGP_I830_GCC1_GMASIZE_64;
674103243Sanholt		else
675103243Sanholt			gcc1 |= AGP_I830_GCC1_GMASIZE_128;
67663010Sdfr
677103243Sanholt		pci_write_config(sc->bdev, AGP_I830_GCC1, gcc1, 2);
678153031Sanholt		break;
679153031Sanholt	case CHIP_I855:
680153031Sanholt	case CHIP_I915:
681171433Sanholt	case CHIP_I965:
682171433Sanholt	case CHIP_G33:
683171433Sanholt		return agp_generic_set_aperture(dev, aperture);
684171433Sanholt	}
685153031Sanholt
686171433Sanholt	return 0;
687171433Sanholt}
688153031Sanholt
689171433Sanholt/**
690171433Sanholt * Writes a GTT entry mapping the page at the given offset from the beginning
691171433Sanholt * of the aperture to the given physical address.
692171433Sanholt */
693171433Sanholtstatic void
694171433Sanholtagp_i810_write_gtt_entry(device_t dev, int offset, vm_offset_t physical,
695171433Sanholt    int enabled)
696171433Sanholt{
697171433Sanholt	struct agp_i810_softc *sc = device_get_softc(dev);
698171433Sanholt	u_int32_t pte;
699171433Sanholt
700171433Sanholt	pte = (u_int32_t)physical | 1;
701171433Sanholt	if (sc->chiptype == CHIP_I965 || sc->chiptype == CHIP_G33) {
702171433Sanholt		pte |= (physical & 0x0000000f00000000ull) >> 28;
703171433Sanholt	} else {
704171433Sanholt		/* If we do actually have memory above 4GB on an older system,
705171433Sanholt		 * crash cleanly rather than scribble on system memory,
706171433Sanholt		 * so we know we need to fix it.
707171433Sanholt		 */
708171433Sanholt		KASSERT((pte & 0x0000000f00000000ull) == 0,
709171433Sanholt		    (">4GB physical address in agp"));
710171433Sanholt	}
711171433Sanholt
712171433Sanholt	switch (sc->chiptype) {
713171433Sanholt	case CHIP_I810:
714171433Sanholt	case CHIP_I830:
715171433Sanholt	case CHIP_I855:
716171433Sanholt		bus_write_4(sc->sc_res[0],
717171433Sanholt		    AGP_I810_GTT + (offset >> AGP_PAGE_SHIFT) * 4, pte);
718153031Sanholt		break;
719171433Sanholt	case CHIP_I915:
720171433Sanholt	case CHIP_G33:
721171433Sanholt		bus_write_4(sc->sc_res[1],
722171433Sanholt		    (offset >> AGP_PAGE_SHIFT) * 4, pte);
723171433Sanholt		break;
724171433Sanholt	case CHIP_I965:
725171433Sanholt		bus_write_4(sc->sc_res[0],
726171433Sanholt		    (offset >> AGP_PAGE_SHIFT) * 4 + (512 * 1024), pte);
727171433Sanholt		break;
728103243Sanholt	}
72963010Sdfr}
73063010Sdfr
73163010Sdfrstatic int
73263010Sdfragp_i810_bind_page(device_t dev, int offset, vm_offset_t physical)
73363010Sdfr{
73463010Sdfr	struct agp_i810_softc *sc = device_get_softc(dev);
73563010Sdfr
736103243Sanholt	if (offset < 0 || offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT)) {
737103243Sanholt		device_printf(dev, "failed: offset is 0x%08x, shift is %d, entries is %d\n", offset, AGP_PAGE_SHIFT, sc->gatt->ag_entries);
73863010Sdfr		return EINVAL;
739103243Sanholt	}
74063010Sdfr
741110785Sanholt	if ( sc->chiptype != CHIP_I810 ) {
742103243Sanholt		if ( (offset >> AGP_PAGE_SHIFT) < sc->stolen ) {
743103243Sanholt			device_printf(dev, "trying to bind into stolen memory");
744103243Sanholt			return EINVAL;
745103243Sanholt		}
746103243Sanholt	}
747103243Sanholt
748171433Sanholt	agp_i810_write_gtt_entry(dev, offset, physical, 1);
749153031Sanholt
75063010Sdfr	return 0;
75163010Sdfr}
75263010Sdfr
75363010Sdfrstatic int
75463010Sdfragp_i810_unbind_page(device_t dev, int offset)
75563010Sdfr{
75663010Sdfr	struct agp_i810_softc *sc = device_get_softc(dev);
75763010Sdfr
75863010Sdfr	if (offset < 0 || offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
75963010Sdfr		return EINVAL;
76063010Sdfr
761110785Sanholt	if ( sc->chiptype != CHIP_I810 ) {
762103272Sanholt		if ( (offset >> AGP_PAGE_SHIFT) < sc->stolen ) {
763103243Sanholt			device_printf(dev, "trying to unbind from stolen memory");
764103272Sanholt			return EINVAL;
765103272Sanholt		}
766103243Sanholt	}
767103243Sanholt
768171433Sanholt	agp_i810_write_gtt_entry(dev, offset, 0, 0);
769171433Sanholt
77063010Sdfr	return 0;
77163010Sdfr}
77263010Sdfr
77363010Sdfr/*
77463010Sdfr * Writing via memory mapped registers already flushes all TLBs.
77563010Sdfr */
77663010Sdfrstatic void
77763010Sdfragp_i810_flush_tlb(device_t dev)
77863010Sdfr{
77963010Sdfr}
78063010Sdfr
78163010Sdfrstatic int
78263010Sdfragp_i810_enable(device_t dev, u_int32_t mode)
78363010Sdfr{
78463010Sdfr
78563010Sdfr	return 0;
78663010Sdfr}
78763010Sdfr
78863010Sdfrstatic struct agp_memory *
78963010Sdfragp_i810_alloc_memory(device_t dev, int type, vm_size_t size)
79063010Sdfr{
79163010Sdfr	struct agp_i810_softc *sc = device_get_softc(dev);
79263010Sdfr	struct agp_memory *mem;
79363010Sdfr
79463010Sdfr	if ((size & (AGP_PAGE_SIZE - 1)) != 0)
79563010Sdfr		return 0;
79663010Sdfr
79763010Sdfr	if (sc->agp.as_allocated + size > sc->agp.as_maxmem)
79863010Sdfr		return 0;
79963010Sdfr
80063010Sdfr	if (type == 1) {
80163010Sdfr		/*
80263010Sdfr		 * Mapping local DRAM into GATT.
80363010Sdfr		 */
804110785Sanholt		if ( sc->chiptype != CHIP_I810 )
805103243Sanholt			return 0;
80663010Sdfr		if (size != sc->dcache_size)
80763010Sdfr			return 0;
80863010Sdfr	} else if (type == 2) {
80963010Sdfr		/*
810158655Sanholt		 * Type 2 is the contiguous physical memory type, that hands
811158655Sanholt		 * back a physical address.  This is used for cursors on i810.
812158655Sanholt		 * Hand back as many single pages with physical as the user
813158655Sanholt		 * wants, but only allow one larger allocation (ARGB cursor)
814158655Sanholt		 * for simplicity.
81563010Sdfr		 */
816158655Sanholt		if (size != AGP_PAGE_SIZE) {
817158655Sanholt			if (sc->argb_cursor != NULL)
818158655Sanholt				return 0;
819158655Sanholt
820158655Sanholt			/* Allocate memory for ARGB cursor, if we can. */
821158655Sanholt			sc->argb_cursor = contigmalloc(size, M_AGP,
822158655Sanholt			   0, 0, ~0, PAGE_SIZE, 0);
823158655Sanholt			if (sc->argb_cursor == NULL)
824158655Sanholt				return 0;
825158655Sanholt		}
82663010Sdfr	}
82763010Sdfr
828111119Simp	mem = malloc(sizeof *mem, M_AGP, M_WAITOK);
82963010Sdfr	mem->am_id = sc->agp.as_nextid++;
83063010Sdfr	mem->am_size = size;
83163010Sdfr	mem->am_type = type;
832158655Sanholt	if (type != 1 && (type != 2 || size == AGP_PAGE_SIZE))
83363010Sdfr		mem->am_obj = vm_object_allocate(OBJT_DEFAULT,
83463010Sdfr						 atop(round_page(size)));
83563010Sdfr	else
83663010Sdfr		mem->am_obj = 0;
83763010Sdfr
83863010Sdfr	if (type == 2) {
839158655Sanholt		if (size == AGP_PAGE_SIZE) {
840158655Sanholt			/*
841158655Sanholt			 * Allocate and wire down the page now so that we can
842158655Sanholt			 * get its physical address.
843158655Sanholt			 */
844158655Sanholt			vm_page_t m;
845158655Sanholt
846158655Sanholt			VM_OBJECT_LOCK(mem->am_obj);
847158655Sanholt			m = vm_page_grab(mem->am_obj, 0, VM_ALLOC_NOBUSY |
848158655Sanholt			    VM_ALLOC_WIRED | VM_ALLOC_ZERO | VM_ALLOC_RETRY);
849158655Sanholt			VM_OBJECT_UNLOCK(mem->am_obj);
850158655Sanholt			mem->am_physical = VM_PAGE_TO_PHYS(m);
851158655Sanholt		} else {
852158655Sanholt			/* Our allocation is already nicely wired down for us.
853158655Sanholt			 * Just grab the physical address.
854158655Sanholt			 */
855158655Sanholt			mem->am_physical = vtophys(sc->argb_cursor);
856158655Sanholt		}
85763010Sdfr	} else {
85863010Sdfr		mem->am_physical = 0;
85963010Sdfr	}
86063010Sdfr
86163010Sdfr	mem->am_offset = 0;
86263010Sdfr	mem->am_is_bound = 0;
86363010Sdfr	TAILQ_INSERT_TAIL(&sc->agp.as_memory, mem, am_link);
86463010Sdfr	sc->agp.as_allocated += size;
86563010Sdfr
86663010Sdfr	return mem;
86763010Sdfr}
86863010Sdfr
86963010Sdfrstatic int
87063010Sdfragp_i810_free_memory(device_t dev, struct agp_memory *mem)
87163010Sdfr{
87263010Sdfr	struct agp_i810_softc *sc = device_get_softc(dev);
87363010Sdfr
87463010Sdfr	if (mem->am_is_bound)
87563010Sdfr		return EBUSY;
87663010Sdfr
87763010Sdfr	if (mem->am_type == 2) {
878158655Sanholt		if (mem->am_size == AGP_PAGE_SIZE) {
879158655Sanholt			/*
880158655Sanholt			 * Unwire the page which we wired in alloc_memory.
881158655Sanholt			 */
882158655Sanholt			vm_page_t m;
883158655Sanholt
884158655Sanholt			VM_OBJECT_LOCK(mem->am_obj);
885158655Sanholt			m = vm_page_lookup(mem->am_obj, 0);
886158655Sanholt			VM_OBJECT_UNLOCK(mem->am_obj);
887158655Sanholt			vm_page_lock_queues();
888158655Sanholt			vm_page_unwire(m, 0);
889158655Sanholt			vm_page_unlock_queues();
890158655Sanholt		} else {
891158655Sanholt			contigfree(sc->argb_cursor, mem->am_size, M_AGP);
892158655Sanholt			sc->argb_cursor = NULL;
893158655Sanholt		}
89463010Sdfr	}
89563010Sdfr
89663010Sdfr	sc->agp.as_allocated -= mem->am_size;
89763010Sdfr	TAILQ_REMOVE(&sc->agp.as_memory, mem, am_link);
89863010Sdfr	if (mem->am_obj)
89963010Sdfr		vm_object_deallocate(mem->am_obj);
90063010Sdfr	free(mem, M_AGP);
90163010Sdfr	return 0;
90263010Sdfr}
90363010Sdfr
90463010Sdfrstatic int
90563010Sdfragp_i810_bind_memory(device_t dev, struct agp_memory *mem,
90663010Sdfr		     vm_offset_t offset)
90763010Sdfr{
90863010Sdfr	struct agp_i810_softc *sc = device_get_softc(dev);
90963010Sdfr	vm_offset_t i;
91063010Sdfr
911158655Sanholt	/* Do some sanity checks first. */
912158655Sanholt	if (offset < 0 || (offset & (AGP_PAGE_SIZE - 1)) != 0 ||
913158655Sanholt	    offset + mem->am_size > AGP_GET_APERTURE(dev)) {
914158655Sanholt		device_printf(dev, "binding memory at bad offset %#x\n",
915158655Sanholt		    (int)offset);
916158655Sanholt		return EINVAL;
917158655Sanholt	}
918158655Sanholt
919158655Sanholt	if (mem->am_type == 2 && mem->am_size != AGP_PAGE_SIZE) {
920158655Sanholt		mtx_lock(&sc->agp.as_lock);
921158655Sanholt		if (mem->am_is_bound) {
922158655Sanholt			mtx_unlock(&sc->agp.as_lock);
923158655Sanholt			return EINVAL;
924158655Sanholt		}
925158655Sanholt		/* The memory's already wired down, just stick it in the GTT. */
926158655Sanholt		for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) {
927171433Sanholt			agp_i810_write_gtt_entry(dev, offset + i,
928171433Sanholt			    mem->am_physical + i, 1);
929158655Sanholt		}
930158655Sanholt		agp_flush_cache();
931158655Sanholt		mem->am_offset = offset;
932158655Sanholt		mem->am_is_bound = 1;
933158655Sanholt		mtx_unlock(&sc->agp.as_lock);
934158655Sanholt		return 0;
935158655Sanholt	}
936158655Sanholt
93763010Sdfr	if (mem->am_type != 1)
93863010Sdfr		return agp_generic_bind_memory(dev, mem, offset);
93963010Sdfr
940110785Sanholt	if ( sc->chiptype != CHIP_I810 )
941103243Sanholt		return EINVAL;
942103243Sanholt
94363010Sdfr	for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) {
944171433Sanholt		bus_write_4(sc->sc_res[0],
945171433Sanholt		    AGP_I810_GTT + (i >> AGP_PAGE_SHIFT) * 4, i | 3);
94663010Sdfr	}
94763010Sdfr
94863010Sdfr	return 0;
94963010Sdfr}
95063010Sdfr
95163010Sdfrstatic int
95263010Sdfragp_i810_unbind_memory(device_t dev, struct agp_memory *mem)
95363010Sdfr{
95463010Sdfr	struct agp_i810_softc *sc = device_get_softc(dev);
95563010Sdfr	vm_offset_t i;
95663010Sdfr
957158655Sanholt	if (mem->am_type == 2 && mem->am_size != AGP_PAGE_SIZE) {
958158655Sanholt		mtx_lock(&sc->agp.as_lock);
959158655Sanholt		if (!mem->am_is_bound) {
960158655Sanholt			mtx_unlock(&sc->agp.as_lock);
961158655Sanholt			return EINVAL;
962158655Sanholt		}
963158655Sanholt
964158655Sanholt		for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) {
965171433Sanholt			agp_i810_write_gtt_entry(dev, mem->am_offset + i,
966171433Sanholt			    0, 0);
967158655Sanholt		}
968158655Sanholt		agp_flush_cache();
969158655Sanholt		mem->am_is_bound = 0;
970158655Sanholt		mtx_unlock(&sc->agp.as_lock);
971158655Sanholt		return 0;
972158655Sanholt	}
973158655Sanholt
97463010Sdfr	if (mem->am_type != 1)
97563010Sdfr		return agp_generic_unbind_memory(dev, mem);
97663010Sdfr
977110785Sanholt	if ( sc->chiptype != CHIP_I810 )
978103243Sanholt		return EINVAL;
979103243Sanholt
980171433Sanholt	for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) {
981171433Sanholt		bus_write_4(sc->sc_res[0],
982171433Sanholt		    AGP_I810_GTT + (i >> AGP_PAGE_SHIFT) * 4, 0);
983171433Sanholt	}
98463010Sdfr
98563010Sdfr	return 0;
98663010Sdfr}
98763010Sdfr
98863010Sdfrstatic device_method_t agp_i810_methods[] = {
98963010Sdfr	/* Device interface */
990155186Sjhb	DEVMETHOD(device_identify,	agp_i810_identify),
99163010Sdfr	DEVMETHOD(device_probe,		agp_i810_probe),
99263010Sdfr	DEVMETHOD(device_attach,	agp_i810_attach),
99363010Sdfr	DEVMETHOD(device_detach,	agp_i810_detach),
994177115Sremko	DEVMETHOD(device_suspend,	bus_generic_suspend),
995177115Sremko	DEVMETHOD(device_resume,	agp_i810_resume),
99663010Sdfr
99763010Sdfr	/* AGP interface */
998171433Sanholt	DEVMETHOD(agp_get_aperture,	agp_generic_get_aperture),
99963010Sdfr	DEVMETHOD(agp_set_aperture,	agp_i810_set_aperture),
100063010Sdfr	DEVMETHOD(agp_bind_page,	agp_i810_bind_page),
100163010Sdfr	DEVMETHOD(agp_unbind_page,	agp_i810_unbind_page),
100263010Sdfr	DEVMETHOD(agp_flush_tlb,	agp_i810_flush_tlb),
100363010Sdfr	DEVMETHOD(agp_enable,		agp_i810_enable),
100463010Sdfr	DEVMETHOD(agp_alloc_memory,	agp_i810_alloc_memory),
100563010Sdfr	DEVMETHOD(agp_free_memory,	agp_i810_free_memory),
100663010Sdfr	DEVMETHOD(agp_bind_memory,	agp_i810_bind_memory),
100763010Sdfr	DEVMETHOD(agp_unbind_memory,	agp_i810_unbind_memory),
100863010Sdfr
100963010Sdfr	{ 0, 0 }
101063010Sdfr};
101163010Sdfr
101263010Sdfrstatic driver_t agp_i810_driver = {
101363010Sdfr	"agp",
101463010Sdfr	agp_i810_methods,
101563010Sdfr	sizeof(struct agp_i810_softc),
101663010Sdfr};
101763010Sdfr
101863010Sdfrstatic devclass_t agp_devclass;
101963010Sdfr
1020153579SjhbDRIVER_MODULE(agp_i810, vgapci, agp_i810_driver, agp_devclass, 0, 0);
1021113506SmdoddMODULE_DEPEND(agp_i810, agp, 1, 1, 1);
1022113506SmdoddMODULE_DEPEND(agp_i810, pci, 1, 1, 1);
1023