xref: /freebsd/sys/dev/agp/agp_i810.c (revision 3193579b66fd7067f898dbc54bdea81a0e6f9bd0)
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/bus.h>
43 #include <sys/lock.h>
44 #include <sys/lockmgr.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 
63 MALLOC_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 
69 #define CHIP_I810 0	/* i810/i815 */
70 #define CHIP_I830 1	/* 830M/845G */
71 #define CHIP_I855 2	/* 852GM/855GM/865G */
72 
73 struct agp_i810_softc {
74 	struct agp_softc agp;
75 	u_int32_t initial_aperture;	/* aperture size at startup */
76 	struct agp_gatt *gatt;
77 	int chiptype;			/* i810-like or i830 */
78 	u_int32_t dcache_size;		/* i810 only */
79 	u_int32_t stolen;		/* number of i830/845 gtt entries for stolen memory */
80 	device_t bdev;			/* bridge device */
81 	struct resource *regs;		/* memory mapped GC registers */
82 	bus_space_tag_t bst;		/* bus_space tag */
83 	bus_space_handle_t bsh;		/* bus_space handle */
84 };
85 
86 static const char*
87 agp_i810_match(device_t dev)
88 {
89 	if (pci_get_class(dev) != PCIC_DISPLAY
90 	    || pci_get_subclass(dev) != PCIS_DISPLAY_VGA)
91 		return NULL;
92 
93 	switch (pci_get_devid(dev)) {
94 	case 0x71218086:
95 		return ("Intel 82810 (i810 GMCH) SVGA controller");
96 
97 	case 0x71238086:
98 		return ("Intel 82810-DC100 (i810-DC100 GMCH) SVGA controller");
99 
100 	case 0x71258086:
101 		return ("Intel 82810E (i810E GMCH) SVGA controller");
102 
103 	case 0x11328086:
104 		return ("Intel 82815 (i815 GMCH) SVGA controller");
105 
106 	case 0x35778086:
107 		return ("Intel 82830M (830M GMCH) SVGA controller");
108 
109 	case 0x25628086:
110 		return ("Intel 82845G (845G GMCH) SVGA controller");
111 
112 	case 0x35828086:
113 		switch (pci_read_config(dev, AGP_I85X_CAPID, 1)) {
114 		case AGP_I855_GME:
115 			return ("Intel 82855GME (855GME GMCH) SVGA controller");
116 
117 		case AGP_I855_GM:
118 			return ("Intel 82855GM (855GM GMCH) SVGA controller");
119 
120 		case AGP_I852_GME:
121 			return ("Intel 82852GME (852GME GMCH) SVGA controller");
122 
123 		case AGP_I852_GM:
124 			return ("Intel 82852GM (852GM GMCH) SVGA controller");
125 
126 		default:
127 			return ("Intel 8285xM (85xGM GMCH) SVGA controller");
128 		}
129 
130 	case 0x25728086:
131 		return ("Intel 82865G (865G GMCH) SVGA controller");
132 	};
133 
134 	return NULL;
135 }
136 
137 /*
138  * Find bridge device.
139  */
140 static device_t
141 agp_i810_find_bridge(device_t dev)
142 {
143 	device_t *children, child;
144 	int nchildren, i;
145 	u_int32_t devid;
146 
147 	/*
148 	 * Calculate bridge device's ID.
149 	 */
150 	devid = pci_get_devid(dev);
151 	switch (devid) {
152 	case 0x71218086:
153 	case 0x71238086:
154 	case 0x71258086:
155 		devid -= 0x10000;
156 		break;
157 
158 	case 0x11328086:
159 	case 0x35778086:
160 	case 0x25628086:
161 	case 0x35828086:
162 	case 0x25728086:
163 		devid -= 0x20000;
164 		break;
165 	};
166 	if (device_get_children(device_get_parent(dev), &children, &nchildren))
167 		return 0;
168 
169 	for (i = 0; i < nchildren; i++) {
170 		child = children[i];
171 
172 		if (pci_get_devid(child) == devid) {
173 			free(children, M_TEMP);
174 			return child;
175 		}
176 	}
177 	free(children, M_TEMP);
178 	return 0;
179 }
180 
181 static int
182 agp_i810_probe(device_t dev)
183 {
184 	const char *desc;
185 
186 	desc = agp_i810_match(dev);
187 	if (desc) {
188 		device_t bdev;
189 		u_int8_t smram;
190 		unsigned int gcc1;
191 		int devid = pci_get_devid(dev);
192 
193 		bdev = agp_i810_find_bridge(dev);
194 		if (!bdev) {
195 			if (bootverbose)
196 				printf("I810: can't find bridge device\n");
197 			return ENXIO;
198 		}
199 
200 		/*
201 		 * checking whether internal graphics device has been activated.
202 		 */
203 		switch (devid) {
204 			/* i810 */
205 		case 0x71218086:
206 		case 0x71238086:
207 		case 0x71258086:
208 		case 0x11328086:
209 			smram = pci_read_config(bdev, AGP_I810_SMRAM, 1);
210 			if ((smram & AGP_I810_SMRAM_GMS)
211 			    == AGP_I810_SMRAM_GMS_DISABLED) {
212 				if (bootverbose)
213 					printf("I810: disabled, not probing\n");
214 				return ENXIO;
215 			}
216 			break;
217 
218 			/* i830 */
219 		case 0x35778086:
220 		case 0x35828086:
221 		case 0x25628086:
222 		case 0x25728086:
223 			gcc1 = pci_read_config(bdev, AGP_I830_GCC1, 1);
224 			if ((gcc1 & AGP_I830_GCC1_DEV2) == AGP_I830_GCC1_DEV2_DISABLED) {
225 				if (bootverbose)
226 					printf("I830: disabled, not probing\n");
227 				return ENXIO;
228 			}
229 			break;
230 
231 		default:
232 			return ENXIO;
233 		}
234 
235 		device_verbose(dev);
236 		device_set_desc(dev, desc);
237 		return 0;
238 	}
239 
240 	return ENXIO;
241 }
242 
243 static int
244 agp_i810_attach(device_t dev)
245 {
246 	struct agp_i810_softc *sc = device_get_softc(dev);
247 	struct agp_gatt *gatt;
248 	int error, rid;
249 
250 	sc->bdev = agp_i810_find_bridge(dev);
251 	if (!sc->bdev)
252 		return ENOENT;
253 
254 	error = agp_generic_attach(dev);
255 	if (error)
256 		return error;
257 
258 	switch (pci_get_devid(dev)) {
259 	case 0x71218086:
260 	case 0x71238086:
261 	case 0x71258086:
262 	case 0x11328086:
263 		sc->chiptype = CHIP_I810;
264 		break;
265 	case 0x35778086:
266 	case 0x25628086:
267 		sc->chiptype = CHIP_I830;
268 		break;
269 	case 0x35828086:
270 	case 0x25728086:
271 		sc->chiptype = CHIP_I855;
272 		break;
273 	};
274 
275 	/* Same for i810 and i830 */
276 	rid = AGP_I810_MMADR;
277 	sc->regs = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
278 				      0, ~0, 1, RF_ACTIVE);
279 	if (!sc->regs) {
280 		agp_generic_detach(dev);
281 		return ENOMEM;
282 	}
283 	sc->bst = rman_get_bustag(sc->regs);
284 	sc->bsh = rman_get_bushandle(sc->regs);
285 
286 	sc->initial_aperture = AGP_GET_APERTURE(dev);
287 
288 	gatt = malloc( sizeof(struct agp_gatt), M_AGP, M_NOWAIT);
289 	if (!gatt) {
290  		agp_generic_detach(dev);
291  		return ENOMEM;
292 	}
293 	sc->gatt = gatt;
294 
295 	gatt->ag_entries = AGP_GET_APERTURE(dev) >> AGP_PAGE_SHIFT;
296 
297 	if ( sc->chiptype == CHIP_I810 ) {
298 		/* Some i810s have on-chip memory called dcache */
299 		if (READ1(AGP_I810_DRT) & AGP_I810_DRT_POPULATED)
300 			sc->dcache_size = 4 * 1024 * 1024;
301 		else
302 			sc->dcache_size = 0;
303 
304 		/* According to the specs the gatt on the i810 must be 64k */
305 		gatt->ag_virtual = contigmalloc( 64 * 1024, M_AGP, 0,
306 					0, ~0, PAGE_SIZE, 0);
307 		if (!gatt->ag_virtual) {
308 			if (bootverbose)
309 				device_printf(dev, "contiguous allocation failed\n");
310 			free(gatt, M_AGP);
311 			agp_generic_detach(dev);
312 			return ENOMEM;
313 		}
314 		bzero(gatt->ag_virtual, gatt->ag_entries * sizeof(u_int32_t));
315 
316 		gatt->ag_physical = vtophys((vm_offset_t) gatt->ag_virtual);
317 		agp_flush_cache();
318 		/* Install the GATT. */
319 		WRITE4(AGP_I810_PGTBL_CTL, gatt->ag_physical | 1);
320 	} else if ( sc->chiptype == CHIP_I830 ) {
321 		/* The i830 automatically initializes the 128k gatt on boot. */
322 		unsigned int gcc1, pgtblctl;
323 
324 		gcc1 = pci_read_config(sc->bdev, AGP_I830_GCC1, 1);
325 		switch (gcc1 & AGP_I830_GCC1_GMS) {
326 			case AGP_I830_GCC1_GMS_STOLEN_512:
327 				sc->stolen = (512 - 132) * 1024 / 4096;
328 				break;
329 			case AGP_I830_GCC1_GMS_STOLEN_1024:
330 				sc->stolen = (1024 - 132) * 1024 / 4096;
331 				break;
332 			case AGP_I830_GCC1_GMS_STOLEN_8192:
333 				sc->stolen = (8192 - 132) * 1024 / 4096;
334 				break;
335 			default:
336 				sc->stolen = 0;
337 				device_printf(dev, "unknown memory configuration, disabling\n");
338 				agp_generic_detach(dev);
339 				return EINVAL;
340 		}
341 		if (sc->stolen > 0)
342 			device_printf(dev, "detected %dk stolen memory\n", sc->stolen * 4);
343 		device_printf(dev, "aperture size is %dM\n", sc->initial_aperture / 1024 / 1024);
344 
345 		/* GATT address is already in there, make sure it's enabled */
346 		pgtblctl = READ4(AGP_I810_PGTBL_CTL);
347 		pgtblctl |= 1;
348 		WRITE4(AGP_I810_PGTBL_CTL, pgtblctl);
349 
350 		gatt->ag_physical = pgtblctl & ~1;
351 	} else {	/* CHIP_I855 */
352 		/* The i855 automatically initializes the 128k gatt on boot. */
353 		unsigned int gcc1, pgtblctl;
354 
355 		gcc1 = pci_read_config(sc->bdev, AGP_I855_GCC1, 1);
356 		switch (gcc1 & AGP_I855_GCC1_GMS) {
357 			case AGP_I855_GCC1_GMS_STOLEN_1M:
358 				sc->stolen = (1024 - 132) * 1024 / 4096;
359 				break;
360 			case AGP_I855_GCC1_GMS_STOLEN_4M:
361 				sc->stolen = (4096 - 132) * 1024 / 4096;
362 				break;
363 			case AGP_I855_GCC1_GMS_STOLEN_8M:
364 				sc->stolen = (8192 - 132) * 1024 / 4096;
365 				break;
366 			case AGP_I855_GCC1_GMS_STOLEN_16M:
367 				sc->stolen = (16384 - 132) * 1024 / 4096;
368 				break;
369 			case AGP_I855_GCC1_GMS_STOLEN_32M:
370 				sc->stolen = (32768 - 132) * 1024 / 4096;
371 				break;
372 			default:
373 				sc->stolen = 0;
374 				device_printf(dev, "unknown memory configuration, disabling\n");
375 				agp_generic_detach(dev);
376 				return EINVAL;
377 		}
378 		if (sc->stolen > 0)
379 			device_printf(dev, "detected %dk stolen memory\n", sc->stolen * 4);
380 		device_printf(dev, "aperture size is %dM\n", sc->initial_aperture / 1024 / 1024);
381 
382 		/* GATT address is already in there, make sure it's enabled */
383 		pgtblctl = READ4(AGP_I810_PGTBL_CTL);
384 		pgtblctl |= 1;
385 		WRITE4(AGP_I810_PGTBL_CTL, pgtblctl);
386 
387 		gatt->ag_physical = pgtblctl & ~1;
388 	}
389 
390 	/* Add a device for the drm to attach to */
391 	if (!device_add_child( dev, "drmsub", -1 ))
392 		printf("out of memory...\n");
393 
394 	return bus_generic_attach(dev);
395 }
396 
397 static int
398 agp_i810_detach(device_t dev)
399 {
400 	struct agp_i810_softc *sc = device_get_softc(dev);
401 	int error;
402 	device_t child;
403 
404 	error = agp_generic_detach(dev);
405 	if (error)
406 		return error;
407 
408 	/* Clear the GATT base. */
409 	if ( sc->chiptype == CHIP_I810 ) {
410 		WRITE4(AGP_I810_PGTBL_CTL, 0);
411 	} else {
412 		unsigned int pgtblctl;
413 		pgtblctl = READ4(AGP_I810_PGTBL_CTL);
414 		pgtblctl &= ~1;
415 		WRITE4(AGP_I810_PGTBL_CTL, pgtblctl);
416 	}
417 
418 	/* Put the aperture back the way it started. */
419 	AGP_SET_APERTURE(dev, sc->initial_aperture);
420 
421 	if ( sc->chiptype == CHIP_I810 ) {
422 		contigfree(sc->gatt->ag_virtual, 64 * 1024, M_AGP);
423 	}
424 	free(sc->gatt, M_AGP);
425 
426 	bus_release_resource(dev, SYS_RES_MEMORY,
427 			     AGP_I810_MMADR, sc->regs);
428 
429 	child = device_find_child( dev, "drmsub", 0 );
430 	if (child)
431 	   device_delete_child( dev, child );
432 
433 	return 0;
434 }
435 
436 static u_int32_t
437 agp_i810_get_aperture(device_t dev)
438 {
439 	struct agp_i810_softc *sc = device_get_softc(dev);
440 
441 	if ( sc->chiptype == CHIP_I810 ) {
442 		u_int16_t miscc;
443 		miscc = pci_read_config(sc->bdev, AGP_I810_MISCC, 2);
444 		if ((miscc & AGP_I810_MISCC_WINSIZE) == AGP_I810_MISCC_WINSIZE_32)
445 			return 32 * 1024 * 1024;
446 		else
447 			return 64 * 1024 * 1024;
448 	} else if ( sc->chiptype == CHIP_I830 ) {
449 		unsigned int gcc1;
450 
451 		gcc1 = pci_read_config(sc->bdev, AGP_I830_GCC1, 2);
452 		if ((gcc1 & AGP_I830_GCC1_GMASIZE) == AGP_I830_GCC1_GMASIZE_64)
453 			return 64 * 1024 * 1024;
454 		else
455 			return 128 * 1024 * 1024;
456 	} else { /* CHIP_I855 */
457 		return 128 * 1024 * 1024;
458 	}
459 }
460 
461 static int
462 agp_i810_set_aperture(device_t dev, u_int32_t aperture)
463 {
464 	struct agp_i810_softc *sc = device_get_softc(dev);
465 	u_int16_t miscc;
466 
467 	if ( sc->chiptype == CHIP_I810 ) {
468 		/*
469 		 * Double check for sanity.
470 		 */
471 		if (aperture != 32 * 1024 * 1024 && aperture != 64 * 1024 * 1024) {
472 			device_printf(dev, "bad aperture size %d\n", aperture);
473 			return EINVAL;
474 		}
475 
476 		miscc = pci_read_config(sc->bdev, AGP_I810_MISCC, 2);
477 		miscc &= ~AGP_I810_MISCC_WINSIZE;
478 		if (aperture == 32 * 1024 * 1024)
479 			miscc |= AGP_I810_MISCC_WINSIZE_32;
480 		else
481 			miscc |= AGP_I810_MISCC_WINSIZE_64;
482 
483 		pci_write_config(sc->bdev, AGP_I810_MISCC, miscc, 2);
484 	} else if ( sc->chiptype == CHIP_I830 ) {
485 		unsigned int gcc1;
486 
487 		if (aperture != 64 * 1024 * 1024 && aperture != 128 * 1024 * 1024) {
488 			device_printf(dev, "bad aperture size %d\n", aperture);
489 			return EINVAL;
490 		}
491 		gcc1 = pci_read_config(sc->bdev, AGP_I830_GCC1, 2);
492 		gcc1 &= ~AGP_I830_GCC1_GMASIZE;
493 		if (aperture == 64 * 1024 * 1024)
494 			gcc1 |= AGP_I830_GCC1_GMASIZE_64;
495 		else
496 			gcc1 |= AGP_I830_GCC1_GMASIZE_128;
497 
498 		pci_write_config(sc->bdev, AGP_I830_GCC1, gcc1, 2);
499 	} else {	/* CHIP_I855 */
500 		if (aperture != 128 * 1024 * 1024) {
501 			device_printf(dev, "bad aperture size %d\n", aperture);
502 			return EINVAL;
503 		}
504 	}
505 
506 	return 0;
507 }
508 
509 static int
510 agp_i810_bind_page(device_t dev, int offset, vm_offset_t physical)
511 {
512 	struct agp_i810_softc *sc = device_get_softc(dev);
513 
514 	if (offset < 0 || offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT)) {
515 		device_printf(dev, "failed: offset is 0x%08x, shift is %d, entries is %d\n", offset, AGP_PAGE_SHIFT, sc->gatt->ag_entries);
516 		return EINVAL;
517 	}
518 
519 	if ( sc->chiptype != CHIP_I810 ) {
520 		if ( (offset >> AGP_PAGE_SHIFT) < sc->stolen ) {
521 			device_printf(dev, "trying to bind into stolen memory");
522 			return EINVAL;
523 		}
524 	}
525 
526 	WRITE4(AGP_I810_GTT + (offset >> AGP_PAGE_SHIFT) * 4, physical | 1);
527 	return 0;
528 }
529 
530 static int
531 agp_i810_unbind_page(device_t dev, int offset)
532 {
533 	struct agp_i810_softc *sc = device_get_softc(dev);
534 
535 	if (offset < 0 || offset >= (sc->gatt->ag_entries << AGP_PAGE_SHIFT))
536 		return EINVAL;
537 
538 	if ( sc->chiptype != CHIP_I810 ) {
539 		if ( (offset >> AGP_PAGE_SHIFT) < sc->stolen ) {
540 			device_printf(dev, "trying to unbind from stolen memory");
541 			return EINVAL;
542 		}
543 	}
544 
545 	WRITE4(AGP_I810_GTT + (offset >> AGP_PAGE_SHIFT) * 4, 0);
546 	return 0;
547 }
548 
549 /*
550  * Writing via memory mapped registers already flushes all TLBs.
551  */
552 static void
553 agp_i810_flush_tlb(device_t dev)
554 {
555 }
556 
557 static int
558 agp_i810_enable(device_t dev, u_int32_t mode)
559 {
560 
561 	return 0;
562 }
563 
564 static struct agp_memory *
565 agp_i810_alloc_memory(device_t dev, int type, vm_size_t size)
566 {
567 	struct agp_i810_softc *sc = device_get_softc(dev);
568 	struct agp_memory *mem;
569 
570 	if ((size & (AGP_PAGE_SIZE - 1)) != 0)
571 		return 0;
572 
573 	if (sc->agp.as_allocated + size > sc->agp.as_maxmem)
574 		return 0;
575 
576 	if (type == 1) {
577 		/*
578 		 * Mapping local DRAM into GATT.
579 		 */
580 		if ( sc->chiptype != CHIP_I810 )
581 			return 0;
582 		if (size != sc->dcache_size)
583 			return 0;
584 	} else if (type == 2) {
585 		/*
586 		 * Bogus mapping of a single page for the hardware cursor.
587 		 */
588 		if (size != AGP_PAGE_SIZE)
589 			return 0;
590 	}
591 
592 	mem = malloc(sizeof *mem, M_AGP, M_WAITOK);
593 	mem->am_id = sc->agp.as_nextid++;
594 	mem->am_size = size;
595 	mem->am_type = type;
596 	if (type != 1)
597 		mem->am_obj = vm_object_allocate(OBJT_DEFAULT,
598 						 atop(round_page(size)));
599 	else
600 		mem->am_obj = 0;
601 
602 	if (type == 2) {
603 		/*
604 		 * Allocate and wire down the page now so that we can
605 		 * get its physical address.
606 		 */
607 		vm_page_t m;
608 
609 		VM_OBJECT_LOCK(mem->am_obj);
610 		m = vm_page_grab(mem->am_obj, 0,
611 		    VM_ALLOC_WIRED | VM_ALLOC_ZERO | VM_ALLOC_RETRY);
612 		VM_OBJECT_UNLOCK(mem->am_obj);
613 		if ((m->flags & PG_ZERO) == 0)
614 			pmap_zero_page(m);
615 		vm_page_lock_queues();
616 		mem->am_physical = VM_PAGE_TO_PHYS(m);
617 		vm_page_wakeup(m);
618 		vm_page_unlock_queues();
619 	} else {
620 		mem->am_physical = 0;
621 	}
622 
623 	mem->am_offset = 0;
624 	mem->am_is_bound = 0;
625 	TAILQ_INSERT_TAIL(&sc->agp.as_memory, mem, am_link);
626 	sc->agp.as_allocated += size;
627 
628 	return mem;
629 }
630 
631 static int
632 agp_i810_free_memory(device_t dev, struct agp_memory *mem)
633 {
634 	struct agp_i810_softc *sc = device_get_softc(dev);
635 
636 	if (mem->am_is_bound)
637 		return EBUSY;
638 
639 	if (mem->am_type == 2) {
640 		/*
641 		 * Unwire the page which we wired in alloc_memory.
642 		 */
643 		vm_page_t m;
644 
645 		VM_OBJECT_LOCK(mem->am_obj);
646 		m = vm_page_lookup(mem->am_obj, 0);
647 		VM_OBJECT_UNLOCK(mem->am_obj);
648 		vm_page_lock_queues();
649 		vm_page_unwire(m, 0);
650 		vm_page_unlock_queues();
651 	}
652 
653 	sc->agp.as_allocated -= mem->am_size;
654 	TAILQ_REMOVE(&sc->agp.as_memory, mem, am_link);
655 	if (mem->am_obj)
656 		vm_object_deallocate(mem->am_obj);
657 	free(mem, M_AGP);
658 	return 0;
659 }
660 
661 static int
662 agp_i810_bind_memory(device_t dev, struct agp_memory *mem,
663 		     vm_offset_t offset)
664 {
665 	struct agp_i810_softc *sc = device_get_softc(dev);
666 	vm_offset_t i;
667 
668 	if (mem->am_type != 1)
669 		return agp_generic_bind_memory(dev, mem, offset);
670 
671 	if ( sc->chiptype != CHIP_I810 )
672 		return EINVAL;
673 
674 	for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE) {
675 		WRITE4(AGP_I810_GTT + (offset >> AGP_PAGE_SHIFT) * 4,
676 		       i | 3);
677 	}
678 
679 	return 0;
680 }
681 
682 static int
683 agp_i810_unbind_memory(device_t dev, struct agp_memory *mem)
684 {
685 	struct agp_i810_softc *sc = device_get_softc(dev);
686 	vm_offset_t i;
687 
688 	if (mem->am_type != 1)
689 		return agp_generic_unbind_memory(dev, mem);
690 
691 	if ( sc->chiptype != CHIP_I810 )
692 		return EINVAL;
693 
694 	for (i = 0; i < mem->am_size; i += AGP_PAGE_SIZE)
695 		WRITE4(AGP_I810_GTT + (i >> AGP_PAGE_SHIFT) * 4, 0);
696 
697 	return 0;
698 }
699 
700 static int
701 agp_i810_print_child(device_t dev, device_t child)
702 {
703 	int retval = 0;
704 
705 	retval += bus_print_child_header(dev, child);
706 	retval += printf(": (child of agp_i810.c)");
707 	retval += bus_print_child_footer(dev, child);
708 
709 	return retval;
710 }
711 
712 static device_method_t agp_i810_methods[] = {
713 	/* Device interface */
714 	DEVMETHOD(device_probe,		agp_i810_probe),
715 	DEVMETHOD(device_attach,	agp_i810_attach),
716 	DEVMETHOD(device_detach,	agp_i810_detach),
717 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
718 	DEVMETHOD(device_suspend,	bus_generic_suspend),
719 	DEVMETHOD(device_resume,	bus_generic_resume),
720 
721 	/* AGP interface */
722 	DEVMETHOD(agp_get_aperture,	agp_i810_get_aperture),
723 	DEVMETHOD(agp_set_aperture,	agp_i810_set_aperture),
724 	DEVMETHOD(agp_bind_page,	agp_i810_bind_page),
725 	DEVMETHOD(agp_unbind_page,	agp_i810_unbind_page),
726 	DEVMETHOD(agp_flush_tlb,	agp_i810_flush_tlb),
727 	DEVMETHOD(agp_enable,		agp_i810_enable),
728 	DEVMETHOD(agp_alloc_memory,	agp_i810_alloc_memory),
729 	DEVMETHOD(agp_free_memory,	agp_i810_free_memory),
730 	DEVMETHOD(agp_bind_memory,	agp_i810_bind_memory),
731 	DEVMETHOD(agp_unbind_memory,	agp_i810_unbind_memory),
732 
733 	/* bus methods */
734 	DEVMETHOD(bus_print_child,	agp_i810_print_child),
735 	DEVMETHOD(bus_alloc_resource,	bus_generic_alloc_resource),
736 	DEVMETHOD(bus_release_resource,	bus_generic_release_resource),
737 	DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
738 	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
739 	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
740 	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
741 	{ 0, 0 }
742 };
743 
744 static driver_t agp_i810_driver = {
745 	"agp",
746 	agp_i810_methods,
747 	sizeof(struct agp_i810_softc),
748 };
749 
750 static devclass_t agp_devclass;
751 
752 DRIVER_MODULE(agp_i810, pci, agp_i810_driver, agp_devclass, 0, 0);
753 MODULE_DEPEND(agp_i810, agp, 1, 1, 1);
754 MODULE_DEPEND(agp_i810, pci, 1, 1, 1);
755