xref: /freebsd/sys/dev/agp/agp_i810.c (revision b28624fde638caadd4a89f50c9b7e7da0f98c4d2)
1 /*-
2  * Copyright (c) 2000 Doug Rabson
3  * Copyright (c) 2000 Ruslan Ermilov
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * 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$");
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 <machine/md_var.h>
62 #include <sys/rman.h>
63 
64 MALLOC_DECLARE(M_AGP);
65 
66 enum {
67 	CHIP_I810,	/* i810/i815 */
68 	CHIP_I830,	/* 830M/845G */
69 	CHIP_I855,	/* 852GM/855GM/865G */
70 	CHIP_I915,	/* 915G/915GM */
71 	CHIP_I965,	/* G965 */
72 	CHIP_G33,	/* G33/Q33/Q35 */
73 };
74 
75 /* The i810 through i855 have the registers at BAR 1, and the GATT gets
76  * allocated by us.  The i915 has registers in BAR 0 and the GATT is at the
77  * start of the stolen memory, and should only be accessed by the OS through
78  * BAR 3.  The G965 has registers and GATT in the same BAR (0) -- first 512KB
79  * is registers, second 512KB is GATT.
80  */
81 static struct resource_spec agp_i810_res_spec[] = {
82 	{ SYS_RES_MEMORY, AGP_I810_MMADR, RF_ACTIVE | RF_SHAREABLE },
83 	{ -1, 0 }
84 };
85 
86 static struct resource_spec agp_i915_res_spec[] = {
87 	{ SYS_RES_MEMORY, AGP_I915_MMADR, RF_ACTIVE | RF_SHAREABLE },
88 	{ SYS_RES_MEMORY, AGP_I915_GTTADR, RF_ACTIVE | RF_SHAREABLE },
89 	{ -1, 0 }
90 };
91 
92 static struct resource_spec agp_i965_res_spec[] = {
93 	{ SYS_RES_MEMORY, AGP_I965_GTTMMADR, RF_ACTIVE | RF_SHAREABLE },
94 	{ -1, 0 }
95 };
96 
97 struct agp_i810_softc {
98 	struct agp_softc agp;
99 	u_int32_t initial_aperture;	/* aperture size at startup */
100 	struct agp_gatt *gatt;
101 	int chiptype;			/* i810-like or i830 */
102 	u_int32_t dcache_size;		/* i810 only */
103 	u_int32_t stolen;		/* number of i830/845 gtt entries for stolen memory */
104 	device_t bdev;			/* bridge device */
105 
106 	void *argb_cursor;		/* contigmalloc area for ARGB cursor */
107 
108 	struct resource_spec * sc_res_spec;
109 	struct resource *sc_res[2];
110 };
111 
112 /* For adding new devices, devid is the id of the graphics controller
113  * (pci:0:2:0, for example).  The placeholder (usually at pci:0:2:1) for the
114  * second head should never be added.  The bridge_offset is the offset to
115  * subtract from devid to get the id of the hostb that the device is on.
116  */
117 static const struct agp_i810_match {
118 	int devid;
119 	int chiptype;
120 	int bridge_offset;
121 	char *name;
122 } agp_i810_matches[] = {
123 	{0x71218086, CHIP_I810, 0x00010000,
124 	    "Intel 82810 (i810 GMCH) SVGA controller"},
125 	{0x71238086, CHIP_I810, 0x00010000,
126 	    "Intel 82810-DC100 (i810-DC100 GMCH) SVGA controller"},
127 	{0x71258086, CHIP_I810, 0x00010000,
128 	    "Intel 82810E (i810E GMCH) SVGA controller"},
129 	{0x11328086, CHIP_I810, 0x00020000,
130 	    "Intel 82815 (i815 GMCH) SVGA controller"},
131 	{0x35778086, CHIP_I830, 0x00020000,
132 	    "Intel 82830M (830M GMCH) SVGA controller"},
133 	{0x35828086, CHIP_I855, 0x00020000,
134 	    "Intel 82852/5"},
135 	{0x25728086, CHIP_I855, 0x00020000,
136 	    "Intel 82865G (865G GMCH) SVGA controller"},
137 	{0x25828086, CHIP_I915, 0x00020000,
138 	    "Intel 82915G (915G GMCH) SVGA controller"},
139 	{0x25928086, CHIP_I915, 0x00020000,
140 	    "Intel 82915GM (915GM GMCH) SVGA controller"},
141 	{0x27728086, CHIP_I915, 0x00020000,
142 	    "Intel 82945G (945G GMCH) SVGA controller"},
143 	{0x27A28086, CHIP_I915, 0x00020000,
144 	    "Intel 82945GM (945GM GMCH) SVGA controller"},
145 	{0x27A28086, CHIP_I915, 0x00020000,
146 	    "Intel 945GME SVGA controller"},
147 	{0x29728086, CHIP_I965, 0x00020000,
148 	    "Intel 946GZ SVGA controller"},
149 	{0x29828086, CHIP_I965, 0x00020000,
150 	    "Intel G965 SVGA controller"},
151 	{0x29928086, CHIP_I965, 0x00020000,
152 	    "Intel Q965 SVGA controller"},
153 	{0x29a28086, CHIP_I965, 0x00020000,
154 	    "Intel G965 SVGA controller"},
155 /*
156 	{0x29b28086, CHIP_G33, 0x00020000,
157 	    "Intel Q35 SVGA controller"},
158 	{0x29c28086, CHIP_G33, 0x00020000,
159 	    "Intel G33 SVGA controller"},
160 	{0x29d28086, CHIP_G33, 0x00020000,
161 	    "Intel Q33 SVGA controller"},
162 */
163 	{0x2a028086, CHIP_I965, 0x00020000,
164 	    "Intel GM965 SVGA controller"},
165 	{0x2a128086, CHIP_I965, 0x00020000,
166 	    "Intel GME965 SVGA controller"},
167 	{0, 0, 0, NULL}
168 };
169 
170 static const struct agp_i810_match*
171 agp_i810_match(device_t dev)
172 {
173 	int i, devid;
174 
175 	if (pci_get_class(dev) != PCIC_DISPLAY
176 	    || pci_get_subclass(dev) != PCIS_DISPLAY_VGA)
177 		return NULL;
178 
179 	devid = pci_get_devid(dev);
180 	for (i = 0; agp_i810_matches[i].devid != 0; i++) {
181 		if (agp_i810_matches[i].devid == devid)
182 		    break;
183 	}
184 	if (agp_i810_matches[i].devid == 0)
185 		return NULL;
186 	else
187 		return &agp_i810_matches[i];
188 }
189 
190 /*
191  * Find bridge device.
192  */
193 static device_t
194 agp_i810_find_bridge(device_t dev)
195 {
196 	device_t *children, child;
197 	int nchildren, i;
198 	u_int32_t devid;
199 	const struct agp_i810_match *match;
200 
201 	match = agp_i810_match(dev);
202 	devid = match->devid - match->bridge_offset;
203 
204 	if (device_get_children(device_get_parent(device_get_parent(dev)),
205 	    &children, &nchildren))
206 		return 0;
207 
208 	for (i = 0; i < nchildren; i++) {
209 		child = children[i];
210 
211 		if (pci_get_devid(child) == devid) {
212 			free(children, M_TEMP);
213 			return child;
214 		}
215 	}
216 	free(children, M_TEMP);
217 	return 0;
218 }
219 
220 static void
221 agp_i810_identify(driver_t *driver, device_t parent)
222 {
223 
224 	if (device_find_child(parent, "agp", -1) == NULL &&
225 	    agp_i810_match(parent))
226 		device_add_child(parent, "agp", -1);
227 }
228 
229 static int
230 agp_i810_probe(device_t dev)
231 {
232 	device_t bdev;
233 	const struct agp_i810_match *match;
234 	u_int8_t smram;
235 	int gcc1, deven;
236 
237 	if (resource_disabled("agp", device_get_unit(dev)))
238 		return (ENXIO);
239 	match = agp_i810_match(dev);
240 	if (match == NULL)
241 		return ENXIO;
242 
243 	bdev = agp_i810_find_bridge(dev);
244 	if (!bdev) {
245 		if (bootverbose)
246 			printf("I810: can't find bridge device\n");
247 		return ENXIO;
248 	}
249 
250 	/*
251 	 * checking whether internal graphics device has been activated.
252 	 */
253 	switch (match->chiptype) {
254 	case CHIP_I810:
255 		smram = pci_read_config(bdev, AGP_I810_SMRAM, 1);
256 		if ((smram & AGP_I810_SMRAM_GMS) ==
257 		    AGP_I810_SMRAM_GMS_DISABLED) {
258 			if (bootverbose)
259 				printf("I810: disabled, not probing\n");
260 			return ENXIO;
261 		}
262 		break;
263 	case CHIP_I830:
264 	case CHIP_I855:
265 		gcc1 = pci_read_config(bdev, AGP_I830_GCC1, 1);
266 		if ((gcc1 & AGP_I830_GCC1_DEV2) ==
267 		    AGP_I830_GCC1_DEV2_DISABLED) {
268 			if (bootverbose)
269 				printf("I830: disabled, not probing\n");
270 			return ENXIO;
271 		}
272 		break;
273 	case CHIP_I915:
274 	case CHIP_I965:
275 	case CHIP_G33:
276 		deven = pci_read_config(bdev, AGP_I915_DEVEN, 4);
277 		if ((deven & AGP_I915_DEVEN_D2F0) ==
278 		    AGP_I915_DEVEN_D2F0_DISABLED) {
279 			if (bootverbose)
280 				printf("I915: disabled, not probing\n");
281 			return ENXIO;
282 		}
283 		break;
284 	}
285 
286 	if (match->devid == 0x35828086) {
287 		switch (pci_read_config(dev, AGP_I85X_CAPID, 1)) {
288 		case AGP_I855_GME:
289 			device_set_desc(dev,
290 			    "Intel 82855GME (855GME GMCH) SVGA controller");
291 			break;
292 		case AGP_I855_GM:
293 			device_set_desc(dev,
294 			    "Intel 82855GM (855GM GMCH) SVGA controller");
295 			break;
296 		case AGP_I852_GME:
297 			device_set_desc(dev,
298 			    "Intel 82852GME (852GME GMCH) SVGA controller");
299 			break;
300 		case AGP_I852_GM:
301 			device_set_desc(dev,
302 			    "Intel 82852GM (852GM GMCH) SVGA controller");
303 			break;
304 		default:
305 			device_set_desc(dev,
306 			    "Intel 8285xM (85xGM GMCH) SVGA controller");
307 			break;
308 		}
309 	} else {
310 		device_set_desc(dev, match->name);
311 	}
312 
313 	return BUS_PROBE_DEFAULT;
314 }
315 
316 static void
317 agp_i810_dump_regs(device_t dev)
318 {
319 	struct agp_i810_softc *sc = device_get_softc(dev);
320 
321 	device_printf(dev, "AGP_I810_PGTBL_CTL: %08x\n",
322 	    bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL));
323 
324 	switch (sc->chiptype) {
325 	case CHIP_I810:
326 		device_printf(dev, "AGP_I810_MISCC: 0x%04x\n",
327 		    pci_read_config(sc->bdev, AGP_I810_MISCC, 2));
328 		break;
329 	case CHIP_I830:
330 		device_printf(dev, "AGP_I830_GCC1: 0x%02x\n",
331 		    pci_read_config(sc->bdev, AGP_I830_GCC1, 1));
332 		break;
333 	case CHIP_I855:
334 		device_printf(dev, "AGP_I855_GCC1: 0x%02x\n",
335 		    pci_read_config(sc->bdev, AGP_I855_GCC1, 1));
336 		break;
337 	case CHIP_I915:
338 	case CHIP_I965:
339 	case CHIP_G33:
340 		device_printf(dev, "AGP_I855_GCC1: 0x%02x\n",
341 		    pci_read_config(sc->bdev, AGP_I855_GCC1, 1));
342 		device_printf(dev, "AGP_I915_MSAC: 0x%02x\n",
343 		    pci_read_config(sc->bdev, AGP_I915_MSAC, 1));
344 		break;
345 	}
346 	device_printf(dev, "Aperture resource size: %d bytes\n",
347 	    AGP_GET_APERTURE(dev));
348 }
349 
350 static int
351 agp_i810_attach(device_t dev)
352 {
353 	struct agp_i810_softc *sc = device_get_softc(dev);
354 	struct agp_gatt *gatt;
355 	const struct agp_i810_match *match;
356 	int error;
357 
358 	sc->bdev = agp_i810_find_bridge(dev);
359 	if (!sc->bdev)
360 		return ENOENT;
361 
362 	match = agp_i810_match(dev);
363 	sc->chiptype = match->chiptype;
364 
365 	switch (sc->chiptype) {
366 	case CHIP_I810:
367 	case CHIP_I830:
368 	case CHIP_I855:
369 		sc->sc_res_spec = agp_i810_res_spec;
370 		agp_set_aperture_resource(dev, AGP_APBASE);
371 		break;
372 	case CHIP_I915:
373 	case CHIP_G33:
374 		sc->sc_res_spec = agp_i915_res_spec;
375 		agp_set_aperture_resource(dev, AGP_I915_GMADR);
376 		break;
377 	case CHIP_I965:
378 		sc->sc_res_spec = agp_i965_res_spec;
379 		agp_set_aperture_resource(dev, AGP_I915_GMADR);
380 		break;
381 	}
382 
383 	error = agp_generic_attach(dev);
384 	if (error)
385 		return error;
386 
387 	if (sc->chiptype != CHIP_I965 && sc->chiptype != CHIP_G33 &&
388 	    ptoa((vm_paddr_t)Maxmem) > 0xfffffffful)
389 	{
390 		device_printf(dev, "agp_i810.c does not support physical "
391 		    "memory above 4GB.\n");
392 		return ENOENT;
393 	}
394 
395 	if (bus_alloc_resources(dev, sc->sc_res_spec, sc->sc_res)) {
396 		agp_generic_detach(dev);
397 		return ENODEV;
398 	}
399 
400 	sc->initial_aperture = AGP_GET_APERTURE(dev);
401 
402 	gatt = malloc( sizeof(struct agp_gatt), M_AGP, M_NOWAIT);
403 	if (!gatt) {
404 		bus_release_resources(dev, sc->sc_res_spec, sc->sc_res);
405  		agp_generic_detach(dev);
406  		return ENOMEM;
407 	}
408 	sc->gatt = gatt;
409 
410 	gatt->ag_entries = AGP_GET_APERTURE(dev) >> AGP_PAGE_SHIFT;
411 
412 	if ( sc->chiptype == CHIP_I810 ) {
413 		/* Some i810s have on-chip memory called dcache */
414 		if (bus_read_1(sc->sc_res[0], AGP_I810_DRT) &
415 		    AGP_I810_DRT_POPULATED)
416 			sc->dcache_size = 4 * 1024 * 1024;
417 		else
418 			sc->dcache_size = 0;
419 
420 		/* According to the specs the gatt on the i810 must be 64k */
421 		gatt->ag_virtual = contigmalloc( 64 * 1024, M_AGP, 0,
422 					0, ~0, PAGE_SIZE, 0);
423 		if (!gatt->ag_virtual) {
424 			if (bootverbose)
425 				device_printf(dev, "contiguous allocation failed\n");
426 			bus_release_resources(dev, sc->sc_res_spec,
427 			    sc->sc_res);
428 			free(gatt, M_AGP);
429 			agp_generic_detach(dev);
430 			return ENOMEM;
431 		}
432 		bzero(gatt->ag_virtual, gatt->ag_entries * sizeof(u_int32_t));
433 
434 		gatt->ag_physical = vtophys((vm_offset_t) gatt->ag_virtual);
435 		agp_flush_cache();
436 		/* Install the GATT. */
437 		bus_write_4(sc->sc_res[0], AGP_I810_PGTBL_CTL,
438 		    gatt->ag_physical | 1);
439 	} else if ( sc->chiptype == CHIP_I830 ) {
440 		/* The i830 automatically initializes the 128k gatt on boot. */
441 		unsigned int gcc1, pgtblctl;
442 
443 		gcc1 = pci_read_config(sc->bdev, AGP_I830_GCC1, 1);
444 		switch (gcc1 & AGP_I830_GCC1_GMS) {
445 			case AGP_I830_GCC1_GMS_STOLEN_512:
446 				sc->stolen = (512 - 132) * 1024 / 4096;
447 				break;
448 			case AGP_I830_GCC1_GMS_STOLEN_1024:
449 				sc->stolen = (1024 - 132) * 1024 / 4096;
450 				break;
451 			case AGP_I830_GCC1_GMS_STOLEN_8192:
452 				sc->stolen = (8192 - 132) * 1024 / 4096;
453 				break;
454 			default:
455 				sc->stolen = 0;
456 				device_printf(dev, "unknown memory configuration, disabling\n");
457 				bus_release_resources(dev, sc->sc_res_spec,
458 				    sc->sc_res);
459 				free(gatt, M_AGP);
460 				agp_generic_detach(dev);
461 				return EINVAL;
462 		}
463 		if (sc->stolen > 0) {
464 			device_printf(dev, "detected %dk stolen memory\n",
465 			    sc->stolen * 4);
466 		}
467 		device_printf(dev, "aperture size is %dM\n",
468 		    sc->initial_aperture / 1024 / 1024);
469 
470 		/* GATT address is already in there, make sure it's enabled */
471 		pgtblctl = bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL);
472 		pgtblctl |= 1;
473 		bus_write_4(sc->sc_res[0], AGP_I810_PGTBL_CTL, pgtblctl);
474 
475 		gatt->ag_physical = pgtblctl & ~1;
476 	} else if (sc->chiptype == CHIP_I855 || sc->chiptype == CHIP_I915 ||
477 	    sc->chiptype == CHIP_I965 || sc->chiptype == CHIP_G33) {
478 		unsigned int gcc1, pgtblctl, stolen, gtt_size;
479 
480 		/* Stolen memory is set up at the beginning of the aperture by
481 		 * the BIOS, consisting of the GATT followed by 4kb for the
482 		 * BIOS display.
483 		 */
484 		switch (sc->chiptype) {
485 		case CHIP_I855:
486 			gtt_size = 128;
487 			break;
488 		case CHIP_I915:
489 			gtt_size = 256;
490 			break;
491 		case CHIP_I965:
492 		case CHIP_G33:
493 			switch (bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL) &
494 			    AGP_I810_PGTBL_SIZE_MASK) {
495 			case AGP_I810_PGTBL_SIZE_128KB:
496 				gtt_size = 128;
497 				break;
498 			case AGP_I810_PGTBL_SIZE_256KB:
499 				gtt_size = 256;
500 				break;
501 			case AGP_I810_PGTBL_SIZE_512KB:
502 				gtt_size = 512;
503 				break;
504 			default:
505 				device_printf(dev, "Bad PGTBL size\n");
506 				bus_release_resources(dev, sc->sc_res_spec,
507 				    sc->sc_res);
508 				free(gatt, M_AGP);
509 				agp_generic_detach(dev);
510 				return EINVAL;
511 			}
512 			break;
513 		default:
514 			device_printf(dev, "Bad chiptype\n");
515 			bus_release_resources(dev, sc->sc_res_spec,
516 			    sc->sc_res);
517 			free(gatt, M_AGP);
518 			agp_generic_detach(dev);
519 			return EINVAL;
520 		}
521 
522 		/* GCC1 is called MGGC on i915+ */
523 		gcc1 = pci_read_config(sc->bdev, AGP_I855_GCC1, 1);
524 		switch (gcc1 & AGP_I855_GCC1_GMS) {
525 		case AGP_I855_GCC1_GMS_STOLEN_1M:
526 			stolen = 1024;
527 			break;
528 		case AGP_I855_GCC1_GMS_STOLEN_4M:
529 			stolen = 4096;
530 			break;
531 		case AGP_I855_GCC1_GMS_STOLEN_8M:
532 			stolen = 8192;
533 			break;
534 		case AGP_I855_GCC1_GMS_STOLEN_16M:
535 			stolen = 16384;
536 			break;
537 		case AGP_I855_GCC1_GMS_STOLEN_32M:
538 			stolen = 32768;
539 			break;
540 		case AGP_I915_GCC1_GMS_STOLEN_48M:
541 			stolen = 49152;
542 			break;
543 		case AGP_I915_GCC1_GMS_STOLEN_64M:
544 			stolen = 65536;
545 			break;
546 		case AGP_G33_GCC1_GMS_STOLEN_128M:
547 			stolen = 128 * 1024;
548 			break;
549 		case AGP_G33_GCC1_GMS_STOLEN_256M:
550 			stolen = 256 * 1024;
551 			break;
552 		default:
553 			device_printf(dev, "unknown memory configuration, "
554 			    "disabling\n");
555 			bus_release_resources(dev, sc->sc_res_spec,
556 			    sc->sc_res);
557 			free(gatt, M_AGP);
558 			agp_generic_detach(dev);
559 			return EINVAL;
560 		}
561 		sc->stolen = (stolen - gtt_size - 4) * 1024 / 4096;
562 		if (sc->stolen > 0)
563 			device_printf(dev, "detected %dk stolen memory\n", sc->stolen * 4);
564 		device_printf(dev, "aperture size is %dM\n", sc->initial_aperture / 1024 / 1024);
565 
566 		/* GATT address is already in there, make sure it's enabled */
567 		pgtblctl = bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL);
568 		pgtblctl |= 1;
569 		bus_write_4(sc->sc_res[0], AGP_I810_PGTBL_CTL, pgtblctl);
570 
571 		gatt->ag_physical = pgtblctl & ~1;
572 	}
573 
574 	if (0)
575 		agp_i810_dump_regs(dev);
576 
577 	return 0;
578 }
579 
580 static int
581 agp_i810_detach(device_t dev)
582 {
583 	struct agp_i810_softc *sc = device_get_softc(dev);
584 	int error;
585 
586 	error = agp_generic_detach(dev);
587 	if (error)
588 		return error;
589 
590 	/* Clear the GATT base. */
591 	if ( sc->chiptype == CHIP_I810 ) {
592 		bus_write_4(sc->sc_res[0], AGP_I810_PGTBL_CTL, 0);
593 	} else {
594 		unsigned int pgtblctl;
595 		pgtblctl = bus_read_4(sc->sc_res[0], AGP_I810_PGTBL_CTL);
596 		pgtblctl &= ~1;
597 		bus_write_4(sc->sc_res[0], AGP_I810_PGTBL_CTL, pgtblctl);
598 	}
599 
600 	/* Put the aperture back the way it started. */
601 	AGP_SET_APERTURE(dev, sc->initial_aperture);
602 
603 	if ( sc->chiptype == CHIP_I810 ) {
604 		contigfree(sc->gatt->ag_virtual, 64 * 1024, M_AGP);
605 	}
606 	free(sc->gatt, M_AGP);
607 
608 	bus_release_resources(dev, sc->sc_res_spec, sc->sc_res);
609 
610 	return 0;
611 }
612 
613 /**
614  * Sets the PCI resource size of the aperture on i830-class and below chipsets,
615  * while returning failure on later chipsets when an actual change is
616  * requested.
617  *
618  * This whole function is likely bogus, as the kernel would probably need to
619  * reconfigure the placement of the AGP aperture if a larger size is requested,
620  * which doesn't happen currently.
621  */
622 static int
623 agp_i810_set_aperture(device_t dev, u_int32_t aperture)
624 {
625 	struct agp_i810_softc *sc = device_get_softc(dev);
626 	u_int16_t miscc, gcc1;
627 
628 	switch (sc->chiptype) {
629 	case CHIP_I810:
630 		/*
631 		 * Double check for sanity.
632 		 */
633 		if (aperture != 32 * 1024 * 1024 && aperture != 64 * 1024 * 1024) {
634 			device_printf(dev, "bad aperture size %d\n", aperture);
635 			return EINVAL;
636 		}
637 
638 		miscc = pci_read_config(sc->bdev, AGP_I810_MISCC, 2);
639 		miscc &= ~AGP_I810_MISCC_WINSIZE;
640 		if (aperture == 32 * 1024 * 1024)
641 			miscc |= AGP_I810_MISCC_WINSIZE_32;
642 		else
643 			miscc |= AGP_I810_MISCC_WINSIZE_64;
644 
645 		pci_write_config(sc->bdev, AGP_I810_MISCC, miscc, 2);
646 		break;
647 	case CHIP_I830:
648 		if (aperture != 64 * 1024 * 1024 &&
649 		    aperture != 128 * 1024 * 1024) {
650 			device_printf(dev, "bad aperture size %d\n", aperture);
651 			return EINVAL;
652 		}
653 		gcc1 = pci_read_config(sc->bdev, AGP_I830_GCC1, 2);
654 		gcc1 &= ~AGP_I830_GCC1_GMASIZE;
655 		if (aperture == 64 * 1024 * 1024)
656 			gcc1 |= AGP_I830_GCC1_GMASIZE_64;
657 		else
658 			gcc1 |= AGP_I830_GCC1_GMASIZE_128;
659 
660 		pci_write_config(sc->bdev, AGP_I830_GCC1, gcc1, 2);
661 		break;
662 	case CHIP_I855:
663 	case CHIP_I915:
664 	case CHIP_I965:
665 	case CHIP_G33:
666 		return agp_generic_set_aperture(dev, aperture);
667 	}
668 
669 	return 0;
670 }
671 
672 /**
673  * Writes a GTT entry mapping the page at the given offset from the beginning
674  * of the aperture to the given physical address.
675  */
676 static void
677 agp_i810_write_gtt_entry(device_t dev, int offset, vm_offset_t physical,
678     int enabled)
679 {
680 	struct agp_i810_softc *sc = device_get_softc(dev);
681 	u_int32_t pte;
682 
683 	pte = (u_int32_t)physical | 1;
684 	if (sc->chiptype == CHIP_I965 || sc->chiptype == CHIP_G33) {
685 		pte |= (physical & 0x0000000f00000000ull) >> 28;
686 	} else {
687 		/* If we do actually have memory above 4GB on an older system,
688 		 * crash cleanly rather than scribble on system memory,
689 		 * so we know we need to fix it.
690 		 */
691 		KASSERT((pte & 0x0000000f00000000ull) == 0,
692 		    (">4GB physical address in agp"));
693 	}
694 
695 	switch (sc->chiptype) {
696 	case CHIP_I810:
697 	case CHIP_I830:
698 	case CHIP_I855:
699 		bus_write_4(sc->sc_res[0],
700 		    AGP_I810_GTT + (offset >> AGP_PAGE_SHIFT) * 4, pte);
701 		break;
702 	case CHIP_I915:
703 	case CHIP_G33:
704 		bus_write_4(sc->sc_res[1],
705 		    (offset >> AGP_PAGE_SHIFT) * 4, pte);
706 		break;
707 	case CHIP_I965:
708 		bus_write_4(sc->sc_res[0],
709 		    (offset >> AGP_PAGE_SHIFT) * 4 + (512 * 1024), pte);
710 		break;
711 	}
712 }
713 
714 static int
715 agp_i810_bind_page(device_t dev, int offset, vm_offset_t physical)
716 {
717 	struct agp_i810_softc *sc = device_get_softc(dev);
718 
719 	if (offset < 0 || offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT)) {
720 		device_printf(dev, "failed: offset is 0x%08x, shift is %d, entries is %d\n", offset, AGP_PAGE_SHIFT, sc->gatt->ag_entries);
721 		return EINVAL;
722 	}
723 
724 	if ( sc->chiptype != CHIP_I810 ) {
725 		if ( (offset >> AGP_PAGE_SHIFT) < sc->stolen ) {
726 			device_printf(dev, "trying to bind into stolen memory");
727 			return EINVAL;
728 		}
729 	}
730 
731 	agp_i810_write_gtt_entry(dev, offset, physical, 1);
732 
733 	return 0;
734 }
735 
736 static int
737 agp_i810_unbind_page(device_t dev, int offset)
738 {
739 	struct agp_i810_softc *sc = device_get_softc(dev);
740 
741 	if (offset < 0 || offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
742 		return EINVAL;
743 
744 	if ( sc->chiptype != CHIP_I810 ) {
745 		if ( (offset >> AGP_PAGE_SHIFT) < sc->stolen ) {
746 			device_printf(dev, "trying to unbind from stolen memory");
747 			return EINVAL;
748 		}
749 	}
750 
751 	agp_i810_write_gtt_entry(dev, offset, 0, 0);
752 
753 	return 0;
754 }
755 
756 /*
757  * Writing via memory mapped registers already flushes all TLBs.
758  */
759 static void
760 agp_i810_flush_tlb(device_t dev)
761 {
762 }
763 
764 static int
765 agp_i810_enable(device_t dev, u_int32_t mode)
766 {
767 
768 	return 0;
769 }
770 
771 static struct agp_memory *
772 agp_i810_alloc_memory(device_t dev, int type, vm_size_t size)
773 {
774 	struct agp_i810_softc *sc = device_get_softc(dev);
775 	struct agp_memory *mem;
776 
777 	if ((size & (AGP_PAGE_SIZE - 1)) != 0)
778 		return 0;
779 
780 	if (sc->agp.as_allocated + size > sc->agp.as_maxmem)
781 		return 0;
782 
783 	if (type == 1) {
784 		/*
785 		 * Mapping local DRAM into GATT.
786 		 */
787 		if ( sc->chiptype != CHIP_I810 )
788 			return 0;
789 		if (size != sc->dcache_size)
790 			return 0;
791 	} else if (type == 2) {
792 		/*
793 		 * Type 2 is the contiguous physical memory type, that hands
794 		 * back a physical address.  This is used for cursors on i810.
795 		 * Hand back as many single pages with physical as the user
796 		 * wants, but only allow one larger allocation (ARGB cursor)
797 		 * for simplicity.
798 		 */
799 		if (size != AGP_PAGE_SIZE) {
800 			if (sc->argb_cursor != NULL)
801 				return 0;
802 
803 			/* Allocate memory for ARGB cursor, if we can. */
804 			sc->argb_cursor = contigmalloc(size, M_AGP,
805 			   0, 0, ~0, PAGE_SIZE, 0);
806 			if (sc->argb_cursor == NULL)
807 				return 0;
808 		}
809 	}
810 
811 	mem = malloc(sizeof *mem, M_AGP, M_WAITOK);
812 	mem->am_id = sc->agp.as_nextid++;
813 	mem->am_size = size;
814 	mem->am_type = type;
815 	if (type != 1 && (type != 2 || size == AGP_PAGE_SIZE))
816 		mem->am_obj = vm_object_allocate(OBJT_DEFAULT,
817 						 atop(round_page(size)));
818 	else
819 		mem->am_obj = 0;
820 
821 	if (type == 2) {
822 		if (size == AGP_PAGE_SIZE) {
823 			/*
824 			 * Allocate and wire down the page now so that we can
825 			 * get its physical address.
826 			 */
827 			vm_page_t m;
828 
829 			VM_OBJECT_LOCK(mem->am_obj);
830 			m = vm_page_grab(mem->am_obj, 0, VM_ALLOC_NOBUSY |
831 			    VM_ALLOC_WIRED | VM_ALLOC_ZERO | VM_ALLOC_RETRY);
832 			VM_OBJECT_UNLOCK(mem->am_obj);
833 			mem->am_physical = VM_PAGE_TO_PHYS(m);
834 		} else {
835 			/* Our allocation is already nicely wired down for us.
836 			 * Just grab the physical address.
837 			 */
838 			mem->am_physical = vtophys(sc->argb_cursor);
839 		}
840 	} else {
841 		mem->am_physical = 0;
842 	}
843 
844 	mem->am_offset = 0;
845 	mem->am_is_bound = 0;
846 	TAILQ_INSERT_TAIL(&sc->agp.as_memory, mem, am_link);
847 	sc->agp.as_allocated += size;
848 
849 	return mem;
850 }
851 
852 static int
853 agp_i810_free_memory(device_t dev, struct agp_memory *mem)
854 {
855 	struct agp_i810_softc *sc = device_get_softc(dev);
856 
857 	if (mem->am_is_bound)
858 		return EBUSY;
859 
860 	if (mem->am_type == 2) {
861 		if (mem->am_size == AGP_PAGE_SIZE) {
862 			/*
863 			 * Unwire the page which we wired in alloc_memory.
864 			 */
865 			vm_page_t m;
866 
867 			VM_OBJECT_LOCK(mem->am_obj);
868 			m = vm_page_lookup(mem->am_obj, 0);
869 			VM_OBJECT_UNLOCK(mem->am_obj);
870 			vm_page_lock_queues();
871 			vm_page_unwire(m, 0);
872 			vm_page_unlock_queues();
873 		} else {
874 			contigfree(sc->argb_cursor, mem->am_size, M_AGP);
875 			sc->argb_cursor = NULL;
876 		}
877 	}
878 
879 	sc->agp.as_allocated -= mem->am_size;
880 	TAILQ_REMOVE(&sc->agp.as_memory, mem, am_link);
881 	if (mem->am_obj)
882 		vm_object_deallocate(mem->am_obj);
883 	free(mem, M_AGP);
884 	return 0;
885 }
886 
887 static int
888 agp_i810_bind_memory(device_t dev, struct agp_memory *mem,
889 		     vm_offset_t offset)
890 {
891 	struct agp_i810_softc *sc = device_get_softc(dev);
892 	vm_offset_t i;
893 
894 	/* Do some sanity checks first. */
895 	if (offset < 0 || (offset & (AGP_PAGE_SIZE - 1)) != 0 ||
896 	    offset + mem->am_size > AGP_GET_APERTURE(dev)) {
897 		device_printf(dev, "binding memory at bad offset %#x\n",
898 		    (int)offset);
899 		return EINVAL;
900 	}
901 
902 	if (mem->am_type == 2 && mem->am_size != AGP_PAGE_SIZE) {
903 		mtx_lock(&sc->agp.as_lock);
904 		if (mem->am_is_bound) {
905 			mtx_unlock(&sc->agp.as_lock);
906 			return EINVAL;
907 		}
908 		/* The memory's already wired down, just stick it in the GTT. */
909 		for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) {
910 			agp_i810_write_gtt_entry(dev, offset + i,
911 			    mem->am_physical + i, 1);
912 		}
913 		agp_flush_cache();
914 		mem->am_offset = offset;
915 		mem->am_is_bound = 1;
916 		mtx_unlock(&sc->agp.as_lock);
917 		return 0;
918 	}
919 
920 	if (mem->am_type != 1)
921 		return agp_generic_bind_memory(dev, mem, offset);
922 
923 	if ( sc->chiptype != CHIP_I810 )
924 		return EINVAL;
925 
926 	for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) {
927 		bus_write_4(sc->sc_res[0],
928 		    AGP_I810_GTT + (i >> AGP_PAGE_SHIFT) * 4, i | 3);
929 	}
930 
931 	return 0;
932 }
933 
934 static int
935 agp_i810_unbind_memory(device_t dev, struct agp_memory *mem)
936 {
937 	struct agp_i810_softc *sc = device_get_softc(dev);
938 	vm_offset_t i;
939 
940 	if (mem->am_type == 2 && mem->am_size != AGP_PAGE_SIZE) {
941 		mtx_lock(&sc->agp.as_lock);
942 		if (!mem->am_is_bound) {
943 			mtx_unlock(&sc->agp.as_lock);
944 			return EINVAL;
945 		}
946 
947 		for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) {
948 			agp_i810_write_gtt_entry(dev, mem->am_offset + i,
949 			    0, 0);
950 		}
951 		agp_flush_cache();
952 		mem->am_is_bound = 0;
953 		mtx_unlock(&sc->agp.as_lock);
954 		return 0;
955 	}
956 
957 	if (mem->am_type != 1)
958 		return agp_generic_unbind_memory(dev, mem);
959 
960 	if ( sc->chiptype != CHIP_I810 )
961 		return EINVAL;
962 
963 	for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) {
964 		bus_write_4(sc->sc_res[0],
965 		    AGP_I810_GTT + (i >> AGP_PAGE_SHIFT) * 4, 0);
966 	}
967 
968 	return 0;
969 }
970 
971 static device_method_t agp_i810_methods[] = {
972 	/* Device interface */
973 	DEVMETHOD(device_identify,	agp_i810_identify),
974 	DEVMETHOD(device_probe,		agp_i810_probe),
975 	DEVMETHOD(device_attach,	agp_i810_attach),
976 	DEVMETHOD(device_detach,	agp_i810_detach),
977 
978 	/* AGP interface */
979 	DEVMETHOD(agp_get_aperture,	agp_generic_get_aperture),
980 	DEVMETHOD(agp_set_aperture,	agp_i810_set_aperture),
981 	DEVMETHOD(agp_bind_page,	agp_i810_bind_page),
982 	DEVMETHOD(agp_unbind_page,	agp_i810_unbind_page),
983 	DEVMETHOD(agp_flush_tlb,	agp_i810_flush_tlb),
984 	DEVMETHOD(agp_enable,		agp_i810_enable),
985 	DEVMETHOD(agp_alloc_memory,	agp_i810_alloc_memory),
986 	DEVMETHOD(agp_free_memory,	agp_i810_free_memory),
987 	DEVMETHOD(agp_bind_memory,	agp_i810_bind_memory),
988 	DEVMETHOD(agp_unbind_memory,	agp_i810_unbind_memory),
989 
990 	{ 0, 0 }
991 };
992 
993 static driver_t agp_i810_driver = {
994 	"agp",
995 	agp_i810_methods,
996 	sizeof(struct agp_i810_softc),
997 };
998 
999 static devclass_t agp_devclass;
1000 
1001 DRIVER_MODULE(agp_i810, vgapci, agp_i810_driver, agp_devclass, 0, 0);
1002 MODULE_DEPEND(agp_i810, agp, 1, 1, 1);
1003 MODULE_DEPEND(agp_i810, pci, 1, 1, 1);
1004