agp_i810.c revision 153031
1183863Snwhitehorn/*-
2183863Snwhitehorn * Copyright (c) 2000 Doug Rabson
3183863Snwhitehorn * Copyright (c) 2000 Ruslan Ermilov
4183863Snwhitehorn * All rights reserved.
5183863Snwhitehorn *
6183863Snwhitehorn * Redistribution and use in source and binary forms, with or without
7183863Snwhitehorn * modification, are permitted provided that the following conditions
8183863Snwhitehorn * are met:
9183863Snwhitehorn * 1. Redistributions of source code must retain the above copyright
10183863Snwhitehorn *    notice, this list of conditions and the following disclaimer.
11183863Snwhitehorn * 2. Redistributions in binary form must reproduce the above copyright
12183863Snwhitehorn *    notice, this list of conditions and the following disclaimer in the
13183863Snwhitehorn *    documentation and/or other materials provided with the distribution.
14183863Snwhitehorn *
15183863Snwhitehorn * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16183863Snwhitehorn * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17183863Snwhitehorn * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18183863Snwhitehorn * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28/*
29 * Fixes for 830/845G support: David Dawes <dawes@xfree86.org>
30 * 852GM/855GM/865G support added by David Dawes <dawes@xfree86.org>
31 */
32
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD: head/sys/dev/agp/agp_i810.c 153031 2005-12-02 23:51:36Z anholt $");
35
36#include "opt_bus.h"
37
38#include <sys/param.h>
39#include <sys/systm.h>
40#include <sys/malloc.h>
41#include <sys/kernel.h>
42#include <sys/module.h>
43#include <sys/bus.h>
44#include <sys/lock.h>
45#include <sys/mutex.h>
46#include <sys/proc.h>
47
48#include <dev/pci/pcivar.h>
49#include <dev/pci/pcireg.h>
50#include <pci/agppriv.h>
51#include <pci/agpreg.h>
52
53#include <vm/vm.h>
54#include <vm/vm_object.h>
55#include <vm/vm_page.h>
56#include <vm/vm_pageout.h>
57#include <vm/pmap.h>
58
59#include <machine/bus.h>
60#include <machine/resource.h>
61#include <sys/rman.h>
62
63MALLOC_DECLARE(M_AGP);
64
65#define READ1(off)	bus_space_read_1(sc->bst, sc->bsh, off)
66#define READ4(off)	bus_space_read_4(sc->bst, sc->bsh, off)
67#define WRITE4(off,v)	bus_space_write_4(sc->bst, sc->bsh, off, v)
68#define WRITEGTT(off,v)	bus_space_write_4(sc->gtt_bst, sc->gtt_bsh, off, v)
69
70#define CHIP_I810 0	/* i810/i815 */
71#define CHIP_I830 1	/* 830M/845G */
72#define CHIP_I855 2	/* 852GM/855GM/865G */
73#define CHIP_I915 3	/* 915G/915GM */
74
75struct agp_i810_softc {
76	struct agp_softc agp;
77	u_int32_t initial_aperture;	/* aperture size at startup */
78	struct agp_gatt *gatt;
79	int chiptype;			/* i810-like or i830 */
80	u_int32_t dcache_size;		/* i810 only */
81	u_int32_t stolen;		/* number of i830/845 gtt entries for stolen memory */
82	device_t bdev;			/* bridge device */
83
84	struct resource *regs;		/* memory mapped GC registers */
85	bus_space_tag_t bst;		/* bus_space tag */
86	bus_space_handle_t bsh;		/* bus_space handle */
87
88	struct resource *gtt;		/* memory mapped GATT entries */
89	bus_space_tag_t gtt_bst;	/* bus_space tag */
90	bus_space_handle_t gtt_bsh;	/* bus_space handle */
91};
92
93static const char*
94agp_i810_match(device_t dev)
95{
96	if (pci_get_class(dev) != PCIC_DISPLAY
97	    || pci_get_subclass(dev) != PCIS_DISPLAY_VGA)
98		return NULL;
99
100	switch (pci_get_devid(dev)) {
101	case 0x71218086:
102		return ("Intel 82810 (i810 GMCH) SVGA controller");
103
104	case 0x71238086:
105		return ("Intel 82810-DC100 (i810-DC100 GMCH) SVGA controller");
106
107	case 0x71258086:
108		return ("Intel 82810E (i810E GMCH) SVGA controller");
109
110	case 0x11328086:
111		return ("Intel 82815 (i815 GMCH) SVGA controller");
112
113	case 0x35778086:
114		return ("Intel 82830M (830M GMCH) SVGA controller");
115
116	case 0x25628086:
117		return ("Intel 82845G (845G GMCH) SVGA controller");
118
119	case 0x35828086:
120		switch (pci_read_config(dev, AGP_I85X_CAPID, 1)) {
121		case AGP_I855_GME:
122			return ("Intel 82855GME (855GME GMCH) SVGA controller");
123
124		case AGP_I855_GM:
125			return ("Intel 82855GM (855GM GMCH) SVGA controller");
126
127		case AGP_I852_GME:
128			return ("Intel 82852GME (852GME GMCH) SVGA controller");
129
130		case AGP_I852_GM:
131			return ("Intel 82852GM (852GM GMCH) SVGA controller");
132
133		default:
134			return ("Intel 8285xM (85xGM GMCH) SVGA controller");
135		}
136
137	case 0x25728086:
138		return ("Intel 82865G (865G GMCH) SVGA controller");
139
140	case 0x25828086:
141		return ("Intel 82915G (915G GMCH) SVGA controller");
142
143	case 0x25928086:
144		return ("Intel 82915GM (915GM GMCH) SVGA controller");
145	};
146
147	return NULL;
148}
149
150/*
151 * Find bridge device.
152 */
153static device_t
154agp_i810_find_bridge(device_t dev)
155{
156	device_t *children, child;
157	int nchildren, i;
158	u_int32_t devid;
159
160	/*
161	 * Calculate bridge device's ID.
162	 */
163	devid = pci_get_devid(dev);
164	switch (devid) {
165	case 0x71218086:
166	case 0x71238086:
167	case 0x71258086:
168		devid -= 0x10000;
169		break;
170
171	case 0x11328086:
172	case 0x35778086:
173	case 0x25628086:
174	case 0x35828086:
175	case 0x25728086:
176	case 0x25828086:
177	case 0x25928086:
178		devid -= 0x20000;
179		break;
180	};
181	if (device_get_children(device_get_parent(dev), &children, &nchildren))
182		return 0;
183
184	for (i = 0; i < nchildren; i++) {
185		child = children[i];
186
187		if (pci_get_devid(child) == devid) {
188			free(children, M_TEMP);
189			return child;
190		}
191	}
192	free(children, M_TEMP);
193	return 0;
194}
195
196static int
197agp_i810_probe(device_t dev)
198{
199	const char *desc;
200
201	if (resource_disabled("agp", device_get_unit(dev)))
202		return (ENXIO);
203	desc = agp_i810_match(dev);
204	if (desc) {
205		device_t bdev;
206		u_int8_t smram;
207		unsigned int gcc1;
208		int devid = pci_get_devid(dev);
209
210		bdev = agp_i810_find_bridge(dev);
211		if (!bdev) {
212			if (bootverbose)
213				printf("I810: can't find bridge device\n");
214			return ENXIO;
215		}
216
217		/*
218		 * checking whether internal graphics device has been activated.
219		 */
220		switch (devid) {
221			/* i810 */
222		case 0x71218086:
223		case 0x71238086:
224		case 0x71258086:
225		case 0x11328086:
226			smram = pci_read_config(bdev, AGP_I810_SMRAM, 1);
227			if ((smram & AGP_I810_SMRAM_GMS)
228			    == AGP_I810_SMRAM_GMS_DISABLED) {
229				if (bootverbose)
230					printf("I810: disabled, not probing\n");
231				return ENXIO;
232			}
233			break;
234
235			/* i830 */
236		case 0x35778086:
237		case 0x35828086:
238		case 0x25628086:
239		case 0x25728086:
240			gcc1 = pci_read_config(bdev, AGP_I830_GCC1, 1);
241			if ((gcc1 & AGP_I830_GCC1_DEV2) == AGP_I830_GCC1_DEV2_DISABLED) {
242				if (bootverbose)
243					printf("I830: disabled, not probing\n");
244				return ENXIO;
245			}
246			break;
247
248			/* i915 */
249		case 0x25828086:
250		case 0x25928086:
251			gcc1 = pci_read_config(bdev, AGP_I915_DEVEN, 4);
252			if ((gcc1 & AGP_I915_DEVEN_D2F0) ==
253			    AGP_I915_DEVEN_D2F0_DISABLED) {
254				if (bootverbose)
255					printf("I915: disabled, not probing\n");
256				return ENXIO;
257			}
258			break;
259
260		default:
261			return ENXIO;
262		}
263
264		device_verbose(dev);
265		device_set_desc(dev, desc);
266		return BUS_PROBE_DEFAULT;
267	}
268
269	return ENXIO;
270}
271
272static int
273agp_i810_attach(device_t dev)
274{
275	struct agp_i810_softc *sc = device_get_softc(dev);
276	struct agp_gatt *gatt;
277	int error, rid;
278
279	sc->bdev = agp_i810_find_bridge(dev);
280	if (!sc->bdev)
281		return ENOENT;
282
283	error = agp_generic_attach(dev);
284	if (error)
285		return error;
286
287	switch (pci_get_devid(dev)) {
288	case 0x71218086:
289	case 0x71238086:
290	case 0x71258086:
291	case 0x11328086:
292		sc->chiptype = CHIP_I810;
293		break;
294	case 0x35778086:
295	case 0x25628086:
296		sc->chiptype = CHIP_I830;
297		break;
298	case 0x35828086:
299	case 0x25728086:
300		sc->chiptype = CHIP_I855;
301		break;
302	case 0x25828086:
303	case 0x25928086:
304		sc->chiptype = CHIP_I915;
305		break;
306	};
307
308	/* Same for i810 and i830 */
309	if (sc->chiptype == CHIP_I915)
310		rid = AGP_I915_MMADR;
311	else
312		rid = AGP_I810_MMADR;
313
314	sc->regs = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
315					  RF_ACTIVE);
316	if (!sc->regs) {
317		agp_generic_detach(dev);
318		return ENODEV;
319	}
320	sc->bst = rman_get_bustag(sc->regs);
321	sc->bsh = rman_get_bushandle(sc->regs);
322
323	if (sc->chiptype == CHIP_I915) {
324		rid = AGP_I915_GTTADR;
325		sc->gtt = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
326						 RF_ACTIVE);
327		if (!sc->gtt) {
328			bus_release_resource(dev, SYS_RES_MEMORY,
329					     AGP_I810_MMADR, sc->regs);
330			agp_generic_detach(dev);
331			return ENODEV;
332		}
333		sc->gtt_bst = rman_get_bustag(sc->gtt);
334		sc->gtt_bsh = rman_get_bushandle(sc->gtt);
335	}
336
337	sc->initial_aperture = AGP_GET_APERTURE(dev);
338
339	gatt = malloc( sizeof(struct agp_gatt), M_AGP, M_NOWAIT);
340	if (!gatt) {
341 		agp_generic_detach(dev);
342 		return ENOMEM;
343	}
344	sc->gatt = gatt;
345
346	gatt->ag_entries = AGP_GET_APERTURE(dev) >> AGP_PAGE_SHIFT;
347
348	if ( sc->chiptype == CHIP_I810 ) {
349		/* Some i810s have on-chip memory called dcache */
350		if (READ1(AGP_I810_DRT) & AGP_I810_DRT_POPULATED)
351			sc->dcache_size = 4 * 1024 * 1024;
352		else
353			sc->dcache_size = 0;
354
355		/* According to the specs the gatt on the i810 must be 64k */
356		gatt->ag_virtual = contigmalloc( 64 * 1024, M_AGP, 0,
357					0, ~0, PAGE_SIZE, 0);
358		if (!gatt->ag_virtual) {
359			if (bootverbose)
360				device_printf(dev, "contiguous allocation failed\n");
361			free(gatt, M_AGP);
362			agp_generic_detach(dev);
363			return ENOMEM;
364		}
365		bzero(gatt->ag_virtual, gatt->ag_entries * sizeof(u_int32_t));
366
367		gatt->ag_physical = vtophys((vm_offset_t) gatt->ag_virtual);
368		agp_flush_cache();
369		/* Install the GATT. */
370		WRITE4(AGP_I810_PGTBL_CTL, gatt->ag_physical | 1);
371	} else if ( sc->chiptype == CHIP_I830 ) {
372		/* The i830 automatically initializes the 128k gatt on boot. */
373		unsigned int gcc1, pgtblctl;
374
375		gcc1 = pci_read_config(sc->bdev, AGP_I830_GCC1, 1);
376		switch (gcc1 & AGP_I830_GCC1_GMS) {
377			case AGP_I830_GCC1_GMS_STOLEN_512:
378				sc->stolen = (512 - 132) * 1024 / 4096;
379				break;
380			case AGP_I830_GCC1_GMS_STOLEN_1024:
381				sc->stolen = (1024 - 132) * 1024 / 4096;
382				break;
383			case AGP_I830_GCC1_GMS_STOLEN_8192:
384				sc->stolen = (8192 - 132) * 1024 / 4096;
385				break;
386			default:
387				sc->stolen = 0;
388				device_printf(dev, "unknown memory configuration, disabling\n");
389				agp_generic_detach(dev);
390				return EINVAL;
391		}
392		if (sc->stolen > 0)
393			device_printf(dev, "detected %dk stolen memory\n", sc->stolen * 4);
394		device_printf(dev, "aperture size is %dM\n", sc->initial_aperture / 1024 / 1024);
395
396		/* GATT address is already in there, make sure it's enabled */
397		pgtblctl = READ4(AGP_I810_PGTBL_CTL);
398		pgtblctl |= 1;
399		WRITE4(AGP_I810_PGTBL_CTL, pgtblctl);
400
401		gatt->ag_physical = pgtblctl & ~1;
402	} else if (sc->chiptype == CHIP_I855 || sc->chiptype == CHIP_I915) {	/* CHIP_I855 */
403		unsigned int gcc1, pgtblctl, stolen;
404
405		/* Stolen memory is set up at the beginning of the aperture by
406		 * the BIOS, consisting of the GATT followed by 4kb for the BIOS
407		 * display.
408		 */
409		if (sc->chiptype == CHIP_I855)
410			stolen = 132;
411		else
412			stolen = 260;
413
414		gcc1 = pci_read_config(sc->bdev, AGP_I855_GCC1, 1);
415		switch (gcc1 & AGP_I855_GCC1_GMS) {
416			case AGP_I855_GCC1_GMS_STOLEN_1M:
417				sc->stolen = (1024 - stolen) * 1024 / 4096;
418				break;
419			case AGP_I855_GCC1_GMS_STOLEN_4M:
420				sc->stolen = (4096 - stolen) * 1024 / 4096;
421				break;
422			case AGP_I855_GCC1_GMS_STOLEN_8M:
423				sc->stolen = (8192 - stolen) * 1024 / 4096;
424				break;
425			case AGP_I855_GCC1_GMS_STOLEN_16M:
426				sc->stolen = (16384 - stolen) * 1024 / 4096;
427				break;
428			case AGP_I855_GCC1_GMS_STOLEN_32M:
429				sc->stolen = (32768 - stolen) * 1024 / 4096;
430				break;
431			case AGP_I915_GCC1_GMS_STOLEN_48M:
432				sc->stolen = (49152 - stolen) * 1024 / 4096;
433				break;
434			case AGP_I915_GCC1_GMS_STOLEN_64M:
435				sc->stolen = (65536 - stolen) * 1024 / 4096;
436				break;
437			default:
438				sc->stolen = 0;
439				device_printf(dev, "unknown memory configuration, disabling\n");
440				agp_generic_detach(dev);
441				return EINVAL;
442		}
443		if (sc->stolen > 0)
444			device_printf(dev, "detected %dk stolen memory\n", sc->stolen * 4);
445		device_printf(dev, "aperture size is %dM\n", sc->initial_aperture / 1024 / 1024);
446
447		/* GATT address is already in there, make sure it's enabled */
448		pgtblctl = READ4(AGP_I810_PGTBL_CTL);
449		pgtblctl |= 1;
450		WRITE4(AGP_I810_PGTBL_CTL, pgtblctl);
451
452		gatt->ag_physical = pgtblctl & ~1;
453	}
454
455	/* Add a device for the drm to attach to */
456	if (!device_add_child( dev, "drmsub", -1 ))
457		printf("out of memory...\n");
458
459	return bus_generic_attach(dev);
460}
461
462static int
463agp_i810_detach(device_t dev)
464{
465	struct agp_i810_softc *sc = device_get_softc(dev);
466	int error;
467	device_t child;
468
469	error = agp_generic_detach(dev);
470	if (error)
471		return error;
472
473	/* Clear the GATT base. */
474	if ( sc->chiptype == CHIP_I810 ) {
475		WRITE4(AGP_I810_PGTBL_CTL, 0);
476	} else {
477		unsigned int pgtblctl;
478		pgtblctl = READ4(AGP_I810_PGTBL_CTL);
479		pgtblctl &= ~1;
480		WRITE4(AGP_I810_PGTBL_CTL, pgtblctl);
481	}
482
483	/* Put the aperture back the way it started. */
484	AGP_SET_APERTURE(dev, sc->initial_aperture);
485
486	if ( sc->chiptype == CHIP_I810 ) {
487		contigfree(sc->gatt->ag_virtual, 64 * 1024, M_AGP);
488	}
489	free(sc->gatt, M_AGP);
490
491	if (sc->chiptype == CHIP_I915) {
492		bus_release_resource(dev, SYS_RES_MEMORY, AGP_I915_GTTADR,
493				     sc->gtt);
494		bus_release_resource(dev, SYS_RES_MEMORY, AGP_I915_MMADR,
495				     sc->regs);
496	} else {
497		bus_release_resource(dev, SYS_RES_MEMORY, AGP_I810_MMADR,
498				     sc->regs);
499	}
500
501	child = device_find_child( dev, "drmsub", 0 );
502	if (child)
503	   device_delete_child( dev, child );
504
505	return 0;
506}
507
508static u_int32_t
509agp_i810_get_aperture(device_t dev)
510{
511	struct agp_i810_softc *sc = device_get_softc(dev);
512	uint32_t temp;
513	u_int16_t miscc;
514
515	switch (sc->chiptype) {
516	case CHIP_I810:
517		miscc = pci_read_config(sc->bdev, AGP_I810_MISCC, 2);
518		if ((miscc & AGP_I810_MISCC_WINSIZE) == AGP_I810_MISCC_WINSIZE_32)
519			return 32 * 1024 * 1024;
520		else
521			return 64 * 1024 * 1024;
522	case CHIP_I830:
523		temp = pci_read_config(sc->bdev, AGP_I830_GCC1, 2);
524		if ((temp & AGP_I830_GCC1_GMASIZE) == AGP_I830_GCC1_GMASIZE_64)
525			return 64 * 1024 * 1024;
526		else
527			return 128 * 1024 * 1024;
528	case CHIP_I855:
529		return 128 * 1024 * 1024;
530	case CHIP_I915:
531		temp = pci_read_config(dev, AGP_I915_MSAC, 1);
532		if ((temp & AGP_I915_MSAC_GMASIZE) ==
533		    AGP_I915_MSAC_GMASIZE_128) {
534			return 128 * 1024 * 1024;
535		} else {
536			return 256 * 1024 * 1024;
537		}
538	}
539
540	return 0;
541}
542
543static int
544agp_i810_set_aperture(device_t dev, u_int32_t aperture)
545{
546	struct agp_i810_softc *sc = device_get_softc(dev);
547	u_int16_t miscc, gcc1;
548	u_int32_t temp;
549
550	switch (sc->chiptype) {
551	case CHIP_I810:
552		/*
553		 * Double check for sanity.
554		 */
555		if (aperture != 32 * 1024 * 1024 && aperture != 64 * 1024 * 1024) {
556			device_printf(dev, "bad aperture size %d\n", aperture);
557			return EINVAL;
558		}
559
560		miscc = pci_read_config(sc->bdev, AGP_I810_MISCC, 2);
561		miscc &= ~AGP_I810_MISCC_WINSIZE;
562		if (aperture == 32 * 1024 * 1024)
563			miscc |= AGP_I810_MISCC_WINSIZE_32;
564		else
565			miscc |= AGP_I810_MISCC_WINSIZE_64;
566
567		pci_write_config(sc->bdev, AGP_I810_MISCC, miscc, 2);
568		break;
569	case CHIP_I830:
570		if (aperture != 64 * 1024 * 1024 &&
571		    aperture != 128 * 1024 * 1024) {
572			device_printf(dev, "bad aperture size %d\n", aperture);
573			return EINVAL;
574		}
575		gcc1 = pci_read_config(sc->bdev, AGP_I830_GCC1, 2);
576		gcc1 &= ~AGP_I830_GCC1_GMASIZE;
577		if (aperture == 64 * 1024 * 1024)
578			gcc1 |= AGP_I830_GCC1_GMASIZE_64;
579		else
580			gcc1 |= AGP_I830_GCC1_GMASIZE_128;
581
582		pci_write_config(sc->bdev, AGP_I830_GCC1, gcc1, 2);
583		break;
584	case CHIP_I855:
585		if (aperture != 128 * 1024 * 1024) {
586			device_printf(dev, "bad aperture size %d\n", aperture);
587			return EINVAL;
588		}
589		break;
590	case CHIP_I915:
591		temp = pci_read_config(dev, AGP_I915_MSAC, 1);
592		temp &= ~AGP_I915_MSAC_GMASIZE;
593
594		switch (aperture) {
595		case 128 * 1024 * 1024:
596			temp |= AGP_I915_MSAC_GMASIZE_128;
597			break;
598		case 256 * 1024 * 1024:
599			temp |= AGP_I915_MSAC_GMASIZE_256;
600			break;
601		default:
602			device_printf(dev, "bad aperture size %d\n", aperture);
603			return EINVAL;
604		}
605
606		pci_write_config(dev, AGP_I915_MSAC, temp, 1);
607		break;
608	}
609
610	return 0;
611}
612
613static int
614agp_i810_bind_page(device_t dev, int offset, vm_offset_t physical)
615{
616	struct agp_i810_softc *sc = device_get_softc(dev);
617
618	if (offset < 0 || offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT)) {
619		device_printf(dev, "failed: offset is 0x%08x, shift is %d, entries is %d\n", offset, AGP_PAGE_SHIFT, sc->gatt->ag_entries);
620		return EINVAL;
621	}
622
623	if ( sc->chiptype != CHIP_I810 ) {
624		if ( (offset >> AGP_PAGE_SHIFT) < sc->stolen ) {
625			device_printf(dev, "trying to bind into stolen memory");
626			return EINVAL;
627		}
628	}
629
630	if (sc->chiptype == CHIP_I915) {
631		WRITEGTT((offset >> AGP_PAGE_SHIFT) * 4, physical | 1);
632	} else {
633		WRITE4(AGP_I810_GTT + (offset >> AGP_PAGE_SHIFT) * 4, physical | 1);
634	}
635
636	return 0;
637}
638
639static int
640agp_i810_unbind_page(device_t dev, int offset)
641{
642	struct agp_i810_softc *sc = device_get_softc(dev);
643
644	if (offset < 0 || offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
645		return EINVAL;
646
647	if ( sc->chiptype != CHIP_I810 ) {
648		if ( (offset >> AGP_PAGE_SHIFT) < sc->stolen ) {
649			device_printf(dev, "trying to unbind from stolen memory");
650			return EINVAL;
651		}
652	}
653
654	if (sc->chiptype == CHIP_I915) {
655		WRITEGTT((offset >> AGP_PAGE_SHIFT) * 4, 0);
656	} else {
657		WRITE4(AGP_I810_GTT + (offset >> AGP_PAGE_SHIFT) * 4, 0);
658	}
659
660	return 0;
661}
662
663/*
664 * Writing via memory mapped registers already flushes all TLBs.
665 */
666static void
667agp_i810_flush_tlb(device_t dev)
668{
669}
670
671static int
672agp_i810_enable(device_t dev, u_int32_t mode)
673{
674
675	return 0;
676}
677
678static struct agp_memory *
679agp_i810_alloc_memory(device_t dev, int type, vm_size_t size)
680{
681	struct agp_i810_softc *sc = device_get_softc(dev);
682	struct agp_memory *mem;
683
684	if ((size & (AGP_PAGE_SIZE - 1)) != 0)
685		return 0;
686
687	if (sc->agp.as_allocated + size > sc->agp.as_maxmem)
688		return 0;
689
690	if (type == 1) {
691		/*
692		 * Mapping local DRAM into GATT.
693		 */
694		if ( sc->chiptype != CHIP_I810 )
695			return 0;
696		if (size != sc->dcache_size)
697			return 0;
698	} else if (type == 2) {
699		/*
700		 * Bogus mapping of a single page for the hardware cursor.
701		 */
702		if (size != AGP_PAGE_SIZE)
703			return 0;
704	}
705
706	mem = malloc(sizeof *mem, M_AGP, M_WAITOK);
707	mem->am_id = sc->agp.as_nextid++;
708	mem->am_size = size;
709	mem->am_type = type;
710	if (type != 1)
711		mem->am_obj = vm_object_allocate(OBJT_DEFAULT,
712						 atop(round_page(size)));
713	else
714		mem->am_obj = 0;
715
716	if (type == 2) {
717		/*
718		 * Allocate and wire down the page now so that we can
719		 * get its physical address.
720		 */
721		vm_page_t m;
722
723		VM_OBJECT_LOCK(mem->am_obj);
724		m = vm_page_grab(mem->am_obj, 0, VM_ALLOC_NOBUSY |
725		    VM_ALLOC_WIRED | VM_ALLOC_ZERO | VM_ALLOC_RETRY);
726		VM_OBJECT_UNLOCK(mem->am_obj);
727		mem->am_physical = VM_PAGE_TO_PHYS(m);
728	} else {
729		mem->am_physical = 0;
730	}
731
732	mem->am_offset = 0;
733	mem->am_is_bound = 0;
734	TAILQ_INSERT_TAIL(&sc->agp.as_memory, mem, am_link);
735	sc->agp.as_allocated += size;
736
737	return mem;
738}
739
740static int
741agp_i810_free_memory(device_t dev, struct agp_memory *mem)
742{
743	struct agp_i810_softc *sc = device_get_softc(dev);
744
745	if (mem->am_is_bound)
746		return EBUSY;
747
748	if (mem->am_type == 2) {
749		/*
750		 * Unwire the page which we wired in alloc_memory.
751		 */
752		vm_page_t m;
753
754		VM_OBJECT_LOCK(mem->am_obj);
755		m = vm_page_lookup(mem->am_obj, 0);
756		VM_OBJECT_UNLOCK(mem->am_obj);
757		vm_page_lock_queues();
758		vm_page_unwire(m, 0);
759		vm_page_unlock_queues();
760	}
761
762	sc->agp.as_allocated -= mem->am_size;
763	TAILQ_REMOVE(&sc->agp.as_memory, mem, am_link);
764	if (mem->am_obj)
765		vm_object_deallocate(mem->am_obj);
766	free(mem, M_AGP);
767	return 0;
768}
769
770static int
771agp_i810_bind_memory(device_t dev, struct agp_memory *mem,
772		     vm_offset_t offset)
773{
774	struct agp_i810_softc *sc = device_get_softc(dev);
775	vm_offset_t i;
776
777	if (mem->am_type != 1)
778		return agp_generic_bind_memory(dev, mem, offset);
779
780	if ( sc->chiptype != CHIP_I810 )
781		return EINVAL;
782
783	for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) {
784		WRITE4(AGP_I810_GTT + (offset >> AGP_PAGE_SHIFT) * 4,
785		       i | 3);
786	}
787
788	return 0;
789}
790
791static int
792agp_i810_unbind_memory(device_t dev, struct agp_memory *mem)
793{
794	struct agp_i810_softc *sc = device_get_softc(dev);
795	vm_offset_t i;
796
797	if (mem->am_type != 1)
798		return agp_generic_unbind_memory(dev, mem);
799
800	if ( sc->chiptype != CHIP_I810 )
801		return EINVAL;
802
803	for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE)
804		WRITE4(AGP_I810_GTT + (i >> AGP_PAGE_SHIFT) * 4, 0);
805
806	return 0;
807}
808
809static int
810agp_i810_print_child(device_t dev, device_t child)
811{
812	int retval = 0;
813
814	retval += bus_print_child_header(dev, child);
815	retval += printf(": (child of agp_i810.c)");
816	retval += bus_print_child_footer(dev, child);
817
818	return retval;
819}
820
821static device_method_t agp_i810_methods[] = {
822	/* Device interface */
823	DEVMETHOD(device_probe,		agp_i810_probe),
824	DEVMETHOD(device_attach,	agp_i810_attach),
825	DEVMETHOD(device_detach,	agp_i810_detach),
826	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
827	DEVMETHOD(device_suspend,	bus_generic_suspend),
828	DEVMETHOD(device_resume,	bus_generic_resume),
829
830	/* AGP interface */
831	DEVMETHOD(agp_get_aperture,	agp_i810_get_aperture),
832	DEVMETHOD(agp_set_aperture,	agp_i810_set_aperture),
833	DEVMETHOD(agp_bind_page,	agp_i810_bind_page),
834	DEVMETHOD(agp_unbind_page,	agp_i810_unbind_page),
835	DEVMETHOD(agp_flush_tlb,	agp_i810_flush_tlb),
836	DEVMETHOD(agp_enable,		agp_i810_enable),
837	DEVMETHOD(agp_alloc_memory,	agp_i810_alloc_memory),
838	DEVMETHOD(agp_free_memory,	agp_i810_free_memory),
839	DEVMETHOD(agp_bind_memory,	agp_i810_bind_memory),
840	DEVMETHOD(agp_unbind_memory,	agp_i810_unbind_memory),
841
842	/* bus methods */
843	DEVMETHOD(bus_print_child,	agp_i810_print_child),
844	DEVMETHOD(bus_alloc_resource,	bus_generic_alloc_resource),
845	DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
846	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
847	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
848	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
849	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
850	{ 0, 0 }
851};
852
853static driver_t agp_i810_driver = {
854	"agp",
855	agp_i810_methods,
856	sizeof(struct agp_i810_softc),
857};
858
859static devclass_t agp_devclass;
860
861DRIVER_MODULE(agp_i810, pci, agp_i810_driver, agp_devclass, 0, 0);
862MODULE_DEPEND(agp_i810, agp, 1, 1, 1);
863MODULE_DEPEND(agp_i810, pci, 1, 1, 1);
864